Skip to content

fix(repl): paste-chunk detection decides submits structurally, not by timing (#284, #287) - #288

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/284-paste-chunk-enter-swallow
Oct 13, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/284-paste-chunk-enter-swallow

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

Fixes #284 (P1 regression from #252) and #287 in one pass — both live on the Windows console input path.

#284: the 50 ms arrival-burst heuristic classified every bare Enter arriving within one burst as paste data, so "text + Enter" in a single feed lost its submit: fast human typists (last char → Enter < 50 ms), ConPTY drivers' send_line, expect scripts, send-keys — everyone needed a second Enter. 35/47 tests/interactive.rs cases timed out on this.

The fix — structural, not timing-based. A machine-fed line break is a CRLF pair: the CR arrives as a bare Enter, its LF as Enter + CONTROL (the console's Ctrl+J), and reedline's own batch loop (engine.rs read_line_helper) stops every batch at a bare Enter — so the LF partner is always still queued when the CR is dispatched. PasteChunkEditMode now runs a bounded lookahead (PASTE_LF_PROBE, 10 ms) at a bare Enter that would open a chunk:

  • LF partner queued → paste: the pair folds into the pending chunk, the Enter never reaches reedline.
  • Other queued key (typeahead text, a second Enter) or silence → typed submit; the lookahead's read is pushed back so it lands, in order, at the next prompt (GNU typeahead semantics — Multiple can't carry them: reedline drops sub-events after the first Exits).
  • A bare Enter + CONTROL (Ctrl+J) with no chunk pending opens one by itself — humans don't submit with Ctrl+J, and it catches slow pastes whose CR already slipped through.
  • The 50 ms gap keeps exactly one job: the burst boundary where the pending chunk flushes as ONE InsertString. burst_chars is gone.

#287: the TypeaheadGuard sweeper's self-heal path reinjected a borrowed view of saved without draining it, so disarm_and_reinject (which takes the buffer after the sweeper parks) wrote the same records back a second time. The self-heal now drains under the lock.

Verification matrix

Case Result
Multi-line 257-line paste (scripts/perf/paste_multiline_bench.py --payload issue202) elapsed 4.62 s (calibration ~4.6 s), bad_executions_paste 0, 3.7 KB output — no #252 regression
Single line + Enter fast (< 50 ms, one write) submits, rc asserted — send_line_first_enter_submits_and_typeahead_executes + all 35 healed send_line tests
Consecutive single-line commands, no waits (typeahead) each executes in order — back-to-back send_line half of the new test; bare-CR feed stays per-line (multiline_raw_paste_windows_crlf_buffers_and_cr_executes_per_line)
ESC-sequenced arrow keys (#277/#280 adjacency) esc_sequenced_arrow_keys_edit_without_absorption: arrows + insert + submit land; vi matrix green
tests/interactive.rs 5 passed / 35 failed / 7 ignored → 43 passed / 0 failed / 7 ignored (553 s → 65 s wall)
tests/issue182_bashit_history_interlock green (was failing on the swallow)
Unit tests (niubash-runtime --lib) 468 passed, incl. 10 paste-chunk + 1 TypeaheadGuard regression tests

Test-contract updates

Two tests pinned the pre-#252 per-line Windows fallback and had to move with the contract (their own doc comments said the pin exists so a parser change "cannot silently change it without notice" — #252/#284 is that change, now deliberate): rapid_input_burst_all_lines_execute and multiline_raw_paste_per_line_windows → renamed/reworked to multiline_raw_paste_windows_crlf_buffers_and_cr_executes_per_line, pinning CRLF feed = buffered one-chunk paste; bare-CR feed = per-line typeahead. Executed-output rc-variable markers distinguish execution from echoed buffer text.

Known-unrelated failures (verified on a clean origin/master worktree, not this branch)

  • smoke_1_3_0 d2/d3 wizard journeys stall at the collection-install → niu-git step. On clean origin/master (2a6a12a) d2 fails identically and d3 fails earlier (at "Pick a theme"); this branch's d3 gets one stage further. d2 spawns niu setup standalone — no REPL, no guard, no paste logic on that path. Introduced between 0830c78 and 2a6a12a by something else; separate defect.
  • ctrl_d_after_background_job_exits is timing-flaky standalone (fails 3/3 on an unmodified checkout too) but green in suite runs.

Notes

  • PASTE_LF_PROBE = 10 ms: an order of magnitude above observed ConPTY handoff latency (the LF is synthesized in the same pipe write as the CR), an order below a perceptible submit delay; only hot bare-CR Enters pay it, and only when the queue is empty.
  • Driver stale-prompt matching (the audit's monotonic prompt-counter recommendation) is untouched — separate improvement.
  • No new dependencies; Cargo.lock untouched; MIT license files untouched.

Fixes #284, fixes #287

… timing (#284)

The 50 ms arrival-burst heuristic of #252 classified every bare Enter
that arrived within one burst as paste data, so "text + Enter" in a
single feed - fast human typing, ConPTY test drivers' send_line,
expect scripts, send-keys - lost its submit and needed a second Enter.
35/47 tests/interactive.rs cases timed out on that swallow.

The submit-versus-paste decision at a bare Enter is now structural:
a machine-fed line break is a CRLF pair (the LF surfaces as
Enter + CONTROL), and reedline's own batch loop stops at a bare Enter,
so the pair's LF is always still queued when the CR is dispatched.
A bounded lookahead (PASTE_LF_PROBE, 10 ms) peeks at the queue: an LF
partner proves paste - the pair folds into the pending chunk and the
Enter never reaches reedline; any other queued key or silence proves a
typed submit, and the lookahead's read is pushed back so it lands, in
order, at the next prompt (GNU typeahead semantics). A bare Ctrl+J
opens a chunk by itself (humans do not submit with it). The 50 ms gap
keeps one job: the burst boundary where a pending chunk flushes as ONE
InsertString.

Multi-line paste keeps buffering quietly until the user's Enter - the
niubash#202 contract holds: the 257-line issue payload benches at
4.62 s end to end with 0 mid-paste executions (calibration ~4.6 s /
0). Interactive suite: 5 passed / 35 failed / 7 ignored before, 43
passed / 0 failed / 7 ignored after.

Also fixes the TypeaheadGuard double-injection (niubash#287): the
sweeper's self-heal path reinjected a borrowed view of the saved
records without draining them, so disarm_and_reinject wrote the same
records back a second time - one swept key per prompt cycle arriving
twice. The self-heal now drains under the lock; a console-input
regression test stages records through a duplicate stdin handle and
asserts each comes back exactly once (verified to fail on the
pre-fix path).

Tests: rapid_input_burst and multiline_raw_paste re-pin the #252/#284
contract (CRLF feed buffers as one chunk, bare-CR feed submits per
line); new send_line_first_enter_submits_and_typeahead_executes pins
the #284 regression shape; multiline_crlf_paste_buffers_whole_chunk_
and_executes_once pins the issue-scale 0-mid-execution surface;
esc_sequenced_arrow_keys_edit_without_absorption pins the #277/#280
arrow path.

Fixes #284, fixes #287
@caomengxuan666
caomengxuan666 merged commit 71c6e79 into master Oct 13, 2026
9 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/284-paste-chunk-enter-swallow branch October 13, 2026 02:30
caomengxuan666 added a commit that referenced this pull request Oct 13, 2026
…hunk (closes #308) (#309)

A paste followed immediately by Enter — send(paste_crlf + "\r"), one
write, the Enter <50 ms after the last paste key — was swallowed: the
pending-chunk branch of translate_event() pushed every bare Enter into
the chunk as newline data unconditionally, so nothing executed and a
second Enter was needed.

Extend the #288 structural judgment to the pending branch: a bare CR
now runs the same LF-partner lookahead as the chunk-open Enter
(extracted as PasteChunkEditMode::cr_has_lf_partner). An LF partner
queued behind the CR proves the break paste data (a break still INSIDE
the block); silence or any other queued key proves the block complete
and the Enter the user's submit — the chunk flushes and submits in one
batch, first Enter, no second. Anchored in reedline 0.50.0 bracketed
paste semantics: Event::Paste folds breaks into \n data of one insert
(edit_mode/mod.rs:44-47) and the buffer "can then be submitted with a
separate enter" (engine.rs:408-416); the probe is sound because
reedline's batch loop itself stops at a bare Enter
(engine.rs:1052-1067), so the queue behind a dispatched CR is intact.

The CR now always consumes its partner when present, which retires the
last_bare_cr pairing flag: a directly dispatched Ctrl+J during a burst
is by construction a lone LF — real newline data, never a folded pair.

Tests: unit — pending bare CR without partner flushes+submits the
whole chunk mid-burst (chunk content pinned), interior CRLF pair stays
data with the partner consumed, typeahead behind the pending Enter
submits in order; interactive — paste_with_same_burst_enter_submits_
on_first_enter pins the one-write repro (fails on pre-fix code, verified).

Co-authored-by: cmx <[email protected]>
caomengxuan666 added a commit that referenced this pull request Oct 13, 2026
… and #309 coverage (refs #291) (#317)

The niubash#291 "minimal repro" is a false positive: the scratch probe's
negative assertion !screen.contains("cho two") fails on a CORRECT run
too, because "cho two" is a strict substring of the intact rendering
"echo two". Every captured failure screen shows "echo two" complete with
both outputs correct on a single submit; ~70 execution-level runs across
builds/claim/write-shapes reproduce no byte loss. #291 therefore stays
OPEN pending owner-side evidence (history bytes / rc / terminal / IME /
build provenance) - this PR closes nothing.

What lands here, all test-only (zero runtime code changed):

- Unit (crates/niubash-runtime/src/repl.rs): feed_paste_burst() feeds the
  exact console event stream of a one-write CRLF paste through
  PasteChunkEditMode (scripted lookahead queue, post-#309 semantics:
  every pasted CR consumes its queued LF partner) and reassembles the
  text reaching reedline from the translated events. Four shapes must
  reassemble byte-for-byte: the reported empty-line burst, two
  consecutive empty lines, an indented line after an empty line, and a
  no-empty-line CRLF control.

- Interactive (tests/interactive.rs): execution-level marker technique -
  the pasted body carries `echo RES_$((40+2))`-shaped lines, so the
  output RES_42 appears nowhere in the echoed input and proves the line
  executed with every leading byte intact; a swallowed head leaves no
  marker. Substring assertions against the screen are banned in this
  suite (the probe's trap). Shapes: the owner burst with a settled
  submit, the SAME-burst submit Enter (one write - niubash#309 coverage
  on the empty-line shape: first Enter must submit, the test sends
  nothing else), two consecutive empty lines, indented line, the
  no-empty-line control, and the claimed/custom-PS1 surface from issue
  comment 3 (the one variable the original 30-run sweep never varied).

Adjacent semantics pinned green on current master: whole-CRLF-block
integrity (#288 surface), burst interior Enters stay data (#284), the
burst-settled Enter submits the whole chunk once, and #309's first-Enter
submit holds on the empty-line shapes.

Verification: cargo fmt --check clean; RUSTFLAGS="-D warnings" cargo
check --all-targets clean; RUST_TEST_THREADS=1 NIU_INTERACTIVE_TESTS=1
cargo test --test interactive: new suite green; lib suite green.
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