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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//! FJ-2600/FJ-2603: Convergence verification with sandbox integration.
//!
//! Bridges convergence property testing to real sandbox execution.
//! Implements apply → verify → re-apply → verify idempotency cycle
//! in isolated sandboxes (pepita namespace or container).

use crate::core::store::sandbox::SandboxLevel;
use crate::core::types::SandboxBackend;
use serde::{Deserialize, Serialize};
use std::fmt;

/// Result of a convergence test for a single resource.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConvergenceResult {
    /// Resource ID tested.
    pub resource_id: String,
    /// Resource type.
    pub resource_type: String,
    /// Whether the first apply converged to desired state.
    pub converged: bool,
    /// Whether the second apply was a no-op (idempotency).
    pub idempotent: bool,
    /// Whether the state was preserved after other resources applied.
    pub preserved: bool,
    /// Duration of the full test in milliseconds.
    pub duration_ms: u64,
    /// Error message if the test failed.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub error: Option<String>,
}

impl ConvergenceResult {
    /// Whether all convergence properties passed.
    pub fn passed(&self) -> bool {
        self.converged && self.idempotent && self.preserved && self.error.is_none()
    }
}

impl fmt::Display for ConvergenceResult {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let status = if self.passed() { "PASS" } else { "FAIL" };
        write!(
            f,
            "[{status}] {}/{}: converge={} idem={} preserve={} ({}ms)",
            self.resource_id,
            self.resource_type,
            self.converged,
            self.idempotent,
            self.preserved,
            self.duration_ms,
        )
    }
}

/// Configuration for convergence testing.
#[derive(Debug, Clone)]
pub struct ConvergenceTestConfig {
    /// Sandbox backend to use (pepita, container, chroot).
    pub backend: SandboxBackend,
    /// Sandbox isolation level.
    pub level: SandboxLevel,
    /// Whether to test pairwise preservation.
    pub test_pairs: bool,
    /// Maximum number of parallel sandboxes.
    pub parallelism: usize,
}

impl Default for ConvergenceTestConfig {
    fn default() -> Self {
        Self {
            backend: SandboxBackend::Pepita,
            level: SandboxLevel::Minimal,
            test_pairs: false,
            parallelism: 4,
        }
    }
}

/// Execution mode for the convergence runner.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RunnerMode {
    /// Simulated execution (hash-based, no real sandbox).
    Simulated,
    /// Real sandbox execution via backend.
    Sandbox,
}

impl fmt::Display for RunnerMode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Simulated => write!(f, "simulated"),
            Self::Sandbox => write!(f, "sandbox"),
        }
    }
}

/// Check if a sandbox backend is available on this system.
pub fn backend_available(backend: SandboxBackend) -> bool {
    match backend {
        SandboxBackend::Pepita => std::path::Path::new("/usr/local/bin/pepita").exists(),
        SandboxBackend::Container => {
            std::process::Command::new("docker")
                .arg("--version")
                .output()
                .map(|o| o.status.success())
                .unwrap_or(false)
                || std::process::Command::new("podman")
                    .arg("--version")
                    .output()
                    .map(|o| o.status.success())
                    .unwrap_or(false)
        }
        SandboxBackend::Chroot => {
            // chroot requires root — check via /proc
            std::fs::read_to_string("/proc/self/status")
                .map(|s| s.lines().any(|l| l.starts_with("Uid:\t0\t")))
                .unwrap_or(false)
        }
    }
}

/// Determine the runner mode based on backend availability.
pub fn resolve_mode(backend: SandboxBackend) -> RunnerMode {
    if backend_available(backend) {
        RunnerMode::Sandbox
    } else {
        RunnerMode::Simulated
    }
}

/// A resource to test for convergence.
#[derive(Debug, Clone)]
pub struct ConvergenceTarget {
    /// Resource ID.
    pub resource_id: String,
    /// Resource type.
    pub resource_type: String,
    /// Apply script (generated by codegen).
    pub apply_script: String,
    /// State query script (to check post-apply state).
    pub state_query_script: String,
    /// Expected state hash after convergence.
    pub expected_hash: String,
}

/// Run convergence test with mode dispatch.
///
/// In Sandbox mode with container backend available, runs inside a real
/// ephemeral container. Otherwise falls back to simulated (hash-based).
pub fn run_convergence_test_dispatch(
    target: &ConvergenceTarget,
    backend: SandboxBackend,
) -> ConvergenceResult {
    let mode = resolve_mode(backend);
    match (mode, backend) {
        (RunnerMode::Sandbox, SandboxBackend::Container) => {
            super::convergence_container::run_convergence_test_container(target)
        }
        _ => run_convergence_test(target),
    }
}

/// Run convergence test for a single resource in a local tempdir sandbox.
///
/// Algorithm (6 steps from spec):
/// 1. Generate apply script via codegen
/// 2. Generate state_query script
/// 3. Execute apply in sandbox
/// 4. Query state — verify it matches desired
/// 5. Execute apply again — verify it's a no-op
/// 6. Query state again — verify unchanged
pub fn run_convergence_test(target: &ConvergenceTarget) -> ConvergenceResult {
    let start = std::time::Instant::now();

    // Create isolated sandbox directory
    let sandbox_dir = std::env::temp_dir().join(format!(
        "forjar-conv-{}-{:x}",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_nanos()
    ));
    let _ = std::fs::create_dir_all(&sandbox_dir);

    let result = run_convergence_in_sandbox(target, &sandbox_dir, start);

    // Cleanup sandbox
    let _ = std::fs::remove_dir_all(&sandbox_dir);
    result
}

fn run_convergence_in_sandbox(
    target: &ConvergenceTarget,
    sandbox_dir: &std::path::Path,
    start: std::time::Instant,
) -> ConvergenceResult {
    // Step 3: First apply
    let first_apply = local_apply(&target.apply_script, sandbox_dir);
    if let Err(e) = first_apply {
        return ConvergenceResult {
            resource_id: target.resource_id.clone(),
            resource_type: target.resource_type.clone(),
            converged: false,
            idempotent: false,
            preserved: false,
            duration_ms: start.elapsed().as_millis() as u64,
            error: Some(e),
        };
    }

    // Step 4: Verify convergence
    let state_after_first = local_state_query(&target.state_query_script, sandbox_dir);
    let converged = if target.expected_hash.is_empty() {
        // No expected hash: convergence means the script ran successfully
        state_after_first.is_ok()
    } else {
        state_after_first
            .as_ref()
            .map(|h| h == &target.expected_hash)
            .unwrap_or(false)
    };

    // Step 5: Second apply (should be no-op)
    let second_apply = local_apply(&target.apply_script, sandbox_dir);
    let idempotent = second_apply.is_ok();

    // Step 6: Verify state unchanged
    let state_after_second = local_state_query(&target.state_query_script, sandbox_dir);
    let preserved = match (&state_after_first, &state_after_second) {
        (Ok(h1), Ok(h2)) => h1 == h2,
        _ => false,
    };

    ConvergenceResult {
        resource_id: target.resource_id.clone(),
        resource_type: target.resource_type.clone(),
        converged,
        idempotent,
        preserved,
        duration_ms: start.elapsed().as_millis() as u64,
        error: None,
    }
}

/// Run convergence tests in parallel sandboxes.
///
/// Each target gets its own sandbox. Results are collected and returned.
/// Uses simulated mode by default (call `run_convergence_parallel_with_backend`
/// for sandbox-aware dispatch).
pub fn run_convergence_parallel(
    targets: Vec<ConvergenceTarget>,
    parallelism: usize,
) -> Vec<ConvergenceResult> {
    run_convergence_parallel_with_backend(targets, parallelism, SandboxBackend::Pepita)
}

/// Run convergence tests in parallel with explicit backend selection.
///
/// When the backend is available, tests run in real sandboxes.
/// Otherwise falls back to simulated (hash-based) mode.
pub fn run_convergence_parallel_with_backend(
    targets: Vec<ConvergenceTarget>,
    parallelism: usize,
    backend: SandboxBackend,
) -> Vec<ConvergenceResult> {
    if targets.is_empty() {
        return Vec::new();
    }

    let par = parallelism.max(1);
    let results = std::sync::Mutex::new(Vec::with_capacity(targets.len()));
    let chunks: Vec<_> = targets.chunks(par).collect();

    for chunk in chunks {
        std::thread::scope(|s| {
            let handles: Vec<_> = chunk
                .iter()
                .map(|target| s.spawn(|| run_convergence_test_dispatch(target, backend)))
                .collect();

            for handle in handles {
                if let Ok(result) = handle.join() {
                    results
                        .lock()
                        .unwrap_or_else(|e| e.into_inner())
                        .push(result);
                }
            }
        });
    }

    results.into_inner().unwrap_or_else(|e| e.into_inner())
}

/// Summary of convergence test results.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ConvergenceSummary {
    /// Total resources tested.
    pub total: usize,
    /// Resources that passed all properties.
    pub passed: usize,
    /// Resources that failed convergence.
    pub convergence_failures: usize,
    /// Resources that failed idempotency.
    pub idempotency_failures: usize,
    /// Resources that failed preservation.
    pub preservation_failures: usize,
}

impl ConvergenceSummary {
    /// Build summary from results.
    pub fn from_results(results: &[ConvergenceResult]) -> Self {
        Self {
            total: results.len(),
            passed: results.iter().filter(|r| r.passed()).count(),
            convergence_failures: results.iter().filter(|r| !r.converged).count(),
            idempotency_failures: results.iter().filter(|r| !r.idempotent).count(),
            preservation_failures: results.iter().filter(|r| !r.preserved).count(),
        }
    }

    /// Pass rate as percentage.
    pub fn pass_rate(&self) -> f64 {
        if self.total == 0 {
            return 100.0;
        }
        (self.passed as f64 / self.total as f64) * 100.0
    }
}

impl fmt::Display for ConvergenceSummary {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "Convergence: {}/{} passed ({:.0}%)",
            self.passed,
            self.total,
            self.pass_rate(),
        )?;
        if self.convergence_failures > 0 {
            write!(f, ", {} convergence failures", self.convergence_failures)?;
        }
        if self.idempotency_failures > 0 {
            write!(f, ", {} idempotency failures", self.idempotency_failures)?;
        }
        if self.preservation_failures > 0 {
            write!(f, ", {} preservation failures", self.preservation_failures)?;
        }
        Ok(())
    }
}

/// Format convergence results for CLI output.
pub fn format_convergence_report(results: &[ConvergenceResult]) -> String {
    let mut out = String::new();
    let summary = ConvergenceSummary::from_results(results);

    out.push_str(&format!("{summary}\n"));
    out.push_str("================================\n");

    for r in results {
        out.push_str(&format!("  {r}\n"));
    }

    if summary.passed < summary.total {
        out.push_str("\nFailures:\n");
        for r in results.iter().filter(|r| !r.passed()) {
            if let Some(err) = &r.error {
                out.push_str(&format!("  {}: {err}\n", r.resource_id));
            } else {
                let mut reasons = Vec::new();
                if !r.converged {
                    reasons.push("convergence");
                }
                if !r.idempotent {
                    reasons.push("idempotency");
                }
                if !r.preserved {
                    reasons.push("preservation");
                }
                out.push_str(&format!(
                    "  {}: failed {}\n",
                    r.resource_id,
                    reasons.join(", "),
                ));
            }
        }
    }

    out
}

/// Patterns that indicate a script would modify system state outside the sandbox.
/// These MUST NEVER run on the host — only in containers or remote sandboxes.
const UNSAFE_PATTERNS: &[&str] = &[
    "systemctl ",
    "apt-get ",
    "apt ",
    "dpkg ",
    "yum ",
    "dnf ",
    "pacman ",
    "mount ",
    "umount ",
    "pkill ",
    "kill ",
    "shutdown ",
    "reboot ",
    "rm -rf /",
    "dd if=",
    "mkfs",
    "fdisk",
];

/// Check if a script is safe to run locally (doesn't modify system state).
fn is_script_safe_for_local(script: &str) -> bool {
    !UNSAFE_PATTERNS.iter().any(|p| script.contains(p))
}

/// Execute a script locally in a sandbox directory, returning stdout hash.
///
/// SAFETY: Rejects scripts containing system-modifying commands
/// (systemctl, apt-get, pkill, etc.) to prevent host damage.
fn local_apply(script: &str, sandbox_dir: &std::path::Path) -> Result<String, String> {
    if script.is_empty() {
        return Err("empty apply script".into());
    }
    if !is_script_safe_for_local(script) {
        return Err("script contains system commands (requires container backend)".into());
    }
    let output = std::process::Command::new("bash")
        .args(["-euo", "pipefail", "-c", script])
        .current_dir(sandbox_dir)
        .env("FORJAR_SANDBOX", sandbox_dir)
        .output()
        .map_err(|e| format!("local exec: {e}"))?;

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        return Err(format!(
            "exit {}: {}",
            output.status.code().unwrap_or(-1),
            stderr.trim()
        ));
    }
    let stdout = String::from_utf8_lossy(&output.stdout);
    let refs = [stdout.as_ref()];
    Ok(crate::tripwire::hasher::composite_hash(&refs))
}

/// Execute a state query script locally, returning stdout hash.
fn local_state_query(script: &str, sandbox_dir: &std::path::Path) -> Result<String, String> {
    if script.is_empty() {
        return Err("empty state query script".into());
    }
    let output = std::process::Command::new("bash")
        .args(["-euo", "pipefail", "-c", script])
        .current_dir(sandbox_dir)
        .env("FORJAR_SANDBOX", sandbox_dir)
        .output()
        .map_err(|e| format!("state query exec: {e}"))?;

    let stdout = String::from_utf8_lossy(&output.stdout);
    let refs = [stdout.as_ref()];
    Ok(crate::tripwire::hasher::composite_hash(&refs))
}

// Container sandbox execution lives in convergence_container.rs