Skip to content

fix(repos): prune keeps worktrees with data or outside its folder - #6477

Merged
dan-petty merged 16 commits into
mainfrom
fix/6435-worktrees-prune-deletes-ignored-and-foreign
Nov 20, 2026
Merged

dan-petty merged 16 commits into
mainfrom
fix/6435-worktrees-prune-deletes-ignored-and-foreign

Conversation

@devops-cli-service

@devops-cli-service devops-cli-service Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

devops repos worktrees prune --confirm removed each clean worktree at the head of a merged pull request in any folder, and git worktree remove also deletes the files that .gitignore hides.

Prune and the reclaim of worktrees add now ask one scope rule. It keeps a worktree outside .data/agent/worktrees, also a record whose directory looks missing, and a worktree that holds an ignored data file. Caches such as .venv and __pycache__ are no data: used worktrees hold 70 to 165 ignored paths, nearly all caches. Prune asks the rule again right before each removal, and a warning of git ls-files makes the count unknown. The dirty check sees untracked files also when status.showUntrackedFiles is no. --force still removes each worktree but the main one and the one of the current directory. Each test gets its own workspace file.

Closes #6435

Change: openspec/changes/archive/2026-11-20-6435-worktrees-prune-deletes-ignored-and-foreign/

@devops-cli-service devops-cli-service Bot added type/docs Type docs: a change to the documents scope/openspec OpenSpec changes, specs and their checks labels Nov 19, 2026
@dan-petty
dan-petty force-pushed the fix/6435-worktrees-prune-deletes-ignored-and-foreign branch from e10d382 to a745746 Compare November 20, 2026 01:51
@dan-petty dan-petty changed the title docs(openspec): plan #6435 Refuse ignored and foreign worktrees in prune fix(repos): prune keeps worktrees with data or outside its folder Nov 20, 2026
@dan-petty dan-petty added scope/repos The devops repos commands and worktrees type/bug Type bug: the code does not do what it must and removed type/docs Type docs: a change to the documents scope/openspec OpenSpec changes, specs and their checks labels Nov 20, 2026
@dan-petty

Copy link
Copy Markdown
Owner

An agent took over the Service plan of #6435 and delivered it on this branch: a plan revision, the build (tests first), a separate read-only review, and the fixes of that review. This pull request is now the delivery pull request.

The 5 findings of the plan state comment

  1. verify_form, group 2: each task group now runs every test file that it changes in its Verification.
  2. missing_path, group 2: managed_root is a required keyword-only parameter of _decided_by_local_state, _decide, plan_prune and prune_worktrees, and each caller (worktrees_prune, reclaim_merged_worktrees, the direct test call) changes in the same group.
  3. false_premise, proposal: the proposal says that prune can remove such files. It does not say that it removed them.
  4. spec_delta_wrong: the help and the spec say that --force keeps the main worktree and the worktree that holds the current directory.
  5. design, missing directory: the folder check comes before the missing-directory rule, so prune keeps the record of a worktree outside the managed folder whose directory looks missing, as the reclaim keeps it.

Design change: count only ignored data files

On 2026-11-19, 12 used worktrees of this repository each held 70 to 165 ignored paths, nearly all of them caches (881 __pycache__ folders, .venv, .pytest_cache, .ruff_cache, .mypy_cache, .hypothesis, dist). A rule that counts each ignored path keeps almost every used worktree, and only --force frees the disk (about 1.5 GB each). The shared rule now counts only the ignored files that no regenerable path holds (CONST_WORKTREE_REGENERABLE_PATHSPECS), with git ls-files --others --ignored --exclude-standard -z and exclude pathspecs. The new call took 0.32 to 0.57 s where git status --ignored took 0.21 to 0.36 s (median of 3, 6 worktrees).

Fixes of the build review (7 findings)

  • .coverage* matched at any depth: only .coverage and .coverage.* at the root and under .data are regenerable now.
  • Prune asks the scope rule again right before each removal, so a data file written during the GitHub reads stays.
  • A warning of git ls-files (a directory it cannot open) makes the count unknown, so the worktree stays.
  • count_dirty_paths passes --untracked-files=normal, so status.showUntrackedFiles=no cannot hide untracked files.
  • Test runs wrote .code-workspace into the worktree root (8 of 27 worktrees). The test floor now gives each test a workspace file under tmp_path.
  • The fake git of tests/git/test_repos_worktrees.py answers ls-files with 128 for a broken .git file, as real git does.
  • The prune spec delta modifies Local state protects a worktree: the scope refusal comes before the lock and the dirty state.

Read-only preview on the real repository (no --confirm, after the review fixes)

devops repos worktrees prune from w-6435 refused 52 of 53 worktrees and found 1 removable: w-6480, head of merged #6482, only caches, 1.1 GB. It refused 35 worktrees outside the managed folder (under <home>/.gemini, <home>/.local/share, /tmp, <home>/.claude/worktrees and .data/agent/scratch), 5 that hold data (w-2176 14354 files of mutants, w-2176-t633 49011, w-6461 400, w-2341 3, w-ghsa-g893 1), 3 dirty, 7 with no merged pull request, the main worktree and the current directory.

The 1 data file of w-ghsa-g893 is a .code-workspace that an old test run wrote (it lists pytest-of-vscode folders). This change stops new ones. A person can delete the old file to let prune take that worktree.

Checks

  • Failing tests first: 5 tests of the review fixes failed on the branch before the fix (commit test(repos): prune keeps data that its first check cannot see).
  • devops ci fail-to-pass --base origin/main: exit 0; the changed tests fail on the base.
  • Full devops ci test: 26284 passed, 3 failed. The 3 failures (review_window of test_git_contract.py, test_the_facts_of_a_version_window) fail on main too in this container; see fix(tests): pass the review_window git contract with git 2.55 #6380 and fix(ci): the local git row fails without the review extra #4997. No .code-workspace was left in the worktree.
  • Scoped devops ci --check rows (lint, format, docs, constructs, openspec, test-settings, test-tree, startup-imports, swallowed-failures): pass. mypy on the changed source files: clean. devops roadmap plan-check 6477: no problem.

The pull request stays a draft until a person reviews the diff in tuicr (tuicr -r origin/main..HEAD).

@dan-petty

dan-petty commented Nov 20, 2026 •

Copy link
Copy Markdown
Owner

Check results

Head commit: bbaa733ebbd35335ce31433beacb7184aa1b4565

Check Conclusion Link
Tests & Coverage success run
devops-cli success run
zizmor success run
trivy-config success run
Trivy success run
Plan Check skipped run
CodeQL success run
Test Job Duration skipped run
Tests on Main skipped run
Dependency Freshness skipped run
MCP Server Image success run
Tests & Coverage (shard 5 of 5) success run
Tests & Coverage (shard 2 of 5) success run
Design Tokens success run
Tests & Coverage (shard 4 of 5) success run
Tests & Coverage (shard 1 of 5) success run
Tests & Coverage (shard 3 of 5) success run
Changed-Test Independence success run
Runner Image success run
Service Image success run
Analyze (actions) success run
Static Analysis success run
Analyze (python) success run
Fail-to-Pass Check success run
Docs Site Build success run
Semantic PR title success run

Fail-to-pass run: https://github.com/dan-petty/devops-cli/actions/runs/44658518944/job/136030624975

Skipped check: Plan Check
Skipped check: Test Job Duration
Skipped check: Tests on Main
Skipped check: Dependency Freshness

devops-cli-service Bot and others added 16 commits November 20, 2026 02:05
Resolve the five review findings of the Service plan and count only the ignored data files: an ignored file that is not a regenerable cache or build output keeps a worktree. The folder refusal comes before the missing-directory rule, also for a prunable record. Each task group lists every caller of a changed signature and runs every test file that it changes.
Five tests fail on the branch before the fix: a note whose name starts
with .coverage, a directory that git cannot open, a data file written
after the decision (prune and the reclaim), and an untracked file that
status.showUntrackedFiles=no hides. The GitHub fake gets a before_answer
hook that runs on each request before its answer.
Prune asks the scope rule again right before each removal, because the
decision pass reads GitHub for each worktree first. A warning of git
ls-files makes the count of ignored data files unknown. The status check
passes --untracked-files=normal, so status.showUntrackedFiles=no cannot
hide untracked files. Only the coverage outputs at the root and under
.data are regenerable.
The sync failure test wrote the relative default .code-workspace into
the root of the worktree that ran the suite, and the scope rule counted
it as data. The test now points workspace.file at tmp_path. The fake
git of the prune tests answers ls-files as real git does, so a broken
.git file fails it with 128.
The prune delta modifies 'Local state protects a worktree': the scope
refusal comes before the lock and the dirty state, and the dirty count
includes untracked files that the git configuration hides. New scenarios
cover a data file written during the GitHub reads, a directory that git
cannot open and a note named like coverage output. TESTING.md names the
before_answer hook of the GitHub fake.
The default .code-workspace resolves in the project root. The sync test
of test_repos.py and the clone tests of test_repos_sync_gitea.py wrote
it into the worktree that ran the suite, and prune counted it as data.
The test floor now writes workspace.file under tmp_path into the config
of each test, so the change of the sync test is not needed.
@dan-petty
dan-petty force-pushed the fix/6435-worktrees-prune-deletes-ignored-and-foreign branch from a745746 to bbaa733 Compare November 20, 2026 02:07
@dan-petty
dan-petty marked this pull request as ready for review November 20, 2026 02:14
@dan-petty
dan-petty self-requested a review as a code owner November 20, 2026 02:14
@dan-petty
dan-petty merged commit 0fc3d13 into main Nov 20, 2026
26 checks passed
@dan-petty
dan-petty deleted the fix/6435-worktrees-prune-deletes-ignored-and-foreign branch November 20, 2026 03:24
dan-petty-release Bot added a commit that referenced this pull request Nov 20, 2026
🤖 I have created a release *beep* *boop*
---


##
[0.182.3](v0.182.2...v0.182.3)
(2026-11-20)


### Bug Fixes

* **repos:** prune keeps worktrees with data or outside its folder
([#6477](#6477))
([0fc3d13](0fc3d13))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: dan-petty-release[bot] <346998822+dan-petty-release[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope/repos The devops repos commands and worktrees type/bug Type bug: the code does not do what it must

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant