Skip to content

fix(repos): worktree cleanup keeps other worktrees #4092

Closed

Description

@dan-petty

Problem

devops repos worktrees add first runs reclaim_merged_worktrees (src/devops_cli/git/worktree_add.py). That function calls inventory_worktrees and prune_worktrees (src/devops_cli/git/worktree_inventory.py). inventory_worktrees reads git worktree list, so it returns every worktree of the repository, wherever its folder is. No code limits the removal to the folder that the command manages (<data dir>/agent/worktrees, with w-<issue> for each issue).

A worktree is selected for removal when all of these are true (_decided_by_local_state, _decide):

  1. It is not the main worktree and not the current directory.
  2. It is not locked.
  3. It is not dirty. count_dirty_paths reads git status --porcelain, so a file that .gitignore hides does not count.
  4. The tip of its branch is the head of a merged pull request (get_commit_pulls).

A worktree whose directory is missing (prunable) is selected without rule 4.

On 2026-11-07 the command removed worktrees of other agents five times in one day:

  • Folders under /home/vscode/.gemini/antigravity/worktrees, for example implement_oldest_planned_tickets, pr3954_evaluate_fix and issue1357_openspec_plans (1.1 GB). These are outside the managed folder.
  • Stale folders under .data/agent/worktrees: w-3892, w-3898 and w-3911. These are inside the managed folder, and the rules above selected them because their branch was merged.

Each branch was merged, but the folder belonged to another agent.

Expected

The cleanup removes only worktrees under the folder it manages, only when their branch is merged and they have no uncommitted change. It names each removal in its output.

  • A worktree outside <data dir>/agent/worktrees is never selected, and --force does not change this for the add reclaim.
  • Inside the managed folder, the merged-branch rule and the clean rule stay. The cleanup treats a file that .gitignore hides as a change (or refuses the worktree and says so), so that it does not delete ignored work.
  • The output names each removal, with its path and size, as _render_prune_item does today. It also names each worktree that it left, with the reason, when the worktree is outside the folder.
  • devops repos worktrees prune keeps its current scope. The change limits the add reclaim only, and the proposal says so.

Acceptance criteria

The test file tests/git/test_repos_worktrees_reclaim_scope.py is new. It uses real git in tmp_path repositories and the GitHub fake of tests/git/test_repos_worktrees_reclaim.py.

  • uv run pytest tests/git/test_repos_worktrees_reclaim_scope.py::test_worktree_outside_managed_folder_is_kept fails on the current code: a clean worktree outside agent/worktrees, at the head of a merged pull request, is removed after worktrees add.
  • uv run pytest tests/git/test_repos_worktrees_reclaim_scope.py::test_merged_clean_worktree_inside_folder_is_removed_and_named passes before and after, and checks the output names the path.
  • uv run pytest tests/git/test_repos_worktrees_reclaim_scope.py::test_ignored_file_keeps_worktree fails on the current code: a worktree whose only change is a file that .gitignore hides is removed.
  • uv run pytest tests/git/test_repos_worktrees_reclaim_scope.py::test_missing_directory_outside_folder_is_kept fails on the current code: a record whose directory is missing and is outside the folder is removed.
  • devops ci fail-to-pass --base origin/main lists the three failing tests above as fail-to-pass.

Existing tool considered

git worktree list, git worktree remove and git status --ignored already give every fact. pathlib.Path.is_relative_to (standard library) decides the folder rule. No open tool decides who owns a worktree, and no new dependency is needed.

Cost

  • Build: about 40k tokens (one filter in reclaim_merged_worktrees, a flag for ignored files, four tests, one paragraph in CONTRIBUTING.md) and about 1 hour.
  • Keep: low. One path comparison, covered by the tests.
  • Fix: the lost work is the cost today. A removed worktree of another agent costs a full re-do of that agent's checkout, and 1.1 GB of ignored data cannot be rebuilt in minutes.

Related

Proposed design

Problem statement

The worktrees add reclaim removes any clean worktree at the head of a merged pull request. It also removes worktrees outside the <data dir>/agent/worktrees folder and worktrees with ignored files. It has removed worktrees of other agents and their ignored data. The change limits the reclaim to the managed folder and keeps a worktree that has an ignored file.

Acceptance criteria

Requirement: Add reclaim scope

The add reclaim SHALL select a worktree only when its resolved path is under the data dir agent worktrees folder.

Scenario: Merged worktree outside the managed folder

  • WHEN A clean worktree at the head of a merged pull request is outside the managed folder and worktrees add runs.
  • THEN The reclaim keeps the worktree, reads no pull request for it, and names it as kept with the outside-folder reason.

Requirement: Merged worktree inside the managed folder

The add reclaim SHALL remove a clean and unlocked worktree under the managed folder when its tip heads a merged pull request.

Scenario: Merged clean worktree inside the managed folder

  • WHEN A clean worktree under the managed folder is at the head of a merged pull request and worktrees add runs.
  • THEN The reclaim removes the worktree and the output names its path and size.

Requirement: Ignored file keeps a worktree

The add reclaim MUST keep a worktree under the managed folder when a file that .gitignore hides is its only change.

Scenario: Only an ignored file is present

  • WHEN A worktree under the managed folder has only an ignored file as an uncommitted change and its tip heads a merged pull request.
  • THEN The reclaim keeps the worktree and names it as kept with an ignored-file reason.

Requirement: Missing directory outside the managed folder

The add reclaim SHALL keep a prunable worktree record whose missing directory is outside the managed folder.

Scenario: Prunable record outside the managed folder

  • WHEN A worktree record has a missing directory outside the managed folder and worktrees add runs.
  • THEN The reclaim keeps the record and names it as kept with the outside-folder reason.

Requirement: Prune command scope

The worktrees prune command MUST keep its current scope and consider worktrees outside the managed folder.

Scenario: Prune of an outside worktree

  • WHEN A clean worktree outside the managed folder is at the head of a merged pull request and worktrees prune --confirm runs.
  • THEN The prune command removes the worktree as it does today.

Requirement: Fail-to-pass check

The fail-to-pass check SHALL list the three new tests that fail on the base revision.

Scenario: Fail-to-pass run on the fix

  • WHEN The fix runs devops ci fail-to-pass --base origin/main.
  • THEN The check lists the outside-folder test, the ignored-file test and the missing-directory test as fail-to-pass.

Requirement: Existing reclaim tests

The existing reclaim tests that expect a removal or a GitHub read SHALL place the worktree under the managed folder.

Scenario: Existing reclaim test file

  • WHEN The updated tests/git/test_repos_worktrees_reclaim.py runs.
  • THEN Every test that expects a removal or a GitHub read uses a worktree under the managed folder and the file passes.

Requirement: Contributing guide scope

CONTRIBUTING.md SHALL state that the add reclaim keeps worktrees outside the managed folder.

Scenario: Contributing guide paragraph

  • WHEN A reader opens the worktrees add paragraph in CONTRIBUTING.md.
  • THEN The paragraph says that the reclaim keeps worktrees outside the managed folder and names them.

Existing Tool Considered

No alternatives check has run. The git commands worktree list, worktree remove and status --ignored give every fact. pathlib.Path.is_relative_to decides the folder rule. No open tool decides who owns a worktree. The change needs no new dependency. The written cost is about 40k tokens and about 1 hour to build, keep and fix the filter, the ignored-file check, four tests and one documentation paragraph.

Key questions

  • Where is the managed worktree folder, and how does the reclaim recognize it?: The managed folder is the resolved <data dir>/agent/worktrees. worktree_directory builds w-<issue> under it. The current-directory guard already uses Path.is_relative_to on resolved paths. The reclaim compares each worktree path to the resolved managed folder with Path.is_relative_to. [sources: src/devops_cli/git/worktree_add.py, src/devops_cli/git/worktree_inventory.py, CONTRIBUTING.md]
  • Where should the managed-folder scope apply?: Apply the scope in reclaim_merged_worktrees before plan_prune or prune_worktrees. Keep prune_worktrees and the worktrees prune command unchanged. Build refused items for out-of-scope worktrees and read no pull request for them. (best practice) [sources: src/devops_cli/git/worktree_add.py, src/devops_cli/git/worktree_inventory.py, src/devops_cli/commands/repos.py]
    • Chosen: Filter in reclaim_merged_worktrees. It limits only the add reclaim and leaves the prune command with its current scope.
    • Rejected: Filter in prune_worktrees for every caller. It changes the prune command too, which the issue says must keep its current scope.
    • Rejected: Filter in _decide with a global setting. It couples a reusable decision function to the add command and risks changing other callers.
  • How should the add reclaim treat a file that .gitignore hides?: Add a separate ignored-path check for the add reclaim. count_ignored_paths reads git status --porcelain --ignored -z and counts entries that start with !!. inventory_worktrees gets an include_ignored flag. Only reclaim_merged_worktrees sets it. _decided_by_local_state keeps a worktree with an ignored path and says so. (best practice) [sources: src/devops_cli/git/worktree_inventory.py:184, src/devops_cli/git/worktree_add.py]
    • Chosen: Add a separate ignored-path check for the add reclaim. It keeps ignored work and names the reason without changing the dirty count.
    • Rejected: Count ignored files as dirty for the add reclaim. It hides the ignored-file reason in the dirty count.
    • Rejected: Keep the current clean rule. It deletes ignored work, which the issue reports as a defect.
  • How does the command report worktrees outside the managed folder?: Add a kept list of WorktreePruneItem records to ReclaimResult and WorktreeAddReport. Each outside worktree is a refused item with the reason outside the managed folder. The command renders it with the same item renderer as a prune item. (best practice) [sources: src/devops_cli/git/worktree_add.py, src/devops_cli/commands/repos.py:444]
    • Chosen: Add a kept list of refused items. It names each outside worktree and reuses the existing item shape.
    • Rejected: Put the reason in reclaim_error. reclaim_error means a failed reclaim, not a kept worktree.
    • Rejected: Skip outside worktrees silently. The expected output requires the reason.
  • Does --force widen the add reclaim?: No. reclaim_merged_worktrees has no force path and calls prune_worktrees with confirm=True and no force. The scope check runs before any local-state decision. The worktrees prune --force command keeps its current behavior. [sources: src/devops_cli/git/worktree_add.py, src/devops_cli/git/worktree_inventory.py, src/devops_cli/commands/repos.py]
  • How do the existing reclaim tests change?: The tests that expect a removal or a GitHub read must create their worktree under <isolate_data_dir>/agent/worktrees/w-<issue>. Keep the new test_repos_worktrees_reclaim_scope.py for the outside-folder and ignored-file cases. Tests that call prune_worktrees directly keep their worktree paths. [sources: tests/git/test_repos_worktrees_reclaim.py, tests/git/test_repos_worktrees.py]
  • Does the ignored-file rule apply to the worktrees prune command?: No. The issue limits the change to the add reclaim. The prune command keeps its current clean rule and its current scope. reclaim_merged_worktrees sets include_ignored; the prune command does not. (best practice) [sources: src/devops_cli/git/worktree_add.py, src/devops_cli/git/worktree_inventory.py, src/devops_cli/commands/repos.py]
    • Chosen: Only the add reclaim checks ignored files. It matches the issue scope and leaves prune unchanged.
    • Rejected: Both add and prune check ignored files. It changes prune behavior beyond the issue.
  • What happens to a prunable worktree record inside the managed folder?: The scope filter runs first. A prunable record under the managed folder keeps the current rule: it is removable without a pull request read. A prunable record outside the managed folder is kept. [sources: src/devops_cli/git/worktree_inventory.py, src/devops_cli/git/worktree_add.py]

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    source/agent
    type/bug
    scope/repos
    status/new
    value/high
    effort/low
    on Nov 7, 2026
  2. dan-petty commented on Nov 7, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones.

    • Type: type/bug. The fix prefix of the title gives the type.
    • Scope: scope/repos. The (repos) scope of the title gives the scope.
    • Priority: P1-High. The bug deleted other agents' worktrees (1.1 GB) on 2026-11-07, causing real data loss.
    • Value: High. Prevents destruction of work belonging to other agents, which is a critical safety property.
    • Effort: Low. One path filter, one ignored-file flag, four tests, and a doc paragraph; the body estimates about 1 hour.
    • Related: feat(cli): worktree inventory & safe orphan reclamation (devops repos worktrees) #700, which asks for a different change. Both concern 'devops repos worktrees add' reclaim, but #700 delivered the feature and the candidate fixes a scope bug in it.
  3. added
    priority/p1-high
    on Nov 7, 2026
  4. added
    status/ready
    and removed
    status/new
    on Nov 8, 2026
  5. devops-cli-service commented on Nov 8, 2026

    @devops-cli-service
    Contributor

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

  6. added
    owner/agent
    status/in-progress
    status/in-review
    and removed
    status/ready
    status/in-progress
    on Nov 8, 2026
  7. removed
    status/in-review
    owner/agent
    on Nov 8, 2026
  8. devops-cli-service commented on Nov 8, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-08 03:31 UTC

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

    effort/lowpriority/p1-highscope/repossource/agenttype/bugvalue/high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions