Skip to content

feat(ci): merge-queue v2 runner core in shadow mode - #4675

Merged
Daniel Meppiel (danielmeppiel) merged 5 commits into
mainfrom
p-mq3-runner-shadow
Nov 21, 2026
Merged

Daniel Meppiel (danielmeppiel) merged 5 commits into
mainfrom
p-mq3-runner-shadow

Conversation

@danielmeppiel

Copy link
Copy Markdown
Collaborator

Description

This is merge-queue v2 step 3: the runner core in shadow mode. It composes, builds, publishes and proves trains, but never merges.

scripts/merge_queue/:

  • api.py is the GitHub REST boundary (gh api, fixed argv, no shell). Everything else takes an Api, so the logic is unit-tested against a fake.
  • tickets.py parses the <!-- apm-queue-ticket head=... prio=P0..P3 fixes=[..] depends=[..] --> marker, where the newest ticket wins. Its admission check requires:
    • the queue:ready label;
    • the head unchanged since the ticket;
    • base main;
    • a same-repo PR;
    • gate green and CodeQL completed at the ticketed head.
  • train.py handles composition (design section 3) and the server-side build:
    • composition orders by lane P0..P3, then ticket time;
    • a Depends-On PR joins only behind its dependencies;
    • at most one big PR (more than 1,500 changed lines), placed first in its lane;
    • a PR that changes .github/workflows trains alone;
    • the window adapts like Zuul: start 4, +1 per green up to 6, halve (minimum 2) after an unattributed red;
    • the build creates queue-build/t<id> at the base and calls the merges API once per member, with Apm-Train: t<id> k= pr=# head= base= and Queue-Base: trailers. A 409 defers that member.
  • refs.py: queue/t<id>/p<k>-guard|full naming and parsing (BATCH puts -full on the head, PIPELINE makes every prefix -full), monotonic ids across the state and live refs, the base probe ref, and the 12 h janitor.
  • proof.py: the verdict is the newest merge-train.yml run on the -full branch at the exact SHA, never a train-gate lookup by SHA alone.
  • state.py: train states (TESTING, then GREEN, RED or ABORTED, forward only). The JSON cache lives under ~/.apm-queue, and rebuild() recovers in-flight trains from the live queue/t* refs and their commit trailers.
  • runner.py: tick, run --interval 60 (with a single-instance file lock and a heartbeat) and metrics.
    • A ShadowApi guard refuses every write except creating queue/* and queue-build/* refs, merges into queue-build/*, and deleting those refs.
    • It logs would-land events (PR, k, predicted tree), one per member of a GREEN train.
  • metrics.py: trains published, verdicts, green rate, would-land count, deferred count, writes to main (expected 0), and mean train minutes.

Docs: a short note in integration-testing.md (Merge-train proof).

Issue and approved scope

Issue: Fixes #4671 (merge-queue v2 step 3; follows slice 1 #4582)

Human scope-approval comment: #4671 (comment)

Does this PR complete the issue, or what remains? It delivers the code. The acceptance criteria are operational and are checked after the merge:

  • 1 working day of shadow with 0 writes to main;
  • at least 5 trains reach a train-gate verdict;
  • every predicted landing matches the merged tree when no other merge intervened;
  • a restart rebuilds the same state.

They are measured by running the shadow runner locally; the results will be posted on #4671.

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Maintenance / refactor

Testing

  • Tested locally

  • All existing tests pass

  • Added tests for new functionality (if applicable)

  • pytest tests/unit/scripts/merge_queue/: the first run gave 37 passed and 2 failed. The 2 failures were missing fake routes in the tests, now added. The re-run is queued on the shared host guard (all slots busy); CI covers it.

    • Coverage: ticket parsing and admission, composition order, dependencies, the big-PR rule, workflow-alone, the window, ref round-trip, ids and janitor, and proof verdicts with urllib.parse query assertions.
    • Also: build ordering and conflict deferral with trailers, forward-only state transitions, state round-trip and rebuild from refs and trailers.
    • Also: shadow ticks (publish, green leads to would-land plus build-ref cleanup, red halves the window, no double composition), the shadow guard refusing writes to main, PR merges and tags, the single-instance lock, and metrics.
  • ruff 0.15.12 check/format: clean. check_complexity_ratchet.py --base-ref main: clean. check_test_assertions.py: clean. check_exact_test_duplicates.py: clean. tests.quality.taxonomy --changed: clean.

Changelog

  • One line in one section under ## [Unreleased] in CHANGELOG.md:
    user-visible effect first, 25 words or fewer, ending with (#PR);
    breaking changes only under upgrade notes; no Highlights; see
    .github/instructions/changelog.instructions.md.
  • N/A -- internal-only change (CI, tests, refactor).

Spec conformance (OpenAPM v0.1)

N/A: no req-XXX behaviour changes.

scripts/merge_queue/ composes trains from ticketed queue:ready PRs (lane then age, Depends-On, one big PR first, workflow changes alone, Zuul-like window), builds them server-side on queue-build/t<id> through the merges API with Apm-Train and Queue-Base trailers, publishes queue/t<id>/p<k>-guard|full refs that merge-train.yml proves, and reads the verdict from the -full branch at the exact SHA. State is a cache under ~/.apm-queue, rebuilt from live queue refs and trailers on restart. A ShadowApi guard refuses every write outside queue refs and the merges API, so the runner logs 'would land #n at T_k' and never writes to main. tick, run --interval 60 (file lock plus heartbeat) and metrics.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved critical defects break Windows CI collection, allow merges targeting main, accept stale or incomplete proofs, and lose in-flight train state during recovery.

16 open issues
What changed in this PR

Adds the shadow-mode runner for merge-queue v2 step 3 (#4671), building on the exact-SHA proof workflow from #4582. It composes and tests speculative trains without landing PRs; operational acceptance remains a post-merge check.

Changes:

  • Adds ticket admission, dependency-aware composition, server-side builds and proof lookup.
  • Adds shadow write restrictions, state recovery, locking, heartbeat and metrics.
  • Adds fake-API tests and documents runner usage.
File Description
tests/​unit/​scripts/​merge_queue/​test_runner.py Tests runner ticks, write restrictions, locking and metrics.
tests/​unit/​scripts/​merge_queue/​test_core.py Tests tickets, composition, refs, proofs and recovery.
tests/​unit/​scripts/​merge_queue/​_fake_api.py Provides a recording fake GitHub API.
tests/​unit/​scripts/​merge_queue/​__init__.py Initializes the test package.
scripts/​merge_queue/​train.py Composes trains and builds speculative merge commits.
scripts/​merge_queue/​tickets.py Parses tickets and checks admission requirements.
scripts/​merge_queue/​state.py Stores train state and reconstructs it from refs.
scripts/​merge_queue/​runner.py Coordinates shadow execution, publication and polling.
scripts/​merge_queue/​refs.py Defines ref names, train IDs and stale-ref selection.
scripts/​merge_queue/​proof.py Selects workflow verdicts by branch and SHA.
scripts/​merge_queue/​metrics.py Summarizes shadow events and train durations.
scripts/​merge_queue/​api.py Wraps GitHub REST calls through gh api.
scripts/​merge_queue/​__init__.py Describes the shadow runner package.
docs/​src/​content/​docs/​contributing/​integration-testing.md Documents shadow runner commands and behavior.

Review effort: Balanced

Comment thread scripts/merge_queue/api.py Resolved
Comment thread scripts/merge_queue/proof.py Resolved
Comment thread scripts/merge_queue/proof.py Resolved
Comment thread scripts/merge_queue/runner.py Resolved
Comment thread scripts/merge_queue/runner.py Resolved
Comment thread scripts/merge_queue/api.py Resolved
Comment thread scripts/merge_queue/runner.py Resolved
Comment thread scripts/merge_queue/runner.py Resolved
Comment thread scripts/merge_queue/runner.py Resolved
Comment thread scripts/merge_queue/train.py Resolved
…ows) (#4671)

fcntl is POSIX-only, so importing the runner broke Windows unit collection. The lock now uses fcntl.flock on POSIX and msvcrt.locking on Windows, imported lazily.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
- api: keep the real HTTP status via --include; paginate list reads
- tickets: a malformed fixes/depends list is an invalid ticket
- proof: only a -full ref proves a train; order runs by created_at, id, attempt
- train: defer conflict dependents transitively, treat 204 as in-base,
  record prefix trees, keep main as Queue-Base for stacked trains
- state: rebuild walks first-parent Apm-Train trailers; reconcile cache with live
- runner: per-endpoint shadow write guard, publication rollback, janitor sweep,
  merged dependencies count as satisfied, would-land logs commit and tree

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@danielmeppiel
Daniel Meppiel (danielmeppiel) merged commit 3466ef0 into main Nov 21, 2026
47 checks passed
@danielmeppiel
Daniel Meppiel (danielmeppiel) deleted the p-mq3-runner-shadow branch November 21, 2026 20:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] ci: merge-queue v2 step 3 - runner core in shadow mode (no merges)

2 participants