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
//! The slash-command parser/dispatcher — DOC-50 §5 Slice 7, §6 Q4 ("mixed
//! routing").
//!
//! Why: DOC-50 §6 Q4 resolves slash-command handling as a split: a small,
//! fixed set of client-side built-ins (`/help`, `/clear`, `/quit`/`/exit`)
//! that never reach a product's [`crate::engine::TuiEngine`], and every
//! other slash command (domain commands like `/model`, `/workstream`, plus
//! any command this crate has never heard of) forwarded verbatim to
//! `TuiEngine::handle_input` — "the client does NOT interpret domain
//! commands" (thin-client axiom, DOC-39 §2.1 C-1/C-2). This module is where
//! that split is decided. It replaces tagent's monolithic
//! `try_handle_slash`/`SLASH_COMMANDS` (`crates/trusty-agents/src/repl/*.rs`,
//! `crates/trusty-agents/src/repl/tui/helpers.rs:156-181`), which mixed
//! generic commands with tagent-specific ones and carried no routing
//! metadata at all.
//!
//! What: three pieces.
//! - [`route`] — pure, engine-free: given a submitted line, decide
//! [`Route::BuiltIn`] or [`Route::Forward`]. Only needs the fixed
//! [`built_in_commands`] table, so [`crate::app::ReplApp::submit_line`]
//! (which has no engine handle — see `crate::app::reduce`'s module doc
//! comment) can call it directly and apply a built-in's effect inline
//! ([`Route::BuiltIn(BuiltIn::Clear)`](BuiltIn::Clear) reuses
//! [`crate::app::ReplApp::clear_scrollback`], the same effect
//! `ReplEvent::ClearScrollback` produces).
//! - [`resolve_forward`]/[`compose_selection`] — the picker half (DOC-50
//! §3.2/Q6): a bare `/name` (no args) `Route::Forward` line whose name
//! matches `TuiEngine::picker(name)` opens an inline picker instead of
//! reaching `handle_input`; confirming a picker item resubmits
//! `"{dispatch_command} {selected.id}"`. These DO need engine access, so
//! they're free functions a product's async event-loop wiring calls
//! (rather than living inside the sync [`crate::app::reduce::apply`](crate::app::apply)
//! reducer, which cannot reach a `TuiEngine` — see that module's doc
//! comment).
//! - [`dispatch_forward`] — composes the two into the actual integration
//! point: given a `Route::Forward` line staged in
//! [`crate::app::ReplApp::pending_submit`], either opens a picker or calls
//! `engine.handle_input`.
//!
//! # Spec References
//! - [`SPEC-TTUI-05~draft`](docs/specs/DOC-50-tcode-tui-claude-code-clone.md#SPEC-TTUI-05~draft) — Slice 7 deliverable (§5, Slice 7) and Q4 (mixed routing), Q6 (inline pickers).
use UnboundedSender;
use crateReplApp;
use crateTuiEngine;
use crateReplEvent;
use crate;
/// The fixed, client-side built-in command table (DOC-50 §6 Q4) — never
/// forwarded to `TuiEngine::handle_input`.
///
/// Why: a plain function (not a `const`) so each call returns an owned
/// `Vec` — cheap, and avoids a `static`/`OnceLock` for four short-lived
/// rows. `/quit` and `/exit` are both listed (two independent entries, not
/// one row with an alias note) so `/help`'s output and prefix-based
/// discovery treat them identically; [`route`] recognizes both names as
/// [`BuiltIn::Quit`].
/// What: `name` matches [`route`]'s match arms exactly; `summary` is what
/// [`render_help`] prints. None of these carry `args_hint` — all four take
/// no arguments.
/// Test: `tests::built_in_commands_names_match_route_recognition`.
/// The effect of a recognized built-in command — applied directly by
/// [`crate::app::ReplApp::submit_line`], never round-tripped through
/// `TuiEngine`.
/// Where a submitted line should route (DOC-50 §6 Q4).
/// Split a trimmed `/`-prefixed line into its command name (no leading `/`)
/// and trimmed argument tail (`""` when bare). `None` when `line` isn't a
/// slash command at all (doesn't start with `/`, or is exactly `/`).
/// Classify one submitted line per DOC-50 §6 Q4's mixed-routing model.
///
/// Why: pure and engine-free (only consults the fixed [`built_in_commands`]
/// table) so [`crate::app::ReplApp::submit_line`] — which has no
/// `TuiEngine` handle — can call it directly. See the module doc comment
/// for why built-in recognition doesn't need `TuiEngine::commands()` at all:
/// the built-in set is fixed and product-independent.
/// What: non-slash lines (plain chat) and any `/name` not in
/// [`built_in_commands`] both resolve to `Route::Forward` — see that
/// variant's doc comment for why "unrecognized" and "domain command" are
/// treated identically.
/// Test: `tests::route_recognizes_each_builtin_name`,
/// `tests::route_forwards_plain_chat`,
/// `tests::route_forwards_unrecognized_slash_command`,
/// `tests::route_forwards_engine_domain_command`.
/// Render `/help`'s output: the built-ins plus every engine-supplied
/// command, one line each.
///
/// Why: DOC-50 §5 Slice 7 requires `/help` to enumerate both sides of the
/// routing split — `engine_commands` is expected to be
/// [`crate::app::ReplApp::commands`] (the cache a product populates from
/// `TuiEngine::commands()` during setup; see that field's doc comment).
/// What: one `" /{name}{ args_hint} — {summary}"` line per command,
/// built-ins first. Pure string formatting — no ratatui dependency, so this
/// stays usable from both the plain-text scrollback path and a future
/// richer help widget.
/// Test: `tests::render_help_lists_builtins_and_engine_commands`,
/// `tests::render_help_includes_args_hint_when_present`.
/// `true` when `line` is a bare `/name` invocation (no arguments) —
/// convention for picker-eligible commands (DOC-50 §3.2
/// `TuiEngine::picker`'s doc comment: "a command's name doubles as its
/// picker name").
/// What the caller should do with a `Route::Forward` line.
/// Resolve a `Route::Forward` line against `engine`: does it open a picker,
/// or does it genuinely need `handle_input`?
///
/// Why: split out of [`dispatch_forward`] so the picker decision itself
/// (needs `&dyn TuiEngine`, but no `tx`/`ReplApp` mutation) is unit-testable
/// against a bare mock engine.
/// What: only a *bare* command (see [`bare_command_name`]) is picker-
/// eligible — `/model opus-4` (already has an argument) always forwards,
/// even if `engine.picker("model")` would return `Some`, matching tagent's
/// precedent (`crates/trusty-agents/src/repl/bridge.rs`: the picker overlay
/// only opens for the no-arg form; a supplied argument is assumed to be the
/// user directly naming their choice).
/// Test: `tests::resolve_forward_opens_picker_for_bare_matching_command`,
/// `tests::resolve_forward_ignores_picker_when_args_present`,
/// `tests::resolve_forward_falls_back_to_engine_when_no_picker_matches`.
/// Compose the line to resubmit when the user confirms `selected` from
/// `request` (DOC-50 §3.2 `PickerRequest` contract:
/// `"{dispatch_command} {selected.id}"`).
///
/// Why: a free function (not a `PickerRequest` method — that type lives in
/// `crate::model`, which must stay free of any TUI-driver-shaped API) so
/// both this module's [`dispatch_forward`]-adjacent flow and a future
/// picker *widget*'s Enter-key handling share one composition rule.
/// Test: `tests::compose_selection_matches_picker_request_contract`.
/// The `Route::Forward` half of the dispatch pipeline: resolve a picker or
/// call `engine.handle_input`.
///
/// Why: the actual integration point a product's event-loop wiring calls
/// after draining a `Route::Forward` line from
/// [`crate::app::ReplApp::pending_submit`] — composes [`resolve_forward`]
/// (the routing decision) with the two possible effects (open a picker on
/// `app`, or await the engine call) in one place, so callers don't have to
/// re-derive the "which one am I supposed to do" branch themselves.
/// What: on [`Forward::OpenPicker`], stages the picker on `app` (via
/// [`crate::app::ReplApp::open_picker`]), clears [`crate::app::ReplApp::busy`],
/// and returns `Ok(true)` — the engine is never touched. Clearing `busy`
/// here (not in [`crate::app::ReplApp::submit_line`]) matters: `submit_line`
/// unconditionally sets `busy = true` for every `Route::Forward` line
/// because it can't know in advance whether this function will end up
/// opening a picker instead of actually calling `handle_input` — this arm
/// is the one place that knows the round-trip to the engine never
/// happened. Leaving `busy` set would strand the input composer's busy
/// spinner for as long as the picker stays open (nothing else clears it —
/// `busy` is only ever reset by an `AssistantOutput` event, which never
/// arrives on this path) and would spuriously gate `apply_up`'s
/// busy-triggered `pending_cancel` (`crate::app::reduce::apply_up`), firing
/// an unwanted `engine.cancel_session()` the next time a picker-open Up
/// arrow is drained. On [`Forward::ToEngine`], forwards verbatim to
/// `engine.handle_input`, returning its `Result<bool>` unchanged (per
/// `TuiEngine::handle_input`'s own contract: `Ok(false)` means quit) —
/// `busy` is left as `submit_line` set it, since the engine call is the
/// thing `busy` is meant to track.
/// Test: `tests::dispatch_forward_opens_picker_without_calling_engine`,
/// `tests::dispatch_forward_calls_engine_when_no_picker_matches`,
/// `tests::dispatch_forward_clears_busy_set_by_submit_line_when_opening_picker`.
pub async