/** * A WALK GOES BEFORE OTHER LOCAL RUNS. * * Measured 27 Oct 2026 on one Mac: the coordinator's walk build waited 40 * minutes on `/tmp/tinmen-build.lock` behind three subagents' cargo builds, * then compiled in about four — every one of them `local`, and a freed lock * went to whoever polled first. `scripts/with-build-lock.sh` now ranks a * caller that says `BUILD_LOCK_CLASS=walk` between `ci` and `local`: * * - a waiting walk files a claim in `.walk-waiting` and is next in line, * ahead of local runs that queued before it; * - a local run never takes a free lock while a walk claim is live; * - a local HOLDER is never stopped for a walk — it finishes first; * - a walk still waits behind a check job's claim; * - a HOLDING walk is never stopped for a check job (only for a release): the * check job waits and goes next. * * Every case runs the real script against a scratch lock, with the intervals * shortened through the script's own seams — nothing touches the machine's lock. */ import { spawn, spawnSync, type ChildProcess } from "node:child_process"; import { existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join, resolve } from "node:path"; import { afterEach, describe, expect, it } from "vitest"; import { publishBuildPeaks, publishDiskReading } from "./helpers/disk-reading"; const ROOT = resolve(__dirname, "../.."); const SCRIPT = join(ROOT, "scripts/with-build-lock.sh"); const CALLER_CLASS = join(ROOT, "scripts/lib/caller-class.sh"); let scratch = ""; const children: ChildProcess[] = []; afterEach(() => { for (const child of children.splice(0)) { if (child.exitCode === null) child.kill("SIGKILL"); } if (scratch) rmSync(scratch, { recursive: true, force: true }); scratch = ""; }); function paths() { scratch ||= mkdtempSync(join(tmpdir(), "build-lock-walk-first-")); return { lock: join(scratch, "lock"), claims: join(scratch, "lock.ci-waiting"), walkClaims: join(scratch, "lock.walk-waiting"), log: join(scratch, "wait.log"), trace: join(scratch, "trace"), }; } function env(cls: "ci" | "walk" | "local", label: string): NodeJS.ProcessEnv { const p = paths(); // A scratch HOME with a fresh device reading: on a Mac the wrapper admits // through the device policy, which reads it there — never the person's HOME. const home = join(scratch, "home"); publishDiskReading(home); publishBuildPeaks(home); return { ...process.env, HOME: home, // One slot: the order of a queue is what is under test, not sharing. BUILD_LOCK_MEMORY_BYTES: "17179869184", BUILD_LOCK_DIR: p.lock, BUILD_LOCK_LOG: p.log, BUILD_LOCK_LABEL: label, BUILD_LOCK_CLASS: cls, BUILD_LOCK_POLL_SECONDS: "1", BUILD_LOCK_CI_POLL_SECONDS: "1", BUILD_LOCK_YIELD_POLL_SECONDS: "1", BUILD_LOCK_YIELD_GRACE_SECONDS: "2", BUILD_LOCK_YIELD: "", TINMEN_BUILD_LOCK_HELD: "", TINMEN_BUILD_LOCK_DIR: "", // A scratch cargo target, not named shared, as build-lock-ci-first.test.ts says: // with none, a worktree's generated .cargo/config.toml hands the wrapper the // machine's real shared target, and every command here waits on that target's // cargo lock behind whatever cargo build is running on the Mac. CARGO_TARGET_DIR: join(scratch, "target"), }; } type Done = { status: number | null; stderr: string }; function start(command: string, e: NodeJS.ProcessEnv): { done: Promise; stderr: () => string } { const child = spawn("bash", [SCRIPT, "bash", "-c", command], { env: e, stdio: ["ignore", "pipe", "pipe"] }); children.push(child); let stderr = ""; child.stderr!.on("data", (b) => (stderr += String(b))); child.stdout!.resume(); const done = new Promise((ok) => child.on("close", (status) => ok({ status, stderr }))); return { done, stderr: () => stderr }; } async function until(what: string, test: () => boolean, ms = 20_000): Promise { const end = Date.now() + ms; while (!test()) { if (Date.now() > end) throw new Error(`timed out waiting for: ${what}`); await new Promise((r) => setTimeout(r, 100)); } } const trace = () => (existsSync(paths().trace) ? readFileSync(paths().trace, "utf8").trim().split("\n") : []); const listed = (dir: string) => (existsSync(dir) ? readdirSync(dir) : []); describe("with-build-lock.sh — a walk goes before other local runs", () => { // RULE: A waiting walk is served before local runs that queued earlier, and a local run that holds the lock is never stopped for it. it("serves a walk before a subagent that queued first, without stopping the holder", async () => { const p = paths(); const finish = join(scratch, "finish"); const holder = start( `echo holder-start >> ${p.trace}; while [ ! -f ${finish} ]; do sleep 0.1; done; echo holder-end >> ${p.trace}`, env("local", "subagent-cargo-1"), ); await until("the holder holds the lock", () => trace().includes("holder-start")); const subagent = start(`echo subagent >> ${p.trace}`, env("local", "subagent-cargo-2")); await until("the subagent is waiting", () => subagent.stderr().includes("waiting for 'subagent-cargo-1'")); const walk = start(`echo walk >> ${p.trace}`, env("walk", "walk-sim")); await until("the walk has filed its claim", () => listed(p.walkClaims).length === 1); // The wrapper files the claim, THEN reads the holder and says it waits; the // line reaches this process later still, through a pipe. Asserting the // moment the claim appears raced both (red on the loaded Linux box, run // 7646), so the line is waited for — while the holder still holds and the // walk's claim is still filed. await until("the walk says it waits claimed as a walk", () => walk.stderr().includes("claimed as a walk")); expect(listed(p.walkClaims)).toHaveLength(1); expect(trace()).toEqual(["holder-start"]); writeFileSync(finish, ""); const [h, s, w] = await Promise.all([holder.done, subagent.done, walk.done]); for (const r of [h, s, w]) expect(r.status, r.stderr).toBe(0); expect(trace()).toEqual(["holder-start", "holder-end", "walk", "subagent"]); expect(h.stderr).not.toContain("YIELDED"); expect(listed(p.walkClaims)).toEqual([]); expect(existsSync(p.lock)).toBe(false); }, 60_000); // RULE: A local run never takes a free build lock while a walk's claim is live. it("a local run never takes a free lock while a walk claim is live", async () => { const p = paths(); mkdirSync(p.walkClaims, { recursive: true }); // This test's own process stands in for a waiting walk. writeFileSync(join(p.walkClaims, String(process.pid)), "walk-sim\n"); const local = start(`echo local >> ${p.trace}`, env("local", "subagent-cargo")); await new Promise((r) => setTimeout(r, 2500)); expect(trace(), "a local run went ahead of a waiting walk").toEqual([]); rmSync(join(p.walkClaims, String(process.pid))); const l = await local.done; expect(l.status, l.stderr).toBe(0); expect(trace()).toEqual(["local"]); }, 30_000); // RULE: A walk waits behind a check job's claim, keeping its own claim filed so no local run slips in between. it("a walk still waits behind a check job's claim, and keeps its own filed", async () => { const p = paths(); mkdirSync(p.claims, { recursive: true }); writeFileSync(join(p.claims, String(process.pid)), "ci-swift-mac\n"); const walk = start(`echo walk >> ${p.trace}`, env("walk", "walk-sim")); await new Promise((r) => setTimeout(r, 2500)); expect(trace(), "a walk went ahead of a waiting check job").toEqual([]); expect(listed(p.walkClaims)).toHaveLength(1); rmSync(join(p.claims, String(process.pid))); const w = await walk.done; expect(w.status, w.stderr).toBe(0); expect(w.stderr).toContain("check job 'ci-swift-mac' goes first"); expect(trace()).toEqual(["walk"]); expect(listed(p.walkClaims)).toEqual([]); }, 30_000); // RULE: A check job's claim never stops a walk that holds the lock; the check job waits and goes when the walk ends (8 Nov 2026: one Mac's half-hour swift-compile tick restarted a lane's walk build from the top three times in an hour). it("a check job's claim waits behind a holding walk instead of stopping it", async () => { const p = paths(); const finish = join(scratch, "finish"); const walk = start( `echo walk-start >> ${p.trace}; while [ ! -f ${finish} ]; do sleep 0.1; done; echo walk-end >> ${p.trace}`, env("walk", "walk-sim"), ); await until("the walk holds the lock", () => trace().includes("walk-start")); const ci = start(`echo ci >> ${p.trace}`, env("ci", "ci-swift-compile")); await until("the check job has filed its claim", () => listed(p.claims).length === 1); await new Promise((r) => setTimeout(r, 3500)); expect(trace(), "the check job stopped the walk").toEqual(["walk-start"]); expect(walk.stderr()).not.toContain("YIELDED"); writeFileSync(finish, ""); const [w, c] = await Promise.all([walk.done, ci.done]); for (const r of [w, c]) expect(r.status, r.stderr).toBe(0); expect(trace()).toEqual(["walk-start", "walk-end", "ci"]); expect(w.stderr).not.toContain("YIELDED"); expect(listed(p.claims)).toEqual([]); }, 60_000); // RULE: A check job behind a holding walk waits up to its own BUILD_LOCK_MAX_MIN, not the ten-minute lock timeout, and gives up past it (swift-main run 8301, 8 Nov: loom gave up at 10m behind a lane's walk build that never yields to it). it("a check job behind a holding walk outlasts the lock timeout, up to its own ceiling", async () => { const p = paths(); const finish = join(scratch, "finish"); const walk = start( `echo walk-start >> ${p.trace}; while [ ! -f ${finish} ]; do sleep 0.1; done; echo walk-end >> ${p.trace}`, env("walk", "walk-sim"), ); await until("the walk holds the lock", () => trace().includes("walk-start")); const ci = start(`echo ci >> ${p.trace}`, { ...env("ci", "ci-loom"), BUILD_LOCK_TIMEOUT_SECONDS: "1", BUILD_LOCK_MAX_MIN: "1", BUILD_LOCK_CEILING_SECONDS: "30", }); await until("the check job says how long it waits", () => ci.stderr().includes("WAITING on 'walk-sim'")); expect(ci.stderr()).toContain("gives up at 30s"); await new Promise((r) => setTimeout(r, 3500)); expect(ci.stderr(), "the check job gave up at the lock timeout").not.toContain("TIMEOUT"); writeFileSync(finish, ""); const [w, c] = await Promise.all([walk.done, ci.done]); for (const r of [w, c]) expect(r.status, r.stderr).toBe(0); expect(trace()).toEqual(["walk-start", "walk-end", "ci"]); }, 60_000); // RULE: The ceiling still bounds that wait: a check job behind a walk that outlives it gives up with exit 75. it("a check job behind a holding walk gives up at its own ceiling", async () => { const p = paths(); const finish = join(scratch, "finish"); const walk = start( `echo walk-start >> ${p.trace}; while [ ! -f ${finish} ]; do sleep 0.1; done`, env("walk", "walk-sim"), ); await until("the walk holds the lock", () => trace().includes("walk-start")); const ci = start(`echo ci >> ${p.trace}`, { ...env("ci", "ci-loom"), BUILD_LOCK_TIMEOUT_SECONDS: "1", BUILD_LOCK_MAX_MIN: "1", BUILD_LOCK_CEILING_SECONDS: "3", }); const c = await ci.done; expect(c.status, c.stderr).toBe(75); expect(c.stderr).toContain("TIMEOUT after 3s"); expect(trace()).toEqual(["walk-start"]); writeFileSync(finish, ""); expect((await walk.done).status).toBe(0); }, 60_000); }); describe("caller-class.sh — rank_as_walk", () => { const rank = (extra: Record) => spawnSync("bash", ["-c", `. "${CALLER_CLASS}"; rank_as_walk; caller_class`], { encoding: "utf8", env: { PATH: process.env.PATH ?? "/usr/bin:/bin", HOME: tmpdir(), ...extra }, }).stdout.trim(); // RULE: A walk script ranks its builds `walk` only when nothing ranked them already; a check job or release running it keeps its rank. it("ranks an unranked local caller as a walk and leaves every other rank alone", () => { expect(rank({})).toBe("walk"); expect(rank({ CI: "true" })).toBe("ci"); expect(rank({ GITHUB_ACTIONS: "true" })).toBe("ci"); expect(rank({ BUILD_LOCK_CLASS: "release" })).toBe("release"); expect(rank({ BUILD_LOCK_CLASS: "local" })).toBe("local"); }); });