From ce734acf79dcc525f732250df5a9b413eb441a6a Mon Sep 17 00:00:00 2001 From: Larry Klosowski Date: Sun, 29 Nov 2026 08:43:07 -0800 Subject: [PATCH 1/3] test(merge-gate): pin which check runs the ci gate treats as pending Port of citrate-core #950's jq test to the org template. Runs the workflow's own summary/failed/pending lines with bash and jq over check-run lists shaped like the API's. Red against main: a run with a conclusion but status still in_progress counts as pending (the core #917 two-hour wait), and a stuck run's failure is not judged. Signed-off-by: Larry Klosowski Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01FPJzHNqGLdTVoFzde3Sxbw --- scripts/ci/test_merge_gate.py | 122 ++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 scripts/ci/test_merge_gate.py diff --git a/scripts/ci/test_merge_gate.py b/scripts/ci/test_merge_gate.py new file mode 100644 index 0000000..c7d3694 --- /dev/null +++ b/scripts/ci/test_merge_gate.py @@ -0,0 +1,122 @@ +#!/usr/bin/env python3 +"""merge-gate.yml `ci` job: which checks are pending, which failed. + +The `ci` job judges every other check run on the commit. These cases run the +workflow's own `summary`, `failed` and `pending` lines (read from the file, run +by bash and jq as on the runner) over check-run lists shaped like the GitHub +API's. Ported from citrate-core #950. The case that motivated it: on 2026-11-28 +the merge-queue commit of citrate-core #917 carried a `lane-guard resolve` run +with conclusion success and completed_at set whose status never left +"in_progress", and the gate waited on it for its full 120 minutes. + +Run: ``python3 -m unittest discover -s scripts/ci -p 'test_*.py'`` +(needs bash and jq on PATH, as the runner has). +""" + +from __future__ import annotations + +import json +import re +import shutil +import subprocess +import unittest +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[2] +WF = (ROOT / ".github" / "workflows" / "merge-gate.yml").read_text(encoding="utf-8") + + +def piece(pattern: str, what: str) -> str: + """One line or block of the wait step, verbatim (bash strips the indentation).""" + m = re.search(pattern, WF, re.M) + if not m: + raise AssertionError(f"merge-gate.yml: no {what}") + return m.group(0) + + +SUMMARY = piece(r"^ *summary='[\s\S]*?'$", "summary filter") +FAILED = piece(r"^ *failed=\$\(jq .*$", "failed line") +PENDING = piece(r"^ *pending=\$\(jq .*$", "pending line") + +T0 = "2026-11-28T20:27:12Z" + + +def run(name, status, conclusion, completed_at=None): + if completed_at is None and conclusion is not None: + completed_at = T0 + return {"name": name, "status": status, "conclusion": conclusion, "completed_at": completed_at} + + +@unittest.skipUnless(shutil.which("bash") and shutil.which("jq"), "needs bash and jq") +class MergeGatePending(unittest.TestCase): + def judge(self, runs): + """The gate's verdict on `runs`: (failed check names, pending count).""" + script = ( + "set -euo pipefail\njson=$(cat)\n" + f"{SUMMARY}\n{FAILED}\n{PENDING}\n" + "printf '%s\\n%s\\n' \"$failed\" \"$pending\"\n" + ) + out = subprocess.run( + ["bash", "-c", script], + input=json.dumps({"check_runs": runs}), + capture_output=True, + text=True, + check=True, + ).stdout + failed, pending = out.split("\n")[:2] + return failed, int(pending) + + def test_run_with_conclusion_is_finished_even_if_status_in_progress(self): + # The two attempts of lane-guard resolve on core #917's queue commit. + r = self.judge([ + run("lane-guard resolve", "in_progress", "success", "2026-11-28T20:27:12Z"), + run("lane-guard resolve", "completed", "success", "2026-11-28T21:22:17Z"), + run("rust (fmt, clippy, test, audit)", "completed", "success"), + ]) + self.assertEqual(r, ("", 0)) + + def test_queued_or_running_run_is_still_waited_on(self): + self.assertEqual( + self.judge([run("rust", "queued", None), run("node", "completed", "success")]), + ("", 1), + ) + self.assertEqual(self.judge([run("rust", "in_progress", None)]), ("", 1)) + + def test_stuck_runs_failure_still_fails_the_gate(self): + self.assertEqual(self.judge([run("lane-guard", "in_progress", "failure")]), ("lane-guard", 0)) + + def test_latest_finished_run_decides_cancelled_ignored_all_cancelled_fails(self): + self.assertEqual( + self.judge([ + run("node", "completed", "failure", "2026-11-28T20:00:00Z"), + run("node", "completed", "success", "2026-11-28T21:00:00Z"), + ]), + ("", 0), + ) + self.assertEqual( + self.judge([ + run("node", "completed", "success", "2026-11-28T20:00:00Z"), + run("node", "completed", "cancelled", "2026-11-28T21:00:00Z"), + ]), + ("", 0), + ) + self.assertEqual(self.judge([run("node", "completed", "cancelled")]), ("node", 0)) + + def test_rerun_in_progress_keeps_check_pending(self): + self.assertEqual( + self.judge([ + run("lane-guard", "completed", "failure", "2026-11-28T20:00:00Z"), + run("lane-guard", "in_progress", None), + ]), + ("", 1), + ) + + def test_gates_own_checks_are_never_judged(self): + self.assertEqual( + self.judge([run("ci", "in_progress", None), run("secret-scan", "in_progress", None)]), + ("", 0), + ) + + +if __name__ == "__main__": + unittest.main() From 403d4cf84c16a82583e849365e687b71cec78563 Mon Sep 17 00:00:00 2001 From: Larry Klosowski Date: Sun, 29 Nov 2026 08:43:22 -0800 Subject: [PATCH 2/3] fix(merge-gate): a check run with a conclusion is finished, whatever its status says Port of citrate-core #950. GitHub can leave a finished run's status at in_progress; the ci gate keyed pending on status and so waited out its 120-minute deadline on such a run. Pending now counts runs with no conclusion (queued and running runs both have conclusion null), and the verdict is taken from runs that have a conclusion, so a stuck failure still fails the gate. Signed-off-by: Larry Klosowski Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01FPJzHNqGLdTVoFzde3Sxbw --- .github/workflows/merge-gate.yml | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/.github/workflows/merge-gate.yml b/.github/workflows/merge-gate.yml index 8313049..3005620 100644 --- a/.github/workflows/merge-gate.yml +++ b/.github/workflows/merge-gate.yml @@ -94,11 +94,20 @@ jobs: # pending; otherwise judge the most recent non-cancelled result. A run # cancelled because a duplicate superseded it is ignored; a check whose # every run was cancelled counts as a failure. + # + # A run is finished when it has a conclusion. GitHub sets the conclusion + # only when a run completes, but it can leave `status` at "in_progress" + # on a run that has finished: on 2026-11-28 citrate-core's queue entry + # for #917 had a run with conclusion success and completed_at set whose + # status never left in_progress, and the gate waited on it for its full + # 120 minutes (fixed there in citrate-core #950). Keying "pending" on the + # missing conclusion still waits on every run that is queued or really + # running (both have conclusion null). summary='[.check_runs[] | select(.name != "ci" and .name != "secret-scan")] | group_by(.name) | map({name: .[0].name, - pending: (map(select(.status != "completed")) | length), - last: (map(select(.status == "completed" and .conclusion != "cancelled")) + pending: (map(select(.conclusion == null)) | length), + last: (map(select(.conclusion != null and .conclusion != "cancelled")) | sort_by(.completed_at) | last | .conclusion)})' stable=0 while :; do From 17e0a1595103cbb88ff6ce34b58a1f227249785c Mon Sep 17 00:00:00 2001 From: Larry Klosowski Date: Sun, 29 Nov 2026 08:47:53 -0800 Subject: [PATCH 3/3] test(merge-gate): a lone stuck run that succeeded passes (review of #15) Pins the 'last' selector: with main's status == "completed" selector this case fails the gate falsely; every other case passes either way. Signed-off-by: Larry Klosowski Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01FPJzHNqGLdTVoFzde3Sxbw --- scripts/ci/test_merge_gate.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/scripts/ci/test_merge_gate.py b/scripts/ci/test_merge_gate.py index c7d3694..c2a4d97 100644 --- a/scripts/ci/test_merge_gate.py +++ b/scripts/ci/test_merge_gate.py @@ -82,6 +82,11 @@ def test_queued_or_running_run_is_still_waited_on(self): ) self.assertEqual(self.judge([run("rust", "in_progress", None)]), ("", 1)) + def test_a_check_whose_only_run_is_stuck_but_successful_passes(self): + # Review of #15: the latest finished run is chosen by conclusion, not status, so a lone + # stuck run that succeeded is not a false failure. + self.assertEqual(self.judge([run("x", "in_progress", "success")]), ("", 0)) + def test_stuck_runs_failure_still_fails_the_gate(self): self.assertEqual(self.judge([run("lane-guard", "in_progress", "failure")]), ("lane-guard", 0))