|
| 1 | +//! FJ-1424: Cross-machine resource dependency analysis. |
| 2 | +//! |
| 3 | +//! `forjar cross-deps` analyzes cross-machine resource dependencies in a config. |
| 4 | +//! Resources on machine A can depend on resources on machine B via depends_on. |
| 5 | +//! This command validates, visualizes, and reports cross-machine dependency chains. |
| 6 | +
|
| 7 | +use super::helpers::*; |
| 8 | +use std::collections::BTreeMap; |
| 9 | +use std::path::Path; |
| 10 | + |
| 11 | +/// A cross-machine dependency edge. |
| 12 | +#[derive(Debug, Clone, serde::Serialize)] |
| 13 | +pub struct CrossDep { |
| 14 | + pub from_resource: String, |
| 15 | + pub from_machine: String, |
| 16 | + pub to_resource: String, |
| 17 | + pub to_machine: String, |
| 18 | + pub dep_type: String, |
| 19 | +} |
| 20 | + |
| 21 | +/// Cross-machine dependency report. |
| 22 | +#[derive(Debug, serde::Serialize)] |
| 23 | +pub struct CrossDepReport { |
| 24 | + pub edges: Vec<CrossDep>, |
| 25 | + pub total_resources: usize, |
| 26 | + pub cross_machine_deps: usize, |
| 27 | + pub same_machine_deps: usize, |
| 28 | + pub machines_involved: Vec<String>, |
| 29 | + pub execution_waves: Vec<Vec<String>>, |
| 30 | +} |
| 31 | + |
| 32 | +/// Analyze cross-machine dependencies. |
| 33 | +pub fn cmd_cross_deps(file: &Path, json: bool) -> Result<(), String> { |
| 34 | + let config = parse_and_validate(file)?; |
| 35 | + let res_machine = build_resource_machine_map(&config); |
| 36 | + let (edges, cross_count, same_count, machines) = analyze_deps(&config, &res_machine); |
| 37 | + let waves = build_execution_waves(&config); |
| 38 | + |
| 39 | + let report = CrossDepReport { |
| 40 | + edges, |
| 41 | + total_resources: config.resources.len(), |
| 42 | + cross_machine_deps: cross_count, |
| 43 | + same_machine_deps: same_count, |
| 44 | + machines_involved: machines.into_iter().collect(), |
| 45 | + execution_waves: waves, |
| 46 | + }; |
| 47 | + |
| 48 | + if json { |
| 49 | + let output = |
| 50 | + serde_json::to_string_pretty(&report).map_err(|e| format!("JSON error: {e}"))?; |
| 51 | + println!("{output}"); |
| 52 | + } else { |
| 53 | + print_cross_dep_report(&report); |
| 54 | + } |
| 55 | + |
| 56 | + Ok(()) |
| 57 | +} |
| 58 | + |
| 59 | +fn build_resource_machine_map( |
| 60 | + config: &crate::core::types::ForjarConfig, |
| 61 | +) -> BTreeMap<String, Vec<String>> { |
| 62 | + config |
| 63 | + .resources |
| 64 | + .iter() |
| 65 | + .map(|(id, res)| (id.clone(), res.machine.to_vec())) |
| 66 | + .collect() |
| 67 | +} |
| 68 | + |
| 69 | +fn analyze_deps( |
| 70 | + config: &crate::core::types::ForjarConfig, |
| 71 | + res_machine: &BTreeMap<String, Vec<String>>, |
| 72 | +) -> (Vec<CrossDep>, usize, usize, std::collections::BTreeSet<String>) { |
| 73 | + let mut edges = Vec::new(); |
| 74 | + let mut cross_count = 0usize; |
| 75 | + let mut same_count = 0usize; |
| 76 | + let mut machines = std::collections::BTreeSet::new(); |
| 77 | + |
| 78 | + for (id, res) in &config.resources { |
| 79 | + let my_machines = res.machine.to_vec(); |
| 80 | + for m in &my_machines { |
| 81 | + machines.insert(m.clone()); |
| 82 | + } |
| 83 | + for dep in &res.depends_on { |
| 84 | + let (c, s) = classify_dep(id, &my_machines, dep, res_machine, &mut edges); |
| 85 | + cross_count += c; |
| 86 | + same_count += s; |
| 87 | + } |
| 88 | + } |
| 89 | + (edges, cross_count, same_count, machines) |
| 90 | +} |
| 91 | + |
| 92 | +fn classify_dep( |
| 93 | + id: &str, |
| 94 | + my_machines: &[String], |
| 95 | + dep: &str, |
| 96 | + res_machine: &BTreeMap<String, Vec<String>>, |
| 97 | + edges: &mut Vec<CrossDep>, |
| 98 | +) -> (usize, usize) { |
| 99 | + let Some(dep_machines) = res_machine.get(dep) else { |
| 100 | + return (0, 0); |
| 101 | + }; |
| 102 | + let is_cross = !my_machines.iter().all(|m| dep_machines.contains(m)); |
| 103 | + if !is_cross { |
| 104 | + return (0, 1); |
| 105 | + } |
| 106 | + for from_m in my_machines { |
| 107 | + for to_m in dep_machines.iter().filter(|t| *t != from_m) { |
| 108 | + edges.push(CrossDep { |
| 109 | + from_resource: id.to_string(), |
| 110 | + from_machine: from_m.clone(), |
| 111 | + to_resource: dep.to_string(), |
| 112 | + to_machine: to_m.clone(), |
| 113 | + dep_type: "cross-machine".to_string(), |
| 114 | + }); |
| 115 | + } |
| 116 | + } |
| 117 | + (1, 0) |
| 118 | +} |
| 119 | + |
| 120 | +fn build_execution_waves(config: &crate::core::types::ForjarConfig) -> Vec<Vec<String>> { |
| 121 | + let mut waves: Vec<Vec<String>> = Vec::new(); |
| 122 | + let mut placed: std::collections::BTreeSet<String> = std::collections::BTreeSet::new(); |
| 123 | + |
| 124 | + // Simple layered topological ordering |
| 125 | + let ids: Vec<String> = config.resources.keys().cloned().collect(); |
| 126 | + let mut remaining: Vec<String> = ids; |
| 127 | + let mut iteration = 0; |
| 128 | + |
| 129 | + while !remaining.is_empty() && iteration < 100 { |
| 130 | + let mut wave = Vec::new(); |
| 131 | + let mut still_remaining = Vec::new(); |
| 132 | + |
| 133 | + for id in &remaining { |
| 134 | + if let Some(res) = config.resources.get(id) { |
| 135 | + let deps_met = res.depends_on.iter().all(|d| placed.contains(d.as_str())); |
| 136 | + if deps_met { |
| 137 | + wave.push(id.clone()); |
| 138 | + } else { |
| 139 | + still_remaining.push(id.clone()); |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + if wave.is_empty() { |
| 145 | + // Circular dependency or unreachable — dump remaining |
| 146 | + waves.push(still_remaining); |
| 147 | + break; |
| 148 | + } |
| 149 | + |
| 150 | + for id in &wave { |
| 151 | + placed.insert(id.clone()); |
| 152 | + } |
| 153 | + waves.push(wave); |
| 154 | + remaining = still_remaining; |
| 155 | + iteration += 1; |
| 156 | + } |
| 157 | + |
| 158 | + waves |
| 159 | +} |
| 160 | + |
| 161 | +fn print_cross_dep_report(report: &CrossDepReport) { |
| 162 | + println!("Cross-Machine Dependency Report"); |
| 163 | + println!("==============================="); |
| 164 | + println!("Resources: {}", report.total_resources); |
| 165 | + println!("Cross-machine deps: {}", report.cross_machine_deps); |
| 166 | + println!("Same-machine deps: {}", report.same_machine_deps); |
| 167 | + println!("Machines: {}", report.machines_involved.join(", ")); |
| 168 | + println!(); |
| 169 | + |
| 170 | + if !report.edges.is_empty() { |
| 171 | + println!("Cross-Machine Edges:"); |
| 172 | + for e in &report.edges { |
| 173 | + println!( |
| 174 | + " {} ({}) -> {} ({})", |
| 175 | + e.from_resource, e.from_machine, e.to_resource, e.to_machine |
| 176 | + ); |
| 177 | + } |
| 178 | + println!(); |
| 179 | + } |
| 180 | + |
| 181 | + println!("Execution Waves:"); |
| 182 | + for (i, wave) in report.execution_waves.iter().enumerate() { |
| 183 | + println!(" Wave {i}: {}", wave.join(", ")); |
| 184 | + } |
| 185 | +} |
| 186 | + |
0 commit comments