Letting AI agents merge their own pull requests, safely
In our repositories AI agents open and merge their own pull requests, but only through green CI, only from their own branches, never by force. The guard rails that make that safe, and why asking a human to click merge adds delay, not safety.
- Our AI agents merge their own pull requests. No human click is needed for routine work.
- Guard rails: own branches only, every check green on the exact tested commit, protected main branch, no force pushes, protected CI files.
- The agent that opened the pull request fixes its own red checks.
- Humans approve the rules, the unusual cases and stable releases, not every merge.
The first instinct with AI-written code is to put a person in front of every merge. In practice that person becomes a rubber stamp: dozens of small, green pull requests a day, each waiting hours for a click that adds nothing the checks have not already verified. So we turned it around: agents merge their own work, and we invested the human attention in making the checks trustworthy.
The guard rails
- Own branches only. Agents can write only to branches with known prefixes (
agent/…,ticket-…,wip/…). The main branch accepts changes only through a pull request. - Green on the exact commit. The merge call carries the commit id the agent tested. If anyone pushed after that, the merge is refused. Every check run must be finished and green, and at least one must exist.
- Branch protection is never bypassed. If the repository requires a human review, the merge waits for it. Nothing in the agent's tooling can switch that off.
- No force, no rebase on shared history. History is append-only.
- The checks are protected. CI workflow files and deployment configuration can change only after the agent has announced the change to everyone on the agents' message board, naming the repository and each file. An agent that could quietly edit the checks could quietly disable them.
- Every merge is announced. Other agents learn about it on the board, so nobody builds on a stale picture.
What CI checks
The checks are the real reviewer, so they have to cover what matters: unit tests, smoke tests of the deployed shape, link and language guards on the website, a guard that members-only content never appears in public files, secret scanning, and every regression test from past bugs (see every bug becomes a test). A check that cannot fail is removed, not kept for comfort.
In depth: how the work is shaped
- One scoped task per pull request. Small pull requests are easy to verify and easy to revert.
- Rebase before merge. When several agents work in one repository, each brings its branch up to date and re-runs CI before merging, so two green branches do not combine into a red main.
- Fix your own red. The agent that opened the pull request owns its failures. Waiting for someone else is not an option.
- Test before merge, not after. Security gates must still let an agent build and test its branch. A rule that forces untested code into main "so CI can run there" is worse than no rule.
What the human still does
The owner approves rules ("agents merge on green", "workflow changes need an announcement"), decides unusual cases and gives the final yes for stable releases (alpha, beta, stable). He is never sent to click merge, start a crawl or edit a token by hand. If a human step is truly unavoidable, it is prepared to one click.
Try it
Protect your main branch, write the checks you would trust with your own eyes closed, and let your agent merge only when all of them are green. If Git is new, start with GitHub for beginners; the broader setup is in agentic development with Claude.
Frequently asked questions
Is it safe to let an AI agent merge code?
It is as safe as your checks. If merging requires every CI check to be green on exactly the commit that was tested, the main branch is protected and force pushes are impossible, an agent's merge is no riskier than a person's click on the same green button.
What should an agent never be able to change?
The checks themselves. CI workflow files and deployment configuration are protected; a change there needs an announcement and, for sensitive cases, a human.
Why not keep a human reviewer for every pull request?
For high-volume routine changes a human click adds hours of delay and very little scrutiny. Human attention is better spent on rules, unusual cases and releases.
What happens when CI is red?
Nothing merges. The agent that opened the pull request fixes its own red checks; it does not wait for someone else to do it.
Written from our own production systems by the Flowsmith editorial team, with AI assistance.
Want this running in your company?
Flowsmith designs and operates automations like this for small and mid-sized teams — with a person approving anything that leaves the building.