Files
PaperClipAI/server/src/__tests__/runner-image-publish.test.ts
T
Nicky LeachandClaude Fable 5.2 25e9a9c2e9 feat(ci): publish both variants of the Runner image from master (#21230)
## 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
> - Parts 2 and 3 of this stack build the Runner alone as an image with
two variants, and check it on pull requests. Nothing publishes it
> - Downstream sandbox and VM images can copy the Runner only from a
registry, and they must be able to verify where it came from
> - A published image must be exactly the bytes that were checked, and
its name must never point at something else later
> - This pull request publishes both variants from `master` as
`ghcr.io/paperclipai/paperclip-runner`, one time for each distinct set
of inputs, and attests them
> - Nothing consumes the image in this pull request. The application
image build does not change
> - The benefit is one verified, attested Runner that later changes can
copy by digest

## Linked Issues or Issue Description

Refs #21204 (part 2 of this stack) and #21096 (the merge-queue image
build). No issue exists for this change.

**Problem or motivation**

The Runner image exists only inside a pull-request check. A consumer
cannot pull it. There is no rule for which commit publishes an image, no
protection for its tags, and no way to verify a copied Runner.

**Proposed solution**

Publish from a `master` push in this repository, and from nothing else.
Build only when no image has the digest of the commit's inputs. Let the
build legs hold no write access. Push the bytes that the legs checked by
digest, attest each multi-platform index, and write the tags last. Write
the full tag after the slim tag, so that the full tag means "both
exist".

**Alternatives considered**

- Publish from `docker.yml`. That workflow holds one concurrency slot
for each ref, and the Runner image needs its own signer identity, so
that its attestation cannot stand in for the application image's.
- Resolve the lockfile in the workflow before a `master` build, as
`docker.yml` does. Then the published Runner depends on the state of the
npm registry at run time, and the digest cannot be computed again from a
commit.
- Hand each variant to the publish job as its own OCI archive. The two
archives hold the two shared layers two times: 1.27 GB for each
architecture in place of 0.85 GB.
- Write the full tag first. Then a consumer can see a full image whose
slim image does not exist yet.

**Roadmap alignment**

This is build infrastructure. It adds no product feature and does not
overlap an item in `ROADMAP.md`.

This is part 4 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
4. #21230: publish both variants from `master` (this pull request)

## What Changed

- `.github/workflows/runner-image.yml`: adds the trigger `push` to
`master` and the job `publish`.
- `identity` checks the committed lockfile against the Runner's
manifests, computes the digest in the build context, and asks the
registry whether these inputs are published.
- `build` is the job of part 3. It also records what it built, on every
event. On a canonical `master` push it uploads that. It cannot write to
the registry on any event. The only token in the job can read packages,
and only the lookup step receives it.
- `publish` runs only for a `master` push in this repository. It builds
nothing and runs no dependency code.
- Lockfile rule. A `master` push builds from the committed
`pnpm-lock.yaml` and resolves nothing, so the digest is a function of
the commit. When the committed lockfile is behind the manifests that the
Runner build reads, the run writes a notice, publishes nothing, and
stays green. The commit that merges the lockfile refresh publishes the
image. Pull requests and manual runs resolve the lockfile first, always
build, and never publish.
- Hand-off. Each leg joins its two OCI archives into one OCI layout that
names the images `slim` and `full`. The layout stores the two shared
layers one time. `record` then reads the layout back, each image by its
name, and checks every blob against its digest. The leg's record binds
the layout to both image digests, the architecture, the inputs digest,
and the commit. Those digests are the digests of the archives that part
3 unpacks and checks, so the tree that is checked and the image that is
pushed are the same bytes. The publish job accepts only records for its
own inputs and commit, and only a layout that still has the recorded
bytes.
- `record` runs on pull requests too. Only the upload is for a `master`
push. A pull request therefore runs the hand-off on real build output.
- Publication order. The publish job pushes the four images by digest
(`skopeo copy --preserve-digests`). It makes a candidate index for the
slim variant, then one for the full variant that names the slim index in
the annotation `io.paperclip.runner.slim-digest`. It verifies that each
index names exactly the recorded images, attests both, and then writes
`inputs-<digest>-slim` and, last, `inputs-<digest>`.
- The full tag is the completion marker. Every "is it published" lookup
reads the full tag. `publication-state` reports one of:

  | State | Registry | What a run does |
  | --- | --- | --- |
  | `absent` | Neither tag | Builds and publishes |
| `incomplete` | The slim tag alone | Builds and publishes. It replaces
the slim tag with its own image, then writes the full tag |
| `published` | The full tag, and the slim tag at the digest that the
full index names | Nothing |
| broken | The full tag, and a slim tag that is missing or points at
another image | Fails, with the steps for an operator |
| `unknown` | No clear answer | Builds. The publish job fails before it
writes a final tag |

- Recovery rule. A run that stops between the two tags leaves the state
`incomplete`. It does not block those inputs. The next run never keeps
the earlier slim image: it writes its own slim tag immediately before
its full tag, verifies both against its own index digests, and then
reads the pair back as every later run will see it. The broken state
cannot be repaired by a run, because no run moves a full tag. An
operator must delete the two package versions and re-run the workflow
run of a `master` push with those inputs.
- Pairing rule for consumers, in the contract: resolve the full tag
first, take the slim digest from the annotation of the full index, and
pull the slim image by that digest.
- `verify-index` accepts `linux/arm64` with the variant `v8`, which some
BuildKit versions record, and no other variant.
- `scripts/runner-image-publish.mjs` (533 lines): the commands
`lock-state`, `record`, `verify-legs`, `publication-state` and
`verify-index`. It reads archives with the reader of
`scripts/runner-image-oci.mjs` (607 lines here), which can now read one
named image of a layout. `lock-state` refuses a checkout, because pnpm
can rewrite the lockfile of the tree that it checks. The workflow gives
it an export that no build reads.
- The workflow test pins the build arguments of the image build. Build
arguments are not part of the image identity.
- `doc/runner-image-contract.md` (197 lines): the section "Publication":
the lockfile rule, the order, the pairing rule, the recovery rule, the
create-only tags, the internal `candidate-` tags, the command that
verifies provenance, and the step to make the package public.
`doc/DOCKER.md` says where the image is published.

## Verification

Tests, and the build jobs of the workflow on GitHub. Nothing in this
pull request ran against a registry, and no publish job has run.

- Linux (the pinned `node:24.22.3-bookworm` base with GNU tar 1.34 and
zstd): `runner-image-publish.test.ts` (75),
`runner-image-workflow.test.ts` (12), the three other Runner image test
files, the provider pack archive tests and the existing Dockerfile
tests: 372 tests pass in 10 files. The same files pass on macOS.
- The tests of the leg record 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, and the publish script uses the one reader of
part 3.
- Leg records: both variants are accepted for this run's inputs and
commit; other inputs, another commit, a changed layout, a record without
a variant, an archive that lacks a blob, a full image that does not
start with the slim image's layers, and a build that is not a
publication build are refused.
- Publication state: all five states, and the half-finished publication
end to end. A slim tag alone lets the next run publish. A full image
that is left with another run's slim tag is reported as broken.
- Index: exactly the recorded images of one variant; the full index must
name its slim index, and the slim index must name none; `v8` is accepted
on arm64 only.
- Registry lookups: a missing tag only when the registry says so; every
refused, failed, or unreachable request is `unknown`.
- Workflow: job permissions; the publish job and the upload are behind
the canonical `master` push; the build steps receive no credential; the
full tag is written last, after a strict lookup and both attestations;
one export and a time limit for each build command; the two build
arguments; actions pinned by commit; no expression inside a shell
script.
- `record` on the two OCI archives of a real `linux/arm64` build from
part 3: the joined layout is 850,149,376 bytes, and both images read
back by name with every blob under its digest. The two archives together
are 1,273,646,592 bytes. The image digests in the record are the digests
that `unpack` reports for the trees that were checked.
- `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. `docker buildx build --check` reports
nothing. The image identity is the same as in part 3
(`a544fc9dde780963419ede0f34252f3a57de43a760d41d3eb8fcd4f577504f6a`),
because this pull request changes no input of the image.

GitHub, at this head:

- The `Runner image` workflow passes with these changes: `Build and
check (amd64)` in 18m30s and `Build and check (arm64)` in 7m48s. Each
leg builds both variants, checks them, and writes the leg record.
`Publish and attest` is skipped, as it must be on a pull request.

Never run:

- The publish job. It runs only for a `master` push.
- The publish path: the registry lookups with a workflow token, `skopeo
copy` from `oci-archive:<layout>:<name>` by digest, `docker buildx
imagetools create` with index annotations, and the tag writes.
- The attestations, and the `gh attestation verify` command in the
document.
- `actionlint`. It is not installed on the local host.
- The full test suite and the full typecheck. CI must supply them.

## Risks

- **Manual step.** GitHub creates the `paperclip-runner` package private
on the first publication. A maintainer must make it public one time.
Until then, consumers cannot pull the image without credentials.
- The first `master` runs of the workflow are the first test of the
publish path. A failure before the tags publishes nothing. A failure
between the two tags leaves the `incomplete` state, and the next run
with those inputs repairs it.
- A slim tag can move while its publication is incomplete. A consumer
must resolve the full tag first and take the slim digest from the
annotation of the full index. It must never resolve
`inputs-<digest>-slim` by tag on its own. The contract says so.
- `candidate-` tags are written before the attestations and can name an
index that is not attested. They are internal.
- The broken state needs an operator, and it fails every `master` run
with those inputs until then. It can arise only when someone changes or
deletes a tag outside the workflow.
- **Known residual.** Two `master` runs that both start before a new
digest is published can each build the image. Each build leg and the
publish job look again before they work, and the publish job runs one at
a time for each digest. The cost is runner time, not correctness.
- A commit whose lockfile is behind the Runner's manifests has no
published Runner image until the lockfile refresh merges. A lockfile
that the refresh cannot fix leaves every `master` run green with only a
notice, for as long as it lasts.
- The tags are create-only because the workflow never moves a tag of a
complete publication. The registry does not enforce that. Consumers must
pin the digest and verify the attestation.
- `payload.runnerSourceRevision` in a pack from this image is the commit
that published the image. Code that reads it as the application's commit
must change before it consumes the image.

> 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>
2026-11-12 20:27:36 -08:00

549 lines
35 KiB
TypeScript

import { spawnSync } from "node:child_process";
import { createHash } from "node:crypto";
import { appendFileSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import path from "node:path";
import { afterAll, describe, expect, it } from "vitest";
import {
SLIM_DIGEST_ANNOTATION,
createLegRecord,
parsePublishArguments,
readRunnerImageManifest,
runPublishCommand,
runnerImageTags,
runnerLockState,
runnerPublicationState,
verifyLegRecords,
verifyPublishedIndex,
} from "../../../scripts/runner-image-publish.mjs";
import { readOciArchive } from "../../../scripts/runner-image-oci.mjs";
/**
* The checks the Runner image workflow runs around publication. The image has
* two variants under two tags, and consumers verify their attestations. So
* the bytes published must be the ones the build legs checked, a tag must
* never be written on a guess, and a publication that stopped half way must
* neither block its inputs nor leave a full image paired with another run's
* slim image.
*/
const INPUTS = "1".repeat(64);
const REVISION = "a".repeat(40);
const TAGS = runnerImageTags(INPUTS);
const sha256 = (bytes: Buffer | string) => `sha256:${createHash("sha256").update(bytes).digest("hex")}`;
const scratch = mkdtempSync(path.join(tmpdir(), "runner-image-publish-"));
afterAll(() => rmSync(scratch, { recursive: true, force: true }));
let sequence = 0;
/** What `docker buildx build --output type=oci --metadata-file` writes for one image with these layers. */
function ociArchive(directory: string, name: string, layers: string[]) {
const layout = path.join(directory, `${name}-layout`);
mkdirSync(path.join(layout, "blobs", "sha256"), { recursive: true });
const blob = (bytes: string) => {
writeFileSync(path.join(layout, "blobs", "sha256", sha256(bytes).slice(7)), bytes);
return { digest: sha256(bytes), size: bytes.length };
};
const manifest = blob(JSON.stringify({
schemaVersion: 2, config: blob(`config of ${name}`),
layers: layers.map((layer) => ({ mediaType: "application/vnd.oci.image.layer.v1.tar+gzip", ...blob(layer) })),
}));
writeFileSync(path.join(layout, "index.json"), JSON.stringify({
schemaVersion: 2, manifests: [{ mediaType: "application/vnd.oci.image.manifest.v1+json", ...manifest }],
}));
writeFileSync(path.join(layout, "oci-layout"), '{"imageLayoutVersion":"1.0.0"}');
const archive = path.join(directory, `${name}.oci.tar`);
// The members buildx writes. `tar -C layout .` would name them ./index.json and ./blobs/...
expect(spawnSync("tar", ["-cf", archive, "-C", layout, "blobs", "index.json", "oci-layout"]).status).toBe(0);
writeFileSync(path.join(directory, `${name}.metadata.json`), JSON.stringify({ "containerimage.digest": manifest.digest }));
return manifest.digest;
}
/** What one build leg uploads: one layout with both variants, and the record of what it verified. */
async function leg(directory: string, architecture: string, options: {
inputsDigest?: string; sourceRevision?: string; publication?: boolean; slim?: string[]; full?: string[];
} = {}) {
const { inputsDigest = INPUTS, sourceRevision = REVISION, publication = true } = options;
const slim = options.slim ?? [`large ${architecture}`, `rest ${architecture}`];
const full = options.full ?? [...slim, `archives ${architecture}`, `overlay ${architecture}`];
const root = path.join(directory, architecture);
mkdirSync(path.join(root, "tree"), { recursive: true });
const images = { slim: ociArchive(root, "slim", slim), full: ociArchive(root, "full", full) };
writeFileSync(path.join(root, "tree", "runner-image.json"), JSON.stringify({
inputsDigest: { digest: inputsDigest }, sourceRevision, publication, platform: { os: "linux", architecture },
}));
const record = await createLegRecord({
architecture,
archives: { slim: path.join(root, "slim.oci.tar"), full: path.join(root, "full.oci.tar") },
metadataFiles: { slim: path.join(root, "slim.metadata.json"), full: path.join(root, "full.metadata.json") },
tree: path.join(root, "tree"),
layout: path.join(root, "images.oci.tar"),
});
writeFileSync(path.join(root, "record.json"), JSON.stringify(record));
return { record, images };
}
async function legs(options: Record<string, object> = {}) {
const directory = path.join(scratch, String(sequence += 1));
const images: Record<string, { slim: string; full: string }> = {};
for (const architecture of ["amd64", "arm64"]) ({ images: images[architecture] } = await leg(directory, architecture, options[architecture]));
return { directory, images };
}
describe("verified build legs", () => {
it("accepts both variants of each architecture for this run's inputs and commit", async () => {
const { directory, images } = await legs();
await expect(verifyLegRecords({ directory, inputsDigest: INPUTS, sourceRevision: REVISION })).resolves.toEqual({
slim_amd64: images.amd64.slim, full_amd64: images.amd64.full, slim_arm64: images.arm64.slim, full_arm64: images.arm64.full,
});
});
it("hands over one layout that names both variants and stores their shared layers once", async () => {
const { directory, images } = await legs();
const layout = path.join(directory, "amd64", "images.oci.tar");
const read = (member: string) => spawnSync("tar", ["-xOf", layout, member], { encoding: "utf8" }).stdout;
expect(JSON.parse(read("index.json")).manifests.map((entry: { digest: string; annotations: Record<string, string> }) =>
[entry.annotations["org.opencontainers.image.ref.name"], entry.digest])).toEqual([["slim", images.amd64.slim], ["full", images.amd64.full]]);
const blobs = spawnSync("tar", ["-tf", layout], { encoding: "utf8" }).stdout.split("\n").filter((name) => /^blobs\/sha256\/[0-9a-f]{64}$/.test(name));
// Two manifests, two configs, and four layers: the two shared ones are not stored twice.
expect(blobs).toHaveLength(8);
expect(new Set(blobs).size).toBe(8);
expect(JSON.parse(read(`blobs/sha256/${images.amd64.full.slice(7)}`)).layers.slice(0, 2))
.toEqual(JSON.parse(read(`blobs/sha256/${images.amd64.slim.slice(7)}`)).layers);
// Each image can be read alone by its name, with every blob under its digest: what a push does.
for (const variant of ["slim", "full"] as const) {
const image = readOciArchive(layout, { ref: variant });
expect(image.descriptor.digest).toBe(images.amd64[variant]);
await expect(image.verifyBlobs()).resolves.toBeUndefined();
}
expect(() => readOciArchive(layout)).toThrow(/must hold exactly one image manifest/);
expect(() => readOciArchive(layout, { ref: "candidate" })).toThrow(/must hold exactly one image named candidate/);
});
it("does not record an archive that lacks a blob of its image, or whose members have other names", async () => {
const directory = path.join(scratch, String(sequence += 1));
await leg(directory, "amd64");
const root = path.join(directory, "amd64");
const input = {
architecture: "amd64",
archives: { slim: path.join(root, "slim.oci.tar"), full: path.join(root, "full.oci.tar") },
metadataFiles: { slim: path.join(root, "slim.metadata.json"), full: path.join(root, "full.metadata.json") },
tree: path.join(root, "tree"),
layout: path.join(root, "again.oci.tar"),
};
// The full archive without one of its layers.
const layer = readOciArchive(input.archives.full).layers[3].digest;
rmSync(path.join(root, "full-layout", "blobs", "sha256", layer.slice(7)));
expect(spawnSync("tar", ["-cf", input.archives.full, "-C", path.join(root, "full-layout"), "blobs", "index.json", "oci-layout"]).status).toBe(0);
await expect(createLegRecord(input)).rejects.toThrow(/has no member blobs\/sha256\//);
// An archive of `.`: the names a registry tool does not find.
expect(spawnSync("tar", ["-cf", input.archives.slim, "-C", path.join(root, "slim-layout"), "."]).status).toBe(0);
await expect(createLegRecord(input)).rejects.toThrow(/has no member index\.json/);
});
it("refuses a leg that built other inputs or another commit", async () => {
const otherInputs = await legs({ arm64: { inputsDigest: "2".repeat(64) } });
await expect(verifyLegRecords({ directory: otherInputs.directory, inputsDigest: INPUTS, sourceRevision: REVISION }))
.rejects.toThrow(/arm64: the image was built from other inputs/);
const otherCommit = await legs({ amd64: { sourceRevision: "b".repeat(40) } });
await expect(verifyLegRecords({ directory: otherCommit.directory, inputsDigest: INPUTS, sourceRevision: REVISION }))
.rejects.toThrow(/amd64: the image was built at another commit/);
});
it("refuses a layout that changed after its leg recorded it, a record without a variant, and a missing leg", async () => {
const { directory } = await legs();
appendFileSync(path.join(directory, "amd64", "images.oci.tar"), "tampered");
await expect(verifyLegRecords({ directory, inputsDigest: INPUTS, sourceRevision: REVISION }))
.rejects.toThrow(/amd64: the image layout does not match its record/);
const oneVariant = await legs();
const recordPath = path.join(oneVariant.directory, "arm64", "record.json");
const record = JSON.parse(readFileSync(recordPath, "utf8"));
writeFileSync(recordPath, JSON.stringify({ ...record, images: { full: record.images.full } }));
await expect(verifyLegRecords({ directory: oneVariant.directory, inputsDigest: INPUTS, sourceRevision: REVISION }))
.rejects.toThrow(/arm64: the record has no slim image digest/);
const missing = await legs();
rmSync(path.join(missing.directory, "arm64"), { recursive: true });
await expect(verifyLegRecords({ directory: missing.directory, inputsDigest: INPUTS, sourceRevision: REVISION })).rejects.toThrow();
});
it.each<[string, object, RegExp]>([
["a full image that does not start with the slim image's layers", { full: ["another large", "rest amd64", "archives", "overlay"] }, /does not start with the layers of the slim image/],
["a full image with the slim layers in another order", { full: ["rest amd64", "large amd64", "archives", "overlay"] }, /does not start with the layers of the slim image/],
["a full image that is the slim image", { full: ["large amd64", "rest amd64"] }, /has no layer of its own/],
["a build that is not a publication build", { publication: false }, /Only a publication build can be recorded/],
])("does not record %s", async (_label, options, expected) => {
await expect(leg(path.join(scratch, String(sequence += 1)), "amd64", options)).rejects.toThrow(expected);
});
it("does not record a tree that is for another architecture, or an archive that is not the image the build reported", async () => {
const directory = path.join(scratch, String(sequence += 1));
await leg(directory, "amd64");
const root = path.join(directory, "amd64");
const input = {
architecture: "amd64",
archives: { slim: path.join(root, "slim.oci.tar"), full: path.join(root, "full.oci.tar") },
metadataFiles: { slim: path.join(root, "slim.metadata.json"), full: path.join(root, "full.metadata.json") },
tree: path.join(root, "tree"),
layout: path.join(root, "again.oci.tar"),
};
await expect(createLegRecord({ ...input, architecture: "arm64" })).rejects.toThrow(/another architecture/);
await expect(createLegRecord({ ...input, archives: { slim: input.archives.full, full: input.archives.full } }))
.rejects.toThrow(/The slim archive is not the image its build reported/);
});
});
describe("registry lookups", () => {
const index = Buffer.from(JSON.stringify({ schemaVersion: 2, mediaType: "application/vnd.oci.image.index.v1+json", manifests: [] }));
const respond = (status: number, body: Buffer | string = "", headers: Record<string, string> = {}) =>
new Response(typeof body === "string" ? body : new Uint8Array(body), { status, headers });
function registry(manifest: () => Response, token: () => Response = () => respond(200, JSON.stringify({ token: "t" }))) {
const requests: { url: string; headers: Record<string, string> }[] = [];
const fetchImpl = async (url: string, init: { headers: Record<string, string> }) => {
requests.push({ url, headers: init.headers });
return url.includes("/token?") ? token() : manifest();
};
return { requests, options: { fetchImpl, sleep: async () => {} } };
}
it("reports a tag that exists, with the digest of the registry bytes", async () => {
const { requests, options } = registry(() => respond(200, index, { "docker-content-digest": sha256(index) }));
const result = await readRunnerImageManifest(`inputs-${INPUTS}`, options);
expect(result).toMatchObject({ state: "present", digest: sha256(index) });
expect(requests.map((request) => request.url)).toEqual([
"https://ghcr.io/token?service=ghcr.io&scope=repository:paperclipai/paperclip-runner:pull",
`https://ghcr.io/v2/paperclipai/paperclip-runner/manifests/inputs-${INPUTS}`,
]);
expect(requests[0].headers.authorization).toBeUndefined();
expect(requests[1].headers.authorization).toBe("Bearer t");
});
it("reports a missing tag only when the registry says it is missing", async () => {
await expect(readRunnerImageManifest("inputs-x", registry(() => respond(404)).options)).resolves.toEqual({ state: "absent" });
});
it.each<[string, () => Response, (() => Response) | undefined]>([
["the token request is refused", () => respond(200, index), () => respond(403)],
["the token response has no token", () => respond(200, index), () => respond(200, "{}")],
["the manifest request is unauthorized", () => respond(401), undefined],
["the manifest request is denied", () => respond(403), undefined],
["the registry keeps failing", () => respond(503), undefined],
])("does not guess when %s", async (_label, manifest, token) => {
const result = await readRunnerImageManifest("inputs-x", registry(manifest, token).options);
expect(result.state).toBe("unknown");
});
it("retries a transient registry error", async () => {
let calls = 0;
const { options } = registry(() => ((calls += 1) < 3 ? respond(502) : respond(404)));
await expect(readRunnerImageManifest("inputs-x", options)).resolves.toEqual({ state: "absent" });
expect(calls).toBe(3);
});
it.each<[string, () => Promise<Response>]>([
["the registry cannot be reached", async () => { throw new TypeError("fetch failed"); }],
["the token response is not JSON", async () => respond(200, "<html>gateway</html>")],
])("does not guess, and does not crash, when %s", async (_label, token) => {
let calls = 0;
const fetchImpl = async () => { calls += 1; return token(); };
const result = await readRunnerImageManifest("inputs-x", { fetchImpl, sleep: async () => {} });
expect(result.state).toBe("unknown");
expect(calls).toBeGreaterThanOrEqual(1);
});
it("does not guess when the manifest request cannot be completed", async () => {
let manifests = 0;
const fetchImpl = async (url: string) => {
if (url.includes("/token?")) return respond(200, JSON.stringify({ token: "t" }));
manifests += 1;
throw new TypeError("fetch failed");
};
await expect(readRunnerImageManifest("inputs-x", { fetchImpl, sleep: async () => {} })).resolves.toMatchObject({ state: "unknown" });
expect(manifests).toBe(5);
});
it("authenticates with the credentials it is given and rejects bytes that do not match their digest", async () => {
const { requests, options } = registry(() => respond(200, index, { "docker-content-digest": sha256("other") }));
await expect(readRunnerImageManifest("inputs-x", { ...options, credentials: { username: "u", password: "p" } }))
.rejects.toThrow(/does not match the manifest bytes/);
expect(requests[0].headers.authorization).toBe(`Basic ${Buffer.from("u:p").toString("base64")}`);
});
});
describe("published index", () => {
const legDigests = { amd64: sha256("manifest amd64"), arm64: sha256("manifest arm64") };
const entry = (architecture: string, digest: string) => ({
mediaType: "application/vnd.oci.image.manifest.v1+json", digest, size: 500, platform: { os: "linux", architecture },
});
const bytes = (manifests: object[]) => Buffer.from(JSON.stringify({
schemaVersion: 2, mediaType: "application/vnd.oci.image.index.v1+json", manifests,
}));
it("accepts an index of exactly the two verified images and returns its digest", () => {
const index = bytes([entry("amd64", legDigests.amd64), entry("arm64", legDigests.arm64)]);
expect(verifyPublishedIndex(index, legDigests)).toBe(sha256(index));
});
it("requires the full index, and no other, to name its slim index", () => {
const slimDigest = sha256("slim index");
const manifests = [entry("amd64", legDigests.amd64), entry("arm64", legDigests.arm64)];
const full = Buffer.from(JSON.stringify({
schemaVersion: 2, mediaType: "application/vnd.oci.image.index.v1+json", manifests, annotations: { [SLIM_DIGEST_ANNOTATION]: slimDigest },
}));
expect(verifyPublishedIndex(full, legDigests, { slimDigest })).toBe(sha256(full));
expect(() => verifyPublishedIndex(full, legDigests, { slimDigest: sha256("an earlier run's slim index") }))
.toThrow(/The full index does not name the slim index published with it/);
expect(() => verifyPublishedIndex(bytes(manifests), legDigests, { slimDigest })).toThrow(/does not name the slim index/);
expect(() => verifyPublishedIndex(full, legDigests)).toThrow(/Only the full index may name a slim index/);
});
const both = [entry("amd64", legDigests.amd64), entry("arm64", legDigests.arm64)];
const extra = (platform: object | undefined) => ({
mediaType: "application/vnd.oci.image.manifest.v1+json", digest: sha256("extra"), size: 500, ...(platform ? { platform } : {}),
});
it.each<[string, object[]]>([
["an image no leg verified", [entry("amd64", legDigests.amd64), entry("arm64", sha256("unverified"))]],
["the images under the wrong architectures", [entry("amd64", legDigests.arm64), entry("arm64", legDigests.amd64)]],
["one architecture only", [entry("amd64", legDigests.amd64)]],
["one architecture twice", [entry("amd64", legDigests.amd64), entry("amd64", legDigests.amd64), entry("arm64", legDigests.arm64)]],
["an attestation manifest beside the images", [...both, extra({ os: "unknown", architecture: "unknown" })]],
["an entry without a platform", [...both, extra(undefined)]],
["an image for another operating system", [...both, extra({ os: "windows", architecture: "amd64" })]],
["an image for another architecture", [...both, extra({ os: "linux", architecture: "riscv64" })]],
["a variant on the amd64 image", [{ ...entry("amd64", legDigests.amd64), platform: { os: "linux", architecture: "amd64", variant: "v8" } },
entry("arm64", legDigests.arm64)]],
["another arm64 variant", [entry("amd64", legDigests.amd64),
{ ...entry("arm64", legDigests.arm64), platform: { os: "linux", architecture: "arm64", variant: "v7" } }]],
])("refuses an index with %s", (_label, manifests) => {
expect(() => verifyPublishedIndex(bytes(manifests), legDigests)).toThrow();
});
it("accepts linux/arm64 with the variant v8, which some BuildKit versions record", () => {
const index = bytes([entry("amd64", legDigests.amd64),
{ ...entry("arm64", legDigests.arm64), platform: { os: "linux", architecture: "arm64", variant: "v8" } }]);
expect(verifyPublishedIndex(index, legDigests)).toBe(sha256(index));
});
it("needs a recorded image for each architecture, and no other", () => {
expect(() => verifyPublishedIndex(bytes(both), { amd64: legDigests.amd64 })).toThrow(/one recorded image for each architecture/);
expect(() => verifyPublishedIndex(bytes(both), { ...legDigests, riscv64: sha256("extra") })).toThrow(/one recorded image for each architecture/);
});
});
describe("lockfile state", () => {
function context() {
const directory = path.join(scratch, String(sequence += 1));
mkdirSync(path.join(directory, "packages", "paperclip-runner"), { recursive: true });
writeFileSync(path.join(directory, "pnpm-lock.yaml"), "lockfileVersion: '9.0'\n");
writeFileSync(path.join(directory, "packages", "paperclip-runner", "package.json"), "{}\n");
return directory;
}
const answer = (status: number | null, stderr = "", extra: object = {}) => {
const calls: { command: string; args: string[]; options: { cwd?: string } }[] = [];
const run = (command: string, args: string[], options: object) => {
calls.push({ command, args, options });
return { status, stdout: "", stderr, ...extra };
};
return { calls, run };
};
it("asks pnpm the image build's own question, offline and without installing", () => {
const directory = context();
const { calls, run } = answer(0);
expect(runnerLockState(directory, { run }).state).toBe("current");
expect(calls).toHaveLength(1);
expect(calls[0].command).toBe("pnpm");
expect(calls[0].options.cwd).toBe(directory);
expect(calls[0].args).toEqual(["install", "--frozen-lockfile", "--lockfile-only", "--offline", "--ignore-scripts",
"--filter", "@paperclipai/paperclip-runner..."]);
});
it.each([
[" ERR_PNPM_OUTDATED_LOCKFILE Cannot install with \"frozen-lockfile\" because pnpm-lock.yaml is not up to date with <ROOT>/packages/paperclip-runner/package.json"],
[" ERR_PNPM_LOCKFILE_CONFIG_MISMATCH Cannot proceed with the frozen installation. The current \"patchedDependencies\" configuration doesn't match the value found in the lockfile"],
])("reports a lockfile the manifests have moved past as behind: %s", (stderr) => {
const result = runnerLockState(context(), { run: answer(1, stderr).run });
expect(result.state).toBe("behind");
expect(result.detail).toContain("ERR_PNPM_");
});
it.each<[string, number | null, string, object]>([
["a broken lockfile", 1, " ERR_PNPM_BROKEN_LOCKFILE The lockfile is broken", {}],
["a failure pnpm does not name", 1, "Killed", {}],
["a pnpm that was killed", null, "", {}],
["a pnpm that cannot be started", null, "", { error: new Error("spawn pnpm ENOENT") }],
])("fails for %s instead of calling it behind", (_label, status, stderr, extra) => {
expect(() => runnerLockState(context(), { run: answer(status, stderr, extra).run })).toThrow();
});
it("fails for a directory that is not a Runner build context", () => {
const directory = context();
rmSync(path.join(directory, "pnpm-lock.yaml"));
expect(() => runnerLockState(directory, { run: answer(0).run })).toThrow(/no pnpm-lock\.yaml/);
});
it("refuses a checkout, whose lockfile pnpm could rewrite, and does not start pnpm there", () => {
const directory = context();
mkdirSync(path.join(directory, ".git"));
const { calls, run } = answer(0);
expect(() => runnerLockState(directory, { run })).toThrow(/is a checkout: pnpm can rewrite its pnpm-lock\.yaml/);
expect(calls).toEqual([]);
});
});
describe("publication state", () => {
type Manifest = Awaited<ReturnType<typeof readRunnerImageManifest>>;
const index = (label: string, slimDigest?: string) => {
const bytes = Buffer.from(JSON.stringify({ schemaVersion: 2, label, ...(slimDigest ? { annotations: { [SLIM_DIGEST_ANNOTATION]: slimDigest } } : {}) }));
return { state: "present" as const, bytes, digest: sha256(bytes) };
};
const registry = (tags: Record<string, Manifest>) => async (reference: string): Promise<Manifest> => tags[reference] ?? { state: "absent" };
const slim = index("slim of this run");
const full = index("full of this run", slim.digest);
const unknown = { state: "unknown" as const, detail: "manifest request: HTTP 503" };
it("is published only when the full tag exists and names the image the slim tag points at", async () => {
await expect(runnerPublicationState(INPUTS, registry({ [TAGS.slim]: slim, [TAGS.full]: full })))
.resolves.toMatchObject({ state: "published", slim: slim.digest, full: full.digest });
await expect(runnerPublicationState(INPUTS, registry({}))).resolves.toMatchObject({ state: "absent" });
});
it("treats a slim tag without the full tag as a publication that did not finish", async () => {
// A run stopped between the two tags. The inputs are not published, so
// the next run builds, and its publish job replaces the slim tag.
const orphan = index("slim of the run that stopped");
await expect(runnerPublicationState(INPUTS, registry({ [TAGS.slim]: orphan })))
.resolves.toMatchObject({ state: "incomplete", slim: orphan.digest });
await expect(runPublishCommand(["publication-state", "--inputs-digest", INPUTS, "--strict"], { env: {}, read: registry({ [TAGS.slim]: orphan }), log: () => {} }))
.resolves.toEqual({ state: "incomplete", exists: false, publish: true });
});
it.each<[string, Record<string, Manifest>]>([
["the slim tag is missing", { [TAGS.full]: full }],
["the slim tag points at another run's image", { [TAGS.full]: full, [TAGS.slim]: index("slim of another run") }],
["the full index names no slim image", { [TAGS.full]: index("full without the annotation"), [TAGS.slim]: slim }],
["the full tag is not an index", { [TAGS.full]: { state: "present", bytes: Buffer.from("not JSON"), digest: sha256("not JSON") }, [TAGS.slim]: slim }],
])("is broken, and fails every run, when the full tag exists but %s", async (_label, tags) => {
await expect(runnerPublicationState(INPUTS, registry(tags))).resolves.toMatchObject({ state: "broken" });
// Also without --strict: no run can repair it, and the message says what an operator must do.
await expect(runPublishCommand(["publication-state", "--inputs-digest", INPUTS], { env: {}, read: registry(tags), log: () => {} }))
.rejects.toThrow(/No run can repair this.*Delete the package versions tagged inputs-1{64} and inputs-1{64}-slim, then re-run/);
});
it.each<[string, Record<string, Manifest>]>([
["the full tag", { [TAGS.full]: unknown }],
["the slim tag, with the full tag absent", { [TAGS.slim]: unknown }],
["the slim tag, with the full tag present", { [TAGS.full]: full, [TAGS.slim]: unknown }],
])("does not guess when the registry gives no clear answer for %s", async (_label, tags) => {
await expect(runnerPublicationState(INPUTS, registry(tags))).resolves.toMatchObject({ state: "unknown" });
});
it("lets a run whose own slim tag is already written finish, and never pairs a full image with another run's slim", async () => {
// What the publish job does, against a registry a stopped run left behind.
const tags: Record<string, Manifest> = { [TAGS.slim]: index("slim of the run that stopped") };
const read = registry(tags);
const gate = await runPublishCommand(["publication-state", "--inputs-digest", INPUTS, "--strict"], { env: {}, read, log: () => {} });
expect(gate.publish).toBe(true);
// It writes its own slim tag over the orphan, then the full tag that names that slim.
tags[TAGS.slim] = slim;
tags[TAGS.full] = full;
await expect(runPublishCommand(["publication-state", "--inputs-digest", INPUTS, "--strict"], { env: {}, read, log: () => {} }))
.resolves.toEqual({ state: "published", exists: true, publish: false });
// Had it kept the orphan, the pair would not hold.
tags[TAGS.slim] = index("slim of the run that stopped");
await expect(runPublishCommand(["publication-state", "--inputs-digest", INPUTS, "--strict"], { env: {}, read, log: () => {} })).rejects.toThrow(/No run can repair this/);
});
});
describe("command line", () => {
const unknown = async () => ({ state: "unknown" as const, detail: "token request: HTTP 403" });
const absent = async () => ({ state: "absent" as const });
const quiet = { env: {}, log: () => {} };
const state = ["publication-state", "--inputs-digest", INPUTS];
it("builds on an inconclusive lookup and fails on one when it is strict", async () => {
const lines: string[] = [];
await expect(runPublishCommand(state, { env: {}, read: unknown, log: (line) => lines.push(line) }))
.resolves.toEqual({ state: "unknown", exists: false, publish: false });
expect(lines.join("\n")).toMatch(/::notice title=Runner image lookup inconclusive::/);
await expect(runPublishCommand([...state, "--strict"], { ...quiet, read: unknown }))
.rejects.toThrow(/Could not determine whether .*paperclip-runner:inputs-1{64} is published \(inputs-1{64}: token request: HTTP 403\)/);
await expect(runPublishCommand([...state, "--strict"], { ...quiet, read: absent }))
.resolves.toEqual({ state: "absent", exists: false, publish: true });
});
it("stays inconclusive, not failed, when the registry cannot be reached", async () => {
const offline = (reference: string) => readRunnerImageManifest(reference, {
fetchImpl: async () => { throw new TypeError("fetch failed"); }, sleep: async () => {},
});
await expect(runPublishCommand(state, { ...quiet, read: offline })).resolves.toEqual({ state: "unknown", exists: false, publish: false });
await expect(runPublishCommand([...state, "--strict"], { ...quiet, read: offline })).rejects.toThrow(/Could not determine/);
});
it("passes the registry credentials from the environment, and none when one is missing", async () => {
const seen: unknown[] = [];
const read = async (_reference: string, options?: { credentials?: unknown }) => { seen.push(options?.credentials); return { state: "absent" as const }; };
await runPublishCommand(state, { env: { RUNNER_IMAGE_REGISTRY_USERNAME: "u", RUNNER_IMAGE_REGISTRY_PASSWORD: "p" }, read, log: () => {} });
await runPublishCommand(state, { env: { RUNNER_IMAGE_REGISTRY_USERNAME: "u" }, read, log: () => {} });
expect(new Set(seen.map((credentials) => JSON.stringify(credentials)))).toEqual(new Set(['{"username":"u","password":"p"}', "null"]));
});
it("verifies the index of each variant against the recorded legs and against the expected digest", async () => {
const { directory, images } = await legs();
const indexOf = (variant: "slim" | "full", annotations?: Record<string, string>) => Buffer.from(JSON.stringify({
schemaVersion: 2, mediaType: "application/vnd.oci.image.index.v1+json",
manifests: ["amd64", "arm64"].map((architecture) => ({
mediaType: "application/vnd.oci.image.manifest.v1+json", digest: images[architecture][variant], size: 500,
platform: { os: "linux", architecture },
})),
...(annotations ? { annotations } : {}),
}));
const slim = indexOf("slim");
const full = indexOf("full", { [SLIM_DIGEST_ANNOTATION]: sha256(slim) });
const serve = (bytes: Buffer) => async () => ({ state: "present" as const, bytes, digest: sha256(bytes) });
const verify = (...args: string[]) => ["verify-index", ...args, "--dir", directory];
await expect(runPublishCommand(verify("candidate-x-slim", "--variant", "slim"), { ...quiet, read: serve(slim) })).resolves.toEqual({ digest: sha256(slim) });
await expect(runPublishCommand(verify("candidate-x", "--variant", "full", "--slim-digest", sha256(slim), "--expect", sha256(full)), { ...quiet, read: serve(full) }))
.resolves.toEqual({ digest: sha256(full) });
// The slim index is not the full one, and the other way round.
await expect(runPublishCommand(verify("inputs-x", "--variant", "full", "--slim-digest", sha256(slim)), { ...quiet, read: serve(slim) })).rejects.toThrow();
await expect(runPublishCommand(verify("inputs-x-slim", "--variant", "slim"), { ...quiet, read: serve(full) })).rejects.toThrow();
await expect(runPublishCommand(verify("inputs-x", "--variant", "full"), { ...quiet, read: serve(full) }))
.rejects.toThrow(/--slim-digest is given for the full index, and only for it/);
await expect(runPublishCommand(verify("inputs-x-slim", "--variant", "slim", "--slim-digest", sha256(slim)), { ...quiet, read: serve(slim) }))
.rejects.toThrow(/--slim-digest is given for the full index, and only for it/);
await expect(runPublishCommand(verify("inputs-x-slim", "--variant", "slim", "--expect", sha256("another index")), { ...quiet, read: serve(slim) }))
.rejects.toThrow(/inputs-x-slim resolves to sha256:[0-9a-f]{64}, expected sha256:[0-9a-f]{64}/);
await expect(runPublishCommand(verify("inputs-x-slim", "--variant", "slim", "--expect", "latest"), { ...quiet, read: serve(slim) }))
.rejects.toThrow(/--expect must be a sha256 digest/);
await expect(runPublishCommand(verify("inputs-x-slim", "--variant", "both"), { ...quiet, read: serve(slim) })).rejects.toThrow(/--variant must be slim or full/);
await expect(runPublishCommand(verify("inputs-x-slim", "--variant", "slim"), { ...quiet, read: absent })).rejects.toThrow(/is absent/);
});
it("reports the lockfile state and fails when the check itself fails", async () => {
await expect(runPublishCommand(["lock-state", "--context", "/context"], { ...quiet, lockState: () => ({ state: "behind", detail: "ERR_PNPM_OUTDATED_LOCKFILE" }) }))
.resolves.toEqual({ state: "behind" });
await expect(runPublishCommand(["lock-state", "--context", "/context"], { ...quiet, lockState: () => { throw new Error("broken"); } }))
.rejects.toThrow("broken");
});
it.each<[string, string[], RegExp]>([
["an empty --expect", ["verify-index", "inputs-x", "--dir", "d", "--variant", "slim", "--expect", ""], /--expect needs a value/],
["an --expect with no value", ["verify-index", "inputs-x", "--dir", "d", "--variant", "slim", "--expect"], /--expect needs a value/],
["an index check without a variant", ["verify-index", "inputs-x", "--dir", "d"], /verify-index needs --variant/],
["a misspelt flag", [...state, "--strikt"], /publication-state does not take --strikt/],
["a flag of another command", [...state, "--expect", "sha256:x"], /publication-state does not take --expect/],
["a repeated flag", ["verify-index", "inputs-x", "--dir", "a", "--dir", "b"], /--dir is given twice/],
["a missing required flag", ["verify-legs", "--dir", "d", "--inputs-digest", INPUTS], /verify-legs needs --source-revision/],
["an empty required value", ["verify-legs", "--dir", "d", "--inputs-digest", "", "--source-revision", REVISION], /--inputs-digest needs a value/],
["a lookup without the inputs digest", ["publication-state", "--strict"], /publication-state needs --inputs-digest/],
["a lookup by tag", ["publication-state", "inputs-x", "--inputs-digest", INPUTS], /takes no positional argument/],
["a missing tag", ["verify-index", "--dir", "d", "--variant", "slim"], /takes exactly one tag or digest/],
["an empty tag", ["verify-index", "", "--dir", "d", "--variant", "slim"], /not a tag or digest/],
["a record of one variant", ["record", "--arch", "amd64", "--full-archive", "f", "--full-metadata", "m", "--tree", "t", "--layout", "l", "--out", "o"], /record needs --slim-archive/],
["an unknown command", ["tag-state", "inputs-x"], /Usage/],
["no command", [], /Usage/],
])("rejects %s", (_label, argv, expected) => {
expect(() => parsePublishArguments(argv[0], argv.slice(1))).toThrow(expected);
});
});