Repository navigation
perf(terminal): let the daemon answer hidden local terminals' queries - #27123
outof-place wants to merge 11 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]>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Priority: ➖ Normal Merge Risk: 🟡 Moderate · up to A reattached hidden terminal can miss query replies, and generation resets or repeated backlog drops can leave stale query behavior or lose replies. Resolve these bounded terminal issues before merging. Pre-merge checks |
|
There was a problem hiding this comment.
Actionable comments posted: 2
ℹ️ Review info
⚙️ Run configuration
- Configuration used: Repository UI
- Review profile: CHILL
- Plan: Advanced
- Run ID:
10af7c2e-943f-4850-a85d-a128762673d2
📥 Commits
Reviewing files that changed from the base of the PR and between 4ec973f and 7c9aa6264266e9db3f7403215883dd62cbc9ada5.
📒 Files selected for processing (77)
src/main/daemon/daemon-foreground-confirmation-protocol.test.tssrc/main/daemon/daemon-protocol-version.tssrc/main/daemon/daemon-pty-adapter-protocol-compatibility.test.tssrc/main/daemon/daemon-pty-adapter-query-responder.test.tssrc/main/daemon/daemon-pty-adapter.tssrc/main/daemon/daemon-pty-connection-lifecycle.tssrc/main/daemon/daemon-pty-owner-query-protocol.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/daemon-query-responder-routing.test.tssrc/main/daemon/daemon-request-router.tssrc/main/daemon/daemon-session-background-routing.tssrc/main/daemon/daemon-stream-events.tssrc/main/daemon/daemon-view-attributes.tssrc/main/daemon/degraded-daemon-pty-provider.test.tssrc/main/daemon/degraded-daemon-pty-provider.tssrc/main/daemon/headless-emulator.tssrc/main/daemon/session-output-pipeline.tssrc/main/daemon/session-output-plane.tssrc/main/daemon/session-query-responder.test.tssrc/main/daemon/session.tssrc/main/daemon/terminal-host-session-inspection-operations.tssrc/main/daemon/terminal-host.tssrc/main/daemon/types.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-daemon-query-responder.test.tssrc/main/ipc/pty-ipc-hidden-delivery-gate.test.tssrc/main/ipc/pty-ipc-provider-fixtures.tssrc/main/ipc/pty/delivery/debug.tssrc/main/ipc/pty/delivery/payload.tssrc/main/ipc/pty/delivery/wire-session.tssrc/main/ipc/pty/ipc/snapshot.tssrc/main/ipc/pty/ipc/terminal-view-attributes-ipc.test.tssrc/main/ipc/pty/ipc/terminal-view-attributes-ipc.tssrc/main/ipc/pty/provider/bind-listeners.tssrc/main/ipc/pty/provider/registry.tssrc/main/ipc/pty/runtime/controller.tssrc/main/ipc/pty/session.tssrc/main/providers/pty-provider-contract.tssrc/main/providers/pty-provider-events.tssrc/main/runtime/daemon-query-responder-delegation.test.tssrc/main/runtime/daemon-query-responder-delegation.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-session-control.tssrc/preload/api/pty-bridge-stream-and-serialization.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/web/preload-api/web-terminal-api.tssrc/shared/default-global-settings.tssrc/shared/global-settings-types.tstests/e2e/terminal-hidden-sidecar-pacing.spec.tstests/e2e/terminal-main-model-dormancy.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.
| attachClient(client: Omit<AttachedClient, 'token'>): symbol { | ||
| // Why: a delegation outlives a quit main; the attaching one starts as responder and delegates again. | ||
| this.answersQueries = false |
There was a problem hiding this comment.
🩺 Stability & Availability | 🟠 Major | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
#!/bin/bash
rg -nP -C4 'attachClient\(' src/main/daemon --glob '!**/*.test.ts'Repository: stablyai/orca
Length of output: 3241
🏁 Script executed:
#!/bin/bash
set -e
printf '%s\n' '--- session-output-plane.ts ---'
nl -ba src/main/daemon/session-output-plane.ts | sed -n '1,240p'
printf '%s\n' '--- terminal-host-session-create.ts ---'
nl -ba src/main/daemon/terminal-host-session-create.ts | sed -n '1,230p'
printf '%s\n' '--- responder and attach references ---'
rg -n -F --glob '*.ts' --glob '!**/*.test.ts' -e 'sessionQueryResponderMarker' -e 'releaseQueryResponderSessions' -e 'setQueryResponder(' -e 'createOrAttach' src/mainRepository: stablyai/orca
Length of output: 28330
🏁 Script executed:
#!/bin/bash
set -e
printf '%s\n' '--- session attach/detach and responder methods ---'
nl -ba src/main/daemon/session.ts | sed -n '170,225p'
nl -ba src/main/daemon/daemon-pty-adapter.ts | sed -n '1,90p'
nl -ba src/main/daemon/daemon-pty-connection-lifecycle.ts | sed -n '1,135p'
printf '%s\n' '--- background routing marker path ---'
nl -ba src/main/daemon/daemon-session-background-routing.ts | sed -n '1,125p'
printf '%s\n' '--- all detachAllClients references ---'
rg -n -F --glob '*.ts' --glob '!**/*.test.ts' 'detachAllClients' src/mainRepository: stablyai/orca
Length of output: 17837
🏁 Script executed:
#!/bin/bash
set -e
printf '%s\n' '--- main-side responder calls and attach flow ---'
rg -n -F --glob '*.ts' --glob '!**/*.test.ts' -e 'setSessionQueryResponder(' -e 'queryResponderMarker' -e 'createOrAttach(' src/main
printf '%s\n' '--- changed hunk versus supplied merge base ---'
git diff --unified=12 9d94a20f84d10641f217b9539b0aefeff8d9b1ed 7c9aa6264266e9db3f7403215883dd62cbc9ada5 -- src/main/daemon/session-output-plane.ts src/main/daemon/terminal-host-session-create.ts src/main/daemon/session.tsRepository: stablyai/orca
Length of output: 13938
Emit a false responder marker when an existing session is reattached.
createOrAttachTerminalSession detaches all clients and then calls attachClient for an existing session. attachClient sets answersQueries to false, but this path emits no sessionQueryResponderMarker. Main can still believe that the daemon answers queries, so neither side answers subsequent queries.
When an existing attach clears delegation, enqueue an ordered responder: false marker. Do not rely only on fresh-connection cleanup.
| forget(ptyId: string): void { | ||
| const entry = this.entries.get(ptyId) | ||
| if (entry) { | ||
| this.clearTimer(entry) | ||
| this.entries.delete(ptyId) | ||
| } | ||
| } |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
#!/bin/bash
# Check whether exit/respawn paths clear daemonHandoffPtys elsewhere.
rg -nP -C3 'daemonHandoffPtys\.(delete|clear)|setHiddenDeliveryDaemonHandoff\(' src/mainRepository: stablyai/orca
Length of output: 5901
🏁 Script executed:
set -e
printf '%s\n' '--- daemon-query-responder-delegation.ts ---'
nl -ba src/main/runtime/daemon-query-responder-delegation.ts | sed -n '1,180p'
printf '%s\n' '--- runtime host binding ---'
nl -ba src/main/runtime/orca-runtime-main-terminal-model-dormancy.ts | sed -n '80,145p'
printf '%s\n' '--- hidden delivery gate cleanup and relevant predicates ---'
nl -ba src/main/ipc/pty-hidden-delivery-gate.ts | sed -n '1,175p'
nl -ba src/main/ipc/pty-hidden-delivery-gate.ts | sed -n '230,310p'
printf '%s\n' '--- delegation call sites ---'
rg -n -F --glob '*.ts' -- 'daemonQueryResponderDelegation.' src/mainRepository: stablyai/orca
Length of output: 23629
🏁 Script executed:
set -e
printf '%s\n' '--- PTY exit handler ---'
nl -ba src/main/runtime/orca-runtime-on-pty-exit.ts | sed -n '100,165p'
printf '%s\n' '--- clearHiddenRendererPtyDeliveryState callers ---'
rg -n -F --glob '*.ts' -- 'clearHiddenRendererPtyDeliveryState(' src/main
printf '%s\n' '--- lifecycle generation definitions and updates ---'
rg -n -F --glob '*.ts' -- 'getPtyLifecycleGeneration' src/main
rg -n -F --glob '*.ts' -- 'lifecycleGeneration' src/main/runtime src/main/ipc
printf '%s\n' '--- provider PTY teardown references ---'
rg -n -F --glob '*.ts' -- 'clearProviderPtyState' src/mainRepository: stablyai/orca
Length of output: 26888
🏁 Script executed:
set -e
printf '%s\n' '--- provider state cleanup ---'
nl -ba src/main/ipc/pty/provider/state-cleanup.ts | sed -n '1,80p'
printf '%s\n' '--- lifecycle-generation storage and mutations ---'
rg -n -C4 -F --glob '*.ts' -- 'ptyLifecycleGenerationById' src/main/runtime
printf '%s\n' '--- generation mutation operators ---'
rg -n -P -C3 --glob '*.ts' 'ptyLifecycleGenerationById[^\\n]*(\\+\\+|\\+=|set\\(|delete\\()' src/main/runtime
printf '%s\n' '--- PTY exit registration/callers ---'
rg -n -C4 --glob '*.ts' -e 'onPtyExit' -e 'register.*Pty.*Exit' src/main/runtime src/main/ipc/ptyRepository: stablyai/orca
Length of output: 14946
🏁 Script executed:
set -e
printf '%s\n' '--- provider state cleanup ---'
nl -ba src/main/ipc/pty/provider/state-cleanup.ts | sed -n '1,80p'
printf '%s\n' '--- lifecycle-generation storage and mutations ---'
rg -n -C4 -F --glob '*.ts' -- 'ptyLifecycleGenerationById' src/main/runtime
printf '%s\n' '--- generation mutation operators ---'
rg -n -P -C3 --glob '*.ts' 'ptyLifecycleGenerationById[^\n]*(\+\+|\+=|set\(|delete\()' src/main/runtime
printf '%s\n' '--- PTY exit registration/callers ---'
rg -n -C4 --glob '*.ts' -e 'onPtyExit' -e 'register.*Pty.*Exit' src/main/runtime src/main/ipc/ptyRepository: stablyai/orca
Length of output: 41969
🏁 Script executed:
set -e
printf '%s\n' '--- generation advancement callers ---'
rg -n -C6 -F --glob '*.ts' -- 'advancePtyLifecycleGeneration(' src/main
printf '%s\n' '--- PTY spawn hooks ---'
rg -n -C8 --glob '*.ts' -e 'onPtySpawned' -e 'registerPty\(' src/main/runtime/orca-runtime-on-pty-spawned.ts src/main/runtime src/main/ipc/pty | head -n 240
printf '%s\n' '--- cleanup tail and spawn registration ---'
nl -ba src/main/ipc/pty/provider/state-cleanup.ts | sed -n '80,145p'
rg -n -C6 -F --glob '*.ts' -- 'clearProviderPtyState(id)' src/main/ipc/pty/providerRepository: stablyai/orca
Length of output: 32273
🏁 Script executed:
set -e
printf '%s\n' '--- provider sequence reset block ---'
nl -ba src/main/runtime/orca-runtime-record-agent-prompt-lifecycle-state.ts | sed -n '108,155p'
printf '%s\n' '--- delegation tests ---'
nl -ba src/main/runtime/daemon-query-responder-delegation.test.ts | sed -n '1,260p'Repository: stablyai/orca
Length of output: 8394
🏁 Script executed:
set -e
rg -n -C8 -F --glob '*.ts' -- 'disposeHeadlessTerminal' src/main/runtime
rg -n -C4 -F --glob '*.ts' -- 'setHiddenDeliveryDaemonHandoff' src/main/runtime src/main/ipcRepository: stablyai/orca
Length of output: 33526
Clear the handoff latch when forgetting a requested generation.
A provider sequence reset can invalidate the entry without running PTY teardown. currentEntry() then calls forget(), which currently removes the entry but leaves the hidden-delivery daemon handoff pending. The hidden view can therefore continue answering and parsing bytes for the new generation.
Suggested fix
--- "a/src/main/runtime/daemon-query-responder-delegation.ts"
+++ "b/src/main/runtime/daemon-query-responder-delegation.ts"
@@ -89,13 +89,16 @@
}
}
forget(ptyId: string): void {
const entry = this.entries.get(ptyId)
if (entry) {
this.clearTimer(entry)
+ if (entry.requested) {
+ this.host.setHandoffPending(ptyId, false)
+ }
this.entries.delete(ptyId)
}
}
private request(ptyId: string, entry: DelegationEntry): void {
if (entry.gaveUp || !this.sendTracked(ptyId, entry, true)) {📝 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.
| forget(ptyId: string): void { | |
| const entry = this.entries.get(ptyId) | |
| if (entry) { | |
| this.clearTimer(entry) | |
| this.entries.delete(ptyId) | |
| } | |
| } | |
| forget(ptyId: string): void { | |
| const entry = this.entries.get(ptyId) | |
| if (entry) { | |
| this.clearTimer(entry) | |
| if (entry.requested) { | |
| this.host.setHandoffPending(ptyId, false) | |
| } | |
| this.entries.delete(ptyId) | |
| } | |
| } |
…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]>
which keeps that model parsing every hidden pane. A v45 daemon now answers them instead: main delegates with setSessionQueryResponder, the daemon flags its emulator writes and queues an in-order sessionQueryResponderMarker on the client stream, and main flips the delivery gate only at that marker. resolveTerminalQueryResponder picks exactly one of view, main or daemon per live chunk. Until the daemon confirms, main's live model keeps answering (the view does only when main's model is already dormant); after a take-back the view stays gated, and its reveal restore is deferred, until the daemon's false marker. Main takes the role back (and wakes its model) for agents, remote viewers, model readers, the kill switch, unconfirmed requests and daemon reattaches. Main pushes the renderer's view attributes so OSC 4/12, ?996n and cursor replies match. Old daemons, degraded routes, SSH, relay and agent panes keep main as the responder. Kill switch: terminalDaemonQueryAuthority. Co-Authored-By: Claude Opus 5.5 <[email protected]>
… caught up When a hidden, delegated PTY stops qualifying (an agent starts, a reader registers, the kill switch flips), main takes its queries back. If main's model was dormant, the take-back used to land before the rebuilt model had caught up. The hidden view then had to answer in between, and a view with a latched restore does not parse. Main now wakes its model first and sends the take-back only once that model has caught up. The daemon keeps answering until then, so main answers from an exact model the moment the daemon lets go. A remote viewer still takes over at once, since main will not answer for it. The handoff E2E now runs with the daemon answering and with main answering. Co-Authored-By: Claude Opus 5.5 <[email protected]>
7c9aa62 to
bff7c7f
Compare
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:
e5b07f78-f496-42cc-bbc7-7b0d096f8981
📥 Commits
Reviewing files that changed from the base of the PR and between 7c9aa6264266e9db3f7403215883dd62cbc9ada5 and bff7c7f.
📒 Files selected for processing (26)
src/main/ipc/pty-hidden-delivery-gate.tssrc/main/ipc/pty-ipc-hidden-delivery-flip-replies.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/ipc/resize-visibility.tssrc/main/runtime/daemon-query-responder-delegation.test.tssrc/main/runtime/daemon-query-responder-delegation.tssrc/main/runtime/main-terminal-model-dormancy.tssrc/main/runtime/orca-runtime-main-terminal-model-dormancy.tssrc/main/runtime/orca-runtime-tests/main-terminal-model-dormancy.spec.tssrc/main/runtime/terminal-model-query-authority.tssrc/renderer/src/components/terminal-pane/pty-connection-hidden-backlog-reconciliation.test.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/live-data-callback.tssrc/renderer/src/components/terminal-pane/pty-connection/write-pty-output-to-xterm.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.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; 7 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
# Find callers of replaceBacklogWithWarning and where backgroundBacklogDropped resets.
rg -n -C4 'replaceBacklogWithWarning\s*\(|backgroundBacklogDropped\s*=' src/renderer/src/lib/pane-managerRepository: stablyai/orca
Length of output: 5955
🏁 Script executed:
#!/bin/bash
set -e
printf '%s\n' '--- backlog module ---'
nl -ba src/renderer/src/lib/pane-manager/pane-terminal-output-queue-backlog.ts
printf '%s\n' '--- writer relevant blocks ---'
nl -ba src/renderer/src/lib/pane-manager/pane-terminal-output-writer.ts | sed -n '1,280p'
printf '%s\n' '--- all state references in pane-manager ---'
rg -n -C5 -F -- 'backgroundBacklogDropped' src/renderer/src/lib/pane-managerRepository: stablyai/orca
Length of output: 23759
🏁 Script executed:
#!/bin/bash
set -e
printf '%s\n' '--- callback consumers ---'
rg -n -C6 -F -- 'onBackgroundBacklogDropped' src
printf '%s\n' '--- flusher ---'
nl -ba src/renderer/src/lib/pane-manager/pane-terminal-output-flusher.ts
printf '%s\n' '--- recovery symbol references ---'
rg -n -C5 'requestRegisteredTerminalBacklogRecovery|background backlog|backlog recovery' src/renderer/src/libRepository: stablyai/orca
Length of output: 28805
Salvage query bytes on every backlog replacement.
backgroundBacklogDropped stays true until the queue entry is discarded. A later write can exceed the cap before that happens. The later replacement captures no bytes and does not call onBackgroundBacklogDropped.
Split the first-drop restore notification from query salvage. Pass the discarded bytes to the salvage path on every replacement.
…tract This PR raises PROTOCOL_VERSION to 45, so the packaged local-build contract has to name 45 as the daemon protocol and list it as attachable. local-build-compatibility-contract.test.ts failed without it. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Closing as not planned. The proposal explicitly admits a remote-attach handover window in which both the daemon and remote viewer can answer an already-emitted query, injecting duplicate replies as program input. It also inherits model rebuild/history trade-offs and adds a responder protocol/state machine. Real CPU savings do not meet our zero-negative-trade-off and low-risk bar here. |
ELI5
Terminal programs sometimes ask the terminal a question, for example "where is my cursor?", and wait for the answer. When a pane is hidden, nobody on screen can answer, so Orca's main process keeps its own copy of the terminal just to answer those questions. The terminal daemon already keeps an identical copy. With this PR, main asks the daemon to answer for hidden panes, and main can then drop its own copy, as #27086 lets it do for visible panes.
What Changed
Before: #27033 made main the only party that answers a view-gated (hidden) pane's terminal queries. To do that, main had to keep parsing every hidden pane's bytes, including panes that print a lot (builds, logs, dev servers).
After: for a hidden local daemon pane, the daemon's own emulator answers the queries, and main's emulator for that pane can go dormant. Answers do not change: the daemon uses the same emulator class and the same responder modules as main:
5n, CPR6n/?6n, DECRQM, DECRQSS, XTVERSION, kittyCSI ? u;?996n, answered from the attributes main now pushes to the daemon;Replies go through
Session.write, so the existing startup-ingress dedupe and the shell-ready queue apply to them unchanged.The mechanism:
Capability. Daemon protocol v45 adds two control notifications,
setTerminalViewAttributesandsetSessionQueryResponder, and one stream event,sessionQueryResponderMarker. Main sends the notifications only to a v45 daemon. Sessions on an older preserved daemon keep main as the responder.Exactly one responder per byte.
resolveTerminalQueryResponderreturnsview,mainordaemonfor each live chunk, at ingestion. It replaces the boolean check from perf(terminal): send hidden sidecar bytes past the view so throttled renderers can't pace agents #27033.Hiding a pane. Main sends the request. Until the daemon's
truemarker arrives, main's emulator keeps answering. If main's emulator is already dormant, the view stays ungated and answers instead. After the marker, the daemon answers, and main's emulator may go dormant under the rules from perf(terminal): let main skip parsing visible local daemon terminals #27086.Revealing a pane. Main sends the take-back. The view stays gated until the daemon's
falsemarker arrives, because the daemon still answers until that point. The reveal restore is deferred to that marker, so the restore snapshot covers every byte the view skipped.Taking replies back while hidden. Main takes the queries back and wakes its emulator when any of these happens:
If main's emulator is dormant, main wakes it first and sends the take-back only once it has caught up, so the daemon answers until then and main answers from an exact model right after the
falsemarker. A remote viewer takes over at once, since main won't answer for it.No confirmation. If the
truemarker does not arrive within 2 s, main takes the role back and keeps answering for that PTY generation. A daemon reattach drops delegations; the adapter reports that as afalsemarker, and main asks again.Kill switch. The hidden setting
terminalDaemonQueryAuthoritydefaults to on.These PTYs keep main as the responder for hidden panes, as today:
Why
Hidden panes include background builds, logs and dev servers, which can print a lot, and #27033 pinned main's emulator to all of them. Main's emulator runs on the main thread, alongside IPC and keystroke forwarding (#27023). The daemon parses every byte anyway, so letting it answer costs it almost nothing extra.
Measured with a headless E2E on a minified build. Each run hides a pane,
cats a 16 MiB agent-TUI fixture into it, and records main'sprocess.cpuUsage()until main goes quiet. I interleaved runs with the kill switch on and off on the same build: n=10 each, on a loaded host (load average 55–65 on 16 cores when checked in the same session; not recorded per run).terminalDaemonQueryAuthorityoffThat is −13.4 ms/MB (−28%). The daemon's keep-tail thinning applies to both arms, because a hidden session is backgrounded.
Alternatives considered:
Stacked on #27086
This branch sits on #27086 (main model dormancy), which sits on #27129 and #27033. The diff here includes their commits until they land.
Linked Issue
Part of #27023 (phase 2). The wire design was posted there first: #27023 (comment)
Visual Proof
N/A: no UI or behavior change. Query replies, output and reveal restores stay the same, and the new E2E test checks all three.
Testing
Daemon tests:
Sessionanswers only while delegated;Main-side tests:
falsemarker and then gets exactly one restore.Runtime tests:
E2E, headless:
Handoff E2E (from perf(terminal): let main skip parsing visible local daemon terminals #27086), with the daemon answering and with main answering (kill switch off), quiet and with output in flight. It runs 60 rounds of CPR, DA1 and DSR 5n across hiding and revealing a dormant pane, and checks for exactly one reply of each kind with the right cursor. All runs exact: 12 of 12 for the dormancy file with
--repeat-each=2before the rebase onto fix(terminal): keep each PTY query's reply owner across a visibility flip #27129's latest head, and 12 of 12 after it.Regression E2E, headless: 13 spec files, 26 tests:
Three full runs passed 26 of 26. A run between the first two, under the same parallel load, failed two tests: one park and reveal cycle missed its frame, and the flapping fixture stopped streaming. Both passed when run alone, and in every later run.
After the rebase onto fix(terminal): keep each PTY query's reply owner across a visibility flip #27129's latest head: the dormancy, visibility-flip, sidecar-pacing, OSC colour query, hidden view parking and hidden-pane TUI restore files passed 36 of 36 with
--repeat-each=2.check:daemon-protocol-crossing: upgrading from v1.4.223 (protocol 41) adopts live terminals.pnpm tcand the changed-code quality gate (ORCA_CODE_QUALITY_BASE=upstream/main pnpm run check:code-quality:changed) pass. The fullpnpm test,pnpm lintandpnpm buildwere left to CI.Unit suites for
src/main/daemon,src/main/ipc,src/main/runtime,src/main/providersand the renderer's terminal pane and pane manager: 26,784 pass. The 18 failures fail the same way on the base commit in this environment: 16 spawn a real zsh (repro-13767,shell-ready) and 2 are inrun-terminal-shell-alone.node-pty.test.ts.I manually tested these changes locally (not done: only the E2E runs and tests 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 and tests. A human (outof-place) directed the work.
Review
Suggested reading order:
resolveTerminalQueryResponderinterminal-model-query-authority.ts, the one responder function.pty-hidden-delivery-gate.ts.daemon-query-responder-delegation.ts, the main-side state machine.Session.setQueryResponder,SessionOutputPlane.setQueryResponder, and the marker indaemon-session-background-routing.ts.Agent skill upstream boundary
Trade-offs
check:daemon-protocol-crossingcovers an upgrade from protocol 41);terminalDaemonQueryAuthority: falsekeeps main as the responder;--expose-gcas heap plus array buffers. The daemon keeps one responder flag per session and one copy of the pushed view attributes (palette, cursor style, color scheme) for all sessions, not per session. In perf(terminal): let main's model rest for agents that read no screen #27185's A/B, the daemon's RSS after a hidden flood was lower with the daemon answering (130 vs 190 MiB); a likely reason is that it buffers less while main keeps up, and RSS is a high-water mark.setSessionQueryResponder,setTerminalViewAttributes) and a new stream event (sessionQueryResponderMarker) that every later daemon and main must keep speaking, with mixed-version behavior to test. Main gains a second responder path and a delegation state machine (confirm timeout, give-up, generation reset). The in-flight double-answer window for remote viewers is a known follow-up (Notes).Notes
Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (or CI will cover; local preferred)🤖 Generated with Claude Code