/** * THE SCCACHE CACHE IS NEVER DELETED UNDER A LIVE SERVER. * * `scripts/disk-guard.sh`'s deep tier used to reclaim the compile cache with a * bare `rm -rf ~/.cache/cottondb/rust/sccache`, and the sccache SERVER kept * running across it. The server opens its cache directory once, at start, and * never recreates one: every write after that delete fails, silently, for as * long as that server lives — and `SCCACHE_IDLE_TIMEOUT=0`, which * `scripts/ci/setup-sccache.sh` sets so a job's `--show-stats` reports its own * work, means it lives until the Mac reboots. * * Measured on one Mac, 2026-09-22, some days after one such reclaim: 29,679 * cache WRITE errors, 78,703 Rust cache misses against 3,508 hits — a 4.32% hit * rate on the crates a compile cache exists to skip. Every build since had paid * the wrapper's overhead and got nothing back, and nothing said so: sccache * reports a write failure as a counter, not an error, so the builds stayed * green and only got slower. * * Two halves, and this file holds both: * - the reclaim STOPS THE SERVER first, then empties, then RECREATES the * directory (`scripts/reclaim-sccache.sh`, run here for real against a stub * `sccache` that records what it was asked and what it could see); * - and the setup `mkdir -p`s the directory before starting a server against * it, so the other way into the same failure — a path that never existed — * is closed too. */import { spawnSync } from"node:child_process";import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from"node:fs";import { tmpdir } from"node:os";import { join, resolve } from"node:path";import { afterEach, describe, expect, it } from"vitest";const ROOT = resolve(__dirname, "../..");const SCRIPT = join(ROOT, "scripts/reclaim-sccache.sh");let scratch = "";afterEach(() => {if (scratch) rmSync(scratch, { recursive: true, force: true }); scratch = "";});typeFixture = { cache: string; calls: string; seen: string; bin: string };/** * A cache directory with a file in it, and a stub `sccache` that writes down * BOTH the arguments it was given and whether the cache was still there when it * ran. The second is what makes the ordering testable at all: a stop recorded * after the directory is gone is the bug, not the fix. */function makeFixture(): Fixture { scratch = mkdtempSync(join(tmpdir(), "reclaim-sccache-"));const cache = join(scratch, "sccache"); mkdirSync(join(cache, "s"), { recursive: true }); writeFileSync(join(cache, "s", "object"), "a cached compile\n");const bin = join(scratch, "bin"); mkdirSync(bin, { recursive: true });const calls = join(scratch, "calls");const seen = join(scratch, "seen");const stub = join(bin, "sccache"); writeFileSync( stub, ["#!/usr/bin/env bash",`printf '%s\\n' "$*" >> '${calls}'`,`if [ -d '${cache}/s' ]; then echo present >> '${seen}'; else echo gone >> '${seen}'; fi`,"exit 0", ].join("\n") + "\n", ); chmodSync(stub, 0o755);// `pgrep -x sccache` answers from a fake process table — FAKE_SCCACHE_ALIVE=1// is a server (or client) still running — so a real sccache on the machine// running the suite never decides a case. writeFileSync( join(bin, "pgrep"),'#!/usr/bin/env bash\n[ "${FAKE_SCCACHE_ALIVE:-}" = "1" ] && [ "$*" = "-x sccache" ] && { echo 4242; exit 0; }\nexit 1\n', { mode: 0o755 }, );return { cache, calls, seen, bin };}function run(f: Fixture, args: string[] = [], env: Record<string, string> = {}) {return spawnSync("bash", [SCRIPT, "--dir", f.cache, ...args], { encoding: "utf8", timeout: 20000,// HOME is redirected so the script's first candidate — the pinned copy at// `~/.cache/cottondb/rust/bin/sccache` — cannot be a real one on this Mac. env: { ...process.env, HOME: scratch, PATH: `${f.bin}:${process.env.PATH ?? ""}`, ...env }, });}describe("scripts/reclaim-sccache.sh", () => { it("stops the server BEFORE the cache is gone, and leaves an empty directory behind", () => {const f = makeFixture();const r = run(f); expect(r.status, r.stderr).toBe(0); expect(readFileSync(f.calls, "utf8")).toContain("--stop-server"); expect( readFileSync(f.seen, "utf8").trim(),"the server was stopped after the delete — that is the failure, not the fix", ).toBe("present"); expect(existsSync(f.cache), "a server started against a missing directory writes nothing").toBe(true); expect(readdirSync(f.cache)).toEqual([]); }); it("empties a cache even when no sccache binary exists — there is then nothing to break", () => {const f = makeFixture(); rmSync(join(f.bin, "sccache"));const r = run(f); expect(r.status, r.stderr).toBe(0); expect(existsSync(f.cache)).toBe(true); expect(readdirSync(f.cache)).toEqual([]); });// RULE: The sccache directory is never removed while any sccache process — the stopped server, another server, a client mid-compile — is still running. it("leaves the cache alone when an sccache is still running after the stop", () => {const f = makeFixture();const r = run(f, [], { FAKE_SCCACHE_ALIVE: "1" }); expect(r.status, r.stderr).toBe(1); expect(readFileSync(f.calls, "utf8")).toContain("--stop-server"); expect(r.stderr).toMatch(/still running .* leaving .* alone/); expect(existsSync(join(f.cache, "s", "object")), "the cache a live server writes into survives").toBe(true); }); it("--dry-run takes nothing", () => {const f = makeFixture();const r = run(f, ["--dry-run"]); expect(r.status, r.stderr).toBe(0); expect(existsSync(join(f.cache, "s", "object"))).toBe(true); expect(existsSync(f.calls), "a dry run stops no server either").toBe(false); }); it("refuses a directory that is not one — the delete is `rm -rf`", () => {const f = makeFixture(); expect(spawnSync("bash", [SCRIPT, "--dir", "/"], { encoding: "utf8" }).status).toBe(2); expect(spawnSync("bash", [SCRIPT, "--dir", ""], { encoding: "utf8" }).status).toBe(2); expect(existsSync(join(f.cache, "s", "object"))).toBe(true); });});describe("nothing else deletes the cache directly", () => {const guard = readFileSync(join(ROOT, "scripts/disk-guard.sh"), "utf8");const setup = readFileSync(join(ROOT, "scripts/ci/setup-sccache.sh"), "utf8"); it("disk-guard reclaims through the script rather than removing the path itself", () => {const code = guard.split("\n").filter((line) => !line.trimStart().startsWith("#")); expect( code.filter((line) => /rm -rf.*sccache/.test(line)),"a bare rm -rf of the sccache directory is the 29,679-write-error bug", ).toEqual([]); expect(guard).toContain("scripts/reclaim-sccache.sh"); }); it("the setup creates the directory before a client can daemonise a server against it", () => {// Line numbers over the CODE, not the file: this script explains itself at// length and `--start-server` appears in its prose long before it runs.const code = setup.split("\n").map((line) => (line.trimStart().startsWith("#") ? "" : line));const made = code.findIndex((line) => line.includes('mkdir -p "$SCCACHE_DIR"'));const started = code.findIndex((line) => line.includes("--start-server")); expect(made, "scripts/ci/setup-sccache.sh: no mkdir -p of SCCACHE_DIR").toBeGreaterThan(-1); expect(started).toBeGreaterThan(-1); expect(made, "the directory must exist before the server starts").toBeLessThan(started); }); it("and so does rust-verify, which starts its own server through the same binary", () => {const verify = readFileSync(join(ROOT, "scripts/rust-verify.sh"), "utf8"); expect(verify).toMatch(/mkdir -p "\$SCCACHE_DIR"/); });});/// RULE: Even a compile cache is retained when it holds evidence; pressure cannot authorize deleting review captures.it("keeps evidence without stopping the cache server", () => {const f = makeFixture(); writeFileSync(join(f.cache, ".cotton-evidence"), "");const result = run(f); expect(result.status, result.stderr).toBe(0); expect(existsSync(join(f.cache, "s/object"))).toBe(true); expect(existsSync(f.calls)).toBe(false); expect(result.stdout).toContain("EVIDENCE marker");});