Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

How It Works

MergeSpan is a durable queue for one repository and one target branch. Each pull request passes through a small set of states before it lands:

labeled with queue → candidate built → required checks pass → merge freshness verified → squash merged → candidate branch deleted

The important design premise: MergeSpan does not trust webhooks to be authoritative. A webhook delivery is only a hint that makes MergeSpan fetch again from GitHub. Observations are the source of truth.

Notation

  • A — current commit of the base branch.
  • B — current head commit of the pull request.
  • C — candidate commit: base tip plus pull request head, committed on mergespan/queue/<number>.
  • check mergespan — the check run MergeSpan publishes on the pull request head (B), never on C itself.

Zero-event initialization (first run)

Before any webhook arrives, a repository has no durable state. From the Console, open the repository page and click Reconcile.

The exact sequence:

  1. The reconcile request is persisted before any network call, and the API answers accepted (HTTP 202). The response acknowledges the request only; initialization runs on a later slice. A repository with no coordinator may only be initialized by a Reconcile panel action; every other write is rejected as NotInitialized.
  2. On the next slice, the runtime calls connect:
    • Mint an installation token scoped to this repository.
    • Read main (the default base used at bootstrap) and .github/mergespan.toml at that exact commit, with ?ref=<sha>.
    • Construct a QueueSession and a Coordinator.
    • Require same_repo_only = true. A config without it fails SameRepoOnlyRequired.
  3. The first alarm performs discovery reconciliation: it lists open pull requests carrying the queue label and starts verification for each eligible one.

A freshly installed App with zero webhooks therefore reaches a running queue through the console. The webhook is not mandatory for initialization. See Console for the Reconcile flow.

Full ordering of stages

Each queued pull request moves through these stages in order.

1. Enqueue

The pull request must be open, not draft, target the configured base, and carry the queue label. Fork heads are never queued with same_repo_only = true.

  • Add the label with gh pr edit <number> --add-label queue.
  • Or comment @mergespan queue.
  • Urgent: comment @mergespan urgent. It adds urgent and queue if missing, then moves the pull request to the front. See Urgent preemption below.

MergeSpan records a Queued event with position. With urgent, the position is 1.

2. Candidate creation

MergeSpan creates mergespan/queue/<number> at the current base commit A, then pushes candidate C = A + B to the same ref. One queue entry produces two pushes:

  • Push A: branch creation.
  • Push C: candidate update.

The verification workflow should skip the creation push (github.event.created == false) and run only on C. A concurrency group per ref and created value prevents a late creation push from cancelling the candidate run. See Sandbox Notes → Verification workflow for the exact workflow shape.

On green, expected artifacts are:

  • branch mergespan/queue/<n>
  • candidate SHA C pushed once to that branch
  • required check runs on C
  • a mergespan check run on PR head B

3. Required checks

MergeSpan reads checks on C. A check counts only when:

  • it is one of required_checks,
  • its source is GitHubActions,
  • its head SHA is exactly C,
  • its status is Completed with conclusion Success.

Neutral, skipped, cancelled, timed out, stale, action required, or unknown conclusions do not count. If the same check name runs more than once, MergeSpan picks the newest by started_at_ms; ties favor a failed observation over success.

When all required checks pass, MergeSpan publishes ChecksPassed and moves to merge freshness verification.

4. Merge freshness verification

Before squashing, MergeSpan re-reads:

  • the pull request (must still be open, non-draft, target the base, still labelled queue, head SHA still B),
  • the base branch (must still be A),
  • the candidate branch (must still point at C, and C’s tree must still match the verified candidate tree).

Any mismatch cancels the current candidate and rebuilds: the candidate is re-created on the new base/head and verified again. This is the pre-build re-observation step.

5. Squash merge

MergeSpan squash-merges B with head pinned to B through the GitHub API. This is not atomic: GitHub’s merge API pins PR head B, not the base branch, so a concurrent merge can land first. MergeSpan re-reads PR and refs immediately before squashing, then reconciles:

  • The squash commit’s parent must be A and its tree must equal C.
  • A parent/tree mismatch pauses the queue and comments on the pull request. It is never reported as success.
  • A moved base requires a new candidate.

5. Squash merge

MergeSpan squash-merges B with head pinned to B through the GitHub API. This is not atomic: GitHub’s merge API pins PR head B, not the base branch, so a concurrent merge can land first. MergeSpan re-reads PR and refs immediately before squashing, then reconciles:

  • The squash commit’s parent must be A and its tree must equal C.
  • A parent/tree mismatch pauses the queue and comments on the pull request. It is never reported as success.
  • A moved base requires a new candidate.

6. Confirm merge, then cleanup

The mergespan check can be green (candidate verified) before the final freshness reads and the squash itself; green is not proof that anything merged. Cleanup happens only after GitHub reports the PR as merged and MergeSpan’s post-merge read confirms the merge SHA’s parent is A and its tree equals C:

  • MergeSpan cancels any workflow runs for C and deletes mergespan/queue/<n>. A missing branch (404) counts as success.
  • Cleanup completion releases the ref gate.
  • If the post-merge read mismatches, the queue is paused and the PR is commented; it is never reported as success. Investigate before re-queueing.

Urgent preemption

The urgent label moves a pull request to position 1 and interrupts the candidate being verified, unless the active candidate is already in Merging phase or belongs to the urgent pull request itself.

  • The interrupted pull request stays queued.
  • It is rebuilt later on the current base.
  • Urgent cannot cancel a squash merge already issued.
  • Use only when the user asks for it: it interrupts whoever is being verified.

What removes a pull request from the queue

ReasonObservable resultWhat to do
Required check failed on Cmergespan check Failure on B; queue label removed; comment links the failing runFix the failure, push, add queue again
Push to the pull request while queuedHead moved; queue label removed; comment with old and new headFinish all pushes, then add queue again
Base moved before mergeCandidate rebuilt; label may stay on queueKeep waiting
Validation exceeded timeout_minutesDequeued with timeout reason; label removedFind out why the check was slow before queueing again
Squash merge conflictDequeued with merge conflict reason; label removedMerge the base, resolve conflicts, push, add queue again
Squash refused / gate revocation failed closedDequeued with squash refusal reason; label removedInvestigate GitHub merge API status; re-queue after investigation
Pull request closed, converted to draft, retargeted, or queue label removedRemovedRe-queue once eligible again

Manual controls

The console exposes:

  • Reconcile — initializes a repository before any webhook, or re-scans queue labels after it is running. Also resumes a queue after configuration changes.
  • Pause — no new candidates, no merges. Queued pull requests keep their place.
  • Freeze — candidates are still verified; nothing is merged.
  • Dequeue — removes queue and urgent from one pull request. Cannot cancel a squash merge already issued.
  • Resume — operator action after investigating a fail-closed pause.

A paused or frozen queue shows its reason in the mergespan check summary or console state view. Do not remove the label or push to work around it.

Failure surfaces and actions at a glance

StageFailure surfaceSource of diagnosisAction
Configuration loadmissing or invalid .github/mergespan.toml; same_repo_only != true; base mismatchConsole setup check repository_config, config_loadedFix TOML on base branch; re-run Reconcile
App identitywrong App ID, missing private keyConsole setup app_permissions / app_eventsRestore documented App settings
Candidate creationbranch creation refused, branch head read failuremergespan check Failure; candidate branch missingRestart queue / Reconcile; verify App contents:write
Checks never appearcandidate branch exists but no check runsVerification workflow concurrency; created == false gateFix workflow trigger; see Sandbox Notes
Check failure / timeoutrequired check missing, neutral, or non-success on CLinked Actions run; check name, source, exact SHAFix CI; re-queue
Merge freshness mismatchbase moved, head pushed, candidate tree changedConsole events; check Failure summaryRe-queue when ready
Squash failureconflict or refusal from GitHub APImergespan check Failure; merge refusal messageResolve conflict or investigate; re-queue
Cleanup failurecandidate branch deletion refusedConsole outbox / diagnosticsRetry queue action; branch cleanup is best-effort for ref gate release

State invariants

  • Only one candidate is active per repository.
  • Commands are durable before any network call; a crash leaves an intent checkpoint that recovers on restart.
  • Deduplication is by X-GitHub-Delivery; the receipt, new state, and outbox rows are written in one SQLite transaction.
  • Observations carry monotonically increasing sequence numbers; stale reads are discarded.
  • While a merge is unresolved, reconciliation is suspended so a generic PR snapshot cannot end the candidate before post-merge parent/tree reconciliation runs.

Flowchart

The site renders this flowchart directly on GitHub (Mermaid is supported in READMEs). It is not a separate image; no external assets are loaded.

flowchart TD
    A[PR labelled queue] --> B[Create mergespan/queue/<n> at base A]
    B --> C[Push candidate C = A + B]
    C --> D[Required checks on C]
    D -- fail/timeout --> E[Remove label, comment, dequeue]
    D -- pass --> F[Re-read PR, base, candidate]
    F -- mismatch --> C
    F -- fresh --> G[Squash merge B]
    G -- parent/tree mismatch --> H[Pause queue, comment]
    G -- ok --> I[Delete candidate branch]

External links are not part of this flow; the Markdown links above stay relative to docs/ and resolve on GitHub as docs/<name>.md after navigation to docs/<name>.html.