Skip to content

fix(pty): echo-safe live terminal query replies (color-scheme 997) - #13160

Closed
bbingz wants to merge 2 commits into
stablyai:mainfrom
bbingz:fix/13137-query-reply-echo-leak
Closed

bbingz wants to merge 2 commits into
stablyai:mainfrom
bbingz:fix/13137-query-reply-echo-leak

Conversation

@bbingz

@bbingz bbingz commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Summary

  • xterm answers Contour color-scheme queries (`CSI ?996n`) with `CSI ?997;1n` / `;2n` via `onData` → PTY write.
  • On POSIX, writing that reply while the slave still has ECHO on paints the reply into cooked prompts — e.g. `npx skills update` confirm shows `997;1n`.
  • Startup color replies already use `PtyStartupReplyDelivery` ([Bug]: Raw OSC 10/11 color-query replies leak as text in Linux agent pane (opencode) #12112). Live query replies now take the same path on local + SSH relay writes.

Closes #13137

Test plan

  • Live color-scheme reply echo swallowed after query authority closes
  • Live reply deferred while echo probe says echoing
  • `isTerminalQueryReply` recognizes `?997;1n` / `?997;2n`
  • Existing startup ingress suite green

@coderabbitai

coderabbitai Bot commented Aug 8, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Terminal query replies are classified by their echo-safety requirements. Cooked-echo-sensitive replies are routed through PtyStartupIngress.answerLiveQueryReply from session, local PTY, and relay input paths. Handled replies bypass direct writes. Reply delivery now applies separate CSI and OSC echo projections and supports deferred delivery while the slave echoes. Tests cover reply classification and POSIX cooked-mode delivery.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the bug and tests but omits the required Screenshots, AI Review Report, Security Audit, and Notes sections. Add all required template sections, mark Screenshots as “No visual change” if applicable, and document AI review, security checks, and platform-specific risks.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the PTY fix for echo-safe live terminal query replies.
Linked Issues check ✅ Passed The changes address issue #13137 by routing affected terminal query replies through echo-safe delivery and adding regression tests.
Out of Scope Changes check ✅ Passed The code and tests remain focused on preventing terminal query replies from appearing as literal text in cooked prompts.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 84d53893-0ca0-4a80-9270-6c3c51ee125e

📥 Commits

Reviewing files that changed from the base of the PR and between f858c5a and 79ce7f215cf039a6b4b6babbeae9f00e449848cf.

📒 Files selected for processing (5)
  • src/main/providers/local-pty-provider.ts
  • src/relay/pty-handler.ts
  • src/shared/pty-startup-ingress-live-query-reply.test.ts
  • src/shared/pty-startup-ingress.ts
  • src/shared/terminal-query-reply.test.ts

Comment on lines +33 to +58
it('swallows a live color-scheme DSR reply echo after query authority closes', () => {
vi.useFakeTimers()
for (const echoOf of POSIX_COOKED_ECHOES) {
const writes: string[] = []
const emissions: PtyIngressEmission[] = []
let ingress!: PtyStartupIngress
ingress = new PtyStartupIngress({
ownerBackend: 'posix-pty',
write: (data) => {
writes.push(data)
ingress.accept(echoOf(data))
},
onEmission: (emission) => emissions.push(emission)
})

expect(ingress.answerLiveQueryReply(COLOR_SCHEME_REPLY)).toBe(true)
vi.advanceTimersByTime(0)
expect(writes).toEqual([COLOR_SCHEME_REPLY])
expect(visible(emissions)).toBe('')

ingress.accept('Ok to proceed? (y) ')
expect(visible(emissions)).toBe('Ok to proceed? (y) ')
ingress.drainAndClose()
}
vi.useRealTimers()
})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

Restore real timers during failure paths.

If an assertion fails, vi.useRealTimers() does not run. Later tests can then use fake timers unexpectedly.

  • src/shared/pty-startup-ingress-live-query-reply.test.ts#L33-L58: Put timer restoration in try/finally or afterEach.
  • src/shared/pty-startup-ingress-live-query-reply.test.ts#L60-L80: Apply the same cleanup pattern.
📍 Affects 1 file
  • src/shared/pty-startup-ingress-live-query-reply.test.ts#L33-L58 (this comment)
  • src/shared/pty-startup-ingress-live-query-reply.test.ts#L60-L80

bbingz added 2 commits August 8, 2026 18:59
Live xterm query replies (CSI ?997 color-scheme DSR, CPR, DA, …) were
written straight to the PTY master. On POSIX, a cooked slave with ECHO
on copies them back as visible junk — the `997;1n` garbage at npx
confirm prompts. Route isTerminalQueryReply writes through the existing
startup ingress delivery so they wait for quiet and suppress their own
echo, matching stablyai#12112 startup color-reply handling.

Closes stablyai#13137
Only private DSR/OSC replies use the ECHO-safe path so CPR stays immediate.
Drop CSI identity projections that re-armed ESC-led holds. Daemon Session.write
shares the same intercept as local/relay hosts.
@bbingz
bbingz force-pushed the fix/13137-query-reply-echo-leak branch from 79ce7f2 to 9697ca2 Compare August 8, 2026 11:01

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 541571fc-b136-42b9-81da-88cf6625aff9

📥 Commits

Reviewing files that changed from the base of the PR and between 79ce7f215cf039a6b4b6babbeae9f00e449848cf and 9697ca2.

📒 Files selected for processing (7)
  • src/main/daemon/session.ts
  • src/main/providers/local-pty-provider.ts
  • src/relay/pty-handler.ts
  • src/shared/pty-startup-ingress-live-query-reply.test.ts
  • src/shared/pty-startup-reply-delivery.ts
  • src/shared/terminal-query-reply.test.ts
  • src/shared/terminal-query-reply.ts
🚧 Files skipped from review as they are similar to previous changes (3)
  • src/main/providers/local-pty-provider.ts
  • src/relay/pty-handler.ts
  • src/shared/pty-startup-ingress-live-query-reply.test.ts

Comment on lines +236 to +240
// Why: daemon-hosted POSIX PTYs hit the same cooked-prompt 997 leak as local
// provider writes; share the ingress echo-safe path (#13137 / #13160 review).
if (needsCookedEchoSafeQueryReply(data) && this.startupIngress.answerLiveQueryReply(data)) {
return
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

Route ECHO-safe replies before the startup queue.

The queue branch at Line 231 runs before this branch. A private DSR or OSC reply received during either gate is later written directly by flushPreReadyQueue() at Lines 737-742. This bypasses PtyStartupIngress and can still paint reply bytes in a cooked prompt.

Move the needsCookedEchoSafeQueryReply() and answerLiveQueryReply() branch before the queue branch.

Proposed fix
+    if (needsCookedEchoSafeQueryReply(data) && this.startupIngress.answerLiveQueryReply(data)) {
+      return
+    }
+
     if (this._shellState === 'pending' || this.postReadyFlushGate.isPending) {
       this.preReadyStdinQueue.push(data)
       return
     }
-
-    if (needsCookedEchoSafeQueryReply(data) && this.startupIngress.answerLiveQueryReply(data)) {
-      return
-    }

@nwparker

nwparker commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Closing as superseded by #13309. Your core echo-safe live-reply routing approach and analysis were incorporated into the final fix, and the squash commit credits you as co-author. Thank you for catching and fixing this path, @bbingz.

@bbingz

bbingz commented Aug 9, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the co-author credit and for consolidating into #13309.

nwparker added a commit that referenced this pull request Aug 26, 2026
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.
nwparker added a commit that referenced this pull request Aug 26, 2026
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.
dallascrilley pushed a commit to dallascrilley/orca that referenced this pull request Aug 27, 2026
…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.
rahuljangirworks pushed a commit to rahuljangirworks/veer that referenced this pull request Aug 27, 2026
…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.
paidaxingyo666 pushed a commit to paidaxingyo666/Manta that referenced this pull request Aug 28, 2026
…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)
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…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.
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.

Terminal escape sequence leaks as literal text during npx interactive prompt (e.g. 997;1n)

2 participants