Skip to content

fix(worktrees): finish a delete Git left half-done on macOS and Linux - #33209

Merged
nwparker merged 9 commits into
stablyai:mainfrom
anmol0b:fix/finish-partial-worktree-delete-posix
Oct 28, 2026
Merged

nwparker merged 9 commits into
stablyai:mainfrom
anmol0b:fix/finish-partial-worktree-delete-posix

Conversation

@anmol0b

@anmol0b anmol0b commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

ELI5

When deleting a workspace on macOS or Linux, Git sometimes fails partway: it forgets the workspace but leaves its folder on disk. Orca then refused every later Delete and told you to remove the folder yourself. Now Orca finishes the delete right away, as it already did on Windows.

What Changed

git worktree remove deletes the checkout and then, even when that fails, still removes .git/worktrees/<id>, on every OS. Both local delete paths (desktop finishLocalWorktreeRemoval and runtime finishRuntimeLocalWorktreeRemoval) then call the partial-removal recovery. That recovery returned early off Windows because of a process.platform !== 'win32' check in isRecoverableWindowsFilesystemRemovalFailure. The delete was recorded as failed, and #25075's rule then refused every retry with "… is no longer a Git worktree, so its folder was left on disk."

This removes the platform check and renames the function to recoverPartialLocalWorktreeRemoval. Its existing safeguards are unchanged: a Git lock still blocks it, a strict worktree listing must no longer contain the path, file watchers are closed first, and removeLocalWorktreePath still handles WSL. It runs only inside the same delete call, right after Git deregistered a checkout Orca had just listed. That is the one place #25075 still lets Orca delete files.

Behaving the same as before: retrying a failed row and the delete that resumes at startup still never delete an unregistered folder. If Orca's own follow-up delete also fails, the row stays listed and the branch is handled as #25075 defines. In that case the error is now Node's, naming the exact file, as on Windows.

Why

Linked Issue

Fixes #33023

Visual Proof

N/A. This changes delete behaviour in the main process, with no UI change. Before: on macOS, the real-Git runtime tests show the leftover recorded with Git's Operation not permitted and Orca never trying to finish the delete. After: Orca's own follow-up delete runs, which the EPERM: operation not permitted, unlink error proves.

Testing

First I reproduced the bug on upstream/main on macOS (git 2.54). The live-Git test that races a file writer against git worktree remove is Windows-only, so I ran it on macOS with the gate removed. It fails: Git exits non-zero, deregisters the worktree and leaves the folder, and the recovery returns undefined. That test stays Windows-only in this PR.

  • A darwin unit test checks that when Git no longer lists the path, the recovery closes watchers, deletes the folder and keeps the branch.
  • In runtime-failed-local-worktree-removal.test.ts (darwin, real Git, immutable file), the startup-finish and runtime-delete tests now assert EPERM: operation not permitted, unlink. Only Orca's follow-up delete produces that error, so they prove the recovery ran on macOS. The row stays listed and retries are still refused.
  • With the platform check restored, these 4 tests fail.
  • Ran the 22 removal test files (259 passed, 2 Windows-only skipped), the node and full typecheck, oxlint, oxfmt and the changed-code quality gate.

Tested on macOS only. Not tested on Windows (no change in behaviour there), Linux or SSH (not on this path).

  • I manually tested these changes locally
  • Automated tests added/updated, or explained why not below

AI Disclosure

Review

Agent skill upstream boundary

  • Not applicable, or this change copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

Security: Orca deletes files only within the same delete call, after Git deregistered the checkout; later retries and the startup finish still never delete an unregistered folder. Cross-platform: Windows behaviour is unchanged; macOS and Linux now get the same recovery. Remote SSH: not on this path. Mobile: same runtime path, no client change. Backwards compatibility: no persisted data or client–host protocol changes. Performance: no new work on successful deletes.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

@coderabbitai

coderabbitai Bot commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Note

Reviews paused

It 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 reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: af5da4b8-791a-4cd0-8ca7-f03ed8acbc09
Commits

Reviewing files that changed from the base of the PR and between a5d0799 and 09afa9e.

Files selected for processing: 3
  • src/main/local-worktree-removal-recovery.test.ts
  • src/main/local-worktree-removal-recovery.ts
  • src/main/runtime/runtime-failed-local-worktree-removal.test.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.

Walkthrough

The recovery helper and its argument type are renamed to describe partial removal. Recovery no longer requires Windows and checks Git registrations, including for nested registered worktrees. The IPC and runtime removal flows use the renamed helper after Git removal fails. Tests add macOS recovery cases and update runtime error assertions to match the specific locked-file deletion error.

Priority: Normal

Severity of issue fixed: Medium

Merge Risk: Minimal · previous review 09afa

No actionable deletion risk is established from the supplied evidence. The change is mergeable after normal checks.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 16.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 6 functions across 6 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title names the main change: a delete that Git left half-done is now finished on macOS and Linux, which matches the recovery change and the platform-check removal in the diff. It is short, specifi…
Description check Passed The description follows the repository template closely. It has the ELI5, What Changed, Why, Linked Issue (Fixes #33023), Visual Proof (N/A with reason), Testing with platform detail, AI Disclosure, R…
Linked Issues check Passed #33023 is addressed. recoverPartialLocalWorktreeRemoval has no platform gate. It rejects locked registrations, orphan errors, failed strict listings, registered target paths, and nested registered w…
Out of Scope Changes check Passed The changes stay within #33023. The helper rename, desktop and runtime call-site updates, cross-platform recovery, safety checks, and focused test updates directly support failed local worktree deleti…
Full details: Title check

Explanation

The title names the main change: a delete that Git left half-done is now finished on macOS and Linux, which matches the recovery change and the platform-check removal in the diff. It is short, specific, and uses the conventional fix(worktrees) prefix. It does not name the rename to recoverPartialLocalWorktreeRemoval, but that is a secondary detail.

Full details: Description check

Explanation

The description follows the repository template closely. It has the ELI5, What Changed, Why, Linked Issue (Fixes #33023), Visual Proof (N/A with reason), Testing with platform detail, AI Disclosure, Review, Agent skill upstream boundary, Notes, and Checklist sections. It explains the before and after behavior, the mechanism (the process.platform check in isRecoverableWindowsFilesystemRemovalFailure), and the alternatives considered. Its gaps are minor: the AI Disclosure and Review sections are empty, and the final pnpm checklist item is unchecked, with no explanation of why that item is unchecked.

Full details: Linked Issues check

Explanation

#33023 is addressed. recoverPartialLocalWorktreeRemoval has no platform gate. It rejects locked registrations, orphan errors, failed strict listings, registered target paths, and nested registered worktrees before it closes watchers and calls removeLocalWorktreePath. Desktop and runtime local-delete paths invoke it after Git removal fails. The recovery then verifies the Git registration is gone and returns the preserved branch result. Darwin unit and runtime tests cover recovery, watcher closure, nested-worktree protection, and failed follow-up deletion. The Windows-only refusal requirement does not apply because recovery is no longer Windows-only.

Full details: Out of Scope Changes check

Explanation

The changes stay within #33023. The helper rename, desktop and runtime call-site updates, cross-platform recovery, safety checks, and focused test updates directly support failed local worktree deletion recovery. No unrelated product behavior is established by the available evidence.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


Review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: cb55913f-e2ab-454e-8b53-99e1d678320a
Commits

Reviewing files that changed from the base of the PR and between 5c447fb and 1ba3f4b.

Files selected for processing: 6
  • src/main/ipc/worktrees/removal/remove-registered-local-worktree.ts
  • src/main/local-worktree-removal-recovery-live.test.ts
  • src/main/local-worktree-removal-recovery.test.ts
  • src/main/local-worktree-removal-recovery.ts
  • src/main/runtime/runtime-failed-local-worktree-removal.test.ts
  • src/main/runtime/runtime-registered-local-worktree-removal.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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to GitHub limitations.

⚠️ Outside diff range comments (1)

Major · Keep the orphan-directory gate ahead of… · local-worktree-removal-recovery.ts:130-141

src/main/local-worktree-removal-recovery.ts:130-141
🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Keep the orphan-directory gate ahead of partial-removal recovery.

With the platform gate removed, isRecoverablePartialRemovalFailure accepts an orphan error whenever Git no longer lists the worktree. The orphan path refuses recursive deletion unless canSafelyRemoveOrphanedWorktreeDirectory proves the directory is safe to remove. Recovery never runs that check, so an unproven directory can be deleted recursively.

Recovery runs before isOrphanedWorktreeError in both the IPC and runtime catch blocks. Once an error matches /is not a working tree/ and the row is absent, the recovery predicate returns true. Recovery then calls removeLocalWorktreePath on the canonical path. This skips the proof gate that cleanupLocalOrphanedWorktreeDirectory applies first. That function deletes only when canSafelyRemoveOrphanedWorktreeDirectory returns true. Otherwise it logs "Refusing recursive cleanup for unproven worktree directory" and throws ORPHANED_WORKTREE_DIRECTORY_MESSAGE while the directory remains.

The trigger needs the Git row to disappear between Orca's registered-worktree lookup and the removeWorktree call. Such a race is feasible, for example through a concurrent git worktree prune or another process. The consequence is an irreversible recursive delete of a directory that the orphan path would keep. Fix the predicate so orphan errors return to the existing fallback. This restores the pre-change orphan behavior on macOS and Linux and leaves recovery for its intended filesystem-failure case.

Suggested fix
 import { removeLocalWorktreePath } from './local-worktree-filesystem'
+import { isOrphanedWorktreeError } from './ipc/worktree-logic'
 async function isRecoverablePartialRemovalFailure(
   args: LocalPartialRemovalRecoveryArgs
 ): Promise<boolean> {
-  if (typeof args.error !== 'object' || args.error === null) {
+  if (typeof args.error !== 'object' || args.error === null || isOrphanedWorktreeError(args.error)) {
     return false
   }

Review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: e8d23018-6df3-4b3d-bacb-480505229c88
Commits

Reviewing files that changed from the base of the PR and between 1ba3f4b and e0e592e.

Files selected for processing: 2
  • src/main/local-worktree-removal-recovery.test.ts
  • src/main/local-worktree-removal-recovery.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.

anmol0b and others added 9 commits October 27, 2026 23:04
git worktree remove drops the registration even when its recursive
delete fails partway, on every OS. The in-operation recovery that
finishes that delete was gated to win32, so on macOS and Linux the
leftover folder was recorded as failed and every retry was refused.

Run the recovery on all platforms with its existing gates (Git lock,
strict listing without the path, watchers closed first). Later retries
and the startup finish still never delete an unregistered folder.

Fixes stablyai#33023
…must stay listed

Git-deregistered partial deletes are now finished by Orca on every OS, so the
mocked IPC and runtime leftover tests make that follow-up delete fail too.

Co-authored-by: Anmol Bhardwaj <anmolbhardwajinv@gmail.com>
Skip the repo-wide git worktree prune (Git already dropped the row; prune
can unregister checkouts on unmounted drives) and verify the row is gone
instead. Delete the branch with the normal -d policy rather than always
preserving it, so merged branches go and unmerged ones are kept.
…-delete test on macOS

Co-authored-by: Anmol Bhardwaj <anmolbhardwajinv@gmail.com>
… in partial-delete tests

Co-authored-by: Anmol Bhardwaj <anmolbhardwajinv@gmail.com>
… race test Windows-only

The recovery swallowed the nested-worktree guard into 'not recoverable', so users saw
Git's 'Directory not empty' instead of the nested-worktree message. The live churn race
flaked on macOS when Git won the race; real-Git darwin runtime tests already cover it.
…heck in partial-delete recovery

The predicate already proved Git dropped the row; re-listing after the rm
only turned a listing failure into a misleading stale-registration error
after the folder was gone. The force argument is now always true for the
remaining stale-registration caller.
@nwparker
nwparker force-pushed the fix/finish-partial-worktree-delete-posix branch from 4fe75ae to 9bc8e48 Compare October 28, 2026 07:22
@nwparker

Copy link
Copy Markdown
Contributor

Thanks @anmol0b! I rebased this onto the latest main cleanly. No fixups were needed. This will merge once CI is green.

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.

[Bug]: A failed local workspace delete on macOS removes the branch, keeps the folder, and can never be retried

2 participants