Skip to content

fix: clean up editor/terminal state when removing a worktree - #532

Merged
nwparker merged 1 commit into
stablyai:mainfrom
hobostay:fix/remove-worktree-state-cleanup
Apr 13, 2026
Merged

nwparker merged 1 commit into
stablyai:mainfrom
hobostay:fix/remove-worktree-state-cleanup

Conversation

@hobostay

Copy link
Copy Markdown
Contributor

Summary

  • Fix memory leaks in removeWorktree where four state properties were never cleaned up after a worktree deletion
  • editorDrafts (unsaved content keyed by file ID), markdownViewMode (rich/source toggle), expandedDirs (file explorer state), and activeTabIdByWorktree (remembered terminal tab) all accumulated orphaned entries indefinitely
  • Over long sessions with many worktree create/delete cycles, these entries consume growing memory and can never be reclaimed because the corresponding file and worktree IDs no longer exist in openFiles or worktreesByRepo

Changes

src/renderer/src/store/slices/worktrees.ts — In the removeWorktree set() callback:

  • Collect the file IDs of the deleted worktree's open files and delete their editorDrafts and markdownViewMode entries
  • Remove the worktree's expandedDirs and activeTabIdByWorktree entries
  • Only create shallow copies when there are actually entries to remove (preserving the no-op identity check)

src/renderer/src/store/slices/worktrees.test.ts — Added 5 new tests:

  • Cleans up editorDrafts for files in the removed worktree
  • Cleans up markdownViewMode for files in the removed worktree
  • Cleans up expandedDirs for the removed worktree
  • Cleans up activeTabIdByWorktree for the removed worktree
  • Skips unnecessary shallow copy when no files belong to the removed worktree

Test plan

  • All 952 existing tests pass
  • pnpm typecheck passes with no new errors
  • pnpm lint passes with 0 errors
  • 5 new tests cover the four leaked state properties

No visual change — this is a pure state management bug fix.

🤖 Generated with Claude Code

@nwparker
nwparker force-pushed the fix/remove-worktree-state-cleanup branch from 9bf6b3d to f5b9501 Compare April 13, 2026 04:53
removeWorktree already cleaned up openFiles, terminal layouts, browser
tabs, and several worktree-keyed maps, but four state properties leaked
indefinitely after a worktree was deleted:

- editorDrafts — unsaved draft content keyed by file ID
- markdownViewMode — rich/source toggle keyed by file ID
- expandedDirs — file explorer expansion state keyed by worktree ID
- activeTabIdByWorktree — remembered terminal tab keyed by worktree ID

Over a long session with many worktree create/delete cycles these
accumulated entries consume memory and can never be reclaimed because the
corresponding file and worktree IDs no longer exist in openFiles or
worktreesByRepo.

The fix collects the file IDs of the removed worktree's open files and
deletes their editorDrafts and markdownViewMode entries, then removes the
worktree's expandedDirs and activeTabIdByWorktree entries. A shallow copy
is only created when there are actually entries to remove, preserving the
no-op identity check that existing tests rely on.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@nwparker
nwparker force-pushed the fix/remove-worktree-state-cleanup branch from f5b9501 to c269f90 Compare April 13, 2026 05:15
@nwparker
nwparker merged commit 1038295 into stablyai:main Apr 13, 2026
brennanb2025 added a commit that referenced this pull request Apr 19, 2026
Adds Playwright coverage for two high-regression areas identified from the
last 200 PRs:

- worktree-lifecycle.spec.ts: guards removeWorktree cleanup (#532),
  worktree switch with right sidebar open (#598/#628/#726), and
  cross-worktree tab isolation (#542/#554).
- tab-close-navigation.spec.ts: guards visual-neighbor activation on
  editor/diff close (#693) and Landing fallback when the last visible
  surface closes (#677).
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…i#532)

removeWorktree already cleaned up openFiles, terminal layouts, browser
tabs, and several worktree-keyed maps, but four state properties leaked
indefinitely after a worktree was deleted:

- editorDrafts — unsaved draft content keyed by file ID
- markdownViewMode — rich/source toggle keyed by file ID
- expandedDirs — file explorer expansion state keyed by worktree ID
- activeTabIdByWorktree — remembered terminal tab keyed by worktree ID

Over a long session with many worktree create/delete cycles these
accumulated entries consume memory and can never be reclaimed because the
corresponding file and worktree IDs no longer exist in openFiles or
worktreesByRepo.

The fix collects the file IDs of the removed worktree's open files and
deletes their editorDrafts and markdownViewMode entries, then removes the
worktree's expandedDirs and activeTabIdByWorktree entries. A shallow copy
is only created when there are actually entries to remove, preserving the
no-op identity check that existing tests rely on.

Co-authored-by: Test User <test@example.com>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
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.

2 participants