Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions .github/workflows/pr-review-advisor.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ jobs:
TYPEBOX_VERSION: "1.1.38"
# Workflow-boundary modules parse YAML before the advisor session starts.
YAML_VERSION: "2.8.3"
# Credential-free E2E discovery invokes the Vitest file-list command.
# Credential-free inventory discovery executes the trusted Vitest entrypoint.
VITEST_VERSION: "4.1.9"
FD_FIND_VERSION: "9.0.0-1"
RIPGREP_VERSION: "14.1.0-1"
Expand Down Expand Up @@ -249,10 +249,12 @@ jobs:
exit 1
fi

PI_SDK_DIR="$RUNNER_TEMP/pi-sdk"
npm install --prefix "$PI_SDK_DIR" --ignore-scripts --no-save --package-lock=false --before=2026-07-11T00:00:00.000Z "@earendil-works/pi-coding-agent@${PI_SDK_VERSION}" "typebox@${TYPEBOX_VERSION}" "vitest@${VITEST_VERSION}" "yaml@${YAML_VERSION}"
rm -rf "$ADVISOR_DIR/node_modules"
ln -s "$PI_SDK_DIR/node_modules" "$ADVISOR_DIR/node_modules"
# Install only from the trusted workflow checkout's committed lockfile.
# --ignore-scripts keeps package lifecycle code out of this secret-bearing job.
(
cd "$ADVISOR_DIR"
npm ci --ignore-scripts --no-audit --no-fund
)

- name: Run PR review advisor
id: analysis
Expand Down
46 changes: 41 additions & 5 deletions test/pr-review-advisor-workflow-boundary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,8 @@ describe("PR review advisor workflow boundary", () => {
const binDir = path.join(tmp, "bin");
const callLog = path.join(tmp, "calls.log");
fs.mkdirSync(binDir);
for (const name of ["npm", "rm", "ln"]) writeFakeCommand(binDir, name);
fs.mkdirSync(path.join(tmp, "advisor"));
writeFakeCommand(binDir, "npm");
fs.writeFileSync(
path.join(binDir, "dpkg-query"),
`#!/bin/bash
Expand Down Expand Up @@ -508,10 +509,9 @@ printf 'sudo %s\\n' "$*" >> "$CALL_LOG"
expect(fs.readFileSync(callLog, "utf8")).toContain("dpkg-query -W -f=${Version} ripgrep");
expect(fs.readFileSync(callLog, "utf8")).toContain("fdfind --version");
expect(fs.readFileSync(callLog, "utf8")).toContain("rg --version");
expect(fs.readFileSync(callLog, "utf8")).toContain("--ignore-scripts");
expect(fs.readFileSync(callLog, "utf8")).toContain("typebox@test-typebox-version");
expect(fs.readFileSync(callLog, "utf8")).toContain("vitest@test-vitest-version");
expect(fs.readFileSync(callLog, "utf8")).toContain("yaml@test-yaml-version");
expect(fs.readFileSync(callLog, "utf8")).toContain(
"npm ci --ignore-scripts --no-audit --no-fund",
);
} finally {
fs.rmSync(tmp, { recursive: true, force: true });
}
Expand Down Expand Up @@ -674,6 +674,42 @@ process.exitCode = valid ? 0 : 1;`,
);
});

it("rejects decoy lockfile-install text outside the npm invocation", () => {
const errors = validateMutation((source) =>
source.replace(
"npm ci --ignore-scripts --no-audit --no-fund",
"npm install --ignore-scripts\n printf '%s\\n' 'npm ci --ignore-scripts --no-audit --no-fund' >/dev/null",
),
);

expect(errors).toContain(
"step 'Install Pi SDK' must use the canonical lockfile-only npm ci command",
);
});

it("rejects drift in the trusted advisor runtime package lock", () => {
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "pr-review-advisor-lock-"));
const lockPath = path.join(tmp, "package-lock.json");
fs.writeFileSync(lockPath, JSON.stringify({ packages: {} }));
try {
expect(
validatePrReviewAdvisorWorkflowBoundary(
path.join(ROOT, ".github", "workflows", "pr-review-advisor.yaml"),
lockPath,
),
).toEqual(
expect.arrayContaining([
"advisor package lock must pin @earendil-works/pi-coding-agent@0.80.6",
"advisor package lock must pin typebox@1.1.38",
"advisor package lock must pin yaml@2.8.3",
"advisor package lock must pin vitest@4.1.9",
]),
);
} finally {
fs.rmSync(tmp, { recursive: true, force: true });
}
});

it("reports workflow parse failures through boundary errors", () => {
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "pr-review-advisor-missing-"));
const missingPath = path.join(tmp, "workflow.yaml");
Expand Down
62 changes: 54 additions & 8 deletions tools/pr-review-advisor/workflow-boundary.mts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ import YAML from "yaml";

const REPO_ROOT = join(dirname(fileURLToPath(import.meta.url)), "..", "..");
const DEFAULT_WORKFLOW_PATH = join(REPO_ROOT, ".github", "workflows", "pr-review-advisor.yaml");
const DEFAULT_PACKAGE_LOCK_PATH = join(REPO_ROOT, "package-lock.json");
const TRUSTED_WORKFLOW_REF = "${{ github.workflow_sha }}";
const CANONICAL_ADVISOR_NPM_CI = "npm ci --ignore-scripts --no-audit --no-fund";
const ADVISOR_RUNTIME_PACKAGE_PINS = [
{ packageName: "@earendil-works/pi-coding-agent", envName: "PI_SDK_VERSION", version: "0.80.6" },
{ packageName: "typebox", envName: "TYPEBOX_VERSION", version: "1.1.38" },
{ packageName: "yaml", envName: "YAML_VERSION", version: "2.8.3" },
{ packageName: "vitest", envName: "VITEST_VERSION", version: "4.1.9" },
] as const;

type WorkflowRecord = Record<string, unknown>;
type WorkflowStep = WorkflowRecord & {
Expand Down Expand Up @@ -78,6 +86,19 @@ function requireRunContains(
}
}

function requireRunLine(
errors: string[],
step: WorkflowStep | undefined,
expected: string,
message: string,
): void {
if (!step) return;
const lines = stringValue(step.run)
.split(/\r?\n/u)
.map((line) => line.trim());
if (!lines.includes(expected)) errors.push(message);
}

function requireRunOrder(
errors: string[],
step: WorkflowStep | undefined,
Expand Down Expand Up @@ -124,6 +145,23 @@ function requireExactPermissions(
}
}

function checkAdvisorRuntimePackageLock(errors: string[], packageLockPath: string): void {
let lock: WorkflowRecord;
try {
lock = asRecord(JSON.parse(readFileSync(packageLockPath, "utf8")));
} catch {
errors.push(`failed to read or parse advisor package lock: ${packageLockPath}`);
return;
}
const packages = asRecord(lock.packages);
for (const { packageName, version } of ADVISOR_RUNTIME_PACKAGE_PINS) {
const actualVersion = asRecord(packages[`node_modules/${packageName}`]).version;
if (actualVersion !== version) {
errors.push(`advisor package lock must pin ${packageName}@${version}`);
}
}
}

function requireActionPins(
errors: string[],
jobName: string,
Expand Down Expand Up @@ -245,12 +283,11 @@ function checkAnalysisJob(errors: string[], reviewJob: WorkflowRecord): void {
"PR_REVIEW_ADVISOR_MODEL",
"${{ matrix.advisor.model }}",
);
requireEnv(errors, "review job", reviewJob, "PI_SDK_VERSION", "0.80.6");
requireEnv(errors, "review job", reviewJob, "FD_FIND_VERSION", "9.0.0-1");
requireEnv(errors, "review job", reviewJob, "RIPGREP_VERSION", "14.1.0-1");
requireEnv(errors, "review job", reviewJob, "TYPEBOX_VERSION", "1.1.38");
requireEnv(errors, "review job", reviewJob, "VITEST_VERSION", "4.1.9");
requireEnv(errors, "review job", reviewJob, "YAML_VERSION", "2.8.3");
for (const { envName, version } of ADVISOR_RUNTIME_PACKAGE_PINS) {
requireEnv(errors, "review job", reviewJob, envName, version);
}
requireEnv(
errors,
"review job",
Expand Down Expand Up @@ -365,11 +402,18 @@ done < <(find "$ADVISOR_WORKDIR" -type l -print0)`;
install,
'"$RG_BINARY_VERSION" != "ripgrep $EXPECTED_RG_BINARY_VERSION"',
);
requireRunContains(errors, install, "npm ci");
requireRunContains(errors, install, 'cd "$ADVISOR_DIR"');
requireRunContains(errors, install, "--ignore-scripts");
requireRunContains(errors, install, '"typebox@${TYPEBOX_VERSION}"');
requireRunContains(errors, install, '"vitest@${VITEST_VERSION}"');
requireRunContains(errors, install, '"yaml@${YAML_VERSION}"');
requireRunContains(errors, install, '"$ADVISOR_DIR/node_modules"');
requireRunContains(errors, install, "--no-audit");
requireRunContains(errors, install, "--no-fund");
requireRunLine(
errors,
install,
CANONICAL_ADVISOR_NPM_CI,
"step 'Install Pi SDK' must use the canonical lockfile-only npm ci command",
);
requireRunOrder(errors, install, 'cd "$ADVISOR_DIR"', CANONICAL_ADVISOR_NPM_CI);

const analyze = requireStep(errors, steps, "Run PR review advisor");
requireRunContains(errors, analyze, 'cd "$ADVISOR_WORKDIR"');
Expand Down Expand Up @@ -529,6 +573,7 @@ function checkPublishJob(errors: string[], publishJob: WorkflowRecord): void {

export function validatePrReviewAdvisorWorkflowBoundary(
workflowPath = DEFAULT_WORKFLOW_PATH,
packageLockPath = DEFAULT_PACKAGE_LOCK_PATH,
): string[] {
const errors: string[] = [];
let workflow: WorkflowRecord;
Expand All @@ -541,6 +586,7 @@ export function validatePrReviewAdvisorWorkflowBoundary(
if (workflow.name !== "PR Review / Advisor") {
errors.push("workflow name must remain PR Review / Advisor");
}
checkAdvisorRuntimePackageLock(errors, packageLockPath);
checkTargetTriggers(errors, workflow);
const concurrencyGroup = stringValue(asRecord(workflow.concurrency).group);
if (!concurrencyGroup.includes("github.event_name")) {
Expand Down