Skip to content

armgit / AiSOC

Public Imported from GitHub
0

Open-source AI Security Operations Center: alert fusion, LLM-agent triage, MITRE ATT&CK investigation, and a replayable decision ledger for every agent step. Self-hostable, runs with no API keys, MIT licensed. Ships an MCP server for Claude, Cursor and Continue.

fix(ci): a required check cancelled by the next merge is a gate that does not grade (#1001)

Beenu Arorad46d29a60207
diff --git a/.github/workflows/aisoc-action.yml b/.github/workflows/aisoc-action.yml
index de0cf6fed..addf6900f 100644
--- a/.github/workflows/aisoc-action.yml
+++ b/.github/workflows/aisoc-action.yml
@@ -25,8 +25,14 @@ permissions:
contents: read
concurrency:
- group: aisoc-action-${{ github.ref }}
- cancel-in-progress: true
+ # Supersede a pull request's own earlier runs; never supersede a push.
+ # Only a branch's head commit needs grading, but every commit that lands
+ # on a protected branch must be graded — and `cancel-in-progress: false`
+ # alone does not achieve that, because a run left *pending* in a busy
+ # group is cancelled when the next one queues. Gate:
+ # scripts/check_workflow_concurrency.py
+ group: aisoc-action-${{ github.ref }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.sha }}
+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
build-test:
diff --git a/.github/workflows/aisoc-cli.yml b/.github/workflows/aisoc-cli.yml
index 6d7898e19..e0477f74e 100644
--- a/.github/workflows/aisoc-cli.yml
+++ b/.github/workflows/aisoc-cli.yml
@@ -22,8 +22,14 @@ permissions:
contents: read
concurrency:
- group: aisoc-cli-${{ github.ref }}
- cancel-in-progress: true
+ # Supersede a pull request's own earlier runs; never supersede a push.
+ # Only a branch's head commit needs grading, but every commit that lands
+ # on a protected branch must be graded — and `cancel-in-progress: false`
+ # alone does not achieve that, because a run left *pending* in a busy
+ # group is cancelled when the next one queues. Gate:
+ # scripts/check_workflow_concurrency.py
+ group: aisoc-cli-${{ github.ref }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.sha }}
+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
build-test:
diff --git a/.github/workflows/attribution.yml b/.github/workflows/attribution.yml
index 9da76be48..651ecf1ba 100644
--- a/.github/workflows/attribution.yml
+++ b/.github/workflows/attribution.yml
@@ -32,8 +32,14 @@ permissions:
contents: read
concurrency:
- group: attribution-${{ github.ref }}
- cancel-in-progress: true
+ # Supersede a pull request's own earlier runs; never supersede a push.
+ # Only a branch's head commit needs grading, but every commit that lands
+ # on a protected branch must be graded — and `cancel-in-progress: false`
+ # alone does not achieve that, because a run left *pending* in a busy
+ # group is cancelled when the next one queues. Gate:
+ # scripts/check_workflow_concurrency.py
+ group: attribution-${{ github.ref }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.sha }}
+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
attribution:
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index b0e8e7766..ce23de56b 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -7,8 +7,14 @@ on:
branches: [main, develop]
concurrency:
- group: ${{ github.workflow }}-${{ github.ref }}
- cancel-in-progress: true
+ # Supersede a pull request's own earlier runs; never supersede a push.
+ # Only a branch's head commit needs grading, but every commit that lands
+ # on a protected branch must be graded — and `cancel-in-progress: false`
+ # alone does not achieve that, because a run left *pending* in a busy
+ # group is cancelled when the next one queues. Gate:
+ # scripts/check_workflow_concurrency.py
+ group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.sha }}
+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
# ──────────────────────────────────────────────
diff --git a/.github/workflows/codeql-alert-gate.yml b/.github/workflows/codeql-alert-gate.yml
index 548df6990..a8b0c1894 100644
--- a/.github/workflows/codeql-alert-gate.yml
+++ b/.github/workflows/codeql-alert-gate.yml
@@ -38,8 +38,14 @@ permissions:
contents: read
concurrency:
- group: codeql-alert-gate-${{ github.event_name }}-${{ github.ref }}
- cancel-in-progress: true
+ # Supersede a pull request's own earlier runs; never supersede a push.
+ # Only a branch's head commit needs grading, but every commit that lands
+ # on a protected branch must be graded — and `cancel-in-progress: false`
+ # alone does not achieve that, because a run left *pending* in a busy
+ # group is cancelled when the next one queues. Gate:
+ # scripts/check_workflow_concurrency.py
+ group: codeql-alert-gate-${{ github.event_name }}-${{ github.ref }}-${{ github.event.pull_request.number || github.sha }}
+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
zero-open-alerts:
diff --git a/.github/workflows/comment-paths.yml b/.github/workflows/comment-paths.yml
index 2b548048b..cd2ebdd6e 100644
--- a/.github/workflows/comment-paths.yml
+++ b/.github/workflows/comment-paths.yml
@@ -60,8 +60,14 @@ permissions:
contents: read
concurrency:
- group: comment-paths-${{ github.ref }}
- cancel-in-progress: true
+ # Supersede a pull request's own earlier runs; never supersede a push.
+ # Only a branch's head commit needs grading, but every commit that lands
+ # on a protected branch must be graded — and `cancel-in-progress: false`
+ # alone does not achieve that, because a run left *pending* in a busy
+ # group is cancelled when the next one queues. Gate:
+ # scripts/check_workflow_concurrency.py
+ group: comment-paths-${{ github.ref }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.sha }}
+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
comment-paths:
diff --git a/.github/workflows/competitor-names.yml b/.github/workflows/competitor-names.yml
index d2cea0eeb..2a0f58cb4 100644
--- a/.github/workflows/competitor-names.yml
+++ b/.github/workflows/competitor-names.yml
@@ -27,8 +27,14 @@ permissions:
contents: read
concurrency:
- group: competitor-names-${{ github.ref }}
- cancel-in-progress: true
+ # Supersede a pull request's own earlier runs; never supersede a push.
+ # Only a branch's head commit needs grading, but every commit that lands
+ # on a protected branch must be graded — and `cancel-in-progress: false`
+ # alone does not achieve that, because a run left *pending* in a busy
+ # group is cancelled when the next one queues. Gate:
+ # scripts/check_workflow_concurrency.py
+ group: competitor-names-${{ github.ref }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.sha }}
+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
competitor-names:
diff --git a/.github/workflows/compose-smoke.yml b/.github/workflows/compose-smoke.yml
index c122c035e..1e5d294c7 100644
--- a/.github/workflows/compose-smoke.yml
+++ b/.github/workflows/compose-smoke.yml
@@ -49,8 +49,14 @@ on:
default: main
concurrency:
- group: ${{ github.workflow }}-${{ github.ref }}
- cancel-in-progress: true
+ # Supersede a pull request's own earlier runs; never supersede a push.
+ # Only a branch's head commit needs grading, but every commit that lands
+ # on a protected branch must be graded — and `cancel-in-progress: false`
+ # alone does not achieve that, because a run left *pending* in a busy
+ # group is cancelled when the next one queues. Gate:
+ # scripts/check_workflow_concurrency.py
+ group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.sha }}
+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
smoke:
diff --git a/.github/workflows/devcontainer-build.yml b/.github/workflows/devcontainer-build.yml
index ef40e82a6..73a6a487a 100644
--- a/.github/workflows/devcontainer-build.yml
+++ b/.github/workflows/devcontainer-build.yml
@@ -47,8 +47,14 @@ permissions:
packages: write
concurrency:
- group: devcontainer-build-${{ github.ref }}
- cancel-in-progress: true
+ # Supersede a pull request's own earlier runs; never supersede a push.
+ # Only a branch's head commit needs grading, but every commit that lands
+ # on a protected branch must be graded — and `cancel-in-progress: false`
+ # alone does not achieve that, because a run left *pending* in a busy
+ # group is cancelled when the next one queues. Gate:
+ # scripts/check_workflow_concurrency.py
+ group: devcontainer-build-${{ github.ref }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.sha }}
+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
build:
diff --git a/.github/workflows/devcontainer-coldstart.yml b/.github/workflows/devcontainer-coldstart.yml
index 6165841a3..7b19907df 100644
--- a/.github/workflows/devcontainer-coldstart.yml
+++ b/.github/workflows/devcontainer-coldstart.yml
@@ -62,8 +62,14 @@ permissions:
packages: read
concurrency:
- group: devcontainer-coldstart-${{ github.ref }}
- cancel-in-progress: true
+ # Supersede a pull request's own earlier runs; never supersede a push.
+ # Only a branch's head commit needs grading, but every commit that lands
+ # on a protected branch must be graded — and `cancel-in-progress: false`
+ # alone does not achieve that, because a run left *pending* in a busy
+ # group is cancelled when the next one queues. Gate:
+ # scripts/check_workflow_concurrency.py
+ group: devcontainer-coldstart-${{ github.ref }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.sha }}
+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
cold-start:
diff --git a/.github/workflows/e2e.yml b/.github/workflows/e2e.yml
index 8fd0802ba..5244c5a3b 100644
--- a/.github/workflows/e2e.yml
+++ b/.github/workflows/e2e.yml
@@ -34,8 +34,14 @@ on:
- ".github/workflows/e2e.yml"
concurrency:
- group: e2e-${{ github.workflow }}-${{ github.ref }}
- cancel-in-progress: true
+ # Supersede a pull request's own earlier runs; never supersede a push.
+ # Only a branch's head commit needs grading, but every commit that lands
+ # on a protected branch must be graded — and `cancel-in-progress: false`
+ # alone does not achieve that, because a run left *pending* in a busy
+ # group is cancelled when the next one queues. Gate:
+ # scripts/check_workflow_concurrency.py
+ group: e2e-${{ github.workflow }}-${{ github.ref }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.sha }}
+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
buyer-journey:
diff --git a/.github/workflows/golden-pipeline.yml b/.github/workflows/golden-pipeline.yml
index de01b939f..ad9cb03ea 100644
--- a/.github/workflows/golden-pipeline.yml
+++ b/.github/workflows/golden-pipeline.yml
@@ -23,8 +23,14 @@ permissions:
contents: read
concurrency:
- group: golden-pipeline-${{ github.ref }}
- cancel-in-progress: true
+ # Supersede a pull request's own earlier runs; never supersede a push.
+ # Only a branch's head commit needs grading, but every commit that lands
+ # on a protected branch must be graded — and `cancel-in-progress: false`
+ # alone does not achieve that, because a run left *pending* in a busy
+ # group is cancelled when the next one queues. Gate:
+ # scripts/check_workflow_concurrency.py
+ group: golden-pipeline-${{ github.ref }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.sha }}
+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
golden:
diff --git a/.github/workflows/grading-integrity.yml b/.github/workflows/grading-integrity.yml
new file mode 100644
index 000000000..0c5168ae7
--- /dev/null
+++ b/.github/workflows/grading-integrity.yml
@@ -0,0 +1,107 @@
+name: Grading integrity
+
+# A required status check is a promise that every commit reaching the branch
+# was graded. This workflow is the only thing that checks the promise is kept,
+# and it does so from both ends because they fail differently:
+#
+# concurrency-shape reads the workflow files. Prevention: a concurrency
+# block that can discard a push to a protected branch is
+# rejected before it lands.
+#
+# cancelled-runs asks the Actions API. Detection: a run on `main` that
+# ended `cancelled` is reported as a problem. The static
+# gate cannot see a manual cancel, a runner eviction, a
+# timeout kill, or a concurrency shape it has not
+# learned to read.
+#
+# Why either is needed: a cancelled run reports `cancelled`, not `failure`, so
+# nothing goes red; and a job whose `needs:` dependency was cancelled never
+# starts, so it produces no check run at all. Measured before the fix, of the
+# last 1,000 push-triggered runs on `main`, 259 ended `cancelled` — `ci.yml`
+# alone 36 of 51 — and only 51% of the required-check x commit pairs over the
+# last 30 commits actually graded. Nothing anywhere was red.
+
+on:
+ push:
+ branches: [main]
+ pull_request:
+ branches: [main]
+ schedule:
+ # 05:17 UTC daily. Off the hour because the runner pool is busiest on it,
+ # and a job that queues past its window is a job that did not run.
+ - cron: '17 5 * * *'
+ workflow_dispatch:
+
+permissions:
+ contents: read
+ actions: read
+
+concurrency:
+ # Supersede a pull request's own earlier runs; never supersede a push.
+ # Only a branch's head commit needs grading, but every commit that lands
+ # on a protected branch must be graded — and `cancel-in-progress: false`
+ # alone does not achieve that, because a run left *pending* in a busy
+ # group is cancelled when the next one queues. Gate:
+ # scripts/check_workflow_concurrency.py
+ group: grading-integrity-${{ github.event_name }}-${{ github.event.pull_request.number || github.sha }}
+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}
+
+jobs:
+ concurrency-shape:
+ name: No workflow can discard a push to main
+ runs-on: ubuntu-latest
+ timeout-minutes: 5
+ steps:
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
+ - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
+ with:
+ python-version: '3.11'
+ - run: python -m pip install --quiet pyyaml
+
+ # Self-test first, on the same tree: a gate that has quietly stopped
+ # detecting anything fails here rather than printing OK below.
+ - name: The gate detects the shape it claims to
+ run: python3 scripts/check_workflow_concurrency.py --self-test
+
+ - name: Every workflow keeps its push runs
+ run: python3 scripts/check_workflow_concurrency.py --list
+
+ cancelled-runs:
+ name: No run on main ended cancelled
+ # Only where there is history to read. A pull request cannot change what
+ # already ran on `main`, so asking there would grade the base branch and
+ # block a change that has nothing to do with it.
+ if: github.event_name != 'pull_request'
+ runs-on: ubuntu-latest
+ timeout-minutes: 5
+ steps:
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
+ - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
+ with:
+ python-version: '3.11'
+ - run: python -m pip install --quiet pyyaml
+
+ - name: The gate detects the condition it claims to
+ run: python3 scripts/check_main_run_cancellations.py --self-test
+
+ # No `continue-on-error`. The whole reason this exists is that a
+ # cancellation is already invisible; a gate that shrugs at one would
+ # add a second layer of silence on top of the first.
+ - name: Ask the Actions API what actually ran
+ env:
+ GH_TOKEN: ${{ github.token }}
+ run: python3 scripts/check_main_run_cancellations.py
+
+ gate-self-test:
+ name: The grading-integrity gates still work
+ if: github.event_name == 'pull_request'
+ runs-on: ubuntu-latest
+ timeout-minutes: 5
+ steps:
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7
+ - uses: actions/setup-python@5fda3b95a4ea91299a34e894583c3862153e4b97 # v7.0.0
+ with:
+ python-version: '3.11'
+ - run: python -m pip install --quiet pyyaml
+ - name: The cancellation detector detects a cancellation
+ run: python3 scripts/check_main_run_cancellations.py --self-test
diff --git a/.github/workflows/graph-schema-check.yml b/.github/workflows/graph-schema-check.yml
index 3a58f098b..44ee98047 100644
--- a/.github/workflows/graph-schema-check.yml
+++ b/.github/workflows/graph-schema-check.yml
@@ -28,8 +28,14 @@ on:
- ".github/workflows/graph-schema-check.yml"
concurrency:
- group: ${{ github.workflow }}-${{ github.ref }}
- cancel-in-progress: true
+ # Supersede a pull request's own earlier runs; never supersede a push.
+ # Only a branch's head commit needs grading, but every commit that lands
+ # on a protected branch must be graded — and `cancel-in-progress: false`
+ # alone does not achieve that, because a run left *pending* in a busy
+ # group is cancelled when the next one queues. Gate:
+ # scripts/check_workflow_concurrency.py
+ group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.sha }}
+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
graph-schema-check:
diff --git a/.github/workflows/helm.yml b/.github/workflows/helm.yml
index 672944703..7230d1720 100644
--- a/.github/workflows/helm.yml
+++ b/.github/workflows/helm.yml
@@ -22,8 +22,14 @@ on:
- '.github/workflows/helm.yml'
concurrency:
- group: ${{ github.workflow }}-${{ github.ref }}
- cancel-in-progress: true
+ # Supersede a pull request's own earlier runs; never supersede a push.
+ # Only a branch's head commit needs grading, but every commit that lands
+ # on a protected branch must be graded — and `cancel-in-progress: false`
+ # alone does not achieve that, because a run left *pending* in a busy
+ # group is cancelled when the next one queues. Gate:
+ # scripts/check_workflow_concurrency.py
+ group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.sha }}
+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}
permissions:
contents: read
diff --git a/.github/workflows/hosted-hostname.yml b/.github/workflows/hosted-hostname.yml
index d4193e8fa..624b68496 100644
--- a/.github/workflows/hosted-hostname.yml
+++ b/.github/workflows/hosted-hostname.yml
@@ -24,8 +24,14 @@ permissions:
contents: read
concurrency:
- group: hosted-hostname-${{ github.ref }}
- cancel-in-progress: true
+ # Supersede a pull request's own earlier runs; never supersede a push.
+ # Only a branch's head commit needs grading, but every commit that lands
+ # on a protected branch must be graded — and `cancel-in-progress: false`
+ # alone does not achieve that, because a run left *pending* in a busy
+ # group is cancelled when the next one queues. Gate:
+ # scripts/check_workflow_concurrency.py
+ group: hosted-hostname-${{ github.ref }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.sha }}
+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
hosted-hostname:
diff --git a/.github/workflows/installer-smoke.yml b/.github/workflows/installer-smoke.yml
index e16b82503..a04975d67 100644
--- a/.github/workflows/installer-smoke.yml
+++ b/.github/workflows/installer-smoke.yml
@@ -59,8 +59,14 @@ on:
workflow_dispatch: {}
concurrency:
- group: ${{ github.workflow }}-${{ github.ref }}
- cancel-in-progress: true
+ # Supersede a pull request's own earlier runs; never supersede a push.
+ # Only a branch's head commit needs grading, but every commit that lands
+ # on a protected branch must be graded — and `cancel-in-progress: false`
+ # alone does not achieve that, because a run left *pending* in a busy
+ # group is cancelled when the next one queues. Gate:
+ # scripts/check_workflow_concurrency.py
+ group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.sha }}
+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
lint-bash:
diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml
index 9627e0305..7d1595eee 100644
--- a/.github/workflows/integration.yml
+++ b/.github/workflows/integration.yml
@@ -51,8 +51,14 @@ on:
workflow_dispatch:
concurrency:
- group: integration-${{ github.ref }}
- cancel-in-progress: true
+ # Supersede a pull request's own earlier runs; never supersede a push.
+ # Only a branch's head commit needs grading, but every commit that lands
+ # on a protected branch must be graded — and `cancel-in-progress: false`
+ # alone does not achieve that, because a run left *pending* in a busy
+ # group is cancelled when the next one queues. Gate:
+ # scripts/check_workflow_concurrency.py
+ group: integration-${{ github.ref }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.sha }}
+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}
permissions:
contents: read
diff --git a/.github/workflows/isolation-live.yml b/.github/workflows/isolation-live.yml
index 15f15bd85..debda020f 100644
--- a/.github/workflows/isolation-live.yml
+++ b/.github/workflows/isolation-live.yml
@@ -38,8 +38,14 @@ on:
workflow_dispatch:
concurrency:
- group: isolation-live-${{ github.ref }}
- cancel-in-progress: true
+ # Supersede a pull request's own earlier runs; never supersede a push.
+ # Only a branch's head commit needs grading, but every commit that lands
+ # on a protected branch must be graded — and `cancel-in-progress: false`
+ # alone does not achieve that, because a run left *pending* in a busy
+ # group is cancelled when the next one queues. Gate:
+ # scripts/check_workflow_concurrency.py
+ group: isolation-live-${{ github.ref }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.sha }}
+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}
permissions:
contents: read
diff --git a/.github/workflows/lake-isolation.yml b/.github/workflows/lake-isolation.yml
index 070e21c6c..bd8fec305 100644
--- a/.github/workflows/lake-isolation.yml
+++ b/.github/workflows/lake-isolation.yml
@@ -80,8 +80,14 @@ permissions:
contents: read
concurrency:
- group: lake-isolation-${{ github.ref }}
- cancel-in-progress: true
+ # Supersede a pull request's own earlier runs; never supersede a push.
+ # Only a branch's head commit needs grading, but every commit that lands
+ # on a protected branch must be graded — and `cancel-in-progress: false`
+ # alone does not achieve that, because a run left *pending* in a busy
+ # group is cancelled when the next one queues. Gate:
+ # scripts/check_workflow_concurrency.py
+ group: lake-isolation-${{ github.ref }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.sha }}
+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
pin-agreement:
diff --git a/.github/workflows/link-check.yml b/.github/workflows/link-check.yml
index 301d71957..e5f288551 100644
--- a/.github/workflows/link-check.yml
+++ b/.github/workflows/link-check.yml
@@ -40,8 +40,14 @@ permissions:
contents: read
concurrency:
- group: link-check-${{ github.ref }}
- cancel-in-progress: true
+ # Supersede a pull request's own earlier runs; never supersede a push.
+ # Only a branch's head commit needs grading, but every commit that lands
+ # on a protected branch must be graded — and `cancel-in-progress: false`
+ # alone does not achieve that, because a run left *pending* in a busy
+ # group is cancelled when the next one queues. Gate:
+ # scripts/check_workflow_concurrency.py
+ group: link-check-${{ github.ref }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.sha }}
+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
front-door:
diff --git a/.github/workflows/mobile.yml b/.github/workflows/mobile.yml
index c535c0146..268db22a9 100644
--- a/.github/workflows/mobile.yml
+++ b/.github/workflows/mobile.yml
@@ -19,8 +19,14 @@ on:
- '.github/workflows/mobile.yml'
concurrency:
- group: ${{ github.workflow }}-${{ github.ref }}
- cancel-in-progress: true
+ # Supersede a pull request's own earlier runs; never supersede a push.
+ # Only a branch's head commit needs grading, but every commit that lands
+ # on a protected branch must be graded — and `cancel-in-progress: false`
+ # alone does not achieve that, because a run left *pending* in a busy
+ # group is cancelled when the next one queues. Gate:
+ # scripts/check_workflow_concurrency.py
+ group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.sha }}
+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}
permissions:
contents: read
diff --git a/.github/workflows/papers.yml b/.github/workflows/papers.yml
index 0e25e4d94..8f16651c1 100644
--- a/.github/workflows/papers.yml
+++ b/.github/workflows/papers.yml
@@ -43,8 +43,14 @@ permissions:
contents: read
concurrency:
- group: papers-${{ github.ref }}
- cancel-in-progress: true
+ # Supersede a pull request's own earlier runs; never supersede a push.
+ # Only a branch's head commit needs grading, but every commit that lands
+ # on a protected branch must be graded — and `cancel-in-progress: false`
+ # alone does not achieve that, because a run left *pending* in a busy
+ # group is cancelled when the next one queues. Gate:
+ # scripts/check_workflow_concurrency.py
+ group: papers-${{ github.ref }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.sha }}
+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
render:
diff --git a/.github/workflows/perf.yml b/.github/workflows/perf.yml
index f41f06e9c..d8579b51b 100644
--- a/.github/workflows/perf.yml
+++ b/.github/workflows/perf.yml
@@ -45,8 +45,14 @@ on:
workflow_dispatch:
concurrency:
- group: perf-${{ github.ref }}
- cancel-in-progress: true
+ # Supersede a pull request's own earlier runs; never supersede a push.
+ # Only a branch's head commit needs grading, but every commit that lands
+ # on a protected branch must be graded — and `cancel-in-progress: false`
+ # alone does not achieve that, because a run left *pending* in a busy
+ # group is cancelled when the next one queues. Gate:
+ # scripts/check_workflow_concurrency.py
+ group: perf-${{ github.ref }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.sha }}
+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}
permissions:
contents: read
diff --git a/.github/workflows/publish-images.yml b/.github/workflows/publish-images.yml
index 3a0f04d69..2428ce7a5 100644
--- a/.github/workflows/publish-images.yml
+++ b/.github/workflows/publish-images.yml
@@ -32,7 +32,24 @@ on:
#
# Queuing instead costs runner minutes and can leave `:latest` a few minutes
# behind on a busy day. GitHub runs a concurrency group in order, so the last
-# push still wins the moving tags, and every run completes its push.
+# push still wins the moving tags.
+#
+# It does NOT mean every run completes its push, which this comment used to
+# claim. GitHub cancels a run that is still *pending* in a busy group when a
+# newer one queues, so `cancel-in-progress: false` only guarantees the first
+# and the last of a burst. Measured on 2026-09-28: 13 runs of this workflow on
+# `main` ended `cancelled` with zero jobs — killed before a single step
+# existed. Each of those commits therefore has no `sha-` image.
+#
+# Keeping the shared group is a deliberate trade, not an oversight. A
+# per-commit group would publish every `sha-` tag, but it would also let two
+# runs of 15 images x 2 architectures race for `:latest` and `:main`, and the
+# older run could win. A stale moving tag is what a self-hoster actually
+# pulls; a missing `sha-` tag is not. check_published_images.py — run daily by
+# image-availability.yml, and again by this workflow after its manifests are
+# pushed — is what catches a moving tag that has fallen behind.
+# scripts/check_workflow_concurrency.py records this as an exception with the
+# same reasoning, so the cost stays visible instead of being forgotten.
concurrency:
group: publish-images-${{ github.ref }}
cancel-in-progress: false
diff --git a/.github/workflows/readme-gates.yml b/.github/workflows/readme-gates.yml
index c0af2dc2b..384340880 100644
--- a/.github/workflows/readme-gates.yml
+++ b/.github/workflows/readme-gates.yml
@@ -56,8 +56,14 @@ permissions:
contents: read
concurrency:
- group: readme-gates-${{ github.ref }}
- cancel-in-progress: true
+ # Supersede a pull request's own earlier runs; never supersede a push.
+ # Only a branch's head commit needs grading, but every commit that lands
+ # on a protected branch must be graded — and `cancel-in-progress: false`
+ # alone does not achieve that, because a run left *pending* in a busy
+ # group is cancelled when the next one queues. Gate:
+ # scripts/check_workflow_concurrency.py
+ group: readme-gates-${{ github.ref }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.sha }}
+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
static:
diff --git a/.github/workflows/reproducible-builds.yml b/.github/workflows/reproducible-builds.yml
index f48959392..0572288a3 100644
--- a/.github/workflows/reproducible-builds.yml
+++ b/.github/workflows/reproducible-builds.yml
@@ -56,8 +56,14 @@ permissions:
contents: read
concurrency:
- group: reproducible-builds-${{ github.ref }}
- cancel-in-progress: true
+ # Supersede a pull request's own earlier runs; never supersede a push.
+ # Only a branch's head commit needs grading, but every commit that lands
+ # on a protected branch must be graded — and `cancel-in-progress: false`
+ # alone does not achieve that, because a run left *pending* in a busy
+ # group is cancelled when the next one queues. Gate:
+ # scripts/check_workflow_concurrency.py
+ group: reproducible-builds-${{ github.ref }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.sha }}
+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
pin-agreement:
diff --git a/.github/workflows/terraform-ci.yml b/.github/workflows/terraform-ci.yml
index 8de2cf2ee..0dacbcffe 100644
--- a/.github/workflows/terraform-ci.yml
+++ b/.github/workflows/terraform-ci.yml
@@ -13,8 +13,14 @@ on:
- "infra/helm/**"
concurrency:
- group: ${{ github.workflow }}-${{ github.ref }}
- cancel-in-progress: true
+ # Supersede a pull request's own earlier runs; never supersede a push.
+ # Only a branch's head commit needs grading, but every commit that lands
+ # on a protected branch must be graded — and `cancel-in-progress: false`
+ # alone does not achieve that, because a run left *pending* in a busy
+ # group is cancelled when the next one queues. Gate:
+ # scripts/check_workflow_concurrency.py
+ group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.sha }}
+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
fmt-check:
diff --git a/.github/workflows/upgrade-test.yml b/.github/workflows/upgrade-test.yml
index aedbc2347..51b13f18c 100644
--- a/.github/workflows/upgrade-test.yml
+++ b/.github/workflows/upgrade-test.yml
@@ -46,8 +46,14 @@ on:
workflow_dispatch:
concurrency:
- group: ${{ github.workflow }}-${{ github.ref }}
- cancel-in-progress: true
+ # Supersede a pull request's own earlier runs; never supersede a push.
+ # Only a branch's head commit needs grading, but every commit that lands
+ # on a protected branch must be graded — and `cancel-in-progress: false`
+ # alone does not achieve that, because a run left *pending* in a busy
+ # group is cancelled when the next one queues. Gate:
+ # scripts/check_workflow_concurrency.py
+ group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.sha }}
+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}
permissions:
contents: read
diff --git a/.github/workflows/visual-regression.yml b/.github/workflows/visual-regression.yml
index 47eba3946..df2c29089 100644
--- a/.github/workflows/visual-regression.yml
+++ b/.github/workflows/visual-regression.yml
@@ -47,8 +47,14 @@ on:
# PR keeps running — the visual suite is the slowest job in CI and
# stacking runs wastes minutes.
concurrency:
- group: visual-${{ github.workflow }}-${{ github.ref }}
- cancel-in-progress: true
+ # Supersede a pull request's own earlier runs; never supersede a push.
+ # Only a branch's head commit needs grading, but every commit that lands
+ # on a protected branch must be graded — and `cancel-in-progress: false`
+ # alone does not achieve that, because a run left *pending* in a busy
+ # group is cancelled when the next one queues. Gate:
+ # scripts/check_workflow_concurrency.py
+ group: visual-${{ github.workflow }}-${{ github.ref }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.sha }}
+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}
jobs:
visual:
diff --git a/.security/allowlist.yml b/.security/allowlist.yml
index b59ec8ca7..aca7c032c 100644
--- a/.security/allowlist.yml
+++ b/.security/allowlist.yml
@@ -34,7 +34,17 @@ ceilings:
# first number. That is the whole argument for the ratchet in one run — a
# count nothing compares to anything drifts, and nobody finds out.
semgrep:
- total: 101
+ # 101 -> 102 (error unchanged at 40). The added WARNING is
+ # `dynamic-urllib-use-detected` on scripts/check_main_run_cancellations.py,
+ # which asks the Actions API whether a run on `main` was cancelled. It is
+ # the same finding the identical call in check_codeql_alerts.py already
+ # contributes: both build the URL from a fixed API root and refuse
+ # anything that does not start with it, so the `file://` read the rule
+ # warns about is not reachable — but the rule fires on any non-literal
+ # argument to `urlopen`, and counting it is better than suppressing it.
+ # `entries:` below stays empty on purpose: a ceiling counts a finding, a
+ # suppression hides it.
+ total: 102
error: 40
measured_with: "1.86.0"
checkov:
diff --git a/scripts/check_main_run_cancellations.py b/scripts/check_main_run_cancellations.py
new file mode 100644
index 000000000..10d6432e8
--- /dev/null
+++ b/scripts/check_main_run_cancellations.py
@@ -0,0 +1,356 @@
+#!/usr/bin/env python3
+"""A run on `main` that ends `cancelled` must be visible, not silently accepted.
+
+Why this exists
+---------------
+`check_workflow_concurrency.py` is prevention: it reads the workflow files and
+refuses a concurrency block that can discard a push. This is detection, and
+the two answer different questions. A workflow can be shaped correctly and
+still lose runs — a manual cancel, a runner-pool eviction, a `timeout-minutes`
+kill, an org-level concurrency limit, or a concurrency block added by a future
+change the static gate has not learned to read.
+
+The reason detection is needed at all is that a cancellation is invisible by
+construction:
+
+* the run's conclusion is `cancelled`, not `failure`, so nothing goes red;
+* a job whose `needs:` dependency was cancelled never starts, so it produces
+ no check run at all — absent is quieter still;
+* the commit page shows a green tick from whatever *did* report, beside a
+ commit that was never graded.
+
+Measured on this repository before the concurrency fix: of the last 1,000
+push-triggered runs on `main`, 259 ended `cancelled`, and only 51% of the
+required-check x commit pairs over the last 30 commits actually graded.
+
+What it asserts
+---------------
+No push-triggered run on `main` created after `SINCE` ended `cancelled`,
+except in workflows that record cancellation as a deliberate trade. Those are
+not silently skipped: their counts are printed every run, so an exception that
+starts costing more than it is worth is visible rather than forgotten.
+
+Non-vacuity
+-----------
+"Found nothing" and "asked nothing" print the same word, so:
+
+* the repository under inspection is resolved from the checkout's own `origin`
+ remote, after sentinel files prove the checkout is this repository. Taking
+ it from `GITHUB_REPOSITORY` alone would let the gate render a confident
+ verdict about `beenuar/AiSOC` while standing in a directory holding nothing
+ — the meta-gate probe that copies `scripts/` into an empty repository caught
+ exactly that on the first draft of this file.
+* the unwindowed fetch must return runs. Zero means the token, the repository
+ or the API shape is wrong — that is a failure, not a clean repository.
+* the window is reported separately from the fetch, and a window holding no
+ runs yet says so loudly instead of rendering a verdict it did not earn.
+* `--self-test` drives the classifier over a synthetic run list and asserts it
+ separates cancelled from successful, honours the exception list, and refuses
+ an empty fetch.
+
+Usage:
+ python3 scripts/check_main_run_cancellations.py --self-test
+ GH_TOKEN=... python3 scripts/check_main_run_cancellations.py
+ GH_TOKEN=... python3 scripts/check_main_run_cancellations.py --window-days 7
+"""
+
+from __future__ import annotations
+
+import argparse
+import json
+import os
+import re
+import subprocess
+import sys
+import urllib.error
+import urllib.request
+from datetime import UTC, datetime, timedelta
+from pathlib import Path
+
+sys.path.insert(0, str(Path(__file__).resolve().parent))
+
+from check_workflow_concurrency import EXCEPTIONS
+from gate_toolkit import repo_root
+
+BRANCH = "main"
+API = "https://api.github.com"
+
+#: Files that must exist for a directory to be this repository. Cheap
+#: insurance against rendering a verdict about `beenuar/AiSOC` while standing
+#: in a tree that is not it.
+SENTINELS = (".github/workflows/ci.yml", "scripts/check_workflow_concurrency.py", "CHANGELOG.md")
+
+#: Runs before this are the history the concurrency fix was measured against.
+#: Grading them would make the gate permanently red for a condition that has
+#: already been fixed, which teaches everyone to ignore it — the exact failure
+#: this repository keeps relearning. Moving this date forward to silence a
+#: finding is the one edit that makes the gate worthless.
+SINCE = "2026-09-29T00:00:00Z"
+
+#: Below this many runs in the unwindowed fetch, the query is broken rather
+#: than the repository quiet.
+MIN_RUNS_FETCHED = 20
+
+#: How many runs to fetch. Comfortably more than a busy day.
+FETCH_LIMIT = 300
+
+
+class GateError(RuntimeError):
+ """Raised when the gate cannot trust its own inputs."""
+
+
+def resolve_repository(root: Path) -> str:
+ """`owner/name`, from the checkout being inspected.
+
+ Deliberately not `GITHUB_REPOSITORY` first: that is an ambient string, and
+ a gate that trusts it will happily describe this repository's run history
+ while pointed at a directory containing nothing.
+ """
+ missing = [s for s in SENTINELS if not (root / s).exists()]
+ if missing:
+ raise GateError(f"{root} does not look like the AiSOC repository (missing: {', '.join(missing)})")
+ out = subprocess.run( # noqa: S603 - fixed argv, no shell
+ ["git", "-C", str(root), "remote", "get-url", "origin"],
+ capture_output=True,
+ text=True,
+ check=False,
+ )
+ url = out.stdout.strip()
+ match = re.search(r"[:/]([^/:]+/[^/]+?)(?:\.git)?$", url) if url else None
+ if match:
+ return match.group(1)
+ ambient = os.environ.get("GITHUB_REPOSITORY")
+ if ambient:
+ # Only reachable once the sentinels have already proved the tree, so
+ # this is a detached checkout of the right repository, not a guess.
+ return ambient
+ raise GateError(f"{root} has no `origin` remote and GITHUB_REPOSITORY is unset")
+
+
+def _get(url: str, token: str) -> dict:
+ # Same shape as check_codeql_alerts.py::_get, including the prefix check:
+ # urllib honours `file://`, so a URL that did not come from API is refused
+ # rather than fetched.
+ if not url.startswith(API + "/"):
+ raise GateError(f"refusing to call a non-GitHub URL: {url}")
+ req = urllib.request.Request( # noqa: S310 - refused above unless it is the GitHub API
+ url,
+ headers={
+ "Accept": "application/vnd.github+json",
+ "Authorization": f"Bearer {token}",
+ "X-GitHub-Api-Version": "2022-11-28",
+ "User-Agent": "aisoc-grading-integrity/1.0",
+ },
+ )
+ with urllib.request.urlopen(req, timeout=30) as resp: # noqa: S310
+ return json.loads(resp.read().decode("utf-8"))
+
+
+def fetch_runs(repo: str, token: str, limit: int = FETCH_LIMIT) -> list[dict]:
+ """Push-triggered runs on `main`, newest first."""
+ runs: list[dict] = []
+ page = 1
+ while len(runs) < limit:
+ url = f"{API}/repos/{repo}/actions/runs?branch={BRANCH}&event=push&per_page=100&page={page}"
+ payload = _get(url, token)
+ batch = payload.get("workflow_runs") or []
+ if not batch:
+ break
+ runs.extend(batch)
+ page += 1
+ return runs[:limit]
+
+
+def workflow_file(run: dict) -> str:
+ return (run.get("path") or "").rsplit("/", 1)[-1]
+
+
+def classify(runs: list[dict], since: datetime, exceptions: dict[str, str]) -> tuple[list[dict], dict[str, int], int]:
+ """(unexcused cancellations, excused counts by workflow, runs in window)."""
+ unexcused: list[dict] = []
+ excused: dict[str, int] = {}
+ in_window = 0
+ for run in runs:
+ created = run.get("created_at") or ""
+ try:
+ when = datetime.fromisoformat(created.replace("Z", "+00:00"))
+ except ValueError:
+ continue
+ if when < since:
+ continue
+ in_window += 1
+ if run.get("conclusion") != "cancelled":
+ continue
+ name = workflow_file(run)
+ if name in exceptions:
+ excused[name] = excused.get(name, 0) + 1
+ else:
+ unexcused.append(run)
+ return unexcused, excused, in_window
+
+
+def self_test() -> int:
+ failures: list[str] = []
+ since = datetime(2026, 1, 1, tzinfo=UTC)
+
+ def run(path: str, concl: str, day: int = 2) -> dict:
+ return {
+ "path": f".github/workflows/{path}",
+ "conclusion": concl,
+ "created_at": f"2026-01-{day:02d}T00:00:00Z",
+ "head_sha": "deadbeefcafe",
+ "html_url": "https://example.invalid/run",
+ }
+
+ excepted = next(iter(EXCEPTIONS))
+
+ unexcused, excused, in_window = classify(
+ [
+ run("ci.yml", "success"),
+ run("ci.yml", "cancelled"),
+ run(excepted, "cancelled"),
+ # Before the window: history, deliberately not graded.
+ run("ci.yml", "cancelled", day=1) | {"created_at": "2025-12-31T00:00:00Z"},
+ ],
+ since,
+ EXCEPTIONS,
+ )
+ if len(unexcused) != 1:
+ failures.append(f"classifier found {len(unexcused)} unexcused cancellations, expected 1")
+ if excused.get(excepted) != 1:
+ failures.append(f"classifier did not excuse the recorded exception {excepted}")
+ if in_window != 3:
+ failures.append(f"classifier counted {in_window} runs in window, expected 3")
+
+ # A clean window must be reachable, or every assertion above is satisfied
+ # by a function that always reports a problem.
+ unexcused, _, in_window = classify([run("ci.yml", "success")], since, EXCEPTIONS)
+ if unexcused or in_window != 1:
+ failures.append("classifier reported a problem over a window with one successful run")
+
+ # An empty fetch must not read as clean.
+ if verdict_for(fetched=0, in_window=0, unexcused=[], excused={}) == 0:
+ failures.append("an empty fetch was treated as a clean result")
+ # A populated fetch with a young window is not a failure, but must not be
+ # reported as a graded window either.
+ if verdict_for(fetched=50, in_window=0, unexcused=[], excused={}) != 0:
+ failures.append("a populated fetch with a young window was treated as a failure")
+ if verdict_for(fetched=50, in_window=10, unexcused=[{"x": 1}], excused={}) == 0:
+ failures.append("an unexcused cancellation in the window was treated as clean")
+
+ if failures:
+ print("self-test FAILED:")
+ for line in failures:
+ print(f" - {line}")
+ return 1
+ print(
+ "self-test OK — classifier separates cancelled from successful, honours the "
+ f"{len(EXCEPTIONS)} recorded exception(s), ignores runs before the window, and "
+ "an empty fetch is not a pass"
+ )
+ return 0
+
+
+def verdict_for(*, fetched: int, in_window: int, unexcused: list, excused: dict) -> int:
+ """The exit code, factored out so the self-test can drive it directly."""
+ del excused # counted and printed, never a failure on its own
+ if fetched < MIN_RUNS_FETCHED:
+ return 2
+ if unexcused:
+ return 1
+ if in_window == 0:
+ return 0
+ return 0
+
+
+def main(argv: list[str] | None = None) -> int:
+ parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
+ parser.add_argument("--self-test", action="store_true", help="prove the gate is not vacuous, then exit")
+ parser.add_argument("--repo-root", type=Path, default=None, help="the checkout to inspect (default: git rev-parse)")
+ parser.add_argument(
+ "--window-days",
+ type=int,
+ default=None,
+ help="grade only the last N days instead of everything since SINCE",
+ )
+ args = parser.parse_args(argv)
+
+ if args.self_test:
+ return self_test()
+
+ root = (args.repo_root or repo_root()).resolve()
+ try:
+ repo = resolve_repository(root)
+ except GateError as exc:
+ print(f"FAIL: {exc}")
+ return 2
+
+ token = os.environ.get("GH_TOKEN") or os.environ.get("GITHUB_TOKEN")
+ if not token:
+ # Refusing is the point. A gate that shrugs at a missing credential is
+ # a gate that reports OK on every run once the credential expires.
+ print("FAIL: no GH_TOKEN/GITHUB_TOKEN in the environment — this gate can only")
+ print(" be answered by the Actions API, and skipping would report a clean")
+ print(" result about runs it never read.")
+ return 2
+
+ since = datetime.fromisoformat(SINCE.replace("Z", "+00:00"))
+ if args.window_days is not None:
+ since = max(since, datetime.now(UTC) - timedelta(days=args.window_days))
+
+ try:
+ runs = fetch_runs(repo, token)
+ except (urllib.error.URLError, urllib.error.HTTPError, TimeoutError, ValueError) as exc:
+ print(f"FAIL: could not read the Actions API for {repo}: {exc}")
+ return 2
+
+ unexcused, excused, in_window = classify(runs, since, EXCEPTIONS)
+
+ print(f"checkout {root}")
+ print(f"repository {repo}")
+ print(f"branch {BRANCH} (push-triggered runs only)")
+ print(f"fetched {len(runs)} run(s)")
+ print(f"graded window created at or after {since.isoformat()} — {in_window} run(s)")
+
+ if len(runs) < MIN_RUNS_FETCHED:
+ print(f"\nFAIL: only {len(runs)} run(s) came back (expected >= {MIN_RUNS_FETCHED}).")
+ print(" The query, the token scope or the repository is wrong; an empty")
+ print(" answer is not a quiet repository.")
+ return 2
+
+ if excused:
+ print("\nrecorded exceptions — cancelled, and deliberately so:")
+ for name, count in sorted(excused.items()):
+ print(f" {name}: {count} cancelled run(s)")
+ print(f" {EXCEPTIONS[name].split('.')[0].strip()}.")
+
+ if unexcused:
+ print(f"\nFAIL: {len(unexcused)} run(s) on {BRANCH} ended `cancelled`.\n")
+ for run in unexcused:
+ print(f" {run.get('created_at')} {workflow_file(run):28s} {(run.get('head_sha') or '')[:8]}")
+ print(f" {run.get('html_url')}")
+ print(
+ "\nA cancelled run on a protected branch is a commit nothing graded, and it\n"
+ "reports `cancelled` rather than `failure`, so nothing else will say so.\n"
+ "Find out why it was cancelled. If a concurrency group did it,\n"
+ "scripts/check_workflow_concurrency.py explains the shape and the fix. If\n"
+ "the cancellation is correct, record it in that gate's EXCEPTIONS with the\n"
+ "reason and what is lost."
+ )
+ return 1
+
+ if in_window == 0:
+ print(
+ f"\nNOTICE: no push-triggered run on {BRANCH} has been created since "
+ f"{since.isoformat()} yet.\n"
+ f" The API answered with {len(runs)} run(s), so the query works — the\n"
+ " window is simply young. Nothing has been graded clean here."
+ )
+ return 0
+
+ print(f"\nOK — {in_window} run(s) on {BRANCH} in the window, none cancelled outside the recorded exceptions")
+ return 0
+
+
+if __name__ == "__main__":
+ sys.exit(main())
diff --git a/scripts/check_workflow_concurrency.py b/scripts/check_workflow_concurrency.py
new file mode 100644
index 000000000..680b2914a
--- /dev/null
+++ b/scripts/check_workflow_concurrency.py
@@ -0,0 +1,414 @@
+#!/usr/bin/env python3
+"""No commit that lands on a protected branch may go ungraded.
+
+Why this exists
+---------------
+A required status check is a promise that every commit reaching the branch was
+graded. `ci.yml` broke that promise quietly for months: its concurrency group
+was keyed on `github.ref`, which is the same string for every push to `main`,
+with `cancel-in-progress: true`. During a merge burst each merge killed the
+previous merge's run mid-`Python — Tests`.
+
+Measured on this repository before the fix, over the last 1,000 push-triggered
+runs on `main`: 259 ended `cancelled`. `ci.yml` alone was 36 cancelled out of
+51. Over the last 30 commits on `main`, only 336 of the 660 required-check x
+commit pairs actually graded — 51%.
+
+None of that looked like a problem, and that is the point:
+
+* A cancelled run reports `cancelled`, not `failure`. Nothing turns red.
+* Worse, a job whose `needs:` dependency was cancelled never starts, so it
+ never produces a check run at all. The eight `python-services-wave-2-test`
+ matrix checks did not report on 15 of 30 commits for exactly that reason:
+ their `needs: python-lint` was killed first. A check that is *absent* is
+ even less visible than one that is cancelled.
+* `main` reads green on whatever HEAD happens to be, because HEAD is the one
+ commit nothing superseded.
+
+What this gate asserts
+----------------------
+For every workflow that can be triggered by a push to a protected branch, the
+concurrency configuration must not be able to discard that push's run. Two
+independent ways it can, and both are checked:
+
+`cancel-in-progress`
+ The obvious one. Truthy for a push event means the next push kills this
+ run mid-flight.
+
+a concurrency group shared across commits
+ The non-obvious one, and the reason `cancel-in-progress: false` is not a
+ fix on its own. GitHub's documented behaviour is that when a run queues
+ into a busy group, *any run already pending in that group is cancelled*.
+ So a non-cancelling group still loses every intermediate run on a burst —
+ it only guarantees the first and the last.
+
+ This is not theoretical either. `publish-images.yml` was moved to
+ `cancel-in-progress: false` after a release left twelve images current and
+ the thirteenth two commits behind. On the day this gate was written that
+ workflow still had 13 runs on `main` ending `cancelled`, each with **zero
+ jobs** — cancelled while pending, before a single step existed.
+
+The fix in both cases is the same: key the group so a push never shares one.
+A pull request still supersedes its own earlier runs, because only a branch's
+head commit needs grading:
+
+ group: <prefix>-${{ github.event_name }}-${{ github.event.pull_request.number || github.sha }}
+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}
+
+Bidirectional by construction
+-----------------------------
+The recurring failure in this repository is the one-directional check: it
+compares A against B and never B against A, so drift in the direction things
+actually change slips past while the check prints OK. An exception list keyed
+only on "is this workflow allowed?" has that shape — the entry outlives the
+reason and is there to launder the next regression.
+
+So every exception records the reason, and both directions are errors:
+
+ tree -> exceptions a workflow that can drop a push run with no entry.
+ exceptions -> tree an entry whose workflow is gone, or which no longer
+ has the shape it excuses.
+
+Non-vacuity
+-----------
+A gate that can pass while inspecting nothing is worse than no gate. Three
+guards, all printed:
+
+* the root comes from `git rev-parse` via `gate_toolkit.repo_root`, never from
+ this file's location, and is verified against sentinel paths;
+* the walk must reach a plausible number of workflows, and must still find
+ push-triggered workflows that declare a concurrency group — a scan that
+ matches none means the parse broke, not that the tree is clean;
+* `--self-test` runs the detector over known-bad and known-good workflow
+ documents and asserts it separates them, and asserts the comparison itself
+ rejects an empty scan.
+
+Usage:
+ python3 scripts/check_workflow_concurrency.py --self-test
+ python3 scripts/check_workflow_concurrency.py
+ python3 scripts/check_workflow_concurrency.py --list
+ python3 scripts/check_workflow_concurrency.py --repo-root /path/to/checkout
+"""
+
+from __future__ import annotations
+
+import argparse
+import sys
+from pathlib import Path
+
+import yaml
+
+sys.path.insert(0, str(Path(__file__).resolve().parent))
+
+from gate_toolkit import repo_root
+
+WORKFLOWS_REL = Path(".github/workflows")
+
+#: Branches whose commits are graded by required status checks. A push to any
+#: of these must survive the next push.
+PROTECTED_BRANCHES = frozenset({"main", "develop"})
+
+#: If the walk finds fewer workflow files than this, the glob is broken rather
+#: than the tree small.
+MIN_WORKFLOWS = 20
+
+#: Expressions that evaluate false for a `push` event. Anything else in
+#: `cancel-in-progress` is treated as "can cancel a push", including a bare
+#: `true` and any expression this gate cannot read — guessing in the
+#: permissive direction is how a gate becomes decoration.
+SAFE_CANCEL_EXPRESSIONS = frozenset(
+ {
+ "${{ github.event_name == 'pull_request' }}",
+ '${{ github.event_name == "pull_request" }}',
+ "${{ github.event_name == 'pull_request' || github.event_name == 'pull_request_target' }}",
+ }
+)
+
+#: Context references that make a concurrency group unique to one commit, so
+#: two pushes can never land in the same group and cancel each other.
+PER_COMMIT_KEYS = ("github.sha", "github.run_id", "github.run_number", "github.event.head_commit.id")
+
+#: workflow file -> why it is allowed to drop a push run. Shrink-only, and
+#: verified in both directions: an entry whose workflow no longer has the shape
+#: is a failure, not a harmless leftover.
+EXCEPTIONS: dict[str, str] = {
+ "deploy-docs.yml": (
+ "GitHub Pages accepts one deployment at a time and the newest build is "
+ "the only one anyone wants served. Publishing an older docs build over "
+ "a newer one is the failure here, so a shared 'pages' group that drops "
+ "superseded deploys is the correct behaviour, not a lost grade."
+ ),
+ "publish-images.yml": (
+ "Deliberate, and the cost is recorded rather than hidden. A per-commit "
+ "group would publish every commit's immutable sha- tag, but it would "
+ "also let two multi-architecture runs (15 images x 2 architectures) "
+ "race for the moving ':latest' and ':main' tags, and the older run "
+ "could win. Serialising keeps the newest commit on the moving tags, at "
+ "the cost of intermediate commits not getting a sha- tag. "
+ "check_published_images.py, run daily by image-availability.yml, is "
+ "what catches a moving tag that fell behind."
+ ),
+}
+
+
+class GateError(RuntimeError):
+ """Raised when the gate cannot trust its own inputs."""
+
+
+def _as_mapping(value: object) -> dict:
+ """`on:` is parsed by PyYAML as the boolean True, and may be a str or list."""
+ if isinstance(value, dict):
+ return value
+ if isinstance(value, list):
+ return dict.fromkeys(value)
+ if isinstance(value, str):
+ return {value: None}
+ return {}
+
+
+def triggers_on_protected_push(doc: dict) -> bool:
+ """Can a push to a protected branch start this workflow?
+
+ A `push:` block carrying only `tags:` is a release trigger — every tag is
+ its own ref, so those runs never contend with each other.
+ """
+ on = _as_mapping(doc.get("on", doc.get(True)))
+ if "push" not in on:
+ return False
+ push = _as_mapping(on.get("push"))
+ branches = push.get("branches")
+ if branches is None:
+ # No branch filter: a tags-only trigger is not a branch trigger;
+ # anything else fires on every branch, which includes the protected
+ # ones.
+ return "tags" not in push and "tags-ignore" not in push
+ return any(b in PROTECTED_BRANCHES or b in ("**", "*") for b in branches)
+
+
+def concurrency_of(doc: dict) -> tuple[str | None, object]:
+ conc = doc.get("concurrency")
+ if conc is None:
+ return None, False
+ if isinstance(conc, str):
+ return conc, False
+ return conc.get("group"), conc.get("cancel-in-progress", False)
+
+
+def cancels_a_push(cancel: object) -> bool:
+ """Would `cancel-in-progress` kill an in-flight run on a push event?"""
+ if cancel is False or cancel is None:
+ return False
+ if cancel is True:
+ return True
+ text = " ".join(str(cancel).split())
+ return text not in SAFE_CANCEL_EXPRESSIONS
+
+
+def group_is_per_commit(group: str | None) -> bool:
+ if not group:
+ return False
+ return any(key in group for key in PER_COMMIT_KEYS)
+
+
+def diagnose(doc: dict) -> list[str]:
+ """Ways this workflow can discard a push run. Empty means it cannot."""
+ if not triggers_on_protected_push(doc):
+ return []
+ group, cancel = concurrency_of(doc)
+ if group is None:
+ # No group means no contention: every run stands on its own.
+ return []
+ problems = []
+ if cancels_a_push(cancel):
+ problems.append(f"cancel-in-progress is {cancel!r}, which is truthy for a push")
+ if not group_is_per_commit(group):
+ problems.append(f"group {group!r} is shared across commits, so a run still pending when the next push queues is cancelled")
+ return problems
+
+
+def scan(root: Path) -> tuple[dict[str, list[str]], int, int]:
+ """(workflow -> problems, workflows read, push-triggered with a group)."""
+ wf_dir = root / WORKFLOWS_REL
+ files = sorted(list(wf_dir.glob("*.yml")) + list(wf_dir.glob("*.yaml")))
+ findings: dict[str, list[str]] = {}
+ with_group = 0
+ for path in files:
+ try:
+ doc = yaml.safe_load(path.read_text(encoding="utf-8"))
+ except yaml.YAMLError as exc:
+ raise GateError(f"{path.name} is not parseable YAML: {exc}") from exc
+ if not isinstance(doc, dict):
+ continue
+ if triggers_on_protected_push(doc) and concurrency_of(doc)[0] is not None:
+ with_group += 1
+ problems = diagnose(doc)
+ if problems:
+ findings[path.name] = problems
+ return findings, len(files), with_group
+
+
+def compare(findings: dict[str, list[str]], exceptions: dict[str, str]) -> list[str]:
+ """Both directions. Returns failure lines; empty means clean."""
+ problems: list[str] = []
+ for name, reasons in sorted(findings.items()):
+ if name in exceptions:
+ continue
+ problems.append(f"DROPS A PUSH RUN {name}")
+ for reason in reasons:
+ problems.append(f" - {reason}")
+ for name in sorted(exceptions):
+ if name not in findings:
+ problems.append(
+ f"STALE EXCEPTION {name}: recorded as allowed to drop a push run, "
+ "but it no longer can (or the workflow is gone) — drop the entry"
+ )
+ return problems
+
+
+def self_test() -> int:
+ """Prove the detector separates known-bad from known-good, both ways."""
+ failures: list[str] = []
+
+ shared_cancelling = {
+ "on": {"push": {"branches": ["main"]}},
+ "concurrency": {"group": "x-${{ github.ref }}", "cancel-in-progress": True},
+ }
+ shared_queueing = {
+ "on": {"push": {"branches": ["main"]}},
+ "concurrency": {"group": "x-${{ github.ref }}", "cancel-in-progress": False},
+ }
+ per_commit = {
+ "on": {"push": {"branches": ["main"]}, "pull_request": None},
+ "concurrency": {
+ "group": "x-${{ github.event_name }}-${{ github.event.pull_request.number || github.sha }}",
+ "cancel-in-progress": "${{ github.event_name == 'pull_request' }}",
+ },
+ }
+ no_group = {"on": {"push": {"branches": ["main"]}}}
+ pr_only = {
+ "on": {"pull_request": None},
+ "concurrency": {"group": "x-${{ github.ref }}", "cancel-in-progress": True},
+ }
+ tag_only = {
+ "on": {"push": {"tags": ["v*"]}},
+ "concurrency": {"group": "x-${{ github.ref }}", "cancel-in-progress": True},
+ }
+ unreadable_cancel = {
+ "on": {"push": {"branches": ["main"]}},
+ "concurrency": {
+ "group": "x-${{ github.sha }}",
+ "cancel-in-progress": "${{ inputs.something }}",
+ },
+ }
+
+ cases: list[tuple[str, dict, bool]] = [
+ ("cancelling shared group on main", shared_cancelling, True),
+ # The one a `cancel-in-progress: false` fix leaves behind.
+ ("non-cancelling but shared group on main", shared_queueing, True),
+ ("per-commit group, PR-only cancellation", per_commit, False),
+ ("no concurrency block at all", no_group, False),
+ ("pull_request only", pr_only, False),
+ ("tag push only", tag_only, False),
+ # An expression the gate cannot evaluate must fail closed.
+ ("unreadable cancel-in-progress expression", unreadable_cancel, True),
+ ]
+ for label, doc, should_flag in cases:
+ flagged = bool(diagnose(doc))
+ if flagged != should_flag:
+ verb = "missed" if should_flag else "flagged"
+ failures.append(f"detector {verb} {label}")
+
+ # The shared-group case must be reported for the *sharing*, not only for
+ # the cancel flag — otherwise flipping the flag would silence it.
+ reasons = diagnose(shared_queueing)
+ if not any("shared across commits" in r for r in reasons):
+ failures.append("detector does not report a shared group when cancel-in-progress is false")
+
+ # compare(), both directions.
+ if not compare({"new.yml": ["x"]}, {}):
+ failures.append("compare() passed an unexcused workflow that drops a push run")
+ if not compare({}, {"gone.yml": "stale"}):
+ failures.append("compare() passed an exception with no matching finding")
+ if compare({"a.yml": ["x"]}, {"a.yml": "documented"}):
+ failures.append("compare() failed a workflow with a recorded exception")
+
+ if failures:
+ print("self-test FAILED:")
+ for line in failures:
+ print(f" - {line}")
+ return 1
+
+ print(
+ f"self-test OK — detector separates {len(cases)} known cases, reports a shared "
+ "group independently of cancel-in-progress, and compare() is bidirectional"
+ )
+ return 0
+
+
+def main(argv: list[str] | None = None) -> int:
+ parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
+ parser.add_argument("--repo-root", type=Path, default=None, help="the tree to inspect (default: git rev-parse)")
+ parser.add_argument("--list", action="store_true", help="print every push-triggered workflow and its verdict")
+ parser.add_argument("--self-test", action="store_true", help="prove the gate is not vacuous, then exit")
+ args = parser.parse_args(argv)
+
+ if args.self_test:
+ return self_test()
+
+ root = (args.repo_root or repo_root()).resolve()
+ wf_dir = root / WORKFLOWS_REL
+ if not wf_dir.is_dir():
+ print(f"FAIL: {wf_dir} does not exist — this is not a checkout of the repository")
+ return 2
+
+ try:
+ findings, total, with_group = scan(root)
+ except GateError as exc:
+ print(f"FAIL: {exc}")
+ return 2
+
+ print(f"read {total} workflow file(s) under {wf_dir}")
+ if total < MIN_WORKFLOWS:
+ print(f"FAIL: only {total} workflow(s) found (expected >= {MIN_WORKFLOWS}); the walk is broken, not the tree small")
+ return 2
+ if with_group == 0:
+ print("FAIL: no push-triggered workflow declares a concurrency group.")
+ print(" Every one of them did when this gate was written, so the trigger")
+ print(" parse broke rather than the tree becoming clean.")
+ return 2
+ print(f"{with_group} of them can be started by a push to {'/'.join(sorted(PROTECTED_BRANCHES))} and declare a concurrency group")
+
+ if args.list:
+ for path in sorted((root / WORKFLOWS_REL).glob("*.yml")):
+ doc = yaml.safe_load(path.read_text(encoding="utf-8"))
+ if not isinstance(doc, dict) or not triggers_on_protected_push(doc):
+ continue
+ group, cancel = concurrency_of(doc)
+ verdict = "DROPS" if diagnose(doc) else "keeps"
+ if path.name in EXCEPTIONS and diagnose(doc):
+ verdict = "DROPS (excepted)"
+ print(f" {verdict:16s} {path.name:30s} cancel={cancel!r} group={group!r}")
+
+ problems = compare(findings, EXCEPTIONS)
+ if problems:
+ print(f"\nFAIL: {len(problems)} problem(s).\n")
+ for line in problems:
+ print(f" {line}")
+ print(
+ "\nA push to a protected branch must not be able to lose its run. Key the\n"
+ "group per commit and cancel only pull-request runs:\n\n"
+ " concurrency:\n"
+ " group: <prefix>-${{ github.event_name }}-${{ github.event.pull_request.number || github.sha }}\n"
+ " cancel-in-progress: ${{ github.event_name == 'pull_request' }}\n\n"
+ "If dropping the run is genuinely correct, add an entry to EXCEPTIONS\n"
+ "saying why and what is lost."
+ )
+ return 1
+
+ excused = len(EXCEPTIONS)
+ print(f"OK — no workflow can discard a push to a protected branch ({excused} recorded exception(s), each with a reason)")
+ return 0
+
+
+if __name__ == "__main__":
+ sys.exit(main())
diff --git a/tests/isolation/test_backtest_lake_live.py b/tests/isolation/test_backtest_lake_live.py
index 874a9524a..9c7bbb746 100644
--- a/tests/isolation/test_backtest_lake_live.py
+++ b/tests/isolation/test_backtest_lake_live.py
@@ -94,6 +94,13 @@ API_PACKAGES: tuple[str, ...] = ("app", "app.core", "app.db", "app.services")
#: they have to stay registered while the call runs, not just while it loads.
API_MODULES: tuple[tuple[str, str], ...] = (
("app.core.config", "app/core/config.py"),
+ # `rule_engine` grew a top-level `from app.services.lucene_eval import …`
+ # after this list was written. `app.services` here is a synthetic
+ # namespace holding only what is registered below, so an unlisted sibling
+ # does not resolve — the error reads `'app.services' is not a package`,
+ # which points at the namespace rather than at the missing entry. Anything
+ # a listed module imports at module scope has to be listed above it.
+ ("app.services.lucene_eval", "app/services/lucene_eval.py"),
("app.services.rule_engine", "app/services/rule_engine.py"),
("app.services.lake_sql", "app/services/lake_sql.py"),
("app.db.clickhouse", "app/db/clickhouse.py"),
diff --git a/tests/test_grading_integrity_gates.py b/tests/test_grading_integrity_gates.py
new file mode 100644
index 000000000..81ad71ed0
--- /dev/null
+++ b/tests/test_grading_integrity_gates.py
@@ -0,0 +1,264 @@
+"""Tests for the two grading-integrity gates.
+
+`scripts/check_workflow_concurrency.py` (prevention, reads the workflow files)
+and `scripts/check_main_run_cancellations.py` (detection, reads the Actions
+API).
+
+The property worth asserting is not "is the tree clean today" but "would the
+gate still notice if it stopped being clean". A check that only ever sees good
+input cannot tell a working detector from a dead one, and prints OK either way
+— the dominant failure shape in this repository.
+
+So the pre-fix shape is reconstructed on disk rather than described. Every
+violation these gates were written against was removed by the same change that
+added them, so a test that only ran against the live tree would be asserting
+that zero equals zero. `test_pre_fix_shape_is_rejected` writes the exact block
+`ci.yml` carried before the fix into a copy of the real workflow directory and
+requires a non-zero exit.
+
+`test_non_cancelling_shared_group_is_still_rejected` is the one that matters
+most, because it encodes the thing that was got wrong once already:
+`cancel-in-progress: false` is not a fix. GitHub cancels a run that is still
+*pending* in a busy group when a newer one queues, so a non-cancelling shared
+group still loses every intermediate run of a burst.
+"""
+
+from __future__ import annotations
+
+import importlib.util
+import shutil
+import subprocess
+import sys
+from datetime import UTC, datetime
+from pathlib import Path
+
+import pytest
+
+_REPO = Path(__file__).resolve().parents[1]
+_CONCURRENCY = _REPO / "scripts" / "check_workflow_concurrency.py"
+_CANCELLATIONS = _REPO / "scripts" / "check_main_run_cancellations.py"
+
+# The block ci.yml carried on origin/main at 82f962c4, before the fix.
+_PRE_FIX_BLOCK = """concurrency:
+ group: ${{ github.workflow }}-${{ github.ref }}
+ cancel-in-progress: true
+"""
+
+
+def _load(path: Path):
+ spec = importlib.util.spec_from_file_location(path.stem, path)
+ assert spec is not None and spec.loader is not None, path
+ module = importlib.util.module_from_spec(spec)
+ sys.modules[spec.name] = module
+ spec.loader.exec_module(module)
+ return module
+
+
+conc = _load(_CONCURRENCY)
+cancels = _load(_CANCELLATIONS)
+
+
+def _run(script: Path, *args: str, cwd: Path | None = None) -> subprocess.CompletedProcess:
+ return subprocess.run(
+ [sys.executable, str(script), *args],
+ capture_output=True,
+ text=True,
+ cwd=str(cwd or _REPO),
+ check=False,
+ )
+
+
+def _tree_with_workflows(tmp_path: Path) -> Path:
+ """A git repository holding a copy of the real workflow directory."""
+ root = tmp_path / "tree"
+ (root / ".github").mkdir(parents=True)
+ shutil.copytree(_REPO / ".github/workflows", root / ".github/workflows")
+ subprocess.run(["git", "init", "-q"], cwd=root, check=True)
+ return root
+
+
+# ── prevention: the static shape gate ────────────────────────────────────────
+
+
+def test_self_test_passes():
+ result = _run(_CONCURRENCY, "--self-test")
+ assert result.returncode == 0, result.stdout + result.stderr
+
+
+def test_live_tree_is_clean():
+ result = _run(_CONCURRENCY)
+ assert result.returncode == 0, result.stdout + result.stderr
+
+
+def test_pre_fix_shape_is_rejected(tmp_path: Path):
+ """The exact block ci.yml carried before the fix must fail the gate."""
+ root = _tree_with_workflows(tmp_path)
+ ci = root / ".github/workflows/ci.yml"
+ text = ci.read_text(encoding="utf-8")
+ start = text.index("concurrency:")
+ end = text.index("\njobs:", start)
+ ci.write_text(text[:start] + _PRE_FIX_BLOCK + text[end + 1 :], encoding="utf-8")
+
+ result = _run(_CONCURRENCY, "--repo-root", str(root))
+ assert result.returncode == 1, result.stdout + result.stderr
+ assert "ci.yml" in result.stdout
+ assert "cancel-in-progress" in result.stdout
+
+
+def test_non_cancelling_shared_group_is_still_rejected():
+ """`cancel-in-progress: false` alone does not save a pending run."""
+ doc = {
+ "on": {"push": {"branches": ["main"]}},
+ "concurrency": {"group": "x-${{ github.ref }}", "cancel-in-progress": False},
+ }
+ reasons = conc.diagnose(doc)
+ assert reasons, "a shared group was accepted merely because it does not cancel in progress"
+ assert any("shared across commits" in r for r in reasons)
+
+
+def test_canonical_form_is_accepted():
+ doc = {
+ "on": {"push": {"branches": ["main"]}, "pull_request": {"branches": ["main"]}},
+ "concurrency": {
+ "group": "x-${{ github.event_name }}-${{ github.event.pull_request.number || github.sha }}",
+ "cancel-in-progress": "${{ github.event_name == 'pull_request' }}",
+ },
+ }
+ assert conc.diagnose(doc) == []
+
+
+def test_tag_only_push_is_not_a_branch_trigger():
+ """Every tag is its own ref, so those runs never contend."""
+ doc = {
+ "on": {"push": {"tags": ["cli-v*"]}},
+ "concurrency": {"group": "x-${{ github.ref }}", "cancel-in-progress": True},
+ }
+ assert conc.diagnose(doc) == []
+
+
+def test_unreadable_cancel_expression_fails_closed():
+ doc = {
+ "on": {"push": {"branches": ["main"]}},
+ "concurrency": {
+ "group": "x-${{ github.sha }}",
+ "cancel-in-progress": "${{ vars.SOMETHING }}",
+ },
+ }
+ assert conc.diagnose(doc), "an expression the gate cannot evaluate was assumed safe"
+
+
+def test_exceptions_are_bidirectional():
+ assert conc.compare({"new.yml": ["x"]}, {}), "an unexcused workflow passed"
+ assert conc.compare({}, {"gone.yml": "stale"}), "a stale exception passed"
+ assert conc.compare({"a.yml": ["x"]}, {"a.yml": "documented"}) == []
+
+
+def test_every_recorded_exception_still_has_the_shape():
+ """A reason that has outlived its workflow is an exemption waiting to launder."""
+ findings, _total, _with_group = conc.scan(_REPO)
+ for name in conc.EXCEPTIONS:
+ assert name in findings, f"{name} is excepted but no longer drops a push run"
+
+
+def test_empty_tree_is_not_a_pass(tmp_path: Path):
+ root = tmp_path / "empty"
+ (root / "scripts").mkdir(parents=True)
+ subprocess.run(["git", "init", "-q"], cwd=root, check=True)
+ result = _run(_CONCURRENCY, "--repo-root", str(root))
+ assert result.returncode != 0, result.stdout
+
+
+def test_too_few_workflows_is_not_a_pass(tmp_path: Path):
+ """A broken glob and a clean tree must not print the same word."""
+ root = tmp_path / "thin"
+ wf = root / ".github/workflows"
+ wf.mkdir(parents=True)
+ (wf / "only.yml").write_text("on: {push: {branches: [main]}}\njobs: {}\n", encoding="utf-8")
+ subprocess.run(["git", "init", "-q"], cwd=root, check=True)
+ result = _run(_CONCURRENCY, "--repo-root", str(root))
+ assert result.returncode == 2, result.stdout
+
+
+# ── detection: the cancelled-run gate ────────────────────────────────────────
+
+
+def _run_record(path: str, conclusion: str, created: str) -> dict:
+ return {
+ "path": f".github/workflows/{path}",
+ "conclusion": conclusion,
+ "created_at": created,
+ "head_sha": "0123456789ab",
+ "html_url": "https://example.invalid/run",
+ }
+
+
+def test_cancellation_self_test_passes():
+ result = _run(_CANCELLATIONS, "--self-test")
+ assert result.returncode == 0, result.stdout + result.stderr
+
+
+def test_cancelled_run_is_reported():
+ since = datetime(2026, 1, 1, tzinfo=UTC)
+ unexcused, _excused, in_window = cancels.classify(
+ [
+ _run_record("ci.yml", "success", "2026-01-02T00:00:00Z"),
+ _run_record("ci.yml", "cancelled", "2026-01-02T01:00:00Z"),
+ ],
+ since,
+ {},
+ )
+ assert len(unexcused) == 1
+ assert in_window == 2
+
+
+def test_runs_before_the_window_are_history_not_findings():
+ since = datetime(2026, 1, 1, tzinfo=UTC)
+ unexcused, _excused, in_window = cancels.classify([_run_record("ci.yml", "cancelled", "2025-12-31T23:59:00Z")], since, {})
+ assert unexcused == []
+ assert in_window == 0
+
+
+def test_recorded_exception_is_counted_not_failed():
+ since = datetime(2026, 1, 1, tzinfo=UTC)
+ name = next(iter(conc.EXCEPTIONS))
+ unexcused, excused, _ = cancels.classify([_run_record(name, "cancelled", "2026-01-02T00:00:00Z")], since, conc.EXCEPTIONS)
+ assert unexcused == []
+ assert excused == {name: 1}
+
+
+def test_empty_fetch_is_not_a_pass():
+ assert cancels.verdict_for(fetched=0, in_window=0, unexcused=[], excused={}) != 0
+
+
+def test_young_window_is_not_a_failure():
+ assert cancels.verdict_for(fetched=200, in_window=0, unexcused=[], excused={}) == 0
+
+
+def test_missing_token_refuses_rather_than_skips(monkeypatch: pytest.MonkeyPatch):
+ monkeypatch.delenv("GH_TOKEN", raising=False)
+ monkeypatch.delenv("GITHUB_TOKEN", raising=False)
+ assert cancels.main([]) == 2
+
+
+def test_empty_tree_refuses_even_with_a_token_and_an_ambient_repository(tmp_path: Path):
+ """The verdict is about a repository, so the tree has to prove it is that one.
+
+ The first draft read `GITHUB_REPOSITORY` and would happily have reported
+ this repository's run history from a directory holding nothing — which the
+ gate-contract probe caught.
+ """
+ root = tmp_path / "empty"
+ (root / "scripts").mkdir(parents=True)
+ subprocess.run(["git", "init", "-q"], cwd=root, check=True)
+ with pytest.raises(cancels.GateError):
+ cancels.resolve_repository(root)
+
+
+def test_a_real_checkout_resolves_to_this_repository():
+ """The paired direction: refusing everything would satisfy the test above."""
+ assert cancels.resolve_repository(_REPO).lower().endswith("/aisoc")
+
+
+def test_the_two_gates_share_one_exception_list():
+ """Two lists drift the first time either learns something the other has not."""
+ assert cancels.EXCEPTIONS is conc.EXCEPTIONS