Skip to content

Worktree garbage collection with UI: retention limits, batch cleanup, visible disk usage #316

Closed

Description

@currentbits

T3 Code's cautionary tale: a user's state dir hit 8 GB across 20+ invisible worktrees and crash-looped the app; the feature request for per-project retention limits followed. Conductor: archiving could silently fail to delete branches; branch names collided and showed unrelated merged PRs.

Solenta has scripts/prune-worktrees.sh but no in-app GC. Add: per-project retention limits for settled threads, batch cleanup of orphaned worktrees (one dialog, not per-item), collision-safe boring branch names, and visible disk usage per project.

Activity

  1. added
    enhancement
    plan:todo
    plan:doing
    and removed
    plan:todo
    on Aug 17, 2026
  2. currentbits commented on Aug 17, 2026

    @currentbits
    OwnerAuthor

    Plan (orchestrated, 2 parallel grok workers)

    Landed on main first — shared contract (Merge worktree GC contract (#316)):

    • src/shared/ipc.ts: GcCandidate, ProjectDiskUsage, GcScanResult, GcCleanInput/Result, ProjectInfo.worktreeRetention
    • seam wired end to end: git:gcScan / git:gcClean across preload.js, wireClient.ts, ipc.js, devCoder.ts
    • worktrees.gcScan/gcClean land as empty-result placeholders so main stays green while both halves are built

    Worker A — backend engine (electron/worktrees.js, services.js, store.js, electron/test/*)

    • real gcScan: per-directory size (du -sk), orphan vs retention classification, blocked reasons, per-project usage rollup
    • real gcClean: re-validates against a fresh scan, batch removal, never deletes a branch (disk only — every commit stays reachable)
    • retention setting plumbed through migrateProject / updateProject
    • collision-safe boring branch names: check local + remote refs, suffix -2, -3

    Worker B — UI (src/components/*, test/*.tsx)

    • per-project worktree disk usage + total in Settings
    • one batch-cleanup dialog, one confirm, blocked rows disabled with reason
    • per-project retention input in Edit Project

    Design decision worth recording: GC reclaims directories only, never branches. Conductor's failure mode was archiving that silently failed to delete branches and branch names colliding onto unrelated merged PRs; keeping every branch ref makes GC unable to lose work, and the collision-safe naming fixes the second half.

  3. currentbits commented on Aug 17, 2026

    @currentbits
    OwnerAuthor

    Landed on main

    Four commits: shared contract → UI half → backend half → two integration fixes found by dogfooding.

    Backend (electron/worktrees.js)

    • gcScan: du -sk per directory, orphan vs retention classification, blocked reasons (uncommitted changes / unreadable), per-project usage rollup + total
    • gcClean: re-scans and only removes what the fresh scan still reports unblocked, so a stale UI selection can never delete live work; clears thread pointers, one broadcast for the batch
    • enforceRetention runs on the boot sweep after the orphan pass — opt-in, so no limit means no-op. Orphans stay the dialog's job (they deserve a human look)
    • collision-safe branch names: uniqueCoderBranch checks local and all remote refs, suffixes -2, -3; a rename that finds no free name leaves the placeholder alone
    • branches are never deleted — GC reclaims directories only, so no commit can be lost to it

    UI (src/components/WorktreeGcSection.tsx, EditProjectModal)

    • per-project usage rows + total in Settings, one batch dialog with pre-selected unblocked candidates, blocked rows disabled with their reason, one confirm reading Delete N worktrees (X MB)
    • retention input per project; empty/0 = unlimited stays the default

    Two gaps found by running the scan against the real state dir

    1. Nothing enforced retention — the scan only surfaced candidates, so a limit applied only when the user opened the dialog. Now enforced on the boot sweep.
    2. isSettledForGc ignored archived. The renderer filters archived threads before the settle split, so they never carry a settled override — and on this machine that was 108 of 127 worktree-holding threads: precisely the invisible worktrees this issue is about. Archived now counts as settled for GC.

    Measured on the real state dir (read-only simulation, nothing deleted): 108 worktrees, 58 GB — 7x T3's cautionary 8 GB. Reclaimable at retention=10: 26 GB; at retention=3: 44 GB. Before the archived fix: 0.

    Tests: electron/test/worktree-gc.test.js (13 cases), test/worktreeGc.test.tsx, plus store/services coverage. Full suite green — 914/915 electron, the single failure being the pre-existing web-integration reconnect baseline on main; renderer 796/796, core and memory green; tsc --noEmit clean.

  4. added
    plan:done
    and removed
    plan:doing
    on Aug 17, 2026
  5. added 2 commits that reference this issue on Aug 17, 2026
  6. currentbits commented on Aug 17, 2026

    @currentbits
    OwnerAuthor

    Research note (fleet ops, Microsoft's attestation model): extend GC to sprawl attestation — stale automations, abandoned worktrees, dormant threads flagged for periodic keep-or-archive review, not just silent deletion.

  7. currentbits commented on Aug 19, 2026

    @currentbits
    OwnerAuthor

    Follow-up filed as #559. The engine landed here works; the default did not. Read-only scan of the maintainer's machine two days after this closed: 96.0 GB across 166 worktree directories, 161 of them belonging to archived threads, worktreeRetention undefined on all five projects, so enforceRetention has been a no-op on every boot sweep (worktrees.js:3789). A default of 10 per project would reclaim 64.0 GB of that, computed against the real store with this issue's own exclusion rules.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementplan:done

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions