Repository navigation
fix: zombie worktree PTY cleanup (issue #218) - #1376
Merged
Merged
Conversation
Worktree deletion only shut down renderer-tracked terminals, so PTYs owned by background tabs, split panes, or pre-reload sessions survived the removal and kept leaking memory. Introduce killAllProcessesForWorktree with three sweeps (runtime leaves, provider-prefix scan of daemon session ids, pty-registry by worktreeId) and wire it into both teardown paths: the CLI-initiated removeManagedWorktree and the renderer-initiated worktrees:remove IPC handler. OrcaRuntimeService gets a lazy getLocalProvider thunk so construction order stays robust. Co-authored-by: Orca <help@stably.ai>
…sign §4.4) When a worktree is deleted, ~25 worktree-scoped maps (tabsByWorktree, git caches, browser state, split-tab models, per-file editor drafts, etc.) kept references to the gone worktree, so SessionsStatusSegment kept mis-classifying orphaned PTYs as bound and dropdowns rendered stale ids. Add purgeWorktreeTerminalState as a single atomic action that wipes every scoped map plus cascades top-level actives. Fire it from the worktrees:changed listener on the set-diff of removed ids, and once more at hydration via fetchAllWorktrees to clean up persisted entries from pre-fix sessions. The hydration-time purge is gated behind a per-repo success check: a single transient IPC error or an all-empty fetch defers the purge so a degraded launch cannot wipe legitimate persisted state. Co-authored-by: Orca <help@stably.ai>
Adds tests for every layer of the zombie-worktree fix: - worktree-teardown: unit coverage of the three-sweep helper including best-effort error swallowing across provider/registry. - orca-runtime: RPC-initiated removeManagedWorktree kills PTYs before any git mutation + verifies the lazy getLocalProvider thunk resolves on each call. - worktrees IPC: renderer-initiated remove kills PTYs before git and skips the kill helper for SSH-backed repos. - renderer slice: fetchAllWorktrees defers the purge when any sibling repo fetch fails or every repo returns empty (F1 regression); happy-path fires the purge once and does not re-run on subsequent calls. Direct purgeWorktreeTerminalState coverage pins the cascade across worktree-keyed, tab-id-keyed, and file-id-keyed maps. Co-authored-by: Orca <help@stably.ai>
…ility) Breadcrumb lets ops distinguish a renderer-state-induced leak (diff-path purge non-empty) from a backend-induced one (nothing to kill but memory still pinned). Emit only when the sweep actually shut anything down so steady-state logs stay quiet. Added at both call sites — removeManagedWorktree (CLI path) and the worktrees:remove IPC handler. Co-authored-by: Orca <help@stably.ai>
…on type The purge unit test seeded ptyIdsByTabId as Record<string, string> when the runtime type is Record<string, string[]>. The unit tests passed because they never hit the UI renderer, but live e2e surfaced a TypeError: (ptyIdsByTabId[tabId] ?? []).some is not a function. Corrected to arrays; 21/21 tests still pass. Co-authored-by: Orca <help@stably.ai>
Contributor
Author
E2E verification in live dev Orca — completeRan a full e2e smoke in a live dev build attached via CDP (using the Electron SKILL workflow). Verified each design-doc mechanism with concrete evidence: 1. Backend teardown (design §4.3) — tri-layer PTY kill
2. Renderer hydration-time purge (design §4.4)
3. F1 regression — 0-repo safety
4. Direct purge action
Final store stateAfter worktree removal, the live store showed: — clean, consistent, no zombie state. Extra commit
Verified on an isolated dev userData ( |
enrick-dev
pushed a commit
to enrick-dev/orca
that referenced
this pull request
Oct 15, 2026
* fix(runtime): kill all PTYs for a worktree on removal (design §4.3) Worktree deletion only shut down renderer-tracked terminals, so PTYs owned by background tabs, split panes, or pre-reload sessions survived the removal and kept leaking memory. Introduce killAllProcessesForWorktree with three sweeps (runtime leaves, provider-prefix scan of daemon session ids, pty-registry by worktreeId) and wire it into both teardown paths: the CLI-initiated removeManagedWorktree and the renderer-initiated worktrees:remove IPC handler. OrcaRuntimeService gets a lazy getLocalProvider thunk so construction order stays robust. Co-authored-by: Orca <help@stably.ai> * fix(renderer): purge worktree-scoped state on removal + hydration (design §4.4) When a worktree is deleted, ~25 worktree-scoped maps (tabsByWorktree, git caches, browser state, split-tab models, per-file editor drafts, etc.) kept references to the gone worktree, so SessionsStatusSegment kept mis-classifying orphaned PTYs as bound and dropdowns rendered stale ids. Add purgeWorktreeTerminalState as a single atomic action that wipes every scoped map plus cascades top-level actives. Fire it from the worktrees:changed listener on the set-diff of removed ids, and once more at hydration via fetchAllWorktrees to clean up persisted entries from pre-fix sessions. The hydration-time purge is gated behind a per-repo success check: a single transient IPC error or an all-empty fetch defers the purge so a degraded launch cannot wipe legitimate persisted state. Co-authored-by: Orca <help@stably.ai> * test(zombie-worktree): regression coverage for design §4.5 Adds tests for every layer of the zombie-worktree fix: - worktree-teardown: unit coverage of the three-sweep helper including best-effort error swallowing across provider/registry. - orca-runtime: RPC-initiated removeManagedWorktree kills PTYs before any git mutation + verifies the lazy getLocalProvider thunk resolves on each call. - worktrees IPC: renderer-initiated remove kills PTYs before git and skips the kill helper for SSH-backed repos. - renderer slice: fetchAllWorktrees defers the purge when any sibling repo fetch fails or every repo returns empty (F1 regression); happy-path fires the purge once and does not re-run on subsequent calls. Direct purgeWorktreeTerminalState coverage pins the cascade across worktree-keyed, tab-id-keyed, and file-id-keyed maps. Co-authored-by: Orca <help@stably.ai> * fix(runtime): log worktree-teardown kill counts (design §4.4 observability) Breadcrumb lets ops distinguish a renderer-state-induced leak (diff-path purge non-empty) from a backend-induced one (nothing to kill but memory still pinned). Emit only when the sweep actually shut anything down so steady-state logs stay quiet. Added at both call sites — removeManagedWorktree (CLI path) and the worktrees:remove IPC handler. Co-authored-by: Orca <help@stably.ai> * test(zombie-worktree): fix ptyIdsByTabId seed shape to match production type The purge unit test seeded ptyIdsByTabId as Record<string, string> when the runtime type is Record<string, string[]>. The unit tests passed because they never hit the UI renderer, but live e2e surfaced a TypeError: (ptyIdsByTabId[tabId] ?? []).some is not a function. Corrected to arrays; 21/21 tests still pass. Co-authored-by: Orca <help@stably.ai> --------- Co-authored-by: Orca <help@stably.ai>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
removeManagedWorktreeor theworktrees:removeIPC handler;this.leavesis renderer-sourced and empty for headless CLI flows.killAllProcessesForWorktreewith three cooperating sweeps (runtime leaves, provider-prefix scan of daemon${worktreeId}@@<hex>session ids,pty-registryby worktreeId) and wire it into both teardown paths.OrcaRuntimeServicegets a lazygetLocalProviderthunk so construction order stays robust.purgeWorktreeTerminalStateto the renderer store so the ~25 worktree-scoped maps (tabs, git caches, browser state, split layouts, editor drafts, etc.) get wiped symmetrically on delete notifications and at hydration, surfacing zombies theSessionsStatusSegmentwas misclassifying as bound.Design doc:
DESIGN_DOC_ZOMBIE_WORKTREE_FIX.md(§4.3, §4.4, §4.5)Investigation:
INVESTIGATION.mdKey design decisions
initDaemonPtyProvider()runs AFTEROrcaRuntimeServiceis built, so a captured reference would point at the pre-daemonLocalPtyProvider. The thunk() => getLocalPtyProvider()resolves at teardown time.getLocalPtyProvider()return type widened fromLocalPtyProvidertoIPtyProvider— no external caller reads concrete-class-only members (grep-verified), and the teardown helper only needslistProcesses()/shutdown()from the shared interface.fetchWorktreesswallows errors and short-circuits on empty returns. A single transient git error at launch would otherwise wipe every persistedtabsByWorktreeentry for the affected repo. The purge is gated on "every repo fetched successfully AND at least one returned ≥1 worktree"; if degraded, it defers and retries on the nextfetchAllWorktrees.repo.connectionIdis set.killagainst a dead session is a no-op + tombstone;IPtyProvider.shutdownon an exited session is a no-op). Double kill beats missing a session.Commits (4 logical chunks)
4823b245— fix(runtime): kill all PTYs for a worktree on removal (§4.3 helper + wire-up)eb739de9— fix(renderer): purge worktree-scoped state on removal + hydration (§4.4)e8378ded— test(zombie-worktree): regression coverage for §4.5 (91 tests across 4 files)db06145e— fix(runtime): log worktree-teardown kill counts (observability per §4.4)Test plan
Automated (run in CI)
pnpm run typecheck— cleanpnpm run lint— 0 warnings / 0 errorspnpm test— 3841 pass / 7 skipped; the 6 pre-existing failures insrc/main/providers/local-pty-shell-ready.test.tsare hardcoded to/Users/aliceand are unrelated (confirmed viagit stash)worktree-teardown.test.ts,orca-runtime.test.ts,ipc/worktrees.test.ts,store/slices/worktrees.test.ts) all pass, covering:worktrees:changed(design §4.5 Add Ctrl+Click to open links in terminal #4)hasHydratedWorktreePurgeidempotence (design §4.5 Add Ctrl+Click to open links in terminal #4)purgeWorktreeTerminalStatecoverage across worktree-keyed, tab-id-keyed, file-id-keyed mapsManual validation (for reviewer — not run locally because another Orca instance was holding the single-instance lock during test)
orca worktree rm --worktree <id>from another shell, watch Activity Monitor. Before fix: ~560 MB shell persists. After fix: count drops to baseline and[worktree-teardown] <id> killed runtime=N provider=M registry=Kappears in main-process logs.tabsByWorktreeentry; the status-bar orphan count lights up for the still-live daemon session; the "Kill N Orphans" button reclaims it.git worktree listtransient-empty case — delete a worktree while the git index is locked (e.g. during a long rebase) and verify persisted tab state is NOT wiped.Rollback
Each commit is revertible independently. If the renderer purge (§4.4) causes a regression, it can be reverted alone — zombies would still be reclaimed on next app restart via the daemon's own
reconcileOnStartup. If the backend sweep (§4.3) causes unexpected daemon churn, the helper can be narrowed to the runtime-leaves path while the registry sweep keeps local coverage intact.🤖 Generated with Claude Code
Made with Orca 🐋