Skip to content

fix(repos): worktrees prune deletes ignored and foreign trees #6435

Closed

Description

@dan-petty

Problem Description

devops repos worktrees prune --confirm removes a clean worktree whose tip heads a merged pull request. It does not count the files that .gitignore hides, and it does not limit itself to the managed folder .data/agent/worktrees. git worktree remove deletes the ignored files of a worktree. So prune can delete agent scratch files, a local .env or other ignored data, and it removes the worktrees that other tools made outside the repository.

#4092 fixed this for the reclaim step of devops repos worktrees add only:

  • The reclaim keeps a worktree outside the managed folder and a worktree that holds ignored files (src/devops_cli/git/worktree_add.py:259 and :274, which calls count_ignored_paths).
  • Prune decides with _decided_by_local_state and _decide (src/devops_cli/git/worktree_inventory.py:386-433). These functions read only main, current directory, --force, locked, prunable and dirty. No caller of prune uses count_ignored_paths (src/devops_cli/git/worktree_inventory.py:227), although its docstring says that the remove deletes that data (fix(repos): worktree cleanup keeps other worktrees #4092).

Reproduction Steps

  1. Make a worktree of a branch whose pull request is merged, outside .data/agent/worktrees, for example under the worktree folder of another tool.
  2. Put a file that .gitignore hides in it, for example .data/agent/scratch/note.md.
  3. Run uv run devops repos worktrees prune.
  4. The preview lists the worktree as removable ("head of merged pull request #N").
  5. Run it again with --confirm. The worktree and the ignored file are gone.

Seen on 2026-11-19: one prune --confirm removed 5 worktrees under the worktree folder of another agent tool in the home directory, some agent worktrees under .claude/worktrees, and one worktree under .data/agent/worktrees. The branches stay, so no commit is lost. Whether the removed worktrees held ignored files is not known.

Expected vs Actual Behavior

Expected: prune applies the same scope rules as the reclaim of #4092. Without --force, it refuses a worktree that holds ignored files ("holds N ignored paths") and a worktree outside the managed folder ("outside .data/agent/worktrees"). The preview names each refusal. --force stays the way to remove every worktree.

Actual: prune removes each clean worktree at the head of a merged pull request, in any folder, with its ignored files.

Acceptance criteria:

  • A test makes a clean worktree at a merged head that holds one ignored file. Prune refuses it and names the ignored count.
  • A test makes a clean worktree at a merged head outside the managed folder. Prune refuses it and names the folder.
  • Prune and the reclaim decide scope with one shared function, so the two rules cannot drift.
  • The help text of prune and CONTRIBUTING.md say which worktrees prune removes.

Environment Diagnostics

devops-cli at main 0057c41 (2026-11-19). The command is uv run devops repos worktrees prune --confirm.

Logs, Traces, or Terminal Output

removed <home>/.gemini/antigravity/worktrees/devops-cli/pr4117_evaluate_fix (1.1 GB)
removed <home>/.gemini/antigravity/worktrees/devops-cli/pr4138_evaluate_fix (1.2 GB)
removed <home>/.gemini/antigravity/worktrees/devops-cli/pr4146_evaluate_fix (1.1 GB)
removed <home>/.gemini/antigravity/worktrees/devops-cli/pr4155_openspec_plans (1.1 GB)
removed <home>/.gemini/antigravity/worktrees/devops-cli/pr4356_evaluate_fix (1.1 GB)

Dependencies

None.

Alternatives check of 2026-11-19: none.
Tool: none found.
Reason: Searches found only gtr (git-worktree-runner) and its wrappers gtri and gtr.nvim: they list, create and clean merged worktrees but decide no scope and refuse no ignored files or foreign folders, and gtr is archived.

Lacks:

  • No refusal of a worktree that holds ignored paths
  • No managed-folder scope outside .data/agent/worktrees
  • No shared scope function with the add reclaim
  • No prune help text or CONTRIBUTING.md scope paragraph

Proposed design

Problem statement

devops repos worktrees prune --confirm removes a clean worktree whose tip heads a merged pull request in any folder, with the files that .gitignore hides. The reclaim of devops repos worktrees add already keeps a worktree outside .data/agent/worktrees and a worktree that holds ignored paths (#4092), but prune does not use that rule. Prune can thus delete agent scratch files, a local .env and worktrees that other tools made outside the repository. This item makes prune and the reclaim take the scope rule from one shared function, and it states the scope in the help text and in CONTRIBUTING.md.

Acceptance criteria

Requirement: Ignored paths keep a worktree

devops repos worktrees prune SHALL refuse a clean worktree at the head of a merged pull request when the worktree holds ignored paths.

Scenario: One ignored file in the managed folder

  • WHEN a test runs prune on a clean worktree under .data/agent/worktrees at a merged head that holds one file that .gitignore hides
  • THEN the preview keeps the worktree with the reason holds 1 ignored path, and --confirm leaves the directory in place

Requirement: A foreign folder keeps a worktree

devops repos worktrees prune SHALL refuse a worktree whose path is not under .data/agent/worktrees.

Scenario: Worktree in another tool's folder

  • WHEN a test runs prune on a clean worktree at a merged head in a folder outside .data/agent/worktrees
  • THEN the preview keeps the worktree with a reason that names .data/agent/worktrees, and --confirm leaves the directory in place

Requirement: One scope function for prune and reclaim

The prune decision and the reclaim of devops repos worktrees add MUST take the scope refusal from one function.

Scenario: Both callers return the shared refusal

  • WHEN a test calls the shared scope function, the prune decision and the reclaim with one worktree outside the managed folder, and with one worktree that holds one ignored path
  • THEN each caller returns the same refusal text as the shared function for each worktree, so the two scope rules cannot drift

Requirement: Force still removes a refused worktree

devops repos worktrees prune --confirm --force SHALL remove a clean worktree at a merged head that the scope rules refuse.

Scenario: Force removes a worktree with an ignored file

  • WHEN a test runs prune with --confirm --force on a clean worktree at a merged head under .data/agent/worktrees that holds one ignored file
  • THEN the worktree directory is gone

Requirement: Prune help states the scope

The help text of devops repos worktrees prune MUST state which worktrees the command removes.

Scenario: Help output names the folder and the force flag

  • WHEN a test runs devops repos worktrees prune --help
  • THEN the output names .data/agent/worktrees, the ignored-path refusal and --force

Requirement: CONTRIBUTING.md states the scope

CONTRIBUTING.md MUST state which worktrees devops repos worktrees prune removes.

Scenario: The documentation check passes

  • WHEN a person runs uv run devops ci docs after the edit
  • THEN the command exits 0 and reports no documentation problem

Existing Tool Considered

None. The alternatives check of 2026-11-19 records the verdict none: no open, maintained tool or library covers the need. The alternatives block in the issue body lists the tools it considered and what they lack. The change is therefore in-repo code: one shared scope function of about 40 lines with tests. The written cost is about 1,500 tokens to build, keep and fix that code until it matches the tool, and about 30 minutes.

Key questions

  • Which module holds the shared scope decision, and which callers use it?: A public function scope_refusal(entry, *, managed_root) in src/devops_cli/git/worktree_inventory.py, beside count_ignored_paths at line 227. _decide at line 417 gains a managed_root parameter and calls it after _decided_by_local_state and before the merged-pull read. The reclaim at src/devops_cli/git/worktree_add.py:259 and :274 calls it in place of its two inline checks. (best practice) [sources: src/devops_cli/git/worktree_inventory.py:227, src/devops_cli/git/worktree_inventory.py:386, src/devops_cli/git/worktree_inventory.py:417, src/devops_cli/git/worktree_add.py:259, src/devops_cli/git/worktree_add.py:274]
    • Chosen: A function in worktree_inventory.py. The module already holds count_ignored_paths and _decide, so one import serves both callers and no new module appears.
    • Rejected: A method on WorktreeEntry. The rule needs the managed root and a git call for the ignored count, which a value object does not hold.
    • Rejected: A new module worktree_scope.py. One small function does not justify a module, and the repository keeps a rule beside its owner.
  • In which order do the two scope rules run, and which reason does the preview show?: Outside the managed folder first, then the ignored paths. The order matches the reclaim at src/devops_cli/git/worktree_add.py:259 (outside) and :274 (ignored), so both callers print the same reason. The outside check also avoids a git call for a foreign worktree. The ignored reason uses the singular and plural form of the existing dirty reason: holds 1 ignored path and holds 3 ignored paths. (best practice) [sources: src/devops_cli/git/worktree_add.py:259, src/devops_cli/git/worktree_add.py:274, src/devops_cli/git/worktree_inventory.py:386]
    • Chosen: Outside first, then ignored. It keeps the reason text of the reclaim and it saves a git call for a worktree outside the managed folder.
    • Rejected: Ignored first, then outside. It changes the reason that a foreign worktree shows and it adds a git call for each foreign worktree.
  • Does --force bypass the scope rules?: Yes. The scope check runs after _decided_by_local_state, which returns removable=True with the reason forced when --force is set. The main worktree and the worktree that holds the current directory stay kept, as before. (best practice) [sources: src/devops_cli/git/worktree_inventory.py:386, src/devops_cli/git/worktree_inventory.py:417]
    • Chosen: --force bypasses the scope rules. The issue states that --force stays the way to remove every worktree, and the existing local-state check already gives force precedence.
    • Rejected: --force keeps outside and ignored worktrees. It gives the user no way to remove a refused worktree, which contradicts the expected behavior of the issue.
  • Which function counts the ignored paths of a worktree?: count_ignored_paths at src/devops_cli/git/worktree_inventory.py:227. Its docstring says that git worktree remove deletes that data (fix(repos): worktree cleanup keeps other worktrees #4092). [sources: src/devops_cli/git/worktree_inventory.py:227, src/devops_cli/git/worktree_add.py:274]
  • Where does CONTRIBUTING.md state the scope of prune?: In section 4, Git Hygiene & Branch Management, as one paragraph beside the branch rules. (best practice) [sources: CONTRIBUTING.md, CONTEXT.md]
    • Chosen: Section 4 of CONTRIBUTING.md. A contributor reads the git and worktree rules there, and the section is a hand-written document.
    • Rejected: docs/CLI_REFERENCE.md. It is a generated document, and nobody edits it by hand.
  • Does the change need a new dependency or tool?: No. Git's own git worktree and git status --ignored plumbing already provide the removal and the ignored list, and the project wraps them in count_ignored_paths. (best practice) [sources: src/devops_cli/git/worktree_inventory.py:227, src/devops_cli/git/worktree_add.py:274]
    • Chosen: Keep the in-repo wrapper of git. Git's plumbing covers the removal and the ignored list, and the alternatives check found no covering library.
    • Rejected: Add a worktree manager library. The alternatives check found only gtr and its wrappers, which are archived and decide no scope.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    type/bug
    source/agent
    on Nov 19, 2026
  2. dan-petty commented on Nov 19, 2026

    @dan-petty
    OwnerAuthor
    • Priority: P1-High. The prune command deletes user data (ignored files, foreign worktrees) when run with --confirm, causing data loss.
    • Related: fix(repos): worktree cleanup keeps other worktrees #4092, which asks for a different change. Both apply the same scope rules (ignore count, managed folder) to worktree removal, but #4092 fixed the reclaim step while this candidate fixes the prune command path.
  3. added
    status/new
    effort/medium
    value/high
    scope/repos
    priority/p1-high
    on Nov 19, 2026
  4. devops-cli-service commented on Nov 19, 2026

    @devops-cli-service
    Contributor

    Next step: the alternatives check records a verdict. See the section Proposed design of the body.

  5. devops-cli-service commented on Nov 19, 2026

    @devops-cli-service
    Contributor

    The result in the alternatives block of the issue body changed. Before this check: no check. A person decides what the new result means for the item.

    Searches (10)

    Tools considered:

    Searches run:

    • open source tool manage git worktrees prune safe, results: 5
    • git worktree prune command that refuses worktrees with ignored files, results: 5
    • git worktree tool library prune merged branch worktrees, results: 5
    • "git worktree" python library remove ignored untracked, results: 5
    • worktree prune CLI, results: 5
    • git worktree, results: 5
    • git worktree manager github tool, results: 5
    • gtr git worktree runner, results: 5
    • codecrafters git-worktree-runner gtr clean merged worktrees, results: 5
    • git-worktree-runner, results: 5

    Sources:

  6. added
    status/ready
    and removed
    status/new
    on Nov 19, 2026
  7. devops-cli-service commented on Nov 19, 2026

    @devops-cli-service
    Contributor

    Next step: the stage job writes the plan of the item. See the section Proposed design of the body.

  8. added
    owner/service
    on Nov 19, 2026
  9. devops-cli-service commented on Nov 19, 2026

    @devops-cli-service
    Contributor

    Plan state: branch fix/6435-worktrees-prune-deletes-ignored-and-foreign, draft pull request #6477. The pull request plans the item and closes no issue. The stage job and the step fix-plan edit this comment, and a comment of each run tells what the run did.

    The task groups, with the tokens of the inputs of each step, for a budget of 64000 tokens:

    1. Move the prune test worktrees under the managed folder: 20572 tokens
    2. Add one scope refusal and use it in the reclaim: 40320 tokens
    3. Make prune ask the scope rule: 35744 tokens
    4. State the prune scope in its help text: 40906 tokens
    5. Regenerate the command page and document the scope: 23078 tokens

    A model review of the plan found these points. They stop no draft, and a person judges them:

    • major verify_form at tasks (2): Group 2 changes the prune fixtures and expectations in tests/git/test_repos_worktrees.py and tests/git/test_repos_worktrees_reclaim.py, but its verify runs only tests/git/test_repos_worktrees_reclaim_scope.py, so the tests it changes are never run and a broken fixture move passes the group. Evidence: "uv run devops ci test tests/git/test_repos_worktrees_reclaim_scope.py"
    • major missing_path at tasks (2): Group 2 gives plan_prune a new first parameter and tells the reader to pass the root in reclaim_merged_worktrees, but src/devops_cli/git/worktree_add.py, which holds that caller, is missing from the group's changes. The existing call plan_prune(entries, repo=repo) then binds entries to the new root parameter and breaks the group's own dry-run tests. Evidence: "Pass root to plan_prune in worktrees_prune and in reclaim_merged_worktrees"
    • minor false_premise at proposal (why): The plan states as fact that prune has already removed agent scratch files and a local .env, but the issue says whether the removed worktrees held ignored files is not known; the issue only says prune can delete such data. Evidence: "It has removed agent scratch files, a local .env and worktrees that other tools made outside the repository (fix(repos): worktrees prune deletes ignored and foreign trees #6435)."
    • minor spec_delta_wrong at spec repos-worktrees-prune (The prune help states the scope): The added requirement makes the help say that --force removes every worktree but the main one, but the base spec keeps the worktree that holds the current directory also under --force, so the help text this requirement asks for would state a false rule. Evidence: "and that --force removes every worktree but the main one"
    • minor other at design (decisions): The scope check runs after _decided_by_local_state, which returns removable=True for a prunable worktree, so prune still removes a worktree outside the managed folder whose directory is missing, while the reclaim keeps it (its requirement says "also when its directory is missing"). The shared function therefore does not keep the two rules from drifting for that case. Evidence: "return _Decision(entry, removable=True, reason="directory is missing")"
  10. 3 remaining items

  11. added
    status/in-progress
    and removed
    status/ready
    on Nov 20, 2026
  12. added 6 commits that reference this issue on Nov 20, 2026
  13. added
    status/in-review
    and removed
    status/in-progress
    on Nov 20, 2026
  14. removed
    status/in-review
    owner/agent
    on Nov 20, 2026
  15. devops-cli-service commented on Nov 20, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-20 06: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/mediumpriority/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