Repository navigation
Prevent sleeping agents from getting stranded in stale terminals - #6800
Conversation
Co-authored-by: Orca <[email protected]>
📝 WalkthroughWalkthroughA new 🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (3 passed)
✨ Finishing Touches📝 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.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 51daa445-0fa4-4168-b65e-6e89637713e2
📒 Files selected for processing (3)
src/renderer/src/lib/resume-sleeping-agent-session.test.tssrc/renderer/src/lib/resume-sleeping-agent-session.tssrc/renderer/src/lib/sleeping-agent-pane-ownership.ts
Co-authored-by: Orca <[email protected]>
…idden restorable panes (#12574) * fix(runtime): report terminal handles disconnected on controller-proven PTY absence leaf.connected mirrors the renderer graph (ptyId !== null), so a restored surface whose PTY died with a prior process was listed connected/writable forever with empty title/lastOutputAt/preview — the exact signature automation saw on run6 workspaces after a restart. listTerminals now threads the controller inventory it already fetches into buildTerminalSummary and demotes only on proven absence, only for locally-scoped ids; unknown liveness and SSH/remote scopes never demote, and no session or pane is retired. * fix(terminal): stop forking hidden restorable panes into replacement resume tabs paneWillConnectOnActivation still assumed the pre-keep-alive mount model, but every non-parked tab of the active worktree mounts and connects hidden at 0x0. Activation therefore appended a replacement resume tab per non-group-active agent pane and handed it the sleeping record, stranding the hidden pane as a bare shell — or forking two live surfaces onto one provider session when the old PTY survived in the daemon. The predicate now answers "will mount and connect": any non-web-mirror tab of the active worktree qualifies; non-active worktrees still answer false so background wake keeps its append-based resume. Contract change: reverses the hidden-tab expectation from #6800, whose premise (hidden panes never connect) no longer holds; that test is updated in place. * test(terminal): pin the remote-scope exemption and the web-mirror ownership exception CodeRabbit flagged both exclusions as untested: a remote-runtime-scoped leaf absent from the local inventory must stay connected (its inventory lives on the remote host), and a web-mirror tab must not own sleeping-session recovery (it never mounts a local pane), so the appended replacement remains its correct resume path. * fix(terminal): rescue just-spawned ptys from absence demotion; unpark panes owning sleeping records Review (GPT verifier) confirmed two gaps: - listTerminals demoted a live just-spawned PTY when listProcesses snapshotted before session registration (the sweep's hasPty rescue is leaf-gated), and federation reads one connected:false as exited. The summary's proven-absence check now also consults the provider's sync hasPty. - Ordinary per-tab cold parking (30s hidden) kept a non-group-active pane unmounted, so a sleeping record it owns under the new ownership predicate could not cold-restore until the user revealed the tab. Per-tab parks now exempt panes owning a sleeping-session record; worktree-level parks are untouched (they clear on activation). * fix(terminal): reconcile the daemon session cache on inventory; scope the park exemption to consumable records Round-2 review confirmed two holes in the round-1 fixes: - DaemonPtyAdapter.hasPty is cached activeSessionIds membership, and a successful listSessions never removed ids the authoritative inventory omitted — an exit missed while the socket was down kept hasPty true forever, and the new spawn/list-race rescue would trust it, reopening connected-forever for that pty. listProcesses now drops pre-request cached ids the inventory does not list alive (ids spawned mid-flight are snapshot- protected). - The park exemption covered records a pane can never consume (automaticResumeBlockedBy, passive-completed evidence), pinning hidden panes mounted indefinitely. The exemption now lives in sleeping-record-park-exemption.ts and requires a consumable record. Also pins the web-mirror replacement's resume claim and startup command (CodeRabbit round-2). --------- Co-authored-by: OrcaWin <[email protected]>
…blyai#6800) Co-authored-by: Orca <[email protected]>
…idden restorable panes (stablyai#12574) * fix(runtime): report terminal handles disconnected on controller-proven PTY absence leaf.connected mirrors the renderer graph (ptyId !== null), so a restored surface whose PTY died with a prior process was listed connected/writable forever with empty title/lastOutputAt/preview — the exact signature automation saw on run6 workspaces after a restart. listTerminals now threads the controller inventory it already fetches into buildTerminalSummary and demotes only on proven absence, only for locally-scoped ids; unknown liveness and SSH/remote scopes never demote, and no session or pane is retired. * fix(terminal): stop forking hidden restorable panes into replacement resume tabs paneWillConnectOnActivation still assumed the pre-keep-alive mount model, but every non-parked tab of the active worktree mounts and connects hidden at 0x0. Activation therefore appended a replacement resume tab per non-group-active agent pane and handed it the sleeping record, stranding the hidden pane as a bare shell — or forking two live surfaces onto one provider session when the old PTY survived in the daemon. The predicate now answers "will mount and connect": any non-web-mirror tab of the active worktree qualifies; non-active worktrees still answer false so background wake keeps its append-based resume. Contract change: reverses the hidden-tab expectation from stablyai#6800, whose premise (hidden panes never connect) no longer holds; that test is updated in place. * test(terminal): pin the remote-scope exemption and the web-mirror ownership exception CodeRabbit flagged both exclusions as untested: a remote-runtime-scoped leaf absent from the local inventory must stay connected (its inventory lives on the remote host), and a web-mirror tab must not own sleeping-session recovery (it never mounts a local pane), so the appended replacement remains its correct resume path. * fix(terminal): rescue just-spawned ptys from absence demotion; unpark panes owning sleeping records Review (GPT verifier) confirmed two gaps: - listTerminals demoted a live just-spawned PTY when listProcesses snapshotted before session registration (the sweep's hasPty rescue is leaf-gated), and federation reads one connected:false as exited. The summary's proven-absence check now also consults the provider's sync hasPty. - Ordinary per-tab cold parking (30s hidden) kept a non-group-active pane unmounted, so a sleeping record it owns under the new ownership predicate could not cold-restore until the user revealed the tab. Per-tab parks now exempt panes owning a sleeping-session record; worktree-level parks are untouched (they clear on activation). * fix(terminal): reconcile the daemon session cache on inventory; scope the park exemption to consumable records Round-2 review confirmed two holes in the round-1 fixes: - DaemonPtyAdapter.hasPty is cached activeSessionIds membership, and a successful listSessions never removed ids the authoritative inventory omitted — an exit missed while the socket was down kept hasPty true forever, and the new spawn/list-race rescue would trust it, reopening connected-forever for that pty. listProcesses now drops pre-request cached ids the inventory does not list alive (ids spawned mid-flight are snapshot- protected). - The park exemption covered records a pane can never consume (automaticResumeBlockedBy, passive-completed evidence), pinning hidden panes mounted indefinitely. The exemption now lives in sleeping-record-park-exemption.ts and requires a consumable record. Also pins the web-mirror replacement's resume claim and startup command (CodeRabbit round-2). --------- Co-authored-by: OrcaWin <[email protected]>
Summary
Boundary: this does not change terminal cold-restore command construction or provider resume protocols. It only changes the renderer decision about whether a sleeping agent record is already owned by a preserved pane versus needs a fresh agent tab.
Design Approach
The failure mode was in
resumeSleepingAgentSessionsForWorktree: a stable-paneworktree-sleeprecord could be treated as owned just because the tab/layout still existed. If the preserved pane had no restorable PTY/session identity, or was hidden and would not mount/connect during activation, that ownership suppressed fresh launch and stranded the agent record.The fix extracts pane-ownership checks into
sleeping-agent-pane-ownership.tsand makes ownership require all of the following for active records:Completed hibernation records are treated as passive evidence: displayable preserved panes may keep them, but they never start work.
Design Review
Design review completed cleanly after confirming the change targets the activation/resume decision layer instead of terminal cold-restore internals. The reviewed design kept the scope to sleeping-agent activation, stale preserved-pane ownership, split visibility, completed evidence, SSH/cross-platform assumptions, and focused regression tests.
Implementation
Implemented as designed, with one structural deviation: pane-ownership logic was moved into a dedicated module so the existing resume file did not grow a broad mixed-responsibility helper section.
Notable behavior details:
Completeness / Headline Status
Headline behavior status: implemented.
Completeness verification found the original issue path covered: stale active sleep records no longer get suppressed by preserved panes that cannot connect, and duplicate/passive evidence no longer blocks active same-provider wake. The exact long-idle wall-clock reproduction was not rerun, but the minimized persisted state that causes the frozen/no-agent outcome is covered by unit and Electron validation.
Broad App Consistency
Source-of-truth behavior compared: terminal pane cold restore and worktree activation should either reconnect an owned visible preserved pane or fresh-launch the sleeping active agent record. Neighboring surfaces checked were terminal tabs, split-group visible terminals, sleeping-agent session records, completed hibernation evidence, and startup pending agent banners.
The PR preserves split-pane behavior for visible terminal groups, fixes hidden/layout-only preserved panes that incorrectly claimed ownership, and intentionally leaves terminal command construction/cold-restore fallback behavior unchanged.
Risks / Non-goals / Ambiguities
connectPanePtyitself cannot build a provider resume command.Perf Audit
Perf audit passed. Touched hot path is worktree activation / startup hydration of sleeping-agent records. The accepted O(n^2) gap is bounded by sleeping-record count and avoids stale cached ownership when earlier records fresh-launch and mutate active terminal state.
Code Review Loop
Validation
pnpm run typecheckpnpm run lint(passed with one unrelated existing warning inuseGitStatusPolling.ts)pnpm exec vitest run --config config/vitest.config.ts src/renderer/src/lib/resume-sleeping-agent-session.test.ts(30 tests)pnpm exec vitest run --config config/vitest.config.ts src/renderer/src/components/terminal-pane/pty-connection.test.ts -t "sleeping|cold|preserved|restore"(37 tests, 198 skipped)pnpm exec oxlint src/renderer/src/lib/resume-sleeping-agent-session.ts src/renderer/src/lib/sleeping-agent-pane-ownership.ts src/renderer/src/lib/resume-sleeping-agent-session.test.tspnpm exec oxfmt --check src/renderer/src/lib/resume-sleeping-agent-session.ts src/renderer/src/lib/sleeping-agent-pane-ownership.ts src/renderer/src/lib/resume-sleeping-agent-session.test.ts && git diff --checkpnpm exec vitest run --config config/vitest.config.ts src/renderer/src/lib/resume-sleeping-agent-session.test.ts,pnpm exec oxlint ...,pnpm exec oxfmt --check ... && git diff --checkoxlint,oxlint --config config/oxlint-react-doctor.json, andoxfmt --writeon staged files.Product Validation Evidence
Brennan's PR process validation verdict: land.
Electron validation used the exact worktree/branch with a temporary profile and
demo-project:Screenshot evidence:
.playwright-cli/page-2026-06-29T22-51-44-780Z.png.Post-PR Stabilization
Completed.
d6cbc98by selecting the newest active duplicate when no pane owns the provider session, with a regression assertion for newerlaunchConfigmetadata.verifypassed. Merge state is clean.