Merge Queue for AI Coding Agents: Lessons From 900 PRs

TL;DR

A merge queue tests a pull request merged with the current main branch, and with the pull requests ahead of it, before it lands. Which kind you need depends on where you are:

  • Your plan includes a hosted merge queue. Use it first, because someone else maintains it. We have not used one, so this post only reports what the GitHub and GitLab documentation says.
  • You have one machine, or a plan with no queue. This is our case. One script puts up to 6 ready pull requests in one tree, runs the full check once, and splits the batch in half when it goes red.
  • Your pull requests arrive one at a time, or review is your slow part. A batch will not help much. Most of our trains carried one pull request, partly because we started a train for each one. Go to the review section.

Our AI agents merged more than 900 pull requests in 16 days on one monorepo. The queue did not make the check faster. It made merging reliable, and then it showed us that the time was going into review.

Our software factory had 20+ pull requests ready, and merging was slow. By software factory I mean a setup where AI agents plan, write, review, and merge code, and people set the direction and the rules. The agents had done their part and the reviews had passed. The pull requests sat there because each merge needed a check of 17 to 20 minutes, and the scripts that waited to merge them had died without telling anyone.

I think many teams will meet this once agents write most of their code. The first half of this post is about the queue we built and what broke. The second half is about review, because that is where the time went after the queue worked. One caution: this is one monorepo on one small machine that the agents share, and we have not tuned each part yet, because we are building the whole workflow first.

What a merge queue is

A merge queue is a line in front of the main branch. A pull request joins the line after review. The queue builds the tree that main would have after the merge, runs the checks on that tree, and merges only when they pass. A pull request that fails leaves the line, and main stays green. GitLab calls its version a merge train.

The idea is older than the name. Jane Street's engineering blog credits Graydon Hoare for the rule, and states it this way: "automatically maintain a repository that never fails its tests". Many teams break this rule every day without noticing, because they test a branch and then merge it into a main branch that has moved since the test.

The bors-ng README has the classic example. One pull request renames a function. Another adds a new call to the old name. Each one passes its tests against main, because each was tested without the other. Both merge, and main is red. No reviewer missed anything, and no test was wrong. The two changes were never tested together, and a merge queue closes that gap by testing the combined tree.

Three ways to merge

There are three common ways to get ready pull requests onto main, and they differ in how many checks each one costs.

WayChecks for each PRWhen one failsWho documents it
One by oneOneFix it, run againAny setup
Queue, one check eachOne, in parallelIt leaves, the rest rerunGitLab, GitHub
Queue with batchesLess than oneThe batch is splitMergify, Aviator, Trunk

The second row buys speed with machines. In GitLab merge trains, each merge request gets its own pipeline on the combined changes of the ones ahead of it, and the pipelines run in parallel, 20 by default. Merge trains need the Premium or Ultimate tier. The GitHub merge queue puts pull requests in merge groups on a temporary branch, removes one that fails, and builds the rest again. It has settings for how many pull requests merge as one group, and its documentation does not describe splitting a failed group to find the bad one. GitHub's documentation lists the queue as available in public repositories owned by an organization and in private repositories on GitHub Enterprise Cloud.

The third row trades machines for waiting. Mergify, Aviator, and Trunk document batches with a split on failure, and bors-ng did it before them (its repository is archived now and points to GitHub's merge queue). We have not used any of these tools, and we did not check whether they have a free tier.

Our repository is private and on the free plan, so the hosted queue was not open to us, and we have one machine. That is why we built the third row ourselves. If the second row is on your plan, start there.

Which merge queue do you need?

The flow has four questions. Is a hosted queue on your plan? Then use it. If not, do your ready pull requests wait together? If they arrive one at a time on a free line, a batch has nothing to combine, and one by one is fine. If they do wait together, does the full check run once for each merge, or do builders run it too? Fix that before you batch, because the load makes tests fail. And does any test fail only under load? Fix it first, because one such test blocks every pull request in a batch.

Why merging became our bottleneck

In our setup one lead agent hands work to other agents, and they all open pull requests on one monorepo. I described the whole setup in Software Factory Levels in 2026. The part that matters here is the volume. From 4 November to 19 November the agents merged more than 900 pull requests, about 57 a day, and six of those days ran at 80 or more.

A person who writes two pull requests a day does not feel a 20 minute check. Agents that write 80 a day do. Twenty minutes for each of 80 merges is more than 26 hours of checking in one day, on one line, so the line can never catch up. Our CI ran on self-hosted runners at the time. The main job had a median of 20.3 minutes, and a job waited a median of 28.5 minutes before it even started. In the week before the queue, five pull requests merged one by one took about 1.5 hours.

We already had a local check on the lead agent's machine that merged one pull request at a time. On 10 November that local check became the gate, and we wrote the first version of the queue around it. CI on pull requests came back later, as a second signal.

How our merge train works

We call ours the merge train. It is one shell script, and it runs on the lead agent's machine.

  1. Check the paperwork first. The script reads each pull request's description and base branch without merging anything. A pull request with an unfinished template is dropped in seconds, before the long check.
  2. Build one tree. It starts from the latest main and merges each pull request in order. A pull request that conflicts is dropped and reported, and the rest go on.
  3. Run the full check once. The check has about 20 steps: install, format, lint, typecheck, the tests of the affected projects, documentation and link checks, integration tests, builds, and a secret scan.
  4. Green: merge each one in order. Every pull request is squash-merged. Before each merge the script proves that the tree it is about to land is the tree that passed. If main moved in the meantime, the train stops and hands the rest to the one by one path.
  5. Red: split the batch in half. Each half runs the same way, down to a single pull request. The one that fails alone is reported with its log. The others still merge.

A red batch is split in half until one fails alone

Take a batch of four where the fourth is broken. The first run is red. The script splits it into two pairs. The first pair is green, and both merge. The second pair is red, so it splits again: the third pull request passes alone and merges, and the fourth fails alone and goes back to its agent. That is four more runs to find one bad pull request, and three good ones still landed without a person looking at them.

The repository has no branch protection on the free plan, so the safety comes from the process. Agents never merge. This script is the only path to main, and it merges only the exact tree that passed. Each train runs as a supervised system service with a memory limit on its check, and a lock makes sure two trains never run at once.

A batch tests the final tree, with all the pull requests in it. It does not test main as it is after the first merge alone, or after the second. GitLab's merge trains test each of those states, and a single machine cannot afford that.

What the numbers say

I expected the train to make merging much faster. The numbers are more modest.

On the first night back on the train, 34 train runs merged 37 pull requests, and a run took 10.4 minutes on average. The best batch that night put 4 pull requests through one 18 minute check. Throughput was about 2.8 pull requests an hour, against about 2.1 an hour during the day before, when merges went one at a time. I do not trust that comparison, because the night had a lower machine load and fewer new pull requests arriving.

Over about 40 hours, 60 of 99 trains carried one pull request. The largest carried 6. A train with one pull request is a merge with extra steps, so the batching did little work most of the time.

The merge train did not make our checks faster. It made merging boring, and that turned out to be what we needed.

What it gave us is reliability. Before the train, small background scripts waited for a pull request to be ready and then merged it. Some waited forever. One night the system's out of memory killer ended the lead agent's own session. When those scripts died, ready pull requests sat unmerged and nobody knew. A train is a supervised service that writes a log and a summary, so a dead agent no longer takes the merge with it. Since we switched back to it on 17 November, the train has carried 136 pull requests.

Why most trains carried one pull request

"Most trains carry one pull request" sounded like one problem. When we counted, it was two. Of the 60 single trains, 31 started within two minutes of the end of the train before them. Those pull requests had waited behind a running train and could have shared the next one. The other 29 started on an idle line, with nothing to share a train with. The line was busy only 48% of the time.

The 31 had a cause, and it was ours. Each passed review started its own train, and joining a train that was still waiting was a manual step, so the agents skipped it. A reader of my LinkedIn post suggested a short holding window. We did not add one, because a timer would have made the 29 wait on an idle line for no gain. The check that is running already works as the window.

When a batch does not help

From these numbers I would not build a batch queue in three cases. Pull requests arrive alone on a free line. A test fails under load, because then a batch fails more often than a single merge. Or the check is long and the pull requests are few, where a queue with one check each gives the same result with less code.

The merge gate, after 19 hours

On 19 November we changed who starts a train. An agent no longer starts one. It marks a pull request as ready, and one long-running gate takes every ready pull request, oldest first, up to 6, each time the line is free. A pull request that cannot merge is held with its reason and is not retried in a loop.

In the first 19 hours the gate ran 66 trains and merged 58 pull requests. Nine trains carried 2 or 3 pull requests, all without a person or an agent arranging it. Single trains that waited behind another train for no reason went from 31 to 0. The median check took 10 minutes and the longest took 22. Seventeen trains merged nothing, and they cost 135 minutes. The two periods had different loads, so I read this as a change of behaviour and not as a measured speed-up.

The 20 holds taught me more than the batches did. Nine were real faults in the pull request that the builder's focused checks had not run. Six were conflicts with main. Five came from a test that the pull request did not touch. So the train was the first full check many of our pull requests ever saw. The fix for the nine was a list: each time, we added the one missing target to the checks a builder runs before it says "ready".

An AI agent runs this gate, and it was wrong five times in those 19 hours. It blamed machine load for a time-out that the pull request itself had caused. It proposed a clever way to pick the bad pull request from a failed batch, which found 0 of 2 when we tested it on old logs. None of this merged anything, because the script needs a green check of the exact tree and does not ask the agent. I wrote the ten rules from that day in a LinkedIn post.

What broke

The first live train merged nothing

It failed for three separate reasons: a wrong call to the build tool, a cache server that was down, and a conflict. A queue is one more program in front of main, and it has its own bugs before it catches anyone else's.

Running the heavy check twice

Agents ran the full check before they said a pull request was ready, and then the train ran it again on the same 6 core machine. Under that load tests started to time out. The rule now is that builders run focused checks on what they changed, and the train runs the full one. A separate change shows how much load costs: after we put a cap on the number of test workers, the same check went from 1,365 seconds to 769 seconds, and the load on the machine fell by about half.

Two tests that looked flaky

The first one stopped three trains in a row. It passed alone and failed in the train, every time. It searched a rendered table of 601 rows by role, which took about 1.7 seconds on an idle machine and about 18 seconds in a loaded train, past its 15 second limit. Searching inside one row took about 1 millisecond.

The second one timed out in 4 trains over 12 hours, for pull requests that did not touch it. Two guesses blamed the CPU, and giving the train more CPU did not stop it. An agent then measured what the test waited for. Each run wrote about 3 GB to a temporary folder on a slow disk. The same file took about 9 seconds with that folder in memory and 46 seconds on disk, with equal CPU time. After the fix, that test run went from 307 seconds to 72.

I now think a test that fails only under load is a slow test, and you should measure what it waits for before you call it random. A high load average with idle CPUs points at the disk.

A retry that could hide a real failure

We retry one check step once when its log shows a resource or time-out error from the system, because we had two red runs that had nothing to do with the code. Another reader of the LinkedIn post warned that a retry can hide a test that fails under load. That was right. The retry is now narrow: one retry, only when the pull request changes no file of the project that failed, and every retry that passes is written to a log. That log is what showed one test behind 4 failed trains.

Stopping a train did not stop it

The check runs in its own scope so that it survives when something else dies. The lead agent stopped a train's service, and the check kept running, went green, and merged 2 pull requests that the lead agent believed were stopped. Nothing unsafe landed, because the gate itself held. The lead agent's picture of the line was wrong, though, and a system that agents steer needs that picture to be right. The stop procedure now stops the waiting trains, then the service, and then its scope.

A stale file got past the gate

The train only runs checks for the projects a change affects, and the check that guards one generated index was outside that selection. The fix was small. The lesson I keep is the test of the fix: we planted a stale index and saw the check go red, then ran a clean tree and saw it go green. A gate that has never been seen to fail is not proven to work.

The secret scan read too much

All the agents' workspaces share one git object store, and the scan read every branch in it. One agent's unpushed commit stopped another agent's pull request.

Stacked pull requests for review

The queue handles merging. Review has its own problem: a large feature makes a large pull request, and nobody reads a large pull request well. Google's guidance for its own engineers says about 100 lines is usually a reasonable size for one change and about 1,000 lines is usually too large.

Stacked pull requests are the standard answer. GitHub defines a stack as two or more pull requests where the bottom one targets the main branch and each next one targets the branch below it. One of our features shipped as a stack of 7 layers, and each layer got a fresh review.

Stacks and squash merge do not fit well. When the bottom pull request is squash-merged, main gets one new commit. The pull request above it still contains the original commits of the one below, so git sees the same change twice and reports conflicts. Patrick Stevens explains the mechanism in a post from 2023. Our fix is a rebase of each remaining layer onto main after every merge, which we script. A stack cannot ride in one batch unless every layer was rebased first, and after one rebase three stacked pull requests failed the format step, so a format check now runs before every push.

The cost is real. One stack of 3 pull requests took 7 trips through the gate. The better fix is earlier: cut the work into parts that branch from main and do not need each other.

GitHub now has stacks built in. Its stacked pull requests feature became generally available on 6 October 2026. The announcement says a stack lands through the merge queue as one group, and that GitHub retargets the stack when its base branch is deleted. We have not tried it. If you start today, look at it before you write your own rebase script.

Review is the slow part now

Once the train worked, I looked at where the time went, and it was not in the train.

In our factory a reviewer agent that did not write the code reads each pull request in a fresh context, and it passes the pull request or sends it back. On 18 November the agents logged 30 review results: 19 passed and 11 were sent back. One round of fixes took 18 to 48 minutes of agent time, plus 3 to 11 minutes for the next review. One pull request shows the shape. It took about 72 minutes to build, and then about 105 minutes of rework and review, across two rounds of fixes and three reviews. The train was about a tenth of the total, and the train time is our estimate.

After the queue, the time went to review

Two practices came from that day. A fix is new code, so we review it too; later rounds kept finding new gaps in fixes. And the findings repeated: a test that could not fail, a click target that was too small, state that outlived its component. We turned the repeated findings into a checklist of 8 checks that a builder agent runs before it says a pull request is ready. We wrote about review layers before in AI Code Review Is Broken. The checklist is the cheap layer of that model, moved in front of the reviewer.

The nine rules

For merging:

  1. Use a queue when ready pull requests wait in line.
  2. Batch when pull requests wait together, up to 6. Run one full check, and split the batch when it goes red.
  3. Run the heavy check once. Builders run focused checks, and the queue runs the full one.
  4. Retry once on an environment failure, and log every retry that passes.
  5. Fix a test that fails under load before anything else. It blocks every batch.
  6. Run the queue under a supervisor, and know how to stop it.

For review:

  1. Stack a change that is too big to review, one layer for each pull request.
  2. With squash merge, rebase the rest of the stack after every merge.
  3. Review the fix as well as the first version.

Where this stops

  • This is one monorepo on one 6 core machine, over 16 days. Your numbers will differ, and a faster machine would change every check time here.
  • We have not used GitHub's merge queue, GitHub's stacked pull requests, GitLab merge trains, Mergify, Aviator, or Trunk. What I say about them comes from their documentation, read on 19 November 2026.
  • The speed comparison is from one night. The gate numbers are from 19 hours. The reliability gain is what I would defend.
  • I have no cost numbers, so I give none.

What we are trying next

Review rework is the next thing to cut, and we have no result there yet. We are also trying a gate that merges without a person: a pull request merges by itself when the checks pass, or when a person adds a label to it. I still decide what is worth building, and I still read what the reviews say. I would like to stop being the one who presses merge.

Do your pull requests arrive in groups, or one at a time?

Further reading

Similar articles

Software factory levels in 2026

Software Factory Levels in 2026: Six Axes Instead of One Ladder

A software factory has six axes: control, compute, context, trust, learning and runtime. Where the agents run is only one of them. Here is how we score ours, the rules we run, and which axis to raise first.

A modern development stack in 2026

What a Modern Development Stack Looks Like in 2026

Many posts explain how to choose a tech stack. Here is what we use in real projects in 2026, our preferences, where each default bends, and the five questions that decide when it should.

Scaling and Integrating Health Management Solutions: Overcoming Tech Challenges in Healthcare

Scaling and Integrating Health Management Solutions: Overcoming Tech Challenges in Healthcare

See how INNOMIZE helped a health platform overcome technical challenges to improve scalability, integration, and user experience in managing chronic conditions.