Repository navigation
fix(pty): match the echo shapes a real tty actually produces - #16542
Conversation
📝 WalkthroughWalkthroughUpdated PTY reply echo modeling to caret-encode all C0 control characters and DEL. Added readline projections for OSC and private DSR replies. Added live PTY fixtures and tests for POSIX echo forms, ConPTY behavior, Windows WSL suppression, and query-reply suppression. Added the tests to reliability-gate and shell-contract test commands. Merge Risk: 🔵 Low · up to The PR improves terminal echo cleanup but still accepts malformed private-DSR parameters that could suppress unintended input in a bounded edge case; it is mergeable with explicit owner awareness and a follow-up validation fix. 🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
Full details: Description checkExplanation The description gives a detailed explanation of the problem, implementation, scope, testing, and known gaps. However, it does not provide the required Linked Issue entry, the required Visual Proof section with N/A or attachments, or the template checklist confirmations. Resolution Add the required Linked Issue reference after "Fixes #". Add the Visual Proof section and provide before/after attachments, or write "N/A" with a brief reason if there is no visual or interaction change. Complete the required Testing, AI Disclosure, Agent skill upstream boundary, Notes, and Checklist sections as applicable, including explicit confirmations or explanations for unchecked items. Full details: Docstring CoverageExplanation Docstring coverage is 60.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 5 functions across 5 files. (1 skipped: 1 unsupported.)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
a1732d1 to
f6f15c4
Compare
Reply echo suppression modelled two echo shapes from the spec rather than from
a tty. Captured under node-pty against real bash, at a readline prompt and
under `read`:
- Readline mangles CSI replies, not just OSC: `ESC [ ?` becomes BEL and the
residue echoes. The projection was gated on an OSC introducer, so a private
DSR echo was never matched at a readline prompt. This is the reachable one:
a mode-2031 theme push (`CSI ?997;1n`) left latched by an exited TUI paints
`997;1n` on a bash prompt (#9993's scenario).
- ECHOCTL carets EVERY control, not just ESC. A BEL-terminated OSC reply
echoes as `^G`, but the needle kept a literal BEL — a string no tty
produces. Hardening only: every in-tree OSC reply is ST-terminated
(terminal-osc-color-reply.ts:112, xterm's own reply), so the changed byte
is unreachable except from a foreign or older emulator.
Why this is not the CSI projection #13160 review dropped: that one was the
identity (`replaceAll('\x1b]', …)` is a no-op on a CSI reply), so it was
ESC-led and 500ms-held bare-ESC tails away from the query parser. This one is
BEL-led. The rule is now asserted for every shape rather than implied by the
gate: holdPartial iff the needle does not start with ESC.
The readline branch is keyed on the private-DSR grammar with a non-empty
parameter list, plus a floor on needle length. The containment grammar admits
`CSI ? n`, and `answerLiveQueryReply` takes client-supplied bytes on the relay
path, so a peer could otherwise arm a two-byte `BEL n` needle and delete the
first bell-then-`n` in ordinary output. #61c65151129 proved this system can eat
real output when a needle outlives its budget; a length floor is cheap.
Live coverage: pty-reply-echo-shapes.node-pty.test.ts writes a reply to a real
bash master and feeds back what it echoes, so a shell or libc change fails the
suite instead of silently disarming suppression. Registered in the
shell-contracts lane. The transcript tests and the caretEcho helpers that
encoded the same ESC-only assumption are corrected alongside.
Suppression is display-only. This does not change what reaches the child's
stdin — the reply is written to the master either way, in call order.
f6f15c4 to
95a9779
Compare
Heads-up: #16123 and this PR are rewriting the same functionFlagging before either merges, because whichever lands second will silently undo the other. Both change
The two are complementary in intent and conflicting in text. Specifically:
Suggested order: land this one first (it is a narrow correction to two existing shapes, plus tests), then rebase #16123 on top. #16123's restructuring absorbs a corrected pair cleanly; the reverse means re-deriving the corrections inside a changed shape of the function. One thing worth carrying across either way: #16123's zsh shapes are BEL-initial, same as the readline needle here. This PR adds a minimum-length floor and requires a non-empty DSR parameter list, because Context for why this matters more than usual here: |
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 5fa5792f-e42e-448b-8d3e-ae65cb269b49
📥 Commits
Reviewing files that changed from the base of the PR and between f6f15c4046b36e06cca961568e566229122652a8 and 95a9779.
📒 Files selected for processing (6)
.github/workflows/pr.ymlsrc/renderer/src/components/terminal-pane/pty-input-write-queue.test.tssrc/shared/pty-reply-echo-shapes.node-pty.test.tssrc/shared/pty-startup-ingress-live-query-reply.test.tssrc/shared/pty-startup-reply-echo-shapes.test.tssrc/shared/pty-startup-reply-echo-shapes.ts
Included review availability: Your plan provides up to 10 included reviews per hour; 3 remain after this review.
| /* oxlint-disable-next-line no-control-regex -- terminal reply grammars are control sequences */ | ||
| const PRIVATE_DSR_RE = new RegExp('^\\u001b\\[\\?[0-9][0-9;]*n$') |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Reject empty private-DSR parameter components. PRIVATE_DSR_RE accepts \x1b[?9;n and \x1b[?9;;1n, which can arm suppression from malformed client input; require digits after every ; and add regression cases.
Proposed fix
-const PRIVATE_DSR_RE = new RegExp('^\\u001b\\[\\?[0-9][0-9;]*n$')
+const PRIVATE_DSR_RE = new RegExp('^\\u001b\\[\\?[0-9]+(?:;[0-9]+)*n$')📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| /* oxlint-disable-next-line no-control-regex -- terminal reply grammars are control sequences */ | |
| const PRIVATE_DSR_RE = new RegExp('^\\u001b\\[\\?[0-9][0-9;]*n$') | |
| /* oxlint-disable-next-line no-control-regex -- terminal reply grammars are control sequences */ | |
| const PRIVATE_DSR_RE = new RegExp('^\\u001b\\[\\?[0-9]+(?:;[0-9]+)*n$') |
…i#16542) Reply echo suppression modelled two echo shapes from the spec rather than from a tty. Captured under node-pty against real bash, at a readline prompt and under `read`: - Readline mangles CSI replies, not just OSC: `ESC [ ?` becomes BEL and the residue echoes. The projection was gated on an OSC introducer, so a private DSR echo was never matched at a readline prompt. This is the reachable one: a mode-2031 theme push (`CSI ?997;1n`) left latched by an exited TUI paints `997;1n` on a bash prompt (stablyai#9993's scenario). - ECHOCTL carets EVERY control, not just ESC. A BEL-terminated OSC reply echoes as `^G`, but the needle kept a literal BEL — a string no tty produces. Hardening only: every in-tree OSC reply is ST-terminated (terminal-osc-color-reply.ts:112, xterm's own reply), so the changed byte is unreachable except from a foreign or older emulator. Why this is not the CSI projection stablyai#13160 review dropped: that one was the identity (`replaceAll('\x1b]', …)` is a no-op on a CSI reply), so it was ESC-led and 500ms-held bare-ESC tails away from the query parser. This one is BEL-led. The rule is now asserted for every shape rather than implied by the gate: holdPartial iff the needle does not start with ESC. The readline branch is keyed on the private-DSR grammar with a non-empty parameter list, plus a floor on needle length. The containment grammar admits `CSI ? n`, and `answerLiveQueryReply` takes client-supplied bytes on the relay path, so a peer could otherwise arm a two-byte `BEL n` needle and delete the first bell-then-`n` in ordinary output. #61c65151129 proved this system can eat real output when a needle outlives its budget; a length floor is cheap. Live coverage: pty-reply-echo-shapes.node-pty.test.ts writes a reply to a real bash master and feeds back what it echoes, so a shell or libc change fails the suite instead of silently disarming suppression. Registered in the shell-contracts lane. The transcript tests and the caretEcho helpers that encoded the same ESC-only assumption are corrected alongside. Suppression is display-only. This does not change what reaches the child's stdin — the reply is written to the master either way, in call order.
…i#16542) Reply echo suppression modelled two echo shapes from the spec rather than from a tty. Captured under node-pty against real bash, at a readline prompt and under `read`: - Readline mangles CSI replies, not just OSC: `ESC [ ?` becomes BEL and the residue echoes. The projection was gated on an OSC introducer, so a private DSR echo was never matched at a readline prompt. This is the reachable one: a mode-2031 theme push (`CSI ?997;1n`) left latched by an exited TUI paints `997;1n` on a bash prompt (stablyai#9993's scenario). - ECHOCTL carets EVERY control, not just ESC. A BEL-terminated OSC reply echoes as `^G`, but the needle kept a literal BEL — a string no tty produces. Hardening only: every in-tree OSC reply is ST-terminated (terminal-osc-color-reply.ts:112, xterm's own reply), so the changed byte is unreachable except from a foreign or older emulator. Why this is not the CSI projection stablyai#13160 review dropped: that one was the identity (`replaceAll('\x1b]', …)` is a no-op on a CSI reply), so it was ESC-led and 500ms-held bare-ESC tails away from the query parser. This one is BEL-led. The rule is now asserted for every shape rather than implied by the gate: holdPartial iff the needle does not start with ESC. The readline branch is keyed on the private-DSR grammar with a non-empty parameter list, plus a floor on needle length. The containment grammar admits `CSI ? n`, and `answerLiveQueryReply` takes client-supplied bytes on the relay path, so a peer could otherwise arm a two-byte `BEL n` needle and delete the first bell-then-`n` in ordinary output. #61c65151129 proved this system can eat real output when a needle outlives its budget; a length floor is cheap. Live coverage: pty-reply-echo-shapes.node-pty.test.ts writes a reply to a real bash master and feeds back what it echoes, so a shell or libc change fails the suite instead of silently disarming suppression. Registered in the shell-contracts lane. The transcript tests and the caretEcho helpers that encoded the same ESC-only assumption are corrected alongside. Suppression is display-only. This does not change what reaches the child's stdin — the reply is written to the master either way, in call order.
…i#16542) Reply echo suppression modelled two echo shapes from the spec rather than from a tty. Captured under node-pty against real bash, at a readline prompt and under `read`: - Readline mangles CSI replies, not just OSC: `ESC [ ?` becomes BEL and the residue echoes. The projection was gated on an OSC introducer, so a private DSR echo was never matched at a readline prompt. This is the reachable one: a mode-2031 theme push (`CSI ?997;1n`) left latched by an exited TUI paints `997;1n` on a bash prompt (stablyai#9993's scenario). - ECHOCTL carets EVERY control, not just ESC. A BEL-terminated OSC reply echoes as `^G`, but the needle kept a literal BEL — a string no tty produces. Hardening only: every in-tree OSC reply is ST-terminated (terminal-osc-color-reply.ts:112, xterm's own reply), so the changed byte is unreachable except from a foreign or older emulator. Why this is not the CSI projection stablyai#13160 review dropped: that one was the identity (`replaceAll('\x1b]', …)` is a no-op on a CSI reply), so it was ESC-led and 500ms-held bare-ESC tails away from the query parser. This one is BEL-led. The rule is now asserted for every shape rather than implied by the gate: holdPartial iff the needle does not start with ESC. The readline branch is keyed on the private-DSR grammar with a non-empty parameter list, plus a floor on needle length. The containment grammar admits `CSI ? n`, and `answerLiveQueryReply` takes client-supplied bytes on the relay path, so a peer could otherwise arm a two-byte `BEL n` needle and delete the first bell-then-`n` in ordinary output. #61c65151129 proved this system can eat real output when a needle outlives its budget; a length floor is cheap. Live coverage: pty-reply-echo-shapes.node-pty.test.ts writes a reply to a real bash master and feeds back what it echoes, so a shell or libc change fails the suite instead of silently disarming suppression. Registered in the shell-contracts lane. The transcript tests and the caretEcho helpers that encoded the same ESC-only assumption are corrected alongside. Suppression is display-only. This does not change what reaches the child's stdin — the reply is written to the master either way, in call order. (cherry picked from commit d1a11b3)
…i#16542) Reply echo suppression modelled two echo shapes from the spec rather than from a tty. Captured under node-pty against real bash, at a readline prompt and under `read`: - Readline mangles CSI replies, not just OSC: `ESC [ ?` becomes BEL and the residue echoes. The projection was gated on an OSC introducer, so a private DSR echo was never matched at a readline prompt. This is the reachable one: a mode-2031 theme push (`CSI ?997;1n`) left latched by an exited TUI paints `997;1n` on a bash prompt (stablyai#9993's scenario). - ECHOCTL carets EVERY control, not just ESC. A BEL-terminated OSC reply echoes as `^G`, but the needle kept a literal BEL — a string no tty produces. Hardening only: every in-tree OSC reply is ST-terminated (terminal-osc-color-reply.ts:112, xterm's own reply), so the changed byte is unreachable except from a foreign or older emulator. Why this is not the CSI projection stablyai#13160 review dropped: that one was the identity (`replaceAll('\x1b]', …)` is a no-op on a CSI reply), so it was ESC-led and 500ms-held bare-ESC tails away from the query parser. This one is BEL-led. The rule is now asserted for every shape rather than implied by the gate: holdPartial iff the needle does not start with ESC. The readline branch is keyed on the private-DSR grammar with a non-empty parameter list, plus a floor on needle length. The containment grammar admits `CSI ? n`, and `answerLiveQueryReply` takes client-supplied bytes on the relay path, so a peer could otherwise arm a two-byte `BEL n` needle and delete the first bell-then-`n` in ordinary output. #61c65151129 proved this system can eat real output when a needle outlives its budget; a length floor is cheap. Live coverage: pty-reply-echo-shapes.node-pty.test.ts writes a reply to a real bash master and feeds back what it echoes, so a shell or libc change fails the suite instead of silently disarming suppression. Registered in the shell-contracts lane. The transcript tests and the caretEcho helpers that encoded the same ESC-only assumption are corrected alongside. Suppression is display-only. This does not change what reaches the child's stdin — the reply is written to the master either way, in call order.
The one-liner
When a program asks Orca's terminal a question, Orca's answer sometimes got typed onto your shell prompt as garbage. We had code to clean that up. It didn't recognise the theme answer, so that one always leaked.
What you'd have seen
You're at a bash prompt, you switch light/dark, and suddenly:
You didn't type it. Orca did.
Why that happens at all
Programs ask the terminal questions — "what's your background colour?", "is the theme dark or light?" Normally the program reads the answer and you never see it.
Two ways it ends up on your prompt instead:
Orca already had a cleanup step: remember what we sent, watch for it coming back, delete it before you see it.
The bug
To delete the echo, Orca must predict exactly what the echoed text looks like — the terminal rewrites it on the way back. Both predictions were written from the spec rather than from watching a real terminal.
The one users hit: the readline prediction only handled colour answers (
ESC ]). Theme answers are a different shape (ESC [ ?), so there was no prediction for them at all — the theme answer was never cleaned up at a bash prompt, ever.The other one: the cooked-mode prediction only caret-encoded ESC, but a terminal caret-encodes every control byte, so a trailing bell should be
^G. The old prediction contained a raw bell — a string no terminal produces.Why nobody caught it: the existing test built the echo using the same wrong assumption as the code. Test and bug agreed, so it passed.
Honest scoping — please read
These two are not equally important, and I don't want to oversell:
The second one is hardening because every colour answer Orca produces today ends with ST, not a bell (
terminal-osc-color-reply.ts:112, and xterm's own reply path) — and the ST shape already matched. A bell-terminated answer can only arrive from an older or third-party terminal on the mixed-version path.An earlier revision of this PR claimed both halves fixed live leaks. That was wrong and is corrected here.
How it was found
Ran a real terminal (
node-pty+ realbash), sent it each answer, recorded the exact bytes that came back — at a normal prompt and at areadprompt. Then made the predictions match the recording.Every echo string in the new test is a transcript, not a guess.
Also re-ran across bash, zsh, and sh: cooked-mode echo is byte-identical in all three (it's the kernel, not the shell), and
stty -echoctlgives the verbatim form the third prediction already covers.Before / after
Why this doesn't regress the thing the gate was protecting
The OSC-only restriction was deliberate (#13160 review), and worth explaining since I'm removing it.
replaceAll('\x1b]', …)on a CSI reply is a no-op — it produced a prediction still starting with ESC. An ESC-initial prediction is dangerous: a read ending on a bare ESC looks like the start of it, so it gets held for 500ms, stealing that ESC from the query parser and potentially losing a query.The replacement produces
\x07+ residue — BEL-initial, so the hazard doesn't apply. The invariant is now asserted directly for every shape (holdPartialiff the needle doesn't start with ESC), which nothing did before.Blast radius
Known gap, not addressed here
zsh's line editor doesn't rewrite the sequence — it redraws the line with cursor motion, so no prediction matches it. Confirmed on a live pty. This was equally true before this PR; readline coverage is bash/sh.
Testing
pty-startup-reply-echo-shapes.test.ts— 35 tests, all from the live-pty transcriptsrc/shared,src/main/daemon,src/relay,src/main/providers,src/renderer/.../terminal-paneterminal-input.cooked-reply-queuereliability gate with a fresh evidence run; manifest check passesFollow-ups, deliberately not bundled
Cross-checked against other terminal implementations
The cooked-mode rule (
^+ code+0x40, DEL →^?) is confirmed by four independent implementations, including the BSDvis(3)ancestor of the notation. No dissent found.Two hardening notes came out of that check and are now in the diff:
^?, per the same convention.Both are pinned by a table test.
The readline rule is only modelled by one other project, and our version is the more accurate of the two. Theirs swallows a fixed 3 bytes after ESC; ours swallows 2 for OSC and 3 for CSI. They agree exactly on the CSI case and differ by one byte on OSC — ours is right, because
ESC [is a bound prefix in readline's keymap (arrow keys) whileESC ]is not, so readline dings one byte earlier.That difference is invisible in their product because they only detect the echo; it would be visible in ours because we remove it — using their constant would leave a stray
1on the prompt.Everyone else sidesteps the problem entirely: predictive-echo implementations give up on control bytes rather than model them, and terminal test suites set
stty raw -echoso the echo never happens. So there's no second reference implementation to check the shipped behaviour against — which is the main reason the tests here are a live transcript rather than a spec reading.