Files
PaperClipAI/scripts/ci-typecheck-lane.mjs
T
DottaandClaude Fable 5.2 c6301c92ff fix(ci): keep the typecheck job working on stacked PRs and stop its CLI guidance test timing out (#21451)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - Every pull request runs the required `Typecheck + Release Registry`
job from the trusted workflow on `master`
> - #21404 replaced that job's six sequential steps with one script,
`scripts/ci-typecheck-lane.mjs`, that runs independent checks in
parallel
> - PR runs take the workflow from `master` but check out the PR's merge
commit. A stacked PR's merge commit is built on its base branch, so it
can lack a script that only `master` has
> - The lane also ran the CLI guidance test beside the typechecks at a
lower CPU priority, and one of its cases is slow enough to cross its 15
second timeout there
> - This pull request makes the job fall back to the earlier commands
when the script is absent, and runs the CLI guidance test before the
parallel checks
> - The benefit is that stacked PRs typecheck again and the job stops
failing on a timeout that has nothing to do with the PR

## Linked Issues or Issue Description

Refs #21404

**What happened?**
In the first eight PR runs after #21404 merged (2026-11-13 18:06Z), the
typecheck job failed three times for reasons unrelated to the PRs:

- Twice on #20617, a PR stacked on `feat/ssh-agent-ai-accounts`: `Error:
Cannot find module
'/home/runner/_work/paperclip/paperclip/scripts/ci-typecheck-lane.mjs'`
(jobs 132076887259 and 132079570039). The job failed 0 seconds into the
step.
- Once on run 43480469363 (job 132075678943): `CLI guidance safety
failed with exit code 1`, from `Error: Test timed out in 15000ms.` in
`allows only exact-allowlist pnpm paperclipai commands on every guidance
surface`, which took 17059 ms. The same case passes in about 5 seconds
when the test runs alone.

**Expected behavior**
The typecheck job passes or fails on the PR's own code, including for
stacked PRs.

**Actual behavior**
Stacked PRs always fail the job. Other PRs fail it intermittently (1 of
6 runs that reached the lane).

**Steps to reproduce**
Open a PR whose base branch does not contain
`scripts/ci-typecheck-lane.mjs`, or run the lane on a loaded 4 vCPU
runner.

## What Changed

- `.github/workflows/pr-trusted.yml`: the lane step runs only when
`scripts/ci-typecheck-lane.mjs` exists in the checkout. A new step runs
the six earlier commands, in their earlier order, when it does not. Only
the `typecheck_release_registry` job changed.
- `scripts/ci-typecheck-lane.mjs`: the CLI guidance test moved from the
parallel checks to the end of the sequential preparation steps, at
normal priority.
- Tests: `scripts/__tests__/ci-typecheck-lane.test.mjs` and
`.github/scripts/tests/ci-plan.test.mjs` assert the two conditions, that
the earlier commands appear only in the fallback step, and that the CLI
guidance test is a preparation step.
- `doc/DEVELOPING.md`: the "Typecheck lane in CI" section describes
both.

## Verification

- `node --test scripts/__tests__/ci-typecheck-lane.test.mjs`: 8 pass.
- `node --test '.github/scripts/tests/*.test.mjs'` (the `policy` job's
command): 807 pass.
- `node --test scripts/__tests__/runner-release-crate-cache.test.mjs`:
11 pass.
- Not exercised by this PR's own CI: PR runs use the workflow from
`master`, so the new conditions first run after merge. The script change
does run here, because the script is read from the PR's checkout.
- Not measured: the lane's wall time with the CLI guidance test moved.
The test takes 2 to 5 seconds alone, so the expected cost is that much.

## Risks

- The fallback step repeats six commands in the workflow. It is needed
only until open stacked PRs are based on a branch that has the script,
and can be removed later.
- `hashFiles` returns an empty string when no file matches, which is
what the two conditions rely on.
- If a future PR deletes or renames the script, the job silently uses
the slower commands instead of failing. The wiring tests would fail in
that PR.

## Model Used

Claude Fable 5.2 (`claude-fable-5-2`), via Claude Code, with tool use
and code execution.

## Checklist

- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [x] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-13 13:24:52 -06:00

282 lines
11 KiB
JavaScript

#!/usr/bin/env node
import { spawn, spawnSync } from "node:child_process";
import { availableParallelism, setPriority } from "node:os";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { listLayers } from "../server/scripts/typecheck-layers.mjs";
import { LOW_MEMORY_VARIABLE, EMIT_ONLY_VARIABLE } from "./run-tsc.mjs";
import { PROJECTS } from "./typecheck-test-projects.mjs";
export const SERIAL_BUILD_GAPS = ["@paperclipai/paperclip-runner"];
export const BACKGROUND_PRIORITY = 10;
const SERVER_DECLARATIONS = "server declarations";
export const CLI_GUIDANCE_SAFETY = "CLI guidance safety";
const lowMemory = { [LOW_MEMORY_VARIABLE]: "1" };
export function planLane({ buildGapPackages, testLayers, projects = PROJECTS }) {
const typecheckPackage = (name, background) => ({
name: `typecheck ${name}`,
command: "pnpm",
args: ["--filter", name, "typecheck"],
env: lowMemory,
background,
});
const prepare = [
{ name: "link workspace packages", command: "pnpm", args: ["run", "preflight:workspace-links"] },
{ name: "build plugin SDK", command: "pnpm", args: ["--filter", "@paperclipai/plugin-sdk", "ensure-build-deps"] },
{
name: "build server without type checks",
command: "pnpm",
args: ["--filter", "@paperclipai/server", "build"],
env: { [EMIT_ONLY_VARIABLE]: "1" },
},
{
name: "check server runtime assets",
command: "node",
args: ["scripts/run-typecheck-build-gaps.mjs", "--runtime-assets-only"],
},
{ name: "install test program dependencies", command: "node", args: ["scripts/typecheck-test-programs.mjs", "--prepare-only"] },
...buildGapPackages.filter((name) => SERIAL_BUILD_GAPS.includes(name)).map((name) => typecheckPackage(name, false)),
{
name: CLI_GUIDANCE_SAFETY,
command: "pnpm",
args: ["exec", "vitest", "run", "--project", "@paperclipai/server", "server/src/__tests__/cli-invocation-safety.test.ts"],
},
];
const needsDeclarations = [SERVER_DECLARATIONS];
const tasks = [
{
name: SERVER_DECLARATIONS,
command: "node",
args: ["server/scripts/typecheck-layers.mjs", "--declarations-only"],
env: lowMemory,
},
{ name: "release registry tests", command: "pnpm", args: ["run", "test:release-registry"] },
{
name: "typecheck test programs",
command: "node",
args: ["scripts/typecheck-test-programs.mjs", "--low-memory", "--skip-prepare"],
background: true,
},
...testLayers.map((layer) => ({
name: `server layer ${layer}`,
command: "node",
args: ["server/scripts/typecheck-layers.mjs", "--layer", layer],
env: lowMemory,
needs: needsDeclarations,
})),
...projects.map((project) => ({
name: `test project ${project}`,
command: "node",
args: ["scripts/typecheck-test-projects.mjs", "--low-memory", "--reuse-declarations", project],
needs: needsDeclarations,
})),
...buildGapPackages.filter((name) => !SERIAL_BUILD_GAPS.includes(name)).map((name) => typecheckPackage(name, true)),
];
return { prepare, tasks };
}
const seconds = (ms) => `${(ms / 1000).toFixed(1)}s`;
function spawnTask(task, { cwd, env, stdio }) {
return spawn(task.command, task.args, {
cwd,
env: { ...env, ...task.env },
stdio,
shell: process.platform === "win32",
});
}
function runCaptured(task, options) {
return new Promise((resolve) => {
const started = Date.now();
const output = [];
let child;
try {
child = spawnTask(task, { ...options, stdio: ["ignore", "pipe", "pipe"] });
} catch (error) {
resolve({ status: 1, output: `${error.message}\n`, durationMs: Date.now() - started });
return;
}
if (task.background && child.pid) {
try {
setPriority(child.pid, BACKGROUND_PRIORITY);
} catch {}
}
child.stdout.on("data", (chunk) => output.push(chunk));
child.stderr.on("data", (chunk) => output.push(chunk));
child.on("error", (error) => output.push(Buffer.from(`${error.message}\n`)));
child.on("close", (code, signal) => {
if (signal) output.push(Buffer.from(`${task.name}: terminated by ${signal}\n`));
resolve({ status: signal ? 1 : (code ?? 1), output: Buffer.concat(output).toString("utf8"), durationMs: Date.now() - started });
});
});
}
function runStreamed(task, options) {
return new Promise((resolve) => {
const started = Date.now();
let child;
try {
child = spawnTask(task, { ...options, stdio: "inherit" });
} catch (error) {
resolve({ status: 1, output: `${error.message}\n`, durationMs: Date.now() - started });
return;
}
child.on("error", (error) => process.stderr.write(`${error.message}\n`));
child.on("close", (code, signal) => {
if (signal) process.stderr.write(`${task.name}: terminated by ${signal}\n`);
resolve({ status: signal ? 1 : (code ?? 1), durationMs: Date.now() - started });
});
});
}
export function createReporter({ write = (text) => process.stdout.write(text), github = Boolean(process.env.GITHUB_ACTIONS) } = {}) {
const groupStart = (title) => write(github ? `::group::${title}\n` : `\n=== ${title}\n`);
const groupEnd = () => write(github ? "::endgroup::\n" : "");
const ensureNewline = (text) => (text.length === 0 || text.endsWith("\n") ? text : `${text}\n`);
return {
prepareStart(task) {
groupStart(`${task.name}: ${task.command} ${task.args.join(" ")}`);
},
prepareEnd(task, result) {
groupEnd();
write(`${result.status === 0 ? "passed" : "FAILED"}: ${task.name} (${seconds(result.durationMs)})\n`);
},
taskEnd(task, result) {
const title = `${task.name}: ${task.command} ${task.args.join(" ")}`;
const output = ensureNewline(result.output);
if (result.status === 0) {
groupStart(`passed in ${seconds(result.durationMs)}: ${title}`);
write(output);
groupEnd();
return;
}
if (github) write(`::error title=Typecheck lane::${task.name} failed with exit code ${result.status}\n`);
write(`\n=== FAILED in ${seconds(result.durationMs)} with exit code ${result.status}: ${title}\n${output}=== end of ${task.name}\n`);
},
taskSkipped(task, missing) {
write(`skipped: ${task.name}, because ${missing.join(", ")} did not pass\n`);
},
summary(rows) {
const width = Math.max(...rows.map((row) => row.name.length));
write("\nTypecheck lane summary (start offset, duration, result):\n");
for (const row of rows) {
const start = row.startMs === undefined ? "" : seconds(row.startMs);
const duration = row.durationMs === undefined ? "" : seconds(row.durationMs);
write(` ${row.name.padEnd(width)} ${start.padStart(7)} ${duration.padStart(7)} ${row.result}\n`);
}
},
};
}
export async function runLane({
prepare,
tasks,
concurrency,
cwd = process.cwd(),
env = process.env,
runPrepare = runStreamed,
runTask = runCaptured,
reporter = createReporter(),
now = Date.now,
}) {
const laneStart = now();
const rows = [];
for (const task of prepare) {
reporter.prepareStart(task);
const startMs = now() - laneStart;
const result = await runPrepare(task, { cwd, env });
reporter.prepareEnd(task, result);
rows.push({ name: task.name, startMs, durationMs: result.durationMs, result: result.status === 0 ? "passed" : "FAILED" });
if (result.status !== 0) {
for (const remaining of [...prepare.slice(prepare.indexOf(task) + 1), ...tasks]) {
rows.push({ name: remaining.name, result: "not run" });
}
reporter.summary(rows);
return result.status || 1;
}
}
const states = new Map(tasks.map((task) => [task.name, "pending"]));
const results = new Map();
const running = new Set();
await new Promise((resolve) => {
const schedule = () => {
for (const task of tasks) {
if (states.get(task.name) !== "pending") continue;
const needs = task.needs ?? [];
const missing = needs.filter((name) => ["failed", "skipped"].includes(states.get(name)));
if (missing.length > 0) {
states.set(task.name, "skipped");
reporter.taskSkipped(task, missing);
continue;
}
if (running.size >= concurrency || !needs.every((name) => states.get(name) === "passed")) continue;
states.set(task.name, "running");
const startMs = now() - laneStart;
const promise = runTask(task, { cwd, env }).then((result) => {
running.delete(promise);
states.set(task.name, result.status === 0 ? "passed" : "failed");
results.set(task.name, { ...result, startMs });
reporter.taskEnd(task, result);
schedule();
});
running.add(promise);
}
if (running.size === 0) resolve();
};
schedule();
});
let exitCode = 0;
for (const task of tasks) {
const state = states.get(task.name);
const result = results.get(task.name);
if (state !== "passed") exitCode ||= result?.status || 1;
rows.push({
name: task.name,
startMs: result?.startMs,
durationMs: result?.durationMs,
result: state === "passed" ? "passed" : state === "failed" ? `FAILED (exit ${result.status})` : "skipped",
});
}
reporter.summary(rows);
return exitCode;
}
function listBuildGapPackages(root) {
const result = spawnSync(process.execPath, ["scripts/run-typecheck-build-gaps.mjs", "--list-packages"], {
cwd: root,
encoding: "utf8",
stdio: ["ignore", "pipe", "inherit"],
});
if (result.error) throw result.error;
if (result.status !== 0) return null;
return JSON.parse(result.stdout.trim().split("\n").at(-1));
}
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
const args = process.argv.slice(2);
const concurrencyIndex = args.indexOf("--concurrency");
const concurrency = concurrencyIndex === -1 ? availableParallelism() : Number(args[concurrencyIndex + 1]);
const extra = args.filter((_, index) => concurrencyIndex === -1 || (index !== concurrencyIndex && index !== concurrencyIndex + 1));
if (!Number.isInteger(concurrency) || concurrency < 1 || extra.length > 0) {
console.error("Usage: node scripts/ci-typecheck-lane.mjs [--concurrency <n>]");
process.exitCode = 2;
} else {
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
const buildGapPackages = listBuildGapPackages(root);
if (buildGapPackages === null) {
process.exitCode = 1;
} else {
const testLayers = listLayers(path.join(root, "server"))
.filter((layer) => !layer.emitsDeclarations)
.map((layer) => layer.name);
console.log(`typecheck lane: ${concurrency} at a time; build gaps: ${buildGapPackages.join(", ")}`);
process.exitCode = await runLane({ ...planLane({ buildGapPackages, testLayers }), concurrency, cwd: root });
}
}
}