Repository navigation
fix(terminal): answer queries split across dropped chunks, and CPR from the snapshot that covers them - #27299
outof-place wants to merge 9 commits into
Conversation
…renderers can't pace agents A hidden pane whose PTY a raw-byte sidecar watches (background-launched and automation agents) kept the hidden-delivery gate off for the whole PTY, so its hidden xterm parsed every byte and main paced the producer to that parse. The parse runs on renderer timers, which Chromium throttles for hidden pages (one wake-up a minute after ~5 minutes), so a flooding agent slowed to tens of KB/s and blocked on its own writes. Delivery interest now only decides who receives the bytes, not whether the view is gated. For a hidden PTY with interest, main sends chunks flagged sidecarOnly. The renderer hands them to sidecars only and credits them on receipt. The view skips them like a gated drop and restores from the model on reveal, and main answers the PTY's terminal queries meanwhile. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…flip Main decides who answers a terminal query when it ingests the chunk: the pane's view while the view parses, main's model while the view is gated. Delivery decided again at send time, so a flip while the chunk was still queued broke that: - Queued while visible, then hidden before the flush: the gate dropped the bytes (or sent them to sidecars only), the view never parsed them, and main had not answered. The program waited for a reply that never came. - Queued while gated, then revealed before the flush: main had answered, and the now-visible view parsed the bytes and answered again. Each pending entry now carries the gate state it was ingested under, and an entry never mixes states. Bytes the view was to answer but will skip have their queries handed to the view the way a pending-cap drop already does. Bytes ingested gated go to sidecars only even after a reveal, and the view restores from main. Co-Authored-By: Claude Opus 5.5 <[email protected]>
rendererPtyViewDelivery now owns how main delivers a PTY's bytes to the
renderer ('parse', 'sidecarsOnly' or 'drop'). The drop and sidecar-only
predicates and the query responder read it, and every pending entry
records the mode it was ingested under instead of a gated bit. Main's
model owns the replies of any mode other than 'parse', so a later mode
such as parse-once's native-view skip slots into the same function and
the same stamp without another ownership rule.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The e2e holds the renderer's delivery credit while it fills main's window. Under heavy load the old 8 KB pacing could stretch the hold past main's 10 s ack-silence heal, which writes the window off and drains the queue the test needs. The filler now writes 16 KB per tick, still far below the queue size at which main pauses the PTY, and the probe writes its reply counts atomically. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Main fed every PTY byte into its own HeadlessEmulator, even for a visible pane whose renderer and daemon already parse the same bytes. For a visible local daemon terminal with no viewer, agent or gate drop, main now drops that emulator after 5 s without demand. A hide, a viewer, a screen read or a snapshot rebuilds it from the daemon's settled snapshot (protocol v36+), skipping live bytes the snapshot covers. The hidden-delivery gate stays open until the rebuilt model catches up, so main never owes a query reply for a byte it skipped. Restores of such terminals read the daemon at full depth. Phase 1 of #27023. Kill switch: terminalMainModelDormancy. Co-Authored-By: Claude Opus 5.5 <[email protected]>
An in-process consumer that reads main's emulator, or relies on main
answering a PTY's queries, can now hold the model live with
acquireTerminalOutputReader(ptyId, { kind: 'model' }) and release it when
done. A pin holds across a PTY respawn and releasing it restarts the
dormancy grace period. The reader kind leaves room for a later bytes-only
reader that does not need main's emulator.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Main hands a chunk to the view only when the view answers its queries; main answers none of them. The renderer still discarded such bytes without replies in several places: - a hidden view with a restore latched; - backlog bytes a restored snapshot already covers, fully or in part, both on arrival and in the restore queue's drain; - a seq gap that forces a fresh restore; - an unmappable hidden filter; - unparsed writes a snapshot apply or a backlog cap discards. A program that sent CPR or DA there waited forever. This mostly hits the dormancy handoff, where a hidden view answers until main's rebuilt model catches up, and the reveal restore after it. Every discard path now salvages the bytes' queries, the way the pending-cap and restore-queue overflow drops already do. As there, CPR reports the cursor where the view is, which keeps the program alive rather than being exact. The E2E test runs rounds of CPR, DA1 and DSR 5n across hiding and revealing a dormant pane, quiet and with output in flight. It checks for exactly one reply each, with the right cursor. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…om the snapshot that covers them The renderer salvages the queries in chunks it drops, but it scanned each dropped piece on its own. A CPR or DA split across two dropped pieces (one PTY read ending in "\e[6", the next starting with "n") parsed as nothing in either, and the program that sent it waited forever. The salvage now keeps the unfinished query a dropped piece ends in, with that piece's raw seq end. It joins the query to the next dropped piece only if that piece starts at exactly that seq; any other discard, or one without seq metadata, clears the carry. A query split at the snapshot seq itself is left alone: the snapshot's escape tail re-arms xterm's parser, and the written rest completes the query there. Queued writes that a snapshot apply discards used to be salvaged before the snapshot was written, so their CPR reported xterm's stale cursor. Those writes are older than the snapshot, so their CPR, DA1 and OSC color replies now wait until the replay has parsed and CPR reads the snapshot's cursor. A replay that aborts still sends them from its finally. Co-Authored-By: Claude Opus 5.5 <[email protected]>
📝 Walkthrough
Priority: ➖ Normal Merge Risk: 🔵 Low · up to Most hidden-output query handling works as intended. However, a terminal flooded repeatedly while in the background can lose replies to cursor or device queries after its first backlog drop. Programs that are waiting for those replies may stall. This is a small fix that can be made before or shortly after merge. Pre-merge checks |
|
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
- Configuration used: Repository UI
- Review profile: CHILL
- Plan: Advanced
- Run ID:
c90ca319-193b-4175-8404-9c200a50cce0
📒 Files selected for processing (67)
src/main/daemon/daemon-protocol-version.tssrc/main/daemon/daemon-pty-adapter-protocol-compatibility.test.tssrc/main/daemon/daemon-pty-router-test-fixture.tssrc/main/daemon/daemon-pty-router.test.tssrc/main/daemon/daemon-pty-router.tssrc/main/daemon/daemon-pty-runtime-state.tssrc/main/daemon/degraded-daemon-pty-provider.test.tssrc/main/daemon/degraded-daemon-pty-provider.tssrc/main/ipc/pty-buffer-snapshot-dispatch.test.tssrc/main/ipc/pty-hidden-delivery-gate.test.tssrc/main/ipc/pty-hidden-delivery-gate.tssrc/main/ipc/pty-ipc-hidden-delivery-flip-replies.test.tssrc/main/ipc/pty-ipc-hidden-delivery-gate.test.tssrc/main/ipc/pty-pending-data-drain-contract.tssrc/main/ipc/pty/delivery/accept.tssrc/main/ipc/pty/delivery/exit.tssrc/main/ipc/pty/delivery/flush.tssrc/main/ipc/pty/delivery/payload.tssrc/main/ipc/pty/delivery/pending-delivery-stamp.tssrc/main/ipc/pty/delivery/pending.tssrc/main/ipc/pty/delivery/wire-session.tssrc/main/ipc/pty/ipc/resize-visibility.tssrc/main/ipc/pty/ipc/snapshot.tssrc/main/ipc/pty/runtime/controller.tssrc/main/ipc/pty/session.tssrc/main/providers/pty-provider-contract.tssrc/main/runtime/main-terminal-model-dormancy.test.tssrc/main/runtime/main-terminal-model-dormancy.tssrc/main/runtime/orca-runtime-attach-remote-terminal-source-range-consumer.tssrc/main/runtime/orca-runtime-fit-override-listeners.tssrc/main/runtime/orca-runtime-main-terminal-model-dormancy.tssrc/main/runtime/orca-runtime-maybe-hydrate-headless-from-renderer.tssrc/main/runtime/orca-runtime-on-pty-data.tssrc/main/runtime/orca-runtime-on-pty-exit.tssrc/main/runtime/orca-runtime-serialize-headless-terminal-buffer.tssrc/main/runtime/orca-runtime-serialize-main-terminal-buffer.tssrc/main/runtime/orca-runtime-tests/main-terminal-model-dormancy.spec.tssrc/main/runtime/orca-runtime-verify-orchestration-compatibility-caller.tssrc/main/runtime/orca-runtime-visible-snapshot-preview.tssrc/main/runtime/orca-runtime.test.tssrc/main/runtime/runtime-pty-controller-contract.tssrc/main/runtime/runtime-store-contract.tssrc/main/runtime/runtime-terminal-state-records.tssrc/main/runtime/terminal-model-query-authority.test.tssrc/main/runtime/terminal-model-query-authority.tssrc/main/runtime/terminal-query-responder.test.tssrc/preload/api/pty-api.tssrc/preload/api/pty-bridge-stream-and-serialization.tssrc/renderer/src/components/terminal-pane/pty-connection-hidden-backlog-reconciliation.test.tssrc/renderer/src/components/terminal-pane/pty-connection/deferred-cold-restore-and-snapshot.tssrc/renderer/src/components/terminal-pane/pty-connection/hidden-output-restore-chunk.tssrc/renderer/src/components/terminal-pane/pty-connection/hidden-output-restore-drain.tssrc/renderer/src/components/terminal-pane/pty-connection/hidden-output-restore-snapshot.tssrc/renderer/src/components/terminal-pane/pty-connection/hidden-startup-renderer-query-write.tssrc/renderer/src/components/terminal-pane/pty-connection/live-data-callback.tssrc/renderer/src/components/terminal-pane/pty-connection/write-pty-output-to-xterm.tssrc/renderer/src/components/terminal-pane/pty-dispatcher-sidecar-only.test.tssrc/renderer/src/components/terminal-pane/pty-dispatcher.tssrc/renderer/src/components/terminal-pane/pty-sidecar-only-delivery.tssrc/renderer/src/lib/pane-manager/pane-terminal-output-queue-backlog.tssrc/renderer/src/lib/pane-manager/pane-terminal-output-queue-registry.tssrc/renderer/src/lib/pane-manager/pane-terminal-output-scheduler-discarded-queries.test.tssrc/shared/default-global-settings.tssrc/shared/global-settings-types.tstests/e2e/terminal-hidden-sidecar-pacing.spec.tstests/e2e/terminal-main-model-dormancy.spec.tstests/e2e/terminal-query-reply-visibility-flip.spec.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
| break | ||
| } | ||
| } | ||
| const droppedData = shouldNotify ? queuedTerminalOutputData(entry) : '' |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
#!/bin/bash
rg -nP -C4 'backgroundBacklogDropped\s*=' src/renderer/src/lib/pane-manager
rg -nP -C3 '\breplaceBacklogWithWarning\s*\(' src/renderer/src/lib/pane-managerRepository: stablyai/orca
Length of output: 4890
🏁 Script executed:
#!/bin/bash
set -u
printf '%s\n' '--- backlog helper ---'
nl -ba src/renderer/src/lib/pane-manager/pane-terminal-output-queue-backlog.ts | sed -n '1,130p'
printf '%s\n' '--- writer relevant declarations and calls ---'
nl -ba src/renderer/src/lib/pane-manager/pane-terminal-output-writer.ts | sed -n '1,270p'
printf '%s\n' '--- callback definitions/usages ---'
rg -n -F --glob '*.ts' -- 'onBackgroundBacklogDropped' src/renderer/src/lib/pane-manager src/renderer/src | head -80Repository: stablyai/orca
Length of output: 19958
🏁 Script executed:
#!/bin/bash
set -u
printf '%s\n' '--- queue registry ---'
nl -ba src/renderer/src/lib/pane-manager/pane-terminal-output-queue-registry.ts | sed -n '1,130p'
printf '%s\n' '--- PTY callback consumer ---'
nl -ba src/renderer/src/components/terminal-pane/pty-connection/write-pty-output-to-xterm.ts | sed -n '70,145p'
printf '%s\n' '--- discarded-query tests ---'
nl -ba src/renderer/src/lib/pane-manager/pane-terminal-output-scheduler-discarded-queries.test.ts | sed -n '1,130p'
printf '%s\n' '--- query-related callback/parser references ---'
rg -n -P -C4 'discarded|BackgroundBacklog|\\x1b\\[6n|\\x1b\\[c|onBackgroundBacklogDropped|queuedTerminalOutputData' src/renderer/src/components/terminal-pane/pty-connection src/renderer/src/lib/pane-managerRepository: stablyai/orca
Length of output: 14200
🏁 Script executed:
#!/bin/bash
set -u
printf '%s\n' '--- queued data helper ---'
rg -n -F -- 'function queuedTerminalOutputData' src/renderer/src/lib/pane-manager
rg -n -F -C8 -- 'queuedTerminalOutputData' src/renderer/src/lib/pane-manager/pane-terminal-output-queue-registry.ts
printf '%s\n' '--- salvage implementation ---'
rg -n -F -- 'salvageRendererQueriesFromDiscardedRestoreData' src/renderer/src
printf '%s\n' '--- query salvage identifiers ---'
rg -n -F -C6 -- 'salvageRendererQueries' src/renderer/src
rg -n -F -C6 -- 'RendererQueriesFromDiscarded' src/renderer/srcRepository: stablyai/orca
Length of output: 41520
🏁 Script executed:
nl -ba src/renderer/src/components/terminal-pane/pty-connection/hidden-startup-renderer-query-write.ts | sed -n '115,245p'Repository: stablyai/orca
Length of output: 4630
🏁 Script executed:
set -u
rg -n -F -- 'function extractHiddenStartupRendererQueryData' src/renderer/src
rg -n -F -C8 -- 'extractHiddenStartupRendererQueryData' src/renderer/src/components/terminal-pane/pty-connectionRepository: stablyai/orca
Length of output: 8399
🏁 Script executed:
set -u
nl -ba src/shared/terminal-reply-query-extraction.ts | sed -n '1,260p'Repository: stablyai/orca
Length of output: 6071
🏁 Script executed:
set -u
printf '%s\n' '--- enqueue helper ---'
rg -n -F -- 'function enqueueChunk' src/renderer/src/lib/pane-manager
rg -n -F -C12 -- 'enqueueChunk' src/renderer/src/lib/pane-manager/pane-terminal-output-queue-chunks.ts
printf '%s\n' '--- backlog-drop flag assignments ---'
rg -n -F -- 'backgroundBacklogDropped' src/renderer/src/lib/pane-managerRepository: stablyai/orca
Length of output: 1813
🏁 Script executed:
nl -ba src/renderer/src/lib/pane-manager/pane-terminal-output-queue-chunks.ts | sed -n '178,245p'Repository: stablyai/orca
Length of output: 1580
Preserve dropped bytes on every backlog replacement.
After the first overflow, later writes remain queued and can trigger another cap check while backgroundBacklogDropped is already true. The current code then discards those bytes without invoking the query-salvage callback. A dropped \x1b[6n or \x1b[c can therefore receive no reply.
Keep the breadcrumb and debug counter gated by shouldNotify, but collect and forward every non-empty dropped payload.
Suggested fix
- const droppedData = shouldNotify ? queuedTerminalOutputData(entry) : ''
+ const droppedData = queuedTerminalOutputData(entry)
...
- if (shouldNotify) {
+ if (droppedData) {
entry.onBackgroundBacklogDropped?.(droppedData)
}
ELI5
Programs in a terminal sometimes ask the terminal a question, such as "where is the cursor?", and wait for the answer. When Orca throws away output it no longer needs to draw (because a fresh snapshot of the screen replaces it), it still answers the questions in that output. It missed two cases: a question cut in half between two thrown-away pieces got no answer at all, so the program hung, and a cursor question answered while a snapshot was being applied got the old cursor position instead of the one the snapshot shows.
What Changed
Before:
\e[6, the next starting withn) went unanswered. Each piece was scanned on its own, and neither held a whole query.After:
finallystill sends them.The mechanism:
salvageRendererQueriesFromDiscardedRestoreData(data, range?, cursorAfter?)inhidden-startup-renderer-query-write.ts.rangeis the dropped bytes' raw seq span. It feeds the existingpendinginput ofextractHiddenStartupRendererQueryData, which already carries unfinished queries for hidden startup output.cursorAfterholds the direct replies (OSC color, CPR, DA1) until the promise resolves. Rarer queries still replay into xterm at once, as before.range: drop-duplicate and the covered prefix after a restore, the restore drain's covered chunks, and its refetch and overflow discards.applyMainBufferSnapshotpasses a promise that resolves afterwaitForTerminalReplayWritesParsed, or in itsfinally.A query split at the snapshot seq itself is left as it was. The snapshot then ends mid-escape, so it carries
pendingEscapeTailAnsi. Its replay re-arms xterm's parser with the first half, and the written rest of the chunk completes the query in xterm. Also answering it from the covered prefix would answer it twice.Why
A lost reply leaves the program that asked waiting for it, until its own timeout if it has one. A query is a few bytes, so a PTY read boundary inside one is rare. It does happen under load, and the dropped-chunk paths run mostly under load: floods and restores.
Alternatives considered:
Stacked on #27086
This branch sits on #27086 (b814bec), which added the salvage at every discard; that sits on #27129 and #27033. The diff here includes their commits until they land. This PR's own change is the last commit.
Linked Issue
Part of #27023 (follow-up to #27086's query salvage)
Visual Proof
N/A: no UI change. What changes is the reply a program gets to a terminal query in dropped output, which the tests below check.
Testing
New tests in
pty-connection-hidden-backlog-reconciliation.test.ts. The first three fail on b814bec, and the other two pass on both:one\e[6(seq 44–50) andntwo(50–54), both covered by a snapshot at 63, gets exactly one CPR reply. Before: none.\e[6nis still queued when the snapshot applies, and the snapshot moves the cursor to row 6, column 8. The reply is exactly\e[6;8R. Before:\e[1;1R, the stale cursor.one\e[6ends at 50,ntwostarts at 52, so there is no reply.one\e[6ntwo, and the snapshot at 66 carries the tail\e[6. There is no salvage reply, andntwois written after the replayed tail.Unit suites on macOS at bc9b0ec (
pnpm test --maxWorkers=4 src/renderer/src/components/terminal-pane src/renderer/src/lib/pane-manager): 6,077 pass, 2 skipped.pnpm tcand the changed-code quality gate (ORCA_CODE_QUALITY_BASE=b814beccf7 pnpm run check:code-quality:changed) pass.Not run: the full
pnpm test,pnpm lint,pnpm build, and the E2E suites (left to CI). No E2E was added: a query split across two PTY reads cannot be placed on demand in a real PTY, so the unit tests drive the renderer's data path with chosen seqs instead.I manually tested these changes locally (not done: only the unit tests and checks listed above ran)
Automated tests added/updated, or explained why not below
AI Disclosure
Implemented with Claude Opus 5.5 via Claude Code: design, code, tests and measurements. A human (outof-place) directed the work.
Review
Suggested reading order:
salvageRendererQueriesFromDiscardedRestoreDatainhidden-startup-renderer-query-write.ts: the seq-adjacent carry andcursorAfter.rangearguments at the discard sites:live-data-callback.ts,hidden-output-restore-drain.tsandhidden-output-restore-chunk.ts.discardedRepliesReadyinhidden-output-restore-snapshot.ts.Agent skill upstream boundary
Trade-offs
The deferred replies are released after the replay parses, or in the apply's
finallyif it aborts, so a thrown replay cannot leave them unsent.@xterm/headlessthat parse takes 2.6 ms for a 1,000-row replay and 10.3 ms for 10,000 rows (120 columns, median of 7, host load 17–20). The renderer's xterm parses the same bytes; the wait does not include painting. Every other salvage path replies as before.Notes
Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (or CI will cover; local preferred)🤖 Generated with Claude Code