← Back to Writing

Lesson 9: Test Your Git Hooks, CI Checks, and Merge Rules

About this artifact

essayon-the-recordmaintained

method · since 2026

What you'll learn: test a pre-push hook, required CI check, and merge rules with a deliberate violation; inspect the results and verify cleanup. What you need: the runner and workflow from Lesson 7, working alerts from Lesson 8, and Lesson 3's checks/ in a disposable GitHub test repository. Use credentials authorized for that test repository. Time: about twenty minutes to configure, then a monthly result review. Cost: CI usage for each drill and notification-service usage; use your existing allowances or account for those charges.

The idea#

A deliberate invalid change tests whether configured controls actually reject it. A hook path, workflow, or branch rule can change while the check script stays unchanged. This drill submits a dummy .env, verifies rejection by the pre-push hook and CI, then attempts to merge the failed pull request.

The result is limited to that violation, execution path, user permissions, and configuration at the recorded time. It does not establish that other checks work or that every invalid change will be rejected. Record a date and result for each control you test; leave untested controls marked unverified.

Controls from Lesson 7 with a column for dated test evidence; the hook, CI, and merge-rule rows identify the operations exercised by this drill.

Why start here#

You now have checks to test and a notification channel for the result. Testing the integrations can reveal failures that a direct invocation of the check script would miss.

The template#

Save one drill script in gate/, add a stale-drill check to the watch, and schedule it monthly.

This script attempts real merges, including --admin. If protection is missing or bypassable, it can merge the dummy .env into main and trigger a deployment. Its local temporary copy is not a sandbox for remote actions. Use a disposable test repository and understand its deployment behavior before running it. A successful merge requires human repair; the script does not revert it.

#!/bin/sh
# gate/drill.sh <home>: break Lesson 3's check on purpose and watch each rung refuse.
# In a throwaway copy, plant a .env in <home> and push it: the hook must refuse. Push
# past the hook with --no-verify and open a pull request: the pipeline must fail it.
# Try to merge, then try again as an administrator: the platform must refuse both.
# Then remove what the drill made and confirm main did not move. One dated message
# through Lesson 8's notify either way.
set -u
cd "$(dirname "$0")/.."
home=${1:-web}; when=$(date -u +%Y%m%d-%H%M%S); branch=drill-$when
here=$(pwd); copy=$(mktemp -d); pr=; done=
log() { printf '%s %s\n' "$(date -u +%FT%TZ)" "drill $when: $*" >> "$here/gate/drills.log"; }
restore() {
  [ -z "$pr" ] || gh pr close "$pr" --delete-branch >/dev/null 2>&1
  git push -q --no-verify origin --delete "$branch" >/dev/null 2>&1
  cd "$here" && rm -rf "$copy"
}
fail() {   # a barrier that did not refuse, or a restore that did not hold
  log "FAILED: $1"; restore; done=1
  git -C "$home" ls-remote --exit-code origin "refs/heads/$branch" >/dev/null 2>&1 \
    && set -- "$1; $branch is still on origin"
  sh gate/notify.sh blocked "drill $when: $1"; exit 1
}
tries=0                                     # Lesson 8's lock, so a tick cannot merge mid-drill
until mkdir gate/running 2>/dev/null; do
  tries=$((tries + 1)); [ "$tries" -lt 20 ] || fail "a run held the lock for ten minutes"; sleep 30
done
trap '[ -n "$done" ] || restore; rmdir "$here/gate/running"' EXIT   # a killed drill still cleans up
for h in */; do                             # the runner's layout, so every check finds its neighbours
  h=${h%/}; url=$(git -C "$h" remote get-url origin 2>/dev/null) || continue
  git clone -q "$url" "$copy/$h" && git -C "$copy/$h" config core.hooksPath hooks || fail "could not clone $h"
done
cd "$copy/$home" || fail "no copy of $home"
main=$(git rev-parse origin/main)

# 1. The hook (skippable): the push must stop at checks/no-tracked-env.sh.
git checkout -q -b "$branch"
echo "DRILL=1" > .env && git add -f .env && git commit -q -m "drill $when: a .env the check must refuse"
out=$(git push -u origin "$branch" 2>&1) && fail "the hook let a .env through"
case $out in
  *"BLOCKED by checks/no-tracked-env.sh"*) log "the hook refused" ;;
  *) fail "the push stopped for another reason: $(printf '%s\n' "$out" | tail -n 1)" ;;
esac

# 2. The pipeline (enforced): past the hook with --no-verify, the checks status must go red.
git push -q --no-verify -u origin "$branch" || fail "could not push past the hook"
pr=$(gh pr create --fill --head "$branch" 2>/dev/null) || fail "could not open the pull request"
tries=0
while gh pr checks "$pr" --required > checks.out 2>&1 || grep -q -e pending -e "no .*checks reported" checks.out; do
  grep -q pending checks.out || grep -q "no .*checks reported" checks.out || break
  tries=$((tries + 1)); [ "$tries" -lt 60 ] || fail "the pipeline never finished"; sleep 10
done
grep -qE '^checks[[:space:]]+fail' checks.out || fail "the checks status did not fail: $(head -n 1 checks.out)"
log "the pipeline refused: $(grep -E '^checks[[:space:]]+fail' checks.out | head -n 1)"

# 3. The merge (enforced): the platform must refuse while the check is red, administrator or not.
for flag in "" --admin; do
  gh pr merge "$pr" --squash $flag > merge.out 2>&1 && fail "the merge went through${flag:+ with $flag}; revert it with a pull request"
  grep -qi -e "not mergeable" -e "status check" merge.out || fail "the merge failed for another reason: $(head -n 1 merge.out)"
  log "the merge was refused${flag:+ with $flag}: $(head -n 1 merge.out)"
done

# 4. Restore, and confirm it: no pull request, no branch, main where it was, no .env on it.
gh pr close "$pr" --delete-branch >/dev/null 2>&1 || fail "could not close the pull request"
[ "$(gh pr view "$pr" --json state --jq .state)" = CLOSED ] || fail "the pull request is still open"
git ls-remote --exit-code origin "refs/heads/$branch" >/dev/null 2>&1; code=$?
[ "$code" -eq 2 ] || { [ "$code" -eq 0 ] && fail "the drill branch is still on origin"; fail "could not read origin"; }
git fetch -q origin main || fail "could not fetch main"
[ "$(git rev-parse origin/main)" = "$main" ] || fail "main moved during the drill"
git ls-tree -r --name-only origin/main > tree.out || fail "could not read main's tree"
! grep -qE '(^|/)\.env$' tree.out || fail ".env is on main"
cd "$here" && rm -rf "$copy"; done=1
date +%s > gate/last-drill.new && mv gate/last-drill.new gate/last-drill
short=$(git -C "$home" rev-parse --short "$main")
log "restored; main at $short"
sh gate/notify.sh review "drill $when: hook, pipeline, and merge each refused a planted .env in $home; restored, main at $short"

Append this check to gate/watch.sh. It reports when the last successful drill is more than 35 days old:

read -r drill < gate/last-drill 2>/dev/null || drill=0        # Lesson 9: a drill that stopped running
[ $(( $(date +%s) - drill )) -le $(( 35 * 86400 )) ] \
  || sh gate/notify.sh blocked "no drill in $(( ( $(date +%s) - drill ) / 86400 )) days"

Add this cron entry beside Lesson 8's entries, replacing the project path:

0 9 1 * *    cd /home/you/project && sh gate/drill.sh web  >> gate/timer.log 2>&1

Each part, and the mistake it prevents#

The drill first expects the local push to fail with BLOCKED by checks/no-tracked-env.sh. It then bypasses that hook with --no-verify so it can test CI separately. The required checks status must fail. Finally, normal and administrator merge attempts must fail with recognizable policy messages. An authentication or network error is not evidence that the intended rule rejected the change.

The temporary copies preserve the neighboring-repository layout expected by the checks. The shared directory lock prevents this runner from starting work during the drill. Other machines and people can still change the remote repository; the lock does not exclude them.

On the successful path, the script confirms that the pull request is closed, its branch is deleted, main has not moved, and main contains no .env. Error paths attempt cleanup but do not establish all those conditions. Inspect remote state after any failure or interruption. A merged violation needs a corrective pull request and inspection of any deployment it triggered.

Successful drills send a dated review message so you can inspect the evidence. Failures send blocked. These use Lesson 8's delivery mechanism and inherit its limitations; a log entry does not confirm receipt on your device. Its same-machine watch also cannot report complete host failure.

Document a failure in a dated amendment to the relevant spec or decision record: expected rejection, observed result, and corrective change. Preserve the earlier decision and its evidence. Add an automated assertion where it can detect the specific defect, then repeat that bounded test after the repair.

A worked example#

In Lesson 7's scratch project, I saved the drill and extended the watch. I ran sh gate/drill.sh web manually; the cron entry remained untested. Thirty-six seconds later, gate/drills.log contained:

2026-10-14T18:09:33Z drill 20261014-180930: the hook refused
2026-10-14T18:10:00Z drill 20261014-180930: the pipeline refused: checks  fail  8s  https://github.com/.../actions/runs/...  
2026-10-14T18:10:01Z drill 20261014-180930: the merge was refused: X Pull request ...#16 is not mergeable: the base branch policy prohibits the merge.
2026-10-14T18:10:03Z drill 20261014-180930: the merge was refused with --admin: GraphQL: Required status check "checks" is failing. (mergePullRequest)
2026-10-14T18:10:09Z drill 20261014-180930: restored; main at aae4bb8

The topic received one review message ending in restored, main at aae4bb8. I then set gate/last-drill back forty days and ran the watch, which reported blocked no drill in 40 days. This receipt covers the manual run shown and the timestamp test, not future scheduled runs.

How it scales#

Choose another control only after defining its expected failure and cleanup. A command restriction or a deployment check needs its own test; this .env drill does not cover it. Retain the tested commit, command, rejection evidence, and restoration result.

Keep usage evidence separate from rejection tests. My first capability inventory had 114 rows and no usage evidence recorded. After connecting usage signals, about two-thirds showed use. A successful drill establishes rejection of its test case; it does not establish that a feature is used. External monitoring, deployment approval, and multiple concurrent agents require additional design.

From the field#

After a machine migration in July, my pre-commit checks appeared to pass for sixteen days. Their commands pointed at the previous user profile's missing directory, and that hook configuration did not block when a command failed to start. An audit found the broken paths. I corrected them and added a weekly job that submits a known violation to each check. The drill here applies that method to one check and its GitHub integrations.

Exercise#

  1. Confirm the remote is a disposable test repository. Inspect its merge rules, bypass permissions, and deployment behavior. Save the drill and extend the watch.
  2. Run sh gate/drill.sh web manually. Read gate/drills.log and confirm the notification arrives.
  3. Open the test pull request and inspect the failed check. Confirm its closure, branch deletion, and unchanged main commit directly on the remote.
  4. Save gate/last-drill, set it back forty days, and run the watch. Confirm the blocked alert, then restore the saved value.
  5. Add the cron entry only after the manual test and cleanup succeed. Record the first scheduled result separately.
  6. Date the controls actually exercised and mark the remaining rows unverified.

You're done when#

The intended hook, CI status, and both merge attempts rejected the dummy violation; the corresponding remote evidence agrees with the log; cleanup is confirmed; and the result arrived on your device. Record failures and unverified steps explicitly. These observations establish only this drill's tested controls and path at that time, not universal repository safety.

Common mistakes#

  • Counting an unrelated command failure as the expected rejection.
  • Assuming a disposable local copy protects the remote from a real merge.
  • Disabling protection to make the test finish.
  • Assuming error-path cleanup succeeded without checking remote state.

Further reading#

The Principles of Chaos Engineering informed testing through actual execution paths. MIT's 6.S191 Introduction to Deep Learning reinforcement learning lecture informed the distinction between agent decisions and environment controls. GitHub's protected branches documentation covers administrator enforcement and bypass configuration.

After this lesson#

I still see regressions that pass existing checks. When one occurs, identify the missing assertion or untested integration and add a focused regression check. Repeat relevant checks after changes to the agent, tools, repository rules, or deployment process. Preserve prior results so you can distinguish what was tested from what has changed since.

Questions this post answers

How do I test that the checks used by an AI agent still run?
Submit a deliberate violation through the operations you need to test. This drill checks a local pre-push hook, a required CI status, and normal and administrator merge attempts. Record the rejection, tested commit, and cleanup. The result applies only to the tested violation, path, permissions, and configuration at that time.
Why does the drill have to run through the real path instead of calling the check script?
Calling a check directly does not establish that Git invokes it, CI requires it, or repository rules block a merge. Those integrations can change independently. The drill exercises them with a dummy .env file, but does not cover every command form or repository rule.
What happens when a drill fails?
It attempts cleanup and sends a blocked alert. Inspect the remote repository and logs yourself. If a merge succeeds, the dummy violation is on main and requires human repair. Record the expected rejection, actual result, and corrective change in a dated amendment to the relevant spec or decision record.
Can the drill change the remote main branch?
Yes. It calls gh pr merge normally and with --admin. Missing or bypassable protection can let the dummy .env merge. A disposable local clone does not isolate the remote repository or deployment. Use a disposable test repository; cleanup does not automatically undo a successful merge.