Skip to content

Auto-cleanup orphaned worktrees on session creation or app startup #201

@PureWeen

Description

@PureWeen

Problem

Worktrees accumulate silently over time and are never automatically cleaned up. Each worktree can consume 1-12 GB of disk space. After several sessions, users can end up with many orphaned worktrees from completed/abandoned work (branches deleted from remote, sessions no longer active, etc.).

Today, cleanup requires the user to manually run git worktree list, cross-reference against active sessions, and git worktree remove each stale one.

Proposed Solution

Add automatic worktree cleanup logic, triggered on app startup or new session creation:

  1. List all worktrees on disk under ~/.polypilot/worktrees/
  2. Cross-reference against active sessions (check _sessions dictionary and Organization.Sessions)
  3. Identify orphaned worktrees — those with no active session pointing to them
  4. Classify orphans by risk level:
    • Safe to auto-remove: Clean (no dirty files) + remote branch gone ([gone] tracking status)
    • Prompt before removing: Has uncommitted changes or untracked files
    • Keep: Has active session or running processes
  5. Auto-remove safe orphans (or at minimum, surface them to the user with an option to clean up)
  6. Run git worktree prune after cleanup to remove stale git refs

Additional Context

RepoManager already has RemoveWorktreeAsync — this work is primarily about adding the discovery/reconciliation logic and hooking it into the session lifecycle.

Key files:

  • PolyPilot/Services/RepoManager.cs — worktree creation/removal
  • PolyPilot/Services/CopilotService.cs — session creation (CreateSessionAsync)

Metadata

Metadata

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions