harn-vm 0.10.170

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
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
//! Merge Captain transcript oracle and audit (#1013).
//!
//! Consumes JSONL transcript artifacts produced by `JsonlEventSink`
//! (`.harn-runs/<session-id>/event_log.jsonl`) and reports oracle
//! findings: extra model calls, invalid structured outputs, repeated
//! reads, bad waits, unsafe attempted actions, skipped verification,
//! missing approvals, and non-minimal tool usage.
//!
//! The oracle works on a stream of `PersistedAgentEvent` envelopes.
//! It can run with or without a golden fixture: without, it emits
//! findings derived purely from transcript-internal heuristics
//! (parse failures, repeated identical tool calls, write tools that
//! preceded any approval gate). With a golden, it additionally
//! cross-checks scenario-specific budgets and required state steps.
//!
//! The output is both serializable JSON (machine-readable for CI
//! gates) and a `Display` impl for human-readable reports.

use std::collections::{BTreeMap, BTreeSet};
use std::fmt;
use std::fs;
use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};

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

/// Severity of an audit finding. `Error` fails CI gates; `Warn`
/// surfaces in reports but does not flip `pass` to `false`; `Info`
/// is observational.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum FindingSeverity {
    Info,
    Warn,
    Error,
}

impl FindingSeverity {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Info => "info",
            Self::Warn => "warn",
            Self::Error => "error",
        }
    }
}

/// Categories the oracle can raise. Stable wire identifiers — the
/// `snake_case` form is what CI parsers should match against.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum FindingCategory {
    /// Model issued more calls than the scenario budget allows.
    ExtraModelCall,
    /// A `Plan` event or tool input failed JSON schema validation,
    /// or a tool dispatch was rejected with `schema_validation`.
    InvalidStructuredOutput,
    /// The same `(tool, args)` pair was issued more than the
    /// configured threshold (default 1) consecutively without a
    /// state change or feedback in between.
    RepeatedRead,
    /// A `wait`/`sleep` / poll-style tool was issued without a
    /// progress signal between consecutive reads of the same
    /// resource.
    BadWait,
    /// The agent attempted a write/delete/force-push action without
    /// any prior approval gate (handoff, approval feedback, or
    /// explicit approval-required plan).
    UnsafeAttemptedAction,
    /// The PR state machine reached a "merge" or "approve" step
    /// without first running a required "verify" step (e.g. checking
    /// CI status).
    SkippedVerification,
    /// A `Plan` event declared `approval_required: true` but no
    /// approval gate (handoff, approval feedback, or pause) followed.
    MissingApproval,
    /// Tool-call count exceeded the golden's `max_tool_calls`.
    NonMinimalToolUsage,
    /// A scenario-required state step was never reached.
    MissingStateStep,
    /// State steps appeared out of the expected order.
    StateOutOfOrder,
    /// Observed state transitions did not match the scenario's exact
    /// golden sequence.
    StateSequenceMismatch,
    /// The transcript ended without a terminal event (IterationEnd,
    /// BudgetExhausted, LoopStuck, Handoff). Often a truncated log.
    IncompleteTranscript,
    /// A tool call listed in the golden's `forbidden_actions` was
    /// invoked.
    ForbiddenAction,
}

impl FindingCategory {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::ExtraModelCall => "extra_model_call",
            Self::InvalidStructuredOutput => "invalid_structured_output",
            Self::RepeatedRead => "repeated_read",
            Self::BadWait => "bad_wait",
            Self::UnsafeAttemptedAction => "unsafe_attempted_action",
            Self::SkippedVerification => "skipped_verification",
            Self::MissingApproval => "missing_approval",
            Self::NonMinimalToolUsage => "non_minimal_tool_usage",
            Self::MissingStateStep => "missing_state_step",
            Self::StateOutOfOrder => "state_out_of_order",
            Self::StateSequenceMismatch => "state_sequence_mismatch",
            Self::IncompleteTranscript => "incomplete_transcript",
            Self::ForbiddenAction => "forbidden_action",
        }
    }
}

/// One oracle finding linked back to the JSONL events that triggered
/// it, plus the PR state-machine step (when known) and the tool
/// names involved.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct AuditFinding {
    pub category: FindingCategory,
    pub severity: FindingSeverity,
    pub message: String,
    /// Monotonic event indexes from `PersistedAgentEvent.index`.
    /// Empty when the finding is suite-level (e.g. a missing state
    /// step that never fired).
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub event_indices: Vec<u64>,
    /// PR state-machine step name if the finding is bound to one.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub state_step: Option<String>,
    /// Tool name(s) involved.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub tools: Vec<String>,
}

/// One observed PR state-machine transition.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct StateTransition {
    /// Step identifier from the golden's `state_steps` (or the
    /// default heuristic step list).
    pub step: String,
    /// Index of the event that triggered the step.
    pub event_index: u64,
    /// Why the step fired: tool name, event variant, or "plan".
    pub triggered_by: String,
}

/// Tool-name shape match for golden state steps. Either an exact
/// name, a substring (`*foo*`), prefix (`foo*`), or suffix
/// (`*foo`). Matched case-insensitively.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Default)]
#[serde(default)]
pub struct ToolPattern {
    /// Exact tool name. Mutually exclusive with `glob`.
    pub name: Option<String>,
    /// Glob pattern (`*` wildcards only). Mutually exclusive with
    /// `name`.
    pub glob: Option<String>,
}

impl ToolPattern {
    pub fn matches(&self, tool: &str) -> bool {
        let needle = tool.to_lowercase();
        if let Some(name) = &self.name {
            return name.eq_ignore_ascii_case(tool);
        }
        if let Some(glob) = &self.glob {
            return glob_match(&glob.to_lowercase(), &needle);
        }
        false
    }
}

// Golden-fixture tool patterns promise `*`-only wildcards (everything else
// literal), which is exactly the shared prose matcher's contract.
use harn_glob::match_prose as glob_match;

/// One state-machine step in the golden fixture.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Default)]
#[serde(default)]
pub struct GoldenStateStep {
    /// Step identifier (e.g. "intake", "verify_ci", "approve",
    /// "merge"). Used to link findings back.
    pub step: String,
    /// Tool patterns that, when invoked, trigger this step.
    pub tools: Vec<ToolPattern>,
    /// Plan field names whose presence triggers the step.
    /// Example: `["review_risk"]` matches a `Plan` event with that
    /// key in the structured plan.
    pub plan_fields: Vec<String>,
    /// Event variant names that trigger this step (e.g.
    /// `"handoff"`, `"feedback_injected"`).
    pub events: Vec<String>,
    /// When `true`, this step is required for the scenario; failure
    /// to reach it produces a `MissingStateStep` finding.
    pub required: bool,
    /// When this step represents an approval gate. Used by the
    /// `MissingApproval` rule to decide whether a preceding
    /// `approval_required: true` plan was satisfied.
    #[serde(default)]
    pub approval_gate: bool,
    /// When this step represents a verification step. Used by the
    /// `SkippedVerification` rule to decide whether a "merge" was
    /// preceded by a verifier.
    #[serde(default)]
    pub verifier: bool,
    /// When this step represents a terminal "ship" action (merge,
    /// label-set, deploy). Used by the `SkippedVerification` rule.
    #[serde(default)]
    pub merge_action: bool,
}

/// Golden fixture: the ideal model behavior for a Merge Captain
/// scenario. Loaded from JSON and shipped under
/// `examples/personas/merge_captain/goldens/`.
#[derive(Clone, Debug, Serialize, Deserialize, Default, PartialEq, Eq)]
#[serde(default)]
pub struct MergeCaptainGolden {
    #[serde(rename = "_type")]
    pub type_name: String,
    /// Free-form scenario id (e.g. `"green_pr"`,
    /// `"failing_ci"`).
    pub scenario: String,
    pub description: Option<String>,
    /// Maximum acceptable model-call count.
    pub max_model_calls: Option<u64>,
    /// Maximum acceptable tool-call count.
    pub max_tool_calls: Option<u64>,
    /// Maximum acceptable repeated-read run length (default 1 — any
    /// repetition beyond that triggers a finding).
    pub max_repeat: Option<u32>,
    /// Tool patterns that must always be preceded by an approval gate.
    pub require_approval_for: Vec<ToolPattern>,
    /// Tool patterns that may never appear in this scenario.
    pub forbidden_actions: Vec<ToolPattern>,
    /// State-machine steps to track. The first matching pattern in
    /// declaration order wins for any given event.
    pub state_steps: Vec<GoldenStateStep>,
    /// Optional exact transition sequence for deterministic fixtures.
    /// When present, the audit fails unless the observed transition
    /// step names match this list byte-for-byte and in order.
    pub expected_state_transitions: Vec<String>,
}

/// The audit report. `pass` is `false` iff any finding has
/// severity `Error`.
#[derive(Clone, Debug, Serialize, Deserialize, Default)]
pub struct AuditReport {
    pub scenario: Option<String>,
    /// Source path of the transcript (when read from disk).
    pub source_path: Option<String>,
    /// Distinct session ids observed in the transcript.
    pub session_ids: Vec<String>,
    pub event_count: u64,
    pub model_call_count: u64,
    pub tool_call_count: u64,
    pub findings: Vec<AuditFinding>,
    pub state_transitions: Vec<StateTransition>,
    pub pass: bool,
}

impl AuditReport {
    pub fn error_findings(&self) -> usize {
        self.findings
            .iter()
            .filter(|f| f.severity == FindingSeverity::Error)
            .count()
    }

    pub fn warn_findings(&self) -> usize {
        self.findings
            .iter()
            .filter(|f| f.severity == FindingSeverity::Warn)
            .count()
    }
}

impl fmt::Display for AuditReport {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(
            f,
            "{} scenario={} events={} tool_calls={} model_calls={}",
            if self.pass { "PASS" } else { "FAIL" },
            self.scenario.as_deref().unwrap_or("<none>"),
            self.event_count,
            self.tool_call_count,
            self.model_call_count
        )?;
        if let Some(path) = &self.source_path {
            writeln!(f, "  transcript: {path}")?;
        }
        if !self.state_transitions.is_empty() {
            writeln!(f, "  state transitions:")?;
            for t in &self.state_transitions {
                writeln!(
                    f,
                    "    [{}] {} <- {}",
                    t.event_index, t.step, t.triggered_by
                )?;
            }
        }
        if self.findings.is_empty() {
            writeln!(f, "  findings: none")?;
        } else {
            writeln!(f, "  findings ({}):", self.findings.len())?;
            for finding in &self.findings {
                let step = finding
                    .state_step
                    .as_deref()
                    .map(|s| format!(" step={s}"))
                    .unwrap_or_default();
                let tools = if finding.tools.is_empty() {
                    String::new()
                } else {
                    format!(" tools={}", finding.tools.join(","))
                };
                let events = if finding.event_indices.is_empty() {
                    String::new()
                } else {
                    format!(
                        " events=[{}]",
                        finding
                            .event_indices
                            .iter()
                            .map(u64::to_string)
                            .collect::<Vec<_>>()
                            .join(",")
                    )
                };
                writeln!(
                    f,
                    "    [{}] {}: {}{}{}{}",
                    finding.severity.as_str(),
                    finding.category.as_str(),
                    finding.message,
                    step,
                    tools,
                    events
                )?;
            }
        }
        Ok(())
    }
}

/// Result of [`load_transcript_jsonl`]. Wraps the deserialized
/// envelopes plus the source path the caller passed in.
#[derive(Clone, Debug)]
pub struct LoadedTranscript {
    pub source_path: PathBuf,
    pub events: Vec<PersistedAgentEvent>,
}

/// Read a JSONL transcript file, accepting either:
///   - a path to an `event_log.jsonl` (or rotated `-NNNNNN.jsonl`)
///   - a path to a `.harn-runs/<session-id>/` directory (we'll
///     read every `event_log*.jsonl` under it and sort by index)
pub fn load_transcript_jsonl(path: &Path) -> Result<LoadedTranscript, VmError> {
    let metadata = fs::metadata(path).map_err(|e| {
        VmError::Runtime(format!("failed to stat transcript {}: {e}", path.display()))
    })?;
    let mut events = Vec::new();
    if metadata.is_dir() {
        let mut files: Vec<PathBuf> = fs::read_dir(path)
            .map_err(|e| {
                VmError::Runtime(format!(
                    "failed to read transcript directory {}: {e}",
                    path.display()
                ))
            })?
            .filter_map(|entry| entry.ok())
            .map(|entry| entry.path())
            .filter(|p| {
                p.file_name()
                    .and_then(|n| n.to_str())
                    .map(|name| {
                        name.starts_with("event_log")
                            && p.extension().and_then(|e| e.to_str()) == Some("jsonl")
                    })
                    .unwrap_or(false)
            })
            .collect();
        files.sort();
        if files.is_empty() {
            return Err(VmError::Runtime(format!(
                "no event_log*.jsonl files under {}",
                path.display()
            )));
        }
        for file in &files {
            events.extend(read_jsonl_file(file)?);
        }
    } else {
        events.extend(read_jsonl_file(path)?);
    }
    // Sort by index so multi-file dirs interleave correctly.
    events.sort_by_key(|e| e.index);
    Ok(LoadedTranscript {
        source_path: path.to_path_buf(),
        events,
    })
}

fn read_jsonl_file(path: &Path) -> Result<Vec<PersistedAgentEvent>, VmError> {
    let file = fs::File::open(path).map_err(|e| {
        VmError::Runtime(format!("failed to open transcript {}: {e}", path.display()))
    })?;
    let reader = BufReader::new(file);
    let mut events = Vec::new();
    for (line_no, line) in reader.lines().enumerate() {
        let line = line.map_err(|e| {
            VmError::Runtime(format!(
                "failed to read line {} of {}: {e}",
                line_no + 1,
                path.display()
            ))
        })?;
        let trimmed = line.trim();
        if trimmed.is_empty() {
            continue;
        }
        let event: PersistedAgentEvent = serde_json::from_str(trimmed).map_err(|e| {
            VmError::Runtime(format!(
                "failed to parse line {} of {} as PersistedAgentEvent: {e}",
                line_no + 1,
                path.display()
            ))
        })?;
        events.push(event);
    }
    Ok(events)
}

/// Load a Merge Captain golden fixture from JSON.
pub fn load_merge_captain_golden(path: &Path) -> Result<MergeCaptainGolden, VmError> {
    let bytes = fs::read(path).map_err(|e| {
        VmError::Runtime(format!(
            "failed to read merge_captain golden {}: {e}",
            path.display()
        ))
    })?;
    let golden: MergeCaptainGolden = serde_json::from_slice(&bytes).map_err(|e| {
        VmError::Runtime(format!(
            "failed to parse merge_captain golden {}: {e}",
            path.display()
        ))
    })?;
    Ok(golden)
}

/// Default state-step list applied when a golden does not declare
/// any. Captures the canonical Merge Captain pipeline: intake →
/// verify_checks → review_threads → decide_risk → approval_gate →
/// merge_or_handoff.
fn default_state_steps() -> Vec<GoldenStateStep> {
    vec![
        GoldenStateStep {
            step: "intake".into(),
            tools: vec![ToolPattern {
                glob: Some("*pull_request*".into()),
                ..Default::default()
            }],
            plan_fields: vec!["pr_number".into()],
            events: vec!["plan".into()],
            ..Default::default()
        },
        GoldenStateStep {
            step: "verify_checks".into(),
            tools: vec![
                ToolPattern {
                    glob: Some("*check*".into()),
                    ..Default::default()
                },
                ToolPattern {
                    glob: Some("*ci*".into()),
                    ..Default::default()
                },
                ToolPattern {
                    glob: Some("*workflow_run*".into()),
                    ..Default::default()
                },
            ],
            verifier: true,
            ..Default::default()
        },
        GoldenStateStep {
            step: "decide_risk".into(),
            plan_fields: vec!["review_risk".into()],
            events: vec!["plan".into()],
            ..Default::default()
        },
        GoldenStateStep {
            step: "approval_gate".into(),
            plan_fields: vec!["approval_required".into()],
            events: vec!["handoff".into(), "feedback_injected".into()],
            approval_gate: true,
            ..Default::default()
        },
        GoldenStateStep {
            step: "merge_or_handoff".into(),
            tools: vec![
                ToolPattern {
                    glob: Some("*merge*".into()),
                    ..Default::default()
                },
                ToolPattern {
                    glob: Some("*label*".into()),
                    ..Default::default()
                },
            ],
            events: vec!["handoff".into()],
            merge_action: true,
            ..Default::default()
        },
    ]
}

/// Heuristic: does this tool name look like a write/mutation
/// action? Used by the `UnsafeAttemptedAction` rule when no golden
/// is provided.
pub(crate) fn is_merge_captain_write_tool(name: &str) -> bool {
    let lower = name.to_lowercase();
    lower.contains("merge")
        || lower.contains("write_file")
        || lower.contains("create_pull")
        || lower.contains("_create")
        || lower.contains("create_")
        || lower.contains("delete")
        || lower.contains("force_push")
        || lower.contains("apply_patch")
        || lower.contains("set_label")
        || lower.contains("post_comment")
        || lower.contains("approve")
}

/// Heuristic: does this tool name look like a wait/poll?
fn is_wait_tool(name: &str) -> bool {
    let lower = name.to_lowercase();
    lower.contains("sleep") || lower.contains("wait") || lower.contains("poll")
}

/// Audit a transcript event stream against an optional golden.
pub fn audit_transcript(
    events: &[PersistedAgentEvent],
    golden: Option<&MergeCaptainGolden>,
) -> AuditReport {
    let scenario = golden.map(|g| g.scenario.clone());
    let mut session_ids: Vec<String> = Vec::new();
    let mut model_calls: u64 = 0;
    let mut tool_calls: u64 = 0;
    let mut findings: Vec<AuditFinding> = Vec::new();
    let mut transitions: Vec<StateTransition> = Vec::new();

    let state_steps_owned: Vec<GoldenStateStep> = match golden {
        Some(g) if !g.state_steps.is_empty() => g.state_steps.clone(),
        _ => default_state_steps(),
    };
    let max_repeat = golden.and_then(|g| g.max_repeat).unwrap_or(1);

    // Track repeated tool calls: (tool, arg-hash) per session.
    let mut last_tool_call: BTreeMap<String, (String, String, Vec<u64>)> = BTreeMap::new();

    // Approval state: how many `approval_required: true` plans are
    // outstanding (waiting for a gate). Decremented when an
    // approval_gate step fires.
    let mut pending_approvals: Vec<u64> = Vec::new();

    // Track verifier-fired subjects before any merge_action. Empty-scope
    // verifier steps are still remembered for fixture steps that have no PR
    // identity, but scoped tool actions must verify the same repo/PR.
    let mut verifier_scopes: BTreeSet<String> = BTreeSet::new();

    // Track which steps fired (for required/order checks).
    let mut steps_seen: Vec<String> = Vec::new();

    let mut last_index: u64 = 0;
    let mut saw_terminal: bool = false;

    for env in events {
        last_index = env.index;
        let event = &env.event;
        let session = event.session_id().to_string();
        if !session_ids.contains(&session) {
            session_ids.push(session.clone());
        }

        match event {
            AgentEvent::AgentMessageChunk { .. } | AgentEvent::AgentThoughtChunk { .. } => {
                // Streamed text doesn't count as a model call by
                // itself; we count `IterationStart` instead so each model
                // round-trip is one call regardless of how many
                // chunk events stream.
            }
            AgentEvent::IterationStart { .. } => {
                model_calls += 1;
            }
            AgentEvent::IterationEnd { .. } => {
                saw_terminal = true;
            }
            AgentEvent::BudgetExhausted { .. } => {
                saw_terminal = true;
                findings.push(AuditFinding {
                    category: FindingCategory::ExtraModelCall,
                    severity: FindingSeverity::Error,
                    message: "loop hit max_iterations without resolving".into(),
                    event_indices: vec![env.index],
                    state_step: None,
                    tools: vec![],
                });
            }
            AgentEvent::LoopStuck { .. } => {
                saw_terminal = true;
                findings.push(AuditFinding {
                    category: FindingCategory::ExtraModelCall,
                    severity: FindingSeverity::Error,
                    message: "loop stuck on consecutive text-only turns".into(),
                    event_indices: vec![env.index],
                    state_step: None,
                    tools: vec![],
                });
            }
            AgentEvent::LoopStuckSignal { payload, .. } => {
                let terminal = payload
                    .get("terminal")
                    .and_then(serde_json::Value::as_bool)
                    .unwrap_or(true);
                if terminal {
                    saw_terminal = true;
                    findings.push(AuditFinding {
                        category: FindingCategory::ExtraModelCall,
                        severity: FindingSeverity::Error,
                        message: "loop stuck on pipeline no-progress signal".into(),
                        event_indices: vec![env.index],
                        state_step: None,
                        tools: vec![],
                    });
                }
            }
            AgentEvent::Handoff { .. } => {
                saw_terminal = true;
                // Approval-gate step (default) consumes any pending
                // approval.
                if !pending_approvals.is_empty() {
                    pending_approvals.clear();
                }
                check_state_transition(
                    &state_steps_owned,
                    StepTrigger::Event("handoff"),
                    env.index,
                    "handoff",
                    &mut transitions,
                    &mut steps_seen,
                    &mut findings,
                    &mut pending_approvals,
                    &mut verifier_scopes,
                );
            }
            AgentEvent::FeedbackInjected { kind, .. } => {
                if kind.eq_ignore_ascii_case("approval") || kind.eq_ignore_ascii_case("approved") {
                    pending_approvals.clear();
                }
                check_state_transition(
                    &state_steps_owned,
                    StepTrigger::Event("feedback_injected"),
                    env.index,
                    "feedback_injected",
                    &mut transitions,
                    &mut steps_seen,
                    &mut findings,
                    &mut pending_approvals,
                    &mut verifier_scopes,
                );
            }
            AgentEvent::OrchestrationDecision { decision: plan, .. } => {
                check_plan_transitions(
                    &state_steps_owned,
                    plan,
                    env.index,
                    &mut transitions,
                    &mut steps_seen,
                    &mut findings,
                    &mut pending_approvals,
                    &mut verifier_scopes,
                );
                if let Some(approval) = plan
                    .get("approval_required")
                    .and_then(serde_json::Value::as_bool)
                {
                    if approval {
                        pending_approvals.push(env.index);
                    }
                }
                if !plan.is_object() {
                    findings.push(AuditFinding {
                        category: FindingCategory::InvalidStructuredOutput,
                        severity: FindingSeverity::Error,
                        message: "Plan event payload was not a JSON object".into(),
                        event_indices: vec![env.index],
                        state_step: None,
                        tools: vec![],
                    });
                }
            }
            AgentEvent::ToolCall {
                tool_name,
                raw_input,
                status,
                ..
            } => {
                tool_calls += 1;
                // Repeated-read detection.
                let arg_hash = crate::canonical_json::to_string(raw_input);
                match last_tool_call.get_mut(&session) {
                    Some(entry) if entry.0 == *tool_name && entry.1 == arg_hash => {
                        entry.2.push(env.index);
                        if (entry.2.len() as u32) > max_repeat {
                            let indices = entry.2.clone();
                            findings.push(AuditFinding {
                                category: FindingCategory::RepeatedRead,
                                severity: FindingSeverity::Error,
                                message: format!(
                                    "tool `{}` called {} times consecutively with identical args",
                                    tool_name,
                                    indices.len()
                                ),
                                event_indices: indices,
                                state_step: None,
                                tools: vec![tool_name.clone()],
                            });
                            // Reset so we don't emit a finding per call.
                            *entry = (tool_name.clone(), arg_hash.clone(), vec![env.index]);
                        }
                    }
                    _ => {
                        last_tool_call.insert(
                            session.clone(),
                            (tool_name.clone(), arg_hash.clone(), vec![env.index]),
                        );
                    }
                }

                // Bad-wait detection: a wait/sleep/poll without
                // arguments that indicate progress.
                if is_wait_tool(tool_name) {
                    let indicates_progress = raw_input
                        .as_object()
                        .map(|obj| {
                            obj.contains_key("until")
                                || obj.contains_key("condition")
                                || obj.contains_key("subscription_id")
                        })
                        .unwrap_or(false);
                    if !indicates_progress {
                        findings.push(AuditFinding {
                            category: FindingCategory::BadWait,
                            severity: FindingSeverity::Warn,
                            message: format!(
                                "wait/poll tool `{tool_name}` invoked without progress predicate (until/condition/subscription_id)"
                            ),
                            event_indices: vec![env.index],
                            state_step: None,
                            tools: vec![tool_name.clone()],
                        });
                    }
                }

                // Unsafe attempted action: check golden's
                // require_approval_for, falling back to a default
                // write-tool heuristic.
                let needs_approval_match = match golden {
                    Some(g) if !g.require_approval_for.is_empty() => {
                        g.require_approval_for.iter().any(|p| p.matches(tool_name))
                    }
                    _ => is_merge_captain_write_tool(tool_name),
                };
                if needs_approval_match
                    && pending_approvals.is_empty()
                    && !already_approved(&steps_seen, &state_steps_owned)
                {
                    findings.push(AuditFinding {
                        category: FindingCategory::UnsafeAttemptedAction,
                        severity: FindingSeverity::Error,
                        message: format!(
                            "tool `{tool_name}` requires prior approval gate, but none observed"
                        ),
                        event_indices: vec![env.index],
                        state_step: None,
                        tools: vec![tool_name.clone()],
                    });
                }

                // Forbidden actions.
                if let Some(g) = golden {
                    if g.forbidden_actions.iter().any(|p| p.matches(tool_name)) {
                        findings.push(AuditFinding {
                            category: FindingCategory::ForbiddenAction,
                            severity: FindingSeverity::Error,
                            message: format!(
                                "tool `{}` is forbidden in scenario `{}`",
                                tool_name, g.scenario
                            ),
                            event_indices: vec![env.index],
                            state_step: None,
                            tools: vec![tool_name.clone()],
                        });
                    }
                }

                // Tool-triggered state transitions. Mutating steps use the
                // repo/PR scope to ensure verification happened for the same
                // PR, not merely earlier in the sweep.
                check_state_transition(
                    &state_steps_owned,
                    StepTrigger::Tool {
                        name: tool_name,
                        scope: transition_scope(raw_input),
                    },
                    env.index,
                    tool_name,
                    &mut transitions,
                    &mut steps_seen,
                    &mut findings,
                    &mut pending_approvals,
                    &mut verifier_scopes,
                );
                let _ = status;
            }
            AgentEvent::ToolCallUpdate {
                status,
                error,
                error_category,
                tool_name,
                ..
            } => {
                if matches!(status, ToolCallStatus::Failed) {
                    if let Some(category) = error_category {
                        if matches!(category, ToolCallErrorCategory::SchemaValidation) {
                            findings.push(AuditFinding {
                                category: FindingCategory::InvalidStructuredOutput,
                                severity: FindingSeverity::Error,
                                message: format!(
                                    "tool `{}` failed schema validation: {}",
                                    tool_name,
                                    error.clone().unwrap_or_default()
                                ),
                                event_indices: vec![env.index],
                                state_step: None,
                                tools: vec![tool_name.clone()],
                            });
                        }
                    }
                }
            }
            _ => {
                // Other events (skill, tool_search, fs_watch, worker
                // updates) are not part of the oracle today.
            }
        }
    }

    // Suite-level checks.
    if !pending_approvals.is_empty() {
        findings.push(AuditFinding {
            category: FindingCategory::MissingApproval,
            severity: FindingSeverity::Error,
            message: format!(
                "{} plan(s) declared approval_required: true with no following approval gate",
                pending_approvals.len()
            ),
            event_indices: pending_approvals.clone(),
            state_step: Some("approval_gate".into()),
            tools: vec![],
        });
    }

    if !events.is_empty() && !saw_terminal {
        findings.push(AuditFinding {
            category: FindingCategory::IncompleteTranscript,
            severity: FindingSeverity::Warn,
            message:
                "transcript ended without a IterationEnd / Handoff / BudgetExhausted / LoopStuck event"
                    .into(),
            event_indices: vec![last_index],
            state_step: None,
            tools: vec![],
        });
    }

    // Required state steps.
    for step in &state_steps_owned {
        if step.required && !steps_seen.iter().any(|s| s == &step.step) {
            findings.push(AuditFinding {
                category: FindingCategory::MissingStateStep,
                severity: FindingSeverity::Error,
                message: format!("required state step `{}` was never reached", step.step),
                event_indices: vec![],
                state_step: Some(step.step.clone()),
                tools: vec![],
            });
        }
    }

    // Step ordering: each step must appear at most once before any
    // step later in the golden's declaration order. We flag if we
    // see step B fire and then step A (where A is declared before B)
    // fire afterwards.
    let order: BTreeMap<&str, usize> = state_steps_owned
        .iter()
        .enumerate()
        .map(|(i, s)| (s.step.as_str(), i))
        .collect();
    let mut highest: usize = 0;
    let mut last_step: Option<&str> = None;
    for step in &steps_seen {
        if let Some(idx) = order.get(step.as_str()) {
            if *idx + 1 < highest && last_step != Some(step.as_str()) {
                findings.push(AuditFinding {
                    category: FindingCategory::StateOutOfOrder,
                    severity: FindingSeverity::Warn,
                    message: format!("state step `{step}` fired after a later step"),
                    event_indices: vec![],
                    state_step: Some(step.clone()),
                    tools: vec![],
                });
            }
            if *idx > highest {
                highest = *idx;
            }
            last_step = Some(step.as_str());
        }
    }

    if let Some(g) = golden {
        if !g.expected_state_transitions.is_empty() {
            let observed: Vec<String> = transitions
                .iter()
                .map(|transition| transition.step.clone())
                .collect();
            if observed != g.expected_state_transitions {
                findings.push(AuditFinding {
                    category: FindingCategory::StateSequenceMismatch,
                    severity: FindingSeverity::Error,
                    message: format!(
                        "state transitions {:?} did not match expected {:?}",
                        observed, g.expected_state_transitions
                    ),
                    event_indices: vec![],
                    state_step: None,
                    tools: vec![],
                });
            }
        }
    }

    // Tool-budget check.
    if let Some(g) = golden {
        if let Some(max) = g.max_tool_calls {
            if tool_calls > max {
                findings.push(AuditFinding {
                    category: FindingCategory::NonMinimalToolUsage,
                    severity: FindingSeverity::Error,
                    message: format!("tool calls ({tool_calls}) exceeded scenario budget ({max})"),
                    event_indices: vec![],
                    state_step: None,
                    tools: vec![],
                });
            }
        }
        if let Some(max) = g.max_model_calls {
            if model_calls > max {
                findings.push(AuditFinding {
                    category: FindingCategory::ExtraModelCall,
                    severity: FindingSeverity::Error,
                    message: format!(
                        "model calls ({model_calls}) exceeded scenario budget ({max})"
                    ),
                    event_indices: vec![],
                    state_step: None,
                    tools: vec![],
                });
            }
        }
    }

    let pass = findings
        .iter()
        .all(|f| f.severity != FindingSeverity::Error);

    AuditReport {
        scenario,
        source_path: None,
        session_ids,
        event_count: events.len() as u64,
        model_call_count: model_calls,
        tool_call_count: tool_calls,
        findings,
        state_transitions: transitions,
        pass,
    }
}

enum StepTrigger<'a> {
    Tool {
        name: &'a str,
        scope: Option<String>,
    },
    Event(&'a str),
}

#[allow(clippy::too_many_arguments)]
fn check_state_transition(
    steps: &[GoldenStateStep],
    trigger: StepTrigger,
    event_index: u64,
    triggered_by: &str,
    transitions: &mut Vec<StateTransition>,
    steps_seen: &mut Vec<String>,
    findings: &mut Vec<AuditFinding>,
    pending_approvals: &mut Vec<u64>,
    verifier_scopes: &mut BTreeSet<String>,
) {
    for step in steps {
        let matched = match &trigger {
            StepTrigger::Tool { name, .. } => step.tools.iter().any(|p| p.matches(name)),
            StepTrigger::Event(name) => step.events.iter().any(|e| e.eq_ignore_ascii_case(name)),
        };
        if !matched {
            continue;
        }
        let scope = match &trigger {
            StepTrigger::Tool { scope, .. } => scope.clone(),
            StepTrigger::Event(_) => None,
        };
        record_step(
            step,
            event_index,
            triggered_by,
            scope.as_deref(),
            transitions,
            steps_seen,
            findings,
            pending_approvals,
            verifier_scopes,
        );
        // Continue: a single event may match multiple steps when
        // golden patterns overlap (e.g. "*pull_request*" intake +
        // "*merge_pull_request*" merge). Each fires independently;
        // dedup happens in `record_step`'s `steps_seen` check.
    }
}

#[allow(clippy::too_many_arguments)]
fn check_plan_transitions(
    steps: &[GoldenStateStep],
    plan: &serde_json::Value,
    event_index: u64,
    transitions: &mut Vec<StateTransition>,
    steps_seen: &mut Vec<String>,
    findings: &mut Vec<AuditFinding>,
    pending_approvals: &mut Vec<u64>,
    verifier_scopes: &mut BTreeSet<String>,
) {
    let obj = match plan.as_object() {
        Some(o) => o,
        None => return,
    };
    for step in steps {
        let plan_match = step.plan_fields.iter().any(|field| {
            if step.approval_gate && field == "approval_required" {
                obj.get(field).and_then(serde_json::Value::as_bool) == Some(true)
            } else {
                obj.contains_key(field)
            }
        });
        let event_match = step.events.iter().any(|e| e.eq_ignore_ascii_case("plan"));
        if !(plan_match || (event_match && step.plan_fields.is_empty())) {
            continue;
        }
        if !plan_match && !event_match {
            continue;
        }
        record_step(
            step,
            event_index,
            "plan",
            transition_scope(plan).as_deref(),
            transitions,
            steps_seen,
            findings,
            pending_approvals,
            verifier_scopes,
        );
    }
}

#[allow(clippy::too_many_arguments)]
fn record_step(
    step: &GoldenStateStep,
    event_index: u64,
    triggered_by: &str,
    scope: Option<&str>,
    transitions: &mut Vec<StateTransition>,
    steps_seen: &mut Vec<String>,
    findings: &mut Vec<AuditFinding>,
    pending_approvals: &mut Vec<u64>,
    verifier_scopes: &mut BTreeSet<String>,
) {
    transitions.push(StateTransition {
        step: step.step.clone(),
        event_index,
        triggered_by: triggered_by.to_string(),
    });
    if !steps_seen.contains(&step.step) {
        steps_seen.push(step.step.clone());
    }
    if step.approval_gate {
        pending_approvals.clear();
    }
    if step.verifier {
        verifier_scopes.insert(scope.unwrap_or("*").to_string());
    }
    let verified = scope
        .map(|scope| verifier_scopes.contains(scope) || verifier_scopes.contains("*"))
        .unwrap_or_else(|| !verifier_scopes.is_empty());
    if step.merge_action && !verified {
        findings.push(AuditFinding {
            category: FindingCategory::SkippedVerification,
            severity: FindingSeverity::Error,
            message: format!(
                "merge action `{}` reached without a preceding verifier step",
                step.step
            ),
            event_indices: vec![event_index],
            state_step: Some(step.step.clone()),
            tools: vec![],
        });
    }
}

fn transition_scope(value: &serde_json::Value) -> Option<String> {
    let repo = value.get("repo").and_then(serde_json::Value::as_str)?;
    let pr_number = value
        .get("pr_number")
        .or_else(|| value.get("number"))
        .and_then(serde_json::Value::as_u64)?;
    Some(format!("{repo}#{pr_number}"))
}

fn already_approved(steps_seen: &[String], steps: &[GoldenStateStep]) -> bool {
    steps
        .iter()
        .filter(|s| s.approval_gate)
        .any(|s| steps_seen.contains(&s.step))
}

#[cfg(test)]
mod tests;