mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-17 21:34:48 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - The Runner is the part that executes agent work: a daemon (`paperclip-runnerd`) and a provider pack of agent tools > - Part 2 of this stack builds the Runner alone as the image `runner-artifacts`, in one layer of about 850 MB > - Most of those bytes are provider binaries and Node. They change only with a provider version or the Node pin, but one layer means that every Runner source change makes all 850 MB new > - About half of the image is the pack's transport archives. A consumer that copies the pack as files does not need them > - This pull request splits the image into layers by how often their content changes, and adds a slim variant without the transport archives > - The benefit is that a new image shares about 93% of its bytes with the one before it, and a consumer that needs no transport archives pulls half as much ## Linked Issues or Issue Description Refs #21204 (part 2 of this stack) and #21167 (provider pack layers). No issue exists for this change. **Problem or motivation** The image from part 2 is one layer. A registry and a builder store and move layers, not files. A change to one Runner source file therefore makes a new 850 MB layer, although 790 MB of its content is the same as before. The image also holds the provider pack two times: as files, and as transport archives of those files. Some consumers need only the files. **Proposed solution** Write the image as four layers: the pack's large dependency files, everything else, the transport archives of those large files, and the other transport content. Make the two large layers byte-identical from build to build. Add the target `runner-artifacts-slim`, which is the first two layers. The full image is the slim image plus the two transport layers, as the same layer blobs. Check all of this on the built images. **Alternatives considered** - Two unrelated images. Then the registry stores the shared content two times unless the layers happen to match, and nothing ties the two together. - Split by path only, with all files of 1 MiB or more in the first layer. Six of those files are the Runner's own bundles under `dist`. They change with the Runner's source, so the first layer would change with it too. - Leave the transport archives as they were written. Their bytes then depended on timing (see "What Changed"), and the archive layer never repeated. **Roadmap alignment** This is build infrastructure. It adds no product feature and does not overlap an item in `ROADMAP.md`. This is part 3 of 4. Each part is on top of the one before it: 1. #21228 (merged): an optional field in the provider pack manifest 2. #21204: build and check the Runner image, with no publication 3. #21229: layers by change rate, and the slim variant (this pull request) 4. #21230: publish both variants from `master` ## What Changed - `Dockerfile`: two final stages. `runner-artifacts-slim` is two `COPY --link` layers. `runner-artifacts` starts from it and adds two more. The build stage sorts its output into the four layers. Every path is where it was in part 2: consumers copy by path and see no difference. | Layer | Content | Changes with | | --- | --- | --- | | 1 | The pack's dependency files of 1 MiB or more: provider binaries and Node | A provider version or the Node pin | | 2 | Everything else, with `runner-image.json` | Every Runner source change | | 3 (full only) | The transport archive of each file in layer 1 | The same as layer 1 | | 4 (full only) | The other transport archives, the overlay archive and the receipt | The same as layer 2 | - Stable bytes. The files of layers 1 and 3 get timestamp 0, and the large dependency files get it before their transport archives are written. A file under the pack's `dist` is the Runner's own output, so it and its archive are in layers 2 and 4. - `scripts/create-provider-pack-archive.mjs`, two separate commits. **This script also writes the application image's archives.** - The same file now gives the same archive every time. The script piped tar into Node's zstd compressor. When the pipe ended while the compressor still had its last chunk queued, Node finished the zstd stream two times, and the archive got an empty frame of nine bytes at its end. That depended on timing. In the pinned Linux base, 37 of 60 runs of the old code had the frame. The compressor is now ended only after it has taken every chunk. - Archive N holds file N of the receipt, and the receipt listed the files in path order. `dist` sorts before `node_modules`, so one more large file in the Runner's own output gave every dependency file another archive name, and layer 3 became a new blob with the same contents. The receipt now lists the files outside `dist` first. The readers (`provider-pack-layers.ts`, `retained-provider-pack-source.ts`) pair file N with `N.tar.zst` and do not use the path order, so they need no change. For the application image, the archive numbers change one time, and the bytes of each archive do not change. - `runner-image.json` is the same file in both variants. Its new field `fullImageOnly` names the two paths that only the full image has. `paths` did not change. - `scripts/runner-image-layout.mjs check` takes `--variant slim|full`. It is required and has no default. A slim tree that holds a transport path fails. A full tree without them fails. - `scripts/runner-image-oci.mjs` (new, 585 lines). It is not an input of the image, because the image build does not run it. - `unpack <oci archive> --out <tree>` writes the tree of an image from the layers of its archive, in order. It checks every blob against its digest. It refuses a whiteout, an absolute path, a path with `..`, a path that is written two times, an entry behind a link, and every entry that is not a file, a directory or a link. - `check-layers --slim <oci archive> --full <oci archive>` checks the table above on the built images. It reads the receipt to decide which archive belongs in which layer. It fails when the full image does not start with the slim image's layer blobs in the same order, when a path that is not a directory is in two layers, and when a shared layer holds transport content under any name or directory. - The script reads the archive and its layers itself. It does not use the system's `tar`, so every platform reads them the same way. It finds a member only under the name that buildx gives it. - `.github/workflows/runner-image.yml`: each leg builds each variant one time, as an OCI archive and nothing else. It unpacks both archives, runs `check` on each tree with its variant, runs `check-layers`, and runs the smoke matrix on the slim tree. The tree that is checked is therefore the bytes of the image, not a second export of the same build. Every step that builds has a time limit. The workflow still has no registry access. - Why one export: in the first runs of this pull request, the slim build stalled on all four legs. Each build command had two exports, a tree and an archive. The slim command, with every stage from the cache, wrote its archive and then made no progress on the tree for 50 minutes. A test now holds the rule of one export and a time limit for each build command. - The smoke matrix runs on the slim tree. The daemon is in a layer that both variants share, so one run covers both. - `server/src/__tests__/runner-image-identity.test.ts`: the test that holds the rules for the Runner stages now finds a stage wherever BuildKit starts one. It fails for `FROM x as name`, a flag on `FROM`, a lowercase or indented `COPY`, an `ADD` or `ONBUILD`, a `COPY` with another flag, and a `COPY` that continues on the next line. Nine samples show each case. - Two comments are corrected. The comment on `PAPERCLIP_RUNNER_INPUTS_DIGEST` in `build-provider-pack.mjs` said that no check reads the field. The `Dockerfile` comment on the publication argument named a workflow "that builds the image for publication". - `doc/runner-image-contract.md` (159 lines): the two variants, the section "Layers", how the checks get their tree, and the new checks. `doc/DOCKER.md` names the slim target. Script sizes: `runner-image-layout.mjs` 969 lines, `runner-image-oci.mjs` 585, `runner-image-identity.mjs` 166, `create-provider-pack-archive.mjs` 114. ## Verification Image identity at this commit: `a544fc9dde780963419ede0f34252f3a57de43a760d41d3eb8fcd4f577504f6a`. Linux (the pinned `node:24.22.3-bookworm` base with GNU tar 1.34 and zstd, the worktree mounted read-only): - `runner-image-identity.test.ts` (41), `runner-image-layout.test.ts` (100), `runner-image-oci.test.ts` (50), `runner-image-workflow.test.ts` (8), `provider-pack-archive.test.ts` (47, none skipped), `retained-provider-pack-source.test.ts` (19), `remote-provider-pack-verification.test.ts`, `docker-image-layer-reuse.test.ts`, `docker-build-stamp.test.ts`: 293 tests pass in 9 files. - The same files pass on macOS. The tests of `check-layers` failed on Linux in the first runs of this pull request: their fixtures named the archive members `./index.json`, which bsdtar finds and GNU tar does not. The fixtures now name the members as buildx does. `linux/arm64`, on a local Docker host, with the new export path: - Each variant builds as an OCI archive only: the full target in 156 seconds without a cache, the slim target in 5 seconds from the cache. - `unpack` writes the full image (10,519 entries that are not directories) in 4 seconds, and the slim image in 4 seconds. - `check <tree> --variant <variant> --expect-inputs-digest <digest> --expect-source-revision <sha> --expect-architecture arm64` passes on each unpacked tree. `runner-image.json` is the same file in both. - `check-layers --slim <slim> --full <full>` passes. - The smoke matrix passes on all five base images, from the unpacked slim tree. | Layer | Digest | Bytes | In | | --- | --- | --- | --- | | 1 | `sha256:3f21b15b3a99…70ac1c` | 379,609,589 | slim and full | | 2 | `sha256:d5567acc977f…b4f12a` | 43,906,956 | slim and full | | 3 | `sha256:6a340088ca2c…6ba5b5` | 410,938,259 | full | | 4 | `sha256:789e97f5db88…3eab44` | 15,656,249 | full | - The slim image is 423,516,545 bytes in two layers. The full image is 850,111,053 bytes in four. Stability, with three builds that share no cache for the build step: | Build | Source revision | Runner build output | Layer 1 | Layer 2 | Layer 3 | Layer 4 | | --- | --- | --- | --- | --- | --- | --- | | A | `aaaa…` | this commit: 6 files of 1 MiB or more under `dist` | `3f21b15b…` | `d5567acc…` | `6a340088…` | `789e97f5…` | | B | `bbbb…` | this commit | `3f21b15b…` | `a2f8a4fe…` | `6a340088…` | `4e6c492d…` | | D | `dddd…` | one new source file that adds two large files under `dist` | `3f21b15b…` | `33b0bdaf…` | `6a340088…` | `0ea322bf…` | - Layers 1 and 3 are byte-identical in all three: 790,547,848 of 850,111,053 bytes, 93%. Build D has two more large files under `dist`, eight in place of six, and another image identity. Its dependency files keep the archive numbers 0 to 20. With the earlier numbering, those numbers moved by two, and layer 3 was a new blob with the same contents. - Layer 1 has the same digest as before this round. Layer 3 has a new digest one time, because the archives have new numbers. Other checks: - `tests/runner-e2e` workflow tests: 39 pass. `node --test .github/scripts/tests/*.test.mjs`: 799 pass. `node --test` on the image contract scripts: 33 pass. `check-docker-deps-stage.mjs`, `check-node-version-policy.mjs` and `docker buildx build --check` report nothing. GitHub, at this head: - The `Runner image` workflow passes on both architectures with the new export path: `Build and check (amd64)` in 14m41s and `Build and check (arm64)` in 8m41s. Each leg builds both variants, checks both layouts and the layer rule, and runs the smoke matrix. The first runs of this pull request stalled in the slim export before any check. Never run: - `native-session-executor.test.ts`. It does not load on this host, with or without this change, because `@paperclipai/plugin-sdk` is not built here. It imports the reader of the retained pack and no archive number. - The full test suite and the full typecheck. CI must supply them. ## Risks - The two changes to `create-provider-pack-archive.mjs` change the application image's transport archives: about half of them lose nine bytes, and all of them get new numbers one time. An archive's content does not change. A reader pairs file N of a receipt with archive N of the same build. - The large dependency files in the pack, and in their transport archives, now have the modification time 0 (1970). Before, they had the build time. Nothing in this repository reads those times. - `unpack` is a reader of tar and gzip that this repository owns. It reads the layers that BuildKit, GNU tar and bsdtar write (tested with each), and it fails for a header it does not know. - The check on the built images, not the recipe, is what proves the layer rule; it runs in each leg of the workflow. - `check` now needs `--variant`. A caller that does not pass it fails with a clear message. No caller exists outside this repository yet. > 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 - Claude Fable 5.2 (`claude-fable-5-2`), Anthropic, used through Claude Code with tool use (shell, file edits, local Docker builds). ## 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 Fable 5.2 <noreply@anthropic.com>
331 lines
17 KiB
TypeScript
331 lines
17 KiB
TypeScript
import { chmodSync, mkdirSync, mkdtempSync, readFileSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
|
|
import { tmpdir } from "node:os";
|
|
import path from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
import { afterEach, describe, expect, it } from "vitest";
|
|
import { computeBuildInputsDigest } from "../../../packages/paperclip-runner/scripts/build-inputs.mjs";
|
|
import {
|
|
RUNNER_IMAGE_CONTEXT_INPUTS,
|
|
RUNNER_IMAGE_INPUTS_SCHEMA,
|
|
computeContextTreeDigest,
|
|
computeRunnerImageIdentity,
|
|
} from "../../../scripts/runner-image-identity.mjs";
|
|
|
|
/**
|
|
* The Runner image is named after its identity. An identity that misses an
|
|
* input gives a changed Runner the name of an older one.
|
|
*/
|
|
|
|
const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..", "..", "..");
|
|
const dockerfile = readFileSync(path.join(repoRoot, "Dockerfile"), "utf8");
|
|
|
|
const roots: string[] = [];
|
|
afterEach(() => {
|
|
for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true });
|
|
});
|
|
|
|
function write(root: string, relative: string, content: string) {
|
|
const file = path.join(root, ...relative.split("/"));
|
|
mkdirSync(path.dirname(file), { recursive: true });
|
|
writeFileSync(file, content);
|
|
return file;
|
|
}
|
|
|
|
/** A repository with the real Dockerfile and one small file for every kind of input. */
|
|
function repository(): string {
|
|
const root = mkdtempSync(path.join(tmpdir(), "runner-image-identity-"));
|
|
roots.push(root);
|
|
write(root, "Dockerfile", dockerfile);
|
|
write(root, ".dockerignore", "node_modules\n");
|
|
for (const input of RUNNER_IMAGE_CONTEXT_INPUTS) {
|
|
if (!input.startsWith("packages/paperclip-") && input !== "patches") write(root, input, `input ${input}\n`);
|
|
}
|
|
write(root, "packages/paperclip-runner/package.json", "{}\n");
|
|
write(root, "packages/paperclip-runner/src/index.ts", "export const runner = 1;\n");
|
|
write(root, "packages/paperclip-runner/runner/crates/runner-core/src/lib.rs", "pub fn runner() {}\n");
|
|
write(root, "packages/paperclip-eval-kernel/src/index.ts", "export const kernel = 1;\n");
|
|
write(root, "patches/acpx@0.22.0.patch", "patch\n");
|
|
write(root, "ui/src/App.tsx", "export const app = 1;\n");
|
|
write(root, "server/src/index.ts", "export const server = 1;\n");
|
|
write(root, "packages/shared/src/index.ts", "export const shared = 1;\n");
|
|
return root;
|
|
}
|
|
|
|
const digestOf = (root: string) => computeRunnerImageIdentity(root).digest as string;
|
|
|
|
describe("Runner image identity", () => {
|
|
it("reports the tag, the context tree, the Dockerfile and the Runner build inputs", () => {
|
|
const identity = computeRunnerImageIdentity(repository());
|
|
expect(identity.schema).toBe(RUNNER_IMAGE_INPUTS_SCHEMA);
|
|
expect(identity.digest).toMatch(/^[0-9a-f]{64}$/);
|
|
expect(identity.tag).toBe(`inputs-${identity.digest}`);
|
|
expect(identity.image).toBe("ghcr.io/paperclipai/paperclip-runner");
|
|
expect(identity.platforms).toEqual(["linux/amd64", "linux/arm64"]);
|
|
expect(identity.context.inputs).toEqual(RUNNER_IMAGE_CONTEXT_INPUTS);
|
|
expect(identity.context.digest).toMatch(/^[0-9a-f]{64}$/);
|
|
expect(identity.dockerfile.sha256).toMatch(/^[0-9a-f]{64}$/);
|
|
expect(identity.buildInputs.schema).toBe("paperclip.runner-build-inputs.v2");
|
|
});
|
|
|
|
it("is the same for the same tree", () => {
|
|
const root = repository();
|
|
expect(digestOf(root)).toBe(digestOf(root));
|
|
expect(digestOf(repository())).toBe(digestOf(root));
|
|
});
|
|
|
|
it.each([
|
|
["a Runner TypeScript source file", "packages/paperclip-runner/src/index.ts"],
|
|
["a Runner Rust source file", "packages/paperclip-runner/runner/crates/runner-core/src/lib.rs"],
|
|
["the eval kernel the Runner builds first", "packages/paperclip-eval-kernel/src/index.ts"],
|
|
["a dependency patch", "patches/acpx@0.22.0.patch"],
|
|
["the lockfile", "pnpm-lock.yaml"],
|
|
["the root manifest", "package.json"],
|
|
["the pnpm configuration", ".npmrc"],
|
|
["the pack archive script", "scripts/create-provider-pack-archive.mjs"],
|
|
["the layout script", "scripts/runner-image-layout.mjs"],
|
|
["the contract copy that build:typescript checks", "packages/shared/src/provider-failure.generated.ts"],
|
|
["the Dockerfile, in a stage the Runner is not built from", "Dockerfile"],
|
|
])("changes when %s changes", (_label, file) => {
|
|
const root = repository();
|
|
const before = digestOf(root);
|
|
write(root, file, file === "Dockerfile" ? dockerfile.replace("FROM base AS production\n", "FROM base AS production\nRUN true\n") : "changed\n");
|
|
expect(digestOf(root)).not.toBe(before);
|
|
});
|
|
|
|
it.each([
|
|
["a UI source file", "ui/src/App.tsx"],
|
|
["a server source file", "server/src/index.ts"],
|
|
["another package's source file", "packages/shared/src/index.ts"],
|
|
["a new file outside the Runner's inputs", "server/src/services/new-service.ts"],
|
|
])("does not change when %s changes", (_label, file) => {
|
|
const root = repository();
|
|
const before = digestOf(root);
|
|
write(root, file, "changed\n");
|
|
expect(digestOf(root)).toBe(before);
|
|
});
|
|
|
|
it("refuses an input that is missing from the build context", () => {
|
|
const root = repository();
|
|
rmSync(path.join(root, "scripts/create-provider-pack-archive.mjs"));
|
|
expect(() => computeRunnerImageIdentity(root)).toThrow(/scripts\/create-provider-pack-archive\.mjs, which is missing/);
|
|
});
|
|
|
|
describe("context tree", () => {
|
|
// The Runner's build-input digest skips dot-directories and any directory
|
|
// named dist, and it does not read file modes. The image build copies
|
|
// those paths and modes all the same, so the identity must see them.
|
|
it.each([
|
|
["a file in a dot-directory", "packages/paperclip-runner/runner/.cargo/config.toml"],
|
|
["a file in a directory named dist", "packages/paperclip-runner/src/vendor/dist/bundle.js"],
|
|
])("changes for %s, which the Runner build-input digest does not read", (_label, file) => {
|
|
const root = repository();
|
|
const before = computeRunnerImageIdentity(root);
|
|
write(root, file, "[build]\n");
|
|
const after = computeRunnerImageIdentity(root);
|
|
expect(after.buildInputs.digest).toBe(before.buildInputs.digest);
|
|
expect(after.digest).not.toBe(before.digest);
|
|
});
|
|
|
|
it("changes when only the executable bit of a file changes", () => {
|
|
const root = repository();
|
|
const file = write(root, "packages/paperclip-runner/scripts/provision.sh", "#!/bin/sh\n");
|
|
chmodSync(file, 0o644);
|
|
const before = computeRunnerImageIdentity(root);
|
|
chmodSync(file, 0o755);
|
|
expect(computeBuildInputsDigest(root).digest).toBe(before.buildInputs.digest);
|
|
expect(digestOf(root)).not.toBe(before.digest);
|
|
});
|
|
|
|
it("records a symbolic link by its target and does not follow it", () => {
|
|
const root = repository();
|
|
const link = path.join(root, "packages/paperclip-runner/current");
|
|
symlinkSync("src", link);
|
|
const before = digestOf(root);
|
|
rmSync(link);
|
|
symlinkSync("../../server/src", link);
|
|
const after = digestOf(root);
|
|
expect(after).not.toBe(before);
|
|
write(root, "server/src/index.ts", "changed\n");
|
|
expect(digestOf(root)).toBe(after);
|
|
});
|
|
|
|
it("changes when a file is added or removed, and not with the order of a directory listing", () => {
|
|
const root = repository();
|
|
const before = digestOf(root);
|
|
write(root, "packages/paperclip-eval-kernel/src/extra.ts", "export {};\n");
|
|
expect(digestOf(root)).not.toBe(before);
|
|
rmSync(path.join(root, "packages/paperclip-eval-kernel/src/extra.ts"));
|
|
expect(digestOf(root)).toBe(before);
|
|
const other = repository();
|
|
for (const name of ["b.ts", "Z.ts", "a.ts", "_c.ts"]) write(root, `packages/paperclip-runner/src/${name}`, "x\n");
|
|
for (const name of ["_c.ts", "a.ts", "Z.ts", "b.ts"]) write(other, `packages/paperclip-runner/src/${name}`, "x\n");
|
|
expect(computeContextTreeDigest(root)).toEqual(computeContextTreeDigest(other));
|
|
});
|
|
});
|
|
|
|
describe(".dockerignore", () => {
|
|
// Its rules decide which files the build receives, and nothing else. The
|
|
// identity is computed inside the build, from the files it received.
|
|
it("does not change the identity when its text changes and the received files do not", () => {
|
|
const root = repository();
|
|
const before = digestOf(root);
|
|
write(root, ".dockerignore", "node_modules\nui/dist\nserver/tmp\n");
|
|
expect(digestOf(root)).toBe(before);
|
|
});
|
|
|
|
it("changes the identity when a rule removes a file from the Runner's context", () => {
|
|
const root = repository();
|
|
write(root, "packages/paperclip-runner/styles.css", "body {}\n");
|
|
const before = digestOf(root);
|
|
// What a new `packages/paperclip-runner/styles.css` rule does to the build context.
|
|
rmSync(path.join(root, "packages/paperclip-runner/styles.css"));
|
|
expect(digestOf(root)).not.toBe(before);
|
|
});
|
|
});
|
|
});
|
|
|
|
/**
|
|
* The identity hashes the whole Dockerfile and the context inputs. That is
|
|
* the whole of what the build reads only while these three rules hold.
|
|
*/
|
|
describe("Runner image stages", () => {
|
|
// BuildKit accepts an instruction in any case and with any indentation, and
|
|
// FROM with flags. A stage starts wherever BuildKit starts one; the first
|
|
// rule below then allows only the spelling the other rules can read.
|
|
const stagesOf = (text: string) => [...text.matchAll(/^[ \t]*from[ \t].*$/gim)].map((match, index, all) => ({
|
|
from: /^FROM (\S+)(?: AS \S+)?$/.exec(match[0])?.[1] ?? "",
|
|
// The name BuildKit gives the stage, however the line is spelt.
|
|
name: (/\sas\s+(\S+)\s*$/i.exec(match[0])?.[1] ?? "").toLowerCase(),
|
|
lines: text.slice(match.index, all[index + 1]?.index ?? text.length).split("\n").filter((line) => !/^\s*#/.test(line)),
|
|
}));
|
|
type Stage = ReturnType<typeof stagesOf>[number];
|
|
/** The lines of a stage that read something and are not in the one spelling these rules read. */
|
|
const misspelt = (stage: Stage) => stage.lines.filter((line, index) => (index === 0
|
|
// No lowercase keyword, no indentation, no flag on FROM.
|
|
? !/^FROM \S+ AS runner-artifacts[a-z-]*$/.test(line)
|
|
// No ADD or ONBUILD, no other flag, and no source on a continuation line.
|
|
: /^\s*(?:from|copy|add|onbuild)(?:\s|$)/i.test(line)
|
|
&& !/^COPY (?:--link )?(?:--from=runner-artifacts[a-z-]* )?[^-\s]\S*(?: [^\s\\]+)+$/.test(line)));
|
|
const stages = stagesOf(dockerfile);
|
|
const names = stages.map((stage) => stage.name);
|
|
const runner = stages.filter((stage) => stage.name.startsWith("runner-artifacts"));
|
|
const copies = (stage: Stage) => stage.lines.filter((line) => /^COPY\s/.test(line));
|
|
|
|
it("spell every FROM and COPY the one way these rules read, and use no ADD or ONBUILD", () => {
|
|
expect(runner.length).toBeGreaterThan(3);
|
|
for (const stage of runner) expect(misspelt(stage), stage.name).toEqual([]);
|
|
});
|
|
|
|
it.each([
|
|
["a lowercase stage keyword", "FROM scratch as runner-artifacts-extra\nCOPY secrets /\n"],
|
|
["a lowercase from", "from scratch AS runner-artifacts-extra\n"],
|
|
["a flag on FROM", "FROM --platform=linux/amd64 node:24 AS runner-artifacts-extra\n"],
|
|
["a lowercase copy", "FROM scratch AS runner-artifacts-extra\ncopy secrets /\n"],
|
|
["an indented COPY", "FROM scratch AS runner-artifacts-extra\n COPY secrets /\n"],
|
|
["an ADD in any case", "FROM scratch AS runner-artifacts-extra\n add secrets /\n"],
|
|
["a COPY continued on the next line", "FROM scratch AS runner-artifacts-extra\nCOPY package.json \\\n secrets /\n"],
|
|
["a COPY with another flag", "FROM scratch AS runner-artifacts-extra\nCOPY --parents secrets /\n"],
|
|
["an ONBUILD trigger", "FROM scratch AS runner-artifacts-extra\nONBUILD COPY secrets /\n"],
|
|
])("would catch %s in a stage BuildKit reads as one of the Runner's", (_label, text) => {
|
|
const added = stagesOf(text).filter((stage) => stage.name.startsWith("runner-artifacts"));
|
|
expect(added).toHaveLength(1);
|
|
expect(misspelt(added[0])).not.toEqual([]);
|
|
});
|
|
|
|
it("start only from each other, from scratch, or from an image pinned by digest", () => {
|
|
for (const stage of runner) {
|
|
if (stage.from === "scratch" || names.includes(stage.from)) {
|
|
expect(stage.from === "scratch" || stage.from.startsWith("runner-artifacts"), stage.name).toBe(true);
|
|
} else {
|
|
expect(stage.from, stage.name).toMatch(/@sha256:[0-9a-f]{64}$/);
|
|
}
|
|
for (const line of copies(stage)) {
|
|
const source = /--from=(\S+)/.exec(line)?.[1];
|
|
if (source) expect(source, line).toMatch(/^runner-artifacts/);
|
|
}
|
|
}
|
|
});
|
|
|
|
it("read the build context only through runner-artifacts-context", () => {
|
|
for (const stage of runner) {
|
|
const text = stage.lines.join("\n");
|
|
expect(text, stage.name).not.toMatch(/^ADD\s/m);
|
|
expect(text, stage.name).not.toMatch(/--mount=/);
|
|
expect(text, stage.name).not.toMatch(/<</);
|
|
if (stage.name === "runner-artifacts-context") continue;
|
|
const fromContext = copies(stage).filter((line) => !line.includes("--from="));
|
|
// The identity stage copies the Dockerfile itself, whose text the identity covers.
|
|
expect(fromContext, stage.name).toEqual(stage.name === "runner-artifacts-identity-compute" ? ["COPY Dockerfile ./"] : []);
|
|
}
|
|
});
|
|
|
|
it("copy into runner-artifacts-context exactly the paths the identity hashes", () => {
|
|
const context = runner.find((stage) => stage.name === "runner-artifacts-context")!;
|
|
const sources = copies(context).flatMap((line) => line.split(/\s+/).slice(1, -1));
|
|
expect(sources.filter((source) => source.startsWith("--"))).toEqual([]);
|
|
expect([...sources].sort()).toEqual([...RUNNER_IMAGE_CONTEXT_INPUTS].sort());
|
|
});
|
|
|
|
it("end in two file trees, the full one being the slim one plus the transport layers", () => {
|
|
// Each COPY --link is one layer that does not depend on the layers under
|
|
// it. The slim variant is the first two; the full variant starts from it,
|
|
// so it has the same two layers and then the transport archives.
|
|
const slim = runner.find((stage) => stage.name === "runner-artifacts-slim")!;
|
|
const full = runner.find((stage) => stage.name === "runner-artifacts")!;
|
|
const instructions = (stage: typeof slim) => stage.lines.filter((line) => /^[A-Z]+\s/.test(line)).slice(1);
|
|
expect(slim.from).toBe("scratch");
|
|
expect(instructions(slim)).toEqual([
|
|
"COPY --link --from=runner-artifacts-build /out-large/ /",
|
|
"COPY --link --from=runner-artifacts-build /out/ /",
|
|
]);
|
|
expect(full.from).toBe("runner-artifacts-slim");
|
|
expect(instructions(full)).toEqual([
|
|
"COPY --link --from=runner-artifacts-build /out-archives/ /",
|
|
"COPY --link --from=runner-artifacts-build /out-transport/ /",
|
|
]);
|
|
// `docker build .` builds the last stage.
|
|
expect(names.at(-1)).toBe("cloud");
|
|
for (const stage of stages) {
|
|
if (!stage.name.startsWith("runner-artifacts")) expect(stage.lines.join("\n"), stage.name).not.toMatch(/runner-artifacts/);
|
|
}
|
|
});
|
|
|
|
it("name the published image in the pack only for a publication build, which is off by default", () => {
|
|
const build = runner.find((stage) => stage.name === "runner-artifacts-build")!.lines.join("\n");
|
|
expect(build).toContain('ARG PAPERCLIP_RUNNER_IMAGE_PUBLICATION=""\n');
|
|
expect(build).toContain('publication="${PAPERCLIP_RUNNER_IMAGE_PUBLICATION}";');
|
|
// The pack builder reads the variable from the environment. It is set in
|
|
// one place, inside the branch that tests for a publication build.
|
|
expect(build.match(/PAPERCLIP_RUNNER_INPUTS_DIGEST=/g)).toHaveLength(1);
|
|
expect(build).toMatch(/if \[ -n "\$publication" \]; then \\\n\s+PAPERCLIP_RUNNER_INPUTS_DIGEST="\$\(node -p /);
|
|
expect(build).not.toMatch(/^ENV .*PAPERCLIP_RUNNER_INPUTS_DIGEST/m);
|
|
});
|
|
|
|
it("run every output step of the Runner's own build, and know every other step", () => {
|
|
// The image build runs the steps of the Runner's `build` script that write
|
|
// output, between the two that record the build stamp. This list is the
|
|
// whole script: a new step fails here until someone decides whether the
|
|
// image build must run it too.
|
|
const manifest = JSON.parse(readFileSync(path.join(repoRoot, "packages/paperclip-runner/package.json"), "utf8"));
|
|
expect((manifest.scripts.build as string).split(" && ")).toEqual([
|
|
"node scripts/record-build-inputs.mjs --begin",
|
|
"pnpm run check:protocol-manifest",
|
|
"pnpm run build:typescript",
|
|
"pnpm run check:capability-contract",
|
|
"pnpm run check:capability-inventory",
|
|
"pnpm run check:protocol-coverage",
|
|
"node scripts/generate-semantic-contracts.mjs --check",
|
|
"node scripts/check-runner-workflow-traceability.mjs",
|
|
"pnpm run build:binary",
|
|
"node scripts/generate-replay-goldens.mjs --check",
|
|
"node scripts/generate-semantic-action-catalog.mjs --check",
|
|
"node scripts/record-build-inputs.mjs --finish",
|
|
]);
|
|
const build = runner.find((stage) => stage.name === "runner-artifacts-build")!.lines.join("\n");
|
|
for (const step of ["node scripts/record-build-inputs.mjs --begin;", "pnpm run build:typescript;", "pnpm run build:binary;",
|
|
"node scripts/record-build-inputs.mjs --finish;", "pnpm install --frozen-lockfile --filter '@paperclipai/paperclip-runner...'"]) {
|
|
expect(build).toContain(step);
|
|
}
|
|
});
|
|
});
|