mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-04 19:28:46 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - Contributors and agents typecheck this repository, often on machines with limited memory > - paperclipai/paperclip#18737 made `pnpm -r typecheck` fit in 4 GiB because the server now checks in layers > - The test projects outside `pnpm -r` still import server source through relative paths, so each one checks most of the server as one program > - These projects peaked between 6.9 GiB and 14.9 GiB, so they fail on a 4 GiB machine > - This pull request makes these projects read the server layers as declarations, and adds a CI step that keeps them that way > - The benefit is that every TypeScript project in the repository can now be checked in 4 GiB, and a later change cannot silently undo this ## Linked Issues or Issue Description Refs paperclipai/paperclip#18737, which is merged. This pull request builds on the server layers that it added. No issue exists for this gap. **What happened?** `tsc -p tests/runner-e2e/tsconfig.json` peaked at 14.9 GiB at default settings. `tests/speko-native` peaked at 12.2 GiB, `server/src/__tests__/helpers/maintenance` at 9.6 GiB, and `tests/lifecycle-baseline` at 6.9 GiB. On one thread, `tests/runner-e2e` still peaked at 7.7 GiB. **Expected behavior** Each project passes with 4 GiB of memory. **Steps to reproduce** Run `tsc -p tests/runner-e2e/tsconfig.json` in a Linux container with `--memory=4g --memory-swap=4g`. The kernel stops the process. ## What Changed - Added `tsconfig.layers.json` to the four projects. Each file extends the project's `tsconfig.json` and references the generated server declarations. - Added `scripts/typecheck-test-projects.mjs`. It builds the server declarations and then checks the named projects, or all four. It takes `--low-memory`. - Changed `test:e2e:runner:typecheck` and `test:lifecycle-baseline:typecheck` to call that script. `docker-runner-check.yml` calls the first script by name, so that workflow needs no edit. - Added `--declarations-only` to `server/scripts/typecheck-layers.mjs`. The script now also generates `tsconfig.declarations.json`, so hand-written configs do not name individual layers. - Exported `runPnpm` from `scripts/typecheck-low-memory.mjs`, so the new script can use it. - Added a step to the `Typecheck + Release Registry` job. It runs straight after the server typecheck and reuses its declarations through `--reuse-declarations`. - The script now fails if a project loads server source, even when the types pass. Test support files, declaration files and JSON files do not count. This check does not enforce a memory limit. - Added a unit test that fails if a new project under `tests/` is neither in `PROJECTS` nor named as a single program. `tests/runner-acceptance` is the one named project. - Added `server/src/types/express.d.ts` to `tests/lifecycle-baseline/tsconfig.json`. That plain config failed alone with 11 TS2339 errors, and it now passes. - Exported `isTestFile` from `server/scripts/typecheck-layers.mjs`, so the source check uses the same rule as the layers. - Turned off `declaration` in the layered configs. These projects emit nothing, and declaration checks gave a false TS2883 error when `tests/runner-e2e` read the server as declarations. - Added tests, one of which runs the real compiler. Updated `doc/DEVELOPING.md` and `tests/lifecycle-baseline/README.md`. The references are not in each project's `tsconfig.json` because Vitest reads that file. A test showed that Vitest fails when a referenced config does not exist yet. The `tsconfig.json` files do not change, so editors and Vitest see the same configs as before. No new `package.json` scripts were added, as `.greptile/rules.md` requires. ## Verification I tested commit `9763880fab`, which is based on `1c9ca5829f`. I copied the commit into a named Docker volume and ran a fresh install. Each row ran in its own Linux arm64 container with `--memory=4g --memory-swap=4g` and 8 CPUs. The first row started with no build output. All rows used `--low-memory`. CI does not set a memory limit. I used the limit only for this measurement. | Project | Errors | Peak (cgroup) | Max process RSS | OOM kills | Time | | --- | --- | --- | --- | --- | --- | | `tests/runner-e2e` | 0 | 3.34 GiB | 3.06 GiB | 0 | 57 s | | `tests/speko-native` | 0 | 3.60 GiB | 3.27 GiB | 0 | 51 s | | `tests/lifecycle-baseline` | 0 | 3.43 GiB | 3.15 GiB | 0 | 50 s | | `server/src/__tests__/helpers/maintenance` | 0 | 3.39 GiB | 3.13 GiB | 0 | 52 s | `PAPERCLIP_TYPECHECK_LOW_MEMORY=1 pnpm test:e2e:runner:typecheck` and `PAPERCLIP_TYPECHECK_LOW_MEMORY=1 pnpm test:lifecycle-baseline:typecheck` also passed in the same limit, with peaks of 3.55 GiB and 3.52 GiB. I also ran the CI sequence in one container with the same limit: `pnpm --filter @paperclipai/server typecheck`, then `node scripts/typecheck-test-projects.mjs --low-memory --reuse-declarations`. Both passed, with a peak of 3.46 GiB. The new step took 16 s. The server layer build sets the peak in each row. Against the declarations, the projects peaked between 0.9 GiB and 2.2 GiB at default settings on macOS. Server source files that each project loads, without test support, declaration or JSON files: | Project | Plain `tsconfig.json` | `tsconfig.layers.json` | | --- | --- | --- | | `tests/runner-e2e` | 1,345 | 0 | | `tests/speko-native` | 1,647 | 0 | | `tests/lifecycle-baseline` | 1,343 | 0 | | `server/src/__tests__/helpers/maintenance` | 1,716 | 0 | I counted these on `3f0f31b2f9`. The source check in the script confirmed 0 for all four projects on this commit. **Limits of this proof:** - Without `--low-memory`, the 4 GiB run does not pass. The kernel stopped the layer build for `tests/runner-e2e` on `3f0f31b2f9`, and the script exited with status 1. I did not repeat this run on the final commit. The script does not turn on low-memory mode by itself. - The Linux runs used arm64, not x64. - The new CI step has not run in CI. `pr-trusted.yml` runs from `@master`, so the step first runs after this pull request merges. Check its first run. **Other checks on this commit:** - `server/scripts/typecheck-layers.test.mjs`: 38 tests pass. One test uses the real compiler to show that the source check tells a layered config from a config with no references. - With the references removed from `tests/speko-native/tsconfig.layers.json`, the script exits with status 1 and reports 1,644 server source files. - With the declarations removed, `--reuse-declarations` exits with status 1 and reports TS6053. - A type error in a test project makes the script exit with status 1. - `tests/lifecycle-baseline/tsconfig.json` passes with 0 errors. - `tests/lifecycle-baseline` Vitest suite: 533 tests pass. - Workflow tests that read `pr-trusted.yml` and CI routing: 114 tests pass. **Earlier checks:** - `server/src/__tests__/server-package-build-script.test.ts` passed 10 tests on `3f0f31b2f9`. This test failed in `ci / General tests (server (9/12))` on an older head. It tested a build script split that the final #18737 removed. - A changed signature in `server/src/services/run-liveness.ts` failed `tests/lifecycle-baseline/authority.test.ts` through the rebuilt declarations, before the rebases. When I restored the signature, the check passed. **Not run locally:** - `pnpm test:run` and `pnpm build`. CI covers these. **Failures that this pull request does not cause:** `pnpm test:e2e:runner:unit` has 13 failures on this commit, in `cursor-cloud-replay-fault`, `first-task`, `operating-receipts`, `pi-controls-evidence` and `workflow-security`. The same 13 tests fail on `master` at `6ef37398c8`. A comparison of the test names shows no differences. PR CI does not run this suite. ## Risks - Low risk to runtime behavior. No shipped code changes. - **Slower check:** `pnpm test:e2e:runner:typecheck` now builds the server layers on each run. Before the rebase, it took 30 s on macOS at default settings, about the same as the old single program for that project. On this commit it took 57 s in low-memory mode in the 4 GiB container. The new CI step reuses the declarations, so it took 16 s. - **Narrower check:** Server source is now checked only by the server's own layers, not again under each project's compiler options. The projects also skip declaration-portability checks. These projects emit nothing, so those checks had no output to protect. - **Unchanged path:** `tests/runner-e2e/native-completion-checks.mjs` still runs `tsc -p tests/runner-e2e/tsconfig.json` as one program. It needs about 15 GiB at default settings. I did not change it because that file and its inputs are part of a fingerprinted source contract, and no workflow calls it. - **Layer growth:** The new checks do not measure memory. If the layers or the import cycle grow, the checks still pass and the peak rises. This is intentional. Crossing 4 GiB does not fail a build. - **Project list:** The CI step checks only the projects in `PROJECTS`. The unit test covers new projects under `tests/`, but not a new project in another directory. - **Reused declarations:** `--reuse-declarations` trusts the declarations on disk. CI uses it straight after the server typecheck on the same commit. If that typecheck fails, the job stops first. Stale declarations can pass locally, so `doc/DEVELOPING.md` warns against this. - **Job dependency:** A type error in a test project now fails `Typecheck + Release Registry`, and so `ci / verify`. - **Not covered:** The new step runs no tests. `node scripts/typecheck-low-memory.mjs` and the Docker image build also have no new guard. > For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and discuss it in `#dev` before opening the PR. Feature PRs that overlap with planned core work may need to be redirected — check the roadmap first. See `CONTRIBUTING.md`. ## Model Used - Provider: Anthropic. Model: Claude Opus 5.5 (`claude-opus-5-5`), through 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 - [x] All Paperclip CI gates are green - [x] 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 Opus 5.5 <noreply@anthropic.com>
281 lines
10 KiB
JavaScript
281 lines
10 KiB
JavaScript
// Typechecks the server in layers so that no single tsc process holds the whole
|
|
// program in memory.
|
|
//
|
|
// tsc keeps every type it creates until it exits, so its peak grows with the
|
|
// source it checks. The server is about 1,700 source files and 500 test files,
|
|
// and one program over all of them needs more than 8 GiB at any setting. Most
|
|
// of those files are not in an import cycle, so they can be checked separately
|
|
// and read back as declarations:
|
|
//
|
|
// below files the largest import cycle depends on, plus unrelated files
|
|
// cycle the largest import cycle, reading `below` as declarations
|
|
// above files that depend on the cycle, reading both as declarations
|
|
// tests test files, reading all three as declarations
|
|
//
|
|
// The layers come from the import graph on every run, so nothing has to move
|
|
// and no list can go stale. The import scan does not have to be perfect: if it
|
|
// misses an edge, a file imports one outside its layer's references and tsc
|
|
// fails with TS6307 instead of skipping the file.
|
|
//
|
|
// server/tsconfig.json stays the single program that editors and `pnpm build`
|
|
// use.
|
|
//
|
|
// Projects outside the server read the source layers as declarations through a
|
|
// `tsconfig.layers.json` that references tsconfig.declarations.json in the output
|
|
// directory. --declarations-only builds those layers and skips the tests layer.
|
|
//
|
|
// Usage: node scripts/typecheck-layers.mjs [--declarations-only]
|
|
|
|
import { mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
import path from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
import { runTsc } from "../../scripts/run-tsc.mjs";
|
|
|
|
const IMPORT_SPECIFIER = /(?:from\s+|import\s*\(\s*|import\s+)["'](\.[^"']+)["']/g;
|
|
const OUTPUT_DIRECTORY = path.join("node_modules", ".cache", "paperclip-typecheck");
|
|
|
|
export const isTestFile = (file) => /\.test\.tsx?$/.test(file) || file.split("/").includes("__tests__");
|
|
// tsconfig.json excludes this tree. A test reaches its helpers through imports.
|
|
const isExcludedFromProgram = (file) => file.startsWith("src/__tests__/");
|
|
|
|
export function listSourceFiles(serverRoot) {
|
|
const files = [];
|
|
const walk = (directory) => {
|
|
for (const entry of readdirSync(path.join(serverRoot, directory), { withFileTypes: true })) {
|
|
const relative = `${directory}/${entry.name}`;
|
|
if (entry.isDirectory()) walk(relative);
|
|
else if (/\.tsx?$/.test(entry.name)) files.push(relative);
|
|
}
|
|
};
|
|
walk("src");
|
|
return files.sort();
|
|
}
|
|
|
|
/** Resolves the relative imports of each file. `files` maps a path to its text. */
|
|
export function readImports(files) {
|
|
const imports = new Map();
|
|
for (const [file, text] of files) {
|
|
const modules = new Set();
|
|
const json = new Set();
|
|
for (const [, specifier] of text.matchAll(IMPORT_SPECIFIER)) {
|
|
const target = path.posix.join(path.posix.dirname(file), specifier);
|
|
if (target.endsWith(".json")) {
|
|
json.add(target);
|
|
continue;
|
|
}
|
|
const candidates = [
|
|
target,
|
|
target.replace(/\.js$/, ".ts"),
|
|
target.replace(/\.js$/, ".tsx"),
|
|
`${target}.ts`,
|
|
`${target}/index.ts`,
|
|
];
|
|
const resolved = candidates.find((candidate) => files.has(candidate));
|
|
if (resolved) modules.add(resolved);
|
|
}
|
|
imports.set(file, { modules, json });
|
|
}
|
|
return imports;
|
|
}
|
|
|
|
/** Tarjan's algorithm, iterative because import chains can be very deep. */
|
|
function largestCycle(graph) {
|
|
const index = new Map();
|
|
const low = new Map();
|
|
const onStack = new Set();
|
|
const stack = [];
|
|
let next = 0;
|
|
let largest = [];
|
|
|
|
for (const root of graph.keys()) {
|
|
if (index.has(root)) continue;
|
|
const work = [{ node: root, edges: [...graph.get(root)], position: 0 }];
|
|
index.set(root, next);
|
|
low.set(root, next);
|
|
next += 1;
|
|
stack.push(root);
|
|
onStack.add(root);
|
|
while (work.length > 0) {
|
|
const frame = work[work.length - 1];
|
|
if (frame.position < frame.edges.length) {
|
|
const edge = frame.edges[frame.position];
|
|
frame.position += 1;
|
|
if (!index.has(edge)) {
|
|
index.set(edge, next);
|
|
low.set(edge, next);
|
|
next += 1;
|
|
stack.push(edge);
|
|
onStack.add(edge);
|
|
work.push({ node: edge, edges: [...graph.get(edge)], position: 0 });
|
|
} else if (onStack.has(edge)) {
|
|
low.set(frame.node, Math.min(low.get(frame.node), index.get(edge)));
|
|
}
|
|
continue;
|
|
}
|
|
work.pop();
|
|
const parent = work[work.length - 1];
|
|
if (parent) low.set(parent.node, Math.min(low.get(parent.node), low.get(frame.node)));
|
|
if (low.get(frame.node) === index.get(frame.node)) {
|
|
const component = [];
|
|
let member;
|
|
do {
|
|
member = stack.pop();
|
|
onStack.delete(member);
|
|
component.push(member);
|
|
} while (member !== frame.node);
|
|
if (component.length > largest.length) largest = component;
|
|
}
|
|
}
|
|
}
|
|
return largest.length > 1 ? new Set(largest) : new Set();
|
|
}
|
|
|
|
function reachableFrom(start, graph) {
|
|
const seen = new Set(start);
|
|
const pending = [...start];
|
|
while (pending.length > 0) {
|
|
for (const edge of graph.get(pending.pop()) ?? []) {
|
|
if (!seen.has(edge)) {
|
|
seen.add(edge);
|
|
pending.push(edge);
|
|
}
|
|
}
|
|
}
|
|
return seen;
|
|
}
|
|
|
|
/**
|
|
* Sorts every file into the layers, in check order. `files` maps a path to its
|
|
* text. Declaration files are ambient, so every layer lists them.
|
|
*/
|
|
export function planLayers(files) {
|
|
const declarations = [...files.keys()].filter((file) => file.endsWith(".d.ts"));
|
|
const candidates = new Map([...files].filter(([file]) => !file.endsWith(".d.ts")));
|
|
const imports = readImports(candidates);
|
|
|
|
const graph = new Map();
|
|
const reversed = new Map();
|
|
for (const file of candidates.keys()) {
|
|
if (!isTestFile(file)) {
|
|
graph.set(file, new Set());
|
|
reversed.set(file, new Set());
|
|
}
|
|
}
|
|
for (const file of graph.keys()) {
|
|
for (const target of imports.get(file).modules) {
|
|
if (graph.has(target)) {
|
|
graph.get(file).add(target);
|
|
reversed.get(target).add(file);
|
|
}
|
|
}
|
|
}
|
|
|
|
const cycle = largestCycle(graph);
|
|
const dependents = reachableFrom(cycle, reversed);
|
|
const above = [...dependents].filter((file) => !cycle.has(file));
|
|
const below = [...graph.keys()].filter((file) => !dependents.has(file));
|
|
const tests = [...candidates.keys()].filter((file) => isTestFile(file) && !isExcludedFromProgram(file));
|
|
|
|
const layer = (name, members, references) => {
|
|
const sorted = [...members].sort();
|
|
const json = new Set(sorted.flatMap((file) => [...imports.get(file).json]));
|
|
return { name, sourceFiles: sorted, files: [...sorted, ...[...json].sort(), ...declarations], references };
|
|
};
|
|
return [
|
|
layer("below", below, []),
|
|
layer("cycle", cycle, ["below"]),
|
|
layer("above", above, ["below", "cycle"]),
|
|
layer("tests", tests, ["below", "cycle", "above"]),
|
|
].filter((candidate) => candidate.sourceFiles.length > 0);
|
|
}
|
|
|
|
/** Builds the tsconfig for one layer. Paths are relative to the output directory. */
|
|
export function layerConfig(layer, { emptyLayers = new Set() } = {}) {
|
|
const fromOutput = (file) => path.posix.join("../../..", file);
|
|
const references = layer.references
|
|
.filter((name) => !emptyLayers.has(name))
|
|
.map((name) => ({ path: `./tsconfig.${name}.json` }));
|
|
const output =
|
|
layer.name === "tests"
|
|
? { composite: false, declaration: false, declarationMap: false, sourceMap: false, noEmit: true }
|
|
: {
|
|
composite: true,
|
|
declaration: true,
|
|
declarationMap: false,
|
|
sourceMap: false,
|
|
emitDeclarationOnly: true,
|
|
outDir: "./dts",
|
|
tsBuildInfoFile: `./${layer.name}.tsbuildinfo`,
|
|
};
|
|
return {
|
|
extends: fromOutput("tsconfig.json"),
|
|
compilerOptions: { ...output, rootDir: fromOutput("src") },
|
|
files: layer.files.map(fromOutput),
|
|
include: [],
|
|
references,
|
|
};
|
|
}
|
|
|
|
/** Builds the config other projects reference to read the source layers as declarations. */
|
|
export function declarationsConfig(layers) {
|
|
return {
|
|
compilerOptions: { composite: true },
|
|
files: [],
|
|
references: layers
|
|
.filter((layer) => layer.name !== "tests")
|
|
.map((layer) => ({ path: `./tsconfig.${layer.name}.json` })),
|
|
};
|
|
}
|
|
|
|
export function typecheckLayers({
|
|
serverRoot,
|
|
declarationsOnly = false,
|
|
run = runTsc,
|
|
log = console.log,
|
|
}) {
|
|
const sourceFiles = listSourceFiles(serverRoot);
|
|
const layers = planLayers(
|
|
new Map(sourceFiles.map((file) => [file, readFileSync(path.join(serverRoot, file), "utf8")])),
|
|
);
|
|
const planned = new Set(layers.map((layer) => layer.name));
|
|
const emptyLayers = new Set(["below", "cycle", "above", "tests"].filter((name) => !planned.has(name)));
|
|
|
|
const outputRoot = path.join(serverRoot, OUTPUT_DIRECTORY);
|
|
rmSync(outputRoot, { recursive: true, force: true });
|
|
mkdirSync(outputRoot, { recursive: true });
|
|
// Without this, the path under node_modules would make the declarations CommonJS.
|
|
writeFileSync(path.join(outputRoot, "package.json"), `${JSON.stringify({ type: "module" })}\n`);
|
|
for (const layer of layers) {
|
|
writeFileSync(
|
|
path.join(outputRoot, `tsconfig.${layer.name}.json`),
|
|
`${JSON.stringify(layerConfig(layer, { emptyLayers }), null, 2)}\n`,
|
|
);
|
|
}
|
|
|
|
writeFileSync(
|
|
path.join(outputRoot, "tsconfig.declarations.json"),
|
|
`${JSON.stringify(declarationsConfig(layers), null, 2)}\n`,
|
|
);
|
|
|
|
let exitCode = 0;
|
|
for (const layer of layers) {
|
|
if (declarationsOnly && layer.name === "tests") continue;
|
|
log(`typecheck:layers: ${layer.name} (${layer.sourceFiles.length} files)`);
|
|
const config = path.join(OUTPUT_DIRECTORY, `tsconfig.${layer.name}.json`);
|
|
const { status, signal } = run(["-p", config], { cwd: serverRoot });
|
|
// A killed process leaves no declarations, so later layers would only add noise.
|
|
if (signal) return status || 1;
|
|
// Later layers still run after type errors. Each file is checked in one
|
|
// layer, so this reports everything a single tsc run would.
|
|
if (status !== 0) exitCode = status;
|
|
}
|
|
return exitCode;
|
|
}
|
|
|
|
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
|
|
process.exitCode = typecheckLayers({
|
|
serverRoot: path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."),
|
|
declarationsOnly: process.argv.includes("--declarations-only"),
|
|
});
|
|
}
|