diff --git a/scripts/checks/pi-qualification-receipt-refresh.mts b/scripts/checks/pi-qualification-receipt-refresh.mts index e4bc96f82d5..640a5bb7302 100644 --- a/scripts/checks/pi-qualification-receipt-refresh.mts +++ b/scripts/checks/pi-qualification-receipt-refresh.mts @@ -163,10 +163,14 @@ function parseReceipt( function requireReceiptSourceParity( git: GitRunner, revision: string, - comparisonRevision: string, + comparisonRevision: string | null, imageSourcePaths: readonly string[], ): void { - const result = git(["diff", "--quiet", revision, comparisonRevision, "--", ...imageSourcePaths]); + const result = git( + comparisonRevision === null + ? ["diff", "--quiet", "--cached", revision, "--", ...imageSourcePaths] + : ["diff", "--quiet", revision, comparisonRevision, "--", ...imageSourcePaths], + ); if (result.error) { throw new Error(`Could not run git to validate Pi receipt source parity (${result.error})`); } @@ -180,8 +184,19 @@ function requireReceiptSourceParity( ); } -function receiptComparisonRevision(git: GitRunner, explicit?: string): string { +function receiptComparisonRevision(git: GitRunner, explicit?: string): string | null { if (explicit) return explicit; + const mergeHead = git(["rev-parse", "--quiet", "--verify", "MERGE_HEAD"]); + if (mergeHead.error) { + throw new Error(`Could not detect an in-progress merge (${mergeHead.error})`); + } + if (mergeHead.status === 0) return null; + if (mergeHead.status !== 1) { + const detail = mergeHead.stderr?.trim(); + throw new Error( + `Could not detect an in-progress merge: git exited ${mergeHead.status ?? "without status"}${detail ? ` (${detail})` : ""}`, + ); + } if (process.env.GITHUB_ACTIONS !== "true" || process.env.GITHUB_EVENT_NAME !== "pull_request") { return "HEAD"; } @@ -199,7 +214,7 @@ function validateReceiptPair( imageSourcePaths: readonly string[], receipts: readonly { path: string; platform: ManagedImagePlatform }[], acceptedDigests: ReadonlySet, - comparisonRevision: string, + comparisonRevision: string | null, ): void { const validated = receipts.map((receipt) => parseReceipt(rootDir, receipt, acceptedDigests)); const contracts = validated.map(({ contract }) => contract); diff --git a/test/repository/pi-qualification-receipt-refresh.test.ts b/test/repository/pi-qualification-receipt-refresh.test.ts index db9b36a17cf..b15125eb154 100644 --- a/test/repository/pi-qualification-receipt-refresh.test.ts +++ b/test/repository/pi-qualification-receipt-refresh.test.ts @@ -6,7 +6,7 @@ import fs from "node:fs"; import os from "node:os"; import path from "node:path"; -import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { checkPiQualificationReceiptRefresh } from "../../scripts/checks/pi-qualification-receipt-refresh.mts"; @@ -49,6 +49,8 @@ describe("Pi qualification receipt refresh", () => { let acceptedDigests: Set; beforeEach(() => { + vi.stubEnv("GITHUB_ACTIONS", "false"); + vi.stubEnv("GITHUB_EVENT_NAME", ""); rootDir = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-pi-receipt-refresh-")); fs.mkdirSync(path.join(rootDir, "agents/pi"), { recursive: true }); fs.mkdirSync(path.join(rootDir, "ci"), { recursive: true }); @@ -67,13 +69,18 @@ describe("Pi qualification receipt refresh", () => { acceptedDigests = new Set([receiptDigest(amd64Contents), receiptDigest(arm64Contents)]); }); - afterEach(() => fs.rmSync(rootDir, { force: true, recursive: true })); + afterEach(() => { + vi.unstubAllEnvs(); + fs.rmSync(rootDir, { force: true, recursive: true }); + }); function run( changedPaths: readonly string[], options: { accepted?: ReadonlySet; - headRevision?: string; + headRevision?: string | null; + mergeInProgress?: boolean; + pullRequestHeadRevision?: string; sourceParity?: boolean; stagedPaths?: readonly string[]; } = {}, @@ -89,18 +96,32 @@ describe("Pi qualification receipt refresh", () => { status: 0, stdout: `${(args.includes("--cached") ? (options.stagedPaths ?? []) : changedPaths).join("\0")}\0`, } - : args.includes("--quiet") - ? args[3] === (options.headRevision ?? "HEAD") - ? { status: options.sourceParity === false ? 1 : 0, stdout: "" } + : args[0] === "rev-parse" + ? args.join("\0") === ["rev-parse", "--quiet", "--verify", "MERGE_HEAD"].join("\0") + ? options.mergeInProgress + ? { status: 0, stdout: `${"e".repeat(40)}\n` } + : { status: 1, stdout: "" } + : args.join("\0") === ["rev-parse", "--verify", "HEAD^2"].join("\0") && + options.pullRequestHeadRevision + ? { status: 0, stdout: `${options.pullRequestHeadRevision}\n` } + : (() => { + throw new Error(`Unexpected revision probe: ${args.join(" ")}`); + })() + : args.includes("--quiet") + ? (args.includes("--cached") && + options.mergeInProgress && + args[3] === SOURCE_REVISION) || + args[3] === (options.headRevision ?? options.pullRequestHeadRevision ?? "HEAD") + ? { status: options.sourceParity === false ? 1 : 0, stdout: "" } + : (() => { + throw new Error(`Unexpected source parity arguments: ${args.join(" ")}`); + })() : (() => { - throw new Error(`Unexpected comparison revision: ${args[3]}`); - })() - : (() => { - throw new Error(`Unexpected git arguments: ${args.join(" ")}`); - })(), + throw new Error(`Unexpected git arguments: ${args.join(" ")}`); + })(), receipts: RECEIPTS, rootDir, - headRevision: options.headRevision ?? "HEAD", + ...(options.headRevision !== null ? { headRevision: options.headRevision ?? "HEAD" } : {}), }); } @@ -141,7 +162,7 @@ describe("Pi qualification receipt refresh", () => { ).not.toThrow(); }); - it("compares receipt parity against the exact PR head instead of a synthetic merge", () => { + it("compares receipt parity against an explicit head revision", () => { const headRevision = "d".repeat(40); expect(() => run(["protected/app/config.json", ...RECEIPTS.map(({ path }) => path)], { @@ -150,6 +171,36 @@ describe("Pi qualification receipt refresh", () => { ).not.toThrow(); }); + it("probes for a merge before comparing receipt parity against HEAD", () => { + expect(() => + run(["protected/app/config.json", ...RECEIPTS.map(({ path }) => path)], { + headRevision: null, + }), + ).not.toThrow(); + }); + + it("compares receipt parity against the exact PR head instead of a synthetic merge", () => { + const pullRequestHeadRevision = "d".repeat(40); + vi.stubEnv("GITHUB_ACTIONS", "true"); + vi.stubEnv("GITHUB_EVENT_NAME", "pull_request"); + + expect(() => + run(["protected/app/config.json", ...RECEIPTS.map(({ path }) => path)], { + headRevision: null, + pullRequestHeadRevision, + }), + ).not.toThrow(); + }); + + it("compares receipt parity against the staged tree during a local merge", () => { + expect(() => + run(["protected/app/config.json", ...RECEIPTS.map(({ path }) => path)], { + headRevision: null, + mergeInProgress: true, + }), + ).not.toThrow(); + }); + it("does not require receipts for unrelated changes", () => { expect(() => run(["docs/reference/pi.mdx"])).not.toThrow(); });