Repository navigation
fix(worktrees): a host-scoped purge leaves a surviving twin's tabs and edits alone - #30030
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Priority: ➖ Normal Merge Risk: 🟡 Moderate · up to
|
There was a problem hiding this comment.
Actionable comments posted: 2
ℹ️ Review info
⚙️ Run configuration
- Configuration used: Repository UI
- Review profile: CHILL
- Plan: Advanced
- Run ID:
41e2ca9f-f8d0-497c-975d-1440b507196d
📒 Files selected for processing: 20
config/focus-setting-read-baseline.txtsrc/renderer/src/hooks/ipc-events/worktree-event-lineage-refresh.test.tssrc/renderer/src/hooks/ipc-events/worktree-event-runtime.tssrc/renderer/src/store/folder-workspaces/folder-workspace-catalog.tssrc/renderer/src/store/host-twin-isolation.test.tssrc/renderer/src/store/host-twin-worktree-purge.test.tssrc/renderer/src/store/repos/repo-removal-host-twin-teardown.tssrc/renderer/src/store/slices/tabs/kept-worktree-surface-pruning.tssrc/renderer/src/store/slices/tabs/worktree-layout-lifecycle.tssrc/renderer/src/store/slices/worktree-purge-target.tssrc/renderer/src/store/slices/worktrees/listing/fetched-worktree-merge.tssrc/renderer/src/store/slices/worktrees/session/worktree-slice-lookups.tssrc/renderer/src/store/slices/worktrees/teardown/host-twin-worktree-purge.tssrc/renderer/src/store/slices/worktrees/teardown/purge-stale-runtime-host-state.tssrc/renderer/src/store/slices/worktrees/teardown/purged-structured-launch-retirement.tssrc/renderer/src/store/slices/worktrees/teardown/worktree-purge-doomed-ids.tssrc/renderer/src/store/slices/worktrees/teardown/worktree-purge-state.tssrc/renderer/src/store/slices/worktrees/teardown/worktree-purge-surface-patch.tssrc/shared/worktree/host-qualified-identity.test.tssrc/shared/worktree/host-qualified-identity.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 1 remain after this review.
| function pruneGroup(group: TabGroup, removedIds: ReadonlySet<string>): TabGroup { | ||
| if (!group.tabOrder.some((id) => removedIds.has(id))) { | ||
| return group | ||
| } | ||
| const tabOrder = group.tabOrder.filter((id) => !removedIds.has(id)) | ||
| const recentTabIds = sanitizeRecentTabIds(group.recentTabIds, tabOrder) | ||
| // Why MRU first: closing the active tab lands on the previously active one, as closeUnifiedTab does. | ||
| const activeTabId = | ||
| group.activeTabId && removedIds.has(group.activeTabId) | ||
| ? (recentTabIds.at(-1) ?? tabOrder[0] ?? null) | ||
| : group.activeTabId | ||
| return { | ||
| ...group, | ||
| tabOrder, | ||
| activeTabId, | ||
| ...(group.recentTabIds ? { recentTabIds } : {}) | ||
| } | ||
| } |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
#!/bin/bash
rg -nP -C3 'tabOrder\.length\s*===\s*0|collapseEmptyGroup|removeEmptyGroup|closeEmptyGroup' --type=ts src/renderer/src/store src/renderer/src/components/tab-groupRepository: stablyai/orca
Length of output: 8427
🏁 Script executed:
set -o pipefail
printf '%s\n' '--- changed file ---'
nl -ba src/renderer/src/store/slices/tabs/kept-worktree-surface-pruning.ts | sed -n '1,260p'
printf '%s\n' '--- group actions ---'
nl -ba src/renderer/src/store/slices/tabs/tabs-group-actions.ts | sed -n '1,190p'
printf '%s\n' '--- layout removal references ---'
rg -n -F --glob '*.ts' -e 'removeWorktreeLayout' -e 'layoutByWorktree' -e 'activeGroupIdByWorktree' src/renderer/src/store | head -200
printf '%s\n' '--- pruner callers ---'
rg -n -F --glob '*.ts' -e 'createKeptWorktreeSurfacePruners' -e 'KeptWorktreeSurfaceRemoval' src/renderer/src
printf '%s\n' '--- relevant diff ---'
git diff --no-ext-diff --unified=30 03cefcb84cbb99b287135f226ae961baf5c170f1 df665a2523b4d40ac3ff652863856c74950393fa -- src/renderer/src/store/slices/tabs/kept-worktree-surface-pruning.ts src/renderer/src/store/slices/tabs/tabs-group-actions.ts src/renderer/src/store | sed -n '1,900p'Repository: stablyai/orca
Length of output: 42302
🏁 Script executed:
set -o pipefail
printf '%s\n' '--- layout tree and collapse behavior ---'
nl -ba src/shared/workspace-layout/tab-group-layout-tree.ts | sed -n '1,280p'
printf '%s\n' '--- renderer layout consumers ---'
rg -n -F --glob '*.{ts,tsx}' -e 'layoutByWorktree' -e 'groupsByWorktree' -e 'groupId' src/renderer/src/components src/renderer/src/store | head -260
printf '%s\n' '--- empty-group and group-tab rendering checks ---'
rg -n -P --glob '*.{ts,tsx}' 'tabOrder\.length|group\.tabOrder|groups\.find|find\(\(.*group|activeGroupIdByWorktree|collapseGroupLayout' src/renderer/src/components src/renderer/src/store src/shared/workspace-layout | head -300
printf '%s\n' '--- layout writer/persistence ---'
rg -n -P --glob '*.{ts,tsx}' 'layoutByWorktree|workspace.?layout|write.*layout|persist.*layout|serialize.*layout' src/renderer/src src/shared | head -300Repository: stablyai/orca
Length of output: 41712
🏁 Script executed:
set -o pipefail
printf '%s\n' '--- terminal pane files ---'
rg --files src/renderer/src/components | rg -i 'terminal.*pane|pane.*layout|workspace.*layout|tab.*group'
printf '%s\n' '--- pane rendering references ---'
rg -n -F --glob '*.{ts,tsx}' -e 'TerminalPane' -e 'groupId:' -e 'groupsByWorktree[worktreeId]' src/renderer/src/components/terminal-pane src/renderer/src/components 2>/dev/null | head -220
printf '%s\n' '--- reconciliation implementation ---'
rg -n -F --glob '*.ts' -e 'nextGroupsWithEmpty' -e 'tabs-reconciliation' -e 'groupsWithEmpty' src/renderer/src/store
nl -ba src/renderer/src/store/slices/tabs/tabs-reconciliation.ts 2>/dev/null | sed -n '1,300p'
printf '%s\n' '--- workspace binding owner ---'
nl -ba src/renderer/src/components/use-terminal-workspace-store-bindings.ts | sed -n '1,180p'Repository: stablyai/orca
Length of output: 41780
🏁 Script executed:
set -o pipefail
printf '%s\n' '--- purge and layout-patch callers ---'
rg -n -F --glob '*.ts' -e 'purgeWorktreeTerminalState' -e 'removeWorktreeLayout(' -e 'reconcileWorktreeTabModel(' src/renderer/src/store src/renderer/src/components | head -260
printf '%s\n' '--- purge implementation context ---'
rg -n -F --glob '*.ts' -e 'function purgeWorktreeTerminalState' -e 'purgeWorktreeTerminalState:' -e 'create.*Purge' src/renderer/src/store
printf '%s\n' '--- layout writer sequencing ---'
rg -n -P --glob '*.ts' 'write.*(Layout|layout)|persist.*(Layout|layout)|layoutByWorktree.*=' src/renderer/src/store src/renderer/src/runtime | head -220Repository: stablyai/orca
Length of output: 15720
🏁 Script executed:
set -o pipefail
nl -ba src/renderer/src/store/slices/worktrees/teardown/host-twin-worktree-purge.ts | sed -n '160,250p'
nl -ba src/renderer/src/store/slices/worktrees/teardown/worktree-purge-state.ts | sed -n '90,165p'
nl -ba src/renderer/src/store/slices/tabs/tabs-session-actions.ts | sed -n '1,100p'Repository: stablyai/orca
Length of output: 12688
Collapse empty split groups after host-twin pruning.
pruneGroup can leave a non-root group with tabOrder: []. The host-twin purge updates groupsByWorktree, but it leaves the retained worktree's layout unchanged. This can leave a stale split-layout leaf for a group with no tabs.
Remove or collapse emptied non-root groups during host-twin pruning. Preserve the canonical empty root group used by zero-tab worktrees.
| const removed = collectRemovedHostTwinState(s, twinIds, hostId, twinHostIds) | ||
| const canonical = (worktreeId: string) => | ||
| removedHostCanonicalizer(s, worktreeId, hostId, twinHostIds.get(worktreeId) ?? []) | ||
| const cancelledSessionIds = cancelStructuredLaunchesForWorktrees( | ||
| twinIds, | ||
| (worktreeId, launchHostId) => canonical(worktreeId)(launchHostId) === hostId | ||
| ) | ||
| const terminalTabs: { id: string; ptyId: string | null }[] = [] | ||
| for (const worktreeId of twinIds) { | ||
| const legacyTabs = (s.tabsByWorktree[worktreeId] ?? []).filter((tab) => | ||
| removed.terminalTabIds.has(tab.id) | ||
| ) | ||
| terminalTabs.push(...legacyTabs) | ||
| retirePurgedTabLaunches( | ||
| s, | ||
| worktreeId, | ||
| legacyTabs, | ||
| (s.unifiedTabsByWorktree[worktreeId] ?? []).filter((tab) => removed.otherTabIds.has(tab.id)), | ||
| cancelledSessionIds | ||
| ) | ||
| } | ||
| // Why: a hydrated terminal can exist only as a unified tab, with no legacy row. | ||
| for (const tabId of removed.terminalTabIds) { | ||
| if (!terminalTabs.some((tab) => tab.id === tabId)) { | ||
| terminalTabs.push({ id: tabId, ptyId: null }) | ||
| } | ||
| } | ||
| // Why: a preview keys its source's editor state, unless a surviving file still uses that source. | ||
| const survivingFiles = s.openFiles.filter((file) => !removed.fileIds.has(file.id)) | ||
| const previewSourceIds = s.openFiles.flatMap((file) => | ||
| removed.fileIds.has(file.id) && | ||
| file.markdownPreviewSourceFileId && | ||
| !survivingFiles.some( | ||
| (other) => | ||
| other.id === file.markdownPreviewSourceFileId || | ||
| other.markdownPreviewSourceFileId === file.markdownPreviewSourceFileId | ||
| ) | ||
| ? [file.markdownPreviewSourceFileId] | ||
| : [] | ||
| ) | ||
| const doomed = collectSurfacePurgeDoomedIds(s, { | ||
| tabs: terminalTabs, | ||
| browserWorkspaceIds: removed.browserTabIds, | ||
| fileIds: [...removed.fileIds, ...previewSourceIds] | ||
| }) | ||
| const omitters = createWorktreePurgeOmitters(s, new Set(), doomed) | ||
| const tabPrefixedSleeping = { | ||
| sleepingAgentSessionsByPaneKey: omitters.omitByPaneKeyTabPrefix( | ||
| s.sleepingAgentSessionsByPaneKey | ||
| ), | ||
| agentLaunchConfigByPaneKey: omitters.omitByPaneKeyTabPrefix(s.agentLaunchConfigByPaneKey) | ||
| } | ||
| // Why SSH only: a record whose tab is gone names its host just by connection; null is ambiguous. | ||
| const sshRecordPaneKeys = Object.entries(s.sleepingAgentSessionsByPaneKey ?? {}).flatMap( | ||
| ([paneKey, record]) => | ||
| twinIds.has(record.worktreeId) && | ||
| record.connectionId && | ||
| canonical(record.worktreeId)(toSshExecutionHostId(record.connectionId)) === hostId | ||
| ? [paneKey] | ||
| : [] | ||
| ) | ||
| const layout = removeWorktreeLayout(s, { | ||
| worktreeIds: new Set(), | ||
| tabIds: doomed.doomedTabIds, | ||
| activeFileCleared: s.activeFileId != null && removed.fileIds.has(s.activeFileId), | ||
| kept: { | ||
| worktreeIds: twinIds, | ||
| terminalTabIds: removed.terminalTabIds, | ||
| fileIds: removed.fileIds, | ||
| browserTabIds: removed.browserTabIds, | ||
| unifiedTabIds: removed.otherTabIds | ||
| } | ||
| }) |
There was a problem hiding this comment.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
rg -nP -C4 '\bmixedTabPtyIds\b' --type=ts src/renderer/src
sed -n '130,239p' src/renderer/src/store/slices/worktrees/teardown/host-twin-worktree-purge.ts
sed -n '160,200p' src/renderer/src/store/repos/repo-removal-host-twin-teardown.tsRepository: stablyai/orca
Length of output: 11090
🏁 Script executed:
rg -n -F --glob '*.ts' -e 'collectRemovedHostTwinState' -e 'buildHostTwinPurgeState' -e 'mixedTabPtyIds' -e 'ptyIdsByTabId' -e 'terminalLayoutsByTabId' -e 'suppressedPtyExitIds' -e 'pendingCodexPaneRestartIds' -e 'codexRestartNoticeByPtyId' src/renderer/src || test "$?" -eq 1Repository: stablyai/orca
Length of output: 45667
🏁 Script executed:
set -o pipefail
printf '%s\n' '--- purge symbols in production source ---'
rg -n -F --glob '*.ts' --glob '!*.test.ts' --glob '!*.spec.ts' \
-e 'collectRemovedHostTwinState' -e 'buildHostTwinPurgeState' -e 'createWorktreePurgeOmitters' \
-e 'buildWorktreePurgeSurfacePatch' -e 'collectSurfacePurgeDoomedIds' -e 'mergeFetchedWorktrees' \
-e 'shutdownWorktreeTerminals' -e 'ptyIdsByTabId' \
src/renderer/src/store src/renderer/src/runtime src/renderer/src/components/terminal-pane || test "$?" -eq 1
printf '%s\n' '--- host twin purge source ---'
nl -ba src/renderer/src/store/slices/worktrees/teardown/host-twin-worktree-purge.ts | sed -n '1,380p'
printf '%s\n' '--- attribution source ---'
nl -ba src/renderer/src/store/repos/repo-removal-host-twin-attribution.ts | sed -n '175,240p'
printf '%s\n' '--- helper definitions ---'
rg -l -F --glob '*.ts' --glob '!*.test.ts' --glob '!*.spec.ts' \
-e 'function createWorktreePurgeOmitters' -e 'function collectSurfacePurgeDoomedIds' -e 'function buildWorktreePurgeSurfacePatch' \
src/renderer/src | while read -r f; do
echo "FILE $f"
rg -n -F -e 'function createWorktreePurgeOmitters' -e 'function collectSurfacePurgeDoomedIds' -e 'function buildWorktreePurgeSurfacePatch' "$f"
doneRepository: stablyai/orca
Length of output: 37209
🏁 Script executed:
set -o pipefail
printf '%s\n' '--- doomed ids ---'
nl -ba src/renderer/src/store/slices/worktrees/teardown/worktree-purge-doomed-ids.ts | sed -n '1,130p'
printf '%s\n' '--- omitters ---'
nl -ba src/renderer/src/store/slices/worktrees/teardown/worktree-purge-omitters.ts | sed -n '1,220p'
printf '%s\n' '--- surface patch ---'
nl -ba src/renderer/src/store/slices/worktrees/teardown/worktree-purge-surface-patch.ts | sed -n '1,180p'
printf '%s\n' '--- purge state ---'
nl -ba src/renderer/src/store/slices/worktrees/teardown/worktree-purge-state.ts | sed -n '1,190p'
printf '%s\n' '--- purge lifecycle references ---'
rg -n -F --glob '*.ts' --glob '!*.test.ts' --glob '!*.spec.ts' \
-e 'purgeWorktrees' -e 'buildWorktreePurgeState' -e 'removedWorktreeRendererTeardown' \
-e 'shutdownWorktreeTerminals' -e 'mergeFetchedWorktrees' \
src/renderer/src/store/slices/worktrees src/renderer/src/store/repos src/renderer/src/store/terminalsRepository: stablyai/orca
Length of output: 27843
🏁 Script executed:
set -o pipefail
printf '%s\n' '--- layout removal ---'
rg -n -F --glob '*.ts' --glob '!*.test.ts' --glob '!*.spec.ts' -e 'export function removeWorktreeLayout' -e 'function removeWorktreeLayout' src/renderer/src
nl -ba src/renderer/src/store/slices/tabs/worktree-layout-lifecycle.ts | sed -n '1,230p'
printf '%s\n' '--- fetched merge purge and kill paths ---'
nl -ba src/renderer/src/store/slices/worktrees/listing/fetched-worktree-merge.ts | sed -n '230,315p'
nl -ba src/renderer/src/store/repos/repo-removal-host-twin-teardown.ts | sed -n '100,205p'
printf '%s\n' '--- PTY release and exit-related cleanup ---'
nl -ba src/renderer/src/store/terminals/terminal-pty-release.ts | sed -n '1,125p'
nl -ba src/renderer/src/store/terminals/terminal-shutdown-guards.ts | sed -n '70,115p'
printf '%s\n' '--- reconnect consumers ---'
nl -ba src/renderer/src/store/terminals/workspace-terminal-reconnect.ts | sed -n '1,145p'
nl -ba src/renderer/src/components/terminal-pane/pty-connection/pane-pty-visibility-bind.ts | sed -n '70,120p'Repository: stablyai/orca
Length of output: 35920
🏁 Script executed:
set -o pipefail
printf '%s\n' '--- host purge tests and mixed-tab coverage ---'
rg -n -F --glob '*host-twin*' --glob '*.ts' -e 'mixedTabPtyIds' -e 'ptyIdsByTabId' -e 'terminalLayoutsByTabId' -e 'buildHostTwinPurgeState' src/renderer/src/store || test "$?" -eq 1
printf '%s\n' '--- direct layout/pty binding consumers ---'
rg -n -F --glob '*.ts' --glob '!*.test.ts' --glob '!*.spec.ts' \
-e 'ptyIdsByLeafId' -e 'terminalLayoutsByTabId[tab.id]' -e 'terminalLayoutsByTabId?.[tab.id]' \
src/renderer/src/components/terminal-pane src/renderer/src/store/terminals | head -n 180
printf '%s\n' '--- orphan/reconciliation production paths ---'
rg -n -F --glob '*.ts' --glob '!*.test.ts' --glob '!*.spec.ts' \
-e 'orphan' -e 'reconcile' -e 'pty.kill' -e 'ptyIdsByTabId' \
src/renderer/src/store/slices/terminal-orphan-helpers.ts \
src/renderer/src/runtime/web-session-terminal-orphan-recovery-surface.ts \
src/renderer/src/store/slices/worktrees/listing/fetched-worktree-merge.ts \
src/renderer/src/store/terminals \
src/renderer/src/components/terminal-pane | head -n 260Repository: stablyai/orca
Length of output: 42634
🏁 Script executed:
set -o pipefail
printf '%s\n' '--- orphan helper implementation ---'
nl -ba src/renderer/src/store/slices/terminal-orphan-helpers.ts | sed -n '1,220p'
printf '%s\n' '--- mixed-tab tests ---'
for f in \
src/renderer/src/store/bulk-worktree-purge-terminal-maps-leak.test.ts \
src/renderer/src/store/project-removal-host-twin-state.test.ts \
src/renderer/src/store/host-twin-worktree-purge.test.ts; do
if test -f "$f"; then
echo "FILE $f"
nl -ba "$f" | sed -n '1,260p'
fi
done
printf '%s\n' '--- direct retained-layout consumers ---'
nl -ba src/renderer/src/components/terminal-pane/terminal-handle-links.ts | sed -n '90,145p'
nl -ba src/renderer/src/components/terminal-pane/terminal-live-layout-reconciliation.ts | sed -n '1,150p'
nl -ba src/renderer/src/components/terminal-pane/terminal-pane-frame-reconciliation.ts | sed -n '1,130p'Repository: stablyai/orca
Length of output: 33288
🏁 Script executed:
set -o pipefail
printf '%s\n' '--- PTY binding mutation helpers ---'
nl -ba src/renderer/src/store/terminals/terminal-pty-bindings.ts | sed -n '1,225p'
nl -ba src/renderer/src/store/terminals/terminal-layout-state.ts | sed -n '1,180p'
printf '%s\n' '--- existing per-PTY filtering helpers ---'
rg -n -F --glob '*.ts' --glob '!*.test.ts' --glob '!*.spec.ts' \
-e 'ptyIdsByLeafId' -e 'omitByPtyId' -e 'filter((ptyId)' -e 'filter((id)' \
src/renderer/src/store/slices src/renderer/src/store/terminals | head -n 240
printf '%s\n' '--- mixed/shared host-twin test references ---'
rg -n -i --glob '*.ts' --glob '*host*twin*' -e 'mixed' -e 'shared.*pty' -e 'pty.*shared' -e 'remotePty' src/renderer/src/store || test "$?" -eq 1Repository: stablyai/orca
Length of output: 41530
🏁 Script executed:
nl -ba src/renderer/src/store/slices/terminal-session-row-hydration.ts | sed -n '1,135p'
rg -n -F --glob '*.ts' --glob '!*.test.ts' --glob '!*.spec.ts' \
-e 'releasedPtyIds' -e 'filterReleased' -e 'ptyIdsByLeafId' \
src/renderer/src/store/slices/terminal-session-row-hydration.ts src/renderer/src/store/slices/worktrees/teardownRepository: stablyai/orca
Length of output: 8257
Release mixed-tab PTYs during the host-scoped purge.
collectRemovedHostTwinState records the removed host's PTYs in removed.mixedTabPtyIds, but buildHostTwinPurgeState only builds doomed IDs from wholly removed tabs. The surviving tab therefore keeps those PTYs in ptyIdsByTabId and terminalLayoutsByTabId[tab].ptyIdsByLeafId; PTY-keyed cleanup also misses them. The orphan sweep treats the retained tab as live, and terminal-handle lookup can resolve the dead PTY. The host-scoped fetch path reaches this code without terminal teardown.
At host-twin-worktree-purge.ts:186-237, release removed.mixedTabPtyIds from the retained tab's PTY index, layout leaf bindings, tab-level reconnect hints, and PTY-keyed maps. Keep the tab and the surviving host's PTYs. Do not add the shared tab to doomedTabIds.
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
- Configuration used: Repository UI
- Review profile: CHILL
- Plan: Advanced
- Run ID:
1ab3caf0-c524-4e17-ac39-13751489e462
📒 Files selected for processing: 2
src/renderer/src/store/host-twin-worktree-purge.test.tssrc/renderer/src/store/slices/worktrees/teardown/host-twin-worktree-purge.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
| for (const tab of s.tabsByWorktree[worktreeId] ?? []) { | ||
| if (!doomed.doomedTabIds.has(tab.id)) { | ||
| for (const ptyId of collectPtyIdsForTab(s, tab.id, tab.ptyId)) { |
There was a problem hiding this comment.
🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift
Preserve PTYs bound to surviving unified-only terminals.
If a surviving terminal exists only in unifiedTabsByWorktree and binds a removed tab’s PTY, this loop never inspects that terminal. The PTY remains in doomed.doomedPtyIds, so the purge removes PTY-keyed state that the surviving terminal still needs. Include surviving unified terminal IDs when checking PTY bindings, and cover this case in the mixed-host test.
There was a problem hiding this comment.
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
- Configuration used: Repository UI
- Review profile: CHILL
- Plan: Advanced
- Run ID:
69907613-0745-4076-87f0-2cdf8d645820
📒 Files selected for processing: 2
src/renderer/src/store/host-twin-worktree-purge.test.tssrc/renderer/src/store/slices/tabs/worktree-layout-lifecycle.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
312dd36 to
0b29251
Compare
ELI5
Sometimes the same project folder is open twice in Orca: once on this computer and once on a remote machine (over SSH or on a paired Orca server). Both copies share one internal name. When Orca learned that one copy was gone, for example because the SSH host's scan stopped listing it, it threw away the open tabs and unsaved edits filed under that shared name. That included the copy that was still there. Now Orca throws away only what clearly belongs to the copy that went away.
What Changed
Before: a local checkout and an SSH (or paired-server) checkout of the same repo path share the worktree id
repoId::path. If the SSH host's authoritative scan dropped that worktree, the purge wiped every tab, terminal, dirty document, draft, git cache and sleeping-agent record under the id. The local copy's work went with it, and it happened the other way round too. Folder workspaces published by two hosts had the same problem.After: when a purge names a host and another host still publishes the same id, only state that can be tied to the purged host is removed:
State that cannot be tied to either host is kept for the surviving copy: tabs with no PTY, files with no provenance, worktree-keyed buckets such as git status, and the raw-id module stores (sleep intent, huge-repo warning dismissals, ambiguous-owner warnings, hosted-review generations, structured launches). If the active surface was removed, selection is derived again. If it pointed at the purged host, it moves to the survivor. When no other host publishes the id, the purge does exactly what it did before. A raw id string still purges everything.
Mechanism:
HostWorktreeTargettype, with its constructorhostWorktreeTarget(row), sits next toHostScopedWorktreeKeyinsrc/shared/worktree/host-qualified-identity.ts.purgeWorktreeTerminalStateandbuildWorktreePurgeStatenow takereadonly (string | HostWorktreeTarget)[]. The old untyped{ id, hostId }form is gone, so every host-scoped caller has to build the brand.buildWorktreePurgeStatelooks for surviving publishers using the listing's own host matching (worktreeMatchesHost). Because of that, an unstamped legacy row, or a paired runtime's SSH row, counts as the purged copy and not as a twin. For folders, an unstamped folder under a project group that two hosts publish counts as either host's.host-twin-worktree-purge.ts. It reuses the attribution from project removal (fix(projects): removing one host's copy of a project keeps its same-path twin's shells, tabs and edits #29954) (collectRemovedHostTwinState,removedHostCanonicalizer,twinSelectionPatch) rather than writing a second one.worktree-purge-surface-patch.ts, so both paths share it. The full path's output is field-for-field the same.removeWorktreeLayoutwriter, which takes a new optionalkeptargument. This adds no new renderer layout write sites.Caller audit:
fetched-worktree-merge.tspasses a host (hostWorktreeTarget). This is the authoritative-scan removal, where the real-world leak happened.worktree-event-runtime.tspasses a host when the event names one, and a raw id otherwise.folder-workspace-catalog.ts(reconcileFailedFolderWorkspaceUpdate) now passes the owner host. It used to pass a raw key even though it had just removed only that host's folder row.purge-stale-runtime-host-state.tskeeps the full purge (buildFullWorktreePurgeState). It already drops survivor ids unless the restored-session partition proves the bare-id state belonged to the removed host.repo-removal.ts(exclusive ids only; twins go through fix(projects): removing one host's copy of a project keeps its same-path twin's shells, tabs and edits #29954's teardown),fetch-all-worktrees.ts(hydration-stale keys), andfolder-workspace-mutations.ts(purges only once no folder row remains).Twin suite: all eight "purge terminal state" cells now assert isolation. That covers local+ssh, local+runtime, folder local+ssh and folder group local+ssh, targeting either host, and their
KNOWN_TWIN_LEAKSentries are deleted. Kill, sleep and select remain known leaks; they are the next slice.Why
The shared buckets stay keyed by the raw id on purpose. Re-keying around 55 store fields to host-qualified keys (rejected in design decision D7) would change behaviour for twins and change the persisted session payloads. A lint baseline on raw-id maps (D6/D51) would start at about 108 entries and would not catch this bug at all: the purge passes ids through a
Set, which such a lint cannot see.Typing the destructive entry point with a branded target, and filtering the shared buckets by owning host, fixes the bug with no wire change and no on-disk change. When the purge cannot tell which host owns something, it keeps it: an unattributable entry can at worst be left stale, but it can never cost the surviving copy its data.
Reusing #29954's attribution keeps project removal and purge on one definition of "this host's state". Its planned shrink (tracker follow-up) will then cover both paths.
Ratchets:
worktree-purge-state.tstopurged-structured-launch-retirement.ts, where the samestructuredAgentSessionTargetForTabcall now lives. Five caller counts drop, because the discovery no longer propagates through the moved void helper; this was pruned with--prune.Linked Issue
Stage 1 row 1.3 (first slice) of the SSH hardening design. No public issue.
Visual Proof
N/A. This is store-state cleanup with no UI change of its own. The visible effect is that the surviving copy keeps its tabs and edits.
Testing
src/renderer/src/store/host-twin-isolation.test.ts: the eight purge cells now assert isolation. They fail on main once their leak entries are removed.src/renderer/src/store/host-twin-worktree-purge.test.ts(new) covers:Existing tests updated:
worktree-event-lineage-refresh.test.ts(new target shape), and the brand type tests inhost-qualified-identity.test.ts. The golden bytes ofcomposeWorktreeHostIdentitywere already frozen there and are reused, not duplicated.Local runs:
pnpm tc;src/renderer/src/store,src/renderer/src/lib,src/renderer/src/components/sidebar,src/renderer/src/hooks/ipc-eventsandsrc/shared/worktree(1565 files pass);Live verify: none. Covered by unit tests only.
I manually tested these changes locally
Automated tests added/updated, or explained why not below
AI Disclosure
Review
Agent skill upstream boundary
Notes
composeWorktreeHostIdentityare unchanged.Checklist
N/Awith reasonpnpm lint,pnpm typecheck,pnpm test, andpnpm buildpass (or CI will cover; local preferred)