Files
PaperClipAI/packages/paperclip-runner/scripts/materialize-kimi-distribution.test.mjs
T
DottaandClaude Opus 5.5 7a9177af0f test(runner): remove coreutils and pid-file timing assumptions from static checks (#21787)
## Thinking Path

> - Paperclip is the open source app people use to manage AI agents for
work
> - The Paperclip Runner package has a "static checks" CI job that runs
`node --test` script tests
> - Two of these tests failed on unrelated pull requests on 2026-11-15
and passed on re-run
> - Each failure costs a re-run of the failed jobs and delays merges,
and it teaches people to re-run instead of read
> - One test assumed that a copied `/bin/sleep` acts as `sleep` under
any name. The other read a pid file before the child finished writing it
> - This pull request removes both assumptions, and one more instance of
the second pattern in the same test family
> - The benefit is that these tests no longer depend on the runner image
or on process scheduling

## Linked Issues or Issue Description

**What happened?**

1. `scripts/stage-runner-binary.test.mjs` failed in "ci / Verify
Paperclip Runner (static checks)" on PR #21765, head `98197c87e9`, job
132850068174:
- `atomically publishes while the previous executable is running`:
`AssertionError: 1 !== null` at line 124 (`old.child.exitCode`), after
77 ms.
- `a source rebuild selects fresh dev bytes ...`: `Expected "actual" not
to be strictly deep-equal` (line 192).
- That job ran on a GitHub-hosted `ubuntu-26.04` image (20261111.82.6).
The same tests pass on the RunsOn `ubuntu24` image, for example job
132822472729.
2. `scripts/materialize-kimi-distribution.test.mjs` › `setup
cancellation physically stops an in-flight owned child` failed in the
same job on PR #21758, head `40e38fc28a`, job 132822472729:
`AssertionError: The real child did not start` after 149 ms.

**Expected behavior**
The tests pass on every supported runner image and on a loaded runner.

**Steps to reproduce**
1. stage-runner-binary: in a Linux container, replace `/bin/sleep` and
`/bin/echo` with one multicall binary that picks the utility from
`argv[0]` and exits 1 for an unknown name (this is how uutils coreutils
works on Ubuntu 26.04). Run `node --test
scripts/stage-runner-binary.test.mjs`. On `master` both tests fail with
the CI messages, 20 of 20 runs.
2. kimi: make the child create the pid file, wait 300 ms, then write the
pid. On `master` the test fails with `The real child did not start` in
about 125 ms, the same as CI.

**Root cause**
1. On Ubuntu 26.04, `/bin/sleep` and `/bin/echo` are the same uutils
multicall binary. The test copies it to `paperclip-runnerd` and runs it.
The binary sees `argv[0] = paperclip-runnerd`, finds no utility of that
name, and exits 1. The fresh-build fixture (a copy of `echo`) also has
the same bytes as the old artifact (a copy of `sleep`), so the "fresh
bytes" assertion fails too.
2. The child wrote the pid with `writeFileSync`, which creates the file
and then writes it. The test loop broke on the first successful read. An
empty read gave `Number("") === 0`, so the loop stopped and the
assertion failed. The loop also had a fixed budget of 100 polls of 10 ms
for a Node child to start.

**Production impact**
None. Only tests change. The staging code works with any executable. The
Kimi setup cancellation code is correct: the deterministic repro shows
that the child is stopped once the test reads a real pid.

## What Changed

- `stage-runner-binary.test.mjs`: the test helper passes `argv0` to
`spawn`. The copied `sleep` runs with `argv0: "sleep"`, so a multicall
binary still acts as `sleep`. The fresh-build fixture is now a copy of
`/bin/sh` and runs `-c "exit 0"`, so its bytes always differ from the
old `sleep` artifact.
- `materialize-kimi-distribution.test.mjs`: the child writes `pid.tmp`
and renames it to `pid`, so a read never sees a partial file. The test
waits until the pid is valid or the command settles. The 3 s command
timeout and 5 s test timeout are unchanged.
- `provision-kimi-package.test.mjs`: the wait for the `download-ready`
marker continues until the child exits, instead of a fixed 1 s budget.
The existing 5 s kill timer still bounds it.
- No retries, skips or timeout changes.

## Verification

- stage-runner-binary, Linux container (`node:24-bookworm-slim`) with a
multicall `sleep`/`echo` built from a 15-line Go program that dispatches
on `argv[0]`:
  - `master`: 0/20 pass, same two assertions as CI.
  - this branch: 50/50 pass.
  - this branch with stock coreutils, and on macOS: all 9 tests pass.
- kimi setup cancellation:
  - delayed-write repro: `master` fails, this branch passes.
- loop on macOS, `master` test without extra load: 59/60 (one natural
failure). This branch: 60/60.
- loop with 4 extra busy-loop processes (load average about 13 before
them): `master` 100/100, this branch 100/100.
- `node --test scripts/stage-runner-binary.test.mjs
scripts/materialize-kimi-distribution.test.mjs
scripts/provision-kimi-package.test.mjs`: 19 pass, 1 skipped (public
download test, opt-in).
- The other CI flake from the same day,
`runnerd-codex-transport-orphaned-tool-at-attach.test.ts`, is a Runner
defect. It is fixed in a separate pull request.

## Risks

- Low risk. Only tests change.
- The rebuild test now uses `/bin/sh` as the fixture for the fresh
build. This file exists on every supported Linux and macOS runner.

## Model Used

- Claude Opus 5.5 (`claude-opus-5-5`), Anthropic, used through Claude
Code with tool use (shell, file edit, GitHub CLI, Docker).

## 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 Opus 5.5 <noreply@anthropic.com>
2026-11-15 10:13:19 -06:00

124 lines
8.3 KiB
JavaScript

import assert from "node:assert/strict";
import { createHash } from "node:crypto";
import { chmod, link, mkdir, mkdtemp, readFile, readdir, realpath, rm, symlink, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import test from "node:test";
import { setTimeout as delay } from "node:timers/promises";
import { assertKimiNodeSystemDependencies, buildKimiDistribution, kimiDistributionEnvironment, kimiDistributionInstallCommand, materializeKimiDistribution, runKimiBuildCommand, verifyLockedKimiPackageGraph } from "./materialize-kimi-distribution.mjs";
import { KIMI_DISTRIBUTION_LOCK_SHA256, KIMI_NODE_DISTRIBUTIONS } from "../src/drivers/acpx/kimi-closure-pins.ts";
import { inventoryKimiRuntimeFiles, kimiRuntimeClosureDigest } from "../src/drivers/acpx/kimi-runtime-files.ts";
async function fixture(t) {
const root = await realpath(await mkdtemp(join(tmpdir(), "paperclip-kimi-distribution-test-")));
t.after(() => rm(root, { recursive: true, force: true }));
const write = async (path, content) => { const file = join(root, path); await mkdir(dirname(file), { recursive: true }); await writeFile(file, content); };
return { root, write };
}
test("Kimi's portable graph pins public integrity and excludes lifecycle and optional TUI dependencies", async () => {
const bytes = await readFile(new URL("./kimi-distribution/package-lock.json", import.meta.url));
assert.equal(createHash("sha256").update(bytes).digest("hex"), KIMI_DISTRIBUTION_LOCK_SHA256);
const lock = JSON.parse(bytes.toString("utf8"));
const pkg = JSON.parse(await readFile(new URL("./kimi-distribution/package.json", import.meta.url), "utf8"));
assert.deepEqual(pkg.dependencies, { "@moonshot-ai/kimi-code": "2.3.3" });
assert.deepEqual(lock.packages[""].dependencies, pkg.dependencies);
assert.equal(lock.packages["node_modules/@moonshot-ai/kimi-code"].integrity, "sha512-3V8bycucf9KgPRH27S8De2u0ip84/Ry3FpNn6gi5z9JmoEHt9vxiYHHZqiMdyDf5nPm4bv+YKGwKEQL4oYpRTw==");
assert.equal(kimiDistributionInstallCommand()[0], "ci");
assert.ok(kimiDistributionInstallCommand().includes("--ignore-scripts"));
assert.ok(kimiDistributionInstallCommand().includes("--omit=optional"));
assert.ok(kimiDistributionInstallCommand().includes("--registry=https://registry.npmjs.org"));
assert.deepEqual(Object.keys(KIMI_NODE_DISTRIBUTIONS).sort(), ["darwin-arm64", "linux-x64"]);
});
test("distribution build admits network settings without provider keys or Node injection", () => {
const network = { PATH: "/usr/bin:/bin", LANG: "C.UTF-8", HTTPS_PROXY: "http://proxy.invalid", NODE_EXTRA_CA_CERTS: "/public/ca.pem" };
assert.deepEqual(kimiDistributionEnvironment({ ...network, HOME: "/private/home", KIMI_MODEL_API_KEY: "canary-secret", NPM_TOKEN: "canary-secret", NODE_OPTIONS: "--require /foreign.js", NODE_PATH: "/foreign", NODE_TLS_REJECT_UNAUTHORIZED: "0" }), network);
});
test("setup cancellation physically stops an in-flight owned child", { timeout: 5_000 }, async t => {
const { root } = await fixture(t), pidFile = join(root, "pid");
const controller = new AbortController();
const result = runKimiBuildCommand(process.execPath, ["-e", "const f=require('node:fs');f.writeFileSync(process.argv[1]+'.tmp',String(process.pid));f.renameSync(process.argv[1]+'.tmp',process.argv[1]);setInterval(()=>{},1000)", pidFile], { env: {}, timeout: 3_000 }, undefined, controller.signal).then(() => null, error => error);
let pid, settled = false;
void result.then(() => { settled = true; });
try {
while (!settled && !(Number.isSafeInteger(pid) && pid > 0)) {
try { pid = Number(await readFile(pidFile, "utf8")); }
catch (error) { if (error.code !== "ENOENT") throw error; }
await delay(10);
}
assert.ok(Number.isSafeInteger(pid) && pid > 0, "The real child did not start");
controller.abort();
assert.equal((await result).code, "ABORT_ERR");
assert.throws(() => process.kill(pid, 0), error => error.code === "ESRCH");
} finally { controller.abort(); await result; }
});
test("setup cancellation aborts the pending download and removes its staging tree", { timeout: 5_000 }, async t => {
const { root } = await fixture(t), controller = new AbortController();
let started;
const downloading = new Promise(resolve => { started = resolve; });
t.mock.method(globalThis, "fetch", (_url, options) => new Promise((_resolve, reject) => {
started(); options.signal.addEventListener("abort", () => reject(options.signal.reason), { once: true });
}));
const result = buildKimiDistribution({ outputRoot: join(root, "output"), signal: controller.signal });
await downloading; controller.abort();
await assert.rejects(result, error => error.name === "AbortError");
assert.deepEqual(await readdir(root), []);
});
test("the installed graph rejects version drift and optional packages", async t => {
const { root, write } = await fixture(t);
const entry = { version: "2.3.3", resolved: "https://registry.npmjs.org/@moonshot-ai/kimi-code/-/kimi-code-2.3.3.tgz", integrity: "sha512-YWJjZA==" };
const lock = { lockfileVersion: 3, packages: { "": { dependencies: { "@moonshot-ai/kimi-code": "2.3.3" } }, "node_modules/@moonshot-ai/kimi-code": entry, "node_modules/optional-native": { ...entry, optional: true } } };
await write("node_modules/@moonshot-ai/kimi-code/package.json", '{"version":"2.3.3"}');
assert.equal(await verifyLockedKimiPackageGraph(root, lock), 1);
await write("node_modules/@moonshot-ai/kimi-code/package.json", '{"version":"2.3.4"}');
await assert.rejects(verifyLockedKimiPackageGraph(root, lock), /differs from its lock/);
await write("node_modules/@moonshot-ai/kimi-code/package.json", '{"version":"2.3.3"}');
await write("node_modules/optional-native/package.json", '{"version":"2.3.3"}');
await assert.rejects(verifyLockedKimiPackageGraph(root, lock), /optional TUI dependency/);
});
test("inventory binds native assets, workers, permissions and bytes in stable path order", async t => {
const { root, write } = await fixture(t);
await write("node_modules/provider/worker.mjs", "worker");
await write("node_modules/provider/native/addon.node", Buffer.from([0, 1, 2, 3]));
await write("node/bin/node", "node");
const first = await inventoryKimiRuntimeFiles(root);
assert.deepEqual(first.map(entry => entry.path), ["node/bin/node", "node_modules/provider/native/addon.node", "node_modules/provider/worker.mjs"]);
const digest = kimiRuntimeClosureDigest(first);
await write("node_modules/provider/worker.mjs", "worker changed");
assert.notEqual(kimiRuntimeClosureDigest(await inventoryKimiRuntimeFiles(root)), digest);
await chmod(join(root, "node/bin/node"), 0o755);
assert.equal((await inventoryKimiRuntimeFiles(root))[0].executable, true);
});
test("inventory rejects external links, hardlinks and special files", async t => {
const { root, write } = await fixture(t);
await write("original", "content");
await symlink("/outside", join(root, "escape"));
await assert.rejects(inventoryKimiRuntimeFiles(root), /symbolic link/);
await rm(join(root, "escape"));
await link(join(root, "original"), join(root, "alias"));
await assert.rejects(inventoryKimiRuntimeFiles(root), /invalid file/);
});
test("setup refuses relative, root, unsupported and existing outputs before network work", async t => {
const { root } = await fixture(t);
for (const operation of [buildKimiDistribution, materializeKimiDistribution]) {
await assert.rejects(operation({ outputRoot: "relative" }), /must be absolute/);
await assert.rejects(operation({ outputRoot: "/" }), /unsafe/);
await assert.rejects(operation({ outputRoot: join(root, "output"), target: "linux-arm64" }), /unsupported/);
await assert.rejects(operation({ outputRoot: root }), /already exists/);
}
});
test("the bundled Node may depend only on platform system libraries", () => {
assert.doesNotThrow(() => assertKimiNodeSystemDependencies("node:\n /usr/lib/libSystem.B.dylib (version 0)\n", "darwin"));
assert.throws(() => assertKimiNodeSystemDependencies("node:\n /opt/homebrew/lib/libnode.dylib (version 0)\n", "darwin"), /unbundled/);
assert.doesNotThrow(() => assertKimiNodeSystemDependencies("linux-vdso.so.1 (0x0)\nlibc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x0)\n/lib64/ld-linux-x86-64.so.2 (0x0)\n", "linux"));
assert.throws(() => assertKimiNodeSystemDependencies("libcustom.so => /opt/private/libcustom.so (0x0)\n", "linux"), /unbundled/);
});