car-eventlog 0.55.0

Event log with JSONL persistence for Common Agent Runtime
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
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
//! Harness-level evaluation metrics.
//!
//! Survey "Code as Agent Harness" §5.2.1: end-task success conflates the
//! base model, the harness, the tools, and the environment. To evaluate the
//! *operational substrate itself*, success accuracy must be complemented by
//! measurements of "execution reliability, feedback quality, context
//! sustainability, safety, coordination, and reproducibility." This module
//! derives those dimensions from the deep telemetry the event log now
//! records (token/cost/latency metrics, branch decisions, rejected
//! alternatives, permission decisions — see `car_eventlog`).
//!
//! These are descriptive, not normative: they characterize a trajectory so
//! harness variants can be compared, and an Evolution Agent (§3.5.2) can
//! attribute cost/failure to specific harness components.

use crate::{Event, EventKind, MetricsTotals};
use serde::{Deserialize, Serialize};

/// The six §5.2.1 dimensions, computed from a trajectory's event stream.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct HarnessMetrics {
    pub trajectory_efficiency: TrajectoryEfficiency,
    pub verification_strength: VerificationStrength,
    pub recovery: Recovery,
    pub state_consistency: StateConsistency,
    pub safety: Safety,
    pub replayability: Replayability,
    /// JSONL lines that failed to parse when computing from a journal tail
    /// (0 when computed from in-memory events). Nonzero means the metrics
    /// were computed over a partial event set — do not compare across
    /// harnesses without accounting for it (neo review).
    pub parse_errors: usize,
    /// Share of graded TASKS the harness completed, when a task-suite runner
    /// measured one (`car-bench-harness --metrics-out`).
    ///
    /// This is **end-task** success, and it is a different quantity from
    /// [`TrajectoryEfficiency::success_rate`], which is attempt-level: a
    /// candidate whose every tool call succeeds while it fails more tasks
    /// scores 1.0 there and lower here. That gap is why the regression gate
    /// guards both.
    ///
    /// It cannot be derived from an event stream — the log knows what ran, not
    /// whether the task was satisfied — so [`compute_harness_metrics`] always
    /// leaves it `None`. Only a runner holding the task suite and its grading
    /// criteria can fill it in.
    ///
    /// **`None` means "not measured", never "zero".** A consumer that defaults
    /// it to 0.0 turns an unmeasured run into a total failure; a consumer that
    /// defaults it to 1.0 turns one into a perfect score. The gate does neither
    /// — with either side absent it simply does not fire.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub task_pass_rate: Option<f64>,
    /// How many tasks [`HarnessMetrics::task_pass_rate`] is a rate **over**.
    ///
    /// A bare rate is not comparable across runs, and the gap is exploitable
    /// rather than merely untidy. A runner drops a task from its denominator
    /// whenever it cannot measure it — and whether it can measure a task
    /// depends on the harness's own toolset, which is a declared, promotable
    /// mutation target (`HarnessComponent::ToolSchema`). Shrink the toolset so
    /// the four file-reading tasks become unmeasurable and they leave the
    /// denominator: exactly the four a file-blind harness would have *failed*.
    /// The scalar goes up, and without this field nothing downstream can see
    /// that the two rates count different things.
    ///
    /// `EvolutionAgent::evaluate` refuses the comparison outright when both
    /// sides carry a denominator and the two disagree.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub task_pass_denominator: Option<usize>,
    /// Tasks the runner ran against but could **not** measure, and therefore
    /// left out of [`HarnessMetrics::task_pass_denominator`].
    ///
    /// Reported for the same reason: a denominator that shrank is a fact, and
    /// the count of what fell out of it is the first thing anyone comparing two
    /// runs needs. Zero and absent are different — absent means the runner does
    /// not track it.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub tasks_unrunnable: Option<usize>,
}

/// (i) Trajectory efficiency — how much work was spent reaching the outcome.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct TrajectoryEfficiency {
    /// Successful actions plus *failed attempts* (a retried action
    /// contributes one success and N failed attempts).
    pub attempts_total: usize,
    pub actions_succeeded: usize,
    /// Failed *attempts* (`ActionFailed` events), which include retries —
    /// see `success_rate`.
    pub failed_attempts: usize,
    /// Sum of input + output tokens across inference events.
    pub total_tokens: u64,
    pub total_cost_usd: f64,
    /// Summed wall-clock across metered events (ms).
    pub wall_clock_ms: f64,
    /// Number of model calls in the trajectory — one per
    /// [`EventKind::InferenceMetered`] event.
    ///
    /// This is the "model calls" leg of the #813 A/B (pass rate, model calls,
    /// tokens per task): a harness change that cuts `total_tokens` by making
    /// *more* calls has not saved anything, so the two are only interpretable
    /// together. Counted from the metered events rather than from a loop's own
    /// turn counter, so it stays correct if a turn ever issues more than one
    /// call — today the assistant loop issues exactly one, but that is a
    /// property of the loop, not of this metric.
    ///
    /// A call whose provider reported no usage still counts here (the call
    /// happened) while contributing nothing to `total_tokens` — which is why
    /// `total_tokens / model_calls` is not a safe per-call average.
    pub model_calls: usize,
    /// **Attempt-level** success: succeeded / (succeeded + failed_attempts).
    /// Counts every `ActionFailed` including retries, so a
    /// retried-then-succeeded action lowers this — a harness with
    /// aggressive retry (good recovery) scores lower here than one that
    /// gives up. Read it alongside `recovery.retries`. `None` when no
    /// attempts ran.
    pub success_rate: Option<f64>,
}

/// (ii) Verification strength — how much the harness checked before
/// accepting. False-acceptance rate needs an external oracle, so it is left
/// `None` here; what the log *can* show is how often verification rejected
/// or policy blocked an action (the verifier doing work).
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct VerificationStrength {
    pub actions_validated: usize,
    pub actions_rejected: usize,
    pub policy_violations: usize,
    /// Rejections / (validated + rejected) — the share caught **by the
    /// validator** before execution (excludes policy blocks, counted
    /// separately). `None` when nothing was validated.
    pub rejection_rate: Option<f64>,
}

/// (iii) Recovery ability — can the harness diagnose and repair failures?
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct Recovery {
    pub replan_attempts: usize,
    pub replan_rejected: usize,
    pub replan_exhausted: usize,
    /// Action-level retries (`ActionRetrying` events) — the within-action
    /// recovery the attempt-level `success_rate` counts against.
    pub retries: usize,
    /// Branch decisions + rejected alternatives — the size of the search
    /// the harness explored when things went wrong.
    pub branch_decisions: usize,
    pub alternatives_rejected: usize,
}

/// (iv) State consistency — how much the shared state churned and how often
/// the harness had to roll back.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct StateConsistency {
    pub state_changes: usize,
    pub snapshots: usize,
    pub rollbacks: usize,
}

/// (v) Safety compliance — permission-gate activity (from the §5.2.5 tier
/// gate). High escalation/denial counts mean the harness governed real
/// risk rather than running unsupervised.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct Safety {
    pub permission_decisions: usize,
    pub escalations: usize,
    pub denials: usize,
    /// EVERY durable human-in-the-loop decision, approvals and rejections
    /// alike.
    pub approvals_recorded: usize,
    /// The subset of [`Self::approvals_recorded`] that were REJECTIONS.
    ///
    /// `ApprovalRecorded` has always carried `approval: approved | rejected`
    /// in its data, and this fold has always thrown that away — so "the
    /// overseer approved 40 things" and "the overseer rejected 40 things"
    /// produced an identical `Safety`, despite being opposite safety facts.
    /// Splitting them makes the disagreement rate (`approvals_rejected /
    /// approvals_recorded`) computable, which is the one signal that
    /// distinguishes a reviewer who is still reading from one who has
    /// started waving things through.
    #[serde(default)]
    pub approvals_rejected: usize,
}

/// (vi) Replayability — what the log carries for reconstruction and audit.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct Replayability {
    pub total_events: usize,
    /// True when the log carries engine-state delta records (`StateChanged`
    /// / `StateSnapshot`) — i.e. the trajectory's *state effects* can be
    /// replayed. This is distinct from auditability: a pure side-effecting
    /// tool (sends an email, writes an untracked file) succeeds with no
    /// state delta, so this is `false` even though the action is fully
    /// logged. Read it as "captured state effects", not "is auditable"
    /// (neo review — the append-only log is auditable regardless).
    pub state_effects_captured: bool,
}

/// Compute harness-level metrics from a trajectory's events.
///
/// [`HarnessMetrics::task_pass_rate`] — and with it
/// [`HarnessMetrics::task_pass_denominator`] and
/// [`HarnessMetrics::tasks_unrunnable`] — is deliberately left `None`:
/// end-task success is not in the event stream, and inventing a value here
/// would put a fabricated number in front of the regression gate.
pub fn compute_harness_metrics(events: &[Event]) -> HarnessMetrics {
    let mut m = HarnessMetrics::default();
    let totals: MetricsTotals = crate::metrics_totals_of(events);

    let mut succeeded = 0usize;
    let mut failed = 0usize;
    let mut model_calls = 0usize;
    let mut any_state_record = false;

    for ev in events {
        match ev.kind {
            EventKind::InferenceMetered => model_calls += 1,
            EventKind::ActionSucceeded => succeeded += 1,
            EventKind::ActionFailed => failed += 1,
            EventKind::ActionRetrying => m.recovery.retries += 1,
            EventKind::ActionValidated => m.verification_strength.actions_validated += 1,
            EventKind::ActionRejected => m.verification_strength.actions_rejected += 1,
            EventKind::PolicyViolation => m.verification_strength.policy_violations += 1,
            EventKind::ReplanAttempted => m.recovery.replan_attempts += 1,
            EventKind::ReplanRejected => m.recovery.replan_rejected += 1,
            EventKind::ReplanExhausted => m.recovery.replan_exhausted += 1,
            EventKind::BranchDecision => m.recovery.branch_decisions += 1,
            EventKind::AlternativeRejected => m.recovery.alternatives_rejected += 1,
            EventKind::StateChanged => {
                m.state_consistency.state_changes += 1;
                any_state_record = true;
            }
            EventKind::StateSnapshot => {
                m.state_consistency.snapshots += 1;
                any_state_record = true;
            }
            EventKind::StateRollback => m.state_consistency.rollbacks += 1,
            EventKind::PermissionDecision => {
                m.safety.permission_decisions += 1;
                match ev.data.get("gate_decision").and_then(|v| v.as_str()) {
                    Some("needs_approval") => m.safety.escalations += 1,
                    Some("deny") => m.safety.denials += 1,
                    _ => {}
                }
            }
            EventKind::ApprovalRecorded => {
                m.safety.approvals_recorded += 1;
                if ev.data.get("approval").and_then(|v| v.as_str()) == Some("rejected") {
                    m.safety.approvals_rejected += 1;
                }
            }
            _ => {}
        }
    }

    m.trajectory_efficiency = TrajectoryEfficiency {
        attempts_total: succeeded + failed,
        actions_succeeded: succeeded,
        failed_attempts: failed,
        total_tokens: totals.tokens,
        total_cost_usd: totals.cost_usd,
        wall_clock_ms: totals.duration_ms,
        model_calls,
        success_rate: ratio(succeeded, succeeded + failed),
    };
    m.verification_strength.rejection_rate = ratio(
        m.verification_strength.actions_rejected,
        m.verification_strength.actions_validated + m.verification_strength.actions_rejected,
    );
    m.replayability = Replayability {
        total_events: events.len(),
        state_effects_captured: any_state_record,
    };
    m
}

fn ratio(num: usize, denom: usize) -> Option<f64> {
    (denom > 0).then(|| num as f64 / denom as f64)
}

/// Compute harness metrics from a JSONL string of events (one `Event` per
/// line) — the form the FFI passes from a journal tail. Unparseable lines
/// are counted in `parse_errors` (not silently dropped), so a half-corrupt
/// tail is distinguishable from a clean one (neo review).
pub fn compute_from_jsonl(jsonl: &str) -> HarnessMetrics {
    let mut parse_errors = 0usize;
    let events: Vec<Event> = jsonl
        .lines()
        .filter(|l| !l.trim().is_empty())
        .filter_map(|l| match serde_json::from_str::<Event>(l) {
            Ok(ev) => Some(ev),
            Err(_) => {
                parse_errors += 1;
                None
            }
        })
        .collect();
    let mut m = compute_harness_metrics(&events);
    m.parse_errors = parse_errors;
    m
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{EventLog, Metrics};

    #[test]
    fn efficiency_and_success_rate() {
        let mut log = EventLog::new();
        log.append_metered(
            EventKind::ActionSucceeded,
            Some("a1"),
            None,
            Default::default(),
            Metrics::latency(50.0),
        );
        log.append_metered(
            EventKind::InferenceMetered,
            None,
            None,
            Default::default(),
            Metrics::inference(100, 40, Some(0.01)),
        );
        log.append(
            EventKind::ActionFailed,
            Some("a2"),
            None,
            Default::default(),
        );

        let m = compute_harness_metrics(log.events());
        assert_eq!(m.trajectory_efficiency.attempts_total, 2);
        assert_eq!(m.trajectory_efficiency.actions_succeeded, 1);
        assert_eq!(m.trajectory_efficiency.failed_attempts, 1);
        assert_eq!(m.trajectory_efficiency.total_tokens, 140);
        assert_eq!(m.trajectory_efficiency.wall_clock_ms, 50.0);
        assert_eq!(m.trajectory_efficiency.success_rate, Some(0.5));
    }

    #[test]
    fn retried_then_succeeded_lowers_attempt_success_but_counts_retries() {
        // One logical action: 2 failed attempts + 2 retries + 1 success.
        let mut log = EventLog::new();
        log.append(
            EventKind::ActionFailed,
            Some("a1"),
            None,
            Default::default(),
        );
        log.append(
            EventKind::ActionRetrying,
            Some("a1"),
            None,
            Default::default(),
        );
        log.append(
            EventKind::ActionFailed,
            Some("a1"),
            None,
            Default::default(),
        );
        log.append(
            EventKind::ActionRetrying,
            Some("a1"),
            None,
            Default::default(),
        );
        log.append(
            EventKind::ActionSucceeded,
            Some("a1"),
            None,
            Default::default(),
        );

        let m = compute_harness_metrics(log.events());
        assert_eq!(m.trajectory_efficiency.failed_attempts, 2);
        assert_eq!(m.recovery.retries, 2);
        // Attempt-level success_rate reflects the retries (1/3), and the
        // retries field explains why — the two are read together.
        assert_eq!(m.trajectory_efficiency.success_rate, Some(1.0 / 3.0));
    }

    /// `model_calls` must equal the number of `InferenceMetered` events —
    /// not the number of actions, and not the number of events that happen to
    /// carry token metrics. The #813 A/B reads tokens *per model call*, so a
    /// count that drifted from the metered events would misattribute the whole
    /// comparison.
    #[test]
    fn model_calls_equals_inference_metered_event_count() {
        let mut log = EventLog::new();
        // Three model calls, one of which reported no usage (a provider that
        // cannot count tokens) — it is still a call.
        log.append_metered(
            EventKind::InferenceMetered,
            None,
            None,
            Default::default(),
            Metrics::inference(100, 40, Some(0.01)),
        );
        log.append_metered(
            EventKind::InferenceMetered,
            None,
            None,
            Default::default(),
            Metrics::inference(60, 10, None),
        );
        log.append_metered(
            EventKind::InferenceMetered,
            None,
            None,
            Default::default(),
            Metrics::latency(12.0),
        );
        // Non-inference events must not inflate the count, including a metered
        // action that carries latency.
        log.append_metered(
            EventKind::ActionSucceeded,
            Some("a1"),
            None,
            Default::default(),
            Metrics::latency(50.0),
        );
        log.append(
            EventKind::ActionFailed,
            Some("a2"),
            None,
            Default::default(),
        );

        let m = compute_harness_metrics(log.events());
        let metered = log
            .events()
            .iter()
            .filter(|e| e.kind == EventKind::InferenceMetered)
            .count();
        assert_eq!(metered, 3, "fixture should hold three metered calls");
        assert_eq!(m.trajectory_efficiency.model_calls, metered);
        // The usage-less third call contributes no tokens but still counts.
        assert_eq!(m.trajectory_efficiency.total_tokens, 210);
        assert_eq!(m.trajectory_efficiency.attempts_total, 2);
    }

    /// An empty trajectory has made no model calls — `model_calls` must be 0,
    /// never a default that reads as "one call".
    #[test]
    fn no_inference_events_means_no_model_calls() {
        let mut log = EventLog::new();
        log.append(
            EventKind::ActionSucceeded,
            Some("a1"),
            None,
            Default::default(),
        );
        assert_eq!(
            compute_harness_metrics(log.events())
                .trajectory_efficiency
                .model_calls,
            0
        );
        assert_eq!(
            compute_harness_metrics(&[])
                .trajectory_efficiency
                .model_calls,
            0
        );
    }

    /// `model_calls` must survive the JSONL round trip the FFI and
    /// `car-bench-harness --metrics-out` use, since that is the form the
    /// evolution gate receives as `harness_candidate_metrics`.
    #[test]
    fn model_calls_survives_json_round_trip() {
        let mut log = EventLog::new();
        log.append_metered(
            EventKind::InferenceMetered,
            None,
            None,
            Default::default(),
            Metrics::inference(7, 3, None),
        );
        let jsonl = serde_json::to_string(&log.events()[0]).unwrap();
        let from_jsonl = compute_from_jsonl(&jsonl);
        assert_eq!(from_jsonl.trajectory_efficiency.model_calls, 1);

        let encoded = serde_json::to_string(&from_jsonl).unwrap();
        let decoded: HarnessMetrics = serde_json::from_str(&encoded).unwrap();
        assert_eq!(decoded.trajectory_efficiency.model_calls, 1);
        assert_eq!(decoded.trajectory_efficiency.total_tokens, 10);
    }

    /// `task_pass_rate` is a task-suite measurement, not an event-stream one.
    /// Computing it here would mean fabricating it, so it must come back
    /// `None` — "not measured" — even from a rich trajectory.
    #[test]
    fn task_pass_rate_is_never_derived_from_events() {
        let mut log = EventLog::new();
        log.append_metered(
            EventKind::InferenceMetered,
            None,
            None,
            Default::default(),
            Metrics::inference(100, 40, Some(0.01)),
        );
        log.append(
            EventKind::ActionSucceeded,
            Some("a1"),
            None,
            Default::default(),
        );
        assert_eq!(compute_harness_metrics(log.events()).task_pass_rate, None);
        assert_eq!(compute_harness_metrics(&[]).task_pass_rate, None);
        assert_eq!(compute_from_jsonl("").task_pass_rate, None);

        // Same rule for the denominator that qualifies it. A folded event
        // stream that reported "0 tasks over 0" would be a fabricated
        // denominator, and the gate would then compare it against a real one.
        let m = compute_harness_metrics(log.events());
        assert_eq!(m.task_pass_denominator, None);
        assert_eq!(m.tasks_unrunnable, None);
    }

    /// The denominator has to reach the gate through the same file
    /// `--metrics-out` writes, and a document written before the field existed
    /// must still load — as `None`, which is what makes the gate's
    /// comparability check skip rather than reject.
    #[test]
    fn task_pass_denominator_round_trips_and_is_backward_compatible() {
        let mut m = compute_harness_metrics(&[]);
        m.task_pass_rate = Some(0.75);
        m.task_pass_denominator = Some(12);
        m.tasks_unrunnable = Some(2);

        let encoded = serde_json::to_string(&m).unwrap();
        let decoded: HarnessMetrics = serde_json::from_str(&encoded).unwrap();
        assert_eq!(decoded.task_pass_denominator, Some(12));
        assert_eq!(decoded.tasks_unrunnable, Some(2));

        // Absent in an older document → None, never 0.
        let old = r#"{"task_pass_rate":0.75}"#;
        let decoded: HarnessMetrics = serde_json::from_str(old).unwrap();
        assert_eq!(decoded.task_pass_rate, Some(0.75));
        assert_eq!(decoded.task_pass_denominator, None);
        assert_eq!(decoded.tasks_unrunnable, None);

        // And an unmeasured document carries neither key at all, so a reader
        // cannot mistake a zero for a measurement.
        let bare = serde_json::to_value(compute_harness_metrics(&[])).unwrap();
        assert!(bare.get("task_pass_denominator").is_none());
        assert!(bare.get("tasks_unrunnable").is_none());
    }

    /// A runner that DID measure it must be able to ship the number to the
    /// gate: it has to survive the JSON round trip `--metrics-out` uses, and an
    /// older document without the field must still load (as `None`).
    #[test]
    fn task_pass_rate_round_trips_and_is_backward_compatible() {
        let mut m = compute_harness_metrics(&[]);
        m.task_pass_rate = Some(0.75);
        let encoded = serde_json::to_string(&m).unwrap();
        let decoded: HarnessMetrics = serde_json::from_str(&encoded).unwrap();
        assert_eq!(decoded.task_pass_rate, Some(0.75));

        // A document written before this field existed carries no key at all.
        let older = r#"{"trajectory_efficiency":{"model_calls":3}}"#;
        let loaded: HarnessMetrics = serde_json::from_str(older).unwrap();
        assert_eq!(loaded.task_pass_rate, None);
        assert_eq!(loaded.trajectory_efficiency.model_calls, 3);
    }

    /// Approve and reject are opposite safety facts. The fold used to
    /// increment one counter for both, so a `Safety` could not say which had
    /// happened.
    #[test]
    fn approvals_and_rejections_are_counted_separately() {
        let mut log = EventLog::new();
        for decision in ["approved", "rejected", "rejected"] {
            log.append(
                EventKind::ApprovalRecorded,
                None,
                None,
                [("approval".to_string(), decision.into())].into(),
            );
        }

        let m = compute_harness_metrics(log.events());
        assert_eq!(m.safety.approvals_recorded, 3, "every decision is recorded");
        assert_eq!(
            m.safety.approvals_rejected, 2,
            "two of them were rejections"
        );
    }

    #[test]
    fn recovery_and_safety_counts() {
        let mut log = EventLog::new();
        log.append(EventKind::ReplanAttempted, None, None, Default::default());
        log.append(EventKind::BranchDecision, None, None, Default::default());
        log.append(
            EventKind::AlternativeRejected,
            None,
            None,
            Default::default(),
        );
        log.append(
            EventKind::PermissionDecision,
            None,
            None,
            [("gate_decision".to_string(), "needs_approval".into())].into(),
        );
        log.append(
            EventKind::PermissionDecision,
            None,
            None,
            [("gate_decision".to_string(), "deny".into())].into(),
        );
        log.append(EventKind::ApprovalRecorded, None, None, Default::default());

        let m = compute_harness_metrics(log.events());
        assert_eq!(m.recovery.replan_attempts, 1);
        assert_eq!(m.recovery.branch_decisions, 1);
        assert_eq!(m.recovery.alternatives_rejected, 1);
        assert_eq!(m.safety.permission_decisions, 2);
        assert_eq!(m.safety.escalations, 1);
        assert_eq!(m.safety.denials, 1);
        assert_eq!(m.safety.approvals_recorded, 1);
    }

    #[test]
    fn state_effects_captured_tracks_state_records() {
        // A success with no state record → no captured state effects (but
        // still fully logged/auditable — that's the point of the rename).
        let mut log = EventLog::new();
        log.append(
            EventKind::ActionSucceeded,
            Some("a1"),
            None,
            Default::default(),
        );
        assert!(
            !compute_harness_metrics(log.events())
                .replayability
                .state_effects_captured
        );

        // Add a StateChanged → state effects captured.
        log.append(
            EventKind::StateChanged,
            Some("a1"),
            None,
            Default::default(),
        );
        assert!(
            compute_harness_metrics(log.events())
                .replayability
                .state_effects_captured
        );
    }

    #[test]
    fn empty_trajectory_has_no_success_rate() {
        let m = compute_harness_metrics(&[]);
        assert_eq!(m.trajectory_efficiency.success_rate, None);
        assert_eq!(m.replayability.total_events, 0);
    }

    #[test]
    fn jsonl_counts_parse_errors() {
        let mut log = EventLog::new();
        log.append(
            EventKind::ActionSucceeded,
            Some("a1"),
            None,
            Default::default(),
        );
        let good = serde_json::to_string(&log.events()[0]).unwrap();
        let jsonl = format!("{good}\nnot json\n{{\"kind\":\"bogus\"}}\n");
        let m = compute_from_jsonl(&jsonl);
        assert_eq!(m.trajectory_efficiency.actions_succeeded, 1);
        // "not json" and the unknown-kind line both fail to parse.
        assert_eq!(m.parse_errors, 2);
    }
}