Files
gitdan-actions/scripts/cache-lock.sh
T
claudeandClaude Opus 5 248af3061e feat(cargo-cache): hardlink-clone a per-ref Cargo cache from a published snapshot
Replaces the phase-0 resolution probe with the real actions, merging the two
independent per-branch Cargo cache implementations on this forge into the
design neither of them had.

## The merge

- zemyna seeds a PR branch by `cp -al` hardlink clone (near-free: cost scales
  with inode count, not bytes) from the base branch's LIVE target dir — a
  torn read waiting for a second job slot (its own #911).
- emowheel seeds from a PUBLISHED IMMUTABLE SNAPSHOT (no race by
  construction) but with `cp -a`, duplicating ~35 GB per branch.

This ships hardlink-clone FROM a published snapshot: zemyna's cost profile,
emowheel's soundness, and #911 closed structurally rather than by the runner
happening to have one execution slot.

## The bug both implementations have

A build inside a `cp -al` clone DOES mutate the directory it was cloned from.
Cargo replaces real artifacts, but writes its metadata — and build scripts
write their OUT_DIR — with a plain truncating write, straight through the
shared inode. Measured set: `.fingerprint/<unit>/dep-<target>` (under
CARGO_UNSTABLE_CHECKSUM_FRESHNESS), `build/<pkg>/{output,root-output,out/**}`,
`deps/*.d` and `<profile>/*.d`.

The checksum-freshness case is a wrong answer, not a slow build: a PR clone
rewrites the base's dep-info to describe the PR's sources while the base's
cache still holds the artifact built from the base's; once the PR merges, the
base's next run finds the checksums match, reports `Fresh`, and links a binary
built from the pre-merge code. Reproduced end to end.

Fix: hardlink the artifacts (the GB), real-copy the metadata (the MB) — about
3.7% of a 6.9 GB Bevy target dir, against 100% for a full copy.

## Contents

- `cargo-cache/action.yml` — consume: resolve keys, seed from the base's
  snapshot via staging + one atomic rename, strip Cargo lock files, unshare
  the mutable paths, restore mtimes from git history, lock, prune.
- `cargo-cache-publish/action.yml` — publish: record the build watermark,
  atomically republish the snapshot on a protected branch, release the lock
  (`mode: release-lock` for the `if: always()` step).
- `scripts/` — all logic, so it is testable standalone; the YAML is wiring.
- `scripts/*selftest.sh` + `selftest.sh` — five suites, 63 assertions, every
  fix paired with a control that reproduces the bug. All green locally.

Eviction merges emowheel's liveness pass (dead branches pruned
unconditionally, not gated on disk pressure) with LRU-under-pressure, but
inverts the order within the pressure pass: `target-*` before `snapshot-*`,
because a snapshot is hardlinked to everything cloned from it, so evicting one
frees almost no real bytes while costing every future PR its warm start.

restore-mtimes.sh is ported from emowheel (the watermark variant, which closes
the merge hazard zemyna's copy still has) with its provenance de-projectised.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Sqh2vscfzisk83VuPVQX9L
2026-08-23 14:18:01 -05:00

37 lines
1.4 KiB
Bash
Executable File

#!/usr/bin/env bash
# Marks a cache directory as held open by a running job, so ANY job's prune
# pass (including this run's own) skips it.
#
# Usage: cache-lock.sh acquire|release <dir> <lock-id>
#
# Without this, a directory's only protection from a concurrently running
# job's eviction pass is "it happens to also be that job's own target dir",
# which is true for the job that owns it and false for everyone else. The
# marker is per-job (not just per-run) so two jobs sharing one cache key each
# hold an independent lock rather than one clobbering the other's.
#
# A lock is a timestamp file, not a real mutex: prune-cache.sh honours it only
# until STALE_LOCK_SECONDS, after which it is treated as abandoned by a job
# the runner killed before it reached its own release step. Honouring a lock
# forever would let one crashed job pin a directory permanently.
set -euo pipefail
MODE="${1:?usage: cache-lock.sh acquire|release <dir> <lock-id>}"
DIR="${2:?}"; ID="${3:?}"
case "$MODE" in
acquire)
mkdir -p "$DIR"
date +%s > "$DIR/.ci-lock-${ID}"
echo "lock: acquired .ci-lock-${ID} on $(basename "$DIR")"
;;
release)
rm -f "$DIR/.ci-lock-${ID}" 2>/dev/null || true
echo "lock: released .ci-lock-${ID} on $(basename "$DIR")"
;;
*)
echo "::error::cache-lock.sh: unknown mode '$MODE' (expected acquire or release)" >&2
exit 1
;;
esac