Files
PaperClipAI/server/scripts/prepare-runner-vendor.test.mjs
T
devinfoleyandClaude Fable 5.1 0575b85473 perf(build): reuse a current Runner build in the server's prepare:runner-vendor step
The server build's prepare:runner-vendor step ran the Runner's full build
every time, so inside `pnpm -r build` the Runner was built twice. The
second build took 12-16s on the CI Build job's critical path (six master
runs, 2026-10-11) and the same again in every other `pnpm build`.

The Runner's build now ends by recording a digest of its inputs, and the
server's step reuses the build when the digest matches the stamp and every
vendored output exists. Any other state runs the same
`pnpm --filter @paperclipai/paperclip-runner build` as before, so a
standalone server build (the Dockerfile's path) still builds the Runner
itself. PAPERCLIP_RUNNER_VENDOR_REBUILD=1 forces the rebuild.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-11 21:04:43 +00:00

268 lines
10 KiB
JavaScript

import { execFile } from "node:child_process";
import { chmodSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
import { promisify } from "node:util";
import { fileURLToPath } from "node:url";
import { afterEach, describe, expect, it } from "vitest";
import {
assessRunnerBuild,
buildInputsStampPath,
readBuildInputsStamp,
recordRunnerBuild,
} from "../../packages/paperclip-runner/scripts/build-inputs.mjs";
import {
FORCE_REBUILD_VARIABLE,
RUNNER_VENDOR_BUILD_COMMAND,
TIMING_SCHEMA,
decideRunnerVendorAction,
prepareRunnerVendor,
resolvePnpmInvocation,
} from "./prepare-runner-vendor.mjs";
const execFileAsync = promisify(execFile);
const prepareScript = fileURLToPath(new URL("./prepare-runner-vendor.mjs", import.meta.url));
const recordScript = fileURLToPath(
new URL("../../packages/paperclip-runner/scripts/record-build-inputs.mjs", import.meta.url),
);
const executable = process.platform === "win32" ? "paperclip-runnerd.exe" : "paperclip-runnerd";
const roots = [];
function writeTree(root, files) {
for (const [relative, content] of Object.entries(files)) {
const absolute = path.join(root, ...relative.split("/"));
mkdirSync(path.dirname(absolute), { recursive: true });
writeFileSync(absolute, content);
}
}
/** A throwaway workspace whose Runner is already built, with no stamp yet. */
function makeRepo() {
const repoRoot = mkdtempSync(path.join(tmpdir(), "prepare-runner-vendor-"));
roots.push(repoRoot);
writeTree(repoRoot, {
"package.json": "{}\n",
"pnpm-lock.yaml": "lockfileVersion: 9\n",
"pnpm-workspace.yaml": "packages:\n - packages/*\n - server\n",
"tsconfig.base.json": "{}\n",
"server/package.json": '{"name":"@paperclipai/server"}\n',
"packages/paperclip-runner/package.json": '{"name":"@paperclipai/paperclip-runner"}\n',
"packages/paperclip-runner/src/index.ts": "export const one = 1;\n",
"packages/paperclip-runner/dist/index.js": "export const one = 1;\n",
"packages/paperclip-runner/dist/live/index.js": "export {};\n",
"packages/paperclip-runner/dist/testing.js": "export {};\n",
[`packages/paperclip-runner/dist/bin/${executable}`]: "binary\n",
"packages/paperclip-eval-kernel/src/index.ts": "export const kernel = 1;\n",
});
const packageRoot = path.join(repoRoot, "packages", "paperclip-runner");
return { repoRoot, packageRoot };
}
afterEach(() => {
for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true });
});
describe("decideRunnerVendorAction", () => {
const current = { current: true, code: "inputs_unchanged", detail: "unchanged" };
const stale = { current: false, code: "inputs_changed", detail: "changed" };
it("reuses a current build and rebuilds a stale one, carrying the assessment's reason", () => {
expect(decideRunnerVendorAction({ env: {}, assessment: current })).toEqual({
action: "reuse",
code: "inputs_unchanged",
detail: "unchanged",
});
expect(decideRunnerVendorAction({ env: {}, assessment: stale })).toEqual({
action: "rebuild",
code: "inputs_changed",
detail: "changed",
});
});
it("rebuilds a current build when the operator forces it", () => {
expect(decideRunnerVendorAction({ env: { [FORCE_REBUILD_VARIABLE]: "1" }, assessment: current })).toEqual({
action: "rebuild",
code: "forced",
detail: `${FORCE_REBUILD_VARIABLE}=1`,
});
// Only the documented value forces; an unrelated value does not.
expect(decideRunnerVendorAction({ env: { [FORCE_REBUILD_VARIABLE]: "0" }, assessment: current }).action).toBe(
"reuse",
);
});
});
describe("resolvePnpmInvocation", () => {
it("runs the pnpm that is running the script, through Node for a JavaScript entry", () => {
expect(resolvePnpmInvocation({ npm_execpath: "/corepack/pnpm/9.15.4/bin/pnpm.cjs" }, "/usr/bin/node")).toEqual({
command: "/usr/bin/node",
argsPrefix: ["/corepack/pnpm/9.15.4/bin/pnpm.cjs"],
});
expect(resolvePnpmInvocation({ npm_execpath: "/opt/pnpm/pnpm" }, "/usr/bin/node")).toEqual({
command: "/opt/pnpm/pnpm",
argsPrefix: [],
});
expect(resolvePnpmInvocation({ npm_execpath: "C:\\pnpm\\pnpm.exe" }, "node.exe")).toEqual({
command: "C:\\pnpm\\pnpm.exe",
argsPrefix: [],
});
});
it("falls back to pnpm on PATH outside a pnpm script or under another package manager", () => {
expect(resolvePnpmInvocation({}, "/usr/bin/node")).toEqual({ command: "pnpm", argsPrefix: [] });
expect(resolvePnpmInvocation({ npm_execpath: "" }, "/usr/bin/node")).toEqual({ command: "pnpm", argsPrefix: [] });
expect(resolvePnpmInvocation({ npm_execpath: "/usr/lib/node_modules/npm/bin/npm-cli.js" }, "/usr/bin/node")).toEqual({
command: "pnpm",
argsPrefix: [],
});
});
});
describe("prepareRunnerVendor", () => {
it("still runs the Runner build the server build has always run", () => {
// The Dockerfile's standalone `pnpm --filter @paperclipai/server build`
// depends on this exact command producing a current vendored Runner.
expect(RUNNER_VENDOR_BUILD_COMMAND).toBe("pnpm --filter @paperclipai/paperclip-runner build");
});
it("skips the rebuild when the recorded build is current and reports the check time", () => {
const { repoRoot, packageRoot } = makeRepo();
recordRunnerBuild({ repoRoot, packageRoot });
const runs = [];
const lines = [];
const status = prepareRunnerVendor({
repoRoot,
env: {},
run: (...args) => {
runs.push(args);
return 0;
},
log: (line) => lines.push(line),
});
expect(status).toBe(0);
expect(runs).toEqual([]);
expect(lines[0]).toMatch(/^prepare:runner-vendor: reusing the current Runner build \(inputs unchanged/);
expect(lines[1]).toMatch(
new RegExp(`^${TIMING_SCHEMA},action:reuse,reason:inputs_unchanged,check_ms:\\d+,build_ms:0$`),
);
expect(readBuildInputsStamp(buildInputsStampPath(packageRoot))).not.toBeNull();
});
it("rebuilds when the inputs changed, removing the stale stamp before the build starts", () => {
const { repoRoot, packageRoot } = makeRepo();
recordRunnerBuild({ repoRoot, packageRoot });
writeFileSync(path.join(packageRoot, "src", "index.ts"), "export const one = 2;\n");
const stampPath = buildInputsStampPath(packageRoot);
const observed = [];
const lines = [];
const env = { npm_execpath: "/corepack/pnpm.cjs" };
const status = prepareRunnerVendor({
repoRoot,
env,
run: (command, options) => {
observed.push({ command, ...options, stampPresent: readBuildInputsStamp(stampPath) !== null });
return 0;
},
log: (line) => lines.push(line),
});
expect(status).toBe(0);
expect(observed).toEqual([
{ command: RUNNER_VENDOR_BUILD_COMMAND, cwd: path.join(repoRoot, "server"), env, stampPresent: false },
]);
expect(lines[0]).toMatch(/^prepare:runner-vendor: rebuilding the Runner \(inputs changed/);
expect(lines[1]).toMatch(
new RegExp(`^${TIMING_SCHEMA},action:rebuild,reason:inputs_changed,check_ms:\\d+,build_ms:\\d+,status:0$`),
);
});
it("returns the build's failure status and leaves no stamp behind", () => {
const { repoRoot, packageRoot } = makeRepo();
const lines = [];
const status = prepareRunnerVendor({
repoRoot,
env: {},
run: () => 3,
log: (line) => lines.push(line),
});
expect(status).toBe(3);
expect(lines[0]).toMatch(/^prepare:runner-vendor: rebuilding the Runner \(no build stamp/);
expect(lines[1]).toMatch(/,action:rebuild,reason:no_stamp,.*,status:3$/);
expect(readBuildInputsStamp(buildInputsStampPath(packageRoot))).toBeNull();
});
it("rebuilds a current build when PAPERCLIP_RUNNER_VENDOR_REBUILD=1", () => {
const { repoRoot, packageRoot } = makeRepo();
recordRunnerBuild({ repoRoot, packageRoot });
let runs = 0;
const lines = [];
const status = prepareRunnerVendor({
repoRoot,
env: { [FORCE_REBUILD_VARIABLE]: "1" },
run: () => {
runs += 1;
return 0;
},
log: (line) => lines.push(line),
});
expect(status).toBe(0);
expect(runs).toBe(1);
expect(lines[1]).toMatch(/,action:rebuild,reason:forced,/);
});
it.skipIf(process.platform === "win32")(
"end to end: the first server build rebuilds the Runner, the second reuses it, a change rebuilds again",
async () => {
const { repoRoot, packageRoot } = makeRepo();
// A stand-in pnpm that behaves like the Runner build: it records how it
// was called, then records the build inputs the way the real build does
// as its final step.
const binDir = path.join(repoRoot, "bin");
const argvLog = path.join(repoRoot, "pnpm-argv.log");
mkdirSync(binDir);
const pnpm = path.join(binDir, "pnpm");
writeFileSync(
pnpm,
`${[
"#!/bin/sh",
"set -e",
`printf '%s\\n' "$*" >> "${argvLog}"`,
`"${process.execPath}" "${recordScript}" --package-root "${packageRoot}" > /dev/null`,
].join("\n")}\n`,
);
chmodSync(pnpm, 0o755);
const env = { ...process.env, PATH: `${binDir}${path.delimiter}${process.env.PATH ?? ""}` };
delete env[FORCE_REBUILD_VARIABLE];
// The suite itself may run under pnpm; the script must then take the
// PATH fallback so the stand-in, not the real pnpm, receives the build.
delete env.npm_execpath;
const run = () => execFileAsync(process.execPath, [prepareScript, "--repo-root", repoRoot], { env });
const calls = () => readFileSync(argvLog, "utf8").split("\n").filter(Boolean);
const first = await run();
expect(calls()).toEqual(["--filter @paperclipai/paperclip-runner build"]);
expect(first.stdout).toMatch(/action:rebuild,reason:no_stamp,/);
expect(assessRunnerBuild({ repoRoot, packageRoot }).current).toBe(true);
const second = await run();
expect(calls()).toHaveLength(1);
expect(second.stdout).toMatch(/action:reuse,reason:inputs_unchanged,/);
writeFileSync(path.join(packageRoot, "src", "index.ts"), "export const one = 2;\n");
const third = await run();
expect(calls()).toHaveLength(2);
expect(third.stdout).toMatch(/action:rebuild,reason:inputs_changed,/);
},
);
});