Repository navigation
fix(terminal): answer queries a multiplex viewer's initial snapshot covers - #27312
outof-place wants to merge 3 commits into
Conversation
…overs A multiplex stream takes terminal-query reply authority from main's model when it registers (registerRemoteTerminalViewSubscriber), and sends its initial snapshot later, after the viewport update and the serialize. Output main took in between went into its model unanswered, because a remote view now held the authority, and the snapshot covers it, so the viewer never saw those bytes live either. A program that asked for the cursor position or a color in that window waited for its own timeout (orb waits 5 s). The stream now records whether main's model held the authority it took. If it did, publishing the initial snapshot extracts the queries from the buffered output the snapshot covers and lets main's model answer them (answerTerminalQueriesFromModel: the query bytes written again on the model's write chain with replies forwarded; queries leave its state as it was). Output after the snapshot still goes to the viewer live, and the viewer answers it, so each query gets one reply. A stream that took the authority from another view leaves the covered output alone, as that view saw it live. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Priority: ➖ Normal Severity of issue fixed: Medium Merge Risk: 🟡 Moderate · up to During multiplex attach, a cursor-position query followed by cursor movement before the initial snapshot can receive the later position, potentially confusing applications that rely on the reply. Resolve this protocol-correctness issue 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:
d2fdf3d0-9af6-4de6-9723-40f6521ee336
📒 Files selected for processing (11)
src/main/runtime/orca-runtime-maybe-hydrate-headless-from-renderer.tssrc/main/runtime/rpc/methods/terminal/terminal-multiplex-initial-snapshot.tssrc/main/runtime/rpc/methods/terminal/terminal-multiplex-stream-initialization.tssrc/main/runtime/rpc/methods/terminal/terminal-snapshot-covered-queries.tssrc/main/runtime/rpc/methods/terminal/terminal-stream-types.tssrc/main/runtime/rpc/terminal-multiplex-escape-tail.test.tssrc/main/runtime/rpc/terminal-multiplex-resync-replay-trim.test.tssrc/main/runtime/rpc/terminal-multiplex-snapshot-covered-queries.test.tssrc/main/runtime/rpc/terminal-multiplex-test-harness.tssrc/main/runtime/rpc/terminal-requested-snapshot-unavailability.test.tssrc/main/runtime/terminal-query-responder.test.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.
| const completion = state.writeChain.then(() => | ||
| state.emulator.write(queryData, { forwardQueryReplies: true }) | ||
| ) |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟠 Major | 🏗️ Heavy lift
Answer stateful queries at their original output position.
If buffered output contains \x1b[6n, then moves the cursor before snapshot publication, this write answers the cursor-position query using the later cursor position. Two queries separated by a cursor move can therefore receive the same incorrect reply. Capture replies as the original output is parsed, or preserve the emulator state at each query position before forwarding replies.
There was a problem hiding this comment.
Leaving this as is, on purpose. The covered window runs from the stream's registration to its initial snapshot, about a millisecond. A program that sent a cursor-position query is blocked reading the reply, so it moves no cursor inside that window, and the cursor after the covered output is the one it asked about. Only a program that sends two queries around a cursor move within that millisecond, without waiting for either reply, would get the later position for both.
An exact answer at each query's position would need main's model to keep reply authority until the snapshot and hand it to the viewer atomically at the snapshot's seq. The snapshot is serialized asynchronously, so that is a bigger change than this fix. The PR description states the approximation; the hidden-pane query salvage already makes the same one.
There was a problem hiding this comment.
@outof-place Thanks for clarifying the intended approximation. For a program that waits for each query reply before moving the cursor, the replay uses the expected cursor position. The finding applies to queries interleaved with cursor changes without waiting for replies.
I accept leaving that edge case outside this fix. Would you like a follow-up issue for exact per-query replies with an atomic authority handoff at the snapshot sequence?
✏️ Learnings added
Learnt from: outof-place
Repo: stablyai/orca PR: 27312
File: src/main/runtime/orca-runtime-maybe-hydrate-headless-from-renderer.ts:183-185
Timestamp: 2026-10-11T12:21:17.118Z
Learning: In stablyai/orca, src/main/runtime/orca-runtime-maybe-hydrate-headless-from-renderer.ts intentionally implements answerTerminalQueriesFromModel by replaying snapshot-covered queries against the model's current state. This is an accepted approximation, consistent with hidden-pane query salvage. Exact stateful replies for queries interleaved with cursor changes without waiting for replies are outside this recovery's scope. An exact implementation needs coordinated query-reply authority handoff at the asynchronously serialized snapshot's output sequence.
Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.
You are interacting with an AI system.
The paired-runtime remount test and the cross-version wire host stub build their own runtime for terminal.multiplex; without the two methods the covered-query answer needs, stream setup threw before the host could refuse the write, so the remount never happened. Co-Authored-By: Claude Opus 5.5 <[email protected]>
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to GitHub limitations.
🟡 Minor · Preserve query data across the second overflow. · terminal-multiplex-initial-snapshot.ts:137-150
src/main/runtime/rpc/methods/terminal/terminal-multiplex-initial-snapshot.ts:137-150
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick winPreserve query data across the second overflow.
When the retry overflows again,
publishMultiplexInitialSnapshotclearsstream.pendingOutput. The later model recovery and viewer replay are both skipped forinitialOutputOverflowed. A retained query can therefore receive no reply.The renderer's resync request carries only
scrollbackRows, and the recovery snapshot restores screen state. It does not restore or reissue the discarded query bytes. Preserve query data separately from the truncated output path, then answer covered queries with the captured model authority and deliver post-snapshot queries to the viewer.
ℹ️ Review info
⚙️ Run configuration
- Configuration used: Repository UI
- Review profile: CHILL
- Plan: Advanced
- Run ID:
b4b9d11e-d2c6-418a-8b60-8424a84b1324
📒 Files selected for processing (2)
tests/e2e/cross-version-wire/host-terminal-runtime-stub.tstests/e2e/paired-runtime-rejected-input-remount.unit.test.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 4 remain after this review.
answerQueriesCoveredBySnapshot joined the extracted queries by kind (stateless, stateful, OSC color), so a cursor-position query followed by DA1 was answered DA1 first. Programs that send DA1 as an "all replies are in" sentinel then read the other query as unsupported. The extractor now also returns every query in output order (`orderedQueryData`), and the covered-query answer uses it. Its other callers are unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
On the outside-diff note about the second overflow in |
|
Rechecked with this fix on a build with OrbStack machines and warm templates. A harness ran 13 rounds and measured, through the terminal stream, the time to the first shell prompt and to Claude's first frame.
|
ELI5
When a remote viewer opens a terminal just as a program in it asks the terminal a question ("where is the cursor?"), the question could fall into a gap where neither Orca nor the viewer answered it, and the program waited for its own timeout (5 s for
orb). Orca now answers questions that fall into that gap.What Changed
initializeMultiplexStreamrecords, just before the stream registers as a remote view subscriber, whether main's model was answering this PTY's queries (runtime.modelAnswersTerminalQueries, the same per-chunk decisiononPtyDatamakes). The flag is stored on the stream asmodelHeldQueryAuthority.publishMultiplexInitialSnapshotalready splits the output it buffered during the attach at the snapshot's seq: what the snapshot covers is dropped and the rest goes out live. Now, whenmodelHeldQueryAuthorityis set,answerQueriesCoveredBySnapshotfirst extracts the queries from the covered part. It uses the extractor the hidden-pane salvage uses (extractHiddenStartupRendererQueryData, which now also returns the queries in output order asorderedQueryData), and hands them toruntime.answerTerminalQueriesFromModelin that order.answerTerminalQueriesFromModelwrites the query bytes once more on the PTY model's write chain, withforwardQueryReplies: true. Main's model has already parsed that output, so the replies reflect the screen after it. Queries don't change terminal state, so the second write only produces the replies.Before: a query in output taken in between the stream's registration and its snapshot got no reply. After: main answers it once. Output after the snapshot reaches the viewer live, and the viewer answers it, as before. If the stream took reply authority from another remote view rather than from main, nothing changes, because that view saw the output live.
Why
The gap is between two steps that both have to stay: the view must hold reply authority from registration on, so main doesn't answer output the view will also answer live. And the snapshot has to come after the viewport update. Answering the covered queries at the one point that knows exactly which output the snapshot covers keeps each query's reply single.
Alternatives considered:
For a cursor-position query, the reply uses the cursor after the covered output, not at the instant of the query. A program waiting for its answer hasn't moved the cursor, so this is the position it asked for. The same approximation is already used where hidden-pane queries are salvaged.
Linked Issue
Fixes #27311
Visual Proof
N/A: no UI change. The effect is a program that gets its reply instead of waiting for its timeout. The unit tests below pin the window.
Testing
terminal-multiplex-snapshot-covered-queries.test.ts(new) holds the snapshot while output arrives:terminal-query-responder.test.ts(one new case), on the realOrcaRuntimeServiceand emulator: output taken in while a remote view holds authority stays unanswered, andanswerTerminalQueriesFromModelthen gives exactly the DA1 and cursor-position replies.Ran locally (macOS arm64), all passing:
node node_modules/typescript/bin/tsc --noEmit -p config/tsconfig.node.jsonand-p config/tsconfig.tc.web.json(whattc:node/tc:webrun);pnpm run audit:anti-slop;pnpm exec oxlintandpnpm exec oxfmt --writeon the changed files;pnpm exec vitest run --config config/vitest.config.ts --maxWorkers=4 src/main/runtime/rpc src/main/runtime/terminal-query-responder.test.ts src/main/ipc/pty src/main/daemon/daemon-server src/renderer/src/components/terminal-pane tests/e2e/paired-runtime-rejected-input-remount.unit.test.ts: 1045 files, 9941 tests.Three test files with their own runtime stubs got the new method.
End to end, automated (a harness, not a person): a build with this fix ran against real OrbStack VMs, where
orbshowed the stall. Over 13 rounds it timed each start through the terminal stream: 0 of 69 starts waited near 5 s, against 2 of 42 (about 5.2 s each) before the fix. That is 51 shell prompts at 70–198 ms and 18 Claude first frames at 275–622 ms. One more start, the first after the VM templates were rebuilt, showed no prompt for 30 s; that is a VM issue, not this stall. Details are in a comment below. A synthetic repro (a program asking in a tight loop while a viewer attaches) never landed a query inside the ~1 ms window in 24 attempts, so these runs are the end-to-end evidence.I manually tested these changes locally
Automated tests added/updated, or explained why not below
AI Disclosure
Claude Code (Claude Opus 5.5) wrote this change, its tests and this description, and ran the checks above, working for @outof-place.
Review
Agent skill upstream boundary
Notes
Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (or CI will cover; local preferred)🤖 Generated with Claude Code