Skip to content
Merged
122 changes: 122 additions & 0 deletions scripts/checks/pi-qualification-receipt-refresh.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

/** Requires both Pi qualification receipts when a Pi image input changes. */

import { spawnSync } from "node:child_process";
import path from "node:path";
import { fileURLToPath } from "node:url";

import { directDockerfileCopySources } from "../lib/dockerfile-copy-sources.mts";

type GitResult = {
error?: string;
status: number | null;
stdout: string;
};

type GitRunner = (args: readonly string[]) => GitResult;

const REPO_ROOT = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../..");
const PI_DOCKERFILES = ["agents/pi/Dockerfile", "agents/pi/Dockerfile.base"] as const;
export const PI_QUALIFICATION_RECEIPTS = [
"ci/pi-agent-qualification-v1-linux-amd64.json",
"ci/pi-agent-qualification-v1-linux-arm64.json",
] as const;

function ownsPath(source: string, changedPath: string): boolean {
const normalizedSource = source.replace(/\/+$/u, "");
return changedPath === normalizedSource || changedPath.startsWith(`${normalizedSource}/`);
}

export function missingPiQualificationReceiptRefreshes(
changedPaths: readonly string[],
imageSourcePaths: readonly string[],
receiptPaths: readonly string[] = PI_QUALIFICATION_RECEIPTS,
): string[] {
if (
!changedPaths.some((changedPath) =>
imageSourcePaths.some((source) => ownsPath(source, changedPath)),
)
) {
return [];
}
return receiptPaths.filter((receipt) => !changedPaths.includes(receipt));
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}

function runGit(args: readonly string[]): GitResult {
const result = spawnSync("git", [...args], {
cwd: REPO_ROOT,
encoding: "utf8",
timeout: 10_000,
});
return {
error: result.error?.message,
status: result.status,
stdout: result.stdout ?? "",
};
}

function requireGitOutput(result: GitResult, operation: string): string {
if (result.error) throw new Error(`${operation}: ${result.error}`);
if (result.status !== 0)
throw new Error(`${operation}: git exited ${result.status ?? "without status"}`);
return result.stdout;
}

function mergeBaseRevision(git: GitRunner, baseBranch: string | undefined): string {
const baseRef = baseBranch ? `origin/${baseBranch}` : "origin/main";
const revision = requireGitOutput(
git(["merge-base", "HEAD", baseRef]),
`Could not resolve the Pi receipt comparison base against ${baseRef}`,
).trim();
if (!revision) {
throw new Error(`Could not resolve the Pi receipt comparison base against ${baseRef}`);
}
return revision;
}

function changedPathsFromBase(git: GitRunner, revision: string): string[] {
return requireGitOutput(
git(["diff", "--no-ext-diff", "--no-textconv", "--name-only", "-z", revision, "HEAD", "--"]),
`Could not inspect changes after ${revision}`,
)
.split("\0")
.filter(Boolean);
}

function piImageSourcePaths(rootDir: string): string[] {
const copiedSources = PI_DOCKERFILES.flatMap((dockerfile) =>
directDockerfileCopySources(path.join(rootDir, dockerfile), dockerfile).map(
({ source }) => source,
),
);
return [...new Set([".dockerignore", ...PI_DOCKERFILES, ...copiedSources])].sort();
}

export function checkPiQualificationReceiptRefresh(
git: GitRunner = runGit,
rootDir: string = REPO_ROOT,
baseBranch: string | undefined = process.env.GITHUB_BASE_REF?.trim(),
): void {
const revision = mergeBaseRevision(git, baseBranch);
const changedPaths = changedPathsFromBase(git, revision);
const missingReceipts = missingPiQualificationReceiptRefreshes(
changedPaths,
piImageSourcePaths(rootDir),
);
if (missingReceipts.length === 0) return;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

throw new Error(
[
"Pi image inputs changed without refreshing both qualification receipts.",
"Publish one exact-commit AMD64 and ARM64 Pi candidate cohort, then update:",
...missingReceipts.map((receipt) => `- ${receipt}`),
].join("\n"),
);
}

const currentModule = fileURLToPath(import.meta.url);
if (process.argv[1] && path.resolve(process.argv[1]) === currentModule) {
checkPiQualificationReceiptRefresh();
}
5 changes: 5 additions & 0 deletions scripts/checks/run.mts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ export const CHECKS: readonly CheckCommand[] = [
command: TSX,
args: ["scripts/checks/optimized-build-context-copy-sources.mts"],
},
{
name: "pi-qualification-receipt-refresh",
command: TSX,
args: ["scripts/checks/pi-qualification-receipt-refresh.mts"],
},
{
name: "test-registration-boundary",
command: TSX,
Expand Down
8 changes: 8 additions & 0 deletions test/repository/checks-runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ describe("checks runner", () => {
});
});

it("registers the Pi qualification receipt refresh check", () => {
expect(CHECKS).toContainEqual({
name: "pi-qualification-receipt-refresh",
command: process.platform === "win32" ? "tsx.cmd" : "tsx",
args: ["scripts/checks/pi-qualification-receipt-refresh.mts"],
});
});

it("runs Windows command shims through cmd.exe", () => {
expect(
buildCheckSpawnInvocation(sampleCheck, "win32", {
Expand Down
47 changes: 47 additions & 0 deletions test/repository/pi-qualification-receipt-refresh.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

import { describe, expect, it } from "vitest";

import { missingPiQualificationReceiptRefreshes } from "../../scripts/checks/pi-qualification-receipt-refresh.mts";

const RECEIPTS = ["ci/pi-amd64.json", "ci/pi-arm64.json"];
const IMAGE_SOURCES = ["agents/pi/Dockerfile", "blueprint/"];
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

describe("Pi qualification receipt refresh", () => {
it("requires both architecture receipts after an image input changes", () => {
expect(
missingPiQualificationReceiptRefreshes(
["blueprint/provider-profiles/example.yaml"],
IMAGE_SOURCES,
RECEIPTS,
),
).toEqual(RECEIPTS);
});

it("rejects a partial architecture receipt refresh", () => {
expect(
missingPiQualificationReceiptRefreshes(
["agents/pi/Dockerfile", RECEIPTS[0]!],
IMAGE_SOURCES,
RECEIPTS,
),
).toEqual([RECEIPTS[1]]);
});

it("accepts one cohort refresh for both architectures", () => {
expect(
missingPiQualificationReceiptRefreshes(
["blueprint/policy.yaml", ...RECEIPTS],
IMAGE_SOURCES,
RECEIPTS,
),
).toEqual([]);
});

it("does not require receipts for unrelated changes", () => {
expect(
missingPiQualificationReceiptRefreshes(["docs/reference/pi.mdx"], IMAGE_SOURCES, RECEIPTS),
).toEqual([]);
});
});
Loading