Skip to content

Scratch pads: warm caches that carry over between jobs

Kata issue jyck. Brainstormed with Gavin on 2026-11-25; approved by him the same day. Evidence: experiments 25 and 26 in docs/experiments/. Reviewed by a fresh builder and by Carbon; their findings are folded in.

Why

Every job starts with empty caches. Running allmyram's own unit tests as a job took 114 s, almost all of it downloading Go modules and compiling dependencies cold (kata h94t); meristem's suite spent ~10 s of 16 s downloading the same 55 packages through the egress proxy (jyck); a cold build of Transpire's webhooks service with its tests takes 34–37 s. Until a job on the hoard can start warm, many suites are slower there than on the laptop, which defeats the point of moving them.

The fundamentals spec already settled the shape (Gavin): named scratch pads. A job may use named pad X, with a soft maximum size; pads persist on a runner between jobs; the job's creator says which pads a job uses and whether it may write them; a soft limit means a warning and a clean-up candidate, never a failed job; each runner limits the disk its pads use.

What this document adds: how concurrent jobs share a pad safely, how pads are scoped, and how they are stored.

What the experiments found

  • File locks don't cross gVisor sandboxes (experiment 25). Two jobs mounting one host directory see each other's writes within milliseconds, but flock() and POSIX locks live in each sandbox's own kernel: two jobs each get an exclusive lock on the same file at once, with no error. cargo, uv and git protect their caches with locks, so two jobs writing one directory could corrupt it silently.
  • Copy on first write isn't possible without root (experiment 26, part A, and gVisor's source). A rootless kernel overlay mounts fine on ZFS, but the kernel won't bind-mount it into containerd's namespace (runc: EINVAL; runsc fails in its gofer); an overlay inside the sandbox copies up byte by byte, and gVisor answers FICLONE with EOPNOTSUPP.
  • An eager copy-on-write clone is cheap (experiment 26, parts B and C). With 16 workers, a real 1.9 GB cargo target/ (3,868 entries) clones in 0.10–0.14 s and Go's 942 MB build cache in 0.16–0.18 s; every block is shared; cargo recompiles nothing from a clone. A job that never touches its pad wastes well under half a second.

So: no two jobs ever write the same directory. Each job gets its own copy-on-write clone of each pad, and the clone becomes the pad's next version when the job ends. Gavin's position, that sharing is the job creator's choice "the same thing that happens on a normal system", holds for what a job does with its own clone; gVisor takes away the locking a normal system would give two jobs on one directory, so the design doesn't put two jobs on one directory.

Goals

  • A job can name pads and where each is mounted in its main container, and starts with their contents from earlier jobs.
  • Any number of concurrent jobs may use the same pad; none waits for another, and none can corrupt another's view or the pad.
  • A branch gets back its own last version of a repo pad (a branch's cargo target/), and a new branch starts from the repo's most recent version (Gavin: "a branch can likely safely share a pile of files").
  • Starting a pad is cheap: a copy-on-write clone, not a byte copy, where the filesystem can clone.
  • Pads work wherever the runner runs (Gavin: "if cloning isn't working we should still you know WORK"). On a filesystem that can't clone (ext4), pads are plain copies: slower and full-size, and said so.
  • Disk use stays bounded.

Non-goals

  • Coordinating concurrent writers or merging their changes. Each job's changes are its own; the last to finish wins.
  • Pads for service containers. Only the job's main container mounts pads.
  • Pads on macOS runners, for now (see "macOS").
  • Moving pads between runners, or backing them up. A pad is a cache: losing one costs a cold start, nothing more.
  • A job spec file format (kata sqg7). Pads are allmyram run flags until that exists; the job carries them as data the format can later fill.

Anti-goals

  • A corrupt cache passed on silently by two jobs writing one directory.
  • A pad filling the runner's disk.
  • A job that silently runs cold because its pad failed. If a requested pad can't be prepared, the run fails to start and says why.
  • Root. Nothing about pads may need root at run time.

Concepts

  • Pad: a named directory tree on a runner. Names are 1 to 63 characters of [a-z0-9-], starting and ending with a letter or digit.
  • Scope (Gavin: "two sets, default is by repo, and then some are global but you have to ask for global"):
  • repo pad (the default): belongs to one repo, by its repo key (the root commit, as runners already key repos). target in repo A and target in repo B are different pads.
  • global pad: shared by every repo on that runner, requested explicitly. For content-keyed caches that are the same across projects: uv's cache, cargo's registry, Go's module cache. With many concurrent jobs, a global pad's latest version is whichever job finished last, so a job's additions can be lost to another's publish; a repo pad (or a global pad warmed once, then used read-only) avoids that.
  • Version: one complete state of a pad, as some job left it. A repo pad keeps a latest version per branch; a global pad keeps one latest version.
  • Branch: the client's current branch name when it submits (from git symbolic-ref --short HEAD); empty for a detached HEAD.

How a job uses pads

allmyram run --runner smaug --image ... \
  --pad target:/work/target \
  --global-pad uv:/cache/uv \
  --global-pad cargo-registry:/cache/cargo:ro \
  --env CARGO_TARGET_DIR=/work/target --env UV_CACHE_DIR=/cache/uv -- cargo test

--pad NAME:PATH[:OPTION...] and --global-pad (same form) are repeatable. Options:

  • ro: mounted read-only; nothing is published.
  • fresh: start from an empty pad instead of the latest version, and publish as usual. This is how a job resets a pad that has gone bad (a half-written file after an OOM kill, a target/ built by another image) without access to the runner.
  • max=SIZE: the soft maximum (sizes as --memory takes them); without it the pad has no soft maximum.

Pointing tools at the path (UV_CACHE_DIR, CARGO_TARGET_DIR, GOMODCACHE, CARGO_HOME) is the job's business; allmyram sets no environment for pads.

Validation (by the client for good messages, by the runner, and by the warden for paths, since it trusts nothing from the runner):

  • At most a small fixed number of pads per job (such as 8).
  • A name appears once per job, across both scopes.
  • PATH is absolute and clean; it is not /, /tmp (the job's default HOME), /workspace or anything under it or above it, and not under /proc, /sys or /dev; no two pads' paths are equal or nested.
  • The branch is at most 255 bytes with no NUL or control characters.

The pad list is part of job.Job, so it reaches the warden with the rest of the job. The submit request also carries the branch. The repo key and the branch come from the client and are trusted: a runner has one owner, so any client the owner admits may use any of that owner's pads.

Lifecycle of a pad in one run

  1. Pick the source. With fresh: empty. For a repo pad: the latest version for this branch; if none, the repo's most recently published version on any named branch; if none, empty. A detached HEAD (empty branch) starts from the repo's most recent version and publishes to its own detached slot, which never seeds a named branch. For a global pad: its latest version, or empty.
  2. Clone. The runner clones the source into the run's staging area, in a directory beside the workspace (<staging>/<run ID>.pads/<name>, so pads never appear inside /workspace), before the warden's Create. While it clones, the runner holds the source version in memory so clean-up won't delete it; nothing about an in-flight clone needs to survive a restart, because Reconcile removes a dead run's staged directories (<run ID> and <run ID>.pads alike). A Cancel or Close during cloning interrupts it, as snapshot extraction is today. Read-only pads are cloned too (about 0.1 s for a large pad): mounting a version in place would make the warden mount a path the runner controls, which it must not trust.
  3. Mount. The warden moves each staged pad directory separately to <workspace root>/<run ID>.pad-<name>, an entry directly in its own root beside the workspace, undoing in reverse if a later step of Create fails, as it does for the workspace. It checks with Lstat that each is a real directory, not a symlink, then bind-mounts it at its path in the main container, read-only for ro. Each mount source must be an entry in the warden's own root: the runner owns the directories it staged and can reach them by path, so a pad left inside a runner-owned .pads directory could be swapped for a symlink between the warden's check and containerd's mount (found in Task 4's review, 2026-11-25).
  4. Run. The job reads and writes its clone. No other job can see it.
  5. Hand back. At Stop the warden moves the workspace and each <run ID>.pad-<name> it finds in its root back to staging, each independently and only if present, so a retried Stop never strands one. A job can change its pad directory's mode or group and block the move, as it can its /workspace; the runner, which owns them, restores both and asks again, as it already does for the workspace. If that fails, the run's teardown is pending as for a workspace today, and nothing is published until it succeeds.
  6. Publish (writable pads only, per the table below). The runner walks the clone once, in a staging area on the pads filesystem:
  7. drops sockets, FIFOs and device files (they carry nothing across sandboxes and would trip the next clone);
  8. gives the owner read access to every file and read and search access to every directory (a job can leave 0000 entries), keeping other mode bits, read-only ones included;
  9. measures the apparent size, counting each inode once.

Then it flushes the filesystem with one syncfs() (measured at 0.01 s; so a crash just after publishing can't leave the latest version half written), moves the clone into the pad's versions, and switches the branch's (or the global pad's) pointer to it with an atomic rename. The replaced version is deleted once no clone is being made from it.

When a run publishes (Gavin: "always, yes, except cancelled"):

How the run ended Published
Completed, Error (non-zero exit), OOMKilled yes
TimedOut yes (Gavin: the IO it did may be what lets the next run finish in time)
Cancelled no
StartFailed no (nothing ran)
RunnerRestarted no (my call: publishing would mean the runner finishing an old run's work during Reconcile; it is rare and costs one cache update)

Two runs that finish in turn both publish; the later one is the latest. A run never sees another's changes until it publishes and a later run clones.

Cloning

  • The runner clones with github.com/otiai10/copy (MIT, widely used), 16 workers, keeping modes and modification times (cargo decides what to rebuild by mtimes). Its file copy is Go's io.Copy into an *os.File, which on Linux is copy_file_range; ZFS (2.2+ with block cloning), XFS and btrfs turn that into a clone, and other filesystems copy bytes. Symlinks are copied as links, never followed; directories made read-only (Go's module cache) are filled before their mode is set; hard links become separate files that share blocks.
  • The cloning check: at start the runner clones a small flushed file between two directories in its pads area with FICLONE, which fails rather than copying. If it works, the runner logs that pads are cloned; if not, it logs once, with the filesystem's type, that pads will be plain copies (slower, full size), and each run that uses a pad gets a warning line saying so. Either way pads work.
  • Same mount: publishing renames a clone from staging into the pads area, and rename fails across mounts. systemd's ReadWritePaths= makes each listed path its own bind mount, so allmyram-runner.service lists /var/lib/allmyram once (as the warden's unit already does) instead of runner, staging and warden/workspaces separately; file permissions keep confining the runner as they do today. Pads live under /var/lib/allmyram/runner/pads.

Storage and limits

  • Layout is the builder's, along the lines of pads/repo/<repo key>/<name>/versions/<id> plus a pointer per branch (branch names encoded into one safe path component) and one for the detached slot, and pads/global/<name>/.... A publish in progress lives in a temporary directory that Reconcile removes if the runner died mid-publish.
  • Soft maximum: a published version over its pad's soft maximum is kept, logged, reported to the client, and cleaned up before versions under their maximum.
  • The disk limit (Gavin's earlier ruling: "a hard limit on the total disk all its pads may use"). With copy-on-write, versions share most of their blocks, and how much disk one version alone holds can't be read cheaply; summing apparent sizes counts shared blocks once per version (ten branches of one 8 GB target/ would count as 80 GB while using about 9). So the limit is on the filesystem's real free space: after each publish, if the pads filesystem's free space (statfs) is below the runner's floor, the runner deletes versions, those over their soft maximum first, then least recently used (cloned or published), until their apparent sizes add up to the shortfall. It reads free space once per check: ZFS shows freed space only seconds after a delete (measured on smaug, 2026-11-25: not even after syncfs, ~6 s later), so re-reading after each delete would delete everything. Apparent sizes overstate what a version alone holds, so a check may free less than the shortfall; the next publish's check catches up. The floor is the data directory's free-space floor (kata xtb5): the smaller of 10% of the filesystem and 40 GB, overridable in the runner config. (Gavin confirmed, 2026-11-25: this replaces a budget in bytes with "pads may use the disk down to the floor".)
  • What clean-up never deletes: a version being cloned from right now, and the version the current publish just made (so a publish can't wipe both the old version and the new one). If the versions that can't be deleted keep free space under the floor, the runner logs that pads are over budget.
  • Deleting a version removes every pointer to it in the same step, so the next run falls back as in step 1 instead of failing on a missing version. Deletion first makes read-only directories writable (Go's module cache), as the runner already does for workspaces.
  • On a filesystem shared with other things (a desktop's /var on btrfs), syncfs() flushes everything on it; the deployment guide (kata m6tj) recommends a dataset or subvolume of allmyram's own. A ZFS quota on that dataset counts every clone at full size (experiment 26), so the floor, not a quota, should bound pads.

How the client hears about pads

The runner records each pad's result in the run record: published or not, its apparent size, over its soft maximum, copied rather than cloned, or why publishing failed. For a run with writable pads, attach waits after the output for those results (a run's own exit doesn't wait for teardown today, so this is new), bounded to about 30 s; its final line carries them and the client prints a warning line for anything worth saying. If the wait runs out, the final line says publishing is still under way. status shows the results too, for a client that wasn't attached at the end. The wait covers the Stop, the publish walk and the flush, measured at well under a second for a 1.9 GB target/.

Failure reporting

  • A pad the runner can't prepare (out of space, a read error) fails the run to start, with a reason naming the pad.
  • A publish that fails is logged and reported as a warning; the run's own outcome is unchanged.
  • Plain copies instead of clones are warned about, once per runner start in the log and once per run to the client.

Where it touches the code

  • job: the pad list, with validation.
  • client and cmd/allmyram: --pad and --global-pad with ro, fresh and max=; the branch in the submit request; pad warnings at the end of a run.
  • runner: the cloning check; choosing sources; cloning into staging; publishing (sanitizing walk, flush, pointers); clean-up against the free-space floor; Reconcile removing <run ID>.pads and unfinished publishes; recording pad results; attach waiting for them.
  • warden: moving <run ID>.pads in and out with the workspace; the Lstat checks; mounting each pad; validating names and paths.
  • deploy/linux: allmyram-runner.service with one ReadWritePaths entry for /var/lib/allmyram. The data directory should be on ZFS, btrfs or XFS for clones (on smaug, minipool/allmyram, done 2026-11-25 with deploy/linux/move-to-zfs.sh).

macOS

Not part of this work; Mac runners refuse pads until a follow-up. What it starts from (the Plan 2 session, 2026-11-25): the VM is Garden Linux; staging is on the virtiofs share (APFS underneath, which FICLONE can't reach), and the warden copies the staged workspace onto the VM's own disk at Create, holding CAP_CHOWN, CAP_FOWNER and CAP_DAC_OVERRIDE inside the VM. So on a Mac, pads live on the VM's disk beside the warden's workspace root, and the warden in the VM clones them. Gavin's ruling for Plan 2 (its Task 11b): the VM's job data gets its own Lima disk, XFS, created once and mounted at /var/lib/allmyram/warden; it survives the instance being recreated, so pads on a Mac persist too.

Approaches

How concurrent jobs share a pad:

  • Plain sharing (rejected). Jobs mount one directory and share writes. Experiment 25: gVisor silently disables the locking cargo, uv and git rely on, so concurrent writers can corrupt a pad.
  • One writer at a time (rejected). Safe, but a job holds its pad for its whole run, so ten agents on one uv cache would run one after another.
  • A pool of copies (superseded). A full copy per concurrent job, started cold when all are busy.
  • Copy-on-write clone per job, published at the end (chosen). Every job starts warm, no two jobs share a writable directory, clones share blocks (Gavin: "then we need the file system to be COW").

How to make the clone:

  • A parallel copy that the filesystem turns into clones (chosen). otiai10/copy with 16 workers: 3–4 times faster than cp, full block sharing, and plain copies where the filesystem can't clone. Gavin asked for a small, well-used library over our own code; a hand-written FICLONE walk measured slightly slower and was dropped.
  • The filesystem's own snapshots (a note for later). btrfs subvolume snapshots and ZFS clones copy any tree in constant time ("so so so fast on ZFS"), but each filesystem needs its own code, XFS has none, mounting a ZFS clone needs root ("the root requirement is very bad"), and deleting a btrfs snapshot needs root unless the filesystem is mounted with user_subvol_rm_allowed. Not needed at 0.1 s per large pad.
  • Copy on first write (what Gavin actually wants; out without root). An overlay copies a file only when a job writes it, so an untouched pad costs nothing. Experiment 26 part A: a rootless kernel overlay can't be handed to containerd; an overlay inside gVisor copies bytes. With root, it would take a mount helper and stacked layers (merging on every publish would cost a full clone anyway), a layer graph like containerd's snapshotter. Not worth it at 0.1 s per clone.

Left to the builder

  • The on-disk layout, the pointer format and how versions are named.
  • The default soft maximum and the maximum number of pads per job.
  • How clean-up and publishing are serialized: a lock held only for choosing sources, switching pointers and choosing what to delete, never across cloning, walking, flushing or deleting.
  • How cloning is tested: clones need ZFS, btrfs or XFS, which the laptop's test directories aren't, but the library copies bytes there, so versioning, publishing and clean-up are testable everywhere; the cloning check and real clones are tested in the Linux integration suite on smaug.