Skip to content

Fix worktree removal blocked by populated submodules (local and SSH paths) - #7354

Closed
soichisumi wants to merge 4 commits into
stablyai:mainfrom
soichisumi:fix/worktree-remove-populated-submodules
Closed

soichisumi wants to merge 4 commits into
stablyai:mainfrom
soichisumi:fix/worktree-remove-populated-submodules

Conversation

@soichisumi

@soichisumi soichisumi commented Jul 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Deleting an Orca worktree of a repository that contains populated (initialized) git submodules fails outright, because git worktree remove refuses without --force. This PR makes worktree removal auto-recover from that specific refusal on both the local and the relay/SSH-host code paths, and surfaces the existing "Force Delete" retry affordance for the same error in the UI.

Why

git worktree remove <path> refuses with fatal: working trees containing submodules cannot be moved or removed whenever the worktree contains any populated submodule — even when the worktree is otherwise clean. Orca invokes this command without --force by default, so removal fails outright:

Error invoking remote method 'worktrees:remove': Error: Failed to delete worktree at <path>.
fatal: working trees containing submodules cannot be moved or removed

Submodule-containing worktrees are common (any repo that vendors dependencies as submodules), so deletion must work.

Root cause: git worktree remove runs check_clean_worktree() → validate_no_submodules() only when force == 0 (builtin/worktree.c). A single --force bypasses only the submodule guard (unlike git worktree move, where --force does not). Orca always invokes the non-force form by default, so any populated submodule blocks removal regardless of cleanliness.

What

Three code paths changed:

  1. Local (src/main/git/worktree.ts) — removeWorktree() gains a try/catch auto-force recovery; assertWorktreeCleanForRemoval() gains an optional stricter --ignore-submodules=none status check. The matched invocation pins LC_ALL=C/LC_MESSAGES=C because git localizes the fatal via gettext.
  2. Relay / SSH host (src/relay/git-handler-worktree-remove.ts) — removeWorktreeOp() gains the same auto-force recovery so SSH/remote worktree removal behaves identically to local (satisfies the "must work against local repositories, remote servers, and SSH worktrees" requirement).
  3. Renderer (src/renderer/src/store/slices/worktrees.ts) — the raw submodule fatal is added to FORCE_RETRYABLE_WORKTREE_REMOVAL_MESSAGES, so the "Force Delete" retry button appears for this error (defense-in-depth, and the manual path for the relay locale-miss case in R2).

New tests: src/relay/git-handler-worktree-remove-populated-submodules.test.ts, plus additions to src/main/git/remove-worktree.test.ts and src/renderer/src/store/slices/worktrees.test.ts.

How

On the initial non-force git worktree remove failure, if the error is the submodule-refusal fatal and the worktree is independently re-verified clean, retry once with --force. The re-verification uses git status --porcelain --untracked-files=all --ignore-submodules=none, whose command-line --ignore-submodules=none overrides submodule.*.ignore / diff.ignoreSubmodules config — so the auto-force path does not discard uncommitted or untracked work that exists at check time. It only routes around git's blanket submodule refusal for a worktree confirmed clean immediately beforehand. If the re-check finds the worktree dirty, the original error is rethrown rather than forcing (covered by a dedicated data-loss-guard test).

The relay executor (GitExec) has no env option, so the locale cannot be pinned there; the relay fatal-text match is therefore locale-dependent on the SSH host, with the renderer force-retry classifier as the fallback (see Residual risk R2).

Screenshots

No new UI. The only user-visible change: the common case now succeeds without any prompt, and the existing "Force Delete" retry affordance appears for the submodule-refusal error (previously it matched no retryable pattern).

Test plan

  • pnpm typecheck — exit 0 (all three projects: tsconfig.node.json, tsconfig.tc.cli.json, tsconfig.tc.web.json).
  • pnpm exec oxlint on all changed files — exit 0, no warnings.
  • pnpm test (full vitest suite) — 22,349 passed, 27 skipped. 108 failures across 34 suites are pre-existing and environment-driven, all in suites that do not import any file changed here (keychain/runtime-auth-service fail with Unexpected Claude config dir on a multi-profile dev box; browser-cookie-import, pty-subprocess/local-pty-provider, orca-runtime-browser screencast, remote-runtime-client socket). Every git / worktree / relay / renderer-worktrees suite passes, including the added tests.
  • Added high-quality tests — relay: force retry after strict clean check; no retry when a hidden dirty submodule is present (data-loss guard); unrelated-error passthrough; fall-through into branch cleanup.
  • pnpm lint — exit 1 only from a pre-existing verify:localization-catalog error (WorktreeList.tsx:2620 references key auto.components.sidebar.WorktreeList.failedUnnestWorkspace, absent from en.json on clean origin/main; unrelated to this PR — none of the 6 changed files touch WorktreeList.tsx or en.json). The oxlint stage passes; the localization-catalog check fails identically on origin/main.
  • pnpm build — full native/electron build not run locally (heavy, requires the native toolchain); deferred to CI. Local proxy pnpm typecheck (all three projects) is green; changes are limited to TypeScript in src/main, src/relay, src/renderer with no new deps.

AI Review Report

Independent review (Codex, gpt-5.5) of origin/main..HEAD. Verdict: APPROVE-WITH-NITS, no MUST-FIX.

  • Cross-platform (macOS / Linux / Windows): PASS. Command construction uses argv arrays and existing cwd/path handling; no new hardcoded separators or shell syntax. Local locale pin preserves process.env and existing WSL/abort options.
  • SSH / remote / local compatibility: PASS with a documented gap. The relay path now mirrors local behavior (strict status re-check, then --force). Gap: the relay cannot pin locale, so non-English SSH hosts may miss the auto-recovery and fall back to the renderer force-retry UX (R2).
  • Supported agents & integrations: PASS. No provider-specific assumptions; plain git worktree behavior.
  • Performance: PASS. Adds at most one git status and one retry, only on the narrow submodule-fatal path. No hot-path/polling impact.
  • UI quality: PASS. The renderer classifier is narrow — it only makes the raw submodule fatal force-retryable and does not broaden generic force-retry behavior.
  • Security: PASS. Commands are argv arrays, not shell strings; worktree paths are passed as argv/cwd values. No command injection introduced.
  • Nit (addressed): the "never discard work" comments were softened to note the residual TOCTOU window between the status check and the forced removal.

Security Audit

  • Input handling: the worktree path is caller-supplied and passed as an argv element / cwd value, never interpolated into a shell string.
  • Command execution: only git subcommands via the existing execFile/GitExec executors (no shell); the added calls are git status … and git worktree remove --force <path>.
  • Path handling: unchanged; reuses existing relay POSIX/Windows path resolution and the local exec options.
  • Auth / secrets: none touched; the local locale-pin call spreads process.env without adding or logging credentials.
  • Dependencies: none added.
  • IPC risks: no new IPC surface; the change is inside existing worktrees:remove / relay removeWorktreeOp handlers.
  • Data-loss: the force retry is gated on an independent --ignore-submodules=none cleanliness re-check that config cannot defeat; a hidden dirty submodule causes the original error to be rethrown rather than a forced removal (covered by a dedicated test).

Residual risks

  • R1 — the auto-force re-check trusts a git status --ignore-submodules=none snapshot taken immediately before the forced removal; a concurrent write into a nested submodule in that narrow window is the residual (same shape as any check-then-act removal).
  • R2 — the fatal-text classifier matches git's English message. The local path pins LC_ALL=C to defeat gettext localization; the relay path cannot (its GitExec has no env option), so on a non-English SSH host the relay auto-recovery may not fire — the manual "Force Delete" classifier is the fallback. A future GitExec env passthrough would let the relay pin the locale like the local path.

X handle: @soichisumi

ELI5

Deleting a worktree with populated submodules failed on local and SSH. Orca recovers from that git refusal and offers Force Delete when needed.

@coderabbitai

coderabbitai Bot commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

This PR adds detection and handling of "populated submodules" errors during git worktree removal across three layers: the main-process git worktree module, the relay git handler, and the renderer store. When git worktree remove fails due to populated submodules, the code re-checks worktree cleanliness while ignoring submodule state, then retries removal with --force if clean. The renderer store now classifies this error as force-retryable, exposing canForceDelete. Corresponding tests were added for all three layers.

Changes

Area Change
Main process (worktree.ts) Added isSubmoduleWorktreeRemovalError, retry logic with locale pinning, extended assertWorktreeCleanForRemoval with statusOptions
Relay handler (git-handler-worktree-remove.ts) Added submodule error detection, cleanliness re-check, and force-retry path in removeWorktreeOp
Renderer store (worktrees.ts) Added SUBMODULE_WORKTREE_REMOVAL_PATTERN, extended canRetryWorktreeRemovalWithForce
Tests New/extended test coverage across all three layers

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant RemovalLogic
  participant Git
  Caller->>RemovalLogic: request worktree removal
  RemovalLogic->>Git: worktree remove
  Git-->>RemovalLogic: fails (populated submodules)
  RemovalLogic->>RemovalLogic: classify error as submodule-related
  RemovalLogic->>Git: status --ignore-submodules=none
  Git-->>RemovalLogic: clean (submodules only)
  RemovalLogic->>Git: worktree remove --force
  Git-->>RemovalLogic: success
  RemovalLogic-->>Caller: removal result
Loading

Estimated code review effort: 4 out of 5 (touches critical path logic across three components with locale-sensitive error matching and force-retry behavior).

Related issues: None specified.

Related PRs: None specified.

Suggested labels: git, worktree, submodules, bug-fix

Suggested reviewers: None specified.

🐰
A submodule hid inside a tree,
Refused to leave, defiantly.
So we checked twice, then forced the door,
Clean and gone forevermore.
Hop, hop, retry — no dirt in sight! 🌿

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: fixing worktree removal for populated submodules on local and SSH paths.
Description check ✅ Passed The description is mostly complete and covers summary, testing, AI review, security, screenshots, and risks; only a dedicated Notes section is missing.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 198fb760-9e99-46c6-b8c0-d6bd001ed24f

📥 Commits

Reviewing files that changed from the base of the PR and between c6bfd86 and 7a8e01a.

📒 Files selected for processing (6)
  • src/main/git/remove-worktree.test.ts
  • src/main/git/worktree.ts
  • src/relay/git-handler-worktree-remove-populated-submodules.test.ts
  • src/relay/git-handler-worktree-remove.ts
  • src/renderer/src/store/slices/worktrees.test.ts
  • src/renderer/src/store/slices/worktrees.ts

Comment on lines +296 to +303
// Why: git's own worktree-remove refusal for a populated submodule (raw,
// unformatted fatal — e.g. surfaced through a path that does not go through
// the local auto-force recovery, such as the SSH/relay provider) is also
// safe to retry with force: it fires even on an otherwise-clean worktree, so
// force here does not risk discarding uncommitted work.
const SUBMODULE_WORKTREE_REMOVAL_PATTERN =
/working trees? containing submodules cannot be moved or removed/i

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.

🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Inspect how the dirty-recheck failure is surfaced in main/relay layers.
rg -n -B5 -A15 'isSubmoduleWorktreeRemovalError' src/main/git/worktree.ts src/relay/git-handler-worktree-remove.ts

Repository: stablyai/orca

Length of output: 6731


🏁 Script executed:

#!/bin/bash
set -euo pipefail

# Inspect the renderer classifier, delete-worktree flow, and any separate dirty-worktree handling.
ast-grep outline src/renderer/src/store/slices/worktrees.ts --view expanded
printf '\n--- delete-worktree-flow matches ---\n'
rg -n -A10 -B10 'FORMATTED_DIRTY_WORKTREE_REMOVAL_PATTERN|SUBMODULE_WORKTREE_REMOVAL_PATTERN|force-retry|worktree remove' src/renderer/src -g '*.ts' -g '*.tsx'

Repository: stablyai/orca

Length of output: 19959


Narrow the submodule retry classifier

The dirty-recheck path still rethrows the original submodule fatal, so this regex can also mark genuinely dirty removals as force-retryable and expose Force Delete for uncommitted work. Emit a distinct dirty-worktree error before retry classification, or tighten this match to the clean-only submodule case.

@OrcaWin

OrcaWin commented Oct 11, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the careful root-cause work, @soichisumi! The delete dialog now confirms with --force by default, which skips Git's populated-submodule guard, and SSH hosts now run the same local removal code on Orca's managed remote server (the relay handler this PR changed has been removed). Closing as resolved on main; please open an issue if a submodule worktree still won't delete.

@OrcaWin OrcaWin closed this Oct 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants