Multiplayer agent harness for work.
Reuse successful CI for exact tested trees (#1777)
Regan Bellb665b4915756
diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.ymlindex 2aacac471..ec9ba13cb 100644--- a/.github/workflows/cicd.yml+++ b/.github/workflows/cicd.yml@@ -6,49 +6,96 @@ on:pull_request:workflow_dispatch:+permissions:+ contents: read+concurrency:group: cicd-${{ github.ref }}cancel-in-progress: ${{ github.event_name == 'pull_request' }}jobs:+ reuse:+ name: Resolve exact tested tree+ timeout-minutes: 3+ continue-on-error: true+ if: github.event_name == 'push' && github.ref == 'refs/heads/main'+ runs-on: ubuntu-latest+ permissions:+ contents: read+ actions: read+ pull-requests: read+ outputs:+ reusable: ${{ steps.proof.outcome == 'success' && steps.proof.outputs.reusable == 'true' }}+ steps:+ - uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd+ with:+ persist-credentials: false+ - uses: actions/setup-node@a0853c24544627f65ddf259abe73b1d18a591444+ with:+ node-version-file: .node-version+ - name: Verify exact successful PR tree+ id: proof+ env:+ GITHUB_TOKEN: ${{ github.token }}+ run: node scripts/reuse-ci.ts+core-typecheck:name: Core typecheck+ needs: reuse+ if: always() && !cancelled()runs-on: ubuntu-lateststeps:+ - name: Reuse the exact successful PR tree+ if: needs.reuse.outputs.reusable == 'true'+ run: echo "Full checks already passed for this exact tree; see Resolve exact tested tree."- uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd+ if: needs.reuse.outputs.reusable != 'true'- uses: actions/setup-node@a0853c24544627f65ddf259abe73b1d18a591444+ if: needs.reuse.outputs.reusable != 'true'with:node-version-file: .node-versioncache: npmcache-dependency-path: package-lock.json- name: Install and typecheck+ if: needs.reuse.outputs.reusable != 'true'run: |npm cinpm run typecheck- name: Desktop sign-in tests+ if: needs.reuse.outputs.reusable != 'true'run: |npm ci --prefix desktop --ignore-scriptsnpm test --prefix desktopcore-tests:name: Core tests (${{ matrix.shard }}/5)+ needs: reuse+ if: always() && !cancelled()runs-on: ubuntu-lateststrategy:fail-fast: falsematrix:shard: [1, 2, 3, 4, 5]steps:+ - name: Reuse the exact successful PR tree+ if: needs.reuse.outputs.reusable == 'true'+ run: echo "Full checks already passed for this exact tree; see Resolve exact tested tree."- uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd+ if: needs.reuse.outputs.reusable != 'true'- uses: actions/setup-node@a0853c24544627f65ddf259abe73b1d18a591444+ if: needs.reuse.outputs.reusable != 'true'with:node-version-file: .node-versioncache: npmcache-dependency-path: package-lock.json- name: Install+ if: needs.reuse.outputs.reusable != 'true'run: npm ci- name: Build connector SDK+ if: needs.reuse.outputs.reusable != 'true'run: npm run build:connector-sdk- name: Install Helm for chart tests+ if: needs.reuse.outputs.reusable != 'true'run: |cd "$(mktemp -d)"curl -fsSLO https://get.helm.sh/helm-v3.19.0-linux-amd64.tar.gz@@ -57,29 +104,41 @@ jobs:tar -xzf helm-v3.19.0-linux-amd64.tar.gzsudo install linux-amd64/helm /usr/local/bin/helm- name: Verify root test shard plan- if: matrix.shard == 1+ if: needs.reuse.outputs.reusable != 'true' && matrix.shard == 1run: npm run test:root:shard:check- name: Root tests+ if: needs.reuse.outputs.reusable != 'true'env:CORE_TEST_SHARD: ${{ matrix.shard }}/5run: npm run test:root:shardcore:name: Core+ if: always() && !cancelled()runs-on: ubuntu-latestneeds:- core-typecheck- core-testssteps:- name: Core checks passed- run: echo "Core typecheck and root test shards passed."+ env:+ TYPECHECK: ${{ needs.core-typecheck.result }}+ TESTS: ${{ needs.core-tests.result }}+ run: test "$TYPECHECK" = success && test "$TESTS" = successcli:name: CLI+ needs: reuse+ if: always() && !cancelled()runs-on: ubuntu-lateststeps:+ - name: Reuse the exact successful PR tree+ if: needs.reuse.outputs.reusable == 'true'+ run: echo "Full checks already passed for this exact tree; see Resolve exact tested tree."- uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd+ if: needs.reuse.outputs.reusable != 'true'- uses: actions/setup-node@a0853c24544627f65ddf259abe73b1d18a591444+ if: needs.reuse.outputs.reusable != 'true'with:node-version-file: .node-versioncache: npm@@ -87,10 +146,12 @@ jobs:cli/package-lock.jsonpackage-lock.json- uses: hashicorp/setup-terraform@b9cd54a3c349d3f38e8881555d616ced269862dd+ if: needs.reuse.outputs.reusable != 'true'with:terraform_version: 1.15.8terraform_wrapper: false- name: Install, typecheck, unit + packaged-artifact + e2e tests+ if: needs.reuse.outputs.reusable != 'true'working-directory: clienv:QM_TERRAFORM_BIN: terraform@@ -101,11 +162,14 @@ jobs:npm testnpm run test:e2e- name: Install root dependencies+ if: needs.reuse.outputs.reusable != 'true'run: npm ci- name: Build the CLI package+ if: needs.reuse.outputs.reusable != 'true'working-directory: clirun: npm run build- name: Deployment stack contracts+ if: needs.reuse.outputs.reusable != 'true'run: |npm run typecheck:contractnode --test "deploy/stacks/*/test/*.test.ts"@@ -146,10 +210,17 @@ jobs:lint:name: Lint+ needs: reuse+ if: always() && !cancelled()runs-on: ubuntu-lateststeps:+ - name: Reuse the exact successful PR tree+ if: needs.reuse.outputs.reusable == 'true'+ run: echo "Full checks already passed for this exact tree; see Resolve exact tested tree."- uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd+ if: needs.reuse.outputs.reusable != 'true'- uses: actions/setup-node@a0853c24544627f65ddf259abe73b1d18a591444+ if: needs.reuse.outputs.reusable != 'true'with:node-version-file: .node-versioncache: npm@@ -157,21 +228,28 @@ jobs:package-lock.jsonplugins/web-ui/package-lock.json- name: Install+ if: needs.reuse.outputs.reusable != 'true'run: |npm cinpm ci --prefix plugins/web-uinpm ci --prefix desktop --ignore-scripts- name: Formatting+ if: needs.reuse.outputs.reusable != 'true'run: npm run format:check- name: Lint whole repo, incl. plugins+ if: needs.reuse.outputs.reusable != 'true'run: npm run lint- name: Dead code (knip)+ if: needs.reuse.outputs.reusable != 'true'run: npm run lint:knip- name: Oxlint+ if: needs.reuse.outputs.reusable != 'true'run: npm run lint:oxcore-postgres:name: Core Postgres tests+ needs: reuse+ if: always() && !cancelled()runs-on: ubuntu-latestservices:postgres:@@ -187,29 +265,40 @@ jobs:--health-timeout 5s--health-retries 5steps:+ - name: Reuse the exact successful PR tree+ if: needs.reuse.outputs.reusable == 'true'+ run: echo "Full checks already passed for this exact tree; see Resolve exact tested tree."- uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd+ if: needs.reuse.outputs.reusable != 'true'- uses: actions/setup-node@a0853c24544627f65ddf259abe73b1d18a591444+ if: needs.reuse.outputs.reusable != 'true'with:node-version-file: .node-versioncache: npmcache-dependency-path: package-lock.json- name: Install+ if: needs.reuse.outputs.reusable != 'true'run: npm ci- name: Build connector SDK+ if: needs.reuse.outputs.reusable != 'true'run: npm run build:connector-sdk- name: Postgres-backed tests (durability + cross-process)+ if: needs.reuse.outputs.reusable != 'true'env:DATABASE_URL: postgres://postgres:postgres@localhost:5432/qmrun: npm run test:pg- name: Swarm HTTP runtime tests+ if: needs.reuse.outputs.reusable != 'true'env:SWARM_TEST_DATABASE_URL: postgres://postgres:postgres@localhost:5432/qmrun: node --experimental-test-module-mocks --test test/swarm-orchestrator.test.ts- name: Model registry cross-process tests+ if: needs.reuse.outputs.reusable != 'true'env:MODEL_OVERLAY_TEST_DATABASE_URL: postgres://postgres:postgres@localhost:5432/qmrun: node --experimental-test-module-mocks --test test/model-overlay-postgres.test.ts- name: Memorable provider e2e (real CLI against Postgres)+ if: needs.reuse.outputs.reusable != 'true'env:MEMORABLE_E2E_DB_URL: postgres://postgres:postgres@localhost:5432/qmMEMORABLE_E2E_BIN: node node_modules/memorable-cli/dist/cli.js@@ -220,29 +309,45 @@ jobs:admin-plugin:name: Admin plugin+ needs: reuse+ if: always() && !cancelled()runs-on: ubuntu-lateststeps:+ - name: Reuse the exact successful PR tree+ if: needs.reuse.outputs.reusable == 'true'+ run: echo "Full checks already passed for this exact tree; see Resolve exact tested tree."- uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd+ if: needs.reuse.outputs.reusable != 'true'- uses: actions/setup-node@a0853c24544627f65ddf259abe73b1d18a591444+ if: needs.reuse.outputs.reusable != 'true'with:node-version-file: .node-versioncache: npmcache-dependency-path: plugins/admin/package-lock.json- name: Install, typecheck, test+ if: needs.reuse.outputs.reusable != 'true'working-directory: plugins/adminrun: |npm cinpm run typechecknpm test- name: Build and boot production image+ if: needs.reuse.outputs.reusable != 'true'run: bash scripts/smoke-surface-image.sh adminweb-ui-plugin:name: Web UI plugin+ needs: reuse+ if: always() && !cancelled()runs-on: ubuntu-lateststeps:+ - name: Reuse the exact successful PR tree+ if: needs.reuse.outputs.reusable == 'true'+ run: echo "Full checks already passed for this exact tree; see Resolve exact tested tree."- uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd+ if: needs.reuse.outputs.reusable != 'true'- uses: actions/setup-node@a0853c24544627f65ddf259abe73b1d18a591444+ if: needs.reuse.outputs.reusable != 'true'with:node-version-file: .node-versioncache: npm@@ -251,10 +356,12 @@ jobs:plugins/web-ui/package-lock.jsonplugins/admin/package-lock.json- name: Install core and mounted admin dependencies+ if: needs.reuse.outputs.reusable != 'true'run: |npm cinpm ci --prefix plugins/admin- name: Install, typecheck, test, build+ if: needs.reuse.outputs.reusable != 'true'working-directory: plugins/web-uirun: |npm ci@@ -262,44 +369,94 @@ jobs:npm testnpm run build- name: Build and boot production image+ if: needs.reuse.outputs.reusable != 'true'run: bash scripts/smoke-surface-image.sh web-uiauth-plugin:name: Auth plugin+ needs: reuse+ if: always() && !cancelled()runs-on: ubuntu-lateststeps:+ - name: Reuse the exact successful PR tree+ if: needs.reuse.outputs.reusable == 'true'+ run: echo "Full checks already passed for this exact tree; see Resolve exact tested tree."- uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd+ if: needs.reuse.outputs.reusable != 'true'- uses: actions/setup-node@a0853c24544627f65ddf259abe73b1d18a591444+ if: needs.reuse.outputs.reusable != 'true'with:node-version-file: .node-versioncache: npmcache-dependency-path: plugins/auth/package-lock.json- name: Install, typecheck, test+ if: needs.reuse.outputs.reusable != 'true'working-directory: plugins/authrun: |npm cinpm run typechecknpm test- name: Build and boot production image+ if: needs.reuse.outputs.reusable != 'true'run: bash scripts/smoke-surface-image.sh authportal-plugin:name: Portal plugin+ needs: reuse+ if: always() && !cancelled()runs-on: ubuntu-lateststeps:+ - name: Reuse the exact successful PR tree+ if: needs.reuse.outputs.reusable == 'true'+ run: echo "Full checks already passed for this exact tree; see Resolve exact tested tree."- uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd+ if: needs.reuse.outputs.reusable != 'true'- uses: actions/setup-node@a0853c24544627f65ddf259abe73b1d18a591444+ if: needs.reuse.outputs.reusable != 'true'with:node-version-file: .node-versioncache: npmcache-dependency-path: plugins/portal/package-lock.json- name: Install embedded auth dependencies+ if: needs.reuse.outputs.reusable != 'true'run: npm ci --prefix plugins/auth- name: Install, typecheck, test+ if: needs.reuse.outputs.reusable != 'true'working-directory: plugins/portalrun: |npm cinpm run typechecknpm test- name: Build and boot production image+ if: needs.reuse.outputs.reusable != 'true'run: bash scripts/smoke-surface-image.sh portal++ certify:+ name: Certify tested tree+ timeout-minutes: 3+ continue-on-error: true+ if: always() && !cancelled() && github.event_name == 'pull_request'+ needs: [core, cli, lint, core-postgres, admin-plugin, web-ui-plugin, auth-plugin, portal-plugin, coauthor-trailers]+ runs-on: ubuntu-latest+ permissions:+ contents: read+ steps:+ - name: Validate receipt eligibility+ env:+ PR_REPOSITORY_ID: ${{ github.event.pull_request.head.repo.id }}+ CHECKS: ${{ toJSON(needs) }}+ run: |+ test -n "$PR_REPOSITORY_ID"+ test "$PR_REPOSITORY_ID" = "$GITHUB_REPOSITORY_ID"+ node -e 'const checks = JSON.parse(process.env.CHECKS); for (const id of ["core", "cli", "lint", "core-postgres", "admin-plugin", "web-ui-plugin", "auth-plugin", "portal-plugin", "coauthor-trailers"]) { if (checks[id]?.result !== "success") throw new Error(`${id} did not pass`); }'+ - name: Checkout tested commit+ uses: actions/checkout@93cb6efe18208431cddfb8368fd83d5badbf9bfd+ with:+ ref: ${{ github.sha }}+ persist-credentials: false+ - name: Record the immutable tested tree+ env:+ PR_NUMBER: ${{ github.event.pull_request.number }}+ run: |+ test "$(git rev-parse HEAD)" = "$GITHUB_SHA"+ printf 'QM_CI_TREE={"sha":"%s","tree":"%s","runId":%s,"attempt":%s,"pr":%s}\n' "$GITHUB_SHA" "$(git rev-parse HEAD^{tree})" "$GITHUB_RUN_ID" "$GITHUB_RUN_ATTEMPT" "$PR_NUMBER"diff --git a/scripts/reuse-ci.ts b/scripts/reuse-ci.tsnew file mode 100644index 000000000..874a97c6f--- /dev/null+++ b/scripts/reuse-ci.ts@@ -0,0 +1,196 @@+import assert from "node:assert/strict";+import { appendFileSync } from "node:fs";+import { pathToFileURL } from "node:url";++export const CI_JOBS = [+ "Core typecheck",+ ...Array.from({ length: 5 }, (_, i) => `Core tests (${i + 1}/5)`),+ "Core",+ "CLI",+ "Lint",+ "Core Postgres tests",+ "Admin plugin",+ "Web UI plugin",+ "Auth plugin",+ "Portal plugin",+ "Co-author trailers",+];+const success = (item: any) => item?.status === "completed" && item.conclusion === "success";+const sha = (value: unknown) => assert.match(String(value), /^[a-f0-9]{40}$/);+const workflow = ".github/workflows/cicd.yml";+const codeql = "dynamic/github-code-scanning/codeql";++type Api = (path: string) => Promise<any>;++export function readReceipt(log: string) {+ const receipts = [...log.matchAll(/^\S+ QM_CI_TREE=(\{[^\r\n]+\})\r?$/gm)];+ assert.equal(receipts.length, 1);+ const proof = JSON.parse(receipts[0]![1]!);+ sha(proof.sha);+ sha(proof.tree);+ assert.ok(Number.isSafeInteger(proof.runId) && Number.isSafeInteger(proof.attempt));+ return proof;+}++export async function reusableCi(+ api: Api,+ log: (id: number) => Promise<string>,+ repository: string,+ release: string,+ now = Date.now(),+) {+ sha(release);+ const commit = await api(`commits/${release}`);+ if (commit.parents.length !== 1) return;+ const base = commit.parents[0].sha;+ const pulls = await api(`commits/${release}/pulls?per_page=100`);+ const matches = pulls.filter((pull: any) => pull.merged_at && pull.merge_commit_sha === release);+ if (matches.length !== 1) return;+ const pull = await api(`pulls/${matches[0].number}`);+ if (+ !pull.merged ||+ pull.state !== "closed" ||+ pull.merge_commit_sha !== release ||+ pull.base.ref !== "main" ||+ pull.head.repo?.full_name !== repository ||+ pull.base.repo?.full_name !== repository+ )+ return;+ sha(pull.head.sha);+ for (const path of [workflow, "scripts/reuse-ci.ts"]) {+ const [before, after] = await Promise.all([base, release].map((ref) => api(`contents/${path}?ref=${ref}`)));+ if (before.type !== "file" || before.sha !== after.sha) return;+ }+ const page = await api(`actions/workflows/cicd.yml/runs?event=pull_request&head_sha=${pull.head.sha}&per_page=100`);+ if (page.total_count > 100) return;+ const run = page.workflow_runs+ .filter((run: any) => run.head_branch === pull.head.ref)+ .sort((a: any, b: any) => b.run_number - a.run_number || b.run_attempt - a.run_attempt)[0];+ if (+ !run ||+ !success(run) ||+ run.event !== "pull_request" ||+ run.path !== workflow ||+ run.head_sha !== pull.head.sha ||+ run.head_repository?.full_name !== repository ||+ run.repository?.full_name !== repository ||+ !Number.isSafeInteger(run.id) ||+ !Number.isSafeInteger(run.run_attempt) ||+ run.run_attempt < 1+ )+ return;+ const age = now - Date.parse(run.created_at);+ if (!Number.isFinite(age) || age < 0 || age > 24 * 60 * 60_000) return;+ const jobs = await api(`actions/runs/${run.id}/attempts/${run.run_attempt}/jobs?per_page=100`);+ if (jobs.total_count > 100) return;+ for (const name of [...CI_JOBS, "Certify tested tree"]) {+ const matches = jobs.jobs.filter((job: any) => job.name === name);+ if (+ matches.length !== 1 ||+ !success(matches[0]) ||+ matches[0].run_id !== run.id ||+ matches[0].run_attempt !== run.run_attempt ||+ matches[0].head_sha !== run.head_sha+ )+ return;+ }+ const receiptJob = jobs.jobs.find((job: any) => job.name === "Certify tested tree");+ for (const name of ["Validate receipt eligibility", "Checkout tested commit", "Record the immutable tested tree"]) {+ const steps = receiptJob.steps.filter((step: any) => step.name === name);+ if (steps.length !== 1 || !success(steps[0])) return;+ }+ assert.ok(Number.isSafeInteger(receiptJob.id));+ const receipt = readReceipt(await log(receiptJob.id));+ if (receipt.runId !== run.id || receipt.attempt !== run.run_attempt || receipt.pr !== pull.number) return;+ const tested = await api(`commits/${receipt.sha}`);+ if (+ tested.sha !== receipt.sha ||+ tested.commit.tree.sha !== receipt.tree ||+ tested.commit.tree.sha !== commit.commit.tree.sha ||+ tested.parents.length !== 2 ||+ tested.parents[0].sha !== base ||+ tested.parents[1].sha !== pull.head.sha+ )+ return;+ const scans = await api(`actions/runs?event=dynamic&head_sha=${pull.head.sha}&per_page=100`);+ if (scans.total_count > 100) return;+ const scan = scans.workflow_runs+ .filter((scan: any) => scan.path === codeql && scan.head_branch === `refs/pull/${pull.number}/head`)+ .sort((a: any, b: any) => b.run_number - a.run_number || b.run_attempt - a.run_attempt)[0];+ if (+ !scan ||+ !success(scan) ||+ scan.head_sha !== pull.head.sha ||+ scan.repository?.full_name !== repository ||+ !Number.isSafeInteger(scan.id) ||+ !Number.isSafeInteger(scan.run_attempt)+ )+ return;+ const fresh = await api(`actions/runs/${run.id}`);+ if (!success(fresh) || fresh.run_attempt !== run.run_attempt || fresh.head_sha !== run.head_sha) return;+ return {+ sha: release,+ tree: tested.commit.tree.sha,+ pr: pull.number,+ testedSha: tested.sha,+ ci: { runId: run.id, attempt: run.run_attempt },+ codeql: { runId: scan.id, attempt: scan.run_attempt },+ };+}++async function main() {+ const { GITHUB_TOKEN, GITHUB_REPOSITORY, GITHUB_SHA, GITHUB_OUTPUT, GITHUB_STEP_SUMMARY } = process.env;+ assert.ok(GITHUB_REPOSITORY && GITHUB_SHA && GITHUB_TOKEN && GITHUB_OUTPUT);+ const headers = {+ Authorization: `Bearer ${GITHUB_TOKEN}`,+ Accept: "application/vnd.github+json",+ "X-GitHub-Api-Version": "2022-11-28",+ };+ const url = (path: string) => `https://api.github.com/repos/${GITHUB_REPOSITORY}/${path}`;+ let proof;+ try {+ proof = await reusableCi(+ async (path) => {+ const response = await fetch(url(path), {+ headers,+ signal: AbortSignal.timeout(15_000),+ });+ if (!response.ok) throw new Error(`CI provenance unavailable (${response.status})`);+ return response.json();+ },+ async (id) => {+ const redirect = await fetch(url(`actions/jobs/${id}/logs`), {+ headers,+ redirect: "manual",+ signal: AbortSignal.timeout(15_000),+ });+ assert.equal(redirect.status, 302);+ const location = new URL(redirect.headers.get("location")!);+ assert.equal(location.protocol, "https:");+ const response = await fetch(location, { redirect: "error", signal: AbortSignal.timeout(15_000) });+ assert.ok(response.ok && response.body);+ let text = "";+ for await (const chunk of response.body) {+ text += Buffer.from(chunk).toString("utf8");+ assert.ok(text.length < 128 * 1024);+ }+ return text;+ },+ GITHUB_REPOSITORY,+ GITHUB_SHA,+ );+ } catch (error) {+ console.log(`No reusable CI proof: ${error instanceof Error ? error.message : String(error)}`);+ }+ if (proof) console.log(`QM_CI_REUSE=${JSON.stringify(proof)}`);+ appendFileSync(GITHUB_OUTPUT, `reusable=${Boolean(proof)}\n`);+ if (GITHUB_STEP_SUMMARY)+ appendFileSync(+ GITHUB_STEP_SUMMARY,+ proof+ ? `Reusing full CI from https://github.com/${GITHUB_REPOSITORY}/actions/runs/${proof.ci.runId}/attempts/${proof.ci.attempt} for identical tree ${proof.tree}.\n`+ : "No exact trusted PR tree proof; running full CI.\n",+ );+}++if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) await main();diff --git a/test/ci-reuse.test.ts b/test/ci-reuse.test.tsnew file mode 100644index 000000000..863ae1bff--- /dev/null+++ b/test/ci-reuse.test.ts@@ -0,0 +1,375 @@+import assert from "node:assert/strict";+import { spawnSync } from "node:child_process";+import { readFileSync } from "node:fs";+import test from "node:test";+import { CI_JOBS, readReceipt, reusableCi } from "../scripts/reuse-ci.ts";++const repository = "example/qm";+const release = "a".repeat(40),+ head = "b".repeat(40),+ base = "c".repeat(40),+ tested = "d".repeat(40),+ tree = "e".repeat(40);+const now = Date.parse("2026-09-01T12:00:00Z");+const success = { status: "completed", conclusion: "success" };+function fixture() {+ const pull = {+ number: 7,+ merged: true,+ state: "closed",+ merged_at: "today",+ merge_commit_sha: release,+ head: { sha: head, ref: "web-fix", repo: { full_name: repository } },+ base: { ref: "main", repo: { full_name: repository } },+ };+ const run = {+ ...success,+ id: 10,+ run_attempt: 2,+ run_number: 20,+ head_sha: head,+ head_branch: "web-fix",+ repository: { full_name: repository },+ head_repository: { full_name: repository },+ path: ".github/workflows/cicd.yml",+ event: "pull_request",+ created_at: new Date(now - 3600_000).toISOString(),+ };+ const scan = {+ ...success,+ id: 30,+ run_attempt: 1,+ run_number: 5,+ head_sha: head,+ head_branch: "refs/pull/7/head",+ path: "dynamic/github-code-scanning/codeql",+ repository: { full_name: repository },+ };+ const jobs = [...CI_JOBS, "Certify tested tree"].map((name, index) => ({+ ...success,+ id: index + 100,+ name,+ run_id: run.id,+ run_attempt: run.run_attempt,+ head_sha: head,+ steps: ["Validate receipt eligibility", "Checkout tested commit", "Record the immutable tested tree"].map(+ (name) => ({ ...success, name }),+ ),+ }));+ const receipt = { sha: tested, tree, runId: run.id, attempt: run.run_attempt, pr: pull.number };+ const data: Record<string, any> = {+ [`commits/${release}`]: { sha: release, parents: [{ sha: base }], commit: { tree: { sha: tree } } },+ [`commits/${release}/pulls?per_page=100`]: [pull],+ [`pulls/${pull.number}`]: pull,+ [`contents/.github/workflows/cicd.yml?ref=${base}`]: { type: "file", sha: "f".repeat(40) },+ [`contents/.github/workflows/cicd.yml?ref=${release}`]: { type: "file", sha: "f".repeat(40) },+ [`contents/scripts/reuse-ci.ts?ref=${base}`]: { type: "file", sha: "9".repeat(40) },+ [`contents/scripts/reuse-ci.ts?ref=${release}`]: { type: "file", sha: "9".repeat(40) },+ [`actions/runs?event=dynamic&head_sha=${head}&per_page=100`]: { total_count: 1, workflow_runs: [scan] },+ [`actions/workflows/cicd.yml/runs?event=pull_request&head_sha=${head}&per_page=100`]: {+ total_count: 1,+ workflow_runs: [run],+ },+ [`actions/runs/10/attempts/2/jobs?per_page=100`]: { total_count: jobs.length, jobs },+ [`commits/${tested}`]: { sha: tested, parents: [{ sha: base }, { sha: head }], commit: { tree: { sha: tree } } },+ [`actions/runs/10`]: structuredClone(run),+ };+ return {+ data,+ pull,+ run,+ scan,+ jobs,+ receipt,+ async resolve() {+ return reusableCi(+ async (path) => {+ assert.ok(data[path], path);+ return data[path];+ },+ async (id) => {+ assert.equal(id, jobs.at(-1)!.id);+ return `2026-09-01T11:00:00Z QM_CI_TREE=${JSON.stringify(receipt)}\n`;+ },+ repository,+ release,+ now,+ );+ },+ };+}++test("reuses the full tested merge tree, not the PR head SHA or an artifact name", async () => {+ assert.deepEqual(await fixture().resolve(), {+ sha: release,+ tree,+ pr: 7,+ testedSha: tested,+ ci: { runId: 10, attempt: 2 },+ codeql: { runId: 30, attempt: 1 },+ });+});++test("changed bases, trees, workflow, producer identity, attempts and missing gates cannot reuse", async () => {+ for (const change of [+ (f: any) => {+ f.data[`commits/${release}`].parents.push({ sha: head });+ },+ (f: any) => {+ f.data[`commits/${release}/pulls?per_page=100`] = [];+ },+ (f: any) => {+ f.data[`commits/${release}/pulls?per_page=100`].push(f.pull);+ },+ (f: any) => {+ f.pull.merged = false;+ },+ (f: any) => {+ f.pull.head.repo.full_name = "fork/qm";+ },+ (f: any) => {+ f.pull.base.ref = "other";+ },+ (f: any) => {+ f.pull.base.repo.full_name = "fork/qm";+ },+ (f: any) => {+ f.data[`contents/.github/workflows/cicd.yml?ref=${release}`].sha = base;+ },+ (f: any) => {+ f.data[`contents/scripts/reuse-ci.ts?ref=${release}`].sha = base;+ },+ (f: any) => {+ f.scan.conclusion = "failure";+ },+ (f: any) => {+ f.scan.head_branch = "refs/pull/8/head";+ },+ (f: any) => {+ f.scan.path = ".github/workflows/fake.yml";+ },+ (f: any) => {+ f.scan.head_sha = release;+ },+ (f: any) => {+ f.scan.repository.full_name = "fork/qm";+ },+ (f: any) => {+ f.data[`actions/runs?event=dynamic&head_sha=${head}&per_page=100`].total_count = 101;+ },+ (f: any) => {+ f.data[`actions/runs?event=dynamic&head_sha=${head}&per_page=100`].workflow_runs.push({+ ...f.scan,+ id: 31,+ run_number: 6,+ conclusion: "failure",+ });+ },+ (f: any) => {+ f.run.conclusion = "failure";+ },+ (f: any) => {+ f.run.event = "push";+ },+ (f: any) => {+ f.run.path = ".github/workflows/fake.yml";+ },+ (f: any) => {+ f.run.head_repository.full_name = "fork/qm";+ },+ (f: any) => {+ f.run.repository.full_name = "fork/qm";+ },+ (f: any) => {+ f.run.head_sha = release;+ },+ (f: any) => {+ f.run.head_branch = "other";+ },+ (f: any) => {+ f.run.created_at = new Date(now - 25 * 3600_000).toISOString();+ },+ (f: any) => {+ f.run.created_at = "invalid";+ },+ (f: any) => {+ f.run.created_at = new Date(now + 1).toISOString();+ },+ (f: any) => {+ f.data[`actions/workflows/cicd.yml/runs?event=pull_request&head_sha=${head}&per_page=100`].total_count = 101;+ },+ (f: any) => {+ f.data["actions/runs/10/attempts/2/jobs?per_page=100"].total_count = 101;+ },+ (f: any) => {+ f.jobs[0].conclusion = "skipped";+ },+ (f: any) => {+ f.jobs[0].run_attempt = 1;+ },+ (f: any) => {+ f.jobs[0].run_id = 11;+ },+ (f: any) => {+ f.jobs[0].head_sha = release;+ },+ (f: any) => {+ f.jobs.splice(1, 1);+ },+ (f: any) => {+ f.jobs.push(f.jobs[0]);+ },+ (f: any) => {+ f.jobs.at(-1).steps.pop();+ },+ (f: any) => {+ f.jobs.at(-1).steps[0].conclusion = "skipped";+ },+ (f: any) => {+ f.receipt.runId++;+ },+ (f: any) => {+ f.receipt.attempt--;+ },+ (f: any) => {+ f.receipt.pr++;+ },+ (f: any) => {+ f.receipt.tree = head;+ },+ (f: any) => {+ f.data[`commits/${tested}`].parents[0].sha = head;+ },+ (f: any) => {+ f.data[`commits/${tested}`].parents[1].sha = release;+ },+ (f: any) => {+ f.data[`commits/${tested}`].commit.tree.sha = head;+ },+ (f: any) => {+ f.data[`commits/${release}`].commit.tree.sha = head;+ },+ (f: any) => {+ f.data["actions/runs/10"].run_attempt++;+ },+ (f: any) => {+ f.data["actions/runs/10"].status = "in_progress";+ },+ ]) {+ const f = fixture();+ change(f);+ assert.equal(await f.resolve(), undefined, change.toString());+ }+});++test("a newer failed run prevents recycling an older green run", async () => {+ const f = fixture();+ f.data[`actions/workflows/cicd.yml/runs?event=pull_request&head_sha=${head}&per_page=100`].workflow_runs.push({+ ...f.run,+ id: 11,+ run_number: 21,+ conclusion: "failure",+ });+ assert.equal(await f.resolve(), undefined);+});++test("receipt parser rejects missing, multiple and malformed job output", () => {+ for (const text of ["", "QM_CI_TREE={}", "timestamp QM_CI_TREE={}", "timestamp QM_CI_TREE=nope"])+ assert.throws(() => readReceipt(text));+ const f = fixture();+ const line = `timestamp QM_CI_TREE=${JSON.stringify(f.receipt)}\n`;+ assert.deepEqual(readReceipt(line), f.receipt);+ assert.throws(() => readReceipt(line + line));+});++const workflow = readFileSync(new URL("../.github/workflows/cicd.yml", import.meta.url), "utf8");+const jobs = Object.fromEntries(+ [...workflow.split("jobs:\n")[1]!.matchAll(/^ {2}([a-z-]+):\n(.*?)(?=^ {2}[a-z-]+:|$(?![\s\S]))/gms)].map(+ (match): [string, string] => [match[1]!, match[2]!],+ ),+);++test("all quality jobs keep successful names and missing proof runs full checks", () => {+ assert.ok(workflow.includes("permissions:\n contents: read"));+ assert.ok(jobs.core!.includes("if: always() && !cancelled()"));+ assert.ok(jobs.core!.includes('test "$TYPECHECK" = success && test "$TESTS" = success'));+ const names: string[] = [];+ for (const [id, block] of Object.entries(jobs)) {+ if (["reuse", "certify"].includes(id)) continue;+ const name = block.match(/^ {4}name: (.+)$/m)![1]!;+ if (id === "core-tests") names.push(...[1, 2, 3, 4, 5].map((n) => name.replace("${{ matrix.shard }}", String(n))));+ else names.push(name);+ if (["core", "coauthor-trailers"].includes(id)) continue;+ assert.ok(block.includes("needs: reuse\n if: always() && !cancelled()"));+ const steps = block.split(/(?=^ {6}- (?:name|uses):)/m).slice(1);+ assert.ok(steps[0]!.includes("if: needs.reuse.outputs.reusable == 'true'"));+ for (const step of steps.slice(1)) assert.ok(step.includes("if: needs.reuse.outputs.reusable != 'true'"));+ }+ assert.deepEqual(names.sort(), [...CI_JOBS].sort());+ assert.ok(jobs.reuse!.includes("continue-on-error: true"));+ assert.ok(jobs.reuse!.includes("steps.proof.outcome == 'success' && steps.proof.outputs.reusable == 'true'"));+ assert.ok(jobs.reuse!.includes("if: github.event_name == 'push' && github.ref == 'refs/heads/main'"));+});++test("receipt is an isolated immutable checkout with no secrets, artifacts, caches or repository scripts", () => {+ const job = jobs.certify!;+ assert.ok(job.includes("continue-on-error: true"));+ assert.ok(job.includes("github.event_name == 'pull_request'"));+ assert.ok(job.includes("PR_REPOSITORY_ID: ${{ github.event.pull_request.head.repo.id }}"));+ assert.ok(job.includes('test "$PR_REPOSITORY_ID" = "$GITHUB_REPOSITORY_ID"'));+ assert.ok(job.includes('if (checks[id]?.result !== "success") throw new Error'));+ assert.ok(job.includes("always() && !cancelled()"));+ assert.ok(job.includes("permissions:\n contents: read"));+ assert.ok(+ job.includes(+ "needs: [core, cli, lint, core-postgres, admin-plugin, web-ui-plugin, auth-plugin, portal-plugin, coauthor-trailers]",+ ),+ );+ assert.equal([...job.matchAll(/^ {6}- /gm)].length, 3);+ assert.ok(job.includes("ref: ${{ github.sha }}\n persist-credentials: false"));+ assert.match(job, /uses: actions\/checkout@[a-f0-9]{40}/);+ assert.ok(job.includes('test "$(git rev-parse HEAD)" = "$GITHUB_SHA"'));+ assert.ok(job.includes("QM_CI_TREE="));+ assert.doesNotMatch(job, /secrets\.|npm |scripts\/|artifact|cache|id-token/);+});++test("inline receipt eligibility rejects missing or failed prerequisites, not an unrelated skipped resolver", () => {+ const script = jobs+ .certify!.split(" - name: Validate receipt eligibility")[1]!+ .split(" - name: Checkout tested commit")[0]!+ .split(" run: |\n")[1]!+ .replace(/^ {10}/gm, "");+ const ids = [+ "core",+ "cli",+ "lint",+ "core-postgres",+ "admin-plugin",+ "web-ui-plugin",+ "auth-plugin",+ "portal-plugin",+ "coauthor-trailers",+ ];+ const checks = Object.fromEntries(ids.map((id) => [id, { result: "success" }]));+ checks.reuse = { result: "skipped" };+ const execute = (source = "123", target = "123") =>+ spawnSync("bash", ["-eo", "pipefail", "-c", script], {+ env: { ...process.env, PR_REPOSITORY_ID: source, GITHUB_REPOSITORY_ID: target, CHECKS: JSON.stringify(checks) },+ encoding: "utf8",+ });+ assert.equal(execute().status, 0);+ for (const [source, target] of [+ ["123", "456"],+ ["", "123"],+ ["", ""],+ ["123", ""],+ ])+ assert.notEqual(execute(source, target).status, 0, `${source}/${target}`);+ for (const id of ids) {+ checks[id] = { result: "failure" };+ assert.notEqual(execute().status, 0, id);+ delete checks[id];+ assert.notEqual(execute().status, 0, id);+ checks[id] = { result: "success" };+ }+});