trusty-code-tui 0.2.0

Engine-agnostic terminal-UI seam (TuiEngine trait + ReplEvent) shared by trusty-code and trusty-agents interactive REPLs
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
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
//! The engine-agnostic render/reducer model: [`ReplApp`].
//!
//! Why: DOC-50 §3.2/§5 Slice 4 generalizes tagent's `ReplApp`
//! (`crates/trusty-agents/src/repl/tui/types.rs`/`app.rs`) into the shape
//! both `AgentEngine` (tagent) and `CodeEngine` (tcode) can render against
//! without either product's semantics leaking into the shared crate. The
//! original interleaves ~15 tagent-specific fields (OpenRouter cost
//! tracking, TM/claude-mpm session counts, model/provider pickers, agent
//! scope) with the genuinely generic chat/input/history/scroll state DOC-50
//! §3.2 calls out for extraction — this module keeps only the latter.
//! Product-specific data (statusline content, picker items, the
//! slash-command table) flows in through the Slice-1/1.5 seam
//! ([`crate::event::ReplEvent::StatuslineUpdate`], [`crate::model`]) instead
//! of being hardcoded here, per the generalization mandate.
//!
//! What: [`ReplApp`] is the `M` the shared [`crate::run::event_loop`] drives
//! (it implements [`crate::run::TuiModel`]). [`reduce::apply`] is the
//! `apply: FnMut(&mut M, ReplEvent)` reducer half of that contract; this
//! module owns the state and its primitive mutators (`insert_char`,
//! `backspace`, `push_user`, `scroll`, …). Most are direct ports; a few
//! (`Up`/`Down`/Ctrl-E key handling) are ports of tagent's REAL production
//! behavior rather than its dead-code helpers — see the disclosure list
//! below, which spells out every point (not just those) where this module's
//! behavior differs from tagent's, since Slice 10's cutover plans against
//! exactly this list. A product wiring `run()` (Slice 5+, tagent's eventual
//! `AgentEngine`/tcode's `CodeEngine`) supplies its own product-specific
//! fields (splash art, banner title, command table) via the public setters
//! below rather than this crate special-casing either product.
//!
//! ## Disclosed behavioral differences from tagent (DOC-50 §5 Slice 4)
//!
//! Ported **faithfully** (same production behavior, verified against
//! tagent's actual `keys.rs`/`app.rs`, not just its doc comments — one round
//! of review on this slice caught a doc/reality drift in tagent itself, see
//! history below):
//! - Up-arrow, when `Self::busy`, sets `Self::pending_cancel` — mirrors
//!   `crates/trusty-agents/src/repl/tui/keys.rs`'s real `KeyCode::Up` arm.
//!   What Up RECALLS diverges (#8181, see the divergence list below).
//! - Ctrl-E, with an empty input buffer and a non-`None`
//!   `Self::last_bash_block`, pastes only that block's first non-blank
//!   line (not the whole block) — matches
//!   `crates/trusty-agents/src/repl/tui/keys.rs`'s `KeyCode::Char('e')` arm
//!   exactly (the REPL input is single-line; pasting a multi-line block
//!   would silently truncate at the first `\n` on submit).
//! - The input composer's right-aligned `[thinking...]` label renders
//!   whenever `Self::busy` is true, REGARDLESS of whether the input
//!   buffer is empty — mirrors
//!   `crates/trusty-agents/src/repl/tui/chat.rs::draw_input`. `busy` is set
//!   `true` at submit time (not on the first streamed chunk), so the
//!   pre-first-token latency window after Enter also shows the indicator.
//!
//! Deliberately **NOT** ported (Phase 2 / later-slice / no-shared-crate-
//! formula scope — not oversights):
//! - The three-row animated activity panel (spinner cycling, elapsed timer,
//!   rust-rainbow shimmer, latest thinking-step echo —
//!   `repl/tui/layout.rs::draw_activity`, `repl/tui/status.rs`'s
//!   `SPINNER_FRAMES`/`rainbow_spans`/`hsl_to_rgb`). `busy: bool` is this
//!   crate's entire generalization of tagent's two-field
//!   `thinking: bool` + `busy_since: Option<Instant>` — there is no elapsed-
//!   time derivation available here (no stored start `Instant`).
//! - Inline token counters in the input row and ALL cost/usage tracking
//!   (`tokens_in`/`tokens_out`, `daily_cost_start`, usage-file persistence,
//!   the OpenRouter pricing formula) — DOC-50 Q9: no cost formula belongs in
//!   the shared crate; cost is engine-supplied, pre-formatted, via
//!   [`crate::model::StatuslineSegment::Cost`].
//! - Model/provider pickers (`PickerState`/`PickerKind`) and the inline
//!   LLM-offered/slash-completion choice list (`choices`/`choices_context`/
//!   `update_slash_completions`) — Phase 2 (DOC-50 §5 Slice 7 and later);
//!   [`crate::model::PickerItem`]/[`crate::model::PickerRequest`] exist
//!   (Slice 1.5) but no widget consumes them yet.
//! - `AgentScope`'s User/Project cyan/yellow semantic — replaced by a plain
//!   `Self::accent_color` field with no "scope" concept; the engine
//!   decides what it means and when to change it.
//! - The shared `trusty_common::banner` splash art, tagent's hardcoded
//!   banner title/identity text, and its fixed `/help`/`/connect`/`/clear`/
//!   `/status` command list — all now engine-supplied fields
//!   (`Self::banner_art`, `Self::banner_title`, [`Self::commands`](crate::commands),
//!   `Self::recent_activity`) rather than constants (this crate must not
//!   depend on `trusty_common`, see DOC-50 §2.2's dependency direction).
//! - The literal `"[trusty-agents] "` status prefix and its exact idle-hint
//!   copy (`"Ask ctrl anything, or /connect <path> for project work"`) —
//!   generalized to `Self::status_prefix` (defaulted from `label`) and a
//!   generic [`crate::widgets::input_composer`] hint string respectively.
//!
//! Deliberately **DIVERGES** from tagent (Slice 5, DOC-50 §5 — an intentional
//! improvement per spec, not an oversight or a parity gap):
//! - **Ctrl-C cancels the in-flight request** (see `reduce::apply_key`'s
//!   `'c'` arm, staging `Self::pending_cancel` for `crate::run::run`'s
//!   dispatch step to relay as a real `TuiEngine::cancel_session` RPC).
//!   Tagent's actual Ctrl-c (`crates/trusty-agents/src/repl/tui/keys.rs`)
//!   only clears the input buffer — tagent's cancel trigger is Up-arrow
//!   while `thinking`. DOC-50 §5 Slice 5 specifies Ctrl-C as the cancel key
//!   (the conventional terminal interrupt), which this crate implements as
//!   written; Up-arrow-while-busy ALSO still signals cancel here (ported
//!   faithfully from tagent, see above), so both triggers work.
//! - **Up/Down walk the whole session's prompt history** (#8181). Tagent's
//!   real `KeyCode::Up` arm recalls only `last_prompt` — one snapshot,
//!   overwritten every submit — and its `KeyCode::Down` arm calls
//!   `history_next` against a `history_idx` nothing ever sets, so Down is a
//!   no-op there. This crate wires Up to `Self::history_prev` instead, which
//!   makes both arms live: Up steps older through `Self::history`, Down
//!   steps newer and restores the in-progress draft at the end. The
//!   busy-cancel half of Up is unchanged.
//!
//! # Spec References
//! - [`SPEC-TTUI-03~draft`](docs/specs/DOC-50-tcode-tui-claude-code-clone.md#SPEC-TTUI-03~draft) — §3.2, the generalization layer.
//! - [`SPEC-TTUI-05~draft`](docs/specs/DOC-50-tcode-tui-claude-code-clone.md#SPEC-TTUI-05~draft) — Slice 4 deliverable (§5, Slice 4): `ReplApp` state.

mod reduce;

pub use reduce::apply;

use std::collections::HashMap;
use std::sync::Arc;
use std::sync::atomic::AtomicUsize;

use ratatui::style::Color;
use ratatui::text::Line;

use crate::event::WorkstreamSummary;
use crate::model::{
    CommandDescriptor, PendingPermission, PermissionAnswer, PermissionResponse, PickerRequest,
    StatuslineSegment,
};
use crate::run::TuiModel;
use crate::text::{strip_interior_blank_lines, trim_surrounding_blank_lines};

/// The scrollback line a dispatched, not-yet-confirmed cancel shows (#8207).
///
/// Why: kept next to [`CANCELLED_STATUS`] so the one word that claims the run
/// actually stopped is visibly separate from the one that only claims it was
/// asked to.
pub(crate) const CANCELLING_STATUS: &str = "cancelling…";

/// The scrollback line a CONFIRMED stop shows (#8207) — the only cancel line
/// in this crate that says the run is over.
pub(crate) const CANCELLED_STATUS: &str = "cancelled";

/// One rendered chat entry in the scrollback.
///
/// Why: a direct, unmodified port of tagent's `ChatLine`
/// (`crates/trusty-agents/src/repl/tui/types.rs`) — already fully
/// engine-agnostic (a role + a string), so no generalization was needed.
#[derive(Debug, Clone)]
pub struct ChatLine {
    pub role: ChatRole,
    pub text: String,
    /// The tool call this entry renders as a card, if any (#4596). When
    /// `Some`, the scrollback draws the card and ignores `text`; `role` still
    /// places it — [`ChatRole::Status`] at top level, [`ChatRole::Delegated`]
    /// inside a delegation block.
    pub tool: Option<ToolCard>,
}

/// One tool call and its result, rendered as a single scrollback card
/// (#4596).
///
/// Why: [`crate::event::ReplEvent::ToolInvocation`] arrives twice per call
/// (start, then completion) sharing an `id`. Holding both halves in one value
/// lets the reducer fill in one card instead of pushing two entries.
/// What: `result` is `None` while the call is in flight. `args` keeps the
/// start event's payload, because a completion event may carry `Null` args.
/// Test: `reduce::tests::tool_invocation_result_merges_into_its_call_card`.
#[derive(Debug, Clone, PartialEq)]
pub struct ToolCard {
    /// The engine-minted call id shared by the start and completion events.
    pub id: String,
    /// The invoked tool's name.
    pub tool_name: String,
    /// The call's arguments, as the start event carried them.
    pub args: serde_json::Value,
    /// The tool's output; `None` while the call is still running.
    pub result: Option<String>,
    /// The backend's own verdict that this call failed (#4596), copied from
    /// [`crate::event::ReplEvent::ToolInvocation`]'s `failed`. `false` while
    /// the call is still running.
    pub failed: bool,
    /// Whether the card draws as a one-line summary rather than its full
    /// result body (#4596).
    ///
    /// Why: a successful `read_file` or `cargo test` buries the conversation
    /// under dozens of rows nobody asked to re-read. A collapsed card keeps
    /// the call visible and costs one row.
    /// What: `false` while the call is in flight (the body is a single
    /// `running…` row anyway); set by the reducer when the result lands —
    /// `true` on success, `false` on an error, so a failure is never hidden.
    /// Ctrl-O toggles it (`reduce::apply_key`).
    pub collapsed: bool,
}

impl ToolCard {
    /// Whether this call failed (#4596).
    ///
    /// Why: an error must be impossible to hide. An earlier revision read
    /// the `FAILED:` / `ERROR:` prefix trusty-code writes into the result
    /// text, which made a producer reword silently render a failed call as
    /// a collapsed green `· ok` card. The verdict now travels as data —
    /// [`crate::event::ReplEvent::ToolInvocation`]'s `failed` — and the
    /// prefix is display text only.
    /// What: [`Self::failed`], AND a result having arrived. A producer is
    /// contracted to leave `failed` false on a start event; requiring the
    /// result too means a slip there cannot paint an in-flight card red.
    /// Test: `widgets::tool_card::tests::errored_card_renders_expanded_by_default`,
    /// `widgets::tool_card::tests::failure_flag_alone_drives_the_error_render`,
    /// `reduce::tests::tool_card_is_error_reads_the_event_failure_flag`.
    pub fn is_error(&self) -> bool {
        self.failed && self.result.is_some()
    }
}

/// Source/role of a chat line — drives the leader glyph and color chosen by
/// [`crate::widgets::scrollback::build_chat_lines`].
///
/// Why: a direct port of tagent's `ChatRole` — the four roles (who's
/// speaking, or a system status line) are universal to any chat-shaped TUI,
/// not specific to tagent's personas.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ChatRole {
    /// User input.
    User,
    /// Assistant/agent response.
    Assistant,
    /// An error response (rendered in the scrollback's error color).
    Error,
    /// An informational status line (e.g. "Connection lost"), or a
    /// top-level tool card when the entry carries [`ChatLine::tool`].
    Status,
    /// The header or footer line framing one delegated sub-agent's block
    /// (#7940). Rendered flush-left with its own glyph so the block's start
    /// and end are scannable.
    Delegation,
    /// Output or a tool notice produced by a delegated sub-agent (#7940).
    /// Rendered indented inside the [`ChatRole::Delegation`] frame above it.
    Delegated,
}

/// One delegated sub-agent whose block is currently open in the scrollback
/// (#7940).
///
/// Why: the reducer needs two things after a delegation starts — whether an
/// arriving `(agent_id, turn_id)` stream belongs inside a delegation block,
/// and which agent name the status line should show. Both are answered by
/// this pair, kept in a `Vec` so the first-seen order is the display order.
/// What: `agent_id` is the seam's opaque id (empty only for a producer that
/// announced the delegation before the sub-agent existed — see
/// [`crate::event::ReplEvent::DelegationStarted`]); `agent` is the display
/// name.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Delegation {
    /// The opaque per-spawn agent id this block is keyed on.
    pub agent_id: String,
    /// The delegated agent's display name.
    pub agent: String,
}

/// All mutable state the shared render/event loop needs to draw one frame.
///
/// Why: kept separate from the render/event-loop machinery (mirroring
/// tagent's original rationale) so unit tests can drive state transitions
/// without a `Terminal`. See the module doc comment for what was
/// deliberately left out of the generalized port.
/// What: every field is `pub` (matching tagent's style — this is a plain
/// data/mutator struct, not an encapsulated type) so a product binary can
/// seed banner/branding fields at construction without a builder API.
#[derive(Debug, Clone)]
pub struct ReplApp {
    /// Scrollback history of user/assistant/status exchanges.
    pub chat: Vec<ChatLine>,
    /// Current input line being edited.
    pub input_buf: String,
    /// Byte offset within `input_buf`.
    pub cursor_pos: usize,
    /// Number of lines scrolled up from the bottom (0 = pinned to newest).
    pub scroll_offset: usize,
    /// Last-rendered maximum scroll offset (published by the scrollback
    /// widget each frame; `scroll()` clamps against it). `Arc` so a render
    /// snapshot and the authoritative instance behind a caller's mutex (if
    /// any) share the same cell — mirrors tagent's `last_max_scroll`.
    pub last_max_scroll: Arc<AtomicUsize>,
    /// Every prompt submitted this session, oldest first, with adjacent
    /// duplicates collapsed ([`Self::remember_input`]). Up/Down walk it
    /// (#8181).
    ///
    /// Why: scoped to one TUI process deliberately. Persisting across
    /// launches would need a store this crate cannot reach — it must not
    /// depend on `trusty_common` (see the crate-level doc comment's
    /// dependency-direction constraint) and has no product-specific state
    /// directory of its own — so cross-launch history stays with whichever
    /// product wants it, seeding this field at construction.
    pub history: Vec<String>,
    /// Index into `history` while navigating. `None` when not navigating.
    pub history_idx: Option<usize>,
    /// The in-progress draft stashed by [`Self::history_prev`]'s first step
    /// and restored by [`Self::history_next`] at the newest end (#8181).
    /// `None` when not navigating.
    pub saved_input: Option<String>,
    /// Whether to show the welcome banner in the scrollback.
    pub show_banner: bool,
    /// The name shown in the chat leader (`⏺ <label> · `) and the input
    /// prompt (`<label>> `). Generic replacement for tagent's
    /// `project_name` — any single display label a product wants there.
    pub label: String,
    /// The user's display name, shown in the banner's identity line.
    pub user_label: String,
    /// Accent color for the chat leader glyph and label. Generic replacement
    /// for tagent's `AgentScope`-driven cyan/yellow — a product sets this to
    /// whatever distinguishes its own contexts (or leaves the default).
    pub accent_color: Color,
    /// Prefix rendered before every [`ChatRole::Status`] line (e.g.
    /// `"[tagent] "`). Defaults to `"[{label}] "` in [`ReplApp::new`];
    /// override if a product wants different bracketed text than its
    /// `label`.
    pub status_prefix: String,
    /// Banner title text (defaults to `label`).
    pub banner_title: String,
    /// Version string shown in the banner (defaults to this crate's own
    /// version — a product should override with its own).
    pub version: String,
    /// Engine-supplied startup splash lines (#8164). Empty by default; when
    /// non-empty they REPLACE the banner's generic `{banner_title}
    /// v{version}` identity row rather than stacking above or below it, so a
    /// product that knows its own launch facts states them once. Set from
    /// [`crate::event::ReplEvent::SplashUpdated`], which is also where the
    /// content contract lives.
    pub splash: Vec<String>,
    /// Recent-activity lines shown in the banner's right column. Generic
    /// replacement for tagent's `git_commits` — a product decides what
    /// "recent activity" means for it (or supplies none).
    pub recent_activity: Vec<String>,
    /// Optional pre-rendered left-column art (e.g. a splash logo). Empty by
    /// default — this crate has no branding assets of its own (see the
    /// dependency-direction constraint in the crate-level doc comment: this
    /// crate must not depend on a product-specific art asset).
    pub banner_art: Vec<Line<'static>>,
    /// Slash commands shown in the banner's "Commands" section, sourced from
    /// [`crate::engine::TuiEngine::commands`] plus any client-side built-ins.
    pub commands: Vec<CommandDescriptor>,
    /// An inline picker currently open for selection (DOC-50 §3.2/§6 Q6),
    /// staged by [`crate::commands::dispatch_forward`] when a bare `/name`
    /// submission matches `TuiEngine::picker(name)`. `None` when no picker
    /// is open. The picker WIDGET itself (navigation/rendering) is a
    /// follow-up slice's concern (DOC-50 §5 Slice 7's deliverable scopes
    /// "routing correctness", not the visual overlay); this field is the
    /// data half of that contract.
    pub active_picker: Option<PickerRequest>,
    /// The permission request currently blocking the turn (#3422), or `None`
    /// when nothing is pending.
    ///
    /// Why: a suspended tool call is a second, independent reason input must
    /// not be accepted — [`Self::busy`] cannot express it, because the
    /// backend is stopped rather than working. `Self::submit_line` refuses
    /// while this is `Some`, exactly as it refuses while `busy`, and
    /// `reduce::apply_key` routes every key to the decision bindings.
    /// What: set by [`crate::event::ReplEvent::PermissionRequested`], cleared
    /// by a matching `PermissionResolved` or by the user answering.
    pub pending_permission: Option<PendingPermission>,
    /// An answer staged for the outer driver to relay to
    /// [`crate::engine::TuiEngine::respond_permission`] (#3422). Drained by
    /// [`TuiModel::take_pending_permission_response`], same as
    /// [`Self::pending_submit`].
    pub pending_permission_response: Option<PermissionResponse>,
    /// Why the last answer to [`Self::pending_permission`] never reached the
    /// backend (#3422), rendered as an inline retry line on the reopened
    /// prompt; `None` on a prompt that has not been answered yet.
    ///
    /// Why: reopening the prompt alone would look like the backend asked
    /// twice. The operator needs to know their answer was lost, not
    /// re-asked, before choosing again.
    /// What: set by [`crate::event::ReplEvent::PermissionAnswerFailed`],
    /// cleared whenever the prompt is opened, answered, or resolved.
    pub permission_error: Option<String>,
    /// Whether a response is currently streaming in. Drives the input
    /// composer's placeholder text.
    pub busy: bool,
    /// Whether a dispatched cancel is still unconfirmed (#8207).
    ///
    /// Why: the daemon's `session.cancel` only returns once the run has
    /// actually stopped, and can answer "not stopped yet". Between the Ctrl-C
    /// and that answer the turn may still be running, so the TUI shows a
    /// cancelling state and accepts no new prompt — printing "cancelled" and
    /// reopening input on request is what earned the next prompt a
    /// `-32003 already has a task running` refusal in #8207's transcript.
    /// What: set by [`TuiModel::on_cancel_requested`] when the cancel is
    /// dispatched, cleared by [`crate::event::ReplEvent::CancelSettled`] on
    /// every arm except [`crate::model::CancelReply::StillCancelling`], which
    /// keeps it set because the run genuinely has not stopped. Read by
    /// [`Self::accepts_submit`] and by
    /// [`crate::widgets::input_composer`]'s label.
    pub cancelling: bool,
    /// Index into `chat` of the in-progress streaming assistant entry, if
    /// any. `None` when idle.
    pub streaming_idx: Option<usize>,
    /// Delegated sub-agents whose scrollback block is currently open, in
    /// first-seen order (#7940). Pushed by
    /// [`crate::event::ReplEvent::DelegationStarted`], removed by
    /// `DelegationFinished`. The last entry names the agent the status line
    /// shows — see [`Self::active_agent`].
    pub delegations: Vec<Delegation>,
    /// Index into `chat` of the in-progress bubble for one `(agent_id,
    /// turn_id)` stream (#7940). Separate from [`Self::streaming_idx`] —
    /// which is the single unkeyed slot
    /// [`crate::event::ReplEvent::AssistantOutput`] uses — precisely so two
    /// agents streaming at once cannot land in one bubble.
    pub agent_streams: HashMap<(String, String), usize>,
    /// Index into `chat` of the card for each in-flight tool call, keyed by
    /// the call's `id` (#4596). Inserted when a call starts; removed when its
    /// result lands in that card.
    pub tool_cards: HashMap<String, usize>,
    /// Engine-supplied statusline segments, most recently pushed via
    /// [`crate::event::ReplEvent::StatuslineUpdate`].
    pub statusline: Vec<StatuslineSegment>,
    /// The active workstream, most recently pushed via
    /// [`crate::event::ReplEvent::WorkstreamUpdated`].
    pub active_workstream: Option<WorkstreamSummary>,
    /// Quit signal — set on Ctrl-D. Backs [`TuiModel::should_quit`].
    pub quit: bool,
    /// A line the user just submitted (typed Enter, or a synthesized
    /// `ReplEvent::Submit`), staged for the outer driver to forward to
    /// `TuiEngine::handle_input`. `apply` cannot reach the engine or the
    /// event channel itself (see [`reduce`]'s doc comment), so — mirroring
    /// tagent's `pending_picker_selection`/`pending_submit` pattern — it
    /// stashes the line here instead. Drained (`Option::take`) by the
    /// caller after each `apply` call.
    pub pending_submit: Option<String>,
    /// Set when the user pressed Ctrl-C. Drained by the caller, which should
    /// call `TuiEngine::cancel_session` (thin-client axiom C-2 — cancellation
    /// is the backend's job, not just clearing local UI state).
    pub pending_cancel: bool,
    /// The most recently submitted line. Direct port of tagent's
    /// `last_prompt`, kept as a plain snapshot a product can read.
    ///
    /// Why: Up-arrow no longer reads it (#8181 moved recall to
    /// [`Self::history`]/[`Self::history_idx`]); the field stays because it
    /// is published API and costs one `String` clone per submit.
    pub last_prompt: String,
    /// The most recently seen executable-shell (bash/sh/zsh/fish) fenced
    /// code block across all assistant messages, updated by
    /// [`Self::update_last_bash_block`]. Ctrl-E pastes this block's first
    /// non-blank line into an empty input buffer — direct port of tagent's
    /// `last_bash_block` (`crates/trusty-agents/src/repl/tui/types.rs`).
    pub last_bash_block: Option<String>,
    /// The generation number of the turn currently considered "live" —
    /// mirrors `crate::run::dispatch_pending`'s `AtomicU64` counter, updated
    /// via [`TuiModel::set_current_generation`] on the SAME serial
    /// event-loop task that owns that counter (never from a spawned task).
    /// A `ReplEvent::TurnFinished { generation }` whose `generation` no
    /// longer matches this field is a stale signal from a superseded turn
    /// and is ignored — see that variant's doc comment for the TOCTOU race
    /// this by-construction design closes (the comparison happens here, in
    /// the reducer, serially — never as a spawned task's load-then-branch
    /// against the shared counter).
    pub current_generation: u64,
}

impl ReplApp {
    /// Construct a fresh app with sensible generic defaults.
    ///
    /// Why: every field needs *some* value; the defaults chosen here (empty
    /// scrollback, banner visible, cyan accent, no commands/art yet) mirror
    /// tagent's `ReplApp::new` starting state minus the fields that didn't
    /// survive generalization.
    /// What: `label`/`user_label` seed both the derived `status_prefix` and
    /// `banner_title` — override those two afterward if a product wants
    /// different text than a straight derivation from `label`.
    pub fn new(label: impl Into<String>, user_label: impl Into<String>) -> Self {
        let label = label.into();
        let status_prefix = format!("[{label}] ");
        let banner_title = label.clone();
        Self {
            chat: Vec::new(),
            input_buf: String::new(),
            cursor_pos: 0,
            scroll_offset: 0,
            last_max_scroll: Arc::new(AtomicUsize::new(0)),
            history: Vec::new(),
            history_idx: None,
            saved_input: None,
            show_banner: true,
            label,
            user_label: user_label.into(),
            accent_color: Color::Cyan,
            status_prefix,
            banner_title,
            version: env!("CARGO_PKG_VERSION").to_string(),
            splash: Vec::new(),
            recent_activity: Vec::new(),
            banner_art: Vec::new(),
            commands: Vec::new(),
            active_picker: None,
            pending_permission: None,
            pending_permission_response: None,
            permission_error: None,
            busy: false,
            cancelling: false,
            streaming_idx: None,
            delegations: Vec::new(),
            agent_streams: HashMap::new(),
            tool_cards: HashMap::new(),
            statusline: Vec::new(),
            active_workstream: None,
            quit: false,
            pending_submit: None,
            pending_cancel: false,
            last_prompt: String::new(),
            last_bash_block: None,
            current_generation: 0,
        }
    }

    /// Append a user prompt line to the chat scrollback.
    pub fn push_user(&mut self, text: impl Into<String>) {
        self.chat.push(ChatLine {
            role: ChatRole::User,
            text: text.into(),
            tool: None,
        });
        self.scroll_offset = 0;
    }

    /// Append an assistant response to the chat scrollback, trimming
    /// surrounding and interior blank lines first.
    ///
    /// Why: assistant/agent responses regularly arrive with leading/trailing
    /// blank lines and markdown-style `\n\n` paragraph breaks; rendering
    /// them verbatim leaves visible dead space. See [`crate::text`] for the
    /// two trimming passes.
    /// Test: `reduce::tests::push_assistant_trims_surrounding_blanks`.
    pub fn push_assistant(&mut self, text: impl Into<String>, is_error: bool) {
        let role = if is_error {
            ChatRole::Error
        } else {
            ChatRole::Assistant
        };
        let raw: String = text.into();
        let trimmed = trim_surrounding_blank_lines(&raw);
        let collapsed = strip_interior_blank_lines(&trimmed);
        self.chat.push(ChatLine {
            role,
            text: collapsed,
            tool: None,
        });
        self.scroll_offset = 0;
        self.update_last_bash_block();
    }

    /// Rescan `self.chat` and refresh [`Self::last_bash_block`] with the
    /// last executable-shell fenced block seen across all assistant
    /// messages, newest-first. Direct port of tagent's
    /// `ReplApp::update_last_bash_block`
    /// (`crates/trusty-agents/src/repl/tui/app.rs`).
    ///
    /// Why: called after every chat mutation that could introduce or
    /// obsolete a shell block ([`Self::push_assistant`], and the streaming
    /// finalize path in [`reduce`]) so Ctrl-E's paste buffer never goes
    /// stale relative to `chat`.
    /// What: walks `chat` newest-to-oldest; the first [`ChatRole::Assistant`]
    /// entry containing an executable shell fence wins (errors and user/
    /// status entries are skipped — an error entry is never a paste source).
    /// `None` when no assistant entry has one.
    /// Test: `reduce::tests::push_assistant_updates_last_bash_block`,
    /// `reduce::tests::push_assistant_skips_error_entries_for_bash_block`.
    pub fn update_last_bash_block(&mut self) {
        for entry in self.chat.iter().rev() {
            if entry.role != ChatRole::Assistant {
                continue;
            }
            if let Some(block) = crate::render::markdown::extract_last_shell_block(&entry.text) {
                self.last_bash_block = Some(block);
                return;
            }
        }
        self.last_bash_block = None;
    }

    /// Append a status line (rendered with [`Self::status_prefix`]).
    pub fn push_status(&mut self, text: impl Into<String>) {
        self.chat.push(ChatLine {
            role: ChatRole::Status,
            text: text.into(),
            tool: None,
        });
        self.scroll_offset = 0;
    }

    /// Clear the scrollback (backs `ReplEvent::ClearScrollback`, the shared
    /// `/clear` built-in per DOC-50 §5 Slice 7).
    pub fn clear_scrollback(&mut self) {
        self.chat.clear();
        self.streaming_idx = None;
        // #7940: both keyed-stream maps index INTO `chat`, so clearing it
        // without clearing them would leave every key pointing at a stale
        // (or out-of-range) row.
        self.agent_streams.clear();
        self.delegations.clear();
        // #4596: same for the tool-call index map.
        self.tool_cards.clear();
        self.scroll_offset = 0;
    }

    /// The delegated agent whose work is currently in flight, if any
    /// (#7940).
    ///
    /// Why: the status line shows "who is working right now"; that is the
    /// most recently opened, not-yet-closed delegation, and it must revert
    /// on its own when the block closes rather than needing the engine to
    /// push a statusline update.
    /// What: the last entry of [`Self::delegations`]; `None` when no
    /// delegation is open.
    /// Test: `delegation_started_pushes_header_and_sets_active_agent`,
    /// `delegation_finished_pushes_footer_and_clears_active_agent`,
    /// `build_statusline_appends_active_agent_while_delegating`.
    pub fn active_agent(&self) -> Option<&str> {
        self.delegations.last().map(|d| d.agent.as_str())
    }

    /// Expand or collapse the newest tool-call card in the scrollback
    /// (#4596).
    ///
    /// Why: this TUI has no card-focus concept — nothing moves a selection
    /// between scrollback entries — so "the focused card" is the most recent
    /// one, which is also the one the user just watched arrive and the only
    /// one still on screen at scroll offset 0. A focus ring would be a
    /// bigger surface than the toggle it serves.
    /// What: walks `chat` newest-first for the first entry carrying a
    /// [`ChatLine::tool`] and flips its [`ToolCard::collapsed`]. No-op when
    /// no card exists. Returns `true` when a card was toggled, so a caller
    /// can tell "nothing to toggle" from "toggled".
    /// Test: `reduce::tests::ctrl_o_toggles_the_newest_tool_card_round_trip`,
    /// `reduce::tests::ctrl_o_is_a_noop_when_no_tool_card_exists`.
    pub fn toggle_last_tool_card(&mut self) -> bool {
        for entry in self.chat.iter_mut().rev() {
            if let Some(card) = entry.tool.as_mut() {
                card.collapsed = !card.collapsed;
                return true;
            }
        }
        false
    }

    /// Open an inline picker (DOC-50 §3.2/§6 Q6), staged by
    /// [`crate::commands::dispatch_forward`] for a bare command matching
    /// `TuiEngine::picker(name)`.
    pub fn open_picker(&mut self, request: PickerRequest) {
        self.active_picker = Some(request);
    }

    /// Close the active picker without a selection (e.g. Esc). No-op if
    /// none is open.
    pub fn close_picker(&mut self) {
        self.active_picker = None;
    }

    /// Confirm the picker item at `index`, closing the picker and returning
    /// the composed `"{dispatch_command} {selected.id}"` line
    /// ([`crate::commands::compose_selection`]) to resubmit — mirrors
    /// [`crate::model::PickerRequest`]'s documented contract. Returns `None`
    /// (leaving the picker untouched) when no picker is open or `index` is
    /// out of range, so a stale/invalid confirmation is a no-op rather than
    /// a panic.
    /// Test: `crate::commands::tests::confirm_picker_selection_composes_and_closes`,
    /// `crate::commands::tests::confirm_picker_selection_out_of_range_is_noop`.
    pub fn confirm_picker_selection(&mut self, index: usize) -> Option<String> {
        let request = self.active_picker.as_ref()?;
        let selected = request.items.get(index)?;
        let composed = crate::commands::compose_selection(request, selected);
        self.active_picker = None;
        Some(composed)
    }

    /// Push the current input onto history (with adjacent-duplicate dedup),
    /// record it as [`Self::last_prompt`] (Up-arrow recall), echo it to the
    /// scrollback, then route it (DOC-50 §5 Slice 7, §6 Q4): a recognized
    /// built-in command ([`crate::commands::route`]) is applied inline and
    /// never reaches an engine; anything else marks the app [`Self::busy`]
    /// and stages it in [`Self::pending_submit`] for the caller to forward.
    ///
    /// Why: shared by the Enter-key path and a synthesized
    /// `ReplEvent::Submit` (e.g. a picker confirmation, DOC-50 §3.2/§6 Q6)
    /// so both go through one echo+route code path rather than duplicating
    /// it — see [`reduce`]. Routing lives here rather than in `reduce`'s
    /// `apply` because [`crate::commands::route`] is pure and needs no
    /// `TuiEngine` handle (`apply` has none — see that module's doc
    /// comment), so both submission paths get built-in short-circuiting for
    /// free. `busy`/`pending_submit` are set ONLY for the non-built-in
    /// (`Route::Forward`) case — a built-in never calls
    /// `TuiEngine::handle_input`, so there is nothing to wait on and no
    /// `AssistantOutput` will ever arrive to clear `busy` again. Setting
    /// `busy` before any streamed chunk arrives (not on the first
    /// `AssistantOutput` chunk) closes the pre-first-token latency window —
    /// see the module doc comment's disclosure list.
    ///
    /// **A second turn cannot start while one is in flight**: if
    /// [`Self::busy`] is already `true`, this whole function (routing
    /// included) is a no-op — DOC-50 §5 Slice 5's "blocks user input until
    /// cancel completes" requirement, made literal rather than reasoned
    /// away. This is the authoritative guard (not just a UI nicety at the
    /// call site): without it, a second `handle_input` task starts while the
    /// first is still streaming, and with `streaming_idx` shared per-app
    /// rather than per-task, the second task's chunks splice into the first
    /// task's (now orphaned) chat entry — a real corruption bug this guard
    /// closes at the source. Callers that want to preserve the user's typed
    /// text when busy (the Enter-key path, [`reduce::apply_key`]) must check
    /// `!app.busy` themselves BEFORE consuming the input buffer, since by
    /// the time this function is reached the line is already taken out of
    /// `input_buf` and would otherwise be silently lost, not just silently
    /// ignored.
    /// What: [`crate::commands::BuiltIn::Clear`] reuses
    /// [`Self::clear_scrollback`] (the same effect
    /// `ReplEvent::ClearScrollback` produces); [`crate::commands::BuiltIn::Quit`]
    /// sets [`Self::quit`]; [`crate::commands::BuiltIn::Help`] renders
    /// [`crate::commands::render_help`] (built-ins + [`Self::commands`]) as
    /// a status line.
    /// Test: [`crate::commands::tests::submit_line_builtin_clear_clears_scrollback`],
    /// [`crate::commands::tests::submit_line_builtin_quit_sets_quit`],
    /// [`crate::commands::tests::submit_line_builtin_help_lists_commands`],
    /// [`crate::commands::tests::submit_line_forwards_non_builtin_and_marks_busy`],
    /// [`reduce::tests::apply_enter_while_busy_inserts_a_newline_instead_of_submitting`],
    /// [`reduce::tests::queued_multi_line_type_ahead_submits_byte_identical_text`],
    /// [`reduce::tests::apply_submit_event_is_noop_while_busy`],
    /// [`reduce::tests::submit_line_is_noop_while_a_permission_prompt_is_pending`].
    pub(crate) fn submit_line(&mut self, line: String) {
        // #3422/#8207: a suspended permission request and an unconfirmed cancel
        // block the turn for the same reason `busy` does — the backend will not
        // accept new work — so all three live behind one predicate, and it is
        // authoritative here rather than only at the key-handling call site.
        if !self.accepts_submit() {
            return;
        }
        self.remember_input(&line);
        self.last_prompt = line.clone();
        self.push_user(line.clone());
        match crate::commands::route(&line) {
            crate::commands::Route::BuiltIn(crate::commands::BuiltIn::Clear) => {
                self.clear_scrollback();
            }
            crate::commands::Route::BuiltIn(crate::commands::BuiltIn::Quit) => {
                self.quit = true;
            }
            crate::commands::Route::BuiltIn(crate::commands::BuiltIn::Help) => {
                self.push_status(crate::commands::render_help(&self.commands));
            }
            crate::commands::Route::Forward(forwarded) => {
                self.busy = true;
                self.pending_submit = Some(forwarded);
            }
        }
    }

    /// Whether the backend can be handed a new turn right now (#8207).
    ///
    /// Why: three independent conditions mean "the backend will refuse new
    /// work" — a turn in flight ([`Self::busy`]), a suspended permission
    /// request ([`Self::pending_permission`]), and a dispatched cancel the
    /// backend has not confirmed yet ([`Self::cancelling`]). They were checked
    /// ad hoc in two places, so the cancel window was easy to add to one and
    /// miss in the other; one predicate makes both call sites ask the same
    /// question. The Enter key must consult it BEFORE taking the input buffer,
    /// or the typed line is consumed and then dropped by
    /// [`Self::submit_line`]'s own guard.
    /// What: pure read, no mutation. `false` does not mean the keystroke is
    /// lost — `reduce::apply_key` queues the typed text in the composer
    /// instead (#8240). Not a link: it is a private free function in a private
    /// module, which rustdoc cannot resolve at all under
    /// `deny(rustdoc::broken_intra_doc_links)`.
    /// Test: `reduce::tests::a_prompt_submitted_while_cancelling_is_not_dispatched`,
    /// `reduce::tests::an_unsettled_cancel_keeps_input_closed_after_the_turn_finished_on_its_own`
    /// (the only case where [`Self::cancelling`] is the deciding term — `busy`
    /// closes the gate in the others),
    /// `reduce::tests::apply_cancel_settled_stopped_reopens_input`.
    pub fn accepts_submit(&self) -> bool {
        !self.busy && !self.cancelling && self.pending_permission.is_none()
    }

    /// Answer the open permission prompt (#3422), staging the answer for the
    /// outer driver and releasing the input block.
    ///
    /// Why: the prompt is modal, so it must close the moment the user
    /// answers rather than waiting on the RPC — the same reasoning
    /// [`TuiModel::on_cancel_requested`] documents for entering the cancelling
    /// state ahead of the cancel RPC. A backend that is slow to confirm must
    /// not leave the keyboard dead. The backend's own
    /// [`crate::event::ReplEvent::PermissionResolved`] is what records the
    /// outcome in the scrollback; this method records nothing, because the
    /// TUI's belief about the decision is not evidence that it was applied.
    /// What: no-op when no prompt is open. Otherwise takes the prompt and
    /// stages [`Self::pending_permission_response`] carrying it whole, so a
    /// relay that fails can hand the same request back (#3422 — see
    /// [`crate::event::ReplEvent::PermissionAnswerFailed`]). Also clears
    /// [`Self::permission_error`]: this attempt has not failed yet, and a
    /// stale retry line must not survive onto it.
    /// Test: `reduce::tests::permission_key_y_answers_allow_once`,
    /// `reduce::tests::permission_key_a_answers_allow_for_session`,
    /// `reduce::tests::permission_key_n_answers_deny`,
    /// `reduce::tests::permission_unbound_key_leaves_the_prompt_pending`,
    /// `crate::run::tests::dispatch_pending_permission_answer_failure_reopens_the_prompt`.
    pub fn answer_permission(&mut self, answer: PermissionAnswer) {
        let Some(pending) = self.pending_permission.take() else {
            return;
        };
        self.permission_error = None;
        self.pending_permission_response = Some(PermissionResponse { pending, answer });
    }

    /// Push a line onto [`Self::history`], deduping an immediate repeat
    /// (readline's `ignoredups` convention, #8181).
    ///
    /// Why: submitting the same prompt twice in a row should cost one Up
    /// press to recall, not two.
    /// What: blank/whitespace-only lines are never recorded; a line equal to
    /// the current newest entry is dropped. A repeat that is NOT adjacent is
    /// kept, so the walk order still reflects what was actually run.
    /// Test: `reduce::tests::consecutive_duplicate_submissions_are_stored_once`,
    /// `reduce::tests::submitting_appends_to_history_and_resets_the_cursor`.
    pub fn remember_input(&mut self, line: &str) {
        if line.trim().is_empty() {
            return;
        }
        if self.history.last().map(|s| s.as_str()) == Some(line) {
            return;
        }
        self.history.push(line.to_string());
    }

    /// Set the input buffer + cursor in one shot (cursor moves to the end).
    pub fn set_input(&mut self, s: String) {
        self.cursor_pos = s.len();
        self.input_buf = s;
    }

    /// Insert a single character at the cursor.
    ///
    /// Why: editing does NOT end history navigation (#8181). It used to
    /// clear [`Self::history_idx`] while leaving [`Self::saved_input`] set,
    /// so Up-then-type stranded the draft with no way back — and Backspace
    /// and Ctrl-U never cleared it at all, making the same gesture behave
    /// three ways. Readline's rule is the consistent one: the draft is
    /// yours until you submit, and Down always walks back to it. Editing a
    /// RECALLED entry still discards that edit when you navigate away;
    /// per-entry edit buffers are not worth the state.
    pub fn insert_char(&mut self, c: char) {
        self.input_buf.insert(self.cursor_pos, c);
        self.cursor_pos += c.len_utf8();
    }

    /// Backspace: delete the char before the cursor.
    pub fn backspace(&mut self) {
        if self.cursor_pos == 0 {
            return;
        }
        let mut prev = self.cursor_pos - 1;
        while prev > 0 && !self.input_buf.is_char_boundary(prev) {
            prev -= 1;
        }
        self.input_buf.replace_range(prev..self.cursor_pos, "");
        self.cursor_pos = prev;
    }

    /// Move the cursor left one char-boundary.
    pub fn cursor_left(&mut self) {
        if self.cursor_pos == 0 {
            return;
        }
        let mut prev = self.cursor_pos - 1;
        while prev > 0 && !self.input_buf.is_char_boundary(prev) {
            prev -= 1;
        }
        self.cursor_pos = prev;
    }

    /// Move the cursor right one char-boundary.
    pub fn cursor_right(&mut self) {
        if self.cursor_pos >= self.input_buf.len() {
            return;
        }
        let mut next = self.cursor_pos + 1;
        while next < self.input_buf.len() && !self.input_buf.is_char_boundary(next) {
            next += 1;
        }
        self.cursor_pos = next;
    }

    /// Up-arrow history navigation: step one entry older, saving the
    /// in-progress draft on the first step (#8181).
    ///
    /// Why: the draft the user was typing must survive a walk through
    /// history and come back when they walk forward past the newest entry —
    /// losing it is the failure readline's `saved_input` slot exists to
    /// prevent.
    /// What: no-op on an empty history. The first step (`history_idx ==
    /// None`) stashes `input_buf` into [`Self::saved_input`] and lands on the
    /// newest entry; later steps move one older. At the oldest entry the key
    /// does NOTHING rather than re-reading `history[0]` into the buffer —
    /// since #8181 made editing keep you in history, a clamp that still
    /// called `set_input` silently destroyed an in-place edit of the oldest
    /// entry. The cursor moves to the end of a recalled line
    /// ([`Self::set_input`]).
    /// Test: `reduce::tests::apply_up_walks_back_through_submitted_prompts`,
    /// `reduce::tests::apply_down_walks_forward_and_restores_the_draft`,
    /// `reduce::tests::apply_up_clamps_at_the_oldest_entry`,
    /// `reduce::tests::apply_up_at_the_oldest_entry_keeps_an_in_place_edit`.
    pub fn history_prev(&mut self) {
        if self.history.is_empty() {
            return;
        }
        let new_idx = match self.history_idx {
            None => {
                self.saved_input = Some(self.input_buf.clone());
                self.history.len() - 1
            }
            // #8181: the floor is a true no-op, not a re-recall.
            Some(0) => return,
            Some(i) => i - 1,
        };
        self.history_idx = Some(new_idx);
        let entry = self.history[new_idx].clone();
        self.set_input(entry);
    }

    /// Down-arrow history navigation: step one entry newer, restoring the
    /// saved draft once past the newest entry (#8181).
    ///
    /// Why: the other half of [`Self::history_prev`]'s contract — walking
    /// forward off the newest entry returns the user to what they were
    /// typing, not to an empty line.
    /// What: no-op when not navigating (`history_idx == None`), so Down on a
    /// freshly typed line never clobbers it. Stepping past the newest entry
    /// clears `history_idx` and restores [`Self::saved_input`] (empty string
    /// when the draft was empty). Editing while navigating does not end
    /// navigation — see [`Self::insert_char`] — so the draft survives
    /// typing, Backspace and Ctrl-U alike.
    /// Test: `reduce::tests::apply_down_walks_forward_and_restores_the_draft`,
    /// `reduce::tests::apply_down_is_noop_when_not_navigating_history`,
    /// `reduce::tests::editing_a_recalled_entry_still_leaves_down_the_draft`.
    pub fn history_next(&mut self) {
        let Some(i) = self.history_idx else { return };
        if i + 1 >= self.history.len() {
            let restore = self.saved_input.take().unwrap_or_default();
            self.history_idx = None;
            self.set_input(restore);
        } else {
            self.history_idx = Some(i + 1);
            let entry = self.history[i + 1].clone();
            self.set_input(entry);
        }
    }

    /// Take the current input buffer and reset the editor state. Returns
    /// `None` if the trimmed buffer was empty (nothing to submit).
    pub fn take_input(&mut self) -> Option<String> {
        let trimmed = self.input_buf.trim();
        if trimmed.is_empty() {
            return None;
        }
        let out = std::mem::take(&mut self.input_buf);
        self.cursor_pos = 0;
        self.history_idx = None;
        self.saved_input = None;
        Some(out)
    }

    /// Apply a scroll delta. Negative = older (up), positive = newer (down).
    /// Clamps to `[0, last_max_scroll]` so mouse-wheel scroll-up can't
    /// accumulate past the actual scrollback height.
    pub fn scroll(&mut self, delta: isize) {
        if delta < 0 {
            self.scroll_offset = self.scroll_offset.saturating_add((-delta) as usize);
        } else {
            self.scroll_offset = self.scroll_offset.saturating_sub(delta as usize);
        }
        let cap = self
            .last_max_scroll
            .load(std::sync::atomic::Ordering::Relaxed);
        if self.scroll_offset > cap {
            self.scroll_offset = cap;
        }
    }
}

impl TuiModel for ReplApp {
    fn should_quit(&self) -> bool {
        self.quit
    }

    /// Drain [`Self::pending_submit`] — see `crate::run::dispatch_pending`
    /// (private to `crate::run`, called from [`crate::run::run`]) for the
    /// caller that reads this after every `apply` call.
    fn take_pending_submit(&mut self) -> Option<String> {
        self.pending_submit.take()
    }

    /// Drain-and-clear [`Self::pending_cancel`].
    fn take_pending_cancel(&mut self) -> bool {
        std::mem::replace(&mut self.pending_cancel, false)
    }

    /// Drain [`Self::pending_permission_response`] (#3422) — see
    /// `crate::run::dispatch_pending` for the caller.
    fn take_pending_permission_response(&mut self) -> Option<PermissionResponse> {
        self.pending_permission_response.take()
    }

    /// Enter the cancelling state the moment a cancel is dispatched (#8207).
    ///
    /// Why: this used to clear [`Self::busy`] and push "cancelled" here, ahead
    /// of the RPC — the bug in #8207. The daemon's cancel is cooperative, so
    /// on request the run may still be executing; saying "cancelled" and
    /// reopening input let the next prompt reach a daemon that still had a task
    /// running, and the user saw a raw
    /// `-32003 already has a task running` instead. `busy` therefore STAYS set
    /// until [`crate::event::ReplEvent::CancelSettled`] reports a confirmed
    /// stop, which is also what keeps #8240's queued-input behaviour covering
    /// the cancel window: keystrokes and Enter accumulate in the composer
    /// instead of starting a turn the backend would refuse.
    /// What: sets [`Self::cancelling`], abandons the in-progress streaming
    /// entry index (a later response starts a fresh chat entry rather than
    /// appending to one no more chunks will ever arrive for), and pushes a
    /// "cancelling…" status line. It deliberately reports no outcome — the
    /// TUI's belief about the cancel is not evidence the run stopped, the same
    /// reasoning [`Self::answer_permission`] records for a permission answer.
    /// Test: `reduce::tests::a_prompt_submitted_while_cancelling_is_not_dispatched`,
    /// `crate::run::tests::dispatch_pending_cancel_holds_input_until_the_reply_lands`.
    fn on_cancel_requested(&mut self) {
        self.cancelling = true;
        self.streaming_idx = None;
        self.push_status(CANCELLING_STATUS);
    }

    /// Record `generation` into [`Self::current_generation`] — see
    /// [`crate::run::TuiModel::set_current_generation`]'s doc comment for
    /// why this is called only from `dispatch_pending` on the serial
    /// event-loop task, and [`ReplEvent::TurnFinished`](crate::ReplEvent::TurnFinished)'s doc comment for
    /// the race this by-construction design closes.
    /// Test: `reduce::tests::apply_turn_finished_with_stale_generation_is_ignored`,
    /// `reduce::tests::apply_turn_finished_clears_busy_and_streaming_idx_without_touching_chat`.
    fn set_current_generation(&mut self, generation: u64) {
        self.current_generation = generation;
    }
}