Skip to content

fix(repl): a WT KEY_EVENT paste's empty line can never park a bare CR in the pushback (closes #341) - #411

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/341-wt-keyevent-paste
Oct 18, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/341-wt-keyevent-paste

Conversation

@caomengxuan666

@caomengxuan666 caomengxuan666 commented Oct 18, 2026 •

Copy link
Copy Markdown
Member

Closes #341.

根因(机制级)

四轮修复(#252/#288/#309/#317)全部建模的是 ConPTY 文本喂入形态:每个换行是 CRLF 对,CR 的 LF partner(Enter + CONTROL)就在队列里,#288 的结构探针能找到它。而真机 Windows Terminal 粘贴从未携带这个签名:WT 把粘贴合成为逐字符 KEY_EVENT 记录,每个换行是一枚裸 CR,空行是连续两枚 CR(\r\r)——LF 永远不存在。

在这个形态下,空行的第二枚 CR 被探针读到,被判成"typed submit 证据"(它不是 LF),推入了 pushback。这枚 CR 跨越了 reedline 的批次边界:

  1. 下一批次,reedline 先把 pushback 转换掉(0.50.0 engine.rs process_input_batch,R2)——CR 变成批次首位的 ReedlineEvent::Enter;
  2. 派发循环在首个 Exits(Enter 提交空 buffer)处直接 return(engine.rs:1246-1258,R3),丢弃同批次里排在 Enter 之后的全部文本 Edit——这些 raw 事件已被 R1 从控制台队列消费,不可恢复;
  3. 下一行的行首 N 字符消失:if is_even 10; then 执行成 _even 10; then。只有空行能把 CR 送进 pushback(非空行的探针退回的是字符,是无害的 Edit)——正对上"两处丢失的上一行都是空行";丢失长度=批次内已排队前缀(不定长)——正对上 5-6/3 的观测。

#309 的 pushed-back-Enter 测试钉住的是"单 raw 批次"(Enter 后无同批事件可丢);owner 的是多 raw 批次,恰好踩进 R3。

修复

结构探针扩展(cr_has_lf_partner → cr_paste_partner):裸 CR 后面排着另一枚裸 CR = WT 空行 = 粘贴数据,随折叠一并消费,永不 pushback。由此建立不变式——pushback 里不可能出现裸 CR,批次首位的 Enter 无从产生。四条既有语义原样保留:静默 CR = typed submit(#284)、块内 CRLF 对 = 数据、burst 后独立 Enter 提交、#308 尾随 Enter flush。CRLF 形态的全部既有回归(#252/#288/#309/#317)不动一行语义,全绿。

复现通道(四轮打不通的那一环)

  • 复现载体(确定性 RED/GREEN)= 协议仿真器:repl.rs 里新增的 reedline R1/R2/R3 批次协议仿真单测(wt_bare_cr_paste_survives_reedline_batch_protocol_341 等),喂 }\r\rif is_even 10; then\r... 的 WT KEY_EVENT 形态流并按 ConPTY 写切片切开——打到未修补 6332f99 上确定性丢 if is_(提交 _even 10; then,即 owner 症状),本 PR 修后逐字节完整,且对每个切片切点/双切点组合成立。端口可复现、与终端通道无关。
  • 记录层取证(工具已随 PR 入库:tests/keyevent_record_probe.rs,默认 #[ignore],cargo test --test keyevent_record_probe -- --ignored 运行):ReadConsoleInputW 探针确认,ConPTY 对 \r 字节合成的正是 VK_RETURN/NONE 的裸 CR 记录(press+release),无任何 LF/CONTROL 记录;\r\n 则额外合成 VK_CONTROL 夹心 + Enter+CONTROL partner(repl: PSReadLine-style paste-chunk detection for the Windows console path #252 建模的形态);win32-input-mode VT 序列在本机 conhost 不被解码。因此 bare-CR 字节喂入 = WT KEY_EVENT 形态。
  • 集成测试是回归守卫、不是复现(CR 复核并确认,正文原表述已纠正):owner 76 行脚本经 bare-CR 形态整块注入的集成测试在未修补 master 上同样通过——活通道的单次大写 + TypeaheadGuard 每轮 prompt 重建的清扫/重注入不会复现探针-pushback 跨批次交错的时序。它的职责是钉住 KEY_EVENT 形态下全脚本端到端可执行、两个 if is_even 分支输出(owner 被粉碎的两行)持续在场;确定性复现由上面的协议仿真器承担。
  • 协议级确定性复现:新单测用 R1/R2/R3 批次协议仿真器喂 }\r\rif is_even 10; then\r...——修复前确定性丢 if is_(提交 _even 10; then,即 owner 症状),修复后逐字节完整,且对每一个切片切点都成立。
  • 集成级:owner 76 行脚本以 bare-CR 形态整块注入,两个 if is_even 分支输出(10 是偶数,函数返回成功 / 7 不是偶数,函数返回失败)作为执行级见证必须全部出现;与 GNU bash bash script.sh 输出对照一致(除 $RANDOM 段与一条既有的非交互引擎 quirk)。

测试

  • cargo test -p niubash-runtime --lib:540+1 通过——+1 为本地 rubash 路径预存环境失败项,与本 PR 无关(另一环境 541/541 全过);含 25 个 paste 单测,其中 6 个新 [P1][blocks 1.4.1] interactive paste still swallows line starts in WT on the current build — full script reproduction (owner live session 2026-10-14) #341 回归——修复前 RED(复现载体即上述仿真器)。
  • cargo test --test interactive(--test-threads=1):新增 2 例全绿;全部既有 paste 回归绿。本机另有 5 例失败,已在未打补丁的 master 上逐一复核为预存环境失败(4 例确定性同签名 + ctrl_d_after_background_job_exits 在 master 上同样失败),与本改动无关。
  • 附带发现(独立引擎 bug,未在本 PR 处理):declare -A name=(...) 走行编辑器路径时,debug 构建子进程在 rubash locale.rs 的 COMPOUND_ASSIGNMENT_MARKER debug_assert 处 panic——3 行纯键入(无粘贴)即可在未修补 master 上复现;release 构建(owner 环境)不受影响。集成测试里的脚本第 3 段因此改写为输出等价的 declare-then-assign 形式,已在测试注释中说明。

… in the pushback (closes #341)

Four rounds of paste fixes (#252/#288/#309/#317) modeled the ConPTY
text-feed shape, where every fed line break is a CRLF pair: the CR's LF
partner (Enter + CONTROL) sits in the queue and the structural probe
(niubash#284) finds it. A real Windows Terminal paste never carries that
signature: the terminal synthesizes a KEY_EVENT record per pasted
character, every line break is a lone bare CR, and an empty line is two
consecutive CRs - no LF ever exists.

In that shape the empty line's second CR was read by the probe, judged
'typed submit evidence' (it is not an LF), and pushed back into the
detector's pushback. There it crossed reedline's batch boundary: the
next batch converts the pushback first (reedline 0.50.0 engine.rs
process_input_batch, R2), so the CR became a batch-leading
ReedlineEvent::Enter; reedline's dispatch returns at the first Exits
(engine.rs:1246-1258, R3) and drops the very text events it queued after
the Enter in that same batch - raw events already consumed from the
console queue. The next line's first bytes vanished: 'if is_even 10;
then' executed as '_even 10; then', always right after an empty line,
exactly the owner's two corruption sites. The pushed-back-Enter test of
#309 pinned the single-raw batch where nothing trails the Enter; the
owner's case is the multi-raw batch.

The fix keeps every #284/#308 semantic (typed submit on silence, in-block
CRLF pairs as data, burst-end and trailing-Enter submits) and extends the
structural probe: a bare CR queued behind a bare CR is the KEY_EVENT
empty line - paste data, consumed by the fold, never pushed back. The
pushback can no longer hold a bare CR, so a batch-leading Enter from the
pushback is impossible.

Tests:
- protocol-level repro: a reedline R1/R2/R3 batch simulator feeds the
  WT-shaped stream ('}\r\rif is_even 10; then\r...') sliced the way
  ConPTY's write slices cut a paste; pre-fix it loses 'if is_' and
  submits '_even 10; then' (the owner symptom, deterministic); post-fix
  the reassembly is byte-exact, at every slice cut.
- integration: the owner's 76-line script over the bare-CR KEY_EVENT
  channel runs in full on a single submit stream, with the two 'if
  is_even' branch outputs as the corruption witnesses; a bare-CR single
  line still submits (driver send_line / fast typist).
- all CRLF paste regressions (#252/#288/#309/#317) stay green.

Channel note: the ConPTY harness reproduces the WT KEY_EVENT record
shape byte for byte with a bare-CR feed (probe-verified against
ReadConsoleInputW: '\r' bytes synthesize VK_RETURN/NONE records only;
win32-input-mode VT sequences are NOT decoded by this host's conhost).
The script's multiline 'declare -A capital=(...)' is rewritten to the
output-identical declare-then-assign form because any 'declare -A
name=(...)' through the line-editor path trips rubash locale.rs's
COMPOUND_ASSIGNMENT_MARKER debug_assert in a debug-build child - a
pre-existing engine bug reproduced on unpatched master with a 3-line
typed (non-paste) input; release builds (what the owner runs) strip
debug_assert.

@caomengxuan666 caomengxuan666 left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

独立评审(APPROVE-WITH-NITS)

作者视角不可信前提下做了全量独立复核:checkout 1a51471 实跑两套件、对 reedline 0.50.0 registry 源码逐条核对仿真器语义、把本 PR 的纯测试 hunks 打到未修补 master(6332f99 worktree、全新构建)做 RED/GREEN 判别。机制级根因主张:证实;修复实现正确;未发现代码缺陷。以下 findings 全部是证据表述与测试注释层面的问题,建议合并前修正(不阻塞代码)。

独立验证记录

  • cargo test -p niubash-runtime --lib:540 通过 + 1 失败(见 F2);paste 23/23、wt_ 5/5 全绿。
  • cargo test --test interactive -- --test-threads=1:53 通过含两个新 #341 测试;5 失败中 4 例在 master worktree 复现(continuation_single_quote_ps2_and_completes、continuation_subshell_paren_completes、ctrl_d_after_background_job_exits、gitbash_factory_prompt_renders_and_tracks_the_branch)=预存环境失败,与 PR 声称一致;ctrl_c_interrupts_external_command 仅在全量套件负载下失败,两分支单跑各两遍均绿=抖动,非本 PR 回退。
  • RED 判别(关键):把 PR 的两个 repl.rs 测试 hunks + interactive.rs 新增 hunk 单独 apply 到 6332f99 worktree(修复代码不在场),4 个新 lib 测试全部 FAILED 且失败签名正是字节丢失(slice cut at event 0 lost or altered bytes、the WT KEY_EVENT paste lost or altered bytes across the batch boundary、旧 pushback 行为下 matches!(parse_event(cr()), ReedlineEvent::None) 不成立)。PR head 上同 4 测试全绿。协议级 RED→GREEN 成立。
  • 仿真器保真度:对照 registry 源 ~/.cargo/registry/src/*/reedline-0.50.0/src/engine.rs 逐条核对——R1 completed()(batch 绝不跨裸 Enter,engine.rs:1052-1067)、R2 process_input_batch 先整体转换后派发(相邻 Edit 融合不改变顺序)、R3 首 Exits 即 Break 丢余部、Multiple 臂同样首-Exits 即 return、Enter→validator Complete→submit_buffer。仿真器在这些语义上忠实。
  • 不变式健全性(独立推演):R1 保证批次内 Enter 之后不可能有同批文本 ⇒ “批次首位 Enter”只能来自 pushback 跨批;parse_event 入口(repl.rs:857)虽也 push raw,但 drain 保证到达的 CR 总在当次调用内翻译(入口 pushback 至多持 1 枚探针退回键,且修复后探针绝不再退回 CR,repl.rs:773-781)⇒ “pushback 永不持有裸 CR”结构成立,批次首位 Enter 无从产生。
  • cargo fmt(rustfmt --check,edition 2021)对 repl.rs 与 interactive.rs 均干净;改动面确认为仅此两文件,无夹带。

Findings

F1(应修正,表述性)— 集成测试不是 #341 的复现见证,PR 正文与测试注释的相应主张被实测证伪。
把 wt_paste_bare_cr_key_event_shape_runs_owner_script_341(tests/interactive.rs:1135)与 wt_paste_bare_cr_single_line_still_submits_341 打到未修补 master 上实跑:两例在 master 上直接通过(全新 debug 构建、--test-threads=1、顺序 expect 到 ===== 测试结束 =====,两个 if 分支见证全部出现)。即该 ConPTY 通道在本机不区分修复前后——正文“bare-CR 字节喂入 = WT KEY_EVENT 形态,现有 portable-pty 通道即可 CI 化复现”(复现通道节第 1 条的结论句)与 tests/interactive.rs:1120-1123 的“pre-fix, … the branch output never appeared”均与实测矛盾。既然协议级机制已被仿真器 RED/GREEN 坐实,master 集成不丢字节只有两种解释:ConPTY 对喂入 \r 合成的记录并非“裸 CR 相邻”形态(正文的“ReadConsoleInputW 探针确认”结论有误;探针脚本 tests/wt_keyevent_probe.rs 未随 PR 提交,无法独立核对),或 niubash 进程内 TypeaheadGuard 的重注入置换(#341 静态取证候选 5)在探针看到之前打散了 \r\r 相邻性。请二选一:给出记录层原始证据,或把正文与注释改为“集成测试是修复后回归守卫,复现载体是协议仿真器”。四轮未修的票,证据链的准确性本身就是交付物。

F2(表述性)— “541 通过”不可按字面复现。
本机 cargo test -p niubash-runtime --lib = 540 通过 + 1 失败:completion::command::tests::test_get_builtin_commands(command.rs:405 断言 setopt 在 builtin 列表,而路径依赖 D:/repo/rubash 的 builtin_names.rs 已无 setopt/unsetopt)。该模块本 PR 未触碰、失败取决于本地 rubash checkout 状态,属预存环境失败,但 PR 正文的“541 通过”应注明环境前提或改述为“541 中 1 例预存环境失败”。

F3(nit)— \r\r\r 边界无测试钉住。
静态推演安全:三连 CR 中前两枚折叠为 "\n\n"(repl.rs:720-722/980-982),第三枚在 pending=Some 下走 #308 静默提交或字符 pushback 的 flush+submit,字节精确。但 every-cut 扫描(repl.rs:3369)的脚本只含一处 \r\r。建议把脚本换成含 \r\r\r(行尾双空行)的形态,一行改动即可钉住。

F4(nit,已被文档覆盖)— 10ms 探针窗内的双 Enter 语义从“两次提交”变为“折叠为空行数据”。
旧测试 paste_chunk_detector_pushed_back_enter_submits_next_dispatch 钉住的“第一枚提交、第二枚 pushback 下批提交”行为被有意替换为折叠(repl.rs:3889)。人工不可达(探针窗 10ms,键盘自动重复 ≥33ms),单枚裸 CR 的 typed-submit 由 tests/interactive.rs:1176 与 repl.rs:3741/3765 钉住,且正文/注释已明示该取舍——接受,仅记录在案:任何在 10ms 内连发 \r\r 并期望两次提交的字节级驱动(如无延时的 expect 双 send "\r")会看到执行点后移到 burst 末,字节不丢。

F5(nit)— 切片全扫的覆盖形状。
for cut in 0..events.len()(repl.rs:3375)遍历的是“单切点双片”全空间 + 一例三片;更深的切片链未扫。当前机制对切片深度不敏感(不变式与批内序无关),可不改。

结论

机制级根因主张独立证实:\r\r 的第二枚 CR 被探针停进 pushback、跨批次变批次首位 ReedlineEvent::Enter、R3 首-Exits 丢弃同批文本 Edit——协议级确定性复现(RED on master → GREEN on PR)与 reedline 0.50.0 源码核对全部吻合,与独立静态取证的候选 1 互证。修复精确实现不变式,#252/#288/#309/#317 既有语义逐条有测试且全绿,无回退。请处理 F1(改正文与测试注释的证据表述)后合并。

… fold boundaries

CR (APPROVE-WITH-NITS) followups before merge:

- tests/interactive.rs: the owner-script test's doc claimed it was the
  reproduction witness ('pre-fix ... the branch output never appeared').
  That was wrong - the test also passes on unpatched master (CR
  verified; re-verified here 7/7 on master vs a flaky host channel).
  The comment now states the honest scope: regression guard, not
  reproduction; the deterministic RED/GREEN reproduction lives in the
  protocol-simulator unit tests (wt_bare_cr_paste_survives_* in
  repl.rs), which go red on 6332f99 with the exact byte loss.

- tests/keyevent_record_probe.rs (new, ignored by default): the forensic
  tool that established the channel claims, previously scratch-only.
  Executable checks: a fed \r byte synthesizes bare-CR KEY_EVENT records
  only (no LF, no control state); a fed \r\n pair synthesizes the
  VK_CONTROL sandwich plus the Enter+CONTROL LF partner (#252's shape);
  win32-input-mode VT sequences are NOT decoded by this host's conhost
  from the input pipe. Per-case child exe names so the ignored cases
  can run concurrently (LNK1104).

- \r\r\r boundary: a triple-CR run (line break + two empty lines) now
  has its own protocol sweep - the pair fold must neither over- nor
  under-count at every slice cut.

- slice sweep generalization: two independent cut points (three-write
  feed) across every cut pair.

- PR-body accuracy fix tracked separately: the reproduction vehicle is
  the protocol simulator; the integration test is a guard; the runtime
  lib count is stated as 540+1 (the +1 being a pre-existing local
  rubash-path environment failure).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[P1][blocks 1.4.1] interactive paste still swallows line starts in WT on the current build — full script reproduction (owner live session 2026-10-14)

1 participant