bashrs 6.66.0

Rust-to-Shell transpiler for deterministic bootstrap scripts
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
//! Replay verification for determinism checking
//!
//! Verifies determinism by running scripts multiple times and comparing outputs.

use super::determinism::{OutputDifference, ReplayResult, RunOutput};

/// Verifies determinism by running scripts multiple times and comparing outputs
#[derive(Debug, Clone)]
pub struct ReplayVerifier {
    /// Number of replay runs to perform (default: 2, min: 2)
    replay_count: usize,
}

impl ReplayVerifier {
    /// Create a new replay verifier with default settings
    pub fn new() -> Self {
        Self { replay_count: 2 }
    }

    /// Set number of replay runs (min: 2)
    pub fn with_replay_count(mut self, count: usize) -> Self {
        self.replay_count = count.max(2);
        self
    }

    /// Verify determinism by running script multiple times
    pub fn verify(&self, script: &str) -> ReplayResult {
        let mut runs = Vec::new();

        // Run script multiple times
        for run_number in 1..=self.replay_count {
            let mut output = Self::execute_script(script);
            output.run_number = run_number;
            runs.push(output);
        }

        // Compare outputs between first two runs
        let differences = if runs.len() >= 2 {
            if let (Some(run0), Some(run1)) = (runs.first(), runs.get(1)) {
                Self::find_differences(run0, run1)
            } else {
                Vec::new()
            }
        } else {
            Vec::new()
        };

        let is_deterministic = differences.is_empty();

        ReplayResult {
            is_deterministic,
            runs,
            differences,
        }
    }

    /// Execute bash script and capture output
    pub(crate) fn execute_script(script: &str) -> RunOutput {
        use std::process::Command;

        match Command::new("bash").arg("-c").arg(script).output() {
            Ok(output) => RunOutput {
                run_number: 0, // Will be set by caller
                stdout: String::from_utf8_lossy(&output.stdout).to_string(),
                stderr: String::from_utf8_lossy(&output.stderr).to_string(),
                exit_code: output.status.code().unwrap_or(-1),
            },
            Err(e) => RunOutput {
                run_number: 0,
                stdout: String::new(),
                stderr: format!("Failed to execute bash script: {}", e),
                exit_code: -1,
            },
        }
    }

    /// Find differences between two runs
    pub(crate) fn find_differences(run1: &RunOutput, run2: &RunOutput) -> Vec<OutputDifference> {
        let mut differences = Vec::new();

        // Compare stdout line by line
        let lines1: Vec<&str> = run1.stdout.lines().collect();
        let lines2: Vec<&str> = run2.stdout.lines().collect();

        let max_lines = lines1.len().max(lines2.len());
        for i in 0..max_lines {
            let line1 = lines1.get(i).unwrap_or(&"");
            let line2 = lines2.get(i).unwrap_or(&"");

            if line1 != line2 {
                differences.push(OutputDifference {
                    line: i + 1, // 1-indexed
                    run1: line1.to_string(),
                    run2: line2.to_string(),
                });
            }
        }

        differences
    }
}

impl Default for ReplayVerifier {
    fn default() -> Self {
        Self::new()
    }
}

// ===== REPL-011-003: DIFF EXPLANATION =====

/// Format replay verification differences for display
///
/// Takes a vector of OutputDifference and formats them in a human-friendly way.
/// Shows line numbers and the actual output from each run.
///
/// # Examples
///
/// ```
/// use bashrs::repl::determinism::OutputDifference;
/// use bashrs::repl::determinism_replay::format_replay_diff;
///
/// let differences = vec![
///     OutputDifference {
///         line: 1,
///         run1: "Random: 12345".to_string(),
///         run2: "Random: 67890".to_string(),
///     },
/// ];
///
/// let formatted = format_replay_diff(&differences);
/// assert!(formatted.contains("Line 1:"));
/// ```
pub fn format_replay_diff(differences: &[OutputDifference]) -> String {
    if differences.is_empty() {
        return String::from("✓ No differences detected - script is deterministic");
    }

    let mut output = String::new();
    output.push_str("❌ Non-deterministic output detected!\n\n");
    output.push_str(&format!("Found {} difference(s):\n\n", differences.len()));

    for diff in differences {
        output.push_str(&format!("Line {}:\n", diff.line));
        output.push_str(&format!("  Run 1: {}\n", diff.run1));
        output.push_str(&format!("  Run 2: {}\n", diff.run2));
        output.push('\n');
    }

    output
}

impl ReplayResult {
    /// Format replay result for display
    ///
    /// Shows determinism status, run count, exit codes, and any differences.
    pub fn format_result(&self) -> String {
        let mut output = String::new();

        // Show determinism status
        if self.is_deterministic {
            output.push_str("✓ Script is deterministic\n");
        } else {
            output.push_str("❌ Script is non-deterministic\n");
        }

        // Show run count
        output.push_str(&format!("\nRuns: {}\n", self.runs.len()));

        // Show exit codes
        output.push_str("Exit codes: ");
        for run in &self.runs {
            output.push_str(&format!("{} ", run.exit_code));
        }
        output.push('\n');

        // Show differences if any
        if !self.differences.is_empty() {
            output.push('\n');
            output.push_str(&format_replay_diff(&self.differences));
        }

        output
    }
}

// ===== TESTS =====

#[cfg(test)]
mod replay_tests {
    use super::*;

    // ===== REPL-011-002: REPLAY VERIFICATION TESTS =====

    /// Test: REPL-011-002-001 - Deterministic script verification
    #[test]
    fn test_REPL_011_002_deterministic_script() {
        // ARRANGE: Simple deterministic script
        let script = r#"
echo "line1"
echo "line2"
echo "line3"
        "#;
        let verifier = ReplayVerifier::new();

        // ACT: Verify determinism
        let result = verifier.verify(script);

        // ASSERT: Should be deterministic
        assert!(
            result.is_deterministic,
            "Simple script should be deterministic"
        );
        assert_eq!(result.runs.len(), 2);
        assert_eq!(result.differences.len(), 0);

        // Both runs should have identical output
        assert_eq!(result.runs[0].stdout, result.runs[1].stdout);
    }

    /// Test: REPL-011-002-002 - Non-deterministic script detection
    #[test]
    fn test_REPL_011_002_nondeterministic_script() {
        // ARRANGE: Script with $RANDOM (non-deterministic)
        let script = r#"
echo "Random: $RANDOM"
        "#;
        let verifier = ReplayVerifier::new();

        // ACT: Verify determinism
        let result = verifier.verify(script);

        // ASSERT: Should be non-deterministic
        assert!(
            !result.is_deterministic,
            "Script with $RANDOM should be non-deterministic"
        );
        assert_eq!(result.runs.len(), 2);
        assert!(!result.differences.is_empty(), "Should have differences");

        // Runs should have different output
        assert_ne!(result.runs[0].stdout, result.runs[1].stdout);
    }

    /// Test: REPL-011-002-003 - Multiple replay runs
    #[test]
    fn test_REPL_011_002_multiple_replays() {
        // ARRANGE: Deterministic script with 5 replays
        let script = "echo 'hello world'";
        let verifier = ReplayVerifier::new().with_replay_count(5);

        // ACT: Verify with multiple runs
        let result = verifier.verify(script);

        // ASSERT: All 5 runs should be identical
        assert!(result.is_deterministic);
        assert_eq!(result.runs.len(), 5);

        // All runs should have same output
        let first_output = &result.runs[0].stdout;
        for run in &result.runs[1..] {
            assert_eq!(&run.stdout, first_output);
        }
    }

    /// Test: REPL-011-002-004 - Difference detection
    #[test]
    fn test_REPL_011_002_difference_detection() {
        // ARRANGE: Script that outputs different values
        let script = "echo $RANDOM";
        let verifier = ReplayVerifier::new();

        // ACT: Verify determinism
        let result = verifier.verify(script);

        // ASSERT: Should detect differences
        assert!(!result.is_deterministic);
        assert_eq!(result.differences.len(), 1);
        assert_eq!(result.differences[0].line, 1);
        assert_ne!(result.differences[0].run1, result.differences[0].run2);
    }

    /// Test: REPL-011-002-005 - Empty script handling
    #[test]
    fn test_REPL_011_002_empty_script() {
        // ARRANGE: Empty script
        let script = "";
        let verifier = ReplayVerifier::new();

        // ACT: Verify determinism
        let result = verifier.verify(script);

        // ASSERT: Empty script is deterministic
        assert!(result.is_deterministic);
        assert_eq!(result.runs[0].stdout, "");
        assert_eq!(result.runs[1].stdout, "");
    }

    /// Test: REPL-011-002-006 - Multiline output
    #[test]
    fn test_REPL_011_002_multiline_output() {
        // ARRANGE: Script with multiple lines of output
        let script = r#"
for i in 1 2 3; do
    echo "Line $i"
done
        "#;
        let verifier = ReplayVerifier::new();

        // ACT: Verify determinism
        let result = verifier.verify(script);

        // ASSERT: Should be deterministic
        assert!(result.is_deterministic);
        assert!(result.runs[0].stdout.contains("Line 1"));
        assert!(result.runs[0].stdout.contains("Line 2"));
        assert!(result.runs[0].stdout.contains("Line 3"));
    }

    /// Test: REPL-011-002-007 - Exit code tracking
    #[test]
    fn test_REPL_011_002_exit_code_tracking() {
        // ARRANGE: Script that exits with error
        let script = "echo 'error'; exit 42";
        let verifier = ReplayVerifier::new();

        // ACT: Verify determinism
        let result = verifier.verify(script);

        // ASSERT: Exit codes should match
        assert!(result.is_deterministic);
        assert_eq!(result.runs[0].exit_code, 42);
        assert_eq!(result.runs[1].exit_code, 42);
    }

    /// Test: REPL-011-002-008 - Minimum replay count
    #[test]
    fn test_REPL_011_002_min_replay_count() {
        // ARRANGE: Verifier with replay_count < 2 (should be clamped to 2)
        let script = "echo 'test'";
        let verifier = ReplayVerifier::new().with_replay_count(1);

        // ACT: Verify determinism
        let result = verifier.verify(script);

        // ASSERT: Should still run at least 2 times
        assert_eq!(result.runs.len(), 2);
    }
}

// ===== PROPERTY TESTS =====

#[cfg(test)]
mod replay_property_tests {
    use super::*;
    use proptest::prelude::*;

    proptest! {
        /// Property: Simple echo statements should always be deterministic
        #[test]
        fn prop_REPL_011_002_deterministic_scripts_always_identical(
            line in "[a-z ]{1,30}"
        ) {
            // Simple echo statements should always be deterministic
            let script = format!("echo '{}'", line);
            let verifier = ReplayVerifier::new();
            let result = verifier.verify(&script);

            prop_assert!(
                result.is_deterministic,
                "Simple echo should be deterministic: '{}'", script
            );
            prop_assert_eq!(&result.runs[0].stdout, &result.runs[1].stdout);
        }

        /// Property: Deterministic scripts should be consistent across N runs
        #[test]
        fn prop_REPL_011_002_multiple_runs_consistent(
            replay_count in 2usize..10
        ) {
            // Deterministic scripts should be consistent across N runs
            let script = "echo 'consistent'";
            let verifier = ReplayVerifier::new().with_replay_count(replay_count);
            let result = verifier.verify(script);

            prop_assert!(result.is_deterministic);
            prop_assert_eq!(result.runs.len(), replay_count);

            // All runs should have identical output
            let first_output = &result.runs[0].stdout;
            for run in &result.runs[1..] {
                prop_assert_eq!(&run.stdout, first_output);
            }
        }

        /// Property: Verifier should never panic on any input
        #[test]
        fn prop_REPL_011_002_verify_never_panics(
            script in ".*{0,100}"
        ) {
            // Verifier should never panic on any input
            let verifier = ReplayVerifier::new();
            let _ = verifier.verify(&script);
        }
    }
    include!("determinism_replay_tests_REPL_2.rs");
}