trusty-code 0.8.1

Per-project Claude-Code-compatible MPM orchestration harness (bin: tcode) — an original coding harness, tracked under epic #2052.
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
//! Tests for `engine.rs` and its sibling modules (`engine_state.rs`,
//! `session_events.rs`, `workstream_subscription.rs`) — issue #3415.
//!
//! Why: kept as a sibling `_tests.rs` file (not inline in `engine.rs`), per
//! this crate's established convention (`serve/uds.rs` -> `uds_tests.rs`,
//! `workstreams/sse.rs` -> `sse_tests.rs`) — a `_tests.rs` suffix is scored
//! against the 1500-SLOC TEST cap (issue #610/#1131), not the 500-SLOC
//! PRODUCTION cap, so splitting tests out of `engine.rs` is what keeps that
//! file itself under cap without shrinking coverage.

use tokio::sync::mpsc::unbounded_channel;

use super::*;
use crate::events::{Event, SessionEventEnvelope};
use crate::tui_client::session_events::{forward_session_event, terminal_stream_failure_event};
use crate::tui_client::workstream_subscription::parse_workstream_envelope;
use trusty_code_tui::{DelegationOutcome, StatuslineSegment, WorkstreamSummary};

fn envelope(event: Event) -> SessionEventEnvelope {
    SessionEventEnvelope::new("s-1".to_string(), 1, chrono::Utc::now(), event)
}

#[test]
fn forward_message_emits_assistant_output_chunk_not_done() {
    let (tx, mut rx) = unbounded_channel();
    let terminal = forward_session_event(
        envelope(Event::Message {
            session_id: "s-1".into(),
            text: "hi".into(),
        }),
        &tx,
    );
    assert!(!terminal);
    assert_eq!(
        rx.try_recv().expect("event"),
        ReplEvent::AssistantOutput {
            chunk: "hi".into(),
            done: false,
            is_error: false,
        }
    );
}

#[test]
fn forward_tool_started_emits_tool_invocation_with_call_id() {
    let (tx, mut rx) = unbounded_channel();
    let terminal = forward_session_event(
        envelope(Event::ToolStarted {
            session_id: "s-1".into(),
            agent: "pm".into(),
            agent_id: String::new(),
            tool: "bash".into(),
            call_id: "call-1".into(),
            args_preview: "ls".into(),
        }),
        &tx,
    );
    assert!(!terminal);
    match rx.try_recv().expect("event") {
        ReplEvent::ToolInvocation {
            id,
            tool_name,
            result,
            ..
        } => {
            assert_eq!(id, "call-1");
            assert_eq!(tool_name, "bash");
            assert!(result.is_none());
        }
        other => panic!("unexpected {other:?}"),
    }
}

#[test]
fn forward_tool_finished_carries_result_and_shares_call_id() {
    let (tx, mut rx) = unbounded_channel();
    forward_session_event(
        envelope(Event::ToolFinished {
            session_id: "s-1".into(),
            agent: "pm".into(),
            agent_id: String::new(),
            tool: "bash".into(),
            call_id: "call-1".into(),
            success: true,
            result_preview: "done".into(),
        }),
        &tx,
    );
    match rx.try_recv().expect("event") {
        ReplEvent::ToolInvocation { id, result, .. } => {
            assert_eq!(id, "call-1");
            assert_eq!(result.as_deref(), Some("done"));
        }
        other => panic!("unexpected {other:?}"),
    }
}

#[test]
fn forward_session_done_is_terminal() {
    let (tx, mut rx) = unbounded_channel();
    let terminal = forward_session_event(
        envelope(Event::SessionDone {
            session_id: "s-1".into(),
            status: "finished".into(),
        }),
        &tx,
    );
    assert!(terminal);
    assert_eq!(
        rx.try_recv().expect("event"),
        ReplEvent::AssistantOutput {
            chunk: String::new(),
            done: true,
            is_error: false,
        }
    );
}

#[test]
fn forward_session_done_failed_marks_is_error() {
    let (tx, mut rx) = unbounded_channel();
    forward_session_event(
        envelope(Event::SessionDone {
            session_id: "s-1".into(),
            status: "failed".into(),
        }),
        &tx,
    );
    assert_eq!(
        rx.try_recv().expect("event"),
        // #11788: an empty chunk rendered nothing; a failure carries text.
        ReplEvent::AssistantOutput {
            chunk: "error: the run ended with status `failed`".into(),
            done: true,
            is_error: true,
        }
    );
}

#[test]
fn forward_session_cancelled_is_terminal() {
    let (tx, mut rx) = unbounded_channel();
    let terminal = forward_session_event(
        envelope(Event::SessionCancelled {
            session_id: "s-1".into(),
        }),
        &tx,
    );
    assert!(terminal);
    assert!(rx.try_recv().is_ok());
}

#[test]
fn forward_unrelated_event_is_ignored() {
    let (tx, mut rx) = unbounded_channel();
    let terminal = forward_session_event(envelope(Event::Ping), &tx);
    assert!(!terminal);
    assert!(rx.try_recv().is_err());
}

/// tcode streaming epic #3696 Slice 2, re-keyed by #7940: a non-final
/// `AgentMessageDelta` (`done: false`) must forward as an `AgentOutput`
/// chunk carrying the producer's `(agent_id, turn_id)` pair, which is what
/// the reducer opens a per-stream bubble on.
#[test]
fn forward_agent_message_delta_not_done_appends() {
    let (tx, mut rx) = unbounded_channel();
    let terminal = forward_session_event(
        envelope(Event::AgentMessageDelta {
            session_id: "s-1".into(),
            agent: "coder".into(),
            agent_id: "agent-1".into(),
            turn_id: "turn-1".into(),
            delta: "Hello".into(),
            done: false,
        }),
        &tx,
    );
    assert!(!terminal);
    assert_eq!(
        rx.try_recv().expect("event"),
        ReplEvent::AgentOutput {
            agent_id: "agent-1".into(),
            turn_id: "turn-1".into(),
            chunk: "Hello".into(),
            done: false,
        }
    );
}

/// The final delta (`done: true`) for a turn must forward `done: true` (so
/// the reducer finalizes that stream's bubble) — and, unlike `SessionDone`,
/// is NOT terminal for the SSE pump itself: one agent's turn finishing
/// doesn't mean the whole session stream is over.
#[test]
fn forward_agent_message_delta_done_finalizes() {
    let (tx, mut rx) = unbounded_channel();
    let terminal = forward_session_event(
        envelope(Event::AgentMessageDelta {
            session_id: "s-1".into(),
            agent: "coder".into(),
            agent_id: "agent-1".into(),
            turn_id: "turn-1".into(),
            delta: " world".into(),
            done: true,
        }),
        &tx,
    );
    assert!(
        !terminal,
        "an agent turn finishing must not end the SSE pump"
    );
    assert_eq!(
        rx.try_recv().expect("event"),
        ReplEvent::AgentOutput {
            agent_id: "agent-1".into(),
            turn_id: "turn-1".into(),
            chunk: " world".into(),
            done: true,
        }
    );
}

/// Two agents streaming concurrently and sharing a `turn_id` (the Slice 0
/// contract's defensive `(agent_id, turn_id)` grouping scenario) must each
/// forward carrying their OWN `agent_id` — `forward_session_event` never
/// accumulates or merges chunks itself; it is a stateless 1:1 mapper, and
/// the id it preserves is what lets the reducer keep the two apart. The
/// reducer half of that guarantee is
/// `trusty_code_tui::app::reduce::tests::agent_output_keys_concurrent_streams_separately`.
#[test]
fn forward_agent_message_delta_distinct_agent_ids_not_merged() {
    let (tx, mut rx) = unbounded_channel();
    forward_session_event(
        envelope(Event::AgentMessageDelta {
            session_id: "s-1".into(),
            agent: "coder".into(),
            agent_id: "agent-1".into(),
            turn_id: "turn-shared".into(),
            delta: "from-agent-1".into(),
            done: false,
        }),
        &tx,
    );
    forward_session_event(
        envelope(Event::AgentMessageDelta {
            session_id: "s-1".into(),
            agent: "reviewer".into(),
            agent_id: "agent-2".into(),
            turn_id: "turn-shared".into(),
            delta: "from-agent-2".into(),
            done: false,
        }),
        &tx,
    );
    assert_eq!(
        rx.try_recv().expect("event"),
        ReplEvent::AgentOutput {
            agent_id: "agent-1".into(),
            turn_id: "turn-shared".into(),
            chunk: "from-agent-1".into(),
            done: false,
        },
        "agent-1's delta must keep its own agent_id, not merge with agent-2's"
    );
    assert_eq!(
        rx.try_recv().expect("event"),
        ReplEvent::AgentOutput {
            agent_id: "agent-2".into(),
            turn_id: "turn-shared".into(),
            chunk: "from-agent-2".into(),
            done: false,
        },
        "agent-2's delta must keep its own agent_id, not merge with agent-1's"
    );
}

// ── #7940: delegation brackets ────────────────────────────────────────────

/// The daemon's `AgentSpawned` must open a delegation block carrying the
/// spawn's own `agent_id` — the key every later tool/message event of that
/// sub-agent's run is attributed with.
#[test]
fn forward_agent_spawned_opens_a_delegation() {
    let (tx, mut rx) = unbounded_channel();
    let terminal = forward_session_event(
        envelope(Event::AgentSpawned {
            session_id: "s-1".into(),
            agent: "engineer".into(),
            agent_id: "spawn-7".into(),
            task_preview: "add the delegation renderer".into(),
        }),
        &tx,
    );
    assert!(!terminal);
    assert_eq!(
        rx.try_recv().expect("event"),
        ReplEvent::DelegationStarted {
            agent_id: "spawn-7".into(),
            agent: "engineer".into(),
            task: "add the delegation renderer".into(),
        }
    );
}

/// `PmDelegating` is the delegating agent's ANNOUNCEMENT and carries no
/// `agent_id` — it must still open a block (with an empty id) so the task
/// summary is visible before the sub-agent exists; the reducer adopts that
/// block when the real `AgentSpawned` lands.
#[test]
fn forward_pm_delegating_opens_a_delegation_without_an_id() {
    let (tx, mut rx) = unbounded_channel();
    forward_session_event(
        envelope(Event::PmDelegating {
            session_id: "s-1".into(),
            agent: "engineer".into(),
            task_preview: "add the delegation renderer".into(),
        }),
        &tx,
    );
    assert_eq!(
        rx.try_recv().expect("event"),
        ReplEvent::DelegationStarted {
            agent_id: String::new(),
            agent: "engineer".into(),
            task: "add the delegation renderer".into(),
        }
    );
}

/// `AgentDone` closes the block with a `Finished` outcome, and — like an
/// agent turn ending — is NOT terminal for the session stream.
#[test]
fn forward_agent_done_closes_the_delegation() {
    let (tx, mut rx) = unbounded_channel();
    let terminal = forward_session_event(
        envelope(Event::AgentDone {
            session_id: "s-1".into(),
            agent: "engineer".into(),
            agent_id: "spawn-7".into(),
            status: "success".into(),
        }),
        &tx,
    );
    assert!(
        !terminal,
        "a delegation finishing must not end the session stream"
    );
    assert_eq!(
        rx.try_recv().expect("event"),
        ReplEvent::DelegationFinished {
            agent_id: "spawn-7".into(),
            agent: "engineer".into(),
            outcome: DelegationOutcome::Finished("success".into()),
        }
    );
}

/// `AgentFailed` closes the block carrying the error text — the whole point
/// of a two-variant outcome is that a failed delegation cannot render the
/// same as a finished one.
#[test]
fn forward_agent_failed_closes_with_the_error() {
    let (tx, mut rx) = unbounded_channel();
    forward_session_event(
        envelope(Event::AgentFailed {
            session_id: "s-1".into(),
            agent: "engineer".into(),
            agent_id: "spawn-7".into(),
            error: "turn cap exceeded".into(),
        }),
        &tx,
    );
    assert_eq!(
        rx.try_recv().expect("event"),
        ReplEvent::DelegationFinished {
            agent_id: "spawn-7".into(),
            agent: "engineer".into(),
            outcome: DelegationOutcome::Failed("turn cap exceeded".into()),
        }
    );
}

/// #3422: `permission_requested` must reach the TUI field for field. Until
/// this mapping existed the event fell into `forward_session_event`'s
/// catch-all, so a tool call suspended on the daemon looked like the TUI had
/// simply stopped responding.
#[test]
fn forward_permission_requested_opens_the_prompt() {
    let (tx, mut rx) = unbounded_channel();
    forward_session_event(
        envelope(Event::PermissionRequested {
            session_id: "s-1".into(),
            request_id: "req-9".into(),
            agent: "python-engineer".into(),
            agent_id: "spawn-7".into(),
            tool: "bash".into(),
            subject: "rm -rf build".into(),
            rule: "bash[rm *]".into(),
        }),
        &tx,
    );
    assert_eq!(
        rx.try_recv().expect("event"),
        ReplEvent::PermissionRequested {
            request_id: "req-9".into(),
            agent: "python-engineer".into(),
            agent_id: "spawn-7".into(),
            tool: "bash".into(),
            subject: "rm -rf build".into(),
            rule: "bash[rm *]".into(),
        }
    );
}

/// #3422: `permission_resolved` is the daemon's own close for the prompt, so
/// its `decision`/`source` words cross the seam verbatim — the TUI renders
/// them and parses neither.
#[test]
fn forward_permission_resolved_closes_the_prompt() {
    let (tx, mut rx) = unbounded_channel();
    forward_session_event(
        envelope(Event::PermissionResolved {
            session_id: "s-1".into(),
            request_id: "req-9".into(),
            agent: "python-engineer".into(),
            agent_id: "spawn-7".into(),
            decision: "allow_for_session".into(),
            source: "client".into(),
        }),
        &tx,
    );
    assert_eq!(
        rx.try_recv().expect("event"),
        ReplEvent::PermissionResolved {
            request_id: "req-9".into(),
            agent: "python-engineer".into(),
            agent_id: "spawn-7".into(),
            decision: "allow_for_session".into(),
            source: "client".into(),
        }
    );
}

/// Classify every [`Event`] variant as agent-attributed or not (#7940).
///
/// Why: `forward_session_event`'s `_ => false` catch-all silently drops
/// anything it has no arm for, which is exactly how `PmDelegating`/
/// `AgentSpawned` went unrendered for as long as they did. This `match` has
/// NO catch-all, so a new `Event` variant fails to COMPILE here until
/// someone decides which side it falls on — that compile break, not the
/// runtime assertions below, is what stops the next agent-attributed
/// variant from being dropped unnoticed.
/// What: returns `Some(kind)` for a variant carrying an `agent` field,
/// `None` otherwise.
fn agent_attributed_kind(event: &Event) -> Option<&'static str> {
    match event {
        // Agent-attributed: every variant carrying an `agent` field.
        Event::ToolStarted { .. }
        | Event::ToolFinished { .. }
        | Event::ToolError { .. }
        | Event::SearchPerformed { .. }
        | Event::MemoryRecalled { .. }
        | Event::PmDelegating { .. }
        | Event::AgentSpawned { .. }
        | Event::AgentMessage { .. }
        | Event::AgentMessageDelta { .. }
        | Event::AgentDone { .. }
        | Event::AgentFailed { .. }
        // (#7948) Both carry `agent`/`agent_id` so a client can route a
        // permission prompt to the delegation that raised it.
        | Event::PermissionRequested { .. }
        | Event::PermissionResolved { .. }
        // (#8235) Carries `agent`/`agent_id` so a client can tell whose
        // checklist changed.
        | Event::TodosChanged { .. } => Some(event.kind()),
        // Not agent-attributed. `AgentStarted`/`ReportGenerated` carry an
        // `agent_name`, not an `agent`, and neither has a producer on this
        // daemon's session path.
        Event::AgentStarted { .. }
        | Event::SessionStarted { .. }
        | Event::SessionDone { .. }
        | Event::SessionCancelled { .. }
        | Event::SessionStatusChanged { .. }
        | Event::SessionInput { .. }
        | Event::Log { .. }
        | Event::Progress { .. }
        | Event::Message { .. }
        | Event::PmThinking { .. }
        | Event::ToolCalled { .. }
        | Event::ToolResult { .. }
        | Event::AstOperation { .. }
        | Event::PhaseStarted { .. }
        | Event::PhaseDone { .. }
        | Event::PhaseSkipped { .. }
        | Event::PersonaDetected { .. }
        | Event::LlmRequested { .. }
        | Event::LlmResponded { .. }
        | Event::ReportGenerated { .. }
        | Event::RecapGenerated { .. }
        | Event::IndexReadiness { .. }
        | Event::ContextBudget { .. }
        | Event::SessionAdded { .. }
        | Event::SessionActivityUpdate { .. }
        | Event::WorkstreamActivationChanged { .. }
        | Event::WorkstreamStateInferred { .. }
        | Event::Ping => None,
    }
}

/// Agent-attributed kinds this client deliberately does NOT render, with the
/// reason (#7940). An entry here is a decision, not an oversight.
const INTENTIONALLY_IGNORED: &[(&str, &str)] = &[
    (
        "search_performed",
        "search telemetry; the tool_started/tool_finished pair for the same call already renders",
    ),
    (
        "memory_recalled",
        "recall telemetry; same reason as search_performed",
    ),
    // #3422 removed `permission_requested`/`permission_resolved` from this
    // list: both now map, and
    // `forward_permission_requested_opens_the_prompt` /
    // `forward_permission_resolved_closes_the_prompt` pin the mapping.
    (
        "todos_changed",
        "the checklist panel is #8182's work; until it lands this client has no \
         ReplEvent to render a checklist into, and the roster read path \
         (session.get_agents) carries the same list",
    ),
];

/// Every agent-attributed `Event` the daemon can emit during a delegation
/// must map to at least one `ReplEvent`, or be named in
/// [`INTENTIONALLY_IGNORED`] (#7940).
///
/// Why: a delegation's whole value in the TUI is that nothing about it goes
/// missing. Before this, `PmDelegating` and `AgentSpawned` fell into the
/// catch-all and rendered as nothing at all, with no test that could tell.
#[test]
fn every_agent_attributed_event_maps_or_is_explicitly_ignored() {
    for event in delegation_event_samples() {
        let Some(kind) = agent_attributed_kind(&event) else {
            panic!("sample {} is not agent-attributed", event.kind());
        };
        let (tx, mut rx) = unbounded_channel();
        forward_session_event(envelope(event), &tx);
        let mapped = rx.try_recv().is_ok();
        let ignored = INTENTIONALLY_IGNORED.iter().any(|(k, _)| *k == kind);
        assert!(
            mapped != ignored,
            "`{kind}` must either map to a ReplEvent or be listed in INTENTIONALLY_IGNORED \
             (mapped={mapped}, ignored={ignored})"
        );
    }
}

/// One sample of every agent-attributed `Event` the daemon emits during a
/// delegation (#7940). `agent_attributed_kind`'s exhaustive `match` is what
/// forces a new variant to be classified; this list is what actually drives
/// it through `forward_session_event`.
fn delegation_event_samples() -> Vec<Event> {
    vec![
        Event::PmDelegating {
            session_id: "s-1".into(),
            agent: "engineer".into(),
            task_preview: "t".into(),
        },
        Event::AgentSpawned {
            session_id: "s-1".into(),
            agent: "engineer".into(),
            agent_id: "spawn-1".into(),
            task_preview: "t".into(),
        },
        Event::AgentMessage {
            session_id: "s-1".into(),
            agent: "engineer".into(),
            text: "hi".into(),
        },
        Event::AgentMessageDelta {
            session_id: "s-1".into(),
            agent: "engineer".into(),
            agent_id: "spawn-1".into(),
            turn_id: "turn-1".into(),
            delta: "hi".into(),
            done: false,
        },
        Event::ToolStarted {
            session_id: "s-1".into(),
            agent: "engineer".into(),
            agent_id: "spawn-1".into(),
            tool: "bash".into(),
            call_id: "c1".into(),
            args_preview: "ls".into(),
        },
        Event::ToolFinished {
            session_id: "s-1".into(),
            agent: "engineer".into(),
            agent_id: "spawn-1".into(),
            tool: "bash".into(),
            call_id: "c1".into(),
            success: true,
            result_preview: "ok".into(),
        },
        Event::ToolError {
            session_id: "s-1".into(),
            agent: "engineer".into(),
            agent_id: "spawn-1".into(),
            tool: "bash".into(),
            call_id: "c1".into(),
            error: "boom".into(),
        },
        Event::SearchPerformed {
            session_id: "s-1".into(),
            agent: "engineer".into(),
            agent_id: "spawn-1".into(),
            lane: "lexical".into(),
            query: "q".into(),
            hit_count: Some(1),
            hits: vec![],
            latency_ms: 1,
        },
        Event::MemoryRecalled {
            session_id: "s-1".into(),
            agent: "engineer".into(),
            agent_id: "spawn-1".into(),
            query: "q".into(),
            results: vec![],
        },
        Event::AgentDone {
            session_id: "s-1".into(),
            agent: "engineer".into(),
            agent_id: "spawn-1".into(),
            status: "success".into(),
        },
        Event::AgentFailed {
            session_id: "s-1".into(),
            agent: "engineer".into(),
            agent_id: "spawn-1".into(),
            error: "boom".into(),
        },
        Event::PermissionRequested {
            session_id: "s-1".into(),
            request_id: "req-1".into(),
            agent: "engineer".into(),
            agent_id: "spawn-1".into(),
            tool: "bash".into(),
            subject: "rm -rf build".into(),
            rule: "bash[rm *]".into(),
        },
        Event::PermissionResolved {
            session_id: "s-1".into(),
            request_id: "req-1".into(),
            agent: "engineer".into(),
            agent_id: "spawn-1".into(),
            decision: "deny".into(),
            source: "client".into(),
        },
        // #8235: the session checklist's change event.
        Event::TodosChanged {
            session_id: "s-1".into(),
            agent: "engineer".into(),
            agent_id: "spawn-1".into(),
            todos: vec![crate::events::TodoItem {
                content: "write the test".into(),
                status: crate::events::TodoStatus::InProgress,
            }],
        },
    ]
}

#[test]
fn workstream_subcommand_parses_list_and_activate() {
    assert_eq!(workstream_subcommand("/workstream list"), Some("list"));
    assert_eq!(
        workstream_subcommand("/ws activate abc-123"),
        Some("activate abc-123")
    );
    assert_eq!(workstream_subcommand("/workstream"), Some(""));
    assert_eq!(workstream_subcommand("/workstreamx foo"), None);
    assert_eq!(workstream_subcommand("hello"), None);
}

#[test]
fn parse_workstream_envelope_round_trips_activation_changed() {
    let json = serde_json::json!({
        "session_id": "",
        "event_type": "workstream_activation_changed",
        "payload": {
            "type": "workstream_activation_changed",
            "new_active_id": "ws-2",
            "prior_id": "ws-1",
        },
    })
    .to_string();
    let env = parse_workstream_envelope(&json).expect("parse");
    match env.payload {
        Event::WorkstreamActivationChanged {
            new_active_id,
            prior_id,
        } => {
            assert_eq!(new_active_id.as_deref(), Some("ws-2"));
            assert_eq!(prior_id.as_deref(), Some("ws-1"));
        }
        other => panic!("unexpected {other:?}"),
    }
}

/// `pump_session_events` giving up after exhausting reconnects must produce
/// a `done: true, is_error: true` `AssistantOutput` — the epic #3411
/// deferred-verification item: a `ConnectionLost` alone never clears
/// `ReplApp::busy`, so this is what actually rules out a silent stall (a
/// stuck spinner with no visible error) once the last reconnect attempt
/// fails.
#[test]
fn terminal_stream_failure_event_is_a_done_error_output() {
    let event = terminal_stream_failure_event("daemon unreachable".to_string());
    assert_eq!(
        event,
        ReplEvent::AssistantOutput {
            chunk: "connection lost: daemon unreachable".to_string(),
            done: true,
            is_error: true,
        }
    );
}

/// The caches must start empty before `setup()` populates them —
/// `TuiEngine::commands()`/`picker()` must degrade to "nothing yet," never
/// panic, when queried before `setup()` runs.
#[test]
fn caches_are_empty_before_setup() {
    let engine = CodeEngine::with_socket("/nonexistent/tcode.sock", None);
    assert!(engine.commands().is_empty());
    assert!(engine.picker("workstream").is_none());
}

/// `picker("workstream")` must carry the exact dispatch command
/// `workstream_subcommand` parses back — proves the picker-selection
/// round trip (`"{dispatch_command} {selected.id}"`) actually reaches
/// `handle_workstream_command`'s `activate <id>` branch, not some other
/// unparsed string.
#[test]
fn workstream_picker_dispatch_command_round_trips_through_workstream_subcommand() {
    let dispatch_command = "/workstream activate";
    let resubmitted = format!("{dispatch_command} ws-1");
    assert_eq!(workstream_subcommand(&resubmitted), Some("activate ws-1"));
}

/// An unknown picker name must be `None`, not an empty-but-`Some` request —
/// this engine has exactly one picker.
#[test]
fn picker_unknown_name_is_none() {
    let engine = CodeEngine::with_socket("/nonexistent/tcode.sock", None);
    assert!(engine.picker("model").is_none());
}

/// `EngineState::statusline_segments` must mirror the `active_workstream`
/// cache exactly: no segment before any workstream is known, one
/// `StatuslineSegment::Workstream` once it is, and — critically — an EMPTY
/// list again (not a stale segment left behind) once deactivated. This is
/// the seam `setup`/`run_workstream_subscription` push through
/// `ReplEvent::StatuslineUpdate` so the status line's "WS: name (id)"
/// segment (DOC-50 §5.3, Slice 6) tracks the daemon's actual active
/// workstream, clearing on deactivation rather than showing stale text.
#[test]
fn statusline_segments_reflect_active_workstream_and_clear_on_none() {
    let state = EngineState::new(UdsRpcClient::new("/nonexistent/tcode.sock"), None, false);
    assert!(
        state.statusline_segments().is_empty(),
        "no active workstream observed yet -> no segments"
    );

    *state.active_workstream.lock().unwrap() = Some(WorkstreamSummary {
        id: "ws-1".to_string(),
        name: "Feature X".to_string(),
    });
    assert_eq!(
        state.statusline_segments(),
        vec![StatuslineSegment::Workstream {
            id: "ws-1".to_string(),
            name: "Feature X".to_string(),
        }]
    );

    *state.active_workstream.lock().unwrap() = None;
    assert!(
        state.statusline_segments().is_empty(),
        "deactivation must clear the segment, not leave a stale one"
    );
}

// ── #8184: the connect line names the agent shape and the working root ──────

/// A bound session's summary names the solo agent and the project root.
#[test]
fn session_shape_summary_names_the_solo_agent_and_root() {
    let summary = session_shape_summary(&json!({
        "no_delegate": true,
        "binding": {"state": "directory", "root": "/tmp/proj"},
    }));
    assert_eq!(
        summary,
        "solo agent (no delegation), file tools rooted at /tmp/proj"
    );
}

/// A projectless session says so, and names the scratch workspace — the TUI's
/// launch directory is NOT where its edits land.
#[test]
fn session_shape_summary_names_the_projectless_scratch_root() {
    let summary = session_shape_summary(&json!({
        "no_delegate": true,
        "binding": {"state": "projectless", "root": null},
    }));
    assert_eq!(
        summary,
        "solo agent (no delegation), projectless — file tools rooted at a scratch workspace"
    );
}

/// `--delegate` reads as the PM, so the two modes are distinguishable on the
/// connect line.
#[test]
fn session_shape_summary_names_the_delegating_pm() {
    let summary = session_shape_summary(&json!({
        "no_delegate": false,
        "binding": {"state": "git_repo", "root": "/tmp/repo"},
    }));
    assert_eq!(summary, "delegating PM, file tools rooted at /tmp/repo");
}

// ── #8207: the cancel outcome the TUI can render ──────────────────────────────

/// `-32010` must reach this client as a NAMED outcome, not an opaque error.
///
/// Why: the daemon mints a domain code for "accepted, not stopped yet" precisely
/// so a client can hold a cancelling state instead of showing a fault — and
/// before this, every client in the repo flattened it. The daemon's own sentence
/// has to survive too: it names what the wait is still on, which is the whole
/// content of a "still cancelling…" line.
/// Test: this test.
#[test]
fn cancel_unconfirmed_is_a_still_cancelling_outcome() {
    let outcome = classify_cancel_error(EngineError::Rpc {
        code: -32010,
        message: "session s-1: cancellation requested but the task did not stop within 10s"
            .to_string(),
        data: None,
    })
    .expect("an unconfirmed cancel is an outcome, not a failure");

    assert_eq!(
        outcome,
        CancelOutcome::StillCancelling {
            detail: "session s-1: cancellation requested but the task did not stop within 10s"
                .to_string()
        }
    );
}

/// A confirmed stop — and the no-session case — must reach the TUI as
/// `CancelReply::Stopped`, the one arm that reopens input (#8207).
///
/// Why: `NoSession` is not a failure. `setup` never minted a session, so nothing
/// is running; reporting it as a failure would leave the pane refusing input
/// with nothing to wait for.
/// Test: this test.
#[test]
fn cancel_reply_reports_a_confirmed_stop() {
    assert_eq!(
        cancel_reply_from(Ok(CancelOutcome::Stopped)),
        CancelReply::Stopped
    );
    assert_eq!(
        cancel_reply_from(Ok(CancelOutcome::NoSession)),
        CancelReply::Stopped
    );
}

/// An unconfirmed cancel must stay its own arm all the way to the TUI, carrying
/// the daemon's sentence (#8207).
///
/// Why: this is the state the TUI renders as "still cancelling" — collapsing it
/// into `Stopped` reopens input while the run continues (the original bug), and
/// collapsing it into `Failed` reads as a cancel that never landed.
/// Test: this test.
#[test]
fn cancel_reply_keeps_an_unconfirmed_cancel_distinct() {
    let detail = "session s-1: cancellation requested but the task did not stop within 10s";
    assert_eq!(
        cancel_reply_from(Ok(CancelOutcome::StillCancelling {
            detail: detail.to_string()
        })),
        CancelReply::StillCancelling {
            detail: detail.to_string()
        }
    );
}

/// A refusal reaches the user as the daemon's sentence WITHOUT its JSON-RPC code
/// (#8207).
///
/// Why: `CancelReply`'s payload is rendered verbatim, and `EngineError`'s own
/// `Display` writes `daemon returned an error (-32003): …` — exactly the string
/// the issue was filed about. Naming `message` here is what keeps the code off
/// the screen; a transport failure has no code to strip and passes through.
/// Test: this test.
#[test]
fn cancel_reply_strips_the_rpc_code_from_a_refusal() {
    let reply = cancel_reply_from(Err(EngineError::Rpc {
        code: -32003,
        message: "session s-1 already has a task running".to_string(),
        data: None,
    }));
    assert_eq!(
        reply,
        CancelReply::Failed {
            error: "session s-1 already has a task running".to_string()
        }
    );
    let CancelReply::Failed { error } = &reply else {
        panic!("a refusal is a failure: {reply:?}");
    };
    assert!(
        !error.contains("-32003"),
        "the wire code must not reach the user: {error}"
    );

    assert_eq!(
        cancel_reply_from(Err(EngineError::NoSession)),
        CancelReply::Failed {
            error: EngineError::NoSession.to_string()
        }
    );
}

/// Every other refusal stays an error (#8207).
///
/// Why: the split has to be exactly one code wide. Reading a `-32603` daemon
/// fault or a `-32007 session_not_found` as "still cancelling" would hide a real
/// failure behind a spinner that never resolves.
/// Test: this test.
#[test]
fn any_other_refusal_stays_an_error() {
    for code in [-32603, -32007, -32003] {
        let err = classify_cancel_error(EngineError::Rpc {
            code,
            message: "nope".to_string(),
            data: None,
        })
        .expect_err("only -32010 is an outcome");
        assert!(
            matches!(err, EngineError::Rpc { code: got, .. } if got == code),
            "code {code} must pass through verbatim, got {err:?}"
        );
    }
}