Skip to content

feat(hooks): remove merged task worktrees and branches at session start - #185

Merged
furedea merged 2 commits into
mainfrom
feat/cleanup-merged-worktrees
Oct 17, 2026
Merged

furedea merged 2 commits into
mainfrom
feat/cleanup-merged-worktrees

Conversation

@furedea-agent

@furedea-agent furedea-agent Bot commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

PR 2 of #181. Merged task worktrees and local branches are removed without a person running git branch -D.

  • agents/hooks/cleanup_worktrees.py. A branch goes only when both hold:

    • a merged pull request's head is exactly its tip;
    • its worktree is clean.

    Squash merges therefore count as merged, while later or uncommitted work stays. The primary checkout, the caller's own worktree, and main/master are never touched. An open Herdr workspace is closed with its checkout (herdr worktree remove), and other worktrees use git worktree remove.

  • Scope. It sweeps the caller's repository and every repository with a Herdr task worktree under ~/.herdr/worktrees. A lane merged in one repository therefore goes at whichever session starts next. A failure in one repository skips only that repository.

  • Triggers.

    • The Claude SessionStart hook runs it asynchronously, so start-up never waits on GitHub. Hooks run outside the sandbox, so agents still never get -D.
    • cleanup_worktrees.py --dry-run reports what would go.
    • It uses only git, gh (one gh pr list --state merged call), and Herdr. No gh-poi.
  • branches.md. Tells agents not to delete merged worktrees by hand.

Remote branches: delete_branch_on_merge is already on for the active repositories. It is off for obsidian (no PRs), book-agent, swe-bench-trial, revisiting-pgd-evaluation, pairs, and older learning repos. The agent App token cannot change repository settings, so that is left to the user.

Closes #181

Verification

  • uv run --frozen pytest tests: 1498 passed. The 3 errors in tests/hermes/test_hermes_gateway.py also occur on main.
  • The new contracts use temporary repositories with fake gh and Herdr. They cover:
    • a merged worktree;
    • a merged branch without a worktree;
    • a later commit, a dirty tree, and an open PR, all kept;
    • the primary and current checkouts, kept;
    • --dry-run;
    • a Herdr workspace closed with its worktree;
    • the hook never blocking;
    • a merged Herdr lane of another repository swept from an unrelated directory.
  • A dry run on this repository found exactly the two squash-merged branches from docs(skills): write Issues and plans in Japanese with diagrams #183 and feat(agents): hand approved lanes to Herdr task worktree sessions #184.
  • The Herdr hand-off from PR 1 was checked live: worktree, agent start, and prompt ran with no permission prompt.
  • ruff, ty, deptry, vulture, and lefthook passed.

🤖 Generated with Claude Code

furedea and others added 2 commits October 17, 2026 19:36
A branch goes only when a merged pull request's head is its tip and its
worktree is clean, so squash merges count while later or uncommitted
work stays. The primary checkout, the caller's worktree, and main are
never touched. Open Herdr workspaces close with their checkout. The
Claude SessionStart hook runs it asynchronously; --dry-run reports.

Refs #181

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Cleanup now covers the caller's repository and each repository under
~/.herdr/worktrees, so a lane merged elsewhere goes at whichever session
starts next. A failure in one repository skips only that repository.

Refs #181

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@furedea
furedea merged commit a27db91 into main Oct 17, 2026
11 checks passed
@furedea
furedea deleted the feat/cleanup-merged-worktrees branch October 17, 2026 10:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

人が判断する 2 か所以外の受け渡しを自動にする

1 participant