## 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 > - `.github/workflows/runner-image.yml` builds the Runner alone and publishes it from `master` as `ghcr.io/paperclipai/paperclip-runner`, one time for each set of inputs > - The build used no cache, so every run compiled the daemon again. The first publication took 25m46s from push to tag > - The toolchain and the daemon change only with the Rust sources. Most commits that change the Runner's inputs do not touch them > - This pull request lets the build legs read a registry build cache for those stages. Only a new job can write the cache, and only for a `master` push > - A weekly run builds with no cache, so the cache cannot hide a broken build for long > - The benefit is about 3 minutes less on each build leg when the Rust sources did not change, with no change to the published image ## Linked Issues or Issue Description Refs #21230 (publication of the Runner image), #21229 and #21204 (the build this change makes faster). No issue exists for this change. **What existing behavior does this improve?** The Runner image build in `.github/workflows/runner-image.yml`. It builds the same image in less time. **Current behavior** Each build leg starts from an empty builder. It installs the Rust toolchain and compiles the daemon in every run. In the first `master` publication (run 43365670428) the amd64 leg spent 7m22s in the build step. The toolchain and the cargo build took 3m26s of that. **Proposed behavior** The build legs read `ghcr.io/paperclipai/paperclip-runner-buildcache:<arch>` for the stages up to the compiled daemon. A new `cache` job writes that cache after a publication. A scheduled run builds with no cache one time each week. **Reason and benefit** The stages before the last one read the Rust sources, the protocol files and the toolchain pin. In the last 30 days, 438 `master` commits changed an input of the image, and at most 181 of them changed a file those stages read. For the other commits, and for each pull request that does not touch those files, the build step is about 3 minutes shorter on each leg. **Breaking changes** None. The `Dockerfile` and each file in the image identity are unchanged. The published digest for a set of inputs does not change. This is part 4 of 4 of a stack ([stack #21309](https://github.com/paperclipai/paperclip/stack/21309)). Merge the parts in order: 1. #21306: the resolver and the no-rebuild mode 2. #21307: the report-only job in `docker.yml` and the pull-request check of the application image's Runner path 3. #21308: reading a retained provider pack from its Runner image 4. #21310: a registry build cache for the Runner image build (this pull request) This pull request uses no code from parts 1 to 3. It is stacked on them because all four parts edit `doc/runner-image-contract.md`. ## What Changed - The build legs pass `--cache-from type=registry,ref=ghcr.io/paperclipai/paperclip-runner-buildcache:<arch>` to the first build command. They do not log in. Their permissions stay `contents: read` and `packages: read`. - A new `cache` job writes the cache with `--cache-to type=registry,...,mode=max` and `--output type=cacheonly`. It runs only for a `master` push in the canonical repository, for inputs that had no image. Its permissions are `contents: read` and `packages: write`. It has no `id-token` and no `attestations` permission. - The `cache` job needs the `publish` job, and no job needs the `cache` job. A cache write cannot delay or fail a publication. - The `cache` job builds the target `runner-artifacts-runnerd` and no later stage. The last stage takes the commit as a build argument, so no other commit can reuse it. The install and its dependency lifecycle scripts are in that stage, so the job with the write token does not run them. - The `cache` job has one concurrency group for each architecture, a job time limit and a step time limit. - A `schedule` trigger starts a weekly build. It passes `--no-cache` to the first command and reads no cache. It runs all checks and publishes nothing. A fork's schedule runs nothing. - `server/src/__tests__/runner-image-workflow.test.ts` pins the new security properties: which jobs hold `packages: write`, that the build legs have no credential and cannot write the cache, the conditions of the `cache` job, that the `publish` job does not depend on it, and that a scheduled run cannot publish or write the cache. - `doc/runner-image-contract.md` has a "Build cache" section. ## Verification Tests, on macOS (Node 26.12.0) and in a `node:24.22.3-bookworm` container (Linux arm64, Node 24.22.3): - `pnpm exec vitest run src/__tests__/runner-image-workflow.test.ts src/__tests__/runner-image-identity.test.ts` in `server/`: 55 of 55 tests pass on both. - `node --test .github/scripts/tests/*.test.mjs`: 807 of 807 tests pass on both. The container needs `jq`. - `pnpm verify` exits 2 for this change, because a workflow file is a global CI input. Required CI supplies the broad coverage. - Each new assertion fails when the workflow breaks its rule. I checked seven such edits, for example: the `publish` job needs the `cache` job, the `cache` job gets `id-token: write`, a build leg gets `--cache-to`. Cache mechanics, on linux/arm64 with a local registry, BuildKit v0.34.2 (the version the hosted runners use), and an empty builder for each build: | Build of both variants | Time | Result | | --- | --- | --- | | No cache, cache ref does not exist | 260 s | An importer error in the log, exit 0 | | One changed TypeScript file, with the cache | 149 s | Toolchain and cargo steps cached | | Unchanged inputs and a new commit, with the cache | 129 s | Toolchain and cargo steps cached | | Each stage in the cache (a whole-build cache, which this change does not write) | 16 s | Both OCI archives complete | | `--no-cache` on the first command | 272 s | No cached step in the first command. The second command reuses all of it | - The images built from the cache pass `runner-image-layout.mjs check` and `runner-image-oci.mjs check-layers`. The daemon has the same SHA-256 as the daemon of a cold build. - A cache ref that does not exist, a host that refuses the connection, a name that does not resolve, and the real `ghcr.io/paperclipai/paperclip-runner-buildcache:arm64` (which does not exist yet) each give an importer error in the log and a complete build with exit 0. - The `cache` job command on an empty builder with the cache present takes 6 s. It downloads no layer. Not tested before merge: - The `cache` job. It runs only on `master`. - The scheduled run. It starts only from the default branch. - The cache package. It does not exist. I did not test a private package, which answers an anonymous read with an authorization error. I expect the same importer error and a cold build. - A fully cached build on the hosted runners. The local test used the same BuildKit version and a different buildx version (0.37.0, and the runners have 0.38.1). ## Risks - **A second job can write packages.** Before this change, only the `publish` job held `packages: write`, and it runs no build. Now the `cache` job holds it too, and it runs BuildKit on the repository's `Dockerfile`. GitHub cannot limit that token to one package, so it can also write to `ghcr.io/paperclipai/paperclip-runner`. The limits are: the job runs only for a `master` push in the canonical repository, the token goes only to the registry login, the build steps run inside BuildKit without it, the job does not run the install, and the job cannot sign. An image that it wrote has no attestation. - **Part of a published image can come from an earlier run.** The daemon in a published image can come from a step that an earlier `master` run compiled and cached. The attestation names the run that published, not the run that compiled. BuildKit reuses a step only for the same instruction and the same file content, but a party that can write the cache package can replace what a later build reuses. Write access to the cache package must stay as narrow as write access to the image package. - **A cached download is not repeated.** The toolchain installer, the toolchain and the crates are not downloaded again until the inputs of their step change. The weekly cold build finds a download that is gone. It does not compare its output with the cache. - **A registry failure after the cache import fails the leg.** A cache that cannot be read at the start is not a failure. A layer download that fails later is a failure. Run the job again. - **The cache package starts private.** GitHub creates it on the first write. A maintainer must make it public one time. Until then each build runs cold and nothing fails. - **The first cache write waits for an input change.** The merge of this pull request changes no input of the image, so its `master` run builds nothing and writes no cache. - **A cancelled cache job marks its run as cancelled.** When three cache writes for one architecture wait at the same time, GitHub cancels the middle one. The image of that run is published. > 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, through Claude Code. Tool use and shell execution. It wrote the change, ran the local builds and tests, and wrote this description. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
17 KiB
Runner image contract
The Runner image is the Runner built alone, as a plain file tree with no
entrypoint and no operating system. An image build copies paths out of it
instead of compiling the Runner again. Nothing in this repository consumes it
yet. Its name is ghcr.io/paperclipai/paperclip-runner, and one build makes
two variants:
- full, the
runner-artifactstarget of theDockerfile: every path below; - slim, the
runner-artifacts-slimtarget: the same without the two paths marked "full only", the provider pack's transport archives.
Layout
Every path a consumer may copy (RUNNER_IMAGE_PATHS in
scripts/runner-image-layout.mjs). Every file is readable by every user.
| Path | Content |
|---|---|
/runner-image.json |
Identity and description of this image. |
/paperclip-runnerd |
The daemon. A stripped release binary, mode 0755. |
/provider-pack/ |
The remote provider pack, with provider-pack.json. |
/provider-pack.archive.json |
Full only. Receipt of the pack's transport archives. |
/provider-pack.archives/ |
Full only. The transport archives the receipt lists. |
/runner-package/package.json |
The Runner package manifest. |
/runner-package/dist/ |
The Runner's complete build output. dist/bin/paperclip-runnerd is the same file as /paperclip-runnerd. |
/runner-package/node_modules/.cache/paperclip-runner-build-inputs.json |
The build stamp the Runner's build recorded for that output. |
/runner-package/scripts/provision-grok.mjs |
Provisions the native Grok executable. Reads platforms.json by relative path. |
/runner-package/src/providers/grok/platforms.json |
The Grok release pins. |
/runner-package/src/live/runnerd-codex-transport.ts |
The transport source, as a contract file. It defines RUNNER_CLIENT_VERSION. |
/bundles/runnerd-codex-transport.mjs |
The pack's dist/live/runnerd-codex-transport.js as one module. It imports without node_modules. |
/pnpm-lock.yaml |
The lockfile the image was installed from. |
Paths under /runner-package/ keep their place inside
packages/paperclip-runner. runner-image.json describes one platform, and
is the same file in both variants:
inputsDigest(the identity below) andbuildInputsDigest(the digest in the Runner's build stamp);sourceRevision: the commit that built the image, also in the pack aspayload.runnerSourceRevision. An image is published once for its inputs, so later commits with the same inputs keep this revision;publication:trueonly for a build with the build argumentPAPERCLIP_RUNNER_IMAGE_PUBLICATION=1. Only then does the pack carry the identity, aspayload.runnerInputsDigest. Another image build that runs the stage inline builds the same Runner but is not this image;platform(linux,amd64orarm64) andlibc: the glibc the daemon was compiled on (Debian bookworm, the oldest a consumer runs) and the newest glibc version it needs;providerPack: the pack's digest and target;pathsandsha256: the table above, and the digest of every single file;fullImageOnly: the names inpathsthat only the full image has.
Layers
- The pack's dependency files of 1 MiB or more: provider binaries and Node.
- Everything else, with
runner-image.json. - Full only: the transport archive of each file in layer 1.
- Full only: the other transport archives, the overlay archive, the receipt.
The slim image is layers 1 and 2. The full image is those same two layer
blobs, then layers 3 and 4, which hold transport content and nothing else.
Layers 1 and 3 have the same bytes in every build with the same dependency
files, so a new image shares them with the one before it. A large file under
the pack's dist is the Runner's own output: it and its archive are in
layers 2 and 4. An archive is named by its file's place in the receipt, which
lists the dependency files first, so their names do not depend on dist.
Identity
Both variants are named by one digest: the variant is not part of the
identity. The full image has the tag inputs-<digest> and the slim image
inputs-<digest>-slim. scripts/runner-image-identity.mjs computes the
digest from:
- every file under the paths the build copies from the build context
(
RUNNER_IMAGE_CONTEXT_INPUTS, which includepnpm-lock.yaml): path, executable bit and content, and each symbolic link by its target; - the whole
Dockerfile: the recipe, the base image pinned by digest and the toolchain pins. Any edit to it makes a new image; - the two platforms,
linux/amd64andlinux/arm64.
The source revision is not part of it, and neither is the text of
.dockerignore: a rule changes the digest only when it changes which of
those files the build receives. The build computes the digest for itself:
docker buildx build --target runner-artifacts-identity --output type=local,dest=/tmp/identity .
jq -r .digest /tmp/identity/runner-image-identity.json
The install is frozen. It fails when the lockfile is behind the manifests the
build reads (the root one, packages/paperclip-runner and
packages/paperclip-eval-kernel). A contributor cannot edit the lockfile, so
pull-request runs resolve it first. For a local build, run
pnpm install --resolution-only --ignore-scripts --no-frozen-lockfile.
Checks
CI builds each variant as an OCI archive and nothing else.
node scripts/runner-image-oci.mjs unpack <oci archive> --out <tree> writes
the tree of an image from its layers, so the tree CI checks is the bytes of
the image. It refuses a whiteout, an absolute or .. path, a path written
twice and an entry behind a link.
node scripts/runner-image-layout.mjs check <tree> --variant slim|full runs
in the build and again on each unpacked tree. The variant has no default: a
slim tree with the transport paths fails, and so does a full tree without
them. The check fails unless:
- every documented path exists, matches its digest in
runner-image.json, and resolves inside the tree; - the build stamp names the inputs the identity names;
- every artifact in
provider-pack.jsonhas its digest, thedisttree and each provider asset tree match their tree digests, and the pack names this image's revision and platform; - the pack names the inputs digest in a publication build and in no other.
--expect-inputs-digestfails on any other build; - in a full image, every transport archive matches its receipt, and the
archives extract to the pack. The check extracts them as a sandbox does (GNU tar
with the options in
provider-pack-layers.ts) and compares paths, content, link targets and all twelve permission bits, after thea+rXgrant the build makes once the archives are written.
node scripts/runner-image-oci.mjs check-layers --slim <oci archive> --full <oci archive>
checks "Layers" on the two archives, and that no file is in two layers.
The build also runs paperclip-runnerd --build-metadata on every base image
in docker/runner-image/Dockerfile.smoke. Add a base there before a consumer
uses it.
Consuming the image
Read the image only with COPY --from, and pin it by digest:
FROM ghcr.io/paperclipai/paperclip-runner@sha256:<digest> AS runner-artifacts
FROM debian:trixie-slim
COPY --from=runner-artifacts /paperclip-runnerd /usr/local/bin/paperclip-runnerd
A Dockerfile with a runner-artifacts or runner-artifacts-slim stage of
its own, as in this repository, can take a built image or tree in its place
with --build-context <stage>=<image or directory>. The stage is then not
built.
Choosing the Runner for an image build
scripts/runner-image-resolve.mjs decides whether a build of a checkout can
copy the published image. No build uses its answer yet: on a master push,
the runner_image_report job of .github/workflows/docker.yml runs it and
writes the answer to the job summary, and nothing reads that job. The job
runs on the committed files, holds no credential (the Runner package is
public, so the lookup is anonymous) and cannot fail the run. The script has
two commands, so that the one that builds reads no registry credential:
node scripts/runner-image-resolve.mjs inputs --dir "$dir"
node scripts/runner-image-resolve.mjs resolve --dir "$dir"
inputs exports the Runner build context, checks the lockfile and computes
the inputs digest in the build, as the publication workflow does. Run it on
the committed files, before any step that rewrites pnpm-lock.yaml.
resolve looks up inputs-<digest> and reports:
mode:publishedorinline, andreason;imageandslim_image: both variants, pinned by index digest. The slim digest is the one the full index names (see "Pairing");build_contexts: one--build-contextvalue on each line, which replace therunner-artifactsandrunner-artifacts-slimstages;labels:io.paperclip.runner.imageandio.paperclip.runner.inputs-digest, for the image that copies the Runner.
published needs a complete publication whose full index names these inputs
and both platforms. Every other answer is inline, with the last four values
empty: the lockfile is behind the Runner's manifests (lock-behind), the
image does not exist (absent), a publication is incomplete or broken,
the full index does not match (mismatch), or the registry refused, failed
or could not be reached (unknown). An inline build runs the Runner stages
itself and carries neither label. The command does not wait for a publication
and does not verify the attestation of the image.
Publication
.github/workflows/runner-image.yml publishes on a master push in this
repository and nowhere else. Other runs build and check, and publish nothing.
- Lockfile. A
masterpush builds from the committedpnpm-lock.yaml, so the digest is a function of the commit. When that file is behind the Runner's manifests, the run writes a notice, publishes nothing and stays green. The commit that merges the lockfile refresh publishes the image. - Once. The build runs only when
inputs-<digest>does not exist. - Verified bytes. The build legs cannot write to the registry. The
publish job pushes the images they checked by digest and joins each
variant into a multi-platform index under a
candidate-tag. It attests both indexes, and only then writes the twoinputs-tags.candidate-tags are internal: one can name an index that is not attested, and the next run overwrites it. Do not consume them. - Order. It writes
inputs-<digest>-slimfirst andinputs-<digest>last, so the full tag marks a complete publication. The full index names its slim index in the annotationio.paperclip.runner.slim-digest. - Pairing. Resolve the full tag first. Take the slim digest from that
annotation of the full index, and pull the slim image by that digest. Never
resolve
inputs-<digest>-slimby tag on its own: while a publication is incomplete, that tag can name the slim image of another run. - Recovery. A run that stops between the two tags leaves a slim tag
alone. Those inputs are not published. The next run for them builds again,
replaces that tag with its own slim image and then writes the full tag. A
full tag whose slim tag is missing or names another image fails every run
for those inputs. An operator must then delete both package versions and
re-run the workflow run of a
masterpush with those inputs. - Create-only. The workflow never moves a tag of a complete publication,
but the registry does not enforce that. Pin the index digest and verify it:
gh attestation verify oci://ghcr.io/paperclipai/paperclip-runner@sha256:<digest> --repo paperclipai/paperclip --signer-workflow paperclipai/paperclip/.github/workflows/runner-image.yml --source-ref refs/heads/master - First publication. GitHub creates the package private. A maintainer must make it public once.
Build cache
The build legs read a registry build cache,
ghcr.io/paperclipai/paperclip-runner-buildcache:<arch>. It is a package of
its own and holds no image, so the image package holds only attested images.
It covers the stages that do not take the commit: the Rust toolchain and the
compiled daemon. The last stage takes the source revision as a build
argument, so every build runs it: the frozen install, the Runner's build and
the provider pack. Of the two build commands of a leg, only the first reads
the registry cache; the second reads what the first left in the job's builder.
- Effect. BuildKit reuses a cached step only when the step and the content of every file it reads are the same. The cache changes how long a build takes, not what it builds, and the identity does not cover it.
- Writer. Only the
cachejob of the workflow writes it: on amasterpush in this repository, after the publish job, for inputs that had no image. When the image exists, nothing was built, and the run that published it wrote the cache for those inputs. That job cannot sign, builds no further than the daemon and never runs the install. Its token reaches the registry login and no other step. It reads the cache it writes and keeps every step of the cached stages (mode=max). Every other run reads the cache without a credential and cannot write it. - Not on the way to an image. No job waits for the
cachejob. When it fails, the image is published and the next build is slower. - One write at a time. The job holds one concurrency slot for each architecture. GitHub keeps one waiting job in a group and cancels the one that waited before it, so the newest write waits. A write from an older commit that lands last costs time and nothing else.
- No Actions cache. The workflow never uses it: workflows that handle pull requests can write it, and what they write must not reach a published image.
- Trust. The daemon in a published image can come from a cached step
that an earlier
masterrun compiled. Whoever can write the cache package can decide what a later build reuses, so write access to it must stay as narrow as write access to the image package. - Fetches. A cached step does not run again. A download inside one (the toolchain installer, the toolchain, the crates) is not repeated until the inputs of that step change.
- Weekly cold build. A scheduled run builds both variants once a week
with no cache: it passes
--no-cacheand reads no registry cache. Like a manual run, it resolves the lockfile, always builds, runs every check and publishes nothing. A fork's schedule runs nothing. It fails when a download is gone or when a step only works from the cache. - Missing cache. A cache that does not exist or cannot be read is a line in the build log, not a failure. The build then runs every step.
- First write. GitHub creates the cache package private. A maintainer must make it public once. Until then every build runs cold.
Reading the transport archives alone
The server needs the transport form of an older pack when a retained session
names a pack that is not the server's own. A pack with
payload.runnerInputsDigest came out of this image, so the server reads it
from inputs-<digest> (retained-provider-pack-oci.ts). It does not count
layers. It uses the rule of "Layers": the full image is the slim image, then
layers that hold transport content and nothing else.
- The full index must carry the annotations
io.paperclip.runner.inputs-digest, equal to the pack's digest of inputs,org.opencontainers.image.revision, equal to the pack'srunnerSourceRevision, andio.paperclip.runner.slim-digest. - The server reads the slim index by that digest, never by the slim tag, and checks the bytes against the digest.
- For the pack's platform, the layers of the slim image must be the first layers of the full image, as the same blobs in the same order. The layers after them are the transport layers: at least one and at most eight.
- The server downloads only those layers. It refuses any entry in them that is not a transport archive or the receipt, and any file that is in two of them.
- The importer then checks every archive against the pack's manifest, as for any other source.
- The record the server keeps of the source names the digest of inputs and
every transport layer under
runnerImage. ItstransportLayerDigestis then the last of those layers andtransportLayerBytestheir total.
When the image cannot be read (a tag or the slim index does not exist, or the
registry refuses a request), the server reads the application image tagged
sha-<runnerSourceRevision>, as it does for a pack without
runnerInputsDigest. An image that can be read and does not match fails.
Known limits
- A build without
PAPERCLIP_RUNNER_SOURCE_REVISIONorPAPERCLIP_BUILD_COMMIThas no pack. It is for local use. - The build runs the steps of the Runner's
buildscript that write output, not its drift checks; repository CI runs those. - A build that vendors
/runner-package/reusesdistonly when its own Runner build-inputs digest equals the stamp's. The image computes that digest in the.dockerignore-filtered context; a checkout gives another. # syntax=docker/dockerfile:1.20is a tag. A new frontend release under it does not change the identity.- The archive check does not tell a directory with its own archive member from one created only as a file's parent.
- A lockfile that the lockfile refresh cannot bring up to the Runner's
manifests leaves every
masterrun green with only a notice, for as long as it lasts. No image is published in that time, and nothing fails.