Skip to content

fix: gate worktree status on live PTYs so sleep reports inactive - #1603

Merged
brennanb2025 merged 7 commits into
mainfrom
brennanb2025/sleep-statuses
May 10, 2026
Merged

brennanb2025 merged 7 commits into
mainfrom
brennanb2025/sleep-statuses

Conversation

@brennanb2025

@brennanb2025 brennanb2025 commented May 8, 2026 •

Copy link
Copy Markdown
Contributor

Summary

After sleeping a worktree, the sidebar lied on three axes: the dot stayed green, in-flight working agent rows persisted until the 30-min stale TTL, and retained done rows kept rendering under a grey card after Phase 1 fixed the first two. Sleep preserves tab.ptyId as a wake-hint sessionId — not a liveness signal — so the previous tab.ptyId != null checks were wrong, and retained-done was preserved across sleep against the "agent lives in the pane" mental model.

Phase 1 — status precondition + live-row drop (commit b5fd86e6):

  • Worktree status now gates on actual live PTYs (ptyIdsByTabId[tab.id].length > 0) via a new tabHasLivePty helper, threaded through getWorktreeStatus, WorktreeCard, WorktreeJumpPalette, and both branches of agent-status.ts (primary pane-titles + tab-title fallback).
  • shutdownWorktreeTerminals drops live agentStatusByPaneKey entries unconditionally on sleep, so working/blocked/waiting rows disappear with the dot.
  • Bonus: also catches the crash case (PTY exits before reattach) where tab.ptyId was populated as a hint but no PTY was alive.

Phase 2 — retained-done drop on sleep (commit 5b61ae64):

  • Sleep now folds retained done rows along with everything else — the prior grey-card-with-green-row mixed signal is gone. Reload, tab close, and worktree remove paths are unchanged.
  • dropAgentStatusByWorktree runs unconditionally from the sleep path, sweeping live + retained + ack for the worktree's tabs and any orphan retained rows whose worktreeId matches.
  • Removed the preserveRetained option from dropAgentStatusByTabPrefix — sleep was its only caller.

Design doc with full root-cause + UX matrix (Phase 1 + Phase 2 unified) lives at docs/PLAN_SLEEP_STATUSES.md (untracked locally).

Screenshots

No visual change to the happy path. Behavior change: the worktree dot transitions to grey within one render of sleeping a worktree, inline working rows disappear at the same time, and retained done rows are also dropped (Phase 2). The "Claude finished while you were away" UX continues to work for reload / unrelated worktree cases — only sleep folds it.

Testing

  • pnpm lint
  • pnpm typecheck
  • pnpm test
  • pnpm build
  • Added or updated high-quality tests that would catch regressions

New/extended tests:

  • tab-has-live-pty.test.ts (new) — direct unit tests for the helper.
  • worktree-status.test.ts — slept-tab (tab.ptyId set, no live PTY) → inactive; slept-with-retained-done → grey via the extracted resolveWorktreeStatus pure function.
  • agent-status.test.ts — getWorkingAgentsPerWorktree and countWorkingAgentsForTab both return 0 for a slept tab on (a) the primary pane-titles branch and (b) the tab-title fallback. Phase 2: regression test for dropAgentStatusByTabPrefix no longer accepting a preserveRetained option (live, retained, ack, suppressors all fold uniformly).
  • store-cascades.test.ts — after shutdownWorktreeTerminals(wt, { keepIdentifiers: true }): agentStatusByPaneKey is cleared, retained-done is also cleared (Phase 2 inversion), ack entries are cleared, suppressors are planted for live keys. Hook-level assertions for useWorktreeAgentRows covering slept and slept-with-retained-done.
  • sleep-worktree-flow.test.ts — end-to-end: retained-done rows for the slept worktree are gone after runSleepWorktree returns.

Manual end-to-end verification (real Electron dev build)

Spawned a fresh dev build via run-electron-vite-dev.mjs --remote-debugging-port=9336, attached playwright-cli over CDP via a named session (to avoid cross-instance leaks across other dev builds on the same user-data-dir), and drove the live store. Created a throwaway test worktree, spawned its PTY, and exercised the scenarios below. Real hook events were posted to agentHookServer over its loopback port + token to seed lastStatusByPaneKey.

  1. Sleep clears the working agent row + greys the dot (Phase 1). Pre-sleep: livePtys=[ptyId], agentEntry={state:'working'}. Called shutdownWorktreeBrowsers + shutdownWorktreeTerminals(wt, { keepIdentifiers: true }). Post-sleep: livePtys=[], agentEntry=null, getWorktreeStatus inputs yield 'inactive' (dot grey), working row gone.

  2. Done → sleep drops retained-done (Phase 2). Set working → done (uninterrupted), waited for useRetainedAgentsSync to capture the live entry in prevAgentsRef, then removed the live entry to land a retained done. Pre-sleep: live PTY present, retainedEntry={state:'done', interrupted:false}. Post-sleep: livePtys=[], liveAgent=null, retainedAgent=null (Phase 2 drop), no suppressor leak.

  3. Acknowledged done → sleep drops retained + ack (Phase 2). Same setup as test 2 but called acknowledgeAgents([paneKey]) before the live drop. Pre-sleep: retainedEntry present, ackTimestamp set. Post-sleep: retained dropped, ackTimestamp=null (acknowledgedAgentsByPaneKey ack entry cleaned up), no suppressor leak.

  4. WorktreeJumpPalette matches the sidebar (Phase 1). Opened the palette over the slept worktree. The option rendered as Inactive sleep-status-e2e-..., agreeing with the sidebar's grey dot. Confirms the jump-palette getWorktreeStatus plumbing — without it the palette would desync from the sidebar (showing the slept worktree as still active off tab.ptyId).

  5. Sleep + wake reactivates fresh; past done does not resurrect. After test 3's sleep, called setActiveWorktree(wtId) to wake. Post-wake: fresh ptyId on the tab, tab.generation=1, liveAgent=null, retainedAgent=null. The worktree reactivates empty as designed.

Tests 7 and 8 from the design doc (renderer reload paths) require a full BrowserWindow recreation for the agentHookServer.setListener replay loop to fire — location.reload() keeps the same BrowserWindow alive, so the replay (which only triggers on setListener(fn) re-call) doesn't run. What was verified upstream: hook POSTs land in renderer state via the IPC bridge (so lastStatusByPaneKey.set runs on every hook), and clearPaneState(paneKey) is invoked from pty.kill teardown at src/main/ipc/pty.ts:349 (so sleep's PTY teardown wipes the replay cache as the design doc claims). Full Cmd+Q+relaunch verification is not in this run.

Remaining manual checks with real agent processes (mid-turn working sleep, Cmd+Q+relaunch reload paths, permission-state sleep, two-worktree isolation) are tracked in docs/PR_1603_MANUAL_TEST_PLAN.md for follow-up before merge.

Cleanup: throwaway worktree removed via removeWorktree(id, true), dev build closed.

AI Review Report

Reviewed with Claude. Risks checked:

  • Wake/reload regression: hydration populates ptyIdsByTabId from the wake-hint sessionId at hydrate time (terminals.ts early-population path), so reload behavior matches today — verified by test, by reading the hydrate path, and by the wake-cycle observed in manual test 5.
  • Phase 2 retention-replay regression: the renderer-side retained-done path is unchanged for non-sleep flows; the main-process lastStatusByPaneKey replay survives renderer-only reloads (where setListener is not re-called) and is cleared on PTY teardown — both pre-existing behaviors. The PR only changes what sleep does to the renderer-side maps.
  • Migration filter at tabs.ts:1289: kept the || tab.ptyId != null clause and added a comment — this is a one-shot migration include, not a liveness check, and reconcile fires again post-reattach.
  • Suppressor-id hygiene: suppressedPtyExitIds (renderer ignore-list for PTY exit events) and retentionSuppressedPaneKeys (retention-side ignore-list) are distinct and unaffected by the unconditional drop. Phase 2's same-frame race (hook ping promotes working→done in the same frame as sleep) is guarded by suppressor-planting, which the unconditional path correctly re-enables.
  • All getWorktreeStatus callers audited: WorktreeCard and WorktreeJumpPalette both threaded; jump palette would otherwise desync from the sidebar (verified live in manual test 4).
  • Cross-platform: no shortcuts, paths, or shell behavior touched. Pure renderer state-derivation change plus a renderer-side store action signature shrink. macOS/Linux/Windows behavior is identical.

Security Audit

No new input handling, command execution, path handling, auth, secrets, dependency, or IPC surfaces. The change is renderer-only state derivation plus an unconditional drop in an existing shutdown path. No follow-up needed.

Notes

  • Out of scope: a separate asleep state in the WorktreeStatus enum (slept and never-started both render grey for now), and the parallel mobile/MCP worktree.ps fix in the main-process runtime — that path uses a different data shape (RuntimeLeafRecord) and is tracked as future work.
  • Out of scope: rate-limiting / acknowledgment flag on worktree.sleep RPC for scripted-sleep misuse. Tracked separately; the design doc adds a code comment above the RPC method as a stopgap.
  • Future: rename tab.ptyId → tab.lastSpawnSessionId to eliminate the wake-hint vs liveness footgun at the type level.

Made with Orca 🐋

brennanb2025 and others added 2 commits May 8, 2026 16:04
Sleep preserves tab.ptyId as a wake-hint sessionId, so the previous
liveness check (`tab.ptyId != null`) kept the workspace dot green and
agent rows as "working" until the 30-min stale TTL decayed them.
Switch liveness to ptyIdsByTabId (cleared by every pty.kill / sleep)
via a new tabHasLivePty helper, and drop live agentStatusByPaneKey
entries on sleep so the inline rows disappear with the dot. Retained
"done" rows survive — that signal is dismissed by the user, not the
system.

Co-authored-by: Orca <[email protected]>
@brennanb2025

Copy link
Copy Markdown
Contributor Author

Electron validation update:

I validated the main sleep-status renderer path in the Electron dev app via CDP.

What passed:

  • Exercised the real renderer store/action path for shutdownWorktreeTerminals, with only window.api.pty.kill stubbed to avoid killing real PTYs.
  • Confirmed sleeping a worktree clears PTY IDs, drops live agent rows, drops retained rows for that worktree including orphan retained rows, clears relevant ack entries, preserves other-worktree retained/ack rows, and plants the live-row retention suppressor.

Also ran:

  • pnpm vitest run --config config/vitest.config.ts src/renderer/src/components/sidebar/useWorktreeAgentRows.test.ts src/renderer/src/store/slices/store-cascades.test.ts src/renderer/src/store/slices/agent-status.test.ts src/renderer/src/store/slices/agent-status-ack-cleanup.test.ts src/renderer/src/components/sidebar/sleep-worktree-flow.test.ts passed: 5 files / 60 tests.
  • pnpm tc:web passed.
  • pnpm exec oxlint ... on the changed files passed.

Scope note: the Electron run covered the live renderer sleep transition. The pure row-derivation/orphan-retained cases are covered by unit tests; mobile/RPC and SSH-specific paths were not fully end-to-end exercised in that Electron run.

@brennanb2025
brennanb2025 merged commit 9290b25 into main May 10, 2026
2 checks passed
@brennanb2025
brennanb2025 deleted the brennanb2025/sleep-statuses branch May 10, 2026 23:02
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…blyai#1603)

* fix: gate worktree status on live PTYs so sleep reports inactive

Sleep preserves tab.ptyId as a wake-hint sessionId, so the previous
liveness check (`tab.ptyId != null`) kept the workspace dot green and
agent rows as "working" until the 30-min stale TTL decayed them.
Switch liveness to ptyIdsByTabId (cleared by every pty.kill / sleep)
via a new tabHasLivePty helper, and drop live agentStatusByPaneKey
entries on sleep so the inline rows disappear with the dot. Retained
"done" rows survive — that signal is dismissed by the user, not the
system.

Co-authored-by: Orca <[email protected]>

* fix: drop retained agent rows on worktree sleep

Co-authored-by: Orca <[email protected]>

* WIP: Changes before auto-review fixes

Co-authored-by: Orca <[email protected]>

* fix: preserve slept worktree status liveness

Co-authored-by: Orca <[email protected]>

* fix: treat slept pty hints as inactive

Co-authored-by: Orca <[email protected]>

* chore: remove sleep status planning docs

Co-authored-by: Orca <[email protected]>

---------

Co-authored-by: Orca <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant