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
//! UX-29 dev/01 — soft-interrupt / steer mid-turn without killing it.
//!
//! dev/02 (already landed, `main.rs::race_ctrl_c`) gives Ctrl-C a HARD
//! cancel: it drops the in-flight turn future. dev/01 is a DIFFERENT,
//! softer affordance: while a turn streams, the user can type a line and
//! press Enter to QUEUE a steering message, which is delivered as the next
//! turn's input once the CURRENT turn finishes normally — the turn itself
//! is never touched, dropped, or truncated.
//!
//! **Why this can't touch termios (and so can't leak raw mode).** dev/02's
//! own doc comment established the invariant this module relies on:
//! outside `rl.readline()`, the terminal is already in normal/cooked mode
//! (`ICANON`+`ISIG` both on) — `readline()` is the only thing in this
//! codebase that ever flips it to raw, and it always restores cooked mode
//! before returning. This module is only ever active DURING an
//! `agent.send()` await (i.e. strictly outside any `readline()` call), so
//! it never needs to touch `termios` at all — there is nothing for it to
//! set, and therefore nothing it could ever leak. `Ctrl-C` staying a real
//! `SIGINT` (which needs `ISIG` enabled) is completely unaffected by this
//! module by construction, not by careful coordination — dev/02's
//! `race_ctrl_c` keeps working exactly as before whether or not this
//! module's watcher is running alongside it.
//!
//! **Why it can't steal the next prompt's keystrokes.** [`watch_loop`]
//! (run on a `spawn_blocking` thread) NEVER calls `read()` unless `poll()`
//! has *just* confirmed a byte is sitting in the kernel's canonical-mode
//! read queue — so it can never block. Every iteration re-checks a stop
//! flag at most [`POLL_TIMEOUT_MS`] later, so a caller can ask it to stop
//! and then `.await` its `JoinHandle` to know — for certain, not just
//! "probably by now" — that it has stopped touching stdin, before ever
//! calling `rl.readline()` again. In canonical mode, `poll(POLLIN)` only
//! ever fires once a *complete* line (or EOF/HUP) is ready — never
//! mid-keystroke — so there's no risk of the watcher grabbing half a line
//! and leaving a corrupt remainder for `readline()` to choke on. It reads
//! ONE byte at a time (not a larger chunk) specifically so it can never
//! over-read past the first completed line into bytes belonging to a
//! SECOND line the user typed in the same window — those stay queued in
//! the kernel, untouched, for the next real reader (`rl.readline()`) to
//! see normally, same as ordinary shell type-ahead.
//!
//! **Scope.** Wired into exactly one call site: the interactive
//! `chat()` REPL loop (`main.rs`). The four one-shot `race_ctrl_c` call
//! sites (`run`, `resume <file> "prompt"`, the resume-picker's one-shot
//! prompt) are untouched — they exit after a single turn, so there is no
//! "next turn" to deliver a queued steer into, and mixing this into them
//! would risk dev/02's already-hardened one-shot exit-130 paths for no
//! benefit. TTY-gating ([`capture_available`]) additionally keeps this
//! entirely inert for non-interactive/piped/machine-format runs, which
//! never call `chat()` at all (see `Command::Chat` — always
//! `OutputFormat`-agnostic, always interactive) but are double-gated here
//! too, belt-and-suspenders, matching `hidden_input.rs`/`picker.rs`'s own
//! pattern.
//!
//! **Windows (UX-12): degraded, not ported.** [`watch_loop`]'s Unix body is
//! `libc::poll(POLLIN)` + a single-byte non-blocking `read(2)` — the same
//! "confirm a byte is ready before ever calling read" primitive
//! `picker.rs` degrades for the same reason (see its module doc): a
//! Windows console handle's readiness signal does not guarantee a
//! subsequent read won't block (it can fire on a single keystroke, then a
//! line-mode `ReadFile` blocks for the rest of the line) — porting this
//! safely needs `ReadConsoleInput`/`PeekConsoleInput`-based key-event
//! parsing, a materially different implementation, not a small cfg-gated
//! swap. Per UX-12's guidance this degrades: [`capture_available`] reports
//! `false` unconditionally on Windows, so [`spawn`] is never actually
//! called (its one caller, `main.rs::race_ctrl_c_with_steer`, always takes
//! the `!steer_enabled` early-return instead) — soft-interrupt steering is
//! simply off. Ctrl-C hard-cancel (`race_ctrl_c`, a wholly separate code
//! path) is completely unaffected; `chat` remains fully usable, just
//! without the mid-turn steer affordance.
use ;
use Arc;
/// How often [`watch_loop`] wakes to re-check whether it's been asked to
/// stop. Bounds the extra latency joining the watcher can add to
/// returning to the prompt once a turn finishes (worst case: one tick).
const POLL_TIMEOUT_MS: i32 = 100;
/// A captured line is capped at this many bytes (before its terminating
/// newline) so a runaway paste with no newline can't grow this thread's
/// buffer unboundedly. The captured prefix is delivered as-is; excess
/// bytes are simply never appended (not consumed from the fd twice, not
/// lost from the kernel's queue — they were already read to get here).
const MAX_LINE_BYTES: usize = 8192;
/// The pure yes/no soft-steer capture gates on: real TTYs on both the
/// input and the chrome-drawing sides — the same bar `chat()`'s own
/// `interactive` flag and `race_ctrl_c`'s callers already use. Factored
/// out so it's unit-testable without an actual terminal.
/// Windows: always `false` — see the module doc's "Windows" section. Takes
/// the same signature as the Unix version (parameters unused) so callers
/// don't need their own `cfg` branches.
/// Spawn a background watcher for ONE in-flight turn. Returns a stop flag
/// and the `JoinHandle` to race the turn against; callers own both:
/// `stop.store(true, ..)` to ask it to exit, then `.await` the handle
/// (regardless of who asked it to stop, or whether it already resolved on
/// its own) to know for certain it is no longer touching stdin before the
/// next `rl.readline()` call.
///
/// Never touches `termios` — see the module doc for why that's safe.
/// Runs on a `spawn_blocking` thread. Polls stdin for a single queued
/// steering line; see the module doc for the no-block / no-over-read
/// safety argument. Returns `None` if asked to stop before a line arrived,
/// or on EOF/HUP (e.g. Ctrl-D pressed on an empty line mid-turn — no
/// steering line was typed, so there's nothing to queue).
/// Windows stub — see module doc "Windows" section. Unreachable at
/// runtime: this function's only caller, [`spawn`], is only invoked from
/// `main.rs::race_ctrl_c_with_steer` when `capture_available()` is `true`,
/// which never happens on Windows. Kept (rather than `unreachable!()`) so
/// a hypothetical future direct call fails safe — "no line captured" —
/// instead of panicking.