transition2.aiStart a project

Software Factories & AI-Assisted Engineering

Delivery Gates for Agent Code

Three gates between tested and delivered: publish only feature branches, integrate through a journaled scratch merge, and merge only when every check on the exact head is green.

Eric MacDougall ·

Answer first

Put three gates between "the tests passed" and "it is delivered". First, a publish gate that only pushes feature branches, never forces, and refuses a branch whose head moved after verification. Second, an integration step that merges in a separate scratch worktree, runs the checks there, and only then moves the real checkout, with a lock and a journal so an interruption leaves evidence instead of a half-merged tree. Third, a merge gate that blocks any merge command unless every required check and every reported check on the exact head concluded success, and the command pins that head. Each gate is designed, per the source, to refuse when it cannot decide.

Gate 1: publish a feature branch, nothing else

In the open-source agent harness, the publisher refuses a protected or default branch, a branch that is not checked out, a source head that changed since final verification, and a dirty tree. It never forces. It pushes from an isolated transport so hooks in the source worktree are not run. A delivery helper then prepares a draft pull request, and in remote mode its finish step only completes after the pull request is merged; until then it reports that the pull request is ready. No command merges a remote pull request on its own. These behaviours were read from the source; the lab ran the publisher and the helper against a local bare remote, with no network and no GitHub.

Two supporting rules:

  • Identity is parsed, not trusted. A function accepts a repository only as an https or ssh GitHub address on default ports, lower-cases owner and name, and refuses embedded logins, other hosts, ports, query strings and local paths. It never contacts the remote.
  • Private inputs must be private. A kit file or pull request body that is not an owner-only regular file is refused.

An explicit local-only mode exists for when the remote is unavailable. It is a separate, deliberate choice, and local delivery leaves publication outstanding.

Gate 2: integrate in a scratch copy, with a journal

The local integration step merges the feature branch into a detached scratch worktree (git-worktree), runs the approved check commands there, and only then fast-forwards the original checkout to the tested merge commit. The original tip is therefore a merge commit, not the feature head. Before that last step it re-checks that the original is clean, on the expected branch and at the expected head.

How it survives interruptions:

  • A lock per checkout and branch. A second integration is refused with a blocked result before it writes anything. Operating-system file locks are released when the holder exits (flock); the lab relied on that documented behaviour and did not test it.
  • A journal written first. Per the source, it is synced to disk and put in place atomically before scratch work starts. Its key is a hash over the checkout, branch, base, source branch and source head.
  • Unresolved means blocked. After a failed check or an interruption, new work on that checkout and branch is refused until someone inspects it and resolves the journal. Resolving re-checks that the original is clean at the expected head, runs the checks on it, and retires the journal to an archive with artifacts retained. It never claims delivery.

Gate 3: the merge gate

Everything in this section is read from the source and documentation: the lab drove the gate with a fake gh and did not run the waiver approval stamp, the tamper check or the time budget. The merge gate is a hook that sees merge commands. It blocks unless every required check and every reported check run and commit status on the exact head concluded success. The required set is the union of branch protection, rulesets and the list on the pull request itself, and it must be known (about protected branches). Skipped and neutral results are accepted only for checks that are not required, and unknown conclusions block. The merge command must carry the verified head (--match-head-commit, see the gh pr merge manual), the head is read twice, and a move between reads blocks. A merge that cannot be tied to one pull request, such as a target built at run time, is blocked and no waiver can attach.

A waiver exists for a red check, but it needs an approver, is tied to repository, pull request and head, is single use, expires in 24 hours, and goes into a hash-chained log. The chain is tamper-evident, not authenticated; it assumes same-user trust. Anything the gate cannot determine blocks.

What we ran

From our lesson labs: Python 3.12.3, standard library only, a throwaway home directory, scratch repositories and a local bare remote. No network and no GitHub. Each gate first got a toy version with tests and planted bugs that must each fail the suite:

Ran 12 tests in <t>s

OK
mutant PROTECTED: FAILED (failures=1)
mutant STALE: FAILED (failures=1)
mutant MODE: FAILED (failures=1)
mutant CHECKS: FAILED (failures=1)
mutant GATE: FAILED (failures=2)
mutant FALLBACK: FAILED (failures=1)
mutant IDEMPOTENT: FAILED (errors=1)
Ran 9 tests in <t>s

OK
mutant LOCK: FAILED (failures=1)
mutant BLOCK: FAILED (failures=1)
mutant WAL: FAILED (failures=2, errors=5)
mutant GATE: FAILED (failures=2, errors=3)
mutant ARCHIVE: FAILED (failures=1)
mutant BASE: FAILED (failures=1)
mutant RECHECK: FAILED (failures=1)
Ran 15 tests in <t>s

OK
mutant UNKNOWN: FAILED (errors=1)
mutant PENDING: FAILED (failures=1)
mutant SOFT: FAILED (failures=1)
mutant ALLOWLIST: FAILED (failures=2)
mutant MISSING: FAILED (failures=1)
mutant PIN: FAILED (failures=1)
mutant MOVED: FAILED (failures=1)
mutant ONCE: FAILED (failures=1)
mutant TTL: FAILED (failures=1)
mutant HARD: FAILED (failures=1)

Then the real publisher and delivery helper at commit 504d930 against a local bare remote:

== publish_feature.py: a local bare remote stands in for the host
default branch refused     exit 2  BLOCKED_POLICY: Refusing to publish a protected or invalid branch
stale head refused         exit 2  BLOCKED_POLICY: Source HEAD changed since final verification
feature branch pushed      exit 0  PUSHED: Feature branch published
bare remote now has: feature/add-greeting main 
== deliver.py prepare: private inputs must be mode 0600
body file mode 0644: exit 3  RECOVERY_REQUIRED: Private input is missing, unsafe, or exceeds its limit
== deliver.py prepare: origin is a local path, not a GitHub repository
exit 2  BLOCKED_POLICY: Origin is unsupported or fetch and push identities differ
== deliver.py finish --mode local: merge and check in a scratch copy, then move the original checkout
exit 0  LOCAL_DELIVERED: Checked local integration complete; remote publication remains outstanding
feature head is now in the original main: yes
parents of the original main tip: 2
== finish again is a receipt, not new work
ALREADY_DELIVERED - Historical delivery remains present; no new remote work was synchronized
https://github.com/Acme/Widgets.git        -> acme/widgets
https://user@github.com/acme/widgets       -> refused (Unsupported GitHub repository identity)
https://github.com:8443/acme/widgets       -> refused (Unsupported GitHub repository identity)
https://example.org/acme/widgets           -> refused (Unsupported GitHub repository identity)
https://github.com/acme/widgets?x=1        -> refused (Unsupported GitHub repository identity)
/tmp/local/origin.git                      -> refused (Unsupported GitHub repository identity)

The real integration step, with a stand-in crash (our own run ended by a timeout while a test slept, so SIGTERM and no signal handler, not a power cut):

== two integrations at once: the scope lock
second integration       exit 3  BLOCKED_LOCAL: Another integration is active for this checkout and branch 
journals written by the refused run: 0
== a failing check in the scratch merge
original main moved: no; active journals: 1
journal phase: scratch
retry, same attempt      exit 3  RECOVERY_REQUIRED: Unresolved scratch journal requires inspection before new work 
== resolving: the original is re-checked, then the journal is retired, not deleted
resolve journal          exit 0  RESOLVED: Tested current checkout accepted as recovery baseline; artifacts retained
active journals: 0; archived: 1
good branch              exit 0  INTEGRATED: Verified feature merged into original checkout {'cleanup_pending': False}
original main moved: YES; active journals: 0; archived: 2
good branch again        exit 0  ALREADY_INTEGRATED: Source is already in original checkout 
== a crash: end our own integrate_local.py with timeout while its check runs
interrupted process exit: 124
original main moved: no; active journals: 1
journal phase: scratch
retry after the crash    exit 3  RECOVERY_REQUIRED: Unresolved scratch journal requires inspection before new work 
original working tree clean: yes

And the real merge gate, driven in-process with a fake gh that returns canned data for one made-up pull request:

== check: the gate reads one pull request through a fake gh
all green, both required       exit 0 allowed
one required check red         exit 2 BLOCKED
      - check 'lint' is FAILURE
required check skipped         exit 2 BLOCKED
      - required check 'e2e' concluded SKIPPED (policy: a required check must be success)
optional check skipped         exit 0 allowed
required never reported        exit 2 BLOCKED
      - required check 'e2e' has not reported on this head
check still running            exit 2 BLOCKED
      - check 'lint' is not finished (IN_PROGRESS)
head moved while reading       exit 2 BLOCKED
      - the pull request head moved while the checks were read
== hook: what an agent's merge command meets
merge, no head pinned          exit 2 BLOCKED
      - the merge must carry the verified head: add --match-head-commit <head>
merge, head pinned             exit 0 allowed
same merge inside bash -c      exit 2 BLOCKED
merge target not one PR        exit 2 BLOCKED
      - merge target is built at runtime; use a literal number or URL
not a merge (pr view)          exit 0, gh calls made: 0
== waiver: single use, tied to repo, PR and head
waive, no approver             exit 3
waive, with approver           exit 0
first merge after waiver       exit 0 allowed
second merge, same head        exit 2 BLOCKED

Not run: real GitHub or the real gh binary, merge queues and rulesets, a recovery after the original moved, power loss or SIGKILL, and the crash-resume path of the delivery helper. We show only part of the output; the decision text printed by the lab is shortened.

Decision framing

These gates add a delay and some friction: a failing scratch check blocks the next integration until someone resolves it. They pay for themselves when agents deliver to a shared branch often enough that a red check, a moved head or an interrupted merge becomes a question of who did what. For a personal repository with no shared branch, a plain pull request and branch protection may be all you need.

Risks and limits

  • The merge gate sees only merge commands that pass through the shell hook. Merge queues, tool-based GitHub integrations and browser merges are never seen, a script can hide a merge, and an agent that switches CI off can merge with zero checks. The documentation lists these as known limits or accepted risk.
  • The gates assume careless or hurried agents, not adversarial ones, and a waiver log is tamper-evident, not authenticated.
  • A failed check on the original after it moved is a separate state that needs a person; our lab did not exercise it.
  • Local delivery leaves the remote unpublished. A later remote merge can have a different history and needs explicit reconciliation.

FAQ

What is a merge gate for AI coding agents?
A merge gate is a check that runs before any merge command and blocks it unless every required and every reported check on the exact head commit concluded successfully. It exists because an agent or an owner can otherwise merge while checks are red.
Why pin the head commit when merging?
Checks are results for one commit. If the branch moves between the check and the merge, you merge code nobody checked. Passing the verified head to the merge command makes the merge fail if the branch changed.
What is a journal in a local integration?
A journal is a small file written before the risky step. If the process is interrupted, the journal shows that an integration was in flight, and new work on that branch is refused until a person inspects the state and resolves it.
Should an agent be able to force-push or merge a pull request on its own?
Treat both as separate, explicit decisions. A safe publisher never forces and refuses default branches, and a merge needs a gate and a human or main-agent decision, not just a passing local test.

Sources