Skip to content

fix(repos): worktrees add warns that a merged worktree was not reclaimed when a parallel run already removed it #2408

Description

@dan-petty

Problem Description

When two devops repos worktrees add runs start at about the same time, the second run warns that it did not reclaim a merged worktree that the first run already removed. The end state is correct, because the worktree is gone. The warning is false, and it tells the person or the agent that something failed.

The cause: prune_worktrees (src/devops_cli/git/worktree_inventory.py) decides every worktree before it removes one, and each decision reads GitHub (get_commit_pulls). The reads take seconds. Two runs decide the same merged worktree as removable. The first run removes it. The git worktree remove of the second run then fails with "is not a working tree", and the loop reports the item as failed. reclaim_merged_worktrees (src/devops_cli/git/worktree_add.py) turns each failed item into reclaim_error, and devops repos worktrees add prints it as a warning.

Reproduction Steps

  1. Have one clean worktree under .data/agent/worktrees/ at the head of a merged pull request (here w-2395, merged as chore(deps): update ollama to 0.40.12 #2397).
  2. Start uv run devops repos worktrees add <issue> --type feat --slug <slug> for two different issues at the same time, from two shells.
  3. Read the output of the run that finishes second.

Seen on 2026-10-28 at origin/main bb46660, when four agents started their worktrees at once.

Expected vs Actual Behavior

Expected: a worktree that is gone when the run removes it counts as removed by another run. The run prints no warning for it.

Actual: Merged worktrees were not reclaimed: /workspaces/devops-cli/.data/agent/worktrees/w-2395: fatal: '/workspaces/devops-cli/.data/agent/worktrees/w-2395' is not a working tree

Environment Diagnostics

devops-cli at origin/main bb46660, Python 3.14, git in the devcontainer, rootless Podman on WSL2.

Logs, Traces, or Terminal Output

Merged worktrees were not reclaimed: /workspaces/devops-cli/.data/agent/worktrees/w-2395: fatal: '/workspaces/devops-cli/.data/agent/worktrees/w-2395' is not a working tree
Created worktree /workspaces/devops-cli/.data/agent/worktrees/w-815 on branch feat/815-ollama-slot-wait-metric.

git worktree list after both runs shows no w-2395.

Pre-Submission Checklist

  • I have verified that no sensitive credentials, private keys, or internal tokens are contained in this issue report.
  • I have checked existing issues and pull requests to ensure this is not a duplicate.

Proposed design

Problem statement

Two parallel devops repos worktrees add runs both decide the same merged worktree is removable. The first run removes it. The second run's git worktree remove fails with "is not a working tree". The code reports this as a failed item, and reclaim_merged_worktrees turns it into a reclaim_error warning. The end state is correct because the worktree is gone, but the warning falsely tells the user or agent that a step failed.

Acceptance criteria

  • Criterion 1: When git worktree remove fails with "is not a working tree", the item is reported with status "removed" instead of "failed".
    Verification: A unit test in tests/test_repos_worktrees_reclaim.py simulates a worktree whose directory was deleted before prune_worktrees runs and asserts the item's status is "removed" and failed_count is 0.
  • Criterion 2: reclaim_merged_worktrees does not set reclaim_error when the only failure is "is not a working tree".
    Verification: A test calls reclaim_merged_worktrees in a scenario where the worktree was already removed and asserts the returned ReclaimResult.error is None.
  • Criterion 3: devops repos worktrees add prints no "Merged worktrees were not reclaimed" warning for a worktree already removed by a parallel run.
    Verification: A test or manual run of two parallel add commands shows no warning line in the second run's output.
  • Criterion 4: All existing worktree tests continue to pass.
    Verification: uv run devops ci test tests/test_repos_worktrees_reclaim.py tests/test_repos_worktrees_add.py tests/test_repos_worktrees.py passes.

Key questions

  • Where should the fix go: in prune_worktrees or in reclaim_merged_worktrees?: In prune_worktrees in src/devops_cli/git/worktree_inventory.py, because it is the source of the "failed" status. Other callers such as devops repos worktrees prune also benefit from the fix. (proposed) [sources: src/devops_cli/git/worktree_inventory.py:486]
  • What status should the item receive when git worktree remove says the path is not a working tree?: "removed", because the end state is identical to a successful removal: the worktree record is gone and the directory is gone. (proposed) [sources: src/devops_cli/git/worktree_inventory.py:486]
  • What is the exact git error message that identifies the race?: The stderr contains "is not a working tree", as shown in the issue's log output: fatal: '/workspaces/devops-cli/.data/agent/worktrees/w-2395' is not a working tree. [sources: src/devops_cli/git/worktree_inventory.py:486]
  • Does the fix require a change to WorktreePruneItem or WorktreePruneReport?: No. The status field is a free-form string and "removed" is already a valid status. The failed_count and removed_count fields in the report already handle the two cases. [sources: src/devops_cli/git/worktree_inventory.py:62]

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

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

    @dan-petty
    OwnerAuthor

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

    • Type: type/bug. A race condition produces a false error warning in an existing command.
    • Priority: P2-Medium. The end state is correct, but the false warning misleads agents and humans into thinking a step failed.
    • Value: Medium. It removes a confusing false alarm that agents and humans must interpret in every parallel run.
    • Effort: Low. The fix is to treat the 'is not a working tree' error as success in the reclaim loop.
    • Related: feat(repos): remove worktrees whose commits are merged without a manual prune #2338, which asks for a different change. Both concern automatic removal of merged worktrees, but #2338 adds the feature while the candidate fixes a false warning when two runs remove the same worktree.
  3. dan-petty commented on Oct 28, 2026

    @dan-petty
    OwnerAuthor

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

  4. added
    priority/p2-medium
    status/ready
    value/medium
    effort/low
    on Oct 28, 2026
  5. added a commit that references this issue on Oct 29, 2026
  6. removed
    status/ready
    on Oct 30, 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

    effort/lowpriority/p2-mediumsource/agenttype/bugvalue/medium

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions