Skip to content

fix(repl): the submit writes the probe-parked typeahead back to the console queue (niubash#417) - #433

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/417-typeahead-writeback
Oct 19, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/417-typeahead-writeback

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Closes #417. Follows #420/#426 (the unread re-injection face) and #284/#308/#341/#373/#418 (the probe's submit semantics this builds on). Open for review — not merging.

The wedge

The Enter signature probe reads the kernel console queue's next record to decide whether a bare Enter is paste data or a typed submit. With typeahead behind it (the next line of a fast burst), that record is not paste evidence — so it was parked in the in-process pushback while the Enter submitted. The accepted command's engine read then reads the kernel queue next, which cannot see the in-process deque: with nothing else queued, read -n1 blocks forever and the parked keystrokes are unreachable until the next prompt.

#426 landed the unread re-injection face (WriteConsoleInputW, typeahead_guard.rs) for #420's partner CR. This PR completes the chain for the parked typeahead.

The fix

  1. Submit-time writeback (PasteChunkEditMode::parse_event): a batch ending in an Enter is the boundary where reedline stops reading and a command may read — whatever the probe parked goes back to the input stream's head at that point, via the existing Unread face. Batches that do not end in a submit keep the park (the next parse_event drains it FIFO; nothing left the read loop in between). A validator-held Enter (PS2) re-reads the records through the same queue — same order, no observable change.
  2. The face grew to carry the pushback's shapes (key_press_records, pure + unit-tested off-Windows): any key Press the console parser can produce, synthesized the way conhost synthesizes it — control code in uChar + base letter in the VK for chords, VKs inverse to crossterm 0.29's parse table for special keys, one record per UTF-16 unit for non-BMP chars — so reedline's parser and the engine's console reader decode back the exact event that was consumed.
  3. Head insertion (typeahead_guard::requeue_console_input): WriteConsoleInputW appends at the tail, but the probed events sat AHEAD of everything still queued — a tail append would let line 2's ead... run before the parked r of read and shred the burst. The pending records are drained first and rewritten behind the re-queued ones; the queue reads the exact order the probe found. interactive shared-driver wedge: session echoes the input line but the submitted command never executes (reproducible with a Ctrl+R flow driver; 5 baseline failures share the signature) #420's partner-CR requeue shares the face and inherits the same guarantee.

The pushback still never holds a bare CR (#341): the probe pushes only non-Enter keys, and an Enter batch's park is exactly that probe's own pushes, so the writeback can never inject a CR across a batch boundary.

New witnesses

  • Execution-level, echo-proof (tests/interactive.rs, interactive shared-driver wedge: session echoes the input line but the submitted command never executes (reproducible with a Ctrl+R flow driver; 5 baseline failures share the signature) #420's two test forms — both verified RED on unpatched origin/master (the read blocks on the emptied queue), GREEN here):
    • probe_parked_typeahead_reaches_engine_read_417 — one write read -n1 R417; echo "R417=[$R417]"\rZ: the probe parks Z, the accepted command's read must get it.
    • typeahead_burst_read_line_gets_parked_key_417 — three back-to-back lines in one write; line 2's submit probe parks line 3's W, and the head writeback must not let line 2 run before it (a tail append fails this).
  • Detector-level: the submit requeues the parked key at the stream's head AHEAD of still-queued events (head-vs-tail pinned, paste_chunk_detector_submit_requeues_parked_typeahead_at_stream_head_417); the two former park-in-pushback assertions re-pinned to the requeue contract; the record-synthesis mapping unit tests (chars, chords, special keys, BackTab, surrogate pairs, releases/unparseable shapes).

不变式自查(BLOCKER 检查)

# 不变式 状态 见证
1 pushback 永不持有裸 CR(#341) ✅ 保持 探针仍只 push 非 Enter 键;提交批次的 parked 集合=探针自身 push(无 CR);paste_chunk_detector_pushback_never_holds_a_bare_cr_341 重钉(提交后 pushback 空、键回流队头);写回面不可能注入裸 CR
2 粘贴合同 24/24(#252/#288/#309/#341/#411 家族) ✅ 全绿 探测器层 paste_chunk 全家 21+5 通过;interactive 83/0(含 WT owner script 341、CRLF scale、#308 burst、#284 typeahead matrix)
3 #418 DSR/TAB 窗与两批次提交语义 ✅ 不回退 issue373 3/0;paste_chunk_wrapper_dismissal_esc_rides_only_the_tab_window、Ctrl+R accept+submit burst(420 形态)全绿;包裹提交批次 [Esc, Enter] 不受影响(parked 集合为空)
4 #420 修复语义(chunk-open 裸 CR 不算粘贴证据、伙伴 CR 经 unread 回流) ✅ 不回退 typeahead_bare_cr_pair_submits_per_line_420、ctrl_r_accept_submit_in_one_burst_420、paste_chunk_detector_open_cr_pair_submits_and_requeues_420 全绿;伙伴 CR 仍走 unread,且新面升级为队头插入(顺序更严格)

Verification(本机 Win10 19045,--test-threads=1)

  • runtime lib: 558 passed / 0 failed(门槛 552+)
  • interactive: 83 passed / 0 failed(5 ignored,unix-only leg);issue373 3/0;completion_probe 29/0;repl_command 11/0
  • cargo fmt --check clean;RUSTFLAGS="-D warnings" cargo check clean;clippy --all-targets 对比 origin/master:无新增(77 → 76,重写消掉一个既有 unnecessary cast)

…onsole queue (niubash#417)

The Enter signature probe reads the kernel queue's next record to decide a
bare Enter. With typeahead behind it — the next line of a fast burst — that
record is not paste evidence, so it was parked in the IN-PROCESS pushback
while the Enter submitted. The accepted command's engine `read` then reads
the KERNEL queue next, which cannot see the in-process deque: with nothing
else queued the read blocks forever, and the parked keystrokes are
unreachable until the next prompt (the #417 wedge). #426 landed the unread
re-injection face (WriteConsoleInputW) for #420's partner CR; this completes
the chain for the parked typeahead.

Parse_event now flushes the pushback to the input stream's head whenever a
batch ends in an Enter — the boundary where reedline stops reading and a
command may read. Batches that do not end in a submit keep the park (the
next parse_event drains it FIFO, nothing left the read loop). The face
grew to carry the shapes the pushback can hold, not just a bare Enter:

- `win_unread` synthesizes KEY_EVENT records for any key Press the console
  parser can produce, mirroring conhost's own conventions (control code in
  uChar + base letter in the VK for chords, VKs inverse to crossterm 0.29's
  parse table for special keys, one record per UTF-16 unit for non-BMP
  chars) so reedline's parser and the engine's console reader decode back
  the exact event that was consumed. Pure mapping, unit-tested off-Windows.
- `typeahead_guard::requeue_console_input` inserts at the queue's HEAD:
  WriteConsoleInputW appends at the tail, but the probed events sat ahead
  of everything still queued — a tail append would let line 2's 'ead...'
  run before the parked 'r' of 'read' and shred the burst. The pending
  records are drained first and rewritten behind the re-queued ones, so
  the queue reads the exact order the probe found. #420's partner-CR
  requeue shares the face and inherits the same order guarantee.

The pushback still never holds a bare CR (#341): the probe pushes only
non-Enter keys, and an Enter batch's park is exactly that probe's own
pushes, so the writeback can never inject a CR across a batch boundary.

New witnesses:
- probe_parked_typeahead_reaches_engine_read_417 and
  typeahead_burst_read_line_gets_parked_key_417 (tests/interactive.rs,
  echo-proof execution level, #420's two test forms): both RED on
  unpatched master (read blocks on the emptied queue) and GREEN here.
- Detector-level: the submit requeues the parked key at the stream's head
  ahead of still-queued events (head-vs-tail pinned), the two former
  park-in-pushback assertions re-pinned to the requeue contract, and the
  record-synthesis mapping unit tests (chars, chords, special keys,
  surrogate pairs, releases/unparseable shapes).

Verification (this host, Win10 19045, --test-threads=1): runtime lib 558
passed / 0 failed; interactive 83 passed / 0 failed (5 ignored, unix-only
legs); issue373 3, completion_probe 29, repl_command 11; cargo fmt --check
clean; RUSTFLAGS=-D warnings cargo check clean; clippy --all-targets vs
origin/master: no new warnings (77 -> 76, the rewrite deletes one
unnecessary cast). No GPL code introduced.
@caomengxuan666

Copy link
Copy Markdown
Member Author

CR verdict: APPROVE-WITH-NITS (no blockers; 5 non-blocking notes below)

Independent review of 26ef3d3 vs base 7b4ee73 (fresh detached worktree, not the author's tree; every suite re-run locally on Win10 19045; clippy/fmt re-measured, not quoted). The three claims I most doubted — head-vs-tail requeue necessity, bare-CR impossibility through the new writeback face, and the RED state of the new tests — were each re-derived from scratch with my own probes, not the PR's.

Verified independently

Non-blocking notes

  1. Dead control-chord arm + doc overclaim (the one worth a follow-up): ('\u{1}'..='\u{1a}').contains(&ch) never matches on the Windows path — crossterm 0.29 decodes uChar 0x00..=0x1F through get_char_for_key (ToUnicodeEx over the VK), so a real Ctrl+R parks as Char('r')+CONTROL, not Char('\u{12}')+CONTROL. The arm's comment and the key_records_synthesize_control_chords docstring claim the parser "re-derives the same event" through that arm; it actually re-derives the letter. Consequence: a parked ctrl-letter chord takes the printable arm and re-injects uChar = the letter, so a byte-level reader of the next command's input sees 'r' where the original keystroke's record carried 0x12 (event-level round-trip via crossterm readers is still exact). Pre-PR that key was lost entirely, so this is still strictly better — but synthesizing the conhost convention (uChar = control code) for CONTROL+letter would be both event-exact and byte-exact.
  2. Drain/rewrite tear window: a keystroke landing between the final GetNumberOfConsoleInputEvents == 0 and the first WriteConsoleInputW is prepended ahead of records (typed last, runs first). No atomic prepend exists on Win32; window is two adjacent syscalls and best-effort is documented. Acceptable.
  3. written ignored: a partially-successful WriteConsoleInputW (input buffer full) silently drops the unwritten tail. After a full drain this needs a >buffer-capacity burst arriving inside the window. Note only.
  4. Pre-existing tear class, not introduced: a straggler sweeper blocked in ReadConsoleInputW from the previous arm cycle can self-heal-reinject a record at the tail after the rewrite, putting an older record behind newer ones. That interleaving exists on master (the [P3] TypeaheadGuard 自愈与 disarm 双重注入同一条输入记录(WriteConsoleInputW 重复写回) #287 self-heal path); this PR neither widens nor narrows it materially.
  5. Nearly-dead comment branch: "batches that do NOT end in a submit keep their park" — parks only ever occur while translating a CR, whose translation is a same-batch trailing Enter, so in practice every park rides the writeback immediately. Harmless; comment could say so.

No blockers. APPROVE-WITH-NITS — recommend a follow-up for note 1; nothing here needs to gate the merge.

Conflict resolution (crates/niubash-runtime/src/repl.rs, two hunks):

1. Unread type doc — both sides kept: #432's MSDN tail-append framing is
   the primitive's truth (WriteConsoleInput APPENDS, it cannot prepend);
   the merged text states that #417's face wraps that primitive with a
   drain-and-rewrite (requeue_console_input) so the re-queued events land
   at the head slot, the position the probe took them from.

2. win_unread record construction — the #417 general synthesis kept
   (fields.*); #432's clippy-cast deletion survives inside it (the
   '0x0D as u16' cast no longer exists anywhere — UnicodeChar comes from
   a u16 field; the necessary char casts live in key_press_records).

Stale #432 derivations of the old tail-append behavior updated to the
compensated face (comments still cite the MSDN append as the primitive):
the unread field doc (tail-write claim -> drain-and-rewrite, and why the
#417 writeback needs the head slot: a plain append would shred a burst
into 'ead...'), with_scripted_queue (head slot is now a production
guarantee, not a test-only fidelity upgrade), enter_opens_chunk (the
requeued partner CR is the next submit nothing has to drain past), and
typeahead_guard::requeue_console_input's doc (same primitive as the
guard's reinjection; the guard can settle for the bare append — its
queue is empty at reinjection — this face buys the head slot).

Verification (this host, Win10 19045, --test-threads=1) on the merge
result: runtime lib 560/0; interactive 85/0 (5 ignored, unix-only legs;
includes #432's two new tests and both #417 echo-proof regressions);
issue373 3/0; cargo fmt --check clean; RUSTFLAGS='-D warnings' cargo
check clean. niubash#434 (control-chord synthesis arm) intentionally NOT
touched here — separate ticket.
@caomengxuan666
caomengxuan666 merged commit b032a66 into master Oct 19, 2026
9 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/417-typeahead-writeback branch October 21, 2026 08:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant