Skip to content

fix(repl): a DSR-silent terminal keeps its shell; TAB completion commits (closes #373) - #418

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/373
Oct 18, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/373

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Follow-up to #355 (findings 3 and 4; history persistence was closed by #372). Closes #373.

Per-sub-item status

# Sub-item Status What / why / evidence
1 DSR fallback (startup stalls ~2.6 s + errors when the terminal never answers ESC[6n) Fixed reedline 0.50 → 0.52 (still crossterm 0.29, the crossterm29 paste-detector pin stays valid). 0.50's painter aborted read_line on the query timeout (crossterm unix path: fixed 2 s poll) → error + dead session. 0.52's anchor_prompt degrades: newline + stale row, prompt paints, later cycles stay interactive. Repro (execution-level): a DSR-silent PTY on master printed the banner never and the error at ~2.6 s (the #355 Termux measurement, re-verified through the new harness on the unix code path); after the fix the prompt paints and the echo round trip completes with no error text.
2 File completion: unique suffix preview never commits (unlike command completion) Fixed The engine's partial_completions were OFF, so menu activation spliced nothing: cat uniqfi<TAB> kept cat uniqfi, the completed word was only the menu's highlighted entry, and Enter executed the unexpanded path. Now every TAB splices the GNU readline shape (lone candidate commits outright; ambiguous set commits its common prefix and still lists). reedline 0.52 additionally offers an open menu first refusal at Enter (nushell semantics: first Enter accepts, second submits); a bash-like shell runs the line as typed, so the paste wrapper prefixes a typed Enter with a menu-dismissal Esc — one Enter submits, grid up or not. quick_completions stays OFF deliberately (its lone-candidate path simulates Enter = accept AND execute — not bash).
3 First-prompt latency: measure and attribute TTP vs GNU bash before changing startup/theme behavior Measured, no behavior change New scripts/perf/ttp-bench.py (sandboxed HOME, ConPTY, DSR answered, N=5 medians, NIU_TRACE_STARTUP attribution). Release binaries, Win10 19045: master niu 3164.5 ms / bash 3260.2 ms → fix niu 3139.8 ms / bash 3289.3 ms — parity, no regression. Attribution: niu's internal startup is ~70–140 ms; the wall clock is this machine's ConPTY transport floor (same for bash), consistent with scripts/perf/budgets.toml.

Acceptance criteria

  • Reproduce with a DSR-silent PTY: tests/issue373_dsr.rs — an answers-nothing terminal driver over portable-pty. Control leg (answers ESC[6n/ESC[c) green everywhere (banner 215–282 ms, prompt +43–53 ms, cycle ~32 ms). Silent leg: hard assertions (banner + prompt + echo round trip + no line-editor error, bounded budgets) on unix; on Windows/ConPTY the silent condition cannot reach niu — measured transcript of a silent session is exactly <ESC>[6n: conhost withholds the client before its first instruction until the terminal answers (session-attach contract, terminal-side; Windows Terminal / pywinpty / the shared driver all answer). The Windows half of the contract is pinned by the control leg; the silent leg prints a rationale skip there.
  • Reproduce with a real Windows terminal: the interactive suites (ConPTY, DSR answered) — full tests/interactive matrix green apart from a pre-existing failure set (below).
  • Regression coverage for fallback and completion commit: issue373_dsr (fallback) + tab_completes_unique_file_path / tab_completes_command_name un-ignored and green (the rubash#287 "wiring gap" residuals are fixed by this change; the unique-file test asserts the execution-level chain: one Enter after TAB runs the completed path — uniq-file-body prints, no No such file).
  • Before/after TTP benchmark, no regression: table above (3164.5 → 3139.8 ms median, within noise).

Scope notes

Verification (this host, Win10 19045, --test-threads=1)

  • cargo test -p niubash-runtime --lib: 542 passed / 1 failed — test_get_builtin_commands, fails identically on the origin/master baseline run (pre-existing, environment-dependent builtin set).
  • cargo test --test interactive: 65 passed / 5 failed — continuation_single_quote, continuation_subshell_paren, ctrl_c_interrupts_external_command, ctrl_d_after_background_job, gitbash_factory_prompt; byte-identical failure set on the origin/master baseline run (pre-existing on master, unrelated surfaces; multiline_crlf_paste flapped in one run on both trees).
  • issue373_dsr 2/2 · completion_probe 29/29 · repl_command 11/11.
  • Baseline worktrees used for attribution: origin/master @ 7a195f1 and @ d60276e (rebased onto the latter).

@caomengxuan666
caomengxuan666 force-pushed the fix/373 branch 2 times, most recently from cff47e3 to 3f7f0ad Compare October 18, 2026 17:40
@caomengxuan666

Copy link
Copy Markdown
Member Author

CR round pushed (3f7f0ad, rebased onto b3c9c52 — #419/#401 merged mid-flight; interactive.rs conflict resolved by taking master's file and re-applying only this PR's deltas).

F1 (Ctrl+R dead key) — fixed. Root cause confirmed as reviewed: handle_event routes the whole event to handle_history_search_event while input_mode == HistorySearch, and that handler maps Multiple(_) => Inapplicable (reedline 0.52 engine.rs ~1476-1493); a plain Enter there is accept-and-load (sets the buffer, exits search). Chose direction (a) with one refinement — the wrapper cannot see engine state (no path from EditMode to the engine), so instead of a guess it now tracks the window explicitly: menu_possible on PasteChunkEditMode. A TAB press opens it (the only opener — Shift+Tab/MenuPrevious doesn't), a CONTROL chord closes it (the verb that follows — history search, cancel, EOF — owns the next Enter), a wrapped submit closes it, plain text is irrelevant (cannot open/close menus). The Esc prefix rides an Enter only inside the window, so a search-mode Enter always arrives as the bare event its handler implements. Paste flows never open the window — their submit batches are byte-for-byte the pre-#373 shapes again (the paste unit assertions are back to len-2/bare-Enter accordingly). Direction (b) is structurally unreachable from EditMode; noted in the comment.

F1 regression coverage. Two layers: (1) unit — paste_chunk_wrapper_dismissal_esc_rides_only_the_tab_window pins bare-Enter outside the window, [Esc, Enter] inside, window-closed-after-submit, and chord-cleared (TAB → Ctrl+R → Enter stays bare); (2) E2E — ctrl_r_history_search_enter_loads_match_and_executes in tests/issue373.rs: seed printf 'cr373-%s\n' R42 (executed output cr373-R42 is distinct from the history line, so only a real load+submit can produce the second occurrence; the first execution's output is consumed first), Ctrl+R → query → accepting Enter → submit. Green (issue373 3/3). Placement note, for the record: I first wrote it in tests/interactive.rs and it wedged deterministically — the session echoes the typed line but no submit ever executes, matching the swallowed-submit family of the five master-baseline failures (continuation_single_quote, continuation_subshell_paren, ctrl_c_interrupts_external_command, ctrl_d_after_background_job, gitbash_factory — byte-identical failure set on the origin/master baseline runs). The same flow passes through the #373 harness (also portable-pty) — the wedge is in the shared interactive driver's session path, pre-existing on master, and is not this PR's surface; I'd propose it as its own ticket with the cr373 repro.

F2 — superseded by the F1 fix: paste flows no longer carry the Esc at all, so their assertions pin the bare shape ([flush, Enter]) and the wrapped shape is pinned by the new TAB-window unit test instead.

F3 — documented in the delegate comment: Ctrl+J (Enter+CONTROL) never wraps (paste-data path, gated by is_bare_enter_press); the console reports Ctrl+M as the bare Enter key itself, so it wraps exactly like Enter — harmless, the Esc is a no-op outside the window.

F4 — documented in tests/issue373.rs module docs: repo interactive CI is Windows-desktop-only, so CI reaches the control leg; the silent leg's hard assertions need a unix host (any Linux/macOS desktop/Termux check-out).

F5 — tab_completion_empty_set_enter_still_submits added and green (empty candidate set → TAB → Enter still executes the line).

Upstream: agreed and noted here for the record — reedline 0.52's history-search handler drops whole Multiple batches (Multiple(_) => Inapplicable), so ANY host that wraps submit events (or otherwise batches them) dead-keys search acceptance. We'll file it upstream with the engine.rs pointer; our TAB-window gate is the local mitigation.

Re-verification on b3c9c52 (--test-threads=1): cargo fmt --check clean; runtime lib 544/0; issue373 3/3; interactive 66 passed / 5 failed = the byte-identical master-baseline failure set (plus one load flake per run, passes in isolation); completion_probe 29/29; repl_command 11/11.

…its (closes #373)

Follow-up to #355 (findings 3 and 4; history persistence was #372).

Sub-item 1, DSR fallback: interactive startup anchored the prompt with an
ESC[6n cursor-position query and reedline 0.50 ABORTED read_line when it
timed out — on a terminal that never answers (dumb terminals, script(1)
recordings, some serial/tmux links) every startup paid a ~2.6 s stall,
printed 'line editor error: The cursor position could not be read within a
normal duration', and exited before painting any prompt. The editor bump to
reedline 0.52 treats the query as advisory: the prompt paints without a
reply and every later cycle stays interactive (crossterm's fixed 2 s query
budget is the remaining per-cycle cost on the unix path; degrade-not-die).
Windows/ConPTY cannot exercise the silent path at all — measured on this
harness (Win10 19045): a silent session's transcript is exactly ESC[6n,
conhost withholds the client before its first instruction until the
terminal answers, a terminal-side contract below niu (Windows Terminal,
pywinpty's agent and the shared test driver all answer).
tests/issue373.rs pins both halves: the answering control leg everywhere,
the silent regression leg on unix.

Sub-item 2, file completion commit: with the engine's partial completions
off, menu activation spliced NOTHING into the line — 'cat uniqfi<TAB>' kept
'cat uniqfi' with the completed word drawn only as the menu's highlighted
entry, and Enter executed the uncompleted path (cat error). Every TAB now
commits the GNU readline shape: the longest common prefix of the candidate
set is spliced into the line, a lone candidate commits outright, the grid
still shows for an ambiguous set. reedline 0.52 also offers an open menu
first refusal at Enter (nushell's completion cell: first Enter accepts,
second submits); a bash-like shell runs the line as typed, so the wrapper
prefixes a typed Enter with a menu-dismissal Esc — gated to the TAB window
(a TAB may leave the menu alive; a CONTROL chord ends the window, so the
Ctrl+R search's accepting Enter stays the bare event its handler
implements — an unconditional wrap made it a dead key, dropping whole
Multiple batches in search mode). One Enter submits, bash's accept-line,
grid up or not. quick_completions stays OFF deliberately: its
lone-candidate path simulates Enter (accept AND execute).

Sub-item 3, TTP: scripts/perf/ttp-bench.py measures spawn-to-first-prompt
against GNU bash over the same ConPTY harness with NIU_TRACE_STARTUP
attribution. Release binaries, N=5 medians, Win10 19045: master niu
3164.5 ms vs bash 3260.2 ms; fix niu 3139.8 ms vs bash 3289.3 ms — parity,
no regression (the wall clock is this machine's ConPTY transport floor;
niu's internal startup is ~70-140 ms).

Regression evidence: runtime lib 542+ passed / 1 failed
(test_get_builtin_commands, fails identically on the master baseline),
interactive 65 passed / 5 failed (the byte-identical master-baseline
failure set; one load flake per run), issue373 3/3 (DSR control + silent
split, full Ctrl+R flow), completion_probe 29/29, repl_command 11/11.
tab_completes_command_name and tab_completes_unique_file_path are
un-ignored and green (the rubash#287 residuals are fixed by this change);
tab_completion_empty_set_enter_still_submits pins the F5 shape.
@caomengxuan666

Copy link
Copy Markdown
Member Author

Resume after a rate-limit kill: rebased onto current master (717327a — #421 landed; only CHANGELOG.md overlapped, resolved; also corrected a stale issue373_dsr.rs → tests/issue373.rs reference in the changelog entry). No F1 changes — the TAB-window gate, chord carve-outs and the Ctrl+R E2E are exactly as described in the round reply.

Re-verification on 717327a (--test-threads=1): cargo fmt --check clean; runtime lib 551/0 (includes #421's new tests); issue373 3/3 (DSR control + silent split, full Ctrl+R flow); interactive 66 passed / 5 failed = the same master-baseline failure family; completion_probe 29/29; repl_command 11/11.

@caomengxuan666

Copy link
Copy Markdown
Member Author

CR round 2 verdict: APPROVE (post-F1-fix, rebase onto 717327a)

Re-reviewed bcd5769 independently (fresh worktree, all suites re-run locally on Win10 19045). Self-approval is blocked by GitHub (same account), so the verdict lands as a comment.

F1 (Ctrl+R dead key) — fixed and verified at two layers.

  • Window mechanism read at crates/niubash-runtime/src/repl.rs: menu_possible opens on bare Tab only, closes on any CONTROL chord and on the wrapped submit itself; the paste flow never touches it (its Enters dispatch with the window closed), so flush+submit batches are back to the pre-[interactive][UX] Follow-up: DSR fallback, file completion commit, and first-prompt latency #373 byte shape. Outside the window the Enter stays the bare ReedlineEvent::Enter, which is exactly what reedline 0.52's handle_history_search_event implements (its Multiple(_) → Inapplicable drop can no longer swallow a search's accept).
  • Unit: paste_chunk_wrapper_dismissal_esc_rides_only_the_tab_window — pins all four arms (bare outside window; Tab → [Esc, Enter] len 2 exact; window closes with the wrap; Tab → Ctrl+R → Enter stays bare). Passes.
  • E2E: tests/issue373.rs::ctrl_r_history_search_enter_loads_match_and_executes — seeds history, reverse-searches, Enter must LOAD the match and a second Enter execute it (expect_take is consuming, so the second cr373-R42 occurrence can only come from a real load+submit). Passes in a real ConPTY session.

Regression discipline re-verified.

Non-blocking notes (no action required):

  1. The window is an approximation: a menu closed by other paths (typed past the word, user Esc) leaves the window open, so one later bare Enter may carry a no-op Esc — harmless by construction (no menu ⇒ deactivate is a no-op; the batch ends in submit so no extra repaint).
  2. Ctrl+J never wraps (paste-data carve-out); on terminals reporting Ctrl+M as Char('m')+CONTROL the wrap is skipped there — unix-only edge key, documented in the delegate comment.
  3. The DSR silent leg still only executes on manual unix runs (CI unix legs run check + informational lib tests) — same accepted gap as round 1.

Ready to merge.

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.

[interactive][UX] Follow-up: DSR fallback, file completion commit, and first-prompt latency

1 participant