harn-vm 0.7.55

Async bytecode virtual machine for the Harn programming language
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
//! Merge Captain end-to-end driver surface (#1019).
//!
//! The driver is intentionally thin: it resolves a backend-specific scenario
//! to the canonical JSONL transcript envelope, runs the same Merge Captain
//! oracle used by `harn merge-captain audit`, and writes a deterministic
//! receipt plus run summary. Backend adapters can later replace the
//! transcript resolver with live connector execution without changing the
//! artifact contract.

use std::collections::BTreeSet;
use std::fs;
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use std::thread;
use std::time::Duration;

use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};

use crate::agent_events::{AgentEvent, PersistedAgentEvent};
use crate::value::VmError;

use super::{
    audit_transcript, load_merge_captain_golden, load_transcript_jsonl, AuditReport,
    LoadedTranscript, MergeCaptainGolden, StateTransition,
};

const RECEIPT_TYPE: &str = "merge_captain_run_receipt";
const SUMMARY_TYPE: &str = "merge_captain_run_summary";

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum MergeCaptainDriverBackend {
    Live,
    Mock { playground_dir: PathBuf },
    Replay { fixture: PathBuf },
}

impl MergeCaptainDriverBackend {
    pub fn kind(&self) -> &'static str {
        match self {
            Self::Live => "live",
            Self::Mock { .. } => "mock",
            Self::Replay { .. } => "replay",
        }
    }
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum MergeCaptainDriverMode {
    Once,
    Watch,
}

#[derive(Clone, Debug)]
pub struct MergeCaptainDriverOptions {
    pub backend: MergeCaptainDriverBackend,
    pub mode: MergeCaptainDriverMode,
    pub model_route: Option<String>,
    pub timeout_tier: Option<String>,
    pub transcript_out: Option<PathBuf>,
    pub receipt_out: Option<PathBuf>,
    pub run_root: PathBuf,
    pub max_sweeps: u32,
    pub watch_backoff_ms: u64,
    pub stream_stdout: bool,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct MergeCaptainRunReceipt {
    #[serde(rename = "_type")]
    pub type_name: String,
    pub version: u32,
    pub persona: String,
    pub run_id: String,
    pub scenario: Option<String>,
    pub mode: MergeCaptainDriverMode,
    pub model_route: Option<String>,
    pub timeout_tier: Option<String>,
    pub sweeps: u32,
    pub event_count: u64,
    pub model_calls: u64,
    pub tool_calls: u64,
    pub approvals_requested: u64,
    pub unsafe_action_attempts: u64,
    pub prs_touched: Vec<MergeCaptainPrTouch>,
    pub state_transitions: Vec<StateTransition>,
    pub oracle_error_findings: u64,
    pub oracle_warn_findings: u64,
    pub pass: bool,
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct MergeCaptainPrTouch {
    pub repo: String,
    pub pr_number: u64,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct MergeCaptainRunSummary {
    #[serde(rename = "_type")]
    pub type_name: String,
    pub version: u32,
    pub run_id: String,
    pub backend: String,
    pub backend_source: Option<String>,
    pub scenario: Option<String>,
    pub mode: MergeCaptainDriverMode,
    pub sweeps: u32,
    pub transcript_path: Option<String>,
    pub receipt_path: String,
    pub event_count: u64,
    pub prs_touched: Vec<MergeCaptainPrTouch>,
    pub state_transitions: Vec<StateTransition>,
    pub approvals_requested: u64,
    pub model_calls: u64,
    pub tool_calls: u64,
    pub cost_usd: f64,
    pub oracle_findings: usize,
    pub oracle_error_findings: usize,
    pub oracle_warn_findings: usize,
    pub pass: bool,
}

#[derive(Clone, Debug)]
pub struct MergeCaptainDriverOutput {
    pub summary: MergeCaptainRunSummary,
    pub receipt_path: PathBuf,
    pub transcript_path: Option<PathBuf>,
    pub audit_report: AuditReport,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(default)]
struct MockScenarioManifest {
    #[serde(rename = "_type")]
    type_name: String,
    scenario: Option<String>,
    transcript: PathBuf,
    golden: Option<PathBuf>,
}

impl Default for MockScenarioManifest {
    fn default() -> Self {
        Self {
            type_name: "merge_captain_mock_scenario".to_string(),
            scenario: None,
            transcript: PathBuf::new(),
            golden: None,
        }
    }
}

#[derive(Clone, Debug)]
struct ResolvedScenario {
    events: Vec<PersistedAgentEvent>,
    golden: Option<MergeCaptainGolden>,
    scenario: Option<String>,
    backend_source: Option<String>,
}

pub fn run_merge_captain_driver(
    options: MergeCaptainDriverOptions,
) -> Result<MergeCaptainDriverOutput, VmError> {
    let mut resolved = resolve_backend(&options.backend)?;
    expand_watch_events(&options, &mut resolved.events);
    if resolved.scenario.is_none() {
        resolved.scenario = resolved
            .golden
            .as_ref()
            .map(|golden| golden.scenario.clone());
    }

    let audit_report = audit_transcript(&resolved.events, resolved.golden.as_ref());
    let run_id = deterministic_run_id(&resolved.events)?;
    let run_dir = options.run_root.join("merge-captain").join(&run_id);
    fs::create_dir_all(&run_dir).map_err(|error| {
        VmError::Runtime(format!(
            "failed to create merge-captain run directory {}: {error}",
            run_dir.display()
        ))
    })?;

    let transcript_path = write_transcript(&options, &run_dir, &resolved.events)?;
    let receipt_path = options
        .receipt_out
        .clone()
        .unwrap_or_else(|| run_dir.join("receipt.json"));
    let receipt = build_receipt(&options, &run_id, &resolved, &audit_report);
    write_json_file(&receipt_path, &receipt)?;

    let summary = build_summary(
        &options,
        &run_id,
        &resolved,
        &audit_report,
        &receipt_path,
        transcript_path.as_deref(),
        &receipt,
    );

    Ok(MergeCaptainDriverOutput {
        summary,
        receipt_path,
        transcript_path,
        audit_report,
    })
}

fn resolve_backend(backend: &MergeCaptainDriverBackend) -> Result<ResolvedScenario, VmError> {
    match backend {
        MergeCaptainDriverBackend::Live => Err(VmError::Runtime(
            "merge-captain live backend requires the production connector runtime; use --backend mock <dir> or --backend replay <fixture> in this checkout".to_string(),
        )),
        MergeCaptainDriverBackend::Replay { fixture } => {
            let loaded = load_transcript_jsonl(fixture)?;
            let golden = load_replay_golden_if_present(fixture)?;
            Ok(ResolvedScenario {
                events: loaded.events,
                golden,
                scenario: None,
                backend_source: Some(fixture.display().to_string()),
            })
        }
        MergeCaptainDriverBackend::Mock { playground_dir } => {
            // Prefer the on-disk playground (presence of `playground.json`)
            // — that's the #1020 surface with real bare git repos. Fall
            // back to the legacy transcript-replay manifest for backwards
            // compatibility with examples/merge_captain/playground_3repos.
            if super::playground::playground_marker_path(playground_dir).exists() {
                let (state, manifest) = super::playground::load_playground(playground_dir)?;
                let events = super::playground::synthesize_sweep(
                    &state,
                    &super::playground::TranscriptOptions::default(),
                );
                let golden = load_playground_golden_if_present(playground_dir)?;
                return Ok(ResolvedScenario {
                    events,
                    golden,
                    scenario: Some(manifest.scenario),
                    backend_source: Some(playground_dir.display().to_string()),
                });
            }
            let manifest_path = find_mock_manifest(playground_dir)?;
            let bytes = fs::read(&manifest_path).map_err(|error| {
                VmError::Runtime(format!(
                    "failed to read mock scenario manifest {}: {error}",
                    manifest_path.display()
                ))
            })?;
            let manifest: MockScenarioManifest = serde_json::from_slice(&bytes).map_err(|error| {
                VmError::Runtime(format!(
                    "failed to parse mock scenario manifest {}: {error}",
                    manifest_path.display()
                ))
            })?;
            if manifest.transcript.as_os_str().is_empty() {
                return Err(VmError::Runtime(format!(
                    "mock scenario manifest {} must set transcript",
                    manifest_path.display()
                )));
            }
            let base = manifest_path.parent().unwrap_or_else(|| Path::new("."));
            let transcript = resolve_relative(base, &manifest.transcript);
            let LoadedTranscript { events, .. } = load_transcript_jsonl(&transcript)?;
            let golden = match manifest.golden {
                Some(path) => Some(load_merge_captain_golden(&resolve_relative(base, &path))?),
                None => None,
            };
            Ok(ResolvedScenario {
                events,
                golden,
                scenario: manifest.scenario,
                backend_source: Some(playground_dir.display().to_string()),
            })
        }
    }
}

fn load_playground_golden_if_present(
    playground_dir: &Path,
) -> Result<Option<MergeCaptainGolden>, VmError> {
    let candidate = playground_dir.join("golden.json");
    if candidate.exists() {
        return load_merge_captain_golden(&candidate).map(Some);
    }
    Ok(None)
}

fn find_mock_manifest(playground_dir: &Path) -> Result<PathBuf, VmError> {
    if playground_dir.is_file() {
        return Ok(playground_dir.to_path_buf());
    }
    let candidates = [
        playground_dir.join("merge_captain.scenario.json"),
        playground_dir.join("scenario.json"),
        playground_dir.join("harn.merge-captain.json"),
    ];
    candidates
        .into_iter()
        .find(|path| path.exists())
        .ok_or_else(|| {
            VmError::Runtime(format!(
                "mock backend expected a scenario manifest at {}/merge_captain.scenario.json",
                playground_dir.display()
            ))
        })
}

fn resolve_relative(base: &Path, path: &Path) -> PathBuf {
    if path.is_absolute() {
        path.to_path_buf()
    } else {
        base.join(path)
    }
}

fn load_replay_golden_if_present(fixture: &Path) -> Result<Option<MergeCaptainGolden>, VmError> {
    let Some(stem) = fixture.file_stem().and_then(|stem| stem.to_str()) else {
        return Ok(None);
    };
    let mut candidates = Vec::new();
    if let Some(parent) = fixture.parent() {
        candidates.push(parent.join(format!("{stem}.golden.json")));
        if parent.file_name().and_then(|name| name.to_str()) == Some("transcripts") {
            if let Some(root) = parent.parent() {
                candidates.push(root.join("goldens").join(format!("{stem}.json")));
            }
        }
    }
    for candidate in candidates {
        if candidate.exists() {
            return load_merge_captain_golden(&candidate).map(Some);
        }
    }
    Ok(None)
}

fn write_transcript(
    options: &MergeCaptainDriverOptions,
    run_dir: &Path,
    events: &[PersistedAgentEvent],
) -> Result<Option<PathBuf>, VmError> {
    let mut line_buffer = Vec::new();
    for event in events {
        serde_json::to_writer(&mut line_buffer, event).map_err(|error| {
            VmError::Runtime(format!("failed to serialize transcript event: {error}"))
        })?;
        line_buffer.push(b'\n');
    }

    if options.stream_stdout {
        io::stdout().write_all(&line_buffer).map_err(|error| {
            VmError::Runtime(format!(
                "failed to stream merge-captain JSONL to stdout: {error}"
            ))
        })?;
    }

    let transcript_path = options
        .transcript_out
        .clone()
        .or_else(|| Some(run_dir.join("event_log.jsonl")));
    if let Some(path) = &transcript_path {
        write_bytes_file(path, &line_buffer)?;
    }
    Ok(transcript_path)
}

fn build_receipt(
    options: &MergeCaptainDriverOptions,
    run_id: &str,
    resolved: &ResolvedScenario,
    report: &AuditReport,
) -> MergeCaptainRunReceipt {
    let stats = collect_stats(&resolved.events);
    MergeCaptainRunReceipt {
        type_name: RECEIPT_TYPE.to_string(),
        version: 1,
        persona: "merge_captain".to_string(),
        run_id: run_id.to_string(),
        scenario: resolved
            .golden
            .as_ref()
            .map(|golden| golden.scenario.clone())
            .or_else(|| report.scenario.clone())
            .or_else(|| resolved.scenario.clone()),
        mode: options.mode.clone(),
        model_route: options.model_route.clone(),
        timeout_tier: options.timeout_tier.clone(),
        sweeps: sweep_count(options),
        event_count: report.event_count,
        model_calls: report.model_call_count,
        tool_calls: report.tool_call_count,
        approvals_requested: stats.approvals_requested,
        unsafe_action_attempts: stats.unsafe_action_attempts,
        prs_touched: stats.prs_touched,
        state_transitions: report.state_transitions.clone(),
        oracle_error_findings: report.error_findings() as u64,
        oracle_warn_findings: report.warn_findings() as u64,
        pass: report.pass,
    }
}

fn build_summary(
    options: &MergeCaptainDriverOptions,
    run_id: &str,
    resolved: &ResolvedScenario,
    report: &AuditReport,
    receipt_path: &Path,
    transcript_path: Option<&Path>,
    receipt: &MergeCaptainRunReceipt,
) -> MergeCaptainRunSummary {
    MergeCaptainRunSummary {
        type_name: SUMMARY_TYPE.to_string(),
        version: 1,
        run_id: run_id.to_string(),
        backend: options.backend.kind().to_string(),
        backend_source: resolved.backend_source.clone(),
        scenario: receipt.scenario.clone(),
        mode: options.mode.clone(),
        sweeps: receipt.sweeps,
        transcript_path: transcript_path.map(|path| path.display().to_string()),
        receipt_path: receipt_path.display().to_string(),
        event_count: report.event_count,
        prs_touched: receipt.prs_touched.clone(),
        state_transitions: report.state_transitions.clone(),
        approvals_requested: receipt.approvals_requested,
        model_calls: report.model_call_count,
        tool_calls: report.tool_call_count,
        cost_usd: 0.0,
        oracle_findings: report.findings.len(),
        oracle_error_findings: report.error_findings(),
        oracle_warn_findings: report.warn_findings(),
        pass: report.pass,
    }
}

#[derive(Default)]
struct TranscriptStats {
    approvals_requested: u64,
    unsafe_action_attempts: u64,
    prs_touched: Vec<MergeCaptainPrTouch>,
}

fn collect_stats(events: &[PersistedAgentEvent]) -> TranscriptStats {
    let mut stats = TranscriptStats::default();
    let mut prs = BTreeSet::new();

    for env in events {
        match &env.event {
            AgentEvent::Plan { plan, .. } => {
                if plan
                    .get("approval_required")
                    .and_then(|value| value.as_bool())
                    .unwrap_or(false)
                {
                    stats.approvals_requested += 1;
                }
                insert_pr_touch(plan, &mut prs);
            }
            AgentEvent::ToolCall {
                tool_name,
                raw_input,
                ..
            } => {
                if looks_like_unsafe_action(tool_name) {
                    stats.unsafe_action_attempts += 1;
                }
                insert_pr_touch(raw_input, &mut prs);
            }
            AgentEvent::ToolCallUpdate {
                raw_output: Some(output),
                ..
            } => {
                insert_pr_touch(output, &mut prs);
            }
            _ => {}
        }
    }

    stats.prs_touched = prs.into_iter().collect();
    stats
}

fn insert_pr_touch(value: &serde_json::Value, prs: &mut BTreeSet<MergeCaptainPrTouch>) {
    let repo = value
        .get("repo")
        .and_then(|value| value.as_str())
        .map(str::to_string);
    let pr_number = value
        .get("pr_number")
        .or_else(|| value.get("number"))
        .and_then(|value| value.as_u64());
    if let (Some(repo), Some(pr_number)) = (repo, pr_number) {
        prs.insert(MergeCaptainPrTouch { repo, pr_number });
    }
}

fn looks_like_unsafe_action(tool_name: &str) -> bool {
    let lower = tool_name.to_lowercase();
    lower.contains("merge")
        || lower.contains("approve")
        || lower.contains("force_push")
        || lower.contains("delete")
        || lower.contains("write")
        || lower.contains("post_comment")
        || lower.contains("set_label")
}

fn sweep_count(options: &MergeCaptainDriverOptions) -> u32 {
    match options.mode {
        MergeCaptainDriverMode::Once => 1,
        MergeCaptainDriverMode::Watch => options.max_sweeps.max(1),
    }
}

fn expand_watch_events(options: &MergeCaptainDriverOptions, events: &mut Vec<PersistedAgentEvent>) {
    if options.mode != MergeCaptainDriverMode::Watch {
        return;
    }
    let sweeps = options.max_sweeps.max(1);
    if sweeps <= 1 || events.is_empty() {
        return;
    }
    let template = events.clone();
    let stride = template
        .iter()
        .map(|event| event.index)
        .max()
        .unwrap_or(0)
        .saturating_add(1);
    let time_stride = template
        .last()
        .and_then(|last| {
            template
                .first()
                .map(|first| last.emitted_at_ms - first.emitted_at_ms)
        })
        .unwrap_or(0)
        .max(1);
    for sweep in 1..sweeps {
        if options.watch_backoff_ms > 0 {
            thread::sleep(Duration::from_millis(options.watch_backoff_ms));
        }
        let index_offset = stride.saturating_mul(sweep as u64);
        let time_offset = time_stride.saturating_mul(sweep as i64);
        for event in &template {
            let mut next = event.clone();
            next.index = next.index.saturating_add(index_offset);
            next.emitted_at_ms = next.emitted_at_ms.saturating_add(time_offset);
            events.push(next);
        }
    }
}

fn deterministic_run_id(events: &[PersistedAgentEvent]) -> Result<String, VmError> {
    let mut hasher = Sha256::new();
    for event in events {
        let bytes = serde_json::to_vec(event).map_err(|error| {
            VmError::Runtime(format!("failed to hash merge-captain transcript: {error}"))
        })?;
        hasher.update((bytes.len() as u64).to_le_bytes());
        hasher.update(bytes);
    }
    let digest = hasher.finalize();
    let mut suffix = String::with_capacity(16);
    for byte in &digest[..8] {
        suffix.push_str(&format!("{byte:02x}"));
    }
    Ok(format!("merge-captain-{suffix}"))
}

fn write_json_file<T: Serialize>(path: &Path, value: &T) -> Result<(), VmError> {
    let mut bytes = serde_json::to_vec_pretty(value)
        .map_err(|error| VmError::Runtime(format!("failed to serialize JSON artifact: {error}")))?;
    bytes.push(b'\n');
    write_bytes_file(path, &bytes)
}

fn write_bytes_file(path: &Path, bytes: &[u8]) -> Result<(), VmError> {
    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent).map_err(|error| {
            VmError::Runtime(format!(
                "failed to create artifact directory {}: {error}",
                parent.display()
            ))
        })?;
    }
    fs::write(path, bytes).map_err(|error| {
        VmError::Runtime(format!(
            "failed to write artifact {}: {error}",
            path.display()
        ))
    })
}

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

    fn repo_root() -> PathBuf {
        PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .parent()
            .unwrap()
            .parent()
            .unwrap()
            .to_path_buf()
    }

    #[test]
    fn mock_backend_resolves_manifest_and_builds_receipt() {
        let temp = tempfile::tempdir().unwrap();
        let output = run_merge_captain_driver(MergeCaptainDriverOptions {
            backend: MergeCaptainDriverBackend::Mock {
                playground_dir: repo_root().join("examples/merge_captain/playground_3repos"),
            },
            mode: MergeCaptainDriverMode::Once,
            model_route: Some("mock/value".to_string()),
            timeout_tier: Some("smoke".to_string()),
            transcript_out: Some(temp.path().join("event_log.jsonl")),
            receipt_out: Some(temp.path().join("receipt.json")),
            run_root: temp.path().join("runs"),
            max_sweeps: 1,
            watch_backoff_ms: 0,
            stream_stdout: false,
        })
        .unwrap();

        assert!(output.summary.pass);
        assert_eq!(output.summary.backend, "mock");
        assert_eq!(output.summary.scenario.as_deref(), Some("green_pr"));
        assert!(!output.summary.prs_touched.is_empty());
        assert!(output.receipt_path.exists());
        assert!(output.transcript_path.unwrap().exists());
    }

    #[test]
    fn mock_backend_drives_real_on_disk_playground() {
        if std::process::Command::new("git")
            .arg("--version")
            .output()
            .map(|o| !o.status.success())
            .unwrap_or(true)
        {
            eprintln!("(skipping — git not on PATH)");
            return;
        }
        let temp = tempfile::tempdir().unwrap();
        let playground = temp.path().join("pg");
        let manifest = super::super::playground::load_builtin("single_green").unwrap();
        super::super::playground::init_playground_at(super::super::playground::InitOptions {
            dir: &playground,
            manifest: &manifest,
            allow_existing: false,
        })
        .unwrap();
        let output = run_merge_captain_driver(MergeCaptainDriverOptions {
            backend: MergeCaptainDriverBackend::Mock {
                playground_dir: playground.clone(),
            },
            mode: MergeCaptainDriverMode::Once,
            model_route: None,
            timeout_tier: None,
            transcript_out: Some(temp.path().join("event_log.jsonl")),
            receipt_out: Some(temp.path().join("receipt.json")),
            run_root: temp.path().join("runs"),
            max_sweeps: 1,
            watch_backoff_ms: 0,
            stream_stdout: false,
        })
        .unwrap();
        assert_eq!(output.summary.backend, "mock");
        assert_eq!(output.summary.scenario.as_deref(), Some("single_green"));
        assert!(output.summary.event_count > 0);
        // PR coverage gets populated from the synthesized transcript.
        assert!(!output.summary.prs_touched.is_empty());
    }

    #[test]
    fn replay_backend_surfaces_oracle_failure() {
        let temp = tempfile::tempdir().unwrap();
        let fixture =
            repo_root().join("examples/personas/merge_captain/transcripts/bad_unsafe_merge.jsonl");
        let output = run_merge_captain_driver(MergeCaptainDriverOptions {
            backend: MergeCaptainDriverBackend::Replay { fixture },
            mode: MergeCaptainDriverMode::Once,
            model_route: None,
            timeout_tier: None,
            transcript_out: Some(temp.path().join("event_log.jsonl")),
            receipt_out: Some(temp.path().join("receipt.json")),
            run_root: temp.path().join("runs"),
            max_sweeps: 1,
            watch_backoff_ms: 0,
            stream_stdout: false,
        })
        .unwrap();

        assert!(!output.summary.pass);
        assert!(output.summary.oracle_error_findings > 0);
    }

    #[test]
    fn mock_and_replay_receipts_match_for_same_transcript() {
        let temp = tempfile::tempdir().unwrap();
        let mock_receipt = temp.path().join("mock-receipt.json");
        let replay_receipt = temp.path().join("replay-receipt.json");
        let common_model_route = Some("mock/value".to_string());
        let common_timeout_tier = Some("smoke".to_string());

        run_merge_captain_driver(MergeCaptainDriverOptions {
            backend: MergeCaptainDriverBackend::Mock {
                playground_dir: repo_root().join("examples/merge_captain/playground_3repos"),
            },
            mode: MergeCaptainDriverMode::Once,
            model_route: common_model_route.clone(),
            timeout_tier: common_timeout_tier.clone(),
            transcript_out: Some(temp.path().join("mock-event_log.jsonl")),
            receipt_out: Some(mock_receipt.clone()),
            run_root: temp.path().join("runs"),
            max_sweeps: 1,
            watch_backoff_ms: 0,
            stream_stdout: false,
        })
        .unwrap();

        run_merge_captain_driver(MergeCaptainDriverOptions {
            backend: MergeCaptainDriverBackend::Replay {
                fixture: repo_root()
                    .join("examples/personas/merge_captain/transcripts/green_pr.jsonl"),
            },
            mode: MergeCaptainDriverMode::Once,
            model_route: common_model_route,
            timeout_tier: common_timeout_tier,
            transcript_out: Some(temp.path().join("replay-event_log.jsonl")),
            receipt_out: Some(replay_receipt.clone()),
            run_root: temp.path().join("runs"),
            max_sweeps: 1,
            watch_backoff_ms: 0,
            stream_stdout: false,
        })
        .unwrap();

        assert_eq!(
            std::fs::read_to_string(mock_receipt).unwrap(),
            std::fs::read_to_string(replay_receipt).unwrap()
        );
    }
}