Skip to content

feat(cli): worktree inventory & safe orphan reclamation (devops repos worktrees) #700

Closed

Description

@dan-petty

Context & Rationale

Nothing in src/ runs git worktree; the only hit, ci/cache.py:168, parses status porcelain. delegate_task (ai/harness/workflow.py:280) hands a sub-agent no workspace and the harness Shell defaults to cwd='.' (ai/harness/shell.py:110), so subagents share the caller's checkout — yet worktrees still accumulate. An external harness left five worktree-wf_* checkouts under .claude/worktrees/, each clean, one commit ahead of main, with a tip on no other local or remote ref, and this tool cannot list them. git itself reports locked and prunable per entry and prune --expire handles missing directories; what it cannot say is whether a checkout still holds unmerged work. vibes patterns/collision-free-worktree-fleet-allocation.md names zombie-checkout accumulation as one of four hazards and prescribes leased, TTL-bounded worktrees with automated pruning.

Scope & Deliverables

  • Context & Rationale*: Nothing in src/ runs git worktree; the only hit, ci/cache.py:168, parses status porcelain. delegate_task (ai/harness/workflow.py:280) hands a sub-agent no workspace and the harness Shell defaults to cwd='.' (ai/harness/shell.py:110), so subagents share the caller's checkout — yet worktrees still accumulate. An external harness left five worktree-wf_* checkouts under .claude/worktrees/, each clean, one commit ahead of main, with a tip on no other local or remote ref, and this tool cannot list them. git itself reports locked and prunable per entry and prune --expire handles missing directories; what it cannot say is whether a checkout still holds unmerged work. vibes patterns/collision-free-worktree-fleet-allocation.md names zombie-checkout accumulation as one of four hazards and prescribes leased, TTL-bounded worktrees with automated pruning.
  • Deliverable*: A devops repos worktrees group, status first: parse git worktree list --porcelain (shell out through core/process.run_subprocess; GitPython 3.1.62 has no worktree API, only WorkTreeRepositoryUnsupported) and add per entry commits ahead of main, dirty-path count, whether any other ref contains the tip, and git's own locked/prunable flags. Then prune, dry-run by default: remove only entries that are clean and contained elsewhere, refuse everything else without --force, finish with git worktree prune. Use git worktree lock --reason as the owner marker so plain git sees it. Port nothing from examples/worktree-swarm-arbiter/worktree_arbiter.py: its lock wait is a fixed 5 x 50 ms loop and process_group_id is hard-coded 0, so its killpg branch never fires. The lease registry (pgid, expiry, heartbeat on the SandboxRegistry._file_lock seam, sandbox/registry.py:40) with SIGTERM-to-SIGKILL release via _terminate_process_group (ai/harness/shell.py:293) is a later v0.3.1 extension of devops ai shadow, and the index.lock retry belongs in ai/retries on tenacity as separate hygiene; both reuse this command's refusal rules.
  • Constraint*: Reclamation deletes commits. Every orphan measured here holds a commit on no other ref, so a TTL-driven remove --force plus branch -D would have destroyed five commits; the refusal rule is the deliverable, not an option. Placement: find_top_level_repo_root (core/repo.py:45-57) returns candidates[-1], so a worktree nested inside the repository resolves to the main checkout and devops ci run there would aim _clean_coverage_artifacts (commands/ci.py:159) at the main checkout's .data/.coverage*. New worktrees follow the outside-the-tree convention the numbered feature worktrees already use; changing the resolver is a separate decision. Data tier: unmerged fix/553-shared-data-dir deliberately shares one data directory across worktrees, the reverse of vibes' per-worktree DEVOPS_CLI_DATA_DIR recipe, which must not be ported.
  • Measured*: git worktree list --porcelain | grep -c '^worktree ' = 13 today; the count has differed in every session that measured it, so it stays out of the prose. For each worktree-wf_* branch, git rev-list --count main..<branch> = 1 and git for-each-ref --contains <tip> refs/heads refs/remotes lists 0 refs other than the branch itself. git -C <worktree> status --porcelain | wc -l = 0 for every one. timeout 60 du -sh .claude/worktrees timed out, so no size is claimed.
  • Source*: vibes patterns/collision-free-worktree-fleet-allocation.md, patterns/multi-agent-codebase-concurrency.md, observations/systems/06-multi-agent-concurrency-shared-workspace-hazards-and-swarm-coordination.md, examples/worktree-swarm-arbiter/worktree_arbiter.py, examples/worktree-swarm-arbiter/README.md

Strategic Alignment

  • Milestone: v0.2.24 (Reactive Workstation Command Center, Interactive TUI & Unified Operations Hub)
  • Roadmap Reference: docs/ROADMAP.md

Update 2026-10-08: refinement

Review worktrees (#1047, git/worktree.py:28-75) are a second kind of orphan; status reuses core/cleanup.py:76-90. Nested worktrees resolve to themselves, so the coverage hazard is gone; #617 owns checkouts below another repository. Read git worktree list --porcelain -z (#1440).

  • status --json lists each entry's ahead and dirty counts, contained-HEAD flag, locked and prunable.
  • prune previews by default and, without --force, removes only clean entries another ref contains.

Proposed design

Problem statement

devops-cli has no command to list git worktrees, inspect whether each holds unmerged work, or safely prune orphan checkouts. External harnesses (e.g. Claude Code) leave behind worktree-wf_* checkouts that accumulate over time; git itself reports locked and prunable flags but cannot say whether a checkout still holds unmerged commits. The tool needs a devops repos worktrees command group with a status subcommand (per-entry ahead-of-main count, dirty-path count, contained-in-other-ref flag, locked/prunable) and a prune subcommand (dry-run by default, removes only clean-and-contained entries, refuses the rest without --force).

Acceptance criteria

  • Criterion 1: devops repos worktrees status lists every worktree with per-entry commits ahead of main, dirty-path count, whether any other local or remote ref contains the tip, and git's locked and prunable flags.
    Verification: Run devops repos worktrees status in a repository with at least one worktree one commit ahead of main and one fully merged; verify the ahead count, dirty count, contained flag, and locked/prunable values match the output of git worktree list --porcelain plus git rev-list --count main.. and git for-each-ref --contains .
  • Criterion 2: devops repos worktrees status --json emits the same per-entry fields as a JSON array.
    Verification: Run devops repos worktrees status --json and pipe to python -m json.tool; verify each object has keys ahead, dirty, contained, locked, prunable with correct types.
  • Criterion 3: devops repos worktrees prune defaults to a dry-run preview that lists removable and non-removable entries without deleting anything.
    Verification: Create a worktree with unmerged commits, run devops repos worktrees prune, verify the worktree still exists and the output labels it as refused.
  • Criterion 4: devops repos worktrees prune without --force removes only entries whose working tree is clean (zero dirty paths) and whose tip is contained in at least one other local or remote ref.
    Verification: Create two worktrees: one with all commits merged into main (clean, contained) and one with an unmerged commit (clean, not contained). Run devops repos worktrees prune; verify the first is removed and the second is refused.
  • Criterion 5: devops repos worktrees prune --force removes entries regardless of clean or contained status.
    Verification: Create a worktree with an unmerged commit, run devops repos worktrees prune --force, verify the worktree directory is gone and git worktree list no longer shows it.
  • Criterion 6: prune finishes by running git worktree prune to clean up metadata for removed directories.
    Verification: After a successful prune, run git worktree list and confirm no stale entries remain; check that .git/worktrees/ no longer contains the removed entry's directory.
  • Criterion 7: git worktree lock --reason is used as the owner marker and the reason appears in status output.
    Verification: Run git worktree lock --reason "agent-42" on a worktree, then devops repos worktrees status; verify the output shows locked=true and the reason string.
  • Criterion 8: Git output is read NUL-separated per the fix(git): git output the code parses is read NUL-separated, and git outcomes come from exit codes #1440 convention (git worktree list --porcelain -z).
    Verification: Code inspection confirms the subprocess call uses -z and the parser splits on NUL bytes rather than newlines; a worktree path containing a newline is handled correctly in a test.

Key questions

  • How is the worktree list parsed given GitPython has no worktree API?: Shell out through core/process.run_subprocess to git worktree list --porcelain -z; GitPython 3.1.62 exposes only WorkTreeRepositoryUnsupported. [sources: docs/agent/tasks/task-700-worktree-inventory-safe-orphan-reclamation-devops-repos-work.md:17]
  • What is the safe-removal rule for prune?: Remove only entries that are clean (zero dirty paths) AND whose tip is contained in at least one other local or remote ref; refuse everything else unless --force is given. (proposed) [sources: docs/agent/tasks/task-700-worktree-inventory-safe-orphan-reclamation-devops-repos-work.md:17]
  • Is the lease registry (pgid, expiry, heartbeat) part of this issue?: No. It is a later v0.3.1 extension of devops ai shadow and reuses this command's refusal rules but is not delivered here. (proposed) [sources: docs/agent/tasks/task-700-worktree-inventory-safe-orphan-reclamation-devops-repos-work.md:17]
  • Should code from examples/worktree-swarm-arbiter/worktree_arbiter.py be ported?: No. Its lock wait is a fixed 5 x 50 ms loop and process_group_id is hard-coded 0, so its killpg branch never fires. (proposed) [sources: docs/agent/tasks/task-700-worktree-inventory-safe-orphan-reclamation-devops-repos-work.md:17]
  • How are nested worktrees (a worktree inside another repository) handled?: Checkouts and Worktrees Below Another Repository Resolve to Their Own Repository #617 owns checkouts below another repository; nested worktrees resolve to themselves, so the coverage-artifact hazard is out of scope for this issue. [sources: docs/agent/tasks/task-700-worktree-inventory-safe-orphan-reclamation-devops-repos-work.md:18]
  • How is git output read?: NUL-separated via git worktree list --porcelain -z, per the fix(git): git output the code parses is read NUL-separated, and git outcomes come from exit codes #1440 convention that git output the code parses is read NUL-separated and outcomes come from exit codes. [sources: docs/agent/tasks/task-700-worktree-inventory-safe-orphan-reclamation-devops-repos-work.md:17]
  • How are review worktrees (a second kind of orphan) accounted for?: status reuses core/cleanup.py:76-90 to identify review worktrees created by fix(review): a review scans a worktree of the reviewed commit, never hands a deleted file to a tool, and reports which files each tool scanned #1047 (git/worktree.py:28-75) and includes them in the listing. [sources: docs/agent/tasks/task-700-worktree-inventory-safe-orphan-reclamation-devops-repos-work.md:17]

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

#1440, #617
[end-marker]

Activity

  1. added this to the v0.2.24 milestone on Sep 28, 2026
  2. added
    type/feature
    scope/cli
    priority/p1-high
    on Sep 28, 2026
  3. modified the milestones: v0.2.24, v0.2.25, v0.2.26 on Sep 30, 2026
  4. dan-petty commented on Sep 30, 2026

    @dan-petty
    OwnerAuthor

    Moved from v0.2.25 to v0.2.26. v0.2.25 is capped at 12 items (#740): it keeps #739-#742, their dependency #704, and the oldest P1 items. This P1 item is next in line.

  5. removed this from the v0.2.26 milestone on Oct 1, 2026
  6. dan-petty commented on Oct 1, 2026

    @dan-petty
    OwnerAuthor

    Moved from v0.2.26 to the backlog. After the v0.2.23 P1 leftovers were retargeted (they were committed earlier), v0.2.25 and v0.2.26 are full at 12 items each, and the roadmap-service items placed in v0.2.26 stay. It is next in line for a planned release.

  7. added this to the v0.2.34 milestone on Oct 9, 2026
  8. dan-petty-bot commented on Oct 9, 2026

    @dan-petty-bot
    Collaborator

    Status set to Ready at b5ea6ff (see proposed design in issue body).

  9. dan-petty commented on Oct 11, 2026

    @dan-petty
    OwnerAuthor

    Merged

  10. added a commit that references this issue on Oct 11, 2026
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

    priority/p1-highscope/clitype/feature

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions