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
//! [`apply`] — the `ReplApp` half of [`crate::run::event_loop`]'s
//! `apply: FnMut(&mut M, ReplEvent)` contract.
//!
//! Why: Slice 4 (already on main) needed *some* `apply` closure so the
//! widgets could be demonstrated against real key input before DOC-50 §5
//! Slice 5 ("Event dispatch and line editing") landed as its own slice — it
//! shipped the generic core: character insertion/deletion, cursor movement,
//! history recall, and scroll. Slice 5 (this revision) completes the keymap
//! to full parity with tagent's actual `crates/trusty-agents/src/repl/tui/keys.rs`
//! (not its doc comments, which in at least one case — Up-arrow —
//! misattribute production behavior to a dead-code helper) and wires
//! [`ReplApp::pending_submit`]/[`ReplApp::pending_cancel`] through to
//! `TuiEngine::handle_input`/`cancel_session` in [`crate::run::run`]. See
//! [`crate::app`]'s module doc comment for the full disclosed-differences
//! list. Tagent-specific extras (pickers, slash-completion, cost/token
//! tracking) stay behind per the generalization mandate; line-editing
//! features tagent's own `keys.rs` never had either (Ctrl-w word-delete, a
//! kill-ring, word-motion) are deliberately NOT invented here — full parity
//! means matching tagent, not exceeding it.
//!
//! What: `apply` cannot reach the event channel or the engine — it only has
//! `&mut ReplApp` — so anything that would normally need to call
//! `TuiEngine::handle_input`/`cancel_session` instead stages a signal on
//! `ReplApp` ([`ReplApp::pending_submit`], [`ReplApp::pending_cancel`]) for
//! the caller to drain after `apply` returns, mirroring tagent's own
//! `pending_picker_selection`/`pending_cancel` precedent
//! (`crates/trusty-agents/src/repl/tui/types.rs`). [`crate::run::run`]'s
//! dispatch step is the actual drain point — see that module for the
//! `engine.handle_input`/`engine.cancel_session` wiring and the
//! `ReplEvent::Quit` round-trip an engine's `Ok(false)` needs to reach
//! `ReplApp::quit`, since a spawned task can't reach `&mut ReplApp` directly.
//!
//! # Spec References
//! - [`SPEC-TTUI-05~draft`](docs/specs/DOC-50-tcode-tui-claude-code-clone.md#SPEC-TTUI-05~draft) — Slice 5 deliverable (§5, Slice 5): line-editor keymap + Ctrl-C daemon cancel.
use ReplApp;
use crate;
use crate;
/// How many lines a Page-Up/Page-Down key press scrolls.
const PAGE_SCROLL: isize = 10;
/// Apply one [`ReplEvent`] to `app`, mutating it in place.
///
/// Why: a free function (rather than an `ReplApp` method) so it matches
/// `crate::run::event_loop`'s `apply: impl FnMut(&mut M, ReplEvent)` shape
/// exactly — a caller passes `trusty_code_tui::app::apply` directly as that
/// argument.
/// What: dispatches on every [`ReplEvent`] variant (see the module doc
/// comment for what's out of scope). `Key` events are further dispatched by
/// [`apply_key`].
/// Test: `tests` below cover every variant this function actually changes
/// state for.
/// Accumulate one streamed assistant-output chunk into the in-progress chat
/// entry, finalizing (trim + role) it on `done`.
///
/// Why: split out of [`apply`] so the streaming state machine — "append to
/// the open entry, or open a new one" — reads as its own unit.
/// What: mirrors [`ReplApp::push_assistant`]'s trim/collapse pass, but only
/// on the *finished* text (trimming mid-stream would strip a blank line the
/// next chunk was about to fill back in). Also refreshes
/// [`ReplApp::last_bash_block`] on finalize, same as `push_assistant`
/// (Ctrl-E's paste buffer must not go stale just because a response arrived
/// via streaming instead of a single push).
/// Test: [`tests::apply_assistant_output_streams_into_one_entry`],
/// [`tests::apply_assistant_output_finalizes_as_error_role`],
/// [`tests::apply_assistant_output_refreshes_last_bash_block_on_finalize`].
/// Dispatch one translated key press to the appropriate `ReplApp` mutator.
///
/// Why: split out of [`apply`] so the (long, mechanical) key-by-key match
/// reads as its own unit, matching tagent's `keys.rs::handle_key` precedent
/// in spirit (though deliberately smaller — see the module doc comment for
/// what's deferred to Slice 5).
/// What: printable chars insert; Backspace/Left/Right/Home/End edit/move;
/// PageUp/PageDown scroll a page; Enter submits; Ctrl-a/u/c/d match the
/// readline bindings DOC-50 §5 Slice 5 specifies. Up, Down, and Ctrl-E are
/// direct ports of tagent's real `keys.rs` bindings rather than a Slice-5
/// invention — see [`apply_up`] and [`apply_ctrl_e`] for why they're pulled
/// into their own functions. Any other key (Tab, Esc, Delete,
/// `KeyCode::Other`) is a no-op — those are slash-completion/picker-
/// navigation concerns this slice doesn't own.
/// Test: [`tests`] below, one per binding.
/// Up-arrow: recall [`ReplApp::last_prompt`], and — while
/// [`ReplApp::busy`] — ALSO signal [`ReplApp::pending_cancel`].
///
/// Why: direct port of tagent's real `KeyCode::Up` arm
/// (`crates/trusty-agents/src/repl/tui/keys.rs`): a busy in-flight request
/// gets cancelled so the user can edit and resubmit, and the cancel signal
/// fires independent of whether `last_prompt` happens to be set (matching
/// tagent's unconditional `if app.thinking { app.pending_cancel = true; }`
/// ahead of the recall). This is NOT the multi-level `history_prev` browser
/// — see `crate::app`'s module doc comment for why that helper stays
/// unwired, exactly as it is in tagent.
/// What: no-ops the recall half when `last_prompt` is empty (nothing to
/// recall); the cancel signal is unconditional on `busy`.
/// Test: [`tests::apply_up_recalls_last_prompt_when_idle`],
/// [`tests::apply_up_signals_cancel_and_recalls_when_busy`],
/// [`tests::apply_up_signals_cancel_even_with_no_last_prompt`],
/// [`tests::apply_up_is_noop_when_idle_and_no_last_prompt`].
/// Ctrl-E: with an empty input buffer and a cached
/// [`ReplApp::last_bash_block`], paste that block's first non-blank line;
/// otherwise (non-empty buffer, or no cached block) move the cursor to the
/// end of the line.
///
/// Why: direct port of tagent's real `KeyCode::Char('e')` arm
/// (`crates/trusty-agents/src/repl/tui/keys.rs`) — only the first line
/// pastes because the REPL input is single-line; pasting a multi-line block
/// verbatim would silently truncate at the first `\n` on submit.
/// Test: [`tests::apply_ctrl_e_pastes_last_bash_block_when_input_empty`],
/// [`tests::apply_ctrl_e_falls_back_to_end_of_line_when_input_nonempty`],
/// [`tests::apply_ctrl_e_noop_when_no_block_and_input_empty`].