Repository navigation
feat(skills): let clean-branches-and-worktrees delete build directories from the worktrees it keeps - #4258
Merged
Conversation
…es from the worktrees it keeps A worktree kept for unmerged or uncommitted work still holds gigabytes of target/ and node_modules, so a cleanup run could recover little space. --prune-build-dirs deletes ignored target, node_modules and dist directories that hold no tracked file from each linked worktree left in place, except the main checkout, the running checkout and locked worktrees. Other ignored files, such as .env, are kept.
netzenbot-reviewer
approved these changes
Nov 16, 2026
chrislacy
marked this pull request as ready for review
November 16, 2026 01:54
bbondy
approved these changes
Nov 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A follow up to PR 4046: feat(skills): add a skill that cleans merged branches and worktrees.
User impact: none -- a contributor skill and its selftest; bravebot itself is unchanged.
The problem
People run the
clean-branches-and-worktreesskill to reclaim disk space, but it only frees the space of worktrees it removes. A worktree it keeps, because its branch is unmerged, has commits past the merged head, or is dirty, still holds gigabytes oftarget/andnode_modules. On a working clone of this repository, the kept worktrees held 32.8 GiB of build output that the skill had no way to delete.Reproduce
brave/bravebotwithghsigned in, have at least one linked worktree under.worktrees/whose branch is not merged and that has been built (target/or anode_modulesdirectory present).python3 agents/skills/clean-branches-and-worktrees/clean-branches-and-worktrees.py --dry-run --prune-build-dirs.Observed before this change: the script has no
--prune-build-dirsoption, and kept worktrees keep their build output.Expected: the dry run lists each ignored
target,node_modulesanddistdirectory in kept linked worktrees with its size and deletes nothing. The same command without--dry-rundeletes them.Real run on a working clone of this repository (branch skip list trimmed):
It matched the preceding dry run. Free space on the volume went from 22 GiB to 59 GiB. Afterwards the three worktrees had no
target,node_modulesordistdirectory,git status --porcelainwas empty in each, and their other ignored files (__pycache__/,docs/website/build/,.docusaurus/) were still present. The main checkout'starget/andnode_moduleswere untouched.The fix
A new
--prune-build-dirsoption runs after the branch cleanup. For each linked worktree still in place, merged or not, it asks Git for wholly ignored directories (git ls-files --others --ignored --exclude-standard --directory) and deletes those namedtarget,node_modulesordist. Git lists a directory that holds a tracked file, or a symlink, differently, so neither is deleted. Other ignored files, such as a local.env, are never deleted. The main checkout, the checkout the script runs from, and locked worktrees are skipped and reported. The dry run reports each directory and its size. The skill tells the agent to warn the person that the next build in those worktrees starts from nothing and that a build running in one of them will fail.Test plan
python3 agents/skills/clean-branches-and-worktrees/selftest.py- passed, 26 testsmake check-clean-branches-selftest- passedpython3 contrib/check-all-selftest.py- passed--prune-build-dirs, on a working clone of this repository - 11 build directories (32.8 GiB) deleted, matching the dry runWhich regressions the tests reject
Each test drives the script against real Git repositories with a fake
ghonPATH. Each guard below was removed from the script in turn, and the named test was observed failing:test_keeps_ignored_files_that_are_not_build_directoriesfails: an ignoredcache/directory is deleted.test_keeps_build_directories_of_the_main_checkout_and_the_running_worktreefails.test_does_not_count_build_directories_of_a_worktree_it_removesfails: the dry run counts directories in a worktree it is about to remove.test_keeps_build_directories_of_a_locked_worktreefails.test_dry_run_reports_build_directories_and_deletes_nonefails.test_keeps_build_directories_without_the_flagfails.test_keeps_a_build_directory_that_holds_a_tracked_fileandtest_keeps_what_a_build_directory_symlink_points_topin behaviour that comes from how Git lists these entries. No guard in the script can be removed to make them fail. The other new tests cover a dirty kept worktree with a nestednode_modulesand a detached worktree.Not tested: Windows, and a deletion that fails partway (the script reports the path and continues, but no test forces the failure).
User facing changes
None for people using bravebot. Contributors can pass
--prune-build-dirsto theclean-branches-and-worktreesskill to reclaim disk space from the worktrees it keeps.