Skip to content

fix(worktree): stop terminal removal fence error flashing on delete - #10240

Merged
brennanb2025 merged 2 commits into
mainfrom
brennanb2025/fix-worktree-delete-terminal
Jul 24, 2026
Merged

brennanb2025 merged 2 commits into
mainfrom
brennanb2025/fix-worktree-delete-terminal

Conversation

@brennanb2025

Copy link
Copy Markdown
Contributor

Problem

Deleting a worktree sometimes flashes a scary error on a terminal tab that lived in it:

TerminalRemovalInProgressError: Terminal cannot start while the worktree is being removed

It clears once the worktree fully deletes (~1s later), but it's bad UI.

Root cause

When a worktree is deleted, main takes the removal fence (acquireWatcherRemovalGate) and kills the worktree's PTYs for the filesystem teardown, then runs git worktree remove (~1s). During that window the doomed pane races a fresh respawn, which main correctly fences with TerminalRemovalInProgressError (a shell must not spawn into a directory being deleted). The bug is purely renderer-side: reportError surfaced that internal fence verbatim as a pane error banner (TerminalPane → setTerminalError), which only disappeared when the worktree unmounted.

Fix (renderer-side, no IPC changes)

  • Stop the doomed respawn — startFreshSpawn skips when the pane's own worktree has deleteStateByWorktreeId[worktreeId].isDeleting (a signal the renderer already sets at removal start). No shell should spawn into a directory being removed, and the pane is about to unmount.
  • Swallow the fence at the sink — reportError (the single choke point for every pane-error path: connect/reattach/attach/SSH) drops the removal fence instead of surfacing it. This also covers the parent-removal-fences-child case that the own-worktree skip can't see.
  • Shared contract — the fence messages now live in one module (src/shared/worktree-removal-fence-error.ts); main's error classes throw them and the renderer's isWorktreeRemovalFenceError predicate matches them (robust to Electron's IPC error prefix), so the thrown text and the predicate can't drift.

Verification

Unit (deterministic): new tests prove — the real gate throws a message the predicate recognizes (main↔renderer contract), reportError swallows the fence (raw + IPC-wrapped) while non-fence errors still surface, and startFreshSpawn skips during isDeleting while normal spawns are unaffected. All affected suites green.

Live app (Electron via CDP): created a worktree with a live PTY and deleted it under a DOM+console watcher for the fence text — clean, then 5 rapid create→activate→delete stress cycles. isDeleting was observed active during every deletion (the guard is genuinely in the removal window), all deletions completed, and zero fence hits (DOM, console, or error events) across all runs.

tsc (node + web) and oxlint clean on all changed files.

Notes / scope

  • The original banner is timing-dependent ("sometimes"), so the live cycles can't guarantee the exact race fired — but the unit tests deterministically exercise the fence-and-swallow path.
  • SSH/remote worktrees are covered: the fence flows through the same reportError sink and isDeleting is set identically for remote removals.

Deleting a worktree kills its PTYs for the filesystem teardown, then runs
git worktree remove (~1s). During that window a doomed pane races a fresh
respawn that main correctly fences with TerminalRemovalInProgressError, but
the renderer surfaced that internal fence verbatim as a pane error banner
until the worktree unmounted.

- startFreshSpawn: skip the respawn when the pane's own worktree is being
  deleted (isDeleting) — no shell should spawn into a directory being removed
  and the pane is about to unmount.
- reportError: swallow the removal fence at the single pane-error sink so it
  never reaches the banner, covering the parent-removal-fences-child case the
  own-worktree skip cannot see.
- Share the fence messages between main (thrown) and renderer (recognized) via
  worktree-removal-fence-error.ts so the thrown text and predicate can't drift.
@coderabbitai

coderabbitai Bot commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 2b90a4ca-f87a-4ca6-a534-da97cedb4dfc

📥 Commits

Reviewing files that changed from the base of the PR and between 6a07390 and 4ec4476.

📒 Files selected for processing (1)
  • src/renderer/src/components/terminal-pane/pty-connection.test.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/renderer/src/components/terminal-pane/pty-connection.test.ts

📝 Walkthrough

Walkthrough

Adds shared constants and a predicate for identifying worktree-removal fence errors. Removal-gate errors now use those constants. PTY connection skips fresh spawns for deleting worktrees, suppresses expected fence errors, and continues reporting other spawn errors. Tests cover predicate matching, gate-generated errors, deletion-state behavior, and PTY error handling.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the fix, but it does not follow the required template and omits Screenshots, Testing checklist, AI Review Report, and Security Audit. Reformat the PR body to the repository template and add the missing sections, including Screenshots or No visual change, Testing, AI Review Report, Security Audit, and Notes.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title is concise and accurately summarizes the main user-visible fix in the PR.
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.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

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 Plus

Run ID: 08c19243-428d-4cb4-bb62-3ab5a3e6a515

📥 Commits

Reviewing files that changed from the base of the PR and between ee87bb3 and 6a07390.

📒 Files selected for processing (6)
  • src/main/ipc/watcher-removal-gate.test.ts
  • src/main/ipc/watcher-removal-gate.ts
  • src/renderer/src/components/terminal-pane/pty-connection.test.ts
  • src/renderer/src/components/terminal-pane/pty-connection.ts
  • src/shared/worktree-removal-fence-error.test.ts
  • src/shared/worktree-removal-fence-error.ts

Optional invocation let the fence-suppression test false-pass if the
transport onError wiring broke; require the callback so a broken wire
fails loudly. Addresses CodeRabbit review on #10240.
@brennanb2025
brennanb2025 merged commit 00cda62 into main Jul 24, 2026
4 checks passed
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…tablyai#10240)

* fix(worktree): stop terminal removal fence error flashing on delete

Deleting a worktree kills its PTYs for the filesystem teardown, then runs
git worktree remove (~1s). During that window a doomed pane races a fresh
respawn that main correctly fences with TerminalRemovalInProgressError, but
the renderer surfaced that internal fence verbatim as a pane error banner
until the worktree unmounted.

- startFreshSpawn: skip the respawn when the pane's own worktree is being
  deleted (isDeleting) — no shell should spawn into a directory being removed
  and the pane is about to unmount.
- reportError: swallow the removal fence at the single pane-error sink so it
  never reaches the banner, covering the parent-removal-fences-child case the
  own-worktree skip cannot see.
- Share the fence messages between main (thrown) and renderer (recognized) via
  worktree-removal-fence-error.ts so the thrown text and predicate can't drift.

* test(worktree): assert onError callback captured before invoking

Optional invocation let the fence-suppression test false-pass if the
transport onError wiring broke; require the callback so a broken wire
fails loudly. Addresses CodeRabbit review on stablyai#10240.
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.

1 participant