fno-agents 0.1.0

PTY supervisor substrate for persistent, attachable multi-CLI coding agents (codex, gemini, claude)
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
689
690
691
692
693
694
695
696
697
698
699
/// Integration tests for `fno-agents` loop_runtime module.
///
/// Each test drives `run_loop` directly via in-process mock impls of the
/// Queue / Dispatcher / Session traits. All file I/O uses temporary
/// directories; no network or real sessions are involved.
///
/// Test naming mirrors the acceptance criteria in the task spec:
///   1. empty_queue_terminates_nowork_without_dispatch
///   2. unit_closes_on_termination_event
///   3. no_event_exit_emits_node_failed_then_redispatches
///   4. iteration_ceiling_terminates_budget
///   5. preexisting_termination_skips_dispatch
///   6. cancel_returns_interrupted
///   7. zero_max_iterations_rejected
///   8. journal_write_failure_is_fatal
///   9. envelope_shape
use fno_agents::loop_runtime::{
    run_loop, CloseOutcome, DispatchCtx, Dispatcher, Evidence, Journal, LoopBudget, LoopError,
    Queue, Session, Unit,
};
// Note: tests use Journal::new_raw (cfg(test) convenience constructor) to avoid
// importing the newtype wrappers in every test.
use fno_agents::loopcheck::TerminationReason;
use std::fs;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Mutex};
use tempfile::TempDir;

// ── helpers ───────────────────────────────────────────────────────────────────

/// Build a minimal Unit for tests.
fn make_unit(id: &str, session_key: &str) -> Unit {
    Unit {
        id: id.to_string(),
        title: format!("Test unit {id}"),
        session_key: session_key.to_string(),
        plan_path: None,
        extra_env: vec![],
    }
}

/// Build an Evidence value for a given TerminationReason.
fn make_evidence(reason: TerminationReason) -> Evidence {
    Evidence {
        reason,
        message: "test done".to_string(),
    }
}

/// Write a valid termination event line into `journal_path` for `session_key`.
fn seed_termination_event(journal_path: &Path, session_key: &str, reason: &str) {
    let line = format!(
        "{{\"ts\":\"2026-06-06T00:00:00Z\",\"type\":\"termination\",\"source\":\"hook\",\
         \"data\":{{\"session_id\":\"{session_key}\",\"reason\":\"{reason}\",\"message\":\"pre-seeded\"}}}}\n"
    );
    let mut f = fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(journal_path)
        .expect("seed termination event");
    f.write_all(line.as_bytes()).expect("write seed");
}

/// Read all lines from a JSONL file and parse them as JSON values.
fn read_jsonl(path: &Path) -> Vec<serde_json::Value> {
    if !path.exists() {
        return vec![];
    }
    let content = fs::read_to_string(path).expect("read jsonl");
    content
        .lines()
        .filter(|l| !l.trim().is_empty())
        .filter_map(|l| serde_json::from_str(l).ok())
        .collect()
}

/// Count events of a given type in a JSONL file.
fn count_events(path: &Path, event_type: &str) -> usize {
    read_jsonl(path)
        .into_iter()
        .filter(|v| v["type"].as_str() == Some(event_type))
        .count()
}

// ── mock impls ────────────────────────────────────────────────────────────────

/// A Queue that serves a fixed list of units in order, then returns None.
struct FixedQueue {
    units: Mutex<Vec<Unit>>,
    /// Records each unit.id passed to close().
    closed: Mutex<Vec<String>>,
    close_outcome: CloseOutcome,
}

impl FixedQueue {
    fn new(units: Vec<Unit>) -> Self {
        Self {
            units: Mutex::new(units),
            closed: Mutex::new(vec![]),
            close_outcome: CloseOutcome::Closed,
        }
    }

    fn with_close_outcome(units: Vec<Unit>, outcome: CloseOutcome) -> Self {
        Self {
            units: Mutex::new(units),
            closed: Mutex::new(vec![]),
            close_outcome: outcome,
        }
    }

    fn close_count(&self) -> usize {
        self.closed.lock().unwrap().len()
    }
}

impl Queue for FixedQueue {
    fn next(&mut self) -> Result<Option<Unit>, LoopError> {
        let mut units = self.units.lock().unwrap();
        if units.is_empty() {
            Ok(None)
        } else {
            Ok(Some(units.remove(0)))
        }
    }

    fn close(&mut self, unit: &Unit, _evidence: &Evidence) -> Result<CloseOutcome, LoopError> {
        self.closed.lock().unwrap().push(unit.id.clone());
        // Return a clone of the stored outcome.
        Ok(match &self.close_outcome {
            CloseOutcome::Closed => CloseOutcome::Closed,
            CloseOutcome::Refused(s) => CloseOutcome::Refused(s.clone()),
            CloseOutcome::Parked(s) => CloseOutcome::Parked(s.clone()),
        })
    }
}

/// A Dispatcher that panics if called (for "no dispatch expected" tests).
struct PanicDispatcher;

impl Dispatcher for PanicDispatcher {
    fn run(&self, _unit: &Unit, _ctx: &DispatchCtx) -> Result<Box<dyn Session>, LoopError> {
        panic!("PanicDispatcher: dispatch must not be called in this test");
    }
}

/// A Session that writes an optional termination event to a journal file, then
/// returns a fixed exit code. Used by MockDispatcher below.
struct MockSession {
    journal_path: PathBuf,
    session_key: String,
    termination_reason: Option<String>, // None -> write nothing
    exit_code: i32,
}

impl Session for MockSession {
    fn wait(&mut self) -> Result<i32, LoopError> {
        if let Some(reason) = &self.termination_reason {
            seed_termination_event(&self.journal_path, &self.session_key, reason);
        }
        Ok(self.exit_code)
    }
}

/// A Dispatcher whose sessions are configured by a list of responses (in order).
/// Each response specifies whether the session writes a termination event and
/// what exit code to return.
struct MockDispatcher {
    journal_path: PathBuf,
    /// Per-dispatch config: (termination_reason, exit_code). Consumed in order;
    /// last element is reused if the list is exhausted.
    responses: Mutex<Vec<(Option<String>, i32)>>,
    dispatch_count: AtomicU64,
}

impl MockDispatcher {
    fn new(journal_path: PathBuf, responses: Vec<(Option<String>, i32)>) -> Self {
        Self {
            journal_path,
            responses: Mutex::new(responses),
            dispatch_count: AtomicU64::new(0),
        }
    }

    fn count(&self) -> u64 {
        self.dispatch_count.load(Ordering::SeqCst)
    }
}

impl Dispatcher for MockDispatcher {
    fn run(&self, unit: &Unit, _ctx: &DispatchCtx) -> Result<Box<dyn Session>, LoopError> {
        self.dispatch_count.fetch_add(1, Ordering::SeqCst);
        let mut responses = self.responses.lock().unwrap();
        let (reason, exit_code) = if responses.len() > 1 {
            responses.remove(0)
        } else {
            // Reuse last response indefinitely.
            responses[0].clone()
        };
        Ok(Box::new(MockSession {
            journal_path: self.journal_path.clone(),
            session_key: unit.session_key.clone(),
            termination_reason: reason,
            exit_code,
        }))
    }
}

// ── test 1: empty queue terminates with NoWork, dispatcher not called ─────────

#[test]
fn empty_queue_terminates_nowork_without_dispatch() {
    let dir = TempDir::new().unwrap();
    let project_events = dir.path().join("events.jsonl");
    let global_events = dir.path().join("global-events.jsonl");

    let mut queue = FixedQueue::new(vec![]);
    let dispatcher = PanicDispatcher;
    let budget = LoopBudget::new(10).unwrap();
    let journal = Journal::new_raw(project_events.clone(), global_events);

    let outcome = run_loop(&mut queue, &dispatcher, &budget, &journal, &|| false, None).unwrap();

    assert_eq!(outcome.reason, TerminationReason::NoWork);
    assert_eq!(outcome.iterations_used, 0);
    assert!(outcome.units.is_empty());

    // loop_terminated event must be present in the project journal.
    let events = read_jsonl(&project_events);
    let terminated: Vec<_> = events
        .iter()
        .filter(|v| v["type"].as_str() == Some("loop_terminated"))
        .collect();
    assert_eq!(
        terminated.len(),
        1,
        "expected exactly one loop_terminated event"
    );
    assert_eq!(
        terminated[0]["data"]["reason"].as_str(),
        Some("NoWork"),
        "loop_terminated reason must be NoWork"
    );
}

// ── test 2: unit closes on termination event ──────────────────────────────────

#[test]
fn unit_closes_on_termination_event() {
    let dir = TempDir::new().unwrap();
    let project_events = dir.path().join("events.jsonl");
    let global_events = dir.path().join("global-events.jsonl");

    let unit = make_unit("ab-001", "sess-001");
    let mut queue = FixedQueue::new(vec![unit]);
    // Session writes DonePRGreen termination event, exits 0.
    let dispatcher = MockDispatcher::new(
        project_events.clone(),
        vec![(Some("DonePRGreen".to_string()), 0)],
    );
    let budget = LoopBudget::new(10).unwrap();
    let journal = Journal::new_raw(project_events.clone(), global_events);

    let outcome = run_loop(&mut queue, &dispatcher, &budget, &journal, &|| false, None).unwrap();

    // Walk reason: queue empty after ab-001 -> NoWork (walk level).
    assert_eq!(outcome.reason, TerminationReason::NoWork);
    assert_eq!(outcome.iterations_used, 1);
    assert_eq!(outcome.units.len(), 1);
    assert_eq!(
        outcome.units[0].evidence.reason,
        TerminationReason::DonePRGreen
    );
    assert_eq!(outcome.units[0].close, CloseOutcome::Closed);

    // close() was called exactly once.
    assert_eq!(queue.close_count(), 1);

    // loop_unit_dispatched event must be present.
    assert_eq!(
        count_events(&project_events, "loop_unit_dispatched"),
        1,
        "expected one loop_unit_dispatched"
    );
    // No node_failed events (session produced a termination event).
    assert_eq!(
        count_events(&project_events, "node_failed"),
        0,
        "expected no node_failed"
    );
}

// ── test 3: no event on exit -> node_failed then re-dispatch ─────────────────

#[test]
fn no_event_exit_emits_node_failed_then_redispatches() {
    let dir = TempDir::new().unwrap();
    let project_events = dir.path().join("events.jsonl");
    let global_events = dir.path().join("global-events.jsonl");

    let unit = make_unit("ab-002", "sess-002");
    let mut queue = FixedQueue::new(vec![unit]);
    // First session: writes nothing, exits 1.
    // Second session: writes DonePRGreen, exits 0.
    let dispatcher = MockDispatcher::new(
        project_events.clone(),
        vec![(None, 1), (Some("DonePRGreen".to_string()), 0)],
    );
    let budget = LoopBudget::new(10).unwrap();
    let journal = Journal::new_raw(project_events.clone(), global_events);

    let outcome = run_loop(&mut queue, &dispatcher, &budget, &journal, &|| false, None).unwrap();

    assert_eq!(outcome.reason, TerminationReason::NoWork);
    assert_eq!(outcome.iterations_used, 2);
    assert_eq!(
        outcome.units[0].evidence.reason,
        TerminationReason::DonePRGreen
    );

    // Exactly 2 dispatches.
    assert_eq!(dispatcher.count(), 2, "expected exactly 2 dispatches");

    // Exactly 1 node_failed (from first session).
    assert_eq!(
        count_events(&project_events, "node_failed"),
        1,
        "expected exactly one node_failed"
    );

    // 2 loop_unit_dispatched events.
    assert_eq!(
        count_events(&project_events, "loop_unit_dispatched"),
        2,
        "expected two loop_unit_dispatched events"
    );
}

// ── test 4: iteration ceiling terminates budget ────────────────────────────────

#[test]
fn iteration_ceiling_terminates_budget() {
    let dir = TempDir::new().unwrap();
    let project_events = dir.path().join("events.jsonl");
    let global_events = dir.path().join("global-events.jsonl");

    let unit = make_unit("ab-003", "sess-003");
    let mut queue = FixedQueue::new(vec![unit]);
    // Session always writes nothing, exits 1.
    let dispatcher = MockDispatcher::new(project_events.clone(), vec![(None, 1)]);
    let budget = LoopBudget::new(3).unwrap();
    let journal = Journal::new_raw(project_events.clone(), global_events);

    let outcome = run_loop(&mut queue, &dispatcher, &budget, &journal, &|| false, None).unwrap();

    assert_eq!(outcome.reason, TerminationReason::Budget);
    assert_eq!(
        outcome.iterations_used, 3,
        "should use exactly budget iterations"
    );
    assert_eq!(dispatcher.count(), 3, "expected exactly 3 dispatches");

    // 3 node_failed events.
    assert_eq!(
        count_events(&project_events, "node_failed"),
        3,
        "expected 3 node_failed"
    );

    // loop_terminated with axis=iterations.
    let events = read_jsonl(&project_events);
    let terminated: Vec<_> = events
        .iter()
        .filter(|v| v["type"].as_str() == Some("loop_terminated"))
        .collect();
    assert_eq!(terminated.len(), 1);
    assert_eq!(terminated[0]["data"]["reason"].as_str(), Some("Budget"));
    assert_eq!(
        terminated[0]["data"]["axis"].as_str(),
        Some("iterations"),
        "Budget termination must carry axis=iterations"
    );
}

// ── test 5: pre-existing termination skips dispatch (AC1-FR core) ─────────────

#[test]
fn preexisting_termination_skips_dispatch() {
    let dir = TempDir::new().unwrap();
    let project_events = dir.path().join("events.jsonl");
    let global_events = dir.path().join("global-events.jsonl");

    // Seed a termination event BEFORE run_loop is called.
    seed_termination_event(&project_events, "sess-004", "DoneAdvisory");

    let unit = make_unit("ab-004", "sess-004");
    let mut queue = FixedQueue::new(vec![unit]);
    // Dispatcher panics if called - proves no dispatch happens.
    let dispatcher = PanicDispatcher;
    let budget = LoopBudget::new(5).unwrap();
    let journal = Journal::new_raw(project_events.clone(), global_events);

    let outcome = run_loop(&mut queue, &dispatcher, &budget, &journal, &|| false, None).unwrap();

    // Walk closes ab-004 without dispatching.
    assert_eq!(
        outcome.iterations_used, 0,
        "resume guard: no dispatch iterations"
    );
    assert_eq!(outcome.units.len(), 1);
    assert_eq!(
        outcome.units[0].evidence.reason,
        TerminationReason::DoneAdvisory
    );
    assert_eq!(outcome.units[0].close, CloseOutcome::Closed);

    // close() was called exactly once.
    assert_eq!(queue.close_count(), 1);

    // No loop_unit_dispatched events.
    assert_eq!(
        count_events(&project_events, "loop_unit_dispatched"),
        0,
        "resume guard: no dispatch events"
    );
}

// ── test 6: cancel returns Interrupted ────────────────────────────────────────

#[test]
fn cancel_returns_interrupted() {
    let dir = TempDir::new().unwrap();
    let project_events = dir.path().join("events.jsonl");
    let global_events = dir.path().join("global-events.jsonl");

    // cancel fires immediately (before any dispatch).
    let mut queue = FixedQueue::new(vec![make_unit("ab-005", "sess-005")]);
    let dispatcher = PanicDispatcher;
    let budget = LoopBudget::new(10).unwrap();
    let journal = Journal::new_raw(project_events.clone(), global_events);

    let cancel_flag = Arc::new(AtomicBool::new(true)); // pre-set to true
    let flag = cancel_flag.clone();
    let outcome = run_loop(
        &mut queue,
        &dispatcher,
        &budget,
        &journal,
        &move || flag.load(Ordering::SeqCst),
        None,
    )
    .unwrap();

    assert_eq!(outcome.reason, TerminationReason::Interrupted);

    let events = read_jsonl(&project_events);
    let terminated: Vec<_> = events
        .iter()
        .filter(|v| v["type"].as_str() == Some("loop_terminated"))
        .collect();
    assert_eq!(terminated.len(), 1);
    assert_eq!(
        terminated[0]["data"]["reason"].as_str(),
        Some("Interrupted")
    );
}

// ── test 7: zero max_iterations rejected ──────────────────────────────────────

#[test]
fn zero_max_iterations_rejected() {
    let result = LoopBudget::new(0);
    assert!(result.is_err(), "LoopBudget::new(0) must return Err");
}

// ── test 8: journal write failure is fatal ────────────────────────────────────

#[test]
fn journal_write_failure_is_fatal() {
    let dir = TempDir::new().unwrap();
    // Create a REGULAR FILE at the path where the journal directory would need
    // to be created, so open(create=true) fails with EISDIR or ENOTDIR.
    // Specifically: place a file as the "parent" component of the events path
    // so that create_dir_all or open fails.
    let blocker = dir.path().join("blocker");
    fs::write(&blocker, b"not a dir").unwrap();
    // Make the project events path be inside the regular file (impossible path).
    let project_events = blocker.join("events.jsonl");
    let global_events = dir.path().join("global-events.jsonl");

    let mut queue = FixedQueue::new(vec![make_unit("ab-006", "sess-006")]);
    let dispatcher = PanicDispatcher; // should not be reached
    let budget = LoopBudget::new(5).unwrap();
    let journal = Journal::new_raw(project_events, global_events);

    let result = run_loop(&mut queue, &dispatcher, &budget, &journal, &|| false, None);
    assert!(
        result.is_err(),
        "journal write failure to project path must be fatal (Err)"
    );
}

// ── test 9: envelope shape ────────────────────────────────────────────────────

#[test]
fn envelope_shape() {
    let dir = TempDir::new().unwrap();
    let project_events = dir.path().join("events.jsonl");
    let global_events = dir.path().join("global-events.jsonl");

    // Seed a garbage line first - must be silently skipped by find_termination.
    {
        let mut f = fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(&project_events)
            .unwrap();
        f.write_all(b"not json at all\n").unwrap();
        // A valid JSON line that is NOT a termination event.
        f.write_all(b"{\"type\":\"other\",\"data\":{}}\n").unwrap();
    }

    let unit = make_unit("ab-007", "sess-007");
    let mut queue = FixedQueue::new(vec![unit]);
    let dispatcher = MockDispatcher::new(
        project_events.clone(),
        vec![(Some("DonePRGreen".to_string()), 0)],
    );
    let budget = LoopBudget::new(5).unwrap();
    let journal = Journal::new_raw(project_events.clone(), global_events.clone());

    run_loop(&mut queue, &dispatcher, &budget, &journal, &|| false, None).unwrap();

    // Read all lines from project events (skip the pre-seeded garbage).
    let content = fs::read_to_string(&project_events).unwrap();
    let runtime_lines: Vec<serde_json::Value> = content
        .lines()
        .filter(|l| !l.trim().is_empty())
        // Skip lines that aren't valid JSON or are our pre-seeded non-runtime lines.
        .filter_map(|l| serde_json::from_str::<serde_json::Value>(l).ok())
        // Filter to lines written by the runtime (source == "loop").
        .filter(|v| v["source"].as_str() == Some("loop"))
        .collect();

    assert!(
        !runtime_lines.is_empty(),
        "runtime must have written at least one loop-source event"
    );

    for line in &runtime_lines {
        // ts: present, string, parseable as RFC3339 with Z suffix.
        let ts = line["ts"].as_str().expect("ts must be a string");
        assert!(ts.ends_with('Z'), "ts must end with Z: {ts}");
        ts.parse::<chrono::DateTime<chrono::Utc>>()
            .expect("ts must be valid RFC3339");

        // type: present and string.
        assert!(line["type"].is_string(), "type must be a string: {line}");

        // source: "loop".
        assert_eq!(
            line["source"].as_str(),
            Some("loop"),
            "source must be 'loop': {line}"
        );

        // data: present and an object.
        assert!(line["data"].is_object(), "data must be an object: {line}");
    }

    // Verify global mirror also has loop-source events.
    let global_content = fs::read_to_string(&global_events).unwrap_or_default();
    let global_runtime_lines: Vec<serde_json::Value> = global_content
        .lines()
        .filter(|l| !l.trim().is_empty())
        .filter_map(|l| serde_json::from_str(l).ok())
        .filter(|v: &serde_json::Value| v["source"].as_str() == Some("loop"))
        .collect();
    assert!(
        !global_runtime_lines.is_empty(),
        "global mirror must also have loop-source events"
    );
}

// ── test 10: F1 - termination event with missing reason field skips (no panic) ─

#[test]
fn termination_event_missing_reason_skips_no_panic() {
    // A termination event matching type+session_id but with no reason field is
    // authoritative-record corruption. The runtime must warn on stderr and skip
    // (no panic, no match) - behavioral no-change from the caller's perspective.
    let dir = TempDir::new().unwrap();
    let project_events = dir.path().join("events.jsonl");
    let global_events = dir.path().join("global-events.jsonl");

    // Write a termination event with no reason field for sess-f1.
    {
        let mut f = fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(&project_events)
            .unwrap();
        // Missing "reason" key entirely.
        f.write_all(
            b"{\"ts\":\"2026-06-06T00:00:00Z\",\"type\":\"termination\",\"source\":\"hook\",\
              \"data\":{\"session_id\":\"sess-f1\",\"message\":\"corrupt\"}}\n",
        )
        .unwrap();
    }

    // Because the corrupt event has no reason, find_termination returns None
    // and the loop dispatches normally (node_failed path), then hits Budget.
    let unit = make_unit("ab-f1", "sess-f1");
    let mut queue = FixedQueue::new(vec![unit]);
    // Dispatcher always returns nothing -> no termination event gets written.
    let dispatcher = MockDispatcher::new(project_events.clone(), vec![(None, 1)]);
    let budget = LoopBudget::new(1).unwrap();
    let journal = Journal::new_raw(project_events.clone(), global_events);

    // Must not panic; budget terminates the walk.
    let outcome = run_loop(&mut queue, &dispatcher, &budget, &journal, &|| false, None).unwrap();
    assert_eq!(
        outcome.reason,
        fno_agents::loopcheck::TerminationReason::Budget,
        "corrupt termination event must not match; walk terminates with Budget"
    );
}

// ── test 11: F7 - parse_termination_reason via serde round-trips correctly ────

#[test]
fn parse_termination_reason_serde_roundtrip() {
    // Verify all known variants round-trip through the journal path correctly.
    // Unknown strings must produce no match (walk continues, not panic).
    let dir = TempDir::new().unwrap();
    let project_events = dir.path().join("events.jsonl");
    let global_events = dir.path().join("global-events.jsonl");

    for (reason_str, expected) in &[
        (
            "DonePRGreen",
            fno_agents::loopcheck::TerminationReason::DonePRGreen,
        ),
        (
            "DoneAdvisory",
            fno_agents::loopcheck::TerminationReason::DoneAdvisory,
        ),
        ("NoWork", fno_agents::loopcheck::TerminationReason::NoWork),
        ("Budget", fno_agents::loopcheck::TerminationReason::Budget),
        (
            "NoProgress",
            fno_agents::loopcheck::TerminationReason::NoProgress,
        ),
        (
            "Interrupted",
            fno_agents::loopcheck::TerminationReason::Interrupted,
        ),
        ("Aborted", fno_agents::loopcheck::TerminationReason::Aborted),
    ] {
        // Clear and re-seed for each variant.
        let _ = fs::remove_file(&project_events);
        seed_termination_event(&project_events, "sess-serde", reason_str);

        let journal = Journal::new_raw(project_events.clone(), global_events.clone());
        let evidence = journal
            .find_termination("sess-serde")
            .expect("find_termination must not error")
            .expect(&format!("must find termination for reason '{reason_str}'"));
        assert_eq!(
            &evidence.reason, expected,
            "serde parse must round-trip '{reason_str}'"
        );
    }

    // Unknown reason string must produce None (no match, no panic).
    let _ = fs::remove_file(&project_events);
    {
        let mut f = fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(&project_events)
            .unwrap();
        f.write_all(
            b"{\"ts\":\"2026-06-06T00:00:00Z\",\"type\":\"termination\",\"source\":\"hook\",\
              \"data\":{\"session_id\":\"sess-serde\",\"reason\":\"UnknownFuture\",\"message\":\"\"}}\n",
        )
        .unwrap();
    }
    let journal = Journal::new_raw(project_events.clone(), global_events.clone());
    let result = journal
        .find_termination("sess-serde")
        .expect("find_termination must not error");
    assert!(
        result.is_none(),
        "unknown reason string must not match (returns None)"
    );
}