Agents merge their own pull requests
A rule in the prompt is a wish. A hook that won't let the session stop is a rule.
The problem
The project’s CLAUDE.md said: once CI is green, squash-merge. Sessions still stopped with PRs open, and each one had to be told to merge, or to fix a conflict, by hand. With several sessions running in parallel, I became the merge button.
The decision
Two hooks in .claude/settings.json:
- After
gh pr create(not a draft): rungh pr merge --auto --squash. GitHub merges once the requiredcheckpasses. - On Stop: look at the branch’s open PR. If it conflicts with
main, has a failing check or has no auto-merge set, send the session back with what to do: sync and resolve, read the failed run and fix, or merge. Once per stop, so it never loops.
The rule moved from text the agent may skip to a gate it cannot pass.
The same day, a second rule
An agent handed me the commands to move some data instead of running them. One had a placeholder, <project-slug>. I pasted it as is, and zsh read < as a redirection.
So a session now runs what its change needs: the data script (dry run first, then --apply), the release, the check that it answers. It hands over only what a permission refuses, and names the permission that would let it through.
What broke
Six weeks later a session in this repository opened a PR in another one, where the owner was meant to review it. The hook armed auto-merge on any PR URL gh pr create printed. Only --draft saved it: that repository’s main had no protection.
The fix: the hook compares the PR’s owner and name with the checkout’s origin and leaves anything else alone, saying why.
Result
1,152 PRs merged in 55 days, about 21 a day, and nobody presses merge. Only a draft waits for a person.
If the agent must always do it, don’t ask. Make the session unable to end without it.