vyre-conform 0.1.0

Conformance suite for vyre backends — proves byte-identical output to CPU reference
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
//! Phase 6 calibration CLI entry point.

use std::collections::BTreeSet;
use std::env;
use std::fs;
use std::path::PathBuf;
use std::process::Command;
use std::time::Instant;

use clap::Parser;

use crate::generate::archetypes::ALL_ARCHETYPES;
use crate::generate::emit::{generate, GenError, GenerationPlan, TemplateKind};
use crate::op_registry::all_specs;
use crate::verify::harnesses::mutation::{AppliedMutation, MutationApplyError, MutationOutcome};
use crate::OpSpec;

/// Shared calibration logic used by both the CLI binary and the acceptance test.
pub mod core {
    use super::*;

    /// Structured report emitted at the end of a calibration run.
    #[derive(Debug, Clone)]
    pub struct CalibrationReport {
        /// Operation id that was calibrated.
        pub op_id: String,
        /// Number of files the generator emitted.
        pub generated_count: usize,
        /// Distinct oracle families observed in the generated files.
        pub unique_oracles: Vec<String>,
        /// Wall time for generation.
        pub generation_duration_ms: u128,
        /// Wall time for `cargo test` on the generated files.
        pub test_duration_ms: u128,
        /// Wall time for the mutation gate.
        pub mutation_duration_ms: u128,
        /// Mutations killed by the generated test sample.
        pub generated_killed: usize,
        /// Mutations that survived the generated test sample.
        pub generated_survived: usize,
        /// Mutations skipped (not applicable or compile error).
        pub generated_skipped: usize,
        /// Mutations killed by hand-written tests, if comparison was attempted.
        pub handwritten_killed: Option<usize>,
        /// Mutations that survived hand-written tests, if comparison was attempted.
        pub handwritten_survived: Option<usize>,
        /// Mutations skipped for hand-written tests, if comparison was attempted.
        pub handwritten_skipped: Option<usize>,
        /// Actionable findings and survivor details.
        pub findings: Vec<String>,
        /// If an upstream stage is not ready, the name of that stage.
        pub pending_upstream: Option<String>,
        /// True when `cargo test` output contained a panic marker.
        pub test_panicked: bool,
    }

    impl CalibrationReport {
        /// Render the report as a line-oriented struct for stdout / test logs.
        #[inline]
        pub fn format_report(&self) -> String {
            let mut lines = Vec::new();
            lines.push(format!(
                "=== Phase 6 Calibration Report: {} ===",
                self.op_id
            ));
            lines.push(format!("generated_files: {}", self.generated_count));
            lines.push(format!("unique_oracles: {:?}", self.unique_oracles));
            lines.push(format!(
                "generation_duration_ms: {}",
                self.generation_duration_ms
            ));
            lines.push(format!("test_duration_ms: {}", self.test_duration_ms));
            lines.push(format!(
                "mutation_duration_ms: {}",
                self.mutation_duration_ms
            ));
            lines.push(format!("generated_killed: {}", self.generated_killed));
            lines.push(format!("generated_survived: {}", self.generated_survived));
            lines.push(format!("generated_skipped: {}", self.generated_skipped));
            if let Some(k) = self.handwritten_killed {
                lines.push(format!("handwritten_killed: {}", k));
            }
            if let Some(s) = self.handwritten_survived {
                lines.push(format!("handwritten_survived: {}", s));
            }
            if let Some(s) = self.handwritten_skipped {
                lines.push(format!("handwritten_skipped: {}", s));
            }
            if let Some(ref p) = self.pending_upstream {
                lines.push(format!("pending_upstream: {}", p));
            }
            lines.push("findings:".to_string());
            for finding in &self.findings {
                lines.push(format!("  - {}", finding));
            }
            lines.push(format!("test_panicked: {}", self.test_panicked));
            lines.join("\n")
        }
    }

    /// Run the full Phase 6 calibration pipeline for `op_id`.
    ///
    /// Unimplemented upstream stages are logged as `pending_upstream` and
    /// returned early so the caller can decide whether to assert or skip.
    #[inline]
    pub fn run(op_id: &str) -> CalibrationReport {
        let start = Instant::now();
        let mut report = CalibrationReport {
            op_id: op_id.to_string(),
            generated_count: 0,
            unique_oracles: Vec::new(),
            generation_duration_ms: 0,
            test_duration_ms: 0,
            mutation_duration_ms: 0,
            generated_killed: 0,
            generated_survived: 0,
            generated_skipped: 0,
            handwritten_killed: None,
            handwritten_survived: None,
            handwritten_skipped: None,
            findings: Vec::new(),
            pending_upstream: None,
            test_panicked: false,
        };

        // ------------------------------------------------------------------
        // 1. Locate the op in the registry.
        // ------------------------------------------------------------------
        let specs = all_specs();
        let Some(op) = specs.into_iter().find(|s| s.id == op_id) else {
            report.pending_upstream = Some(format!("registry missing op {}", op_id));
            return report;
        };
        let op: &'static OpSpec = Box::leak(Box::new(op));

        // ------------------------------------------------------------------
        // 2. Generate tests into a tempdir.
        // ------------------------------------------------------------------
        let gen_start = Instant::now();
        let out_dir = std::env::temp_dir().join(format!(
            "vyre_phase6_{}_{}",
            op_id.replace('.', "_"),
            std::process::id()
        ));
        let _ = fs::remove_dir_all(&out_dir);

        let plan = GenerationPlan {
            ops: vec![op],
            archetypes: ALL_ARCHETYPES.to_vec(),
            templates: vec![
                TemplateKind::OpCorrectness,
                TemplateKind::Law,
                TemplateKind::BackendEquiv,
                TemplateKind::Archetype,
                TemplateKind::Validation,
                TemplateKind::MutationKill,
            ],
            seed: 0x5151_5151_0000_0001,
        };

        let gen_report = match generate(&plan, &out_dir) {
            Ok(r) => r,
            Err(GenError::InvalidPlan { ref reason })
                if reason.contains("Phase 5") || reason.contains("target") =>
            {
                report.pending_upstream = Some("generator".to_string());
                return report;
            }
            Err(GenError::Template(ref e))
                if e.to_string().contains("not found") || e.to_string().contains("stub") =>
            {
                report.pending_upstream = Some("generator/templates".to_string());
                return report;
            }
            Err(e) => {
                report.findings.push(format!("generation failed: {}", e));
                return report;
            }
        };

        report.generated_count = gen_report.files.len();
        let oracles: BTreeSet<_> = gen_report.files.iter().map(|f| f.oracle.clone()).collect();
        report.unique_oracles = oracles.into_iter().collect();
        report.generation_duration_ms = gen_start.elapsed().as_millis();

        if gen_report.files.is_empty() {
            report
                .findings
                .push("generation produced zero files".to_string());
            return report;
        }

        // ------------------------------------------------------------------
        // 3. Materialise a single integration test file in tests/.
        // ------------------------------------------------------------------
        let manifest_dir = env::var_os("CARGO_MANIFEST_DIR")
            .map(PathBuf::from)
            .unwrap_or_else(|| env::current_dir().expect("Fix: current dir must be available"));
        let tests_dir = manifest_dir.join("tests");

        let mut combined = String::new();
        for file in &gen_report.files {
            let src = match fs::read_to_string(&file.path) {
                Ok(s) => s,
                Err(e) => {
                    report.findings.push(format!(
                        "failed to read generated file {}: {}",
                        file.path.display(),
                        e
                    ));
                    continue;
                }
            };
            combined.push_str(&src);
            combined.push('\n');
        }

        let test_file_stem = format!(
            "phase6_generated_{}_{}",
            op_id.replace('.', "_"),
            start.elapsed().as_millis()
        );
        let test_file_path = tests_dir.join(format!("{}.rs", test_file_stem));

        if let Err(e) = fs::write(&test_file_path, &combined) {
            report.findings.push(format!(
                "failed to write temp test file {}: {}",
                test_file_path.display(),
                e
            ));
            return report;
        }

        struct TempTestFile(PathBuf);
        impl Drop for TempTestFile {
            fn drop(&mut self) {
                let _ = fs::remove_file(&self.0);
            }
        }
        let _test_guard = TempTestFile(test_file_path.clone());

        // ------------------------------------------------------------------
        // 4. Run `cargo test` on the generated integration test.
        // ------------------------------------------------------------------
        let test_start = Instant::now();
        let test_result = Command::new("cargo")
            .args(["test", "--offline", "--test", &test_file_stem])
            .output();
        report.test_duration_ms = test_start.elapsed().as_millis();

        match test_result {
            Ok(output) => {
                let stderr = String::from_utf8_lossy(&output.stderr);
                let stdout = String::from_utf8_lossy(&output.stdout);
                let combined_out = format!("{}{}", stdout, stderr);

                if combined_out.contains("panicked at")
                    || combined_out.contains("thread 'main' panicked")
                {
                    report.test_panicked = true;
                    report
                        .findings
                        .push("generated tests caused panics".to_string());
                } else if !output.status.success() {
                    report
                        .findings
                        .push("generated tests had structured failures".to_string());
                }
            }
            Err(e) => {
                report
                    .findings
                    .push(format!("cargo test spawn failed: {}", e));
            }
        }

        // ------------------------------------------------------------------
        // 5. Mutation gate on a sample of generated tests.
        // ------------------------------------------------------------------
        let mutation_start = Instant::now();
        let catalog_mutations: Vec<CatalogMutation> =
            crate::adversarial::mutations::catalog::MUTATION_CATALOG
                .iter()
                .filter(|m| {
                    op.mutation_sensitivity
                        .contains(&crate::adversarial::mutations::catalog::class_of(m))
                })
                .map(|m| CatalogMutation(m.clone()))
                .collect();
        let mutation_refs: Vec<&dyn AppliedMutation> = catalog_mutations
            .iter()
            .map(|m| m as &dyn AppliedMutation)
            .collect();
        let classes: Vec<_> = op.mutation_sensitivity.iter().copied().collect();

        let source_file = manifest_dir.join("src/specs/primitive/add.rs");
        if !source_file.exists() {
            report
                .findings
                .push("mutation source src/specs/primitive/add.rs not found".to_string());
        } else if mutation_refs.is_empty() {
            report
                .findings
                .push("no mutations selected for this op".to_string());
        } else {
            // Sample one representative test to keep the run under 120 s.
            // (Every generated test currently has the same determinism shape,
            // so one is representative until the generator diversifies.)
            let sample = gen_report.files.first();
            if let Some(file) = sample {
                let gate_report = crate::verify::harnesses::mutation::mutation_probe(
                    &source_file,
                    &file.test_name,
                    &mutation_refs,
                    &classes,
                    &[],
                );
                for result in &gate_report.results {
                    match result.outcome {
                        MutationOutcome::Killed => {
                            report.generated_killed += 1;
                        }
                        MutationOutcome::Survived => {
                            report.generated_survived += 1;
                            report.findings.push(format!(
                                "SURVIVOR: {} survived {} ({})",
                                file.test_name, result.mutation_id, result.description
                            ));
                        }
                        MutationOutcome::Skipped { .. } => {
                            report.generated_skipped += 1;
                        }
                        _ => {
                            // Future MutationOutcome variants get counted
                            // as "skipped" by default to keep the report
                            // shape stable when new outcomes are added.
                            report.generated_skipped += 1;
                        }
                    }
                }
            }
        }
        report.mutation_duration_ms = mutation_start.elapsed().as_millis();

        // ------------------------------------------------------------------
        // 6. Compare against hand-written vyre/tests/primitive_programs.rs.
        // ------------------------------------------------------------------
        let vyre_tests = manifest_dir.join("../vyre/tests/primitive_programs.rs");
        let vyre_source = manifest_dir.join("../vyre/src/ops/primitive/add.rs");
        if vyre_tests.exists() && vyre_source.exists() {
            let src = match fs::read_to_string(&vyre_tests) {
                Ok(s) => s,
                Err(e) => {
                    report
                        .findings
                        .push(format!("handwritten comparison skipped: {}", e));
                    return report;
                }
            };

            let mut test_names = Vec::new();
            let lines: Vec<_> = src.lines().collect();
            for i in 0..lines.len().saturating_sub(1) {
                if lines[i].trim().starts_with("#[test]") {
                    let next = lines[i + 1].trim();
                    if let Some(rest) = next.strip_prefix("fn ") {
                        if let Some(name) = rest.split('(').next() {
                            test_names.push(name.to_string());
                        }
                    }
                }
            }

            let mut hk = 0;
            let mut hs = 0;
            let mut hskip = 0;

            let original_dir = match env::current_dir() {
                Ok(dir) => dir,
                Err(error) => {
                    report.findings.push(format!(
                        "Fix: handwritten comparison skipped because current dir was unavailable: {error}"
                    ));
                    return report;
                }
            };
            if let Ok(vyre_dir) = manifest_dir.join("../vyre").canonicalize() {
                for name in test_names.iter().filter(|n| {
                    n.contains("add")
                        || *n
                            == "binary_primitive_programs_have_buffer_entry_and_operator_contracts"
                }) {
                    if env::set_current_dir(&vyre_dir).is_err() {
                        continue;
                    }
                    let abs_source = match vyre_source.canonicalize() {
                        Ok(p) => p,
                        Err(_) => continue,
                    };
                    let gate_report = crate::verify::harnesses::mutation::mutation_probe(
                        &abs_source,
                        name,
                        &mutation_refs,
                        &classes,
                        &[],
                    );
                    for result in &gate_report.results {
                        match result.outcome {
                            MutationOutcome::Killed => hk += 1,
                            MutationOutcome::Survived => hs += 1,
                            MutationOutcome::Skipped { .. } => hskip += 1,
                            _ => hskip += 1,
                        }
                    }
                }
                let _ = env::set_current_dir(&original_dir);
            }

            report.handwritten_killed = Some(hk);
            report.handwritten_survived = Some(hs);
            report.handwritten_skipped = Some(hskip);
        }

        report
    }

    struct CatalogMutation(crate::adversarial::mutations::catalog::Mutation);

    impl AppliedMutation for CatalogMutation {
        fn id(&self) -> &str {
            leak_string(format!("{:?}", self.0))
        }

        fn description(&self) -> &str {
            leak_string(format!("{:?}", self.0))
        }

        fn class(&self) -> crate::spec::MutationClass {
            crate::adversarial::mutations::catalog::class_of(&self.0)
        }

        fn apply(&self, source: &str) -> Result<String, MutationApplyError> {
            crate::adversarial::mutations::catalog::apply(source, &self.0).map_err(|e| {
                MutationApplyError::NotApplicable {
                    reason: e.to_string(),
                }
            })
        }
    }

    fn leak_string(s: String) -> &'static str {
        Box::leak(s.into_boxed_str())
    }
}

/// Arguments for Phase 6 calibration.
#[derive(Debug, Parser)]
pub struct Args {
    /// Operation to calibrate.
    #[arg(long, default_value = "primitive.math.add")]
    pub op: String,
}

/// Run the Phase 6 calibration report.
pub fn run(args: Args) -> Result<(), Box<dyn std::error::Error>> {
    let report = core::run(&args.op);
    println!("{}", report.format_report());
    Ok(())
}