Repository navigation
fix(worktrees): guard lineage pruning from failed scans - #13248
Conversation
📝 WalkthroughWalkthroughThe change centralizes repository worktree lineage pruning in a shared utility. It adds a runtime scanner that uses strict Git worktree listing and supports optional WSL settings. Runtime worktree resolution and pruning now use the shared utilities. IPC worktree handling also uses the shared pruning utility. Tests cover scan failures, empty scans, stale lineage removal, missing-parent identity rotation, and runtime scanner mocking. 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 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
🧹 Nitpick comments (1)
src/main/worktree-lineage-pruning.test.ts (1)
117-125: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winAssert stale workspace lineage cleanup.
The test creates workspace lineage for
missingChildIdbut does not verify its removal. A regression that retains this record will pass.Proposed test assertions
expect(store.removeWorktreeLineage).toHaveBeenCalledWith(missingChildId) expect(store.removeWorktreeLineage).not.toHaveBeenCalledWith(liveChildId) + expect(workspaceLineageByChildKey[worktreeWorkspaceKey(missingChildId)]).toBeUndefined() + expect(workspaceLineageByChildKey[worktreeWorkspaceKey(liveChildId)]).toBeDefined() expect(store.setWorktreeMeta).toHaveBeenCalledWith(missingParentId, {
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro Plus
Run ID: e206082e-046f-499c-b161-43734895868e
📒 Files selected for processing (7)
src/main/ipc/worktrees.tssrc/main/runtime/orca-runtime.test.tssrc/main/runtime/orca-runtime.tssrc/main/runtime/repo-worktree-resolution-scan.test.tssrc/main/runtime/repo-worktree-resolution-scan.tssrc/main/worktree-lineage-pruning.test.tssrc/main/worktree-lineage-pruning.ts
| scanLocalRepoWorktreesForResolutionMock | ||
| .mockReset() | ||
| .mockImplementation(async (repoPath: string, options: { wslDistro?: string }) => { | ||
| try { | ||
| const worktrees = options.wslDistro | ||
| ? await listWorktrees(repoPath, options) | ||
| : await listWorktrees(repoPath) | ||
| return { ok: true, worktrees } | ||
| } catch { | ||
| return { ok: false, worktrees: [] } | ||
| } | ||
| }) |
There was a problem hiding this comment.
🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
set -euo pipefail
printf 'Locate files and relevant symbols\n'
fd -a 'orca-runtime.*\.ts$' . | sed 's#^\./##'
printf '\nSearch listWorktreesStrict and scanLocalRepoWorktreesForResolutionMock\n'
rg -n "listWorktreesStrict|listWorktrees\(|scanLocalRepoWorktreesForResolutionMock|wslDistro" src/main -S || true
printf '\nRead targeted test section\n'
file="$(fd 'orca-runtime\.test\.ts$' . | head -n1 || true)"
if [ -n "${file:-}" ]; then
wc -l "$file"
sed -n '620,710p' "$file" | cat -n
fi
printf '\nRead candidate implementation sections\n'
impl="$(fd 'orca-runtime\.ts$' . | head -n1 || true)"
if [ -n "${impl:-}" ]; then
wc -l "$impl"
rg -n "listWorktreesStrict|function listWorktrees|const listWorktrees|export .*listWorktreesStrict" "$impl" -A 30 -B 10
fiRepository: stablyai/orca
Length of output: 50370
🏁 Script executed:
#!/bin/bash
set -euo pipefail
file="src/main/runtime/orca-runtime.test.ts"
impl="src/main/runtime/orca-runtime.ts"
printf 'Targeted test section\n'
sed -n '640,690p' "$file" | cat -n
printf '\nDefinitions around listWorktreesStrict/listWorktrees in implementation\n'
rg -n "export .*listWorktreesStrict|function listWorktreesStrict|const listWorktreesStrict|listWorktreesStrict|function listWorktrees|const listWorktrees" "$impl" -A 45 -B 15
printf '\nImports/mock declarations in target test\n'
sed -n '1,120p' "$file" | cat -n
printf '\nRead-only JS behavior probe for omitted options branch\n'
node - <<'JS'
function currentBranch(options) {
try {
return options.wslDistro ? "listWorktrees(repo, options)" : "listWorktrees(repo)"
} catch (err) {
return "failed -> { ok: false }"
}
}
function proposedBranch(options) {
try {
return options?.wslDistro ? "listWorktreesStrict(repo, options)" : "listWorktreesStrict(repo)"
} catch (err) {
return "failed -> { ok: false }"
}
}
for (const options of [undefined, {}, { wslDistro: undefined }, { wslDistro: "Ubuntu" }]) {
console.log(JSON.stringify({
input: options,
current: currentBranch(options),
proposed: proposedBranch(options)
}))
}
JSRepository: stablyai/orca
Length of output: 17976
🏁 Script executed:
#!/bin/bash
set -euo pipefail
impl="src/main/git/worktree.ts"
printf 'Locate scanLocalRepoWorktreesForResolution and listWorktreesStrict in git/worktree\n'
rg -n "export .*scanLocalRepoWorktreesForResolution|function scanLocalRepoWorktreesForResolution|const scanLocalRepoWorktreesForResolution|scanLocalRepoWorktreesForResolution|export .*listWorktreesStrict|function listWorktreesStrict|const listWorktreesStrict|listWorktreesStrict|export .*listWorktrees|function listWorktrees|const listWorktrees" "$impl" -A 80 -B 20
printf '\nSearch all production scanLocalRepoWorktreesForResolution usages\n'
rg -n "scanLocalRepoWorktreesForResolution\(" src/main -S --glob '!**/*.test.ts' --glob '!**/*.integration.test.ts'Repository: stablyai/orca
Length of output: 10230
🏁 Script executed:
#!/bin/bash
set -euo pipefail
prod="src/main/runtime/orca-runtime.ts"
scanner="src/main/runtime/repo-worktree-resolution-scan.ts"
printf 'scanner implementation\n'
sed -n '1,80p' "$scanner" | cat -n
printf '\norca-runtime call site around scanLocalRepoWorktreesForResolution\n'
sed -n '28855,28920p' "$prod" | cat -n
printf '\nsearch scanLocalRepoWorktreesForResolution in prod\n'
rg -n "scanLocalRepoWorktreesForResolution|scanLocalRepoWorktreesForResolutionMock" "$prod" -A 20 -B 20Repository: stablyai/orca
Length of output: 7227
Use listWorktreesStrict in the resolution scan test double.
scanLocalRepoWorktreesForResolution always calls listWorktreesStrict, including when no WSL options are passed. This reset block still calls the non-strict listWorktrees, so a test that makes the strict scanner fail will not fail here.
Proposed fix
- .mockImplementation(async (repoPath: string, options: { wslDistro?: string }) => {
+ .mockImplementation(async (repoPath: string, options?: { wslDistro?: string }) => {
try {
- const worktrees = options.wslDistro
- ? await listWorktrees(repoPath, options)
- : await listWorktrees(repoPath)
+ const worktrees = options?.wslDistro
+ ? await listWorktreesStrict(repoPath, options)
+ : await listWorktreesStrict(repoPath)
return { ok: true, worktrees }📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| scanLocalRepoWorktreesForResolutionMock | |
| .mockReset() | |
| .mockImplementation(async (repoPath: string, options: { wslDistro?: string }) => { | |
| try { | |
| const worktrees = options.wslDistro | |
| ? await listWorktrees(repoPath, options) | |
| : await listWorktrees(repoPath) | |
| return { ok: true, worktrees } | |
| } catch { | |
| return { ok: false, worktrees: [] } | |
| } | |
| }) | |
| scanLocalRepoWorktreesForResolutionMock | |
| .mockReset() | |
| .mockImplementation(async (repoPath: string, options?: { wslDistro?: string }) => { | |
| try { | |
| const worktrees = options?.wslDistro | |
| ? await listWorktreesStrict(repoPath, options) | |
| : await listWorktreesStrict(repoPath) | |
| return { ok: true, worktrees } | |
| } catch { | |
| return { ok: false, worktrees: [] } | |
| } | |
| }) |
31049ac to
a3899ed
Compare
QA validation @
|
| Check | Result |
|---|---|
| Edges before disconnect | 1 (child → parent) |
| Edges after disconnect refresh | 1 (unchanged) |
| Edges after reconnect | 1 (unchanged) |
Parent instanceId stable |
yes (f5b59927-...) |
Persisted orca-data.json mid-disconnect |
1 lineage key retained |
| Persisted final | 1 lineage key retained |
Remote git backing (over SSH after reconnect)
worktree /work/demo-project
worktree /work/demo-project-qa-parent-mskrlpqp [qa-parent-mskrlpqp]
worktree /work/demo-project-qa-child-mskrlpqp [qa-child-mskrlpqp]
Screenshots (full app window)
Logs inspected
- Electron: relay deploy success;
Relay channel lostafter forced kill; reconnect; node-pty skip (no build-essential on throwaway image — files/git still served) - No evidence of lineage wipe in store or persisted maps during disconnect/refresh
Explicit non-claims / gaps
- This is a live SSH disconnect + refresh proof, not a forced local empty
git worktree listspawn-failure (EAGAIN) path. The empty-scan circuit breaker / local strict-scan behavior is covered by the PR’s deterministic unit tests (worktree-lineage-pruning.test.ts,repo-worktree-resolution-scan.test.ts), not re-forced in-process here. - Remote PTY unavailable on this throwaway image (missing
make/g++); not required for worktree scan/lineage surface under test.
Cleanup
- Remote test worktrees/branches, container, temp keys/profile, and Electron process from this run will be removed by the QA worker after comment.
* fix(worktrees): guard lineage pruning from failed scans * fix(worktrees): back off failed local resolution scans



Problem
A failed
git worktree list(spawn failure under resource exhaustion — EAGAIN/ENOMEM, git killed, etc.) is swallowed into an empty list bylistWorktreesUnshared, which the local branch oflistRepoWorktreesForResolutionUncachedthen wraps as{ok: true, worktrees: []}. Both prune call sites gate onscan.ok, sopruneLineageForMissingRepoWorktreestreats every lineage child as "proven gone": it purges allworktreeLineageById/workspaceLineageByChildKeyentries for the repo and re-mints parentinstanceIds.This is not theoretical: on 2026-08-07 ~19:00 PDT, under system load 579 / 91 MB free RAM, this path wiped all 268 lineage entries on a production profile (1.4.177-hourly), flattening every worktree in the sidebar to root level. Data was recovered only because the hourly
orca-data.json.bak.Nrotation (5 kept) hadn't yet aged out the pre-wipe snapshot.Fix
Two independent guards, defense in depth:
ok: false(mirroring the existing SSH-branch behavior), so a failed scan can never masquerade as an authoritative empty catalog. Extracted intorepo-worktree-resolution-scan.ts.worktree-lineage-pruning.ts: pruning refuses to act on an empty scan while the store still has registered lineage for the repo. Applied to both consumers (runtime +ipc/worktrees.ts).Tests
a3899ed9c9: degraded local scans now use the existing 30-second cache, preventing repeatedgit worktree listspawns under EAGAIN/ENOMEM while remainingok: false; SSH retry behavior is unchanged. A repeat review returned clean.git diff --check.pnpm run typecheck, fullpnpm run lint, andgit diff --check origin/main...HEADpassed.repo-worktree-resolution-scan.test.ts,worktree-lineage-pruning.test.ts, andworktree-ps-degraded-repo-scan.test.ts.a3899ed9c98a9fbbd30f362adad227575665f0a4: a real remote parent/child worktree lineage remained intact in the UI, store, and persistedorca-data.jsonafter a forced relay kill, disconnect + refresh, and reconnect + refresh. ParentinstanceIdremained stable and remote Git still reported all three worktrees. Screenshots and full QA evidence.Incident forensics
Root-caused from the hourly state backups (268 → 0 lineage entries between the 18:50 and 19:50 snapshots, worktreeMeta intact, 25 lineage-parent instanceIds re-minted, no crash/restart/migration in the window) plus main-process traces. Happy to share the full writeup.