From 82274853729f8c5b0f812da08595020f7eb64d66 Mon Sep 17 00:00:00 2001 From: Devin Foley Date: Mon, 12 Oct 2026 11:25:31 -0700 Subject: [PATCH] perf(ci): cache the Runner's release crate artifacts keyed by their inputs (#15932) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - Its trusted PR workflow builds the Runner's Rust binary in five jobs: Typecheck + Release Registry, Build, Canary Dry Run, and the `rust` and `vitest 2/2` lanes of Verify Paperclip Runner. The `Build` job is the slowest check in almost every green run (461s, 489s and 515s in runs 38180574768, 38182717052 and 38183149920 on 2026-10-11). > - Each of those jobs restores master's `release-runner-v2` dependency cache. That cache holds only third-party crates. `rust-cache` removes workspace crates when it saves, and a fresh checkout invalidates cargo's mtime fingerprints for workspace crates in any case. So cargo compiles `paperclip-runner-core` in release mode from scratch on every run. The job logs show one `Compiling` line and 28s to 49s for it. > - 18 of the 239 commits on `master` in the seven days before this change touched the crate's inputs. The other 221 compiled the same sources to the same binary again. > - This pull request adds a second cache that holds only that crate's release artifacts (about 70 MB). The key is made from the compiler, the checkout root and a hash of every input the build reads. The master Rust lane writes it. A pull request whose merge tree has byte-identical inputs restores it and marks the artifacts newer than the checkout, so cargo keeps them. Every other pull request misses and compiles as before. > - The benefit is about 28s to 49s less on `Build`, which is the run's critical path, and the same on Typecheck, Canary Dry Run and two Runner lanes, for about nine of ten pull requests. ## Linked Issues or Issue Description No GitHub issue exists for this. The description below follows the enhancement template. **What existing behavior does this improve?** The time the trusted PR workflow spends to compile the Runner's own crate on every run. **Subsystem affected** CI / build **Current behavior** Every PR job that builds the Runner binary compiles `paperclip-runner-core` in release mode from scratch. The compile takes 28s to 49s on the `runs-on` fleet (one `Compiling paperclip-runner-core` line, then `Finished release profile` in runs 38169436532 to 38183149920 on 2026-10-11). The shared Rust cache cannot help: it holds only third-party crates, and a fresh checkout gives every source a new mtime, which makes cargo discard the crate's fingerprints. **Proposed behavior** The master Rust lane in `release-verify.yml` saves the crate's release artifacts under a key made from the compiler, the checkout root and a hash of every input the release build reads. The PR jobs that build the binary restore that entry read-only. On a hit, a script sets the mtime of every restored artifact to now, so cargo sees every source as older than the build and keeps the artifacts. `pnpm build` then prints `Finished release profile` in under a second for that crate. On a miss, cargo compiles the crate exactly as before. **Reason and benefit** About 28s to 49s off `Build`, the slowest check in almost every green run, and the same off the Typecheck, Canary Dry Run and two Verify Paperclip Runner lanes. The saving applies to every pull request whose merge tree does not change the crate's inputs, which was 221 of 239 `master` commits in the week before this change. **Breaking changes** None. The checks are unchanged. A miss or an eviction costs the compile time it costs today and nothing else. **Additional context** This is a workflow change. `pr.yml` calls `pr-trusted.yml@master`, so this pull request's own run uses the current reader steps, and the writer runs only on a master push. The saving becomes visible on the first PR runs after the merge and after the next master push has written the entry. ## What Changed - `scripts/runner-release-crate-cache.mjs` (new): the one place that defines the cached path list (`CACHED_PATHS`), the hashed inputs (`HASHED_INPUTS`) and the files a restore must contain (`REQUIRED`). The `scope` subcommand prints the platform, the `rustc --version --verbose` release, commit and host, and a hash of the checkout root. The `mark-current` subcommand sets the mtime of every restored artifact to now and prints a `paperclip.runner-release-crate.v1` JSON line with the file count and bytes. If the restored set lacks any of the seven files cargo's freshness check reads or checks for (the fingerprint hash and dep-info of each unit, the lib's rlib and rmeta, the binary under `deps`), it removes the restored files, prints `action: discarded` naming the missing file, and exits 0, so cargo compiles as on a miss. - `.github/workflows/pr-trusted.yml`: after the `release-runner-v2` restore in `typecheck_release_registry`, `build` and `canary_dry_run`, and in the `rust` and `vitest 2/2` lanes of `verify_paperclip_runner` (new matrix key `release_crate: true`), three steps: compute the key scope, `actions/cache/restore` the crate under `runner-release-crate-v1--` with no `restore-keys`, and run `mark-current` when `cache-hit == 'true'`. The `hashFiles` list covers the toolchain pin, the manifests and lockfile, the crate sources, the protocol tree and the three fixture trees the test modules include (two under the Runner package and `tests/runner-e2e/fixtures` at the repository root). - `.github/workflows/release-verify.yml`: in the Rust lane of an own-repository master push, after the debug warm-up: compute the key scope, look the key up (`lookup-only: true`), and on a miss run `cargo clean --release -p paperclip-runner-core` and `pnpm build:binary` (the exact command the readers run, so the entry carries no test-profile variants), then `actions/cache/save` the same path list under the same key. - `scripts/__tests__/runner-release-crate-cache.test.mjs` (new, 10 tests, in `test:release-registry`): the path list and hashed inputs are the expected ones and exist; every `include_str!` and `include_bytes!` in the crate sources resolves under a hashed input, whether its argument is a literal or a `concat!` of literals, `env!("CARGO_MANIFEST_DIR")` and macro metavariables, and any other form fails the test by name (this guard found the `test/fixtures` tree during development and the `tests/runner-e2e/fixtures` tree in review); the parser itself is tested on a synthetic crate for each supported form and six rejected ones; every PR reader restores after the dependency cache and marks before its build command with the script's exact path list and key and no `restore-keys`; the Verify lanes are gated by `release_crate`; the writer runs after the checks with the lookup, the clean and build, and the save under the readers' exact path list and key; the scope changes with the compiler and the checkout root and survives a missing `rustc`; `mark-current` makes every artifact newer than the checkout, leaves a dependency's artifacts alone, discards a restore missing any one of the seven required files (one case each, checking the named reason), and still marks a restore missing any one of the seven files cargo does not need. ## Review fixes Greptile's two findings on the first commit were both valid and are fixed in the second commit: - **Incomplete caches looked usable.** `REQUIRED` checked four files, but cargo's freshness check needs seven. Measured with cargo 1.97.1 on the real crate (restore, touch the artifacts, delete one file, build): without the lib's fingerprint hash, dep-info, rlib or rmeta it recompiles the crate (24s to 27s); without the bin's fingerprint hash, dep-info or the binary under `deps` it relinks (1.5s); without either `.json` fingerprint record, either rustc `.d` file, the uplifted binary or its `.d` it reports the crate fresh in 0.15s and recreates the uplifted binary from `deps`, and the staging step then copies it. `REQUIRED` now lists exactly the seven files, the glob for the binary under `deps` excludes the `.d` beside it, and the test removes each of the seven in turn (discarded, naming it) and each of the other seven in turn (still marked). - **Composed include paths escaped the guard.** The regex matched only `include_str!("literal")` and skipped `include_str!(concat!(...))`, which the crate uses four times (30 of 34 includes were checked). The guard now parses every include argument and resolves the `concat!` forms; one of them, in `provider_events.rs`, reads `tests/runner-e2e/fixtures/native-completion/terminal-tool-carrier.json` at the repository root, outside every hashed input. It is test-only, so today's release binary does not read it, but the key is meant to cover every include whatever its build, so `tests/runner-e2e/fixtures/**` (9 files, 80 KB) joins `HASHED_INPUTS` and the six key embeddings in the two workflows. With the first commit's input list, the new guard fails on exactly that include. - `doc/RELEASE-AUTOMATION-SETUP.md`: a "Runner release crate cache" section under the Runner verification dependency cache, with the key, the writer, the readers and the trust boundary. - `package.json`: the new test file in `test:release-registry`. ## Verification - `node --test scripts/__tests__/runner-release-crate-cache.test.mjs`: 10 tests pass, three consecutive runs. - The second commit, with the real toolchain on the real crate (`rustc 1.97.1`, cold build 2m20s, the crate's artifacts 74 MB): every source touched to a fresh-checkout mtime and the archived artifacts restored, cargo compiles (`Compiling paperclip-runner-core`, 24.8s and 25.1s, the two controls); restored and marked by `mark-current`, `Finished release profile in 0.16s` with no `Compiling`, and the same on a second build. Then, for each of the fourteen files cargo wrote for the two units, one at a time: restored, marked and that file deleted, cargo recompiles without any of the seven `REQUIRED` files and reports fresh without any of the other seven; restored with that file deleted and run through `mark-current`, the seven required ones print `action: discarded` naming the file and the others print `marked`. A restore holding only the seven required files builds fresh in 0.15s, cargo recreates the uplifted binary, and `stage-runner-binary.mjs` copies it into `dist/bin`. - `node --test '.github/scripts/tests/*.test.mjs'`: 378 of 381 pass. The 3 failures are in `docker-canary-promotion.test.mjs` and fail the same way on an unmodified `master` checkout in this sandbox (the stand-in docker writes no `calls.jsonl`). - `node --test scripts/__tests__/release-verify-workflow.test.mjs scripts/__tests__/e2e-shard.test.mjs scripts/__tests__/run-vitest-stable-shard.test.mjs`: 99 of 101 pass; the 2 failures spawn `pnpm`, which is not on this sandbox's PATH, and fail the same way on `master`. - Both workflows parse as YAML. No job has a duplicate step name or step id. - The mechanism, with the real toolchain (`rustc 1.97.1`, the package's pin) on a copy of the crate: a cold `cargo build --release -p paperclip-runner-core --bin paperclip-runnerd` (2m23s here). Then the artifact set was archived with `tar`, removed, every source re-copied so each had a new mtime, and the set restored: - restored, not marked: `Compiling paperclip-runner-core`, 24.6s (the control; this is what CI does today); - restored and marked by `mark-current`: `Finished release profile in 0.15s`, no `Compiling`; a second build is the same; - one Rust source edited after a marked restore: `Compiling`, 24.0s; - one protocol schema edited after a marked restore: `Compiling`, 23.9s (the protocol files enter cargo's dep-info as absolute paths and are hashed into the key); - the rlib removed from a restored set: `mark-current` prints `action: discarded, reason: incomplete: the lib rlib is missing` and removes the rest, and cargo compiles. - Not run here: the workflow steps themselves. `pr.yml` pins `pr-trusted.yml@master`, so this pull request's own `Build` job runs the current reader steps, and the writer runs only on a master push. The first PR runs after the merge and after the next master push show the `Restore the Runner release crate` step's result and the `paperclip.runner-release-crate.v1` line in their `Build` logs. ## Risks - Correctness: a reader can only restore artifacts that master built from inputs byte-identical to its own merge tree, because every input the build reads is in the key, and the key has no prefix match. A pull request cannot write. The trust boundary is unchanged: master writes, a pull request restores master's entry. - An input the build reads that is outside the hashed patterns would let a stale artifact pass. The release dep-info lists the crate sources and the protocol schemas and fixtures; all are hashed. The guard test resolves every `include_str!` and `include_bytes!` in the crate, literal or `concat!`, and fails if one lands outside the hashed inputs or uses a form it cannot resolve, so a new include form cannot slip past it by not matching a regex. - GitHub-hosted runners check out under `/home/runner/work`, the `runs-on` fleets under `/home/runner/_work`. Cargo records the included protocol files as absolute paths, so an entry from one root is useless under the other. The checkout root is in the key, so the other root misses instead of restoring something cargo then discards. - A cache that does not keep mtimes on restore changes nothing: the artifacts are then already newer than the checkout, and `mark-current` sets them to now in any case. - Cost: one new entry of about 70 MB per distinct input set, written only by the master Rust lane on a miss, plus one `cargo clean` and `build:binary` (about 40s to 50s) on that lane when the inputs changed. The readers pay one restore of about 70 MB (two to three seconds at the fleet's observed restore speed) and a sub-second `mark-current`. - An incomplete entry (a writer that saved a partial set) is discarded by `mark-current` and costs one compile, the same as a miss. ## Model Used - Claude (Anthropic), model id `claude-fable-5-1` (Claude Fable 5.1), extended thinking, with tool use (shell, file editing, GitHub CLI, a local Rust toolchain for the experiment) in a Paperclip agent session. ## 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: devinfoley <139239+devinfoley@users.noreply.github.com> Co-authored-by: Claude Fable 5.1 --- .github/workflows/pr-trusted.yml | 153 +++++ .github/workflows/release-verify.yml | 55 ++ doc/RELEASE-AUTOMATION-SETUP.md | 55 +- package.json | 2 +- .../runner-release-crate-cache.test.mjs | 558 ++++++++++++++++++ scripts/runner-release-crate-cache.mjs | 210 +++++++ 6 files changed, 1030 insertions(+), 3 deletions(-) create mode 100644 scripts/__tests__/runner-release-crate-cache.test.mjs create mode 100644 scripts/runner-release-crate-cache.mjs diff --git a/.github/workflows/pr-trusted.yml b/.github/workflows/pr-trusted.yml index 228ac3bd4b7..ec62707c417 100644 --- a/.github/workflows/pr-trusted.yml +++ b/.github/workflows/pr-trusted.yml @@ -475,6 +475,42 @@ jobs: # Restore only. Never let a pull request evict master's entry. save-if: false + # The Runner's own crate is the one thing the entry above leaves out: + # rust-cache drops workspace crates when it saves, and a fresh checkout + # would invalidate cargo's mtime fingerprints for them anyway. So this + # job compiled paperclip-runner-core in release mode on every run, + # 28-49s (runs 38169436532 to 38183149920, 2026-10-11), while 221 of + # the 239 master commits of the preceding week left its inputs alone. + # The master Rust lane in release-verify.yml saves that crate's release + # artifacts (about 70 MB) under a key made from the compiler, the + # checkout root and a hash of every input the build reads. A pull + # request whose merge tree carries byte-identical inputs restores them + # and marks them newer than the checkout, so cargo keeps them; any + # other pull request misses and compiles exactly as before. Exact key + # only: a prefix match would hand cargo another revision's output. The + # path list and the key live in scripts/runner-release-crate-cache.mjs + # and must match the writer's; a guard test holds them together. + - name: Compute the Runner release crate cache key + id: runner_release_crate_key + run: echo "scope=$(node scripts/runner-release-crate-cache.mjs scope)" >> "$GITHUB_OUTPUT" + + - name: Restore the Runner release crate (read only) + id: runner_release_crate + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: | + packages/paperclip-runner/runner/target/release/.fingerprint/paperclip-runner-core-* + packages/paperclip-runner/runner/target/release/deps/libpaperclip_runner_core-* + packages/paperclip-runner/runner/target/release/deps/paperclip_runner_core-*.d + packages/paperclip-runner/runner/target/release/deps/paperclip_runnerd-* + packages/paperclip-runner/runner/target/release/paperclip-runnerd + packages/paperclip-runner/runner/target/release/paperclip-runnerd.d + key: runner-release-crate-v1-${{ steps.runner_release_crate_key.outputs.scope }}-${{ hashFiles('packages/paperclip-runner/rust-toolchain.toml', 'packages/paperclip-runner/runner/Cargo.toml', 'packages/paperclip-runner/runner/Cargo.lock', 'packages/paperclip-runner/runner/crates/**', 'packages/paperclip-runner/protocol/**', 'packages/paperclip-runner/test-fixtures/**', 'packages/paperclip-runner/test/fixtures/**', 'tests/runner-e2e/fixtures/**') }} + + - name: Mark the restored Runner release crate current + if: ${{ steps.runner_release_crate.outputs.cache-hit == 'true' }} + run: node scripts/runner-release-crate-cache.mjs mark-current + - name: Typecheck workspaces whose build scripts skip TypeScript run: pnpm run typecheck:build-gaps @@ -749,10 +785,12 @@ jobs: command: check:static - lane_label: rust command: check:runner + release_crate: true - lane_label: vitest 1/2 command: test:typescript:vitest --shard=1/2 - lane_label: vitest 2/2 command: test:typescript:vitest --shard=2/2 + release_crate: true steps: - name: Checkout repository @@ -878,6 +916,49 @@ jobs: # Restore only. Never let a pull request evict master's entry. save-if: false + # The Runner's own crate is the one thing the entry above leaves out: + # rust-cache drops workspace crates when it saves, and a fresh checkout + # would invalidate cargo's mtime fingerprints for them anyway. So this + # job compiled paperclip-runner-core in release mode on every run, + # 28-49s (runs 38169436532 to 38183149920, 2026-10-11), while 221 of + # the 239 master commits of the preceding week left its inputs alone. + # The master Rust lane in release-verify.yml saves that crate's release + # artifacts (about 70 MB) under a key made from the compiler, the + # checkout root and a hash of every input the build reads. A pull + # request whose merge tree carries byte-identical inputs restores them + # and marks them newer than the checkout, so cargo keeps them; any + # other pull request misses and compiles exactly as before. Exact key + # only: a prefix match would hand cargo another revision's output. The + # path list and the key live in scripts/runner-release-crate-cache.mjs + # and must match the writer's; a guard test holds them together. + # + # Only the lanes that build the release crate restore it: the Rust + # lane's release tests link the lib, and the final vitest shard hosts + # the server's native-runner suite, which builds the binary in its + # beforeAll. The static and first vitest lanes run no cargo at all. + - name: Compute the Runner release crate cache key + id: runner_release_crate_key + if: ${{ matrix.release_crate == true }} + run: echo "scope=$(node scripts/runner-release-crate-cache.mjs scope)" >> "$GITHUB_OUTPUT" + + - name: Restore the Runner release crate (read only) + id: runner_release_crate + if: ${{ matrix.release_crate == true }} + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: | + packages/paperclip-runner/runner/target/release/.fingerprint/paperclip-runner-core-* + packages/paperclip-runner/runner/target/release/deps/libpaperclip_runner_core-* + packages/paperclip-runner/runner/target/release/deps/paperclip_runner_core-*.d + packages/paperclip-runner/runner/target/release/deps/paperclip_runnerd-* + packages/paperclip-runner/runner/target/release/paperclip-runnerd + packages/paperclip-runner/runner/target/release/paperclip-runnerd.d + key: runner-release-crate-v1-${{ steps.runner_release_crate_key.outputs.scope }}-${{ hashFiles('packages/paperclip-runner/rust-toolchain.toml', 'packages/paperclip-runner/runner/Cargo.toml', 'packages/paperclip-runner/runner/Cargo.lock', 'packages/paperclip-runner/runner/crates/**', 'packages/paperclip-runner/protocol/**', 'packages/paperclip-runner/test-fixtures/**', 'packages/paperclip-runner/test/fixtures/**', 'tests/runner-e2e/fixtures/**') }} + + - name: Mark the restored Runner release crate current + if: ${{ steps.runner_release_crate.outputs.cache-hit == 'true' }} + run: node scripts/runner-release-crate-cache.mjs mark-current + - name: Verify Paperclip Runner # pnpm appends trailing args to the end of the script's shell chain, # so the vitest lanes' --shard lands on `vitest run`. Do not add a @@ -1013,6 +1094,42 @@ jobs: # Restore only. Never let a pull request evict master's entry. save-if: false + # The Runner's own crate is the one thing the entry above leaves out: + # rust-cache drops workspace crates when it saves, and a fresh checkout + # would invalidate cargo's mtime fingerprints for them anyway. So this + # job compiled paperclip-runner-core in release mode on every run, + # 28-49s (runs 38169436532 to 38183149920, 2026-10-11), while 221 of + # the 239 master commits of the preceding week left its inputs alone. + # The master Rust lane in release-verify.yml saves that crate's release + # artifacts (about 70 MB) under a key made from the compiler, the + # checkout root and a hash of every input the build reads. A pull + # request whose merge tree carries byte-identical inputs restores them + # and marks them newer than the checkout, so cargo keeps them; any + # other pull request misses and compiles exactly as before. Exact key + # only: a prefix match would hand cargo another revision's output. The + # path list and the key live in scripts/runner-release-crate-cache.mjs + # and must match the writer's; a guard test holds them together. + - name: Compute the Runner release crate cache key + id: runner_release_crate_key + run: echo "scope=$(node scripts/runner-release-crate-cache.mjs scope)" >> "$GITHUB_OUTPUT" + + - name: Restore the Runner release crate (read only) + id: runner_release_crate + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: | + packages/paperclip-runner/runner/target/release/.fingerprint/paperclip-runner-core-* + packages/paperclip-runner/runner/target/release/deps/libpaperclip_runner_core-* + packages/paperclip-runner/runner/target/release/deps/paperclip_runner_core-*.d + packages/paperclip-runner/runner/target/release/deps/paperclip_runnerd-* + packages/paperclip-runner/runner/target/release/paperclip-runnerd + packages/paperclip-runner/runner/target/release/paperclip-runnerd.d + key: runner-release-crate-v1-${{ steps.runner_release_crate_key.outputs.scope }}-${{ hashFiles('packages/paperclip-runner/rust-toolchain.toml', 'packages/paperclip-runner/runner/Cargo.toml', 'packages/paperclip-runner/runner/Cargo.lock', 'packages/paperclip-runner/runner/crates/**', 'packages/paperclip-runner/protocol/**', 'packages/paperclip-runner/test-fixtures/**', 'packages/paperclip-runner/test/fixtures/**', 'tests/runner-e2e/fixtures/**') }} + + - name: Mark the restored Runner release crate current + if: ${{ steps.runner_release_crate.outputs.cache-hit == 'true' }} + run: node scripts/runner-release-crate-cache.mjs mark-current + - name: Build Runner Evalbook viewer run: pnpm --filter @paperclipai/paperclip-runner build:issue-thread @@ -1259,6 +1376,42 @@ jobs: # Restore only. Never let a pull request evict master's entry. save-if: false + # The Runner's own crate is the one thing the entry above leaves out: + # rust-cache drops workspace crates when it saves, and a fresh checkout + # would invalidate cargo's mtime fingerprints for them anyway. So this + # job compiled paperclip-runner-core in release mode on every run, + # 28-49s (runs 38169436532 to 38183149920, 2026-10-11), while 221 of + # the 239 master commits of the preceding week left its inputs alone. + # The master Rust lane in release-verify.yml saves that crate's release + # artifacts (about 70 MB) under a key made from the compiler, the + # checkout root and a hash of every input the build reads. A pull + # request whose merge tree carries byte-identical inputs restores them + # and marks them newer than the checkout, so cargo keeps them; any + # other pull request misses and compiles exactly as before. Exact key + # only: a prefix match would hand cargo another revision's output. The + # path list and the key live in scripts/runner-release-crate-cache.mjs + # and must match the writer's; a guard test holds them together. + - name: Compute the Runner release crate cache key + id: runner_release_crate_key + run: echo "scope=$(node scripts/runner-release-crate-cache.mjs scope)" >> "$GITHUB_OUTPUT" + + - name: Restore the Runner release crate (read only) + id: runner_release_crate + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: | + packages/paperclip-runner/runner/target/release/.fingerprint/paperclip-runner-core-* + packages/paperclip-runner/runner/target/release/deps/libpaperclip_runner_core-* + packages/paperclip-runner/runner/target/release/deps/paperclip_runner_core-*.d + packages/paperclip-runner/runner/target/release/deps/paperclip_runnerd-* + packages/paperclip-runner/runner/target/release/paperclip-runnerd + packages/paperclip-runner/runner/target/release/paperclip-runnerd.d + key: runner-release-crate-v1-${{ steps.runner_release_crate_key.outputs.scope }}-${{ hashFiles('packages/paperclip-runner/rust-toolchain.toml', 'packages/paperclip-runner/runner/Cargo.toml', 'packages/paperclip-runner/runner/Cargo.lock', 'packages/paperclip-runner/runner/crates/**', 'packages/paperclip-runner/protocol/**', 'packages/paperclip-runner/test-fixtures/**', 'packages/paperclip-runner/test/fixtures/**', 'tests/runner-e2e/fixtures/**') }} + + - name: Mark the restored Runner release crate current + if: ${{ steps.runner_release_crate.outputs.cache-hit == 'true' }} + run: node scripts/runner-release-crate-cache.mjs mark-current + # `release.sh` always executes its Step 2/7 workspace build, even when # `--skip-verify` bypasses the initial verification gate. release.sh # also requires a clean working tree, and the install step may have diff --git a/.github/workflows/release-verify.yml b/.github/workflows/release-verify.yml index 3fd321edb68..986968201d8 100644 --- a/.github/workflows/release-verify.yml +++ b/.github/workflows/release-verify.yml @@ -414,6 +414,61 @@ jobs: if: ${{ matrix.lane == 'rust' && github.repository == 'paperclipai/paperclip' && github.event_name == 'push' && github.ref == 'refs/heads/master' && inputs.ref == github.sha }} run: pnpm --filter @paperclipai/paperclip-runner build:rust + # The dependency cache above deliberately leaves the Runner's own crate + # out, so every pull request build compiled paperclip-runner-core in + # release mode (28-49s) although most pushes leave its inputs alone. + # This master push saves that crate's release artifacts for the readers + # in pr-trusted.yml ("Restore the Runner release crate") under a key + # made from the compiler, the checkout root and a hash of every input + # the build reads, so a reader only ever restores output of inputs + # byte-identical to its own. Same writer gate as the dependency cache: + # the Rust lane of an own-repository master push. The entry is looked + # up first; a push that left the inputs alone already has it. On a + # miss the crate is cleaned and built with the exact command the + # readers run, so the artifacts carry no test-profile variants. The + # path list and the key live in scripts/runner-release-crate-cache.mjs + # and must match the readers'; a guard test holds them together. + - name: Compute the Runner release crate cache key + id: runner_release_crate_key + if: ${{ matrix.lane == 'rust' && github.repository == 'paperclipai/paperclip' && github.event_name == 'push' && github.ref == 'refs/heads/master' && inputs.ref == github.sha }} + run: echo "scope=$(node scripts/runner-release-crate-cache.mjs scope)" >> "$GITHUB_OUTPUT" + + - name: Check for this push's Runner release crate + id: runner_release_crate + if: ${{ matrix.lane == 'rust' && github.repository == 'paperclipai/paperclip' && github.event_name == 'push' && github.ref == 'refs/heads/master' && inputs.ref == github.sha }} + uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + lookup-only: true + path: | + packages/paperclip-runner/runner/target/release/.fingerprint/paperclip-runner-core-* + packages/paperclip-runner/runner/target/release/deps/libpaperclip_runner_core-* + packages/paperclip-runner/runner/target/release/deps/paperclip_runner_core-*.d + packages/paperclip-runner/runner/target/release/deps/paperclip_runnerd-* + packages/paperclip-runner/runner/target/release/paperclip-runnerd + packages/paperclip-runner/runner/target/release/paperclip-runnerd.d + key: runner-release-crate-v1-${{ steps.runner_release_crate_key.outputs.scope }}-${{ hashFiles('packages/paperclip-runner/rust-toolchain.toml', 'packages/paperclip-runner/runner/Cargo.toml', 'packages/paperclip-runner/runner/Cargo.lock', 'packages/paperclip-runner/runner/crates/**', 'packages/paperclip-runner/protocol/**', 'packages/paperclip-runner/test-fixtures/**', 'packages/paperclip-runner/test/fixtures/**', 'tests/runner-e2e/fixtures/**') }} + + - name: Build the Runner release crate for its cache + if: ${{ matrix.lane == 'rust' && github.repository == 'paperclipai/paperclip' && github.event_name == 'push' && github.ref == 'refs/heads/master' && inputs.ref == github.sha && steps.runner_release_crate.outputs.cache-hit != 'true' }} + working-directory: packages/paperclip-runner + run: | + set -euo pipefail + cargo clean --release --manifest-path runner/Cargo.toml -p paperclip-runner-core + pnpm build:binary + + - name: Save the Runner release crate + if: ${{ matrix.lane == 'rust' && github.repository == 'paperclipai/paperclip' && github.event_name == 'push' && github.ref == 'refs/heads/master' && inputs.ref == github.sha && steps.runner_release_crate.outputs.cache-hit != 'true' }} + uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: | + packages/paperclip-runner/runner/target/release/.fingerprint/paperclip-runner-core-* + packages/paperclip-runner/runner/target/release/deps/libpaperclip_runner_core-* + packages/paperclip-runner/runner/target/release/deps/paperclip_runner_core-*.d + packages/paperclip-runner/runner/target/release/deps/paperclip_runnerd-* + packages/paperclip-runner/runner/target/release/paperclip-runnerd + packages/paperclip-runner/runner/target/release/paperclip-runnerd.d + key: runner-release-crate-v1-${{ steps.runner_release_crate_key.outputs.scope }}-${{ hashFiles('packages/paperclip-runner/rust-toolchain.toml', 'packages/paperclip-runner/runner/Cargo.toml', 'packages/paperclip-runner/runner/Cargo.lock', 'packages/paperclip-runner/runner/crates/**', 'packages/paperclip-runner/protocol/**', 'packages/paperclip-runner/test-fixtures/**', 'packages/paperclip-runner/test/fixtures/**', 'tests/runner-e2e/fixtures/**') }} + build: name: Build runs-on: ${{ vars.AWS_POST_MERGE_CI_ENABLED == 'true' && github.repository == 'paperclipai/paperclip' && github.repository_id == '1170821064' && github.ref == 'refs/heads/master' && (github.event_name == 'push' || github.event_name == 'workflow_dispatch') && github.sha != '' && inputs.ref == github.sha && 'runs-on/fleet=paperclip-post-merge-x64/env=public-ci' || 'ubuntu-latest' }} diff --git a/doc/RELEASE-AUTOMATION-SETUP.md b/doc/RELEASE-AUTOMATION-SETUP.md index 25890751a4d..1c65ae12824 100644 --- a/doc/RELEASE-AUTOMATION-SETUP.md +++ b/doc/RELEASE-AUTOMATION-SETUP.md @@ -363,14 +363,65 @@ paths and the PR lanes can restore master's entry. The guard tests under `.github/scripts/tests/` require this step, with identical text, before every Rust cache step in both workflows. -Workspace crates and installed Cargo binaries are excluded. Every run rebuilds -workspace code and runs all assigned checks, including on a cache hit. Only an +Workspace crates and installed Cargo binaries are excluded from this entry. +Every run runs all assigned checks, including on a cache hit. Only an own-repository master-push run verifying that push's exact SHA can restore the cache, and only a successful Rust lane saves it. PR, tag, and manual candidate verification compile without this cache. A miss or eviction costs compilation time but does not change the checks. To discard old dependency caches, increment the shared-key version and let the next successful master verification warm it. +### Runner release crate cache + +The dependency entry leaves the Runner's own crate out, and a fresh checkout +invalidates cargo's mtime fingerprints for workspace crates anyway, so every +pull request build compiled `paperclip-runner-core` in release mode from +scratch (28-49s per job on 2026-10-11) although most master commits leave its +inputs untouched. A second, content-addressed entry holds only that crate's +release artifacts: its fingerprints, the lib's rlib and rmeta, the +`paperclip-runnerd` binary and their dep-info, about 70 MB. The path list and +the key inputs live in `scripts/runner-release-crate-cache.mjs`, which both +workflows call, and `scripts/__tests__/runner-release-crate-cache.test.mjs` +holds the readers and the writer to them. + +The key is `runner-release-crate-v1--`. The scope names the +platform, the pinned compiler (`rustc --version --verbose`: release, commit and +host) and a hash of the checkout root, because cargo's dep-info records the +protocol files the crate includes by relative path as absolute paths. The +`hashFiles` part covers every input the build reads: the toolchain pin, the +manifests and lockfile, the crate sources, the protocol schemas and fixtures, +and the three fixture trees the test modules include (two under the Runner +package and `tests/runner-e2e/fixtures` at the repository root). Any change +to any of them selects a new key. A guard test resolves every `include_str!` +and `include_bytes!` in the crate, whether its argument is a literal or a +`concat!` of literals, `env!("CARGO_MANIFEST_DIR")` and macro metavariables, +and fails on an include that lands outside these patterns or uses a form it +cannot resolve. Readers pass no `restore-keys`: a prefix match would hand +cargo another revision's output. + +The writer is the Rust lane of the same master-push verification, after its +checks and the debug warm-up. It looks the key up first and, on a miss, runs +`cargo clean --release -p paperclip-runner-core` and then the package's +`build:binary` (the exact command the readers run) before saving, so the entry +carries no test-profile variants. In the trusted PR workflow, the Typecheck, +Build and Canary Dry Run jobs and the two Verify Paperclip Runner lanes that +build the crate (`rust` and the final vitest shard) restore the entry +read-only right after the dependency restore. On a hit, `mark-current` sets +the mtime of every restored artifact to now, so cargo's freshness check sees +every source as older than the build and keeps the artifacts. A restore that +lacks any file cargo reads or checks for either unit (the fingerprint hash +and dep-info of each unit, the lib's rlib and rmeta, the binary under +`deps`; measured with cargo 1.97.1, which recompiles without any one of +them and recreates the uplifted binary without any of the rest) is removed +instead, the job log names the missing file, and cargo compiles as on a +miss. `pnpm build` +then prints `Finished release profile` in well under a second for that crate. +On a miss nothing changes: cargo compiles the crate as before. + +The trust boundary is unchanged: only master writes, a pull request can only +restore master's entry, and it restores only artifacts built from inputs +byte-identical to its own merge tree. + The trust boundary is the protected master branch, not the cache-key text. GitHub does not let master restore caches created by a child branch, sibling branch, tag, or PR merge ref. Both permitted restore scopes (current branch and diff --git a/package.json b/package.json index 1d8f7fb078a..df1a2323f53 100644 --- a/package.json +++ b/package.json @@ -61,7 +61,7 @@ "smoke:posthog-live": "node scripts/smoke/posthog-live.mjs", "smoke:pipelines-tutorial": "./scripts/smoke/pipelines-tutorial-smoke.sh", "smoke:terminal-bench-loop-skill": "node scripts/smoke/terminal-bench-loop-skill-smoke.mjs", - "test:release-registry": "node --test scripts/__tests__/canary-startup-playwright.test.mjs scripts/__tests__/cursor-public-install-sandbox.test.mjs scripts/__tests__/grok-public-install-sandbox.test.mjs scripts/__tests__/prefetch-child.test.mjs scripts/__tests__/stage-bundled-packages.test.mjs scripts/__tests__/pack-staged-packages.test.mjs scripts/__tests__/retain-runner-qualification-packages.test.mjs scripts/verify-release-registry-state.test.mjs scripts/release-package-map.test.mjs scripts/check-release-package-bootstrap.test.mjs scripts/check-no-git-push.test.mjs scripts/release-lib.test.mjs scripts/release-registry-versions.test.mjs scripts/link-plugin-dev-sdk.test.js scripts/acpx-patch-packaging.test.mjs scripts/service-onboard-smoke.test.mjs scripts/docker-onboard-smoke.test.mjs scripts/preview-artifacts.test.mjs scripts/cloud-migrator-artifacts.test.mjs", + "test:release-registry": "node --test scripts/__tests__/canary-startup-playwright.test.mjs scripts/__tests__/cursor-public-install-sandbox.test.mjs scripts/__tests__/grok-public-install-sandbox.test.mjs scripts/__tests__/prefetch-child.test.mjs scripts/__tests__/stage-bundled-packages.test.mjs scripts/__tests__/pack-staged-packages.test.mjs scripts/__tests__/retain-runner-qualification-packages.test.mjs scripts/__tests__/runner-release-crate-cache.test.mjs scripts/verify-release-registry-state.test.mjs scripts/release-package-map.test.mjs scripts/check-release-package-bootstrap.test.mjs scripts/check-no-git-push.test.mjs scripts/release-lib.test.mjs scripts/release-registry-versions.test.mjs scripts/link-plugin-dev-sdk.test.js scripts/acpx-patch-packaging.test.mjs scripts/service-onboard-smoke.test.mjs scripts/docker-onboard-smoke.test.mjs scripts/preview-artifacts.test.mjs scripts/cloud-migrator-artifacts.test.mjs", "storybook-visual:baseline": "node scripts/storybook-visual-baseline.mjs", "test:storybook-visual": "node scripts/storybook-visual-baseline.mjs download && node scripts/storybook-visual-baseline.mjs verify && pnpm build-storybook && npx playwright test --config tests/storybook-visual/playwright.config.ts", "test:storybook-visual:update": "node scripts/storybook-visual-baseline.mjs download && pnpm build-storybook && npx playwright test --config tests/storybook-visual/playwright.config.ts --update-snapshots && node scripts/storybook-visual-baseline.mjs pack", diff --git a/scripts/__tests__/runner-release-crate-cache.test.mjs b/scripts/__tests__/runner-release-crate-cache.test.mjs new file mode 100644 index 00000000000..e85b3b7b45f --- /dev/null +++ b/scripts/__tests__/runner-release-crate-cache.test.mjs @@ -0,0 +1,558 @@ +import assert from "node:assert/strict"; +import { execFileSync, spawnSync } from "node:child_process"; +import { chmodSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, statSync, utimesSync, writeFileSync } from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import test from "node:test"; +import { fileURLToPath } from "node:url"; + +import { CACHED_PATHS, HASHED_INPUTS, REQUIRED, RUNNER_PACKAGE, computeScope, markCurrent } from "../runner-release-crate-cache.mjs"; + +const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../.."); +const script = path.join(repoRoot, "scripts/runner-release-crate-cache.mjs"); +const readWorkflow = (name) => readFileSync(path.join(repoRoot, ".github/workflows", name), "utf8"); +const prWorkflow = readWorkflow("pr-trusted.yml"); +const releaseWorkflow = readWorkflow("release-verify.yml"); + +// Slice one job out of a workflow, from its two-space-indented key to the next. +function job(workflow, name) { + const start = workflow.indexOf(`\n ${name}:\n`); + assert.ok(start >= 0, `workflow is missing the ${name} job`); + const body = workflow.slice(start + 1); + const header = ` ${name}:\n`.length; + const next = body.slice(header).search(/\n [a-z0-9_-]+:\n/); + return next === -1 ? body : body.slice(0, header + next + 1); +} + +// Slice one step out of a job by its name, up to the next step. +function step(body, name) { + const marker = ` - name: ${name}\n`; + const start = body.indexOf(marker); + assert.ok(start >= 0, `missing step: ${name}`); + const rest = body.slice(start + marker.length); + const next = rest.search(/\n - name: /); + return marker + (next === -1 ? rest : rest.slice(0, next + 1)); +} + +const RELEASE_DIR = `${RUNNER_PACKAGE}/runner/target/release`; +const KEY_STEP = "Compute the Runner release crate cache key"; +const RESTORE_STEP = "Restore the Runner release crate (read only)"; +const MARK_STEP = "Mark the restored Runner release crate current"; +const LOOKUP_STEP = "Check for this push's Runner release crate"; +const BUILD_STEP = "Build the Runner release crate for its cache"; +const SAVE_STEP = "Save the Runner release crate"; +const RUST_RESTORE_STEP = "Restore Runner Rust dependencies (read only)"; + +// Every PR job that builds the Runner release binary, and the command each +// one runs cargo through. The crate must be restored and marked before it. +const READERS = [ + ["typecheck_release_registry", "run: pnpm run typecheck:build-gaps"], + ["verify_paperclip_runner", "run: pnpm --filter @paperclipai/paperclip-runner ${{ matrix.command }}"], + ["build", "run: pnpm build"], + ["canary_dry_run", "- name: Release canary dry run via release.sh internal build"], +]; + +const WRITER_GATE = "matrix.lane == 'rust' && github.repository == 'paperclipai/paperclip' && github.event_name == 'push' && github.ref == 'refs/heads/master' && inputs.ref == github.sha"; + +const expectedPathBlock = ` path: |\n${CACHED_PATHS.map((entry) => ` ${entry}\n`).join("")}`; +const expectedKey = `key: runner-release-crate-v1-\${{ steps.runner_release_crate_key.outputs.scope }}-\${{ hashFiles(${HASHED_INPUTS.map((entry) => `'${entry}'`).join(", ")}) }}`; + +function assertCacheStep(body, name, { lookupOnly = false } = {}) { + assert.match(body, /uses: actions\/cache\/(restore|save)@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6\.1\.0/, name); + assert.ok(body.includes(expectedPathBlock), `${name}: the cached path list must be exactly the script's CACHED_PATHS`); + assert.ok(body.includes(expectedKey), `${name}: the key must be built from the script's scope and HASHED_INPUTS`); + // An exact match or nothing: a prefix match would restore another + // revision's artifacts and the mark step would make cargo keep them. + assert.doesNotMatch(body, /restore-keys:/, name); + assert.equal(/lookup-only: true/.test(body), lookupOnly, name); + assert.doesNotMatch(body, /enableCrossOsArchive|fail-on-cache-miss: true/, name); +} + +test("the script's path list and key inputs cover what the release build of the crate reads and writes", () => { + assert.deepEqual(CACHED_PATHS, [ + `${RELEASE_DIR}/.fingerprint/paperclip-runner-core-*`, + `${RELEASE_DIR}/deps/libpaperclip_runner_core-*`, + `${RELEASE_DIR}/deps/paperclip_runner_core-*.d`, + `${RELEASE_DIR}/deps/paperclip_runnerd-*`, + `${RELEASE_DIR}/paperclip-runnerd`, + `${RELEASE_DIR}/paperclip-runnerd.d`, + ]); + assert.deepEqual(HASHED_INPUTS, [ + `${RUNNER_PACKAGE}/rust-toolchain.toml`, + `${RUNNER_PACKAGE}/runner/Cargo.toml`, + `${RUNNER_PACKAGE}/runner/Cargo.lock`, + `${RUNNER_PACKAGE}/runner/crates/**`, + `${RUNNER_PACKAGE}/protocol/**`, + `${RUNNER_PACKAGE}/test-fixtures/**`, + `${RUNNER_PACKAGE}/test/fixtures/**`, + "tests/runner-e2e/fixtures/**", + ]); + for (const input of HASHED_INPUTS) { + const fixed = input.replace(/\/\*\*$/, ""); + assert.ok(statSync(path.join(repoRoot, fixed)), `${input} must exist in the repository`); + } + // The stage step copies the uplifted binary; it has to be in the set. + const stage = readFileSync(path.join(repoRoot, RUNNER_PACKAGE, "scripts/stage-runner-binary.mjs"), "utf8"); + assert.match(stage, /"runner", "target", "release", executable/); + assert.ok(CACHED_PATHS.includes(`${RELEASE_DIR}/paperclip-runnerd`)); +}); + +// Every `include_str!` / `include_bytes!` in one Rust source, with the path +// each one names. Three argument forms are resolved: a string literal +// (relative to the source file), and `concat!(...)` of string literals, +// `env!("CARGO_MANIFEST_DIR")` (the directory of the nearest Cargo.toml) and +// macro metavariables (`$name`, which make the resolved path a directory +// prefix rather than a file). Anything else is reported as unsupported, so +// a new include form cannot slip past the guard by not matching a regex. +function crateIncludes(source, text) { + const includes = []; + for (const match of text.matchAll(/include_(?:str|bytes)!\s*\(/g)) { + const start = match.index + match[0].length; + let depth = 1; + let inString = false; + let end = start; + for (; end < text.length && depth > 0; end += 1) { + const ch = text[end]; + if (inString) { + if (ch === "\\") end += 1; + else if (ch === '"') inString = false; + } else if (ch === '"') inString = true; + else if (ch === "(") depth += 1; + else if (ch === ")") depth -= 1; + } + assert.equal(depth, 0, `${source}: unbalanced include at offset ${match.index}`); + const argument = text.slice(start, end - 1).trim(); + includes.push({ argument, ...resolveIncludeArgument(source, argument) }); + } + return includes; +} + +function manifestDir(source) { + let dir = path.dirname(source); + while (!statSync(path.join(dir, "Cargo.toml"), { throwIfNoEntry: false })?.isFile()) { + const parent = path.dirname(dir); + assert.notEqual(parent, dir, `${source}: no Cargo.toml above the source`); + dir = parent; + } + return dir; +} + +function resolveIncludeArgument(source, argument) { + const literal = argument.match(/^"((?:[^"\\]|\\.)*)"$/); + if (literal) return { form: "literal", resolved: path.resolve(path.dirname(source), literal[1]), exact: true }; + + const concat = argument.match(/^concat!\s*\(([\s\S]*)\)$/); + assert.ok(concat, `${source}: unsupported include form: ${argument}`); + const parts = splitTopLevel(concat[1]); + // A relative path is joined to the source's directory; one that starts + // with the manifest directory is one string, since its next literal + // starts with the "/" that separates the two. + let base = `${path.dirname(source)}/`; + let text = ""; + let exact = true; + let form = "concat"; + for (const [index, part] of parts.entries()) { + const string = part.match(/^"((?:[^"\\]|\\.)*)"$/); + if (string) { + assert.ok(exact || !/(^|\/)\.\.(\/|$)/.test(string[1]), `${source}: a path segment after a metavariable must not climb out: ${argument}`); + if (exact) text += string[1]; + continue; + } + if (part === 'env!("CARGO_MANIFEST_DIR")') { + assert.equal(index, 0, `${source}: env!("CARGO_MANIFEST_DIR") must be the first part of concat!: ${argument}`); + base = manifestDir(source); + form = "concat-manifest-dir"; + continue; + } + if (/^\$[A-Za-z_][A-Za-z0-9_]*$/.test(part)) { + // The fixed prefix before the first metavariable is the directory the + // macro's callers can reach; the suffix cannot climb out of it. + exact = false; + form = form === "concat-manifest-dir" ? form : "concat-metavariable"; + continue; + } + assert.fail(`${source}: unsupported concat! part in include: ${part}`); + } + const joined = base + text; + const resolved = path.resolve(exact || text.endsWith("/") ? joined : path.dirname(joined)); + return { form, resolved, exact }; +} + +// Split concat!'s arguments on the commas outside string literals. +function splitTopLevel(text) { + const parts = []; + let current = ""; + let inString = false; + for (let index = 0; index < text.length; index += 1) { + const ch = text[index]; + if (inString) { + current += ch; + if (ch === "\\") current += text[++index] ?? ""; + else if (ch === '"') inString = false; + } else if (ch === '"') { + inString = true; + current += ch; + } else if (ch === ",") { + parts.push(current); + current = ""; + } else current += ch; + } + parts.push(current); + return parts.map((part) => part.trim()).filter(Boolean); +} + +const underHashedInputs = (resolved) => + HASHED_INPUTS.map((input) => path.join(repoRoot, input.replace(/\/\*\*$/, ""))).some((root) => resolved === root || resolved.startsWith(`${root}${path.sep}`)); + +test("the include parser resolves literals, concat! with the manifest directory and with metavariables, and rejects other forms", () => { + const root = mkdtempSync(path.join(os.tmpdir(), "runner-release-crate-includes-")); + try { + const crate = path.join(root, "crates/demo"); + mkdirSync(path.join(crate, "src/nested"), { recursive: true }); + mkdirSync(path.join(root, "protocol/fixtures"), { recursive: true }); + writeFileSync(path.join(crate, "Cargo.toml"), '[package]\nname = "demo"\n'); + writeFileSync(path.join(root, "protocol/schema.json"), "{}"); + const source = path.join(crate, "src/nested/mod.rs"); + const text = [ + 'let a = include_str!("../../../../protocol/schema.json");', + 'let b = include_bytes!(\n "../../../../protocol/schema.json"\n);', + 'let c = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../protocol/", "schema.json"));', + 'let d = include_str!(concat!("../../../../protocol/fixtures/", $fixture, ".json"));', + 'let e = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/../../protocol/fixtures/", $golden, ".summary.json"));', + 'let f = include_str!("a (paren) in the name.json");', + ].join("\n"); + const includes = crateIncludes(source, text); + assert.deepEqual( + includes.map(({ form, resolved, exact }) => [form, path.relative(root, resolved), exact]), + [ + ["literal", "protocol/schema.json", true], + ["literal", "protocol/schema.json", true], + ["concat-manifest-dir", "protocol/schema.json", true], + ["concat-metavariable", "protocol/fixtures", false], + ["concat-manifest-dir", "protocol/fixtures", false], + ["literal", "crates/demo/src/nested/a (paren) in the name.json", true], + ], + ); + + const rejected = [ + 'include_str!(concat!("../protocol/", $x, "/../../../outside.json"))', + 'include_str!(concat!("../protocol/", env!("CARGO_MANIFEST_DIR")))', + 'include_str!(concat!(env!("OTHER_DIR"), "/schema.json"))', + 'include_str!(r"../protocol/schema.json")', + 'include_str!(path_from_a_function())', + 'include_str!(concat!("../protocol/", some_macro!("x")))', + ]; + for (const argument of rejected) { + assert.throws(() => crateIncludes(source, argument), /unsupported|must not climb out|must be the first part/, argument); + } + } finally { + rmSync(root, { recursive: true, force: true }); + } +}); + +test("every file the crate includes by path sits under a hashed input, so an edit to it changes the key", () => { + const crateRoot = path.join(repoRoot, RUNNER_PACKAGE, "runner/crates"); + const sources = []; + (function walk(dir) { + for (const entry of readdirSync(dir, { withFileTypes: true })) { + const full = path.join(dir, entry.name); + if (entry.isDirectory()) walk(full); + else if (entry.name.endsWith(".rs")) sources.push(full); + } + })(crateRoot); + assert.ok(sources.length > 10, "the crate sources must be found"); + const forms = new Set(); + let includes = 0; + for (const source of sources) { + for (const { argument, form, resolved, exact } of crateIncludes(source, readFileSync(source, "utf8"))) { + includes += 1; + forms.add(form); + const where = `${path.relative(repoRoot, source)} includes ${argument.replace(/\s+/g, " ")}`; + assert.ok(underHashedInputs(resolved), `${where}, which resolves to ${path.relative(repoRoot, resolved)}, outside every HASHED_INPUTS pattern`); + const info = statSync(resolved, { throwIfNoEntry: false }); + assert.ok(exact ? info?.isFile() : info?.isDirectory(), `${where}: ${path.relative(repoRoot, resolved)} must exist`); + } + } + assert.ok(includes > 0, "the crate includes protocol files by path; the guard must see them"); + // The crate uses every supported form today, so none of the parser's + // branches is exercised only by the synthetic test above. + assert.deepEqual([...forms].sort(), ["concat-manifest-dir", "concat-metavariable", "literal"]); +}); + +test("every PR reader restores the crate after the dependency cache and marks it before building", () => { + for (const [name, buildCommand] of READERS) { + const body = job(prWorkflow, name); + const positions = [RUST_RESTORE_STEP, KEY_STEP, RESTORE_STEP, MARK_STEP].map((stepName) => body.indexOf(` - name: ${stepName}\n`)); + assert.ok(positions.every((position) => position >= 0), `${name}: a crate cache step is missing`); + assert.deepEqual([...positions].sort((a, b) => a - b), positions, `${name}: the crate steps must follow the dependency restore in order`); + const build = body.indexOf(buildCommand); + assert.ok(build > positions.at(-1), `${name}: the crate must be marked before ${buildCommand}`); + + const key = step(body, KEY_STEP); + assert.match(key, /id: runner_release_crate_key\n/, name); + assert.match(key, /run: echo "scope=\$\(node scripts\/runner-release-crate-cache\.mjs scope\)" >> "\$GITHUB_OUTPUT"/, name); + + const restore = step(body, RESTORE_STEP); + assert.match(restore, /id: runner_release_crate\n/, name); + assertCacheStep(restore, `${name} restore`); + assert.doesNotMatch(restore, /actions\/cache\/save/, name); + + const mark = step(body, MARK_STEP); + assert.match(mark, /if: \$\{\{ steps\.runner_release_crate\.outputs\.cache-hit == 'true' \}\}/, name); + assert.match(mark, /run: node scripts\/runner-release-crate-cache\.mjs mark-current/, name); + } + // No other job restores or saves the crate. + const total = prWorkflow.match(/- name: Restore the Runner release crate \(read only\)\n/g)?.length ?? 0; + assert.equal(total, READERS.length); + assert.doesNotMatch(prWorkflow, /actions\/cache\/save@/); +}); + +test("the Verify Paperclip Runner job restores the crate only in the lanes that build it", () => { + const body = job(prWorkflow, "verify_paperclip_runner"); + const lanes = [...body.matchAll(/- lane_label: (.+)\n\s+command: (.+)\n(\s+release_crate: (true)\n)?/g)].map(([, label, , , flag]) => [label, flag === "true"]); + assert.deepEqual(lanes, [ + ["static checks", false], + ["rust", true], + ["vitest 1/2", false], + ["vitest 2/2", true], + ]); + for (const stepName of [KEY_STEP, RESTORE_STEP]) { + assert.match(step(body, stepName), /if: \$\{\{ matrix\.release_crate == true \}\}/, stepName); + } + // A lane that skipped the restore has no cache-hit output, so the mark + // step's own condition is enough there. + assert.match(step(body, MARK_STEP), /if: \$\{\{ steps\.runner_release_crate\.outputs\.cache-hit == 'true' \}\}/); + for (const [name] of READERS.filter(([name]) => name !== "verify_paperclip_runner")) { + for (const stepName of [KEY_STEP, RESTORE_STEP]) { + assert.doesNotMatch(step(job(prWorkflow, name), stepName), /^\s+if:/m, `${name}: ${stepName} is unconditional`); + } + } +}); + +test("only the master Rust lane writes the crate, after a lookup, with the readers' exact path list and key", () => { + const body = job(releaseWorkflow, "verify_paperclip_runner"); + const positions = ["Verify Paperclip Runner", "Warm debug dependencies for the shared Runner cache", KEY_STEP, LOOKUP_STEP, BUILD_STEP, SAVE_STEP].map((stepName) => body.indexOf(` - name: ${stepName}\n`)); + assert.ok(positions.every((position) => position >= 0), "a writer step is missing"); + assert.deepEqual([...positions].sort((a, b) => a - b), positions, "the writer steps must follow the checks and the debug warm-up, in order"); + + for (const stepName of [KEY_STEP, LOOKUP_STEP]) { + assert.match(step(body, stepName), new RegExp(`if: \\$\\{\\{ ${WRITER_GATE.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")} \\}\\}`), stepName); + } + for (const stepName of [BUILD_STEP, SAVE_STEP]) { + assert.match( + step(body, stepName), + new RegExp(`if: \\$\\{\\{ ${WRITER_GATE.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")} && steps\\.runner_release_crate\\.outputs\\.cache-hit != 'true' \\}\\}`), + stepName, + ); + } + assert.match(step(body, KEY_STEP), /run: echo "scope=\$\(node scripts\/runner-release-crate-cache\.mjs scope\)" >> "\$GITHUB_OUTPUT"/); + + const lookup = step(body, LOOKUP_STEP); + assert.match(lookup, /id: runner_release_crate\n/); + assert.match(lookup, /uses: actions\/cache\/restore@/); + assertCacheStep(lookup, "writer lookup", { lookupOnly: true }); + + // The saved artifacts come from the readers' exact command, on a crate + // cleaned of the test-profile variants the lane's checks built. + const build = step(body, BUILD_STEP); + assert.match(build, /working-directory: packages\/paperclip-runner\n/); + assert.match(build, /set -euo pipefail\n\s+cargo clean --release --manifest-path runner\/Cargo\.toml -p paperclip-runner-core\n\s+pnpm build:binary\n/); + const runnerScripts = JSON.parse(readFileSync(path.join(repoRoot, RUNNER_PACKAGE, "package.json"), "utf8")).scripts; + assert.match(runnerScripts["build:binary"], /^cargo build --release --manifest-path runner\/Cargo\.toml --locked -p paperclip-runner-core --bin paperclip-runnerd && /); + + const save = step(body, SAVE_STEP); + assert.match(save, /uses: actions\/cache\/save@/); + assertCacheStep(save, "writer save"); + + // The dependency cache's own writer gate is the same expression. + assert.match(body, new RegExp(`save-if: \\$\\{\\{ ${WRITER_GATE.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")} \\}\\}`)); + assert.equal((releaseWorkflow.match(/actions\/cache\/save@/g) ?? []).length, 1); +}); + +test("the scope names the platform, the compiler and the checkout root, and survives a missing rustc", () => { + const a = computeScope({ workspace: "/home/runner/_work/paperclip/paperclip", rustcVersion: "1.97.1-abcdef123-x86_64-unknown-linux-gnu" }); + const b = computeScope({ workspace: "/home/runner/work/paperclip/paperclip", rustcVersion: "1.97.1-abcdef123-x86_64-unknown-linux-gnu" }); + const c = computeScope({ workspace: "/home/runner/_work/paperclip/paperclip", rustcVersion: "1.97.2-0123456ab-x86_64-unknown-linux-gnu" }); + assert.notEqual(a, b, "a different checkout root must select a different entry"); + assert.notEqual(a, c, "a different compiler must select a different entry"); + assert.equal(a, computeScope({ workspace: "/home/runner/_work/paperclip/paperclip/", rustcVersion: "1.97.1-abcdef123-x86_64-unknown-linux-gnu" })); + assert.match(a, new RegExp(`^${os.platform()}-${os.arch()}-1\\.97\\.1-abcdef123-x86_64-unknown-linux-gnu-[0-9a-f]{12}$`)); + assert.doesNotMatch(computeScope({ rustcVersion: "weird version/with spaces" }), /[^A-Za-z0-9._-]/); + + // The CLI reads rustc from PATH; a stand-in proves which fields it reads, + // and an empty PATH proves a missing compiler still yields a usable key. + const bin = mkdtempSync(path.join(os.tmpdir(), "runner-release-crate-rustc-")); + try { + const stub = path.join(bin, "rustc"); + writeFileSync(stub, '#!/bin/sh\nprintf "rustc 1.97.1 (8bab26f4f 2026-06-30)\\nbinary: rustc\\ncommit-hash: 8bab26f4f0123456789abcdef\\ncommit-date: 2026-06-30\\nhost: x86_64-unknown-linux-gnu\\nrelease: 1.97.1\\nLLVM version: 21.1.2\\n"\n'); + chmodSync(stub, 0o755); + const withStub = execFileSync(process.execPath, [script, "scope"], { encoding: "utf8", env: { ...process.env, PATH: bin, GITHUB_WORKSPACE: "/home/runner/_work/paperclip/paperclip" } }).trim(); + assert.equal(withStub, computeScope({ workspace: "/home/runner/_work/paperclip/paperclip", rustcVersion: "1.97.1-8bab26f4f-x86_64-unknown-linux-gnu" })); + const without = execFileSync(process.execPath, [script, "scope"], { encoding: "utf8", env: { ...process.env, PATH: mkdtempSync(path.join(os.tmpdir(), "empty-path-")), GITHUB_WORKSPACE: "/home/runner/_work/paperclip/paperclip" } }).trim(); + assert.match(without, /-no-rustc-/); + } finally { + rmSync(bin, { recursive: true, force: true }); + } +}); + +// The tree cargo leaves behind for the crate's release lib and bin units, +// with the mtimes GitHub's cache restores: older than a fresh checkout. +function layRestoredCrate(root) { + const release = path.join(root, RELEASE_DIR); + const lib = path.join(release, ".fingerprint/paperclip-runner-core-0b6c4654c2710743"); + const bin = path.join(release, ".fingerprint/paperclip-runner-core-46cce78dbc4840c2"); + mkdirSync(path.join(release, "deps"), { recursive: true }); + mkdirSync(lib, { recursive: true }); + mkdirSync(bin, { recursive: true }); + const files = { + [path.join(lib, "lib-paperclip_runner_core")]: "0123456789abcdef", + [path.join(lib, "lib-paperclip_runner_core.json")]: "{}", + [path.join(lib, "dep-lib-paperclip_runner_core")]: "dep", + [path.join(lib, "invoked.timestamp")]: "t", + [path.join(bin, "bin-paperclip-runnerd")]: "fedcba9876543210", + [path.join(bin, "bin-paperclip-runnerd.json")]: "{}", + [path.join(bin, "dep-bin-paperclip-runnerd")]: "dep", + [path.join(release, "deps/libpaperclip_runner_core-0b6c4654c2710743.rlib")]: "rlib", + [path.join(release, "deps/libpaperclip_runner_core-0b6c4654c2710743.rmeta")]: "rmeta", + [path.join(release, "deps/paperclip_runner_core-0b6c4654c2710743.d")]: "d", + [path.join(release, "deps/paperclip_runnerd-46cce78dbc4840c2")]: "ELF", + [path.join(release, "deps/paperclip_runnerd-46cce78dbc4840c2.d")]: "d", + [path.join(release, "paperclip-runnerd")]: "ELF", + [path.join(release, "paperclip-runnerd.d")]: "d", + // A dependency's artifacts sit beside the crate's and must not be touched. + [path.join(release, "deps/libserde-1234567890abcdef.rlib")]: "dep rlib", + }; + const old = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000); + for (const [file, content] of Object.entries(files)) { + writeFileSync(file, content); + utimesSync(file, old, old); + } + for (const dir of [lib, bin, path.join(release, "deps"), path.join(release, ".fingerprint"), release]) utimesSync(dir, old, old); + return { release, files: Object.keys(files), old }; +} + +test("mark-current makes every restored artifact newer than the checkout and leaves dependencies alone", () => { + const root = mkdtempSync(path.join(os.tmpdir(), "runner-release-crate-")); + try { + const { release, files, old } = layRestoredCrate(root); + const checkout = path.join(root, RUNNER_PACKAGE, "runner/crates/runner-core/src/lib.rs"); + mkdirSync(path.dirname(checkout), { recursive: true }); + writeFileSync(checkout, "// checked out after the artifacts were built\n"); + const checkedOutAt = statSync(checkout).mtimeMs; + + const result = markCurrent({ root, now: new Date(checkedOutAt + 5000) }); + assert.equal(result.action, "marked"); + assert.equal(result.files, 14); + assert.equal(result.bytes, files.filter((file) => !file.includes("libserde")).reduce((sum, file) => sum + statSync(file).size, 0)); + + for (const file of files) { + const mtime = statSync(file).mtimeMs; + if (file.includes("libserde")) assert.equal(Math.floor(mtime / 1000), Math.floor(old.getTime() / 1000), "a dependency artifact must keep its mtime"); + else assert.ok(mtime > checkedOutAt, `${path.relative(release, file)} must be newer than the checkout`); + } + for (const dir of [".fingerprint/paperclip-runner-core-0b6c4654c2710743", ".fingerprint/paperclip-runner-core-46cce78dbc4840c2"]) { + assert.ok(statSync(path.join(release, dir)).mtimeMs > checkedOutAt, `${dir} must be newer than the checkout`); + } + assert.ok(statSync(checkout).mtimeMs === checkedOutAt, "sources are never touched"); + + // The CLI prints the same result as one line of JSON for the job log. + const printed = JSON.parse(execFileSync(process.execPath, [script, "mark-current"], { encoding: "utf8", env: { ...process.env, GITHUB_WORKSPACE: root } })); + assert.equal(printed.schema, "paperclip.runner-release-crate.v1"); + assert.equal(printed.action, "marked"); + assert.equal(printed.files, 14); + } finally { + rmSync(root, { recursive: true, force: true }); + } +}); + +// One file per REQUIRED entry, as cargo 1.97.1 lays them out for the crate's +// release lib and bin units. Measured: without any one of these cargo +// recompiles the unit (the whole crate for a lib file, a relink for a bin +// file); without any of the files in NOT_NEEDED it reports the crate fresh +// and recreates the uplifted binary from the one under deps. +const REQUIRED_FILES = { + "the lib fingerprint hash": ".fingerprint/paperclip-runner-core-0b6c4654c2710743/lib-paperclip_runner_core", + "the lib dep-info": ".fingerprint/paperclip-runner-core-0b6c4654c2710743/dep-lib-paperclip_runner_core", + "the lib rlib": "deps/libpaperclip_runner_core-0b6c4654c2710743.rlib", + "the lib rmeta": "deps/libpaperclip_runner_core-0b6c4654c2710743.rmeta", + "the bin fingerprint hash": ".fingerprint/paperclip-runner-core-46cce78dbc4840c2/bin-paperclip-runnerd", + "the bin dep-info": ".fingerprint/paperclip-runner-core-46cce78dbc4840c2/dep-bin-paperclip-runnerd", + "the bin under deps": "deps/paperclip_runnerd-46cce78dbc4840c2", +}; +const NOT_NEEDED = [ + ".fingerprint/paperclip-runner-core-0b6c4654c2710743/lib-paperclip_runner_core.json", + ".fingerprint/paperclip-runner-core-0b6c4654c2710743/invoked.timestamp", + ".fingerprint/paperclip-runner-core-46cce78dbc4840c2/bin-paperclip-runnerd.json", + "deps/paperclip_runner_core-0b6c4654c2710743.d", + "deps/paperclip_runnerd-46cce78dbc4840c2.d", + "paperclip-runnerd", + "paperclip-runnerd.d", +]; + +test("mark-current discards a restore that lacks any file cargo needs for either unit, naming the file, so cargo sees a plain miss", () => { + assert.deepEqual(REQUIRED.map(({ label }) => label), Object.keys(REQUIRED_FILES), "every REQUIRED entry has a missing-file case"); + for (const [label, file] of Object.entries(REQUIRED_FILES)) { + const root = mkdtempSync(path.join(os.tmpdir(), "runner-release-crate-")); + try { + const { release } = layRestoredCrate(root); + rmSync(path.join(release, file)); + const result = markCurrent({ root }); + assert.equal(result.action, "discarded", `${file} missing must discard the restore`); + assert.equal(result.reason, `incomplete: ${label} is missing`); + assert.ok(result.removed > 0, label); + assert.deepEqual(readdirSync(path.join(release, ".fingerprint")), [], label); + assert.deepEqual(readdirSync(path.join(release, "deps")), ["libserde-1234567890abcdef.rlib"], `${label}: only the crate's own artifacts are removed`); + assert.deepEqual(readdirSync(release).sort(), [".fingerprint", "deps"], label); + } finally { + rmSync(root, { recursive: true, force: true }); + } + } + + // The bin's dep-info alone does not stand in for the bin: the glob that + // finds the binary under deps must not be satisfied by the `.d` beside it. + const root = mkdtempSync(path.join(os.tmpdir(), "runner-release-crate-")); + try { + const { release } = layRestoredCrate(root); + rmSync(path.join(release, "deps/paperclip_runnerd-46cce78dbc4840c2")); + assert.ok(statSync(path.join(release, "deps/paperclip_runnerd-46cce78dbc4840c2.d")).isFile()); + assert.equal(markCurrent({ root }).reason, "incomplete: the bin under deps is missing"); + } finally { + rmSync(root, { recursive: true, force: true }); + } + + // A file cargo neither reads nor checks for may be absent without costing + // a rebuild, so its absence must not cost one here either. + for (const file of NOT_NEEDED) { + const intact = mkdtempSync(path.join(os.tmpdir(), "runner-release-crate-")); + try { + const { release } = layRestoredCrate(intact); + rmSync(path.join(release, file)); + const result = markCurrent({ root: intact }); + assert.equal(result.action, "marked", `${file} missing must not discard the restore`); + assert.equal(result.files, 13, file); + } finally { + rmSync(intact, { recursive: true, force: true }); + } + } + + // Nothing restored at all is reported the same way and exits cleanly. + const empty = mkdtempSync(path.join(os.tmpdir(), "runner-release-crate-empty-")); + try { + const printed = JSON.parse(execFileSync(process.execPath, [script, "mark-current"], { encoding: "utf8", env: { ...process.env, GITHUB_WORKSPACE: empty } })); + assert.equal(printed.action, "discarded"); + assert.equal(printed.reason, "incomplete: the lib fingerprint hash is missing"); + assert.equal(printed.removed, 0); + } finally { + rmSync(empty, { recursive: true, force: true }); + } +}); + +test("the CLI prints the lists the workflows embed", () => { + const paths = execFileSync(process.execPath, [script, "paths"], { encoding: "utf8" }).trim().split("\n"); + assert.deepEqual(paths, CACHED_PATHS); + const inputs = execFileSync(process.execPath, [script, "hashed-inputs"], { encoding: "utf8" }).trim().split("\n"); + assert.deepEqual(inputs, HASHED_INPUTS); + const usage = spawnSync(process.execPath, [script, "nonsense"], { encoding: "utf8" }); + assert.equal(usage.status, 2); + assert.match(usage.stderr, /usage: runner-release-crate-cache\.mjs/); +}); diff --git a/scripts/runner-release-crate-cache.mjs b/scripts/runner-release-crate-cache.mjs new file mode 100644 index 00000000000..898781a8124 --- /dev/null +++ b/scripts/runner-release-crate-cache.mjs @@ -0,0 +1,210 @@ +#!/usr/bin/env node +// The Runner's own crate is the one thing the shared Rust dependency cache +// leaves out: rust-cache drops workspace crates when it saves, and a fresh +// checkout would invalidate cargo's mtime fingerprints for them anyway. So +// every CI build compiled `paperclip-runner-core` in release mode from +// scratch (28-49s per run on 2026-10-11) although 221 of the 239 master +// commits of the preceding week left its inputs untouched. +// +// This script backs a second, content-addressed cache that holds only that +// crate's release artifacts (about 70 MB). The master writer saves them +// under a key made from the compiler, the checkout root and a hash of every +// input the build reads; a pull request whose merge tree carries +// byte-identical inputs restores them. Cargo judges a workspace crate fresh +// only while no source is newer than its dep-info file, and GitHub's cache +// keeps the artifacts' original mtimes, so `mark-current` touches the +// restored set after the checkout. Any input change selects a different key, +// the restore misses, and cargo compiles exactly as it did before. +// +// Subcommands: +// paths print the cached paths, one per line (repo-relative globs) +// hashed-inputs print the hashFiles() patterns that make up the key +// scope print the compiler/checkout part of the key +// mark-current make a restored artifact set newer than the checkout, or +// discard an incomplete one so cargo rebuilds instead +import { execFileSync } from "node:child_process"; +import { createHash } from "node:crypto"; +import { existsSync, readdirSync, rmSync, statSync, utimesSync, lstatSync } from "node:fs"; +import os from "node:os"; +import path from "node:path"; +import { fileURLToPath } from "node:url"; + +export const RUNNER_PACKAGE = "packages/paperclip-runner"; +const RELEASE_DIR = `${RUNNER_PACKAGE}/runner/target/release`; + +// Everything cargo writes for the crate's release lib and bin units: +// fingerprints (both units share the package-named directory prefix), the +// lib's rlib/rmeta and dep-info, the bin and its dep-info under deps, and the +// uplifted executable the staging step copies into dist/bin. +export const CACHED_PATHS = [ + `${RELEASE_DIR}/.fingerprint/paperclip-runner-core-*`, + `${RELEASE_DIR}/deps/libpaperclip_runner_core-*`, + `${RELEASE_DIR}/deps/paperclip_runner_core-*.d`, + `${RELEASE_DIR}/deps/paperclip_runnerd-*`, + `${RELEASE_DIR}/paperclip-runnerd`, + `${RELEASE_DIR}/paperclip-runnerd.d`, +]; + +// Every file the release build of the crate reads, as hashFiles() patterns: +// the toolchain pin, the manifests and lockfile, the crate sources, and the +// protocol schemas and fixtures the sources include with include_str!. The +// three fixture trees are read only by test builds, but hashing them costs +// nothing and keeps the include_str! guard test simple: every include in the +// crate, whatever its form, must land under one of these patterns. +export const HASHED_INPUTS = [ + `${RUNNER_PACKAGE}/rust-toolchain.toml`, + `${RUNNER_PACKAGE}/runner/Cargo.toml`, + `${RUNNER_PACKAGE}/runner/Cargo.lock`, + `${RUNNER_PACKAGE}/runner/crates/**`, + `${RUNNER_PACKAGE}/protocol/**`, + `${RUNNER_PACKAGE}/test-fixtures/**`, + `${RUNNER_PACKAGE}/test/fixtures/**`, + "tests/runner-e2e/fixtures/**", +]; + +// A restored set is usable only when both units are complete: everything +// cargo's freshness check reads for a unit (the fingerprint hash and the +// dep-info it compares source mtimes against) and every output it checks for +// (the lib's rlib and rmeta, the bin under deps). Measured with cargo 1.97.1: +// without any one of these it recompiles the unit; without the `.json` +// fingerprint records, rustc's `.d` files or the uplifted binary and its `.d` +// it reports the crate fresh and recreates the uplifted copy from deps. +// Anything less than this set is discarded, with the missing file named in +// the job log, so cargo sees a plain miss rather than a half-restored crate. +export const REQUIRED = [ + { label: "the lib fingerprint hash", glob: `${RELEASE_DIR}/.fingerprint/paperclip-runner-core-*/lib-paperclip_runner_core` }, + { label: "the lib dep-info", glob: `${RELEASE_DIR}/.fingerprint/paperclip-runner-core-*/dep-lib-paperclip_runner_core` }, + { label: "the lib rlib", glob: `${RELEASE_DIR}/deps/libpaperclip_runner_core-*.rlib` }, + { label: "the lib rmeta", glob: `${RELEASE_DIR}/deps/libpaperclip_runner_core-*.rmeta` }, + { label: "the bin fingerprint hash", glob: `${RELEASE_DIR}/.fingerprint/paperclip-runner-core-*/bin-paperclip-runnerd` }, + { label: "the bin dep-info", glob: `${RELEASE_DIR}/.fingerprint/paperclip-runner-core-*/dep-bin-paperclip-runnerd` }, + { label: "the bin under deps", glob: `${RELEASE_DIR}/deps/paperclip_runnerd-*`, exclude: /\.d$/ }, +]; + +const SCHEMA = "paperclip.runner-release-crate.v1"; + +function keySafe(value) { + return String(value).replace(/[^A-Za-z0-9._-]+/g, "_"); +} + +/** + * The compiler and checkout part of the cache key. The compiler decides the + * artifacts' metadata hashes; the checkout root is baked into cargo's + * dep-info for the protocol files the sources include by relative path, so an + * entry written under one root is useless under another (the RunsOn fleets + * check out under /home/runner/_work, GitHub-hosted runners under + * /home/runner/work). Hashing the root into the key turns that into a plain + * miss instead of a restore cargo then throws away. + */ +export function computeScope({ workspace = process.env.GITHUB_WORKSPACE ?? process.cwd(), rustcVersion = readRustcVersion() } = {}) { + const root = createHash("sha256").update(path.resolve(workspace)).digest("hex").slice(0, 12); + return `${keySafe(os.platform())}-${keySafe(os.arch())}-${keySafe(rustcVersion)}-${root}`; +} + +function readRustcVersion(env = process.env) { + let output; + try { + output = execFileSync("rustc", ["--version", "--verbose"], { encoding: "utf8", env }); + } catch { + return "no-rustc"; + } + const field = (name) => output.match(new RegExp(`^${name}: (.+)$`, "m"))?.[1]?.trim() ?? "unknown"; + return `${field("release")}-${field("commit-hash").slice(0, 9)}-${field("host")}`; +} + +function expandGlob(root, pattern) { + // Patterns only ever use `*` inside a single path segment. + const segments = pattern.split("/"); + let current = [root]; + for (const segment of segments) { + const next = []; + for (const base of current) { + if (!segment.includes("*")) { + const candidate = path.join(base, segment); + if (existsSync(candidate)) next.push(candidate); + continue; + } + let entries; + try { + entries = readdirSync(base); + } catch { + continue; + } + const matcher = new RegExp(`^${segment.split("*").map((part) => part.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join(".*")}$`); + for (const entry of entries) { + if (matcher.test(entry)) next.push(path.join(base, entry)); + } + } + current = next; + } + return current.sort(); +} + +function walk(target, visit) { + const info = lstatSync(target); + visit(target, info); + if (info.isDirectory()) { + for (const entry of readdirSync(target)) walk(path.join(target, entry), visit); + } +} + +/** + * Touch every restored artifact so cargo's freshness check, which compares + * each source's mtime against the unit's dep-info, sees the checkout as + * older than the build. When the set is incomplete, remove whatever was + * restored and report it: cargo then compiles the crate as on a miss. + */ +export function markCurrent({ root = process.env.GITHUB_WORKSPACE ?? process.cwd(), now = new Date() } = {}) { + const missing = REQUIRED.find(({ glob, exclude }) => expandGlob(root, glob).filter((entry) => !exclude?.test(entry)).length === 0); + const matched = CACHED_PATHS.flatMap((pattern) => expandGlob(root, pattern)); + if (missing) { + for (const target of matched) rmSync(target, { recursive: true, force: true }); + return { schema: SCHEMA, action: "discarded", reason: `incomplete: ${missing.label} is missing`, removed: matched.length }; + } + let files = 0; + let bytes = 0; + const seen = new Set(); + for (const target of matched) { + walk(target, (entry, info) => { + if (info.isSymbolicLink()) return; + utimesSync(entry, now, now); + if (info.isFile()) { + files += 1; + // The uplifted binary is a hard link to the one under deps. + const { ino, size } = statSync(entry); + if (!seen.has(ino)) { + seen.add(ino); + bytes += size; + } + } + }); + } + return { schema: SCHEMA, action: "marked", files, bytes }; +} + +function main(argv) { + const [command] = argv; + switch (command) { + case "paths": + process.stdout.write(`${CACHED_PATHS.join("\n")}\n`); + return 0; + case "hashed-inputs": + process.stdout.write(`${HASHED_INPUTS.join("\n")}\n`); + return 0; + case "scope": + process.stdout.write(`${computeScope()}\n`); + return 0; + case "mark-current": { + const result = markCurrent(); + process.stdout.write(`${JSON.stringify(result)}\n`); + return 0; + } + default: + process.stderr.write("usage: runner-release-crate-cache.mjs \n"); + return 2; + } +} + +if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) { + process.exitCode = main(process.argv.slice(2)); +}