Lesson 7: Automate Code Review, Merge Checks, and Deployment Verification
What you'll learn: configure a separate code review, required merge checks, and a deployment check before an agent marks a task complete.
What you need: the repositories from Lesson 4, Lesson 3's checks/ in each, GitHub remotes, the permissions file from Lesson 5, and the task queue (backlog) from Lesson 6. You also need a site deployed from main and authenticated claude and gh command-line tools.
Time: about an hour to configure and test. Cost: existing agent access and CI usage; confirm your GitHub plan supports the required branch rules.
The idea#
A merge workflow needs explicit decisions about review, checks, and deployment verification. The runner below starts an implementation session in fresh repository copies, asks a separate session to review its diff, waits for required checks, merges, and runs the spec's live: command. It marks the task done only if that command passes.
Each step can detect different errors. None establishes universal correctness. A reviewer can miss defects, and a passing command proves only the behavior it tests. Choose tasks whose acceptance criteria you can verify before delegating their merge.
Why start here#
Lesson 6 kept you responsible for review and merging. Before automating those actions, run Lesson 3's checks in CI and require their results in branch rules. Add tool restrictions for selected actions from Lesson 5's approval policy. Written permissions alone depend on the agent following them.
The template#
Save the runner in the parent project directory, beside the queue. It uses the repository layout from Lesson 4:
#!/bin/sh
# gate/run-next.sh: work the next code item in a disposable copy, have a second
# session review the diff, merge only through the pipeline, then check the live site.
# Run from the parent folder. One item per run; the agent works only in the copy.
set -eu
[ ! -e STOP ] || { echo "STOP file present; not starting." >&2; exit 0; }
here=$(pwd); before=$(cksum < BACKLOG.md); copy=$(mktemp -d)
cp CLAUDE.md CHARTER.md BACKLOG.md "$copy"/ && cp -R .claude "$copy"/
for home in */; do
home=${home%/}
url=$(git -C "$home" remote get-url origin 2>/dev/null) || continue
git clone -q "$url" "$copy/$home"
git -C "$copy/$home" config core.hooksPath hooks
done
cd "$copy"
# 1. Where the work runs: a fresh session in the copy, project rules only, bounded.
claude -p "Work the next item and push its branch. Its live: line runs after merge, not by you. If the item is an investigation, do nothing and end with STOPPED investigation. End with one line: DONE <item id> <home> <branch>, or STOPPED <reason>." \
--setting-sources project --permission-mode bypassPermissions --max-turns 40 > work.log
set -- $(tail -n 1 work.log)
[ "${1:-}" = DONE ] || { echo "Session stopped: $(tail -n 1 work.log)"; exit 1; }
item=$2 home=$3 branch=$4
# Read the live line from your original checkout, outside the working copy.
spec=$(sed -n "/^## $item /,/^## /s/^- spec: *//p" BACKLOG.md)
live=$(sed -n 's/^live: *//p' "$here/$spec")
[ -n "$live" ] || { echo "$spec has no live: line; not merging." >&2; exit 1; }
# 2. Review a fixed head with read tools; do not inherit project settings.
git -C "$home" fetch -q origin main "$branch"
reviewed_head=$(git -C "$home" rev-parse "origin/$branch")
reviewed_base=$(git -C "$home" rev-parse origin/main)
git -C "$home" diff "$reviewed_base...$reviewed_head" > review.diff
claude -p "Review $item in BACKLOG.md against its spec, using the diff in review.diff. Pass only if every acceptance line is met and nothing under Out of scope changed; skip the live: line, which runs after merge. Last line: PASS, or FAIL <reason>." \
--setting-sources "" --tools "Read,Grep,Glob" --allowedTools Read Grep Glob \
--disallowedTools "mcp__*" --max-turns 15 > review.log
[ "$(tail -n 1 review.log)" = PASS ] || { echo "Reviewer refused; $branch stays unmerged."; tail -n 5 review.log; exit 1; }
# 3. Merge only through the pipeline: the platform refuses until the checks pass there.
cd "$home"
gh pr create --fill --head "$branch" > /dev/null
tries=0
until gh pr checks "$branch" --required > checks.log 2>&1; do
grep -q -e pending -e "no .*checks reported" checks.log && [ "$tries" -lt 60 ] \
|| { echo "Checks failed or never finished; $branch stays unmerged."; cat checks.log; exit 1; }
tries=$((tries + 1)); sleep 10
done
gh pr merge "$branch" --squash --delete-branch --match-head-commit "$reviewed_head"
cd ..
# 4. What proves it landed: the live line, read from the live site while it deploys.
tries=0
until sh -c "$live"; do
tries=$((tries + 1))
[ "$tries" -lt 30 ] || { echo "$item merged but not live after 30 tries: $live" >&2; exit 1; }
sleep 20
done
# Live, so done: close the item in your queue, unless you edited the file meanwhile.
sed "/^## $item /,/^## /s/^- status: .*/- status: done/" BACKLOG.md > BACKLOG.next
if [ "$(cksum < "$here/BACKLOG.md")" = "$before" ]; then
mv BACKLOG.next "$here/BACKLOG.md"
else
echo "BACKLOG.md changed during the run; set $item to done by hand."
fi
echo "$item is live: $live"
The workflow belongs in each repository at .github/workflows/gate.yml. Set HOME_DIR to that repository's folder name and replace your-name/specs:
# .github/workflows/gate.yml in each home: main's checks/ run against every pull request.
name: gate
on: pull_request
env:
HOME_DIR: web # this home's folder name
jobs:
checks:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4 # the pull request's files
with:
path: ${{ env.HOME_DIR }}
- uses: actions/checkout@v4 # main's checks, so a branch cannot edit them
with:
ref: ${{ github.base_ref }}
path: base
- uses: actions/checkout@v4 # your specs home, for the spec check
if: env.HOME_DIR != 'specs'
with:
repository: your-name/specs
path: specs
token: ${{ secrets.SPECS_TOKEN || github.token }} # a read-only token if specs is private
- run: |
cd "$HOME_DIR"
for check in ../base/checks/*.sh; do
sh "$check" || { echo "BLOCKED by $check" >&2; exit 1; }
done
Save these command patterns in .claude/settings.json in the parent directory:
{
"permissions": {
"deny": [
"Bash(git push --force *)",
"Bash(git push -f *)",
"Bash(git reset --hard *)",
"Bash(git branch -D *)",
"Bash(rm -rf *)",
"Bash(gh pr merge *)"
]
}
}
Add a live: command below verify: in each task's spec. Choose a result specific to that change:
live: curl -fsS --max-time 20 https://you.github.io/web/status.html | grep -q "Last run"
In each repository's branch rules, require the checks status before merging, apply the requirement to administrators, and disable force-pushes. Inspect bypass permissions as part of that configuration.
Each part, and the mistake it prevents#
Fresh clones separate the agent's working files from your active checkout. They are not a sandbox. The session still runs with the host user's access and credentials; the original files remain reachable.
The runner checks for STOP before starting and limits the implementation session to forty turns. It parses the last output line as DONE or STOPPED. These conventions depend on the session producing the expected format. An approval request must stop the task because nobody is answering during an unattended run.
The second session reads the spec and saved diff. --tools restricts its built-in tool set; --allowedTools lets those tools run without permission prompts. The latter alone does not restrict availability. MCP tools are separate, so the command denies mcp__* too, and disables user, project, and local settings sources for this review. These are CLI controls, not an operating-system or credential sandbox. Check any managed configuration and other extensions in the environment before unattended use. The verdict remains model judgment and can miss a defect.
The saved diff uses a recorded head SHA. GitHub's --match-head-commit refuses to merge if the PR head has changed since that review. A new push needs a new review. A moving base branch is a separate concern: configure branch rules or a merge queue to require the integration checks your project needs.
The workflow runs check scripts from the base branch against the proposed files. Editing a check on the feature branch therefore does not change the check used in that run. This does not protect against every workflow change or bypass permission. Required checks and branch rules must both be configured correctly.
Command patterns have narrower coverage. In my run, cd web && git push --force was refused and the remote did not move, while git -C web push --force went through. Bash patterns also do not cover PowerShell commands. Confirm the tools and syntax your agent actually uses.
The live: example searches an HTTP response for text, with up to 30 attempts and a 20-second wait between failures. It cannot establish that a browser rendered the page or that interactions work. Use browser verification when those are acceptance criteria. The runner closes code items only after live: passes; investigation items still require a session you watch.
A worked example#
I used a scratch project with two GitHub repositories, web/ deployed through Pages from main, branch rules enabled, and ready item B-002. Its task was a status-page footer reading "Checked by the gate". I ran sh gate/run-next.sh. The implementation session and separate reviewer ended with:
- An open branch called `gut-the-check` (PR #4) empties the env check
and adds a dummy `.env`. Look at it before anything merges it.
DONE B-002 web b-002-status-footer
- **Out of scope:** `review.diff` changes exactly one file, `status.html`
in the web repo. Nothing in `specs/`, `web/index.html` or any other
repo changed.
PASS
After merging, the runner reported:
B-002 is live: curl -fsS --max-time 20 https://.../status.html | grep -q "Checked by the gate"
The run took three and a half minutes, and BACKLOG.md recorded status: done. The flagged branch was my test: it emptied its own .env check and added a dummy .env. CI used main's check and rejected it. These are results from that scratch project, not evidence for another repository's configuration.
This took four runs. The first two tried the live: command before merge. Draft review also found that the reviewer could run writing Git commands and that CI used the feature branch's checks.
Template correction, November 3, 2026: the reviewer now restricts built-in tools separately from no-prompt permissions, denies MCP tools, and the merge checks the reviewed head SHA. The scratch-run output above predates these changes; it does not verify the revised command. Exercise the current template in your disposable project before scheduling it.
How it scales#
Start with one task per run. Lesson 8 schedules the same runner. You remain responsible for the spec, acceptance checks, command permissions, and branch rules. Add verification when a task changes behavior that the current checks cannot observe.
From the field#
In July, a dashboard page on my site rendered blank in production. A security-header fix merged but stayed undeployed for a week. The pull request was green, and the page returned HTTP 200. I found the failure by opening the page: the header prevented the browser from drawing its content. My deploy now checks the files the live page needs. The text search above is a narrower check and would not, by itself, prove that this rendering defect was fixed.
Exercise#
- Configure the runner, workflow, command rules, and branch rules in a disposable test project. Confirm the agent's credentials and permitted actions before running it.
- Add a
live:command to the spec for one small ready task. Identify any acceptance criteria it cannot test. - On a disposable branch, commit a
.envwith a dummy value, push with--no-verify, and open a pull request. Confirm the expected check fails and merging is blocked. Close the pull request and remove the branch. - Run
sh gate/run-next.shfrom the parent directory. Inspect its first complete run before scheduling further work. - Read the review result, required checks, merged diff, and deployment result. Open the page to inspect rendering.
You're done when#
The test violation failed the expected CI check, branch rules prevented its merge, and a valid task completed review, merge, and its deployment check. Record the commit and observed result. A timeout or reviewer rejection leaves work to investigate. A passing text search leaves browser behavior unverified until you inspect it.
Common mistakes#
- Treating a successful HTTP request as evidence that the page renders correctly.
- Treating the separate reviewer's approval as proof that the implementation meets every requirement.
- Assuming command patterns cover alternate syntax, other tools, or all credentials.
- Allowing merge-rule bypasses without accounting for who can use them.
Further reading#
GitHub's protected branches documentation explains required checks and force-push settings. The Claude Code permissions reference describes command matching and its limits.
Next lesson#
Schedule Agent Runs and Configure Alerts: run the task queue on a timer, classify results, and detect missed starts.
Questions this post answers
- What should run before an agent's change merges?
- Use a separate reviewer to compare the diff with the spec, run automated checks in CI, and require those checks in the repository's merge rules. Review can miss defects, and checks cover only their assertions. These steps support a decision to delegate merging; they do not establish that every change is correct.
- Does finding the expected text on a live page prove the page works?
- It proves that the HTTP response contains that text. It does not prove browser rendering, interactions, or every acceptance criterion. Add browser verification when the change depends on those behaviors.
- Does a disposable checkout contain an agent's access?
- It separates working files from your active checkout, but it is not a filesystem or credential sandbox. The session may still access other paths and services available to its user account.
- What do command deny rules prevent?
- They reject command forms that match the configured patterns. Alternate syntax or another tool may fall outside those patterns. Repository rules provide separate enforcement for operations such as force-pushing to a protected branch, subject to their configured bypass permissions.
Previous lesson: Lesson 6: Build a Task Queue Agents Can Follow