Skip to content

fix(pty): stop color-scheme 997 replies from painting cooked prompts - #13309

Merged
nwparker merged 5 commits into
mainfrom
nwparker/terminal-escape-sequence-leaks-as-literal-text-d
Aug 9, 2026
Merged

nwparker merged 5 commits into
mainfrom
nwparker/terminal-escape-sequence-leaks-as-literal-text-d

Conversation

@nwparker

@nwparker nwparker commented Aug 9, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #13137: terminal-generated mode-2031 color-scheme replies (CSI ?997;1n / CSI ?997;2n) could be echoed as literal text into cooked prompts such as npx skills update confirmations.

This combines the core echo-safe routing approach from #13160 with multi-reply extraction, queue atomicity, daemon startup-gate coverage, and bounded live-session resources. Credit to @bbingz for the original parallel fix and analysis.

Closes #13137

Behavior

  • Routes private DSR and OSC color replies through cooked-ECHO-safe delivery on local PTYs, SSH relay PTYs, and daemon sessions.
  • Preserves latency-critical CPR, public DSR, DA, and ordinary input on their existing immediate paths.
  • Extracts consecutive reply-only payloads and delivers every valid reply independently; repeated identical reports are not deduplicated because a subscription and query can legitimately produce the same reply.
  • Prevents the input queue from coalescing echo-risk replies into a raw-write payload.
  • Routes replies before the daemon startup gate, while ordinary startup input remains queued.
  • Bounds pending replies and echo projections at 64, shares in-flight stty probes, caps probe starts at 10/second per PTY, and hard-falls back within 200 ms if a probe stalls.

Reliability contract

  • Invariant: every valid terminal-generated reply reaches the program in order, while its cooked-mode echo never reaches visible terminal output.
  • Failure source: Terminal escape sequence leaks as literal text during npx interactive prompt (e.g. 997;1n) #13137; PTY master writes under POSIX ECHO painted 997;1n, including coalesced/repeated replies and daemon pre-ready writes.
  • Oracle: byte-level tests assert reply delivery count/order, startup-gate bypass, caret/OSC echo stripping, mixed-input fallthrough, bounded state, bounded probe starts, and the 200 ms deadline.
  • Provider/platform coverage: local, daemon, and SSH relay host paths; POSIX echo handling plus unchanged ConPTY/WSL fallback behavior; no new path, shell, native-module, dependency, persistence, RPC schema, stream opcode, or wire-format contract.
  • Performance: deterministic tests cover the 64-entry bounds, 10 probes/second cap, shared in-flight probe, and 200 ms delivery ceiling.
  • Residual gap: the Docker SSH E2E proves a fresh Linux relay and real remote PTY round-trip, while the exact 997 byte containment is pinned at the relay/provider and shared-delivery layers rather than by invoking the external npx skills update command.

Validation

  • pnpm run lint
  • pnpm run typecheck
  • ORCA_CROSS_VERSION_BASELINE_REF=v1.4.176 pnpm test — 4,523 files passed; 48,471 tests passed; 97 skipped
  • Registered mobile query-authority gates — 12 tests passed
  • Focused main/renderer/mobile query-authority gates — 644 tests passed
  • Focused daemon/relay/startup/input-queue suites — 279 tests passed
  • Cross-version terminal wire journey — 4 tests passed
  • pnpm run build:relay — Linux, macOS, Windows, and WSL artifacts built
  • ORCA_E2E_SSH_DOCKER=1 pnpm run test:e2e:ssh-docker-terminal-parking — 2 tests passed against a fresh Debian/OpenSSH container
  • git diff --check
  • Fresh PR CI — all Node 24/26 shards, macOS/Linux/Windows native smoke, static analysis, typecheck, wire compatibility, packaging, and aggregate verify passed

Readiness review

No P0, P1, or P2 findings. Reviewed functional failure paths, startup ordering, concurrency, teardown, casts/crash/retry/growth, security and supply chain, performance/resources, mobile/backward compatibility, macOS/Linux/Windows/WSL behavior, SSH/folder-workspace behavior, and remote-wire compatibility.

No visual change.

Live xterm query replies (CSI ?997 color-scheme DSR, OSC) were written
straight to the PTY master. On POSIX, a cooked slave with ECHO on copies
them back as visible junk — the `997;1n` paint at npx confirm prompts.
Route cooked-echo-risk replies through the existing startup ingress
delivery so they wait for quiet and suppress their own echo, matching
startup color-reply handling. CPR/DA stay on the immediate path.

Closes #13137
Dual mode-2031 answerers enqueue two CSI ?997;1n writes; the input
queue could coalesce them into one payload that failed the whole-string
classifier and fell through to a raw PTY write under ECHO. Keep
cooked-echo-risk replies atomic in the queue, extract multi-reply
payloads on host write, and dedupe identical in-flight replies in
delivery so stdin is not doubled either.
After a quiet write, CSI color-scheme replies have no echo projection, so
a late second answerer could still double-write stdin. Keep reply identity
in expectedEchoes briefly even with empty projections, and pin that with a
regression test.
@coderabbitai

coderabbitai Bot commented Aug 9, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The change adds cooked-echo-safe terminal query reply classification and extraction for private-mode DSR and OSC replies. PTY write paths route handled replies through startup ingress instead of direct writes. Startup reply delivery tracks projections, suppresses duplicate replies, bounds pending state, and limits echo probes. PTY input coalescing excludes cooked-echo-risk replies. Tests cover extraction, routing, echo suppression, deferred delivery, deduplication, and session terminal control.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the issue, solution, scope, risks, and tests, but omits the required AI Review Report and Security Audit sections. Add the required AI Review Report with explicit macOS, Linux, and Windows compatibility checks, plus a Security Audit section covering input, IPC, command, and dependency risks.
Docstring Coverage ⚠️ Warning Docstring coverage is 58.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the PTY fix for color-scheme replies painting cooked prompts.
Linked Issues check ✅ Passed The changes directly address #13137 by routing color-scheme replies through echo-safe delivery and preventing duplicate or leaked 997 responses.
Out of Scope Changes check ✅ Passed The production changes and regression tests support the linked issue and stated reliability objectives; no unrelated scope is evident.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch

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: 2


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: b8a7be12-db60-4af1-a968-ed6747763093

📥 Commits

Reviewing files that changed from the base of the PR and between e172a51 and 9328cef.

📒 Files selected for processing (10)
  • src/main/daemon/session.ts
  • src/main/providers/local-pty-provider.ts
  • src/relay/pty-handler.ts
  • src/renderer/src/components/terminal-pane/pty-input-write-queue.test.ts
  • src/renderer/src/components/terminal-pane/pty-input-write-queue.ts
  • src/shared/pty-startup-ingress-live-query-reply.test.ts
  • src/shared/pty-startup-ingress.ts
  • src/shared/pty-startup-reply-delivery.ts
  • src/shared/terminal-query-reply.test.ts
  • src/shared/terminal-query-reply.ts

Comment thread src/main/daemon/session.ts Resolved
Keep repeated protocol replies intact while routing each through cooked-echo containment. Cover daemon startup gates, cap live reply state and probe subprocess starts, and force a hard reply deadline when stty stalls.\n\nCo-authored-by: bbingz <[email protected]>

@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: eef79d74-2100-48df-9f5b-231c4757467a

📥 Commits

Reviewing files that changed from the base of the PR and between 9328cef and ffef9ae.

📒 Files selected for processing (14)
  • src/main/daemon/session-terminal-control.test.ts
  • src/main/daemon/session.test.ts
  • src/main/daemon/session.ts
  • src/main/providers/local-pty-provider.ts
  • src/relay/pty-handler.ts
  • src/renderer/src/components/terminal-pane/pty-input-write-queue.test.ts
  • src/renderer/src/components/terminal-pane/pty-input-write-queue.ts
  • src/shared/pty-slave-line-discipline-echo.test.ts
  • src/shared/pty-slave-line-discipline-echo.ts
  • src/shared/pty-startup-ingress-live-query-reply.test.ts
  • src/shared/pty-startup-ingress.test.ts
  • src/shared/pty-startup-ingress.ts
  • src/shared/pty-startup-reply-delivery.ts
  • src/shared/terminal-query-reply.ts
🚧 Files skipped from review as they are similar to previous changes (7)
  • src/shared/pty-startup-ingress.ts
  • src/relay/pty-handler.ts
  • src/main/providers/local-pty-provider.ts
  • src/renderer/src/components/terminal-pane/pty-input-write-queue.ts
  • src/shared/terminal-query-reply.ts
  • src/main/daemon/session.ts
  • src/renderer/src/components/terminal-pane/pty-input-write-queue.test.ts

Comment on lines +357 to +372
private claimEchoProbeStart(): boolean {
const now = Date.now()
if (
this.echoProbeWindowStartedAt === null ||
now < this.echoProbeWindowStartedAt ||
now - this.echoProbeWindowStartedAt >= 1_000
) {
this.echoProbeWindowStartedAt = now
this.echoProbeStartsInWindow = 0
}
if (this.echoProbeStartsInWindow >= ECHO_PROBE_MAX_STARTS_PER_SECOND) {
return false
}
this.echoProbeStartsInWindow += 1
return true
}

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.

🚀 Performance & Scalability | 🟡 Minor | ⚡ Quick win

Use a sliding probe-start window.

This fixed window can admit ten probe starts immediately before reset and ten immediately after reset. That permits 20 starts in a trailing one-second interval.

Store recent start timestamps. Remove timestamps older than one second before checking the limit. Add a boundary regression test.

@nwparker
nwparker merged commit 34f2a62 into main Aug 9, 2026
50 checks passed
@bbingz

bbingz commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Thanks for landing this @nwparker — glad the echo-safe live-reply routing from #13160 made it into the final fix.

nwparker added a commit that referenced this pull request Aug 20, 2026
…15559)

A background-colour probe writes `OSC 11 ;? ST` then `CSI 6n` and reads
exactly one response, using the CPR as its sentinel: a non-OSC first
response means "unsupported" and it stops draining. #13309 routed live
cooked-echo-risk replies through the ECHO-probe deferral while CPR kept
the immediate path, so the CPR overtook the colour reply, the prober gave
up, and the stray `ESC ]` was left in the tty for the next program —
`gh auth login` died on it with an escape-sequence error.

Queue a reply that needs no echo containment behind ones that do, FIFO,
and only while something is actually deferred, so latency-critical
replies stay immediate on every other path. Windows is unaffected: only
posix-pty defers, so the queue is always empty there.

Also: an in-flight echo probe is already the write continuation, so
re-arming the timer for a queued reply would fork a second stty and throw
away the first verdict; and teardown now hands queued uncontained writes
to the pty best-effort instead of dropping bytes the caller was told were
sent.

Known scope limit, pinned by tests and tracked for follow-up: the
guarantee is FIFO among recognised query replies, not over every byte —
a reply coalesced with a keystroke, and ordinary typed input, still
bypass the queue. Both were unordered before this change too.
paidaxingyo666 pushed a commit to paidaxingyo666/Manta that referenced this pull request Aug 21, 2026
…tablyai#15559)

A background-colour probe writes `OSC 11 ;? ST` then `CSI 6n` and reads
exactly one response, using the CPR as its sentinel: a non-OSC first
response means "unsupported" and it stops draining. stablyai#13309 routed live
cooked-echo-risk replies through the ECHO-probe deferral while CPR kept
the immediate path, so the CPR overtook the colour reply, the prober gave
up, and the stray `ESC ]` was left in the tty for the next program —
`gh auth login` died on it with an escape-sequence error.

Queue a reply that needs no echo containment behind ones that do, FIFO,
and only while something is actually deferred, so latency-critical
replies stay immediate on every other path. Windows is unaffected: only
posix-pty defers, so the queue is always empty there.

Also: an in-flight echo probe is already the write continuation, so
re-arming the timer for a queued reply would fork a second stty and throw
away the first verdict; and teardown now hands queued uncontained writes
to the pty best-effort instead of dropping bytes the caller was told were
sent.

Known scope limit, pinned by tests and tracked for follow-up: the
guarantee is FIFO among recognised query replies, not over every byte —
a reply coalesced with a keystroke, and ordinary typed input, still
bypass the queue. Both were unordered before this change too.
dallascrilley pushed a commit to dallascrilley/orca that referenced this pull request Aug 27, 2026
…tablyai#15559)

A background-colour probe writes `OSC 11 ;? ST` then `CSI 6n` and reads
exactly one response, using the CPR as its sentinel: a non-OSC first
response means "unsupported" and it stops draining. stablyai#13309 routed live
cooked-echo-risk replies through the ECHO-probe deferral while CPR kept
the immediate path, so the CPR overtook the colour reply, the prober gave
up, and the stray `ESC ]` was left in the tty for the next program —
`gh auth login` died on it with an escape-sequence error.

Queue a reply that needs no echo containment behind ones that do, FIFO,
and only while something is actually deferred, so latency-critical
replies stay immediate on every other path. Windows is unaffected: only
posix-pty defers, so the queue is always empty there.

Also: an in-flight echo probe is already the write continuation, so
re-arming the timer for a queued reply would fork a second stty and throw
away the first verdict; and teardown now hands queued uncontained writes
to the pty best-effort instead of dropping bytes the caller was told were
sent.

Known scope limit, pinned by tests and tracked for follow-up: the
guarantee is FIFO among recognised query replies, not over every byte —
a reply coalesced with a keystroke, and ordinary typed input, still
bypass the queue. Both were unordered before this change too.
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…tablyai#13309)

Route cooked-echo-risk terminal replies through bounded echo-safe delivery across local, daemon, and SSH relay PTYs. Preserve repeated valid replies, bypass the daemon startup input gate, and keep ordinary input plus latency-critical replies on their existing paths.

Closes stablyai#13137

Co-authored-by: bbingz <[email protected]>
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…tablyai#15559)

A background-colour probe writes `OSC 11 ;? ST` then `CSI 6n` and reads
exactly one response, using the CPR as its sentinel: a non-OSC first
response means "unsupported" and it stops draining. stablyai#13309 routed live
cooked-echo-risk replies through the ECHO-probe deferral while CPR kept
the immediate path, so the CPR overtook the colour reply, the prober gave
up, and the stray `ESC ]` was left in the tty for the next program —
`gh auth login` died on it with an escape-sequence error.

Queue a reply that needs no echo containment behind ones that do, FIFO,
and only while something is actually deferred, so latency-critical
replies stay immediate on every other path. Windows is unaffected: only
posix-pty defers, so the queue is always empty there.

Also: an in-flight echo probe is already the write continuation, so
re-arming the timer for a queued reply would fork a second stty and throw
away the first verdict; and teardown now hands queued uncontained writes
to the pty best-effort instead of dropping bytes the caller was told were
sent.

Known scope limit, pinned by tests and tracked for follow-up: the
guarantee is FIFO among recognised query replies, not over every byte —
a reply coalesced with a keystroke, and ordinary typed input, still
bypass the queue. Both were unordered before this change too.
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