Repository navigation
fix(runtime): resume terminal agents in headless serve after a daemon loss or sleep - #27527
outof-place wants to merge 1 commit into
Conversation
… loss or sleep
A headless runtime (`orca serve`) had no renderer to keep resume records or
issue `--resume`, so a pane restored after the terminal daemon died came back
as a plain shell, and `worktree.sleep` did nothing.
- Starting a pending pane resumes its agent with the saved session id: the
pane's sleeping record, else its status row, in the renderer's cold-restore
order. A session that ended under its shell, or one captured on another
host, is not resumed. A sleep record is spent by its resume.
- A second open of a pane that is already starting waits for that start, so
one pane never starts two agents.
- Under serve, the host keeps the renderer's `live` resume anchors, written in
batches (at most one session write per second per worktree, none when
nothing changed) and synchronously before startup reaping drops rows whose
PTY died with the daemon. A Claude SessionEnd that ends the process and a
closed tab retire the pane's records.
- `worktree.sleep` with no renderer records each agent (`worktree-sleep`,
lazy for finished panes) and stops the PTYs through the host's own sleep.
- The terminal tab carries `agentResume { agent, outcome }` after the host
started it: `resumed`, or `unavailable` when an agent ran there but cannot
be resumed here (the pane opens as a plain shell). A tab whose agent never
ran keeps the fresh launch.
- The resume host-scope guard moves to shared so main uses the same rule.
- ORCA_HEADLESS_AGENT_RESUME=0 turns all of it off.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
📝 Walkthrough
Priority: ➖ Normal Severity of issue fixed: Medium Merge Risk: ⚪ Minimal · up to No confirmed behavior issue blocks merging. Whether every status-row removal safely retires its resume record remains unverified. Pre-merge checks |
|
There was a problem hiding this comment.
🔇 Additional comments (20)
src/main/runtime/orca-runtime-perform-mobile-session-pty-records-refresh.ts (2)
164-176: Begin the in-flight registration before the firstawait, or every exit path must callfinishMaterialization.
beginPaneMaterializationis called synchronously afterjoinPaneMaterialization, so the concurrent-open guard holds. Two code paths still need checks:
- Line 194: when
resolvePaneAgentResumeStartupreturns anunavailableplan,agentResumeis non-null. The code then skips thetab.launchAgentre-resolve. The pane opens as a plain shell, which is the intended result.- Lines 177-204: if
resolveTerminalWorkspaceLaunchScopethrows, the innertry/catchswallows it. Noawaitbetween line 176 and thetryat line 205 can throw without that guard. The only exception isresolvePaneAgentResumeStartup, and the.catchon that call handles it.The
finallyblock always releases the entry, so the materialization is not leaked.
181-243: LGTM!src/main/runtime/agent-resume-records.ts (1)
1-214: LGTM!src/shared/agent-resume-origin-host.ts (1)
1-59: LGTM!src/renderer/src/lib/sleeping-record-execution-host-scope.ts (1)
2-2: LGTM!Also applies to: 8-8
src/main/runtime/agent-resume-records.test.ts (1)
1-181: LGTM!src/main/runtime/headless-agent-resume-anchors.ts (1)
1-161: LGTM!src/main/startup/main-process-runtime-service.ts (1)
180-192: LGTM!src/main/startup/main-process-pty-startup.ts (1)
101-105: LGTM!src/main/runtime/orca-runtime-headless-agent-resume.ts (1)
1-313: LGTM!src/main/runtime/orca-runtime-create-runtime-owned-mobile-session-terminal.ts (1)
5-8: LGTM!Also applies to: 64-64, 117-119
src/main/runtime/orca-runtime-list-managed-worktrees.ts (1)
208-217: LGTM!src/main/runtime/orca-runtime-migration-catalog.ts (1)
16-21: LGTM!src/main/runtime/orca-runtime-close-headless-mobile-terminal-tab.ts (1)
128-128: LGTM!src/main/runtime/orca-runtime-headless-agent-resume.test.ts (1)
1-293: LGTM!src/shared/runtime-mobile-session-tab-contracts.ts (1)
24-25: LGTM!Also applies to: 36-40
src/main/runtime/runtime-mobile-session-projection-contract.ts (1)
10-10: LGTM!Also applies to: 37-37
src/main/runtime/orca-runtime-prune-mobile-session-tab-group-layout.ts (1)
119-119: LGTM!src/main/runtime/runtime-mobile-session-projection.ts (1)
292-292: LGTM!Also applies to: 304-304
src/main/runtime/headless-agent-resume-anchors.test.ts (1)
99-99: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value
⚠️ Unverified finding
Verification ran but could not confirm this finding. It is shown for review, not as a verified issue.Remove the debug
console.logfrom the test.Line 99 prints output on every test run. This output adds noise to CI logs. The assertions already check the write budget.
Proposed fix
- console.log(`anchor writes per minute, 5 agents: ${anchors.writes} for ${events} events`)
ℹ️ Review info
⚙️ Run configuration
- Configuration used: Repository UI
- Review profile: CHILL
- Plan: Advanced
- Run ID:
a1defa57-6440-4092-af15-f23714f48342
📒 Files selected for processing (19)
src/main/runtime/agent-resume-records.test.tssrc/main/runtime/agent-resume-records.tssrc/main/runtime/headless-agent-resume-anchors.test.tssrc/main/runtime/headless-agent-resume-anchors.tssrc/main/runtime/orca-runtime-close-headless-mobile-terminal-tab.tssrc/main/runtime/orca-runtime-create-runtime-owned-mobile-session-terminal.tssrc/main/runtime/orca-runtime-headless-agent-resume.test.tssrc/main/runtime/orca-runtime-headless-agent-resume.tssrc/main/runtime/orca-runtime-list-managed-worktrees.tssrc/main/runtime/orca-runtime-migration-catalog.tssrc/main/runtime/orca-runtime-perform-mobile-session-pty-records-refresh.tssrc/main/runtime/orca-runtime-prune-mobile-session-tab-group-layout.tssrc/main/runtime/runtime-mobile-session-projection-contract.tssrc/main/runtime/runtime-mobile-session-projection.tssrc/main/startup/main-process-pty-startup.tssrc/main/startup/main-process-runtime-service.tssrc/renderer/src/lib/sleeping-record-execution-host-scope.tssrc/shared/agent-resume-origin-host.tssrc/shared/runtime-mobile-session-tab-contracts.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.
ELI5
When
orca serveruns with no desktop window, a terminal agent (Claude Code, Codex, …) does not come back after the terminal daemon dies or after "Close Terminals". The tab returns, but as a plain shell, and the conversation is gone from the UI. The desktop app avoids this because its window keeps a small "how to resume this pane" record and startsclaude --resume <id>when the pane starts again. This PR gives the headless host the same records and the same resume.What Changed
Before: after a daemon loss (crash, reboot) the restored agent tabs are pending; opening one starts a bare shell, idle or mid-turn alike. A mid-turn agent also loses its session id, because the startup sweep (
reapRestoredClaudeSubagentsWithoutLiveAgent) deletes hook rows whose PTY died.worktree.sleepdoes nothing without a window.After, on a host with no renderer:
session.tabs.activatestarts a pending pane, the runtime looks for the pane's resume identity: its record insleepingAgentSessionsByPaneKey, else its hook status row, in the renderer's cold-restore order. It launches the existingbuildAgentResumeStartupPlancommand through the same terminal creation as a new agent tab and passesresumeProviderSession. The resume only re-opens the conversation; nothing is sent to the agent. It does not resume a session whose agent already exited under its shell, nor a session captured on another host (the renderer's guard, moved tosrc/shared/agent-resume-origin-host.ts). A sleep record is spent by its resume.liverecord per status update; under serve the host keeps the same records from status-row changes, in batches, and writes them synchronously before the startup sweep drops rows whose PTY died. A ClaudeSessionEndthat ends the process and a closed tab retire the pane's records. Nothing is written while an authoritative window exists, so the renderer stays the only writer on desktop.worktree.sleepwith no notifier records each agent asworktree-sleep(finished panes lazy, as the renderer's manual sleep) and stops the PTYs through the existingsleepTerminalsForWorktree. Automatic activations stay refused while the sleep is held; a user open resumes.agentResume { agent, outcome }after the host started it:resumed, orunavailablewhen an agent ran in the pane but its session cannot be resumed here. The pane then opens as a plain shell, and a client can say so instead of showing a silent shell. A tab whose agent never ran keeps the [Bug]: Creating a Claude tab from mobile spawns it on the desktop, then reaps it ~10s later — mobile tab creation broken #7587 fresh launch.ORCA_HEADLESS_AGENT_RESUME=0restores today's behavior.Why
The records and the resume command already exist and are what the desktop uses; the headless host was the only path without them, so this reuses them rather than adding a format. A window opened later reads the same records.
This builds on the approach of #23919 by @JustTalDevelops, which resumes a pending pane from the host's hook record. Thanks for that groundwork. This PR extends it to the panes that approach cannot reach: agents that were working or waiting at the crash (their rows are gone after the startup sweep), worktree sleep, and the anchors that keep the identity across restarts.
On the remote-server rework mentioned in #21743: the change is self-contained (two new runtime files, a mixin, and small hooks at the activation, sleep, close and startup call sites) and sits behind
ORCA_HEADLESS_AGENT_RESUME, so it can be switched off or removed in one piece once the rework lands.Trade-offs.
activateMobileSessionTabnow prefers a resume over the #7587 fresh launch when an identity exists, and serializes concurrent starts of the same paneagent-resume-records.ts, next to the renderer'sLinked Issue
Fixes #21743
Visual Proof
N/A: headless host, no UI. The before/after is the stand-in
claude's recorded argv in the table below.Testing
macOS only. SSH and Windows were not run: SSH origins go through the existing host-scope guard (unit-tested), and the command builder is the existing cross-platform one.
Manual check, on
main0df25b7 with and without this commit:--serveruntime with E2E user data and home, a mock keychain, and a local-only loopback pin so nothing listens beyond 127.0.0.1;claudeon PATH that records its argv;Agent A was idle (
Stop), agent B mid-turn. Then: SIGKILL of the runtime and the daemon, relaunch, open both tabs (A twice at once); thenworktree.sleep, an automatic activation, a user activation.claude --dangerously-skip-permissions --resume <A>claude --dangerously-skip-permissions --resume <B>worktree.sleepterminal.sleep)--resume <A'>/--resume <B>Commands run on this branch:
pnpm run tcORCA_CODE_QUALITY_BASE=upstream/main pnpm run check:code-quality:changedpnpm run build:electron-vitepnpm run build:cli && pnpm run verify:built-skills-clinode config/scripts/run-vitest.mjs run --config config/vitest.config.ts --maxWorkers=4 src/main/runtime src/main/startup src/main/agent-hooks src/sharedplus the renderer host-scope, cold-restore and sleep-flow tests: 26430 passed, 2 failed. The 2 failures arerun-terminal-shell-alone.node-pty.test.ts, which fails the same way on cleanmain(0df25b7) on this machine.pnpm run tc, the changed-code gate, and the new tests plusorca-runtime.test.ts(1363 passed).Not run: full
pnpm lint, fullpnpm test,pnpm build.AI Disclosure
Claude Opus 5.5 (Claude Code) investigated, wrote the change and the tests, and ran the manual check above.
Review
Agent skill upstream boundary
Notes
agentResumefield. Records hold the same provider session ids the desktop already persists.unavailable.Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (or CI will cover; local preferred)🤖 Generated with Claude Code