Repository navigation
fix(mobile): keep a terminal lease alive when the handle lookup fails - #15463
Conversation
`waitForTerminal(..., 'exit')` rejects with `terminal_handle_stale` whenever the handle cannot be resolved right now. The loudest source is `record.rendererGraphEpoch !== this.rendererGraphEpoch` — every renderer graph reload invalidates handles issued before it, on panes whose PTY is untouched. All three terminal.subscribe branches wired that rejection straight to teardown (`.catch(() => registration.releaseIfCurrent())`), so the host retired a live lease and emitted `end`. Mobile reads `end` as "PTY gone" and rearms; after MAX_REARM_ATTEMPTS = 3 it stops and leaves the composer on "Waiting for terminal…", recovering only when a replacement handle arrives. A live PTY keeps its handle, so no replacement ever comes: the pane is dead until the app is backgrounded and reopened. Demand proof instead. Retire on abort (this socket is going away) or when isLeafPtyProvenAbsent says the process is gone; a real exit still resolves and retires through the .then leg. Absence must be proven, never inferred from a lookup that failed. Reproduced live: a paired 0.0.44 client over LAN, fault injected on the handle lookup, composer locked on "Waiting for terminal…" and STILL locked 97s after the fault was lifted with the PTY alive and the handle unchanged. Narrows one behavior #14992 pinned: a stale handle alone no longer retires the owner. The ownership property is unchanged — once absence is proven, the owning registration is still what retires. That test is updated, not deleted.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (11)
Included review availability: Your plan provides up to 10 included reviews per hour; 0 remain after this review. 📝 WalkthroughWalkthroughThe runtime now provides 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/main/runtime/rpc/methods/terminal.ts (1)
324-337: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winShorten the new comments and correct the helper description.
The helper comment says cleanup occurs only when the socket closes, but lines 348-352 also release on proven PTY absence. Keep the non-obvious release conditions. Remove the incident narrative.
src/main/runtime/rpc/methods/terminal.ts#L324-L337: Replace this block with one concise comment that states both release conditions.src/main/runtime/rpc/terminal-lease-stale-handle-survival.test.ts#L1-L10: Remove the incident narrative. The test name already states the scenario.src/main/runtime/rpc/terminal-subscribe-ownership.test.ts#L111-L114: Reduce this block to one concise statement of the proven-absence condition.As per coding guidelines, “Comments must be concise, non-obvious, and brief—prefer one line; do not explain obvious behavior or walk through code.”
Source: Coding guidelines
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 7fffc644-5030-4c9f-a589-a83c44e2f1e9
📒 Files selected for processing (4)
src/main/runtime/orca-runtime.tssrc/main/runtime/rpc/methods/terminal.tssrc/main/runtime/rpc/terminal-lease-stale-handle-survival.test.tssrc/main/runtime/rpc/terminal-subscribe-ownership.test.ts
Included review availability: Your plan provides up to 10 included reviews per hour; 4 remain after this review.
There was a problem hiding this comment.
Actionable comments posted: 2
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: df161547-47dc-439a-8ef8-a574e06fdf06
📒 Files selected for processing (7)
src/main/runtime/orca-runtime.tssrc/main/runtime/rpc/methods/terminal.tssrc/main/runtime/rpc/streaming.test.tssrc/main/runtime/rpc/terminal-lease-stale-handle-survival.test.tssrc/main/runtime/rpc/terminal-subscribe-lease-only.test.tssrc/main/runtime/rpc/terminal-subscribe-ownership.test.tssrc/main/runtime/terminal-pty-exit-waiter.test.ts
Included review availability: Your plan provides up to 10 included reviews per hour; 5 remain after this review.
| private isPtyKnownExited(ptyId: string): boolean { | ||
| const pty = this.ptysById.get(ptyId) | ||
| if (pty) { | ||
| return !pty.connected | ||
| } | ||
| return this.getLeavesForPty(ptyId).some((leaf) => getTerminalState(leaf) === 'exited') | ||
| } | ||
|
|
||
| private notifyPtyExitListeners(ptyId: string): void { | ||
| const listeners = this.ptyExitListenersByPtyId.get(ptyId) | ||
| if (!listeners) { | ||
| return | ||
| } | ||
| this.ptyExitListenersByPtyId.delete(ptyId) | ||
| notifyRuntimeListeners(listeners, (listener) => listener(), 'pty-exit') | ||
| } | ||
|
|
There was a problem hiding this comment.
🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift
Notify exit listeners on every PTY disconnect transition.
These reconciliation paths set pty.connected = false without notifying existing exit listeners. Because the subscription lifetime has no timeout, listeners waiting through these transitions can remain registered indefinitely, including after record pruning. Route every transition to connected = false through a notifier, or call notifyPtyExitListeners(ptyId) after updating the state.
📍 Affects 1 file
src/main/runtime/orca-runtime.ts#L35226-L35242(this comment)src/main/runtime/orca-runtime.ts#L15084-L15089
Live validation — commit
|
…nously The runtime doubles for terminal.subscribe never stubbed subscribeToPtyExit, so the new lifetime watcher threw and killed every subscription under test. Also return early when the watcher releases synchronously: cleanup had already run, so the stream handlers, view subscribers, and mobile presence registered afterwards were never torn down.
…stablyai#15463) * fix(mobile): keep a terminal lease alive when the handle lookup fails `waitForTerminal(..., 'exit')` rejects with `terminal_handle_stale` whenever the handle cannot be resolved right now. The loudest source is `record.rendererGraphEpoch !== this.rendererGraphEpoch` — every renderer graph reload invalidates handles issued before it, on panes whose PTY is untouched. All three terminal.subscribe branches wired that rejection straight to teardown (`.catch(() => registration.releaseIfCurrent())`), so the host retired a live lease and emitted `end`. Mobile reads `end` as "PTY gone" and rearms; after MAX_REARM_ATTEMPTS = 3 it stops and leaves the composer on "Waiting for terminal…", recovering only when a replacement handle arrives. A live PTY keeps its handle, so no replacement ever comes: the pane is dead until the app is backgrounded and reopened. Demand proof instead. Retire on abort (this socket is going away) or when isLeafPtyProvenAbsent says the process is gone; a real exit still resolves and retires through the .then leg. Absence must be proven, never inferred from a lookup that failed. Reproduced live: a paired 0.0.44 client over LAN, fault injected on the handle lookup, composer locked on "Waiting for terminal…" and STILL locked 97s after the fault was lifted with the PTY alive and the handle unchanged. Narrows one behavior stablyai#14992 pinned: a stale handle alone no longer retires the owner. The ownership property is unchanged — once absence is proven, the owning registration is still what retires. That test is updated, not deleted. * fix(mobile): observe PTY exit independent of stale handles * fix(mobile): stop subscribe setup when an exited pty releases synchronously The runtime doubles for terminal.subscribe never stubbed subscribeToPtyExit, so the new lifetime watcher threw and killed every subscription under test. Also return early when the watcher releases synchronously: cleanup had already run, so the stream handlers, view subscribers, and mobile presence registered afterwards were never torn down.
…stablyai#15463) * fix(mobile): keep a terminal lease alive when the handle lookup fails `waitForTerminal(..., 'exit')` rejects with `terminal_handle_stale` whenever the handle cannot be resolved right now. The loudest source is `record.rendererGraphEpoch !== this.rendererGraphEpoch` — every renderer graph reload invalidates handles issued before it, on panes whose PTY is untouched. All three terminal.subscribe branches wired that rejection straight to teardown (`.catch(() => registration.releaseIfCurrent())`), so the host retired a live lease and emitted `end`. Mobile reads `end` as "PTY gone" and rearms; after MAX_REARM_ATTEMPTS = 3 it stops and leaves the composer on "Waiting for terminal…", recovering only when a replacement handle arrives. A live PTY keeps its handle, so no replacement ever comes: the pane is dead until the app is backgrounded and reopened. Demand proof instead. Retire on abort (this socket is going away) or when isLeafPtyProvenAbsent says the process is gone; a real exit still resolves and retires through the .then leg. Absence must be proven, never inferred from a lookup that failed. Reproduced live: a paired 0.0.44 client over LAN, fault injected on the handle lookup, composer locked on "Waiting for terminal…" and STILL locked 97s after the fault was lifted with the PTY alive and the handle unchanged. Narrows one behavior stablyai#14992 pinned: a stale handle alone no longer retires the owner. The ownership property is unchanged — once absence is proven, the owning registration is still what retires. That test is updated, not deleted. * fix(mobile): observe PTY exit independent of stale handles * fix(mobile): stop subscribe setup when an exited pty releases synchronously The runtime doubles for terminal.subscribe never stubbed subscribeToPtyExit, so the new lifetime watcher threw and killed every subscription under test. Also return early when the watcher releases synchronously: cleanup had already run, so the stream handlers, view subscribers, and mobile presence registered afterwards were never torn down.
…stablyai#15463) * fix(mobile): keep a terminal lease alive when the handle lookup fails `waitForTerminal(..., 'exit')` rejects with `terminal_handle_stale` whenever the handle cannot be resolved right now. The loudest source is `record.rendererGraphEpoch !== this.rendererGraphEpoch` — every renderer graph reload invalidates handles issued before it, on panes whose PTY is untouched. All three terminal.subscribe branches wired that rejection straight to teardown (`.catch(() => registration.releaseIfCurrent())`), so the host retired a live lease and emitted `end`. Mobile reads `end` as "PTY gone" and rearms; after MAX_REARM_ATTEMPTS = 3 it stops and leaves the composer on "Waiting for terminal…", recovering only when a replacement handle arrives. A live PTY keeps its handle, so no replacement ever comes: the pane is dead until the app is backgrounded and reopened. Demand proof instead. Retire on abort (this socket is going away) or when isLeafPtyProvenAbsent says the process is gone; a real exit still resolves and retires through the .then leg. Absence must be proven, never inferred from a lookup that failed. Reproduced live: a paired 0.0.44 client over LAN, fault injected on the handle lookup, composer locked on "Waiting for terminal…" and STILL locked 97s after the fault was lifted with the PTY alive and the handle unchanged. Narrows one behavior stablyai#14992 pinned: a stale handle alone no longer retires the owner. The ownership property is unchanged — once absence is proven, the owning registration is still what retires. That test is updated, not deleted. * fix(mobile): observe PTY exit independent of stale handles * fix(mobile): stop subscribe setup when an exited pty releases synchronously The runtime doubles for terminal.subscribe never stubbed subscribeToPtyExit, so the new lifetime watcher threw and killed every subscription under test. Also return early when the watcher releases synchronously: cleanup had already run, so the stream handlers, view subscribers, and mobile presence registered afterwards were never torn down.








ELI5
When the phone holds a terminal open, the desktop watches that terminal for "did it exit?". If the desktop momentarily can't look the terminal up — which happens on an ordinary renderer reload — it was treating that as "the terminal died" and telling the phone so. The phone believes it, retries three times, then gives up and locks the message box on "Waiting for terminal…" until you force-quit the app. This makes the desktop require actual proof before declaring a terminal dead.
What Changed
terminal.subscribebranches wired the exit-waiter's rejection straight to teardown:.catch(() => registration.releaseIfCurrent()).releaseLeaseOnWaitFailure, which retires only when the socket aborted (this connection is going away) orisLeafPtyProvenAbsent(ptyId)says the process is gone.isLeafPtyProvenAbsentbecomes public so the RPC layer can use the runtime's existing proven-absence predicate rather than inventing a second one..thenleg.Why
waitForTerminal(handle, { condition: 'exit' })rejects withterminal_handle_stalewhenevergetLiveLeafForHandlecan't resolve the handle. The loudest source is:Every renderer graph reload invalidates handles issued before it — on panes whose PTY is perfectly alive. The old wiring turned that into
emit({ type: 'end' })on a live lease.Mobile reads
endas "PTY gone", rearms, and afterMAX_REARM_ATTEMPTS = 3deliberately stops — "Give up after a few tries and leave the composer honestly locked." Recovery then requires a replacement handle. A live PTY keeps its handle, so no replacement ever arrives and the pane stays dead: spinner, empty transcript, composer reading "Waiting for terminal…", on both the chat and terminal surfaces.Note
condition: 'exit'has no timeout (effectiveTimeoutMs = 0), so slowness alone cannot fire this. Load matters only because it causes the renderer churn that bumps the graph epoch.This is host-side on purpose: mobile 0.0.44 is in App Store review and can't be patched, and the 3-strikes give-up lives in the shipped client.
Linked Issue
Fixes #
Visual Proof
Reproduced live: shipped 0.0.44 client (built from
64de8dd637) on a paired iOS Simulator over LAN, against an isolated Electron host. The handle lookup was faulted behind a marker file so it could be lifted without restarting.EMIT endfor one handle. Composer flipped to "Waiting for terminal…", transcript empty, centre spinner.Testing
src/main/runtime/rpc/terminal-lease-stale-handle-survival.test.tsdrives the real dispatcher andTERMINAL_METHODSthrough a lease-only subscribe:end, nohandleMobileUnsubscribe..thenleg is untouched, so a dead pane can't hold the mobile input floor forever.Case 1 verified RED before the fix, with exactly the reported wire pattern:
Regression: full
src/main/runtime/rpc/suite — 170 files, 1473 passed, 1 skipped.oxlintclean.Platforms: macOS, local PTY, LAN mobile transport. Not exercised on Windows/Linux, SSH, or Relay — the change is transport-agnostic and sits in the shared subscribe path.
AI Disclosure
Claude Opus 4.5 (Claude Code) did the investigation, live repro, fix, and tests.
Review
Please look hardest here: this narrows a behavior #14992 pinned. Its test "tears down the current owner when the terminal handle goes stale" asserted that a stale handle retires the owner. That is now "…when the handle is stale and the PTY is proven absent". The ownership property #14992 cared about is unchanged — once absence is proven, the owning registration is still what retires — but the epistemics changed, and the test was updated rather than deleted. If that trade is wrong, this is the line to push on.
Second-order risk: when a handle is stale and liveness cannot be probed,
isLeafPtyProvenAbsentreturnsfalse, so the lease is now retained until the socket closes. That is a bounded retention (the connection owns it) traded against a permanent client lock.Agent skill upstream boundary
docs/reference/agent-skill-sharing-upstream-boundary.mdand copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.Notes
endframe on an existing path. Fixes already-installed 0.0.44 clients with no new build. Perdocs/reference/remote-wire-compatibility.mdthis is Rule 3 in the safe direction: the host stops sending a frame that misrepresented state.isLeafPtyProvenAbsentis cached/TTL'd and short-circuits on the controller's synchronous inventory.Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (or CI will cover; local preferred)oxlintand thesrc/main/runtime/rpc/suite pass locally. Fullpnpm typecheck/pnpm buildleft to CI.Author