Skip to content

ci(cancel-superseded): order queue runs by (datetime, id), fail on a non-queue ref - #1923

Merged
zackees merged 1 commit into
mainfrom
agent/test-cancel-superseded
Nov 5, 2026
Merged

zackees merged 1 commit into
mainfrom
agent/test-cancel-superseded

Conversation

@zackees

@zackees zackees commented Nov 5, 2026

Copy link
Copy Markdown
Member

Why

ci/cancel_superseded.py is the merge-queue janitor: on every merge_group run it
cancels the in-flight queue runs for the same PR that a newer queue run replaced, so
they stop holding runners when main moves. It had zero tests, and three ways to get
the decision wrong. All three are reproduced against origin/main and are regression
cases in the new module.

1. Timestamps compared as strings (cancels the LIVE run)

run["created_at"] < current["created_at"] is a lexicographic comparison. GitHub
sometimes emits fractional seconds, and . (0x2E) sorts before Z (0x5A):

current : 2026-11-05T20:00:00Z        (00.000)
newer   : 2026-11-05T20:00:00.500Z    (00.500)  -> genuinely LATER
main    -> superseded() returns [906]           -> cancels the newer run

The dangerous direction: the run that actually owns the queue slot is cancelled and
the stale one keeps a runner. Order is now (datetime, id), parsed with
datetime.fromisoformat (fractional seconds, offsets and naive stamps handled).

2. A same-second tie cancelled nothing

Two queue runs created in the same second satisfied neither <, so neither was
cancelled and the redundant run survived the whole queue. id increases with
creation, so it breaks the tie.

3. A non-queue ref reported success

When the triggering run's ref is not gh-readonly-queue/<base>/pr-<N>-<sha>, main
printed PR #None: cancelling 0 superseded queue run(s): [] and exited 0 — success
for a cleanup that never had a target, exactly what a broken event wiring looks like.
It is now rc 1 on stderr naming the ref.

Also

  • QUEUE_RE used [^/]+ for the base branch, so a queue ref for a slashed base
    (gh-readonly-queue/release/2.x/pr-7-abc123) was silently classified as "not a
    queue run". Base segment is .+ now.
  • URLError during a cancel is tolerated per run: cleanup is best-effort, and a
    network blip must not turn a merge_group check red and block the queue.

Tests

New ci/test_cancel_superseded.py, 21 tests, driving the real module with a stubbed
API (no network, no token): queue-ref parsing (incl. slashed base), fractional-second
ordering in both directions, the same-second tie, multi-run selection, dry-run, 409
tolerance, network tolerance, and the non-queue-ref hard error.

Real output:

$ uv run --no-project python -m unittest ci.test_cancel_superseded
Ran 21 tests ... OK

$ hosted self-test set (ci-workflow-drift.yml command, 18 modules)
Ran 235 tests in 11.233s
OK

$ uv run --no-project python ci/gate_script_coverage.py --budget 4
Every workflow-invoked gate script is accounted for

$ uv run --no-project python ci/check_python_complexity.py
All checks passed!

$ uv run --no-project ruff check <changed files>   -> All checks passed!
$ uv run --no-project ruff format --check <changed> -> 2 files already formatted
$ uv run --no-project python ci/render_workflows.py --check -> rc 0

RED proven by mutating the fix in place, one at a time, then restoring:

mutation failing tests
order(run) < cutoff -> run["created_at"] < current["created_at"] 2 (fractional_second_run_that_is_later, same_second_tie)
tuple order -> created_at(run) < created_at(current) 1 (same_second_tie)
drop the pr is None hard error 1 (non_queue_ref_is_a_hard_error)
QUEUE_RE base .+ -> [^/]+ 1 (parses_a_non_main_base)

Ratchet

ci/gate_script_tests.toml: cancel_superseded moves out of untested with
tests = ["ci/test_cancel_superseded.py"]; MAX_UNTESTED 5 -> 4. Registered in both
entry points (ci/local_suites.py DRIFT_UNITTEST_MODULES and the
ci-workflow-drift.yml unittest command) in the same position, plus a ci/README.md
bullet.

Part of the untested-gate backlog from #1915 (#1917, #1919, #1922 are done).

…non-queue ref

The merge-queue janitor decides which in-flight queue runs a newer run
replaced, and it had three ways to get that wrong:

* `run["created_at"] < current["created_at"]` compared ISO timestamps as
  STRINGS. GitHub sometimes emits fractional seconds, and `.` sorts before
  `Z`, so a run created 500 ms AFTER a whole-second one read as older: the
  janitor cancelled the LIVE queue run and left the stale one holding a
  runner. Order is now `(datetime, id)`.
* A same-second tie satisfied neither `<`, so the redundant run survived the
  whole queue. The id (which increases with creation) breaks the tie.
* A triggering ref that was not `gh-readonly-queue/<base>/pr-<N>-<sha>`
  printed `PR #None: cancelling 0` and exited 0 -- success reported for a
  cleanup that never had a target. It is now a hard error naming the ref.

Also fixes `QUEUE_RE`, whose `[^/]+` base segment silently classified a
queue ref for a slashed base branch (`release/2.x`) as "not a queue run",
and tolerates `URLError` per run so a network blip during cleanup cannot
turn a merge_group check red.

New `ci/test_cancel_superseded.py` (21 tests) drives the real module with a
stubbed API: fractional-second ordering both ways, the same-second tie,
multi-run selection, dry-run, 409 tolerance, network tolerance, and the
non-queue-ref hard error. Registry entry added, `untested` 5 -> 4, both
entry points wired.
@coderabbitai

coderabbitai Bot commented Nov 5, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@zackees
zackees added this pull request to the merge queue Nov 5, 2026
Merged via the queue into main with commit 15c36fa Nov 5, 2026
31 checks passed
@zackees
zackees deleted the agent/test-cancel-superseded branch November 5, 2026 20:49
@fastled-project-sync fastled-project-sync Bot moved this to Triage in FastLED Tracker Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Triage

Development

Successfully merging this pull request may close these issues.

1 participant