forjar 1.4.2

Rust-native Infrastructure as Code — bare-metal first, BLAKE3 state, provenance tracing
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//! Phase 97 — State Analytics & Capacity Planning: validate commands.

use crate::core::types;
use std::collections::{HashMap, HashSet};
use std::path::Path;

// ============================================================================
// FJ-1038: Resource health correlation
// ============================================================================

/// Identify dependency "hubs" — resources depended on by 3+ others — and warn
/// that failures in shared dependencies will cause correlated failures across
/// all resources that share them.
pub(crate) fn cmd_validate_check_resource_health_correlation(
    file: &Path,
    json: bool,
) -> Result<(), String> {
    let content = std::fs::read_to_string(file).map_err(|e| e.to_string())?;
    let config: types::ForjarConfig =
        serde_yaml_ng::from_str(&content).map_err(|e| e.to_string())?;

    let hubs = find_dependency_hubs(&config);
    let correlated = find_correlated_groups(&config, &hubs);

    if json {
        let hub_items: Vec<String> = hubs
            .iter()
            .map(|(dep, dependents)| {
                let arr: Vec<String> = dependents.iter().map(|d| format!("\"{d}\"")).collect();
                format!(
                    "{{\"hub\":\"{}\",\"dependents\":[{}],\"fan_in\":{}}}",
                    dep,
                    arr.join(","),
                    dependents.len()
                )
            })
            .collect();
        let corr_items: Vec<String> = correlated
            .iter()
            .map(|(dep, resources)| {
                let arr: Vec<String> = resources.iter().map(|r| format!("\"{r}\"")).collect();
                format!(
                    "{{\"shared_dependency\":\"{}\",\"correlated_resources\":[{}]}}",
                    dep,
                    arr.join(",")
                )
            })
            .collect();
        println!(
            "{{\"dependency_hubs\":[{}],\"correlated_failure_groups\":[{}]}}",
            hub_items.join(","),
            corr_items.join(",")
        );
    } else if hubs.is_empty() && correlated.is_empty() {
        println!("No dependency hubs or correlated failure groups detected.");
    } else {
        print_hub_warnings(&hubs);
        print_correlation_warnings(&correlated);
    }
    Ok(())
}

/// Build a map of dependency -> list of resources that depend on it, filtered
/// to only those with a fan-in of 3 or more (dependency "hubs").
fn find_dependency_hubs(config: &types::ForjarConfig) -> Vec<(String, Vec<String>)> {
    let mut dep_to_dependents: HashMap<String, Vec<String>> = HashMap::new();

    for (name, resource) in &config.resources {
        for dep in &resource.depends_on {
            dep_to_dependents
                .entry(dep.clone())
                .or_default()
                .push(name.clone());
        }
    }

    let mut hubs: Vec<(String, Vec<String>)> = dep_to_dependents
        .into_iter()
        .filter(|(_, dependents)| dependents.len() >= 3)
        .collect();

    for (_, dependents) in &mut hubs {
        dependents.sort();
    }
    hubs.sort_by(|a, b| b.1.len().cmp(&a.1.len()).then_with(|| a.0.cmp(&b.0)));
    hubs
}

/// Find groups of resources that share the same depends_on targets, meaning a
/// failure in that shared dependency would cause correlated failures.
fn find_correlated_groups(
    config: &types::ForjarConfig,
    hubs: &[(String, Vec<String>)],
) -> Vec<(String, Vec<String>)> {
    let hub_set: HashSet<&String> = hubs.iter().map(|(h, _)| h).collect();
    let mut dep_to_resources: HashMap<&String, Vec<String>> = HashMap::new();

    for (name, resource) in &config.resources {
        for dep in &resource.depends_on {
            if hub_set.contains(dep) {
                dep_to_resources.entry(dep).or_default().push(name.clone());
            }
        }
    }

    let mut groups: Vec<(String, Vec<String>)> = dep_to_resources
        .into_iter()
        .filter(|(_, resources)| resources.len() >= 2)
        .map(|(dep, mut resources)| {
            resources.sort();
            resources.dedup();
            (dep.clone(), resources)
        })
        .collect();

    groups.sort_by(|a, b| b.1.len().cmp(&a.1.len()).then_with(|| a.0.cmp(&b.0)));
    groups
}

fn print_hub_warnings(hubs: &[(String, Vec<String>)]) {
    for (dep, dependents) in hubs {
        println!(
            "warning: '{}' is a dependency hub (fan-in {}) — depended on by: {}",
            dep,
            dependents.len(),
            dependents.join(", ")
        );
    }
}

fn print_correlation_warnings(correlated: &[(String, Vec<String>)]) {
    for (dep, resources) in correlated {
        println!(
            "warning: failure in '{}' would cause correlated failures in: {}",
            dep,
            resources.join(", ")
        );
    }
}

// ============================================================================
// FJ-1041: Dependency optimization
// ============================================================================

/// Find redundant (transitive) edges in the dependency graph. If A depends on
/// B and C, and B depends on C, then A -> C is redundant because it is already
/// implied through B -> C. Reports these as optimization suggestions.
pub(crate) fn cmd_validate_check_dependency_optimization(
    file: &Path,
    json: bool,
) -> Result<(), String> {
    let content = std::fs::read_to_string(file).map_err(|e| e.to_string())?;
    let config: types::ForjarConfig =
        serde_yaml_ng::from_str(&content).map_err(|e| e.to_string())?;

    let redundant = find_redundant_edges(&config);

    if json {
        let items: Vec<String> = redundant
            .iter()
            .map(|(resource, edge, via)| {
                format!(
                    "{{\"resource\":\"{resource}\",\"redundant_dep\":\"{edge}\",\"implied_via\":\"{via}\"}}"
                )
            })
            .collect();
        println!("{{\"redundant_edges\":[{}]}}", items.join(","));
    } else if redundant.is_empty() {
        println!("No redundant dependency edges detected.");
    } else {
        for (resource, edge, via) in &redundant {
            println!(
                "suggestion: {resource}'s dependency on '{edge}' is redundant — already implied through '{via}'"
            );
        }
    }
    Ok(())
}

/// Compute the transitive closure of dependencies for a single resource,
/// excluding its own direct edges. Returns the set of all resources reachable
/// through at least one intermediate step.
fn transitive_deps_via(
    resource: &str,
    direct_deps: &[String],
    dep_map: &HashMap<&str, &[String]>,
) -> HashMap<String, String> {
    // For each direct dep D of `resource`, BFS/DFS from D to find everything
    // D can reach. Those are "implied via D".
    let mut implied: HashMap<String, String> = HashMap::new();
    for direct in direct_deps {
        let reachable = reachable_from(direct, dep_map);
        for r in reachable {
            if r != resource && !implied.contains_key(&r) {
                implied.insert(r, direct.clone());
            }
        }
    }
    implied
}

/// BFS from `start` collecting all transitively reachable resources.
fn reachable_from(start: &str, dep_map: &HashMap<&str, &[String]>) -> HashSet<String> {
    let mut visited = HashSet::new();
    let mut queue = std::collections::VecDeque::new();

    if let Some(deps) = dep_map.get(start) {
        for d in *deps {
            queue.push_back(d.as_str());
        }
    }

    while let Some(current) = queue.pop_front() {
        if !visited.insert(current.to_string()) {
            continue;
        }
        if let Some(deps) = dep_map.get(current) {
            for d in *deps {
                if !visited.contains(d.as_str()) {
                    queue.push_back(d.as_str());
                }
            }
        }
    }
    visited
}

/// Returns `(resource_name, redundant_edge, implied_via)` for each transitive
/// dependency edge that can be safely removed.
fn find_redundant_edges(config: &types::ForjarConfig) -> Vec<(String, String, String)> {
    // Build dep_map: resource_name -> &[depends_on]
    let dep_map: HashMap<&str, &[String]> = config
        .resources
        .iter()
        .map(|(name, res)| (name.as_str(), res.depends_on.as_slice()))
        .collect();

    let mut redundant = Vec::new();

    for (name, resource) in &config.resources {
        if resource.depends_on.len() < 2 {
            continue;
        }
        let implied = transitive_deps_via(name, &resource.depends_on, &dep_map);
        for direct in &resource.depends_on {
            if let Some(via) = implied.get(direct.as_str()) {
                redundant.push((name.clone(), direct.clone(), via.clone()));
            }
        }
    }

    redundant.sort_by(|a, b| a.0.cmp(&b.0).then_with(|| a.1.cmp(&b.1)));
    redundant
}

// ============================================================================
// FJ-1044: Resource consolidation opportunities
// ============================================================================

/// Find resources of the same type on different machines with similar names
/// (edit distance <= 2) or identical content. Suggest these as consolidation
/// candidates — they may be duplicated work that could use `machine: [a, b]`.
pub(crate) fn cmd_validate_check_resource_consolidation_opportunities(
    file: &Path,
    json: bool,
) -> Result<(), String> {
    let content = std::fs::read_to_string(file).map_err(|e| e.to_string())?;
    let config: types::ForjarConfig =
        serde_yaml_ng::from_str(&content).map_err(|e| e.to_string())?;

    let opportunities = find_consolidation_opportunities(&config);

    if json {
        let items: Vec<String> = opportunities
            .iter()
            .map(|opp| {
                format!(
                    "{{\"resource_a\":\"{}\",\"resource_b\":\"{}\",\"reason\":\"{}\",\"resource_type\":\"{}\"}}",
                    opp.name_a, opp.name_b, opp.reason, opp.resource_type
                )
            })
            .collect();
        println!("{{\"consolidation_opportunities\":[{}]}}", items.join(","));
    } else if opportunities.is_empty() {
        println!("No resource consolidation opportunities detected.");
    } else {
        for opp in &opportunities {
            println!(
                "suggestion: {} and {} ({}) could be consolidated — {}",
                opp.name_a, opp.name_b, opp.resource_type, opp.reason
            );
        }
    }
    Ok(())
}

/// A consolidation opportunity between two resources.
struct ConsolidationOpportunity {
    name_a: String,
    name_b: String,
    resource_type: String,
    reason: String,
}

/// Find consolidation opportunities across resources of the same type on
/// different machines.
fn find_consolidation_opportunities(config: &types::ForjarConfig) -> Vec<ConsolidationOpportunity> {
    let entries: Vec<(&String, &types::Resource)> = config.resources.iter().collect();
    let mut opportunities = Vec::new();
    let mut seen = HashSet::new();

    for i in 0..entries.len() {
        for j in (i + 1)..entries.len() {
            let (name_a, res_a) = entries[i];
            let (name_b, res_b) = entries[j];

            if res_a.resource_type != res_b.resource_type {
                continue;
            }
            if !on_different_machines(res_a, res_b) {
                continue;
            }

            let pair_key = format!("{name_a}:{name_b}");
            if seen.contains(&pair_key) {
                continue;
            }

            if let Some(reason) = check_consolidation_reason(name_a, res_a, name_b, res_b) {
                seen.insert(pair_key);
                opportunities.push(ConsolidationOpportunity {
                    name_a: name_a.clone(),
                    name_b: name_b.clone(),
                    resource_type: res_a.resource_type.to_string(),
                    reason,
                });
            }
        }
    }

    opportunities.sort_by(|a, b| {
        a.name_a
            .cmp(&b.name_a)
            .then_with(|| a.name_b.cmp(&b.name_b))
    });
    opportunities
}

/// Check whether two resources target different machines.
fn on_different_machines(a: &types::Resource, b: &types::Resource) -> bool {
    let machines_a: HashSet<&str> = a.machine.iter().collect();
    let machines_b: HashSet<&str> = b.machine.iter().collect();
    machines_a.is_disjoint(&machines_b)
}

/// Return a consolidation reason if two same-type resources on different
/// machines have similar names (edit distance <= 2) or identical content.
fn check_consolidation_reason(
    name_a: &str,
    res_a: &types::Resource,
    name_b: &str,
    res_b: &types::Resource,
) -> Option<String> {
    // Check identical content first (stronger signal).
    if has_identical_content(res_a, res_b) {
        return Some("identical content on different machines".to_string());
    }

    // Check similar names (edit distance <= 2).
    let distance = levenshtein(name_a, name_b);
    if distance <= 2 && distance > 0 {
        return Some(format!(
            "similar names (edit distance {distance}), may be duplicates"
        ));
    }

    None
}

/// Check whether two resources have identical non-empty content.
fn has_identical_content(a: &types::Resource, b: &types::Resource) -> bool {
    match (&a.content, &b.content) {
        (Some(ca), Some(cb)) if !ca.is_empty() && !cb.is_empty() => ca == cb,
        _ => false,
    }
}

/// Compute the Levenshtein edit distance between two strings.
///
/// Uses a single-row dynamic programming approach (O(min(m,n)) space).
pub(crate) fn levenshtein(a: &str, b: &str) -> usize {
    let a_chars: Vec<char> = a.chars().collect();
    let b_chars: Vec<char> = b.chars().collect();

    // Ensure a is the shorter string for space efficiency.
    if a_chars.len() > b_chars.len() {
        return levenshtein_chars(&b_chars, &a_chars);
    }
    levenshtein_chars(&a_chars, &b_chars)
}

/// Inner Levenshtein computation over char slices where `a.len() <= b.len()`.
fn levenshtein_chars(a: &[char], b: &[char]) -> usize {
    let n = a.len();
    let m = b.len();

    let mut prev: Vec<usize> = (0..=n).collect();
    let mut curr = vec![0usize; n + 1];

    for j in 1..=m {
        curr[0] = j;
        for i in 1..=n {
            let cost = if a[i - 1] == b[j - 1] { 0 } else { 1 };
            curr[i] = (prev[i] + 1).min(curr[i - 1] + 1).min(prev[i - 1] + cost);
        }
        std::mem::swap(&mut prev, &mut curr);
    }
    prev[n]
}