Skip to content

[Bug]: OSC/DSR query replies are reordered behind CPR/DA1 by the POSIX echo-safe write path, stranding them into the next child's stdin #13892

Closed

Description

@nwparker

Summary

On POSIX hosts, terminal query replies reach the pty out of the order the queries were observed. OSC responses and private-DSR replies are deferred 21–200 ms by the echo-safe delivery path added in #13309, while CPR and DA1 replies are written immediately.

Programs that use DA1 as a read sentinel — fish, and the idiom generally (neovim, kitty/ghostty capability detection, termwiz) — block until DA1 returns, consume everything ahead of it, then hand the tty to the next child. Any reply arriving after DA1 is stranded in the tty input queue and is read as stdin by that child.

This is a sibling of #9993 but a distinct mechanism, and it is deterministic, not racy.

The key finding: latency is not the failure mode — ordering is

Measured with real fish 4.7.1 under node-pty:

Reply emission Leak?
In query order, 0 ms no
In query order, uniform 200 ms delay no
Query order, OSC 11 delayed 1 ms relative to CPR/DA1 yes
Query order, OSC 11 delayed 5 / 20 / 200 ms yes (all)
DA1 delayed 50 ms (DA1 last) no

A 200 ms in-order reply is fine. A 1 ms out-of-order reply leaks.

Root cause

extractOnlyCookedEchoSafeQueryReplies (src/shared/terminal-query-reply.ts:100-115) admits only OSC responses and private DSR CSI ? … n. So per fish prompt paint:

Reply Path
\x1b]11;rgb:…\x1b\\ (OSC 11) echo-safe queue: 21–200 ms
\x1b[?997;1n (private DSR) echo-safe queue
\x1b[1;1R (CPR) immediate
\x1b[?1;2c (DA1) immediate

PtyStartupReplyDelivery.answer (src/shared/pty-startup-reply-delivery.ts:199-218) never writes directly on posix-pty; it queues and sets a deadline of ECHO_POLL_BUDGET_MS = 200 (:71). attemptPendingWrites (:290-329) polls stty — a subprocess per poll, measured min 21.2 / p50 21.7 / max 112.9 ms — and a deadline timer calls flushPendingWrites() unconditionally regardless of echo state. So when fish has already handed the tty to a cooked-mode child, the reply is held the full budget and then written into that child's stdin anyway.

Three call sites split identically: src/main/providers/local-pty-provider.ts:1082-1087, src/main/daemon/session.ts:247-252, src/relay/pty-handler.ts:1695-1700. Hidden/parked panes answer from main's headless emulator but sink through the same ptyController.write (src/main/runtime/orca-runtime.ts:11461-11478), so they invert identically — the renderer round trip is not required to reproduce this.

Reproduction

Verified against real fish + the real PtyStartupIngress / PtyStartupReplyDelivery / createPtySlaveEchoProbe imported from the worktree, with local-pty-provider.write()'s split reproduced verbatim (3/3 runs leaked):

[ 868.900ms] ORCA-WRITE   OSC11 -> ECHO-SAFE QUEUE "\e]11;rgb:2828/2c2c/3434\e\"
[ 868.944ms] ORCA-WRITE   CPR   -> immediate       "\e[1;1R"
[ 868.956ms] ORCA-WRITE   DA1   -> immediate       "\e[?1;2c"     <-- fish's sentinel arrives FIRST
[1079.316ms] INGRESS->PTY deferred write           "\e]11;rgb:..." <-- exactly +200.16ms
[3610.492ms] RESULT GOT-line "GOT: '\x1b]11;rgb:2828/2c2c/3434\x1b\\hello\n'"   LEAKED

Control run with --bypass-echo-safe: 3/3 clean, GOT: 'hello\n'.

User-visible impact is the same as #9993: brew / npx / git add -p confirmations swallow the escape bytes.

Proposed fix — order-preserving reply FIFO per PTY

Restore the invariant: replies reach the pty in the order the queries were observed. Not "answer faster" (200 ms in-order is fine) and not "answer closer to the tty" (main already does for hidden panes and still leaks).

  1. src/shared/pty-startup-reply-delivery.ts:199 — add a sibling to answer():

    /** Latency-critical replies that need no echo hold, but must not overtake a held one. */
    answerInOrder(reply: string): boolean {
      if (this.closed) return false
      if (!defersWrite(this.ownerBackend) || this.pendingWrites.length === 0) return this.writeReply(reply)
      this.pendingWrites.push({ reply, onFailed: undefined })  // no new deadline; rides the head's
      return true
    }

    flushPendingWrites() (:331-337) already drains FIFO, so nothing else changes.

  2. src/shared/pty-startup-ingress.ts:97-101 — route echo-risk replies via answer() and every other isTerminalQueryReply payload via answerInOrder().

  3. The three call sites above — admit plain CPR/DA/DECRPM/DCS replies into the FIFO instead of letting them jump it.

Validated by execution (--order-fifo, 3/3 clean): GOT: 'hello\n', no escape bytes.

Two properties worth noting:

Risk

Medium-narrow; every POSIX pty reply on three hosts. Main risk: CPR/DA1 inherit up to 200 ms latency whenever an OSC/private-DSR reply is pending for the same pty; a program with a sub-200 ms CPR timeout that also probes colors in the same burst could time out. Mitigation: consider a shorter live-reply budget (the 200 ms was sized for startup multi-pane restore, not steady state — see the rationale at pty-startup-reply-delivery.ts:59-70), or cap the in-order hold at one probe interval. Windows/ConPTY is unaffected (defersWrite() is posix-pty-only, :81-83).

Worth considering as a follow-up, not in the first fix: on budget expiry with the tty still echoing, the querying program has demonstrably left, so writing is strictly harmful. Dropping instead of flushing would close the residual window, but risks hanging a genuinely-cooked querier and needs its own evidence.

Notes

Made with Orca 🐋

Activity

  1. rhlsthrm commented on Aug 12, 2026

    @rhlsthrm

    A second user-visible impact of this ordering inversion, beyond brew/npx/git add -p swallowing bytes: TUI agents that use DA1 as the OSC 11 barrier resolve the wrong light/dark theme. Filed the symptom separately as #13908; adding the mechanism detail here since it bears on how the FIFO fix should be regression-tested.

    omp (oh-my-pi) 17.2.14 doesn't wait on a timeout for the color reply. It emits the query and a DA1 immediately after, and treats DA1's arrival as proof that nothing more is coming. From its shipped bundle (minified, names as emitted):

    #n0(){ this.#_.push({kind:"osc11"}), this.#o0("\x1B]11;?\x07"), this.#o0("\x1B[c") }

    The DA1 branch of its reply handler pops that pending osc11 entry and abandons the in-flight query.

    So for this class of prober, your table's distinction between "1 ms out-of-order" and "200 ms in-order" has a sharper consequence than leakage: a late reply is not slow, it is a negative answer. The prober cannot distinguish "deferred 21–200 ms behind DA1" from "terminal does not support OSC 11". No bytes need to be dropped or misread for the failure to land.

    Downstream, omp's fallback chain on macOS terminates in a constant:

    function ST0(){ return process.platform === "darwin" && !!Bun.env.ZELLIJ }
    function EKf(){
      if (!ST0() && nl) return nl;                          // OSC 11 result — absent
      let i = Bun.env.COLORFGBG || ""; /* … */              // unset in an Orca pane
      if (ST0()) { let n = Jc0 ?? Aon(); if (n) return n }  // detectMacOSAppearance, ZELLIJ-gated
      return "dark";                                        // hardcoded
    }

    Net effect on macOS in Light mode, in a light Orca pane: dark TUI. That's the same end state as #10492 reports on bundled ConPTY, reached through the POSIX path described here. (The ZELLIJ gate is an omp-side bug of its own — theirs to fix, not yours.)

    Attribution caveat, stated plainly: I verified omp's probe shape, its DA1-as-barrier handling, and its fallback chain by reading the installed bundle, and I verified macOS is in Light mode with COLORFGBG unset. I have not captured a byte-level ordering trace inside my own pane, so "this issue's mechanism causes that theme outcome" is inference from two code paths that fit — not a measured reproduction. Anyone with the node-pty harness from this issue can confirm or kill it quickly.

    Suggested addition to the fix's test surface: the proposed answerInOrder FIFO should be validated against a DA1-sentinel prober, asserting the OSC 11 reply is observed before DA1 — not only that no bytes strand into the next child's stdin. The fish repro exercises the leak surface but passes regardless of whether a prober that already gave up ever gets its answer in a usable order.

    Environment: Orca 1.4.180, macOS darwin 27.0.0 (Light), omp 17.2.14, no multiplexer, COLORFGBG unset in the pane shell.

  2. nwparker commented on Aug 12, 2026

    @nwparker
    ContributorAuthor

    I'm going to leave this open for a minute

    Appreciate if people share screenshots on how this affects them!

  3. AmethystLiang commented on Aug 12, 2026

    @AmethystLiang
    Contributor

    thanks for reporting. looking into it

  4. AmethystLiang commented on Aug 14, 2026

    @AmethystLiang
    Contributor

    Related report transferred from #14020 (closing as a duplicate).

    gh auth login on Linux (and independently on macOS 1.4.180 / gh 2.97.0) fails at the first prompt with:

    could not prompt: unexpected escape sequence from terminal: ['\x1b' ']']
    

    That is the user-visible shape of this issue: gh (via termenv) sends OSC 11 then CPR and treats the CPR reply as its read sentinel, so the deferred OSC 11 reply lands in the prompt reader as an unexpected ESC ]. Tracking continues here.

  5. yulin0629 commented on Aug 16, 2026

    @yulin0629

    Still reproducing on 1.4.183 (current Latest) — neither #13998 nor #14609 has landed yet. Adding a timing data point, since the reports above are on 1.4.180.

    Client: chezmoi 2.72.0 (Homebrew), another termenv v0.16.0 consumer. chezmoi status emits ESC ] 11 ; ? ESC \ then CSI 6n on startup.

    Measured from inside an Orca pane, raw mode, sending exactly that pair, 5 runs:

    +  52.98 ms  b'\x1b[9;1R'                           <- CPR (queried second)
    +  58.50 ms  b'\x1b]11;rgb:2828/2c2c/3434\x1b\\'    <- OSC 11 (queried first)
    
    +   2.67 ms  b'\x1b[9;1R'
    +   7.92 ms  b'\x1b]11;rgb:2828/2c2c/3434\x1b\\'
    
    +   3.99 ms  b'\x1b[9;1R'
    +  15.42 ms  b'\x1b]11;rgb:2828/2c2c/3434\x1b\\'
    
    +   2.82 ms  b'\x1b[9;1R'
    +   7.72 ms  b'\x1b]11;rgb:2828/2c2c/3434\x1b\\'
    
    +   4.12 ms  b'\x1b[9;1R'
    + 191.30 ms  b'\x1b]11;rgb:2828/2c2c/3434\x1b\\'
    

    CPR first in 5/5. Same script, same machine, Terminal.app: OSC 11 first in 5/5, 0.30–0.74 ms.

    The stranded reply is not just visible — it lands in the zsh edit buffer and runs:

    ~ ❯ 11;rgb:2828/2c2c/3434clear
    zsh: command not found: 11
    zsh: no such file or directory: rgb:2828/2c2c/3434clear
    

    (the clear I typed was swallowed into the leaked text)

    macOS Darwin 25.5.0 arm64, zsh, TERM=xterm-256color, plain (non-agent) pane.

  6. added theissue type on Sep 19, 2026
  7. nwparker commented on Sep 19, 2026

    @nwparker
    ContributorAuthor

    Current main contains the ordered live-query reply path. The shared regression suite explicitly models termenv/OMP behavior (OSC 11 then CPR) and proves replies are delivered [OSC 11, CPR], while reverse query order remains [CPR, OSC 11]. The focused Orca run passed 182/182, including real node-pty fish coverage and daemon/session reply paths. Closing as fixed; reopen if a current build reproduces it.

  8. nwparker commented on Sep 19, 2026

    @nwparker
    ContributorAuthor

    Fixed and covered by current query-reply ordering tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions