From 07a4a6119c0967a5e242152c9f0f39aff411d74e Mon Sep 17 00:00:00 2001 From: "dam-application[bot]" <280654635+dam-application[bot]@users.noreply.github.com> Date: Tue, 10 Nov 2026 12:45:14 +0000 Subject: [PATCH] feat: prune the free slots' build output when the disk runs low (6.16.0) Ported from the bash run-state.sh to the app's run-state start. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 12 +++++++++ CLAUDE.md | 2 ++ ONBOARDING.md | 5 +++- VERSION | 2 +- app/src/developer/run-state.ts | 47 +++++++++++++++++++++++++++++++++- app/src/developer/verify.ts | 3 +++ app/test/run-state.test.ts | 32 +++++++++++++++++++++++ docs/runs.md | 5 ++++ 8 files changed, 105 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b81b7e8..a57b156 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,18 @@ Applying the kit never touches an agent already created from it, so an instance crosses a version only when its operator walks it through these steps, in the direct session. +## 6.16.0 — 2026-11-10 + +**Prune the free slots' build output when the disk runs low.** Two optional +keys: `prune`, the command that deletes a checkout's rebuildable build output, +and `prune_below_gb`, default 20. With less free than that, `start` runs +`prune` in the checkout and in every slot no run holds, each slot locked while +it runs; a held slot and the run's own are left alone. Without `prune`, +nothing changes. + +**Upgrade:** optional, in the direct session — add `- prune: ` to +`work/CONFIG.md`. + ## 6.15.2 — 2026-11-05 **A demo shows the change for real, or there is none.** A demo Environment diff --git a/CLAUDE.md b/CLAUDE.md index 868ca30..822b126 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -70,6 +70,8 @@ a line in any other shape is invisible, not wrong. take no new item; missing: none ([`docs/runs.md`](docs/runs.md) → **Slots**). `babysit_max_hours` — default 4: how long a run may `wait` in its turn. `stuck_after_min` — default 120: every slot held and a run silent this long lets a diagnostic run through. + `prune` — deletes a slot's rebuildable output, below `prune_below_gb` (default 20) + GiB free ([`docs/runs.md`](docs/runs.md) → **Slots**); missing: nothing is. - `work_repo` — `owner/name`, private, holding the backup of `work/` ([`docs/persistence.md`](docs/persistence.md) → **Backup**). Missing: none. `work_account` — the GitHub account (`gh auth status`) the backup acts as, diff --git a/ONBOARDING.md b/ONBOARDING.md index 0df365e..66b6655 100644 --- a/ONBOARDING.md +++ b/ONBOARDING.md @@ -210,6 +210,8 @@ that is not one of them fails verification, because nothing would ever read it: - slots_closed: - babysit_max_hours: 4 - stuck_after_min: 120 +- prune: +- prune_below_gb: 20 - work_repo: owner/name-work - work_account: - slack_channel: C0123ABCD @@ -234,7 +236,8 @@ neither check, or no path that needs one, `verify_required_paths`; with no Environment at all, `label_ui_review`; with no review label, `label_review`; with no backup, leave out `work_repo` and `work_account`; when the active account reaches it, `work_account`; with no Slack, leave out `slack_channel`, `slack_handoff`, `slack_ui_review` and `slack_automerge`; with every slot open, `slots_closed`; for the default grill skills, `skill_grill` and `skill_file_issue`; leave -`repos_also` out when the work never leaves `repo`. +`repos_also` out when the work never leaves `repo`; leave `prune` and +`prune_below_gb` out when builds leave little behind. `slots`, `babysit_max_hours`, `stuck_after_min` and `environments_max` are not questions for the user: write the defaults. Lower `slots` when the sandbox cannot run that many builds at once; diff --git a/VERSION b/VERSION index 0bffdbe..bfec95b 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -6.15.2 +6.16.0 diff --git a/app/src/developer/run-state.ts b/app/src/developer/run-state.ts index 2e6c3ce..eb2b657 100644 --- a/app/src/developer/run-state.ts +++ b/app/src/developer/run-state.ts @@ -49,13 +49,14 @@ // quiet live runs silent for stuck_after_min, one a line // diagnosed the precheck let a diagnostic run through // show all of it, for a human -import { appendFileSync, existsSync, mkdirSync, readdirSync, rmSync, statSync } from "node:fs"; +import { appendFileSync, existsSync, mkdirSync, readdirSync, rmSync, statfsSync, statSync } from "node:fs"; import { join, relative } from "node:path"; import type { Command, Deps } from "../factory/context.ts"; import { Exit } from "../factory/context.ts"; import type { Conditional } from "../factory/github.ts"; import { kv, readText, writeKv } from "../factory/kv.ts"; import { BOOKING_GRACE_MIN, MESSAGE_GRACE_MIN, waiterAlive } from "../factory/locks.ts"; +import { run } from "../factory/proc.ts"; import { ATTENDED } from "../factory/runtime.ts"; import { epoch, iso, nowSeconds } from "../factory/time.ts"; import { persistTo } from "./backup.ts"; @@ -319,10 +320,54 @@ class RunState { babysat_out: "-", updated_at: now, }); + this.pruneFree(k); this.out(`slot ${k}: ${join(this.kit.slots, k)} on ${branch}`); return 0; } + /** Whole GiB free on the disk work/ is on; undefined when it cannot be read. */ + private freeGb(): number | undefined { + const fake = this.deps.env.SD_FREE_GB; + if (fake !== undefined && fake !== "") return /^[0-9]+$/.test(fake) ? Number(fake) : undefined; + try { + const f = statfsSync(this.kit.work); + return Math.floor((f.bavail * f.bsize) / 1024 ** 3); + } catch { + return undefined; + } + } + + /** + * With `prune` set and the disk below `prune_below_gb`, `prune` runs in the + * checkout and in every other slot no run holds, each slot taken for it so + * no run starts there meanwhile. A held slot's build output is its run's, + * and the run's own slot is about to be used: neither is touched. + */ + private pruneFree(mine: string): void { + const cmd = this.kit.cfg.get("prune"); + if (!cmd) return; + const min = this.kit.cfg.int("prune_below_gb", 20); + const free = this.freeGb(); + if (free === undefined) { + this.say("free disk was not measured — prune skipped"); + return; + } + if (free >= min) return; + this.say(`${String(free)} GiB free, under ${String(min)} — pruning the checkout and the free slots`); + const prune = (cwd: string) => run("bash", ["-c", cmd], { cwd, env: this.deps.env }).code === 0; + if (!prune(this.kit.checkout)) this.say("prune failed in the checkout"); + for (let j = 1; j <= this.kit.slotCount; j++) { + const c = String(j); + const dir = join(this.kit.slots, c); + if (c === mine || !existsSync(dir)) continue; + if (!this.locks.take(`slot-${c}`, { slot: c })) continue; + if (!prune(dir)) this.say(`prune failed in slot ${c}`); + this.locks.drop(`slot-${c}`); + } + const after = this.freeGb(); + if (after !== undefined) this.say(`${String(after)} GiB free after the prune`); + } + /** One more repository a run takes beside its slot, in a worktree of its own on the slot's branch. */ async also(args: readonly string[]): Promise { const raw = args[0]; diff --git a/app/src/developer/verify.ts b/app/src/developer/verify.ts index db6dfc2..783ae03 100644 --- a/app/src/developer/verify.ts +++ b/app/src/developer/verify.ts @@ -47,6 +47,8 @@ export const KNOWN = [ "slots_closed", "babysit_max_hours", "stuck_after_min", + "prune", + "prune_below_gb", "work_repo", "work_account", "slack_channel", @@ -191,6 +193,7 @@ function configChecks(kit: Kit, r: Report): void { }; whole("stuck_after_min", true, "a whole number of minutes", "write 120, or more"); whole("babysit_max_hours", false, "a whole number of hours", "write 4, or leave it out"); + whole("prune_below_gb", true, "a whole number of GiB", "write 20, or leave it out"); whole("slots", false, "a whole number of slots", "write 3, or leave it out"); const closed = cfg.list("slots_closed"); const most = cfg.int("slots", 3, 1); diff --git a/app/test/run-state.test.ts b/app/test/run-state.test.ts index 54c5938..ff8a0bd 100644 --- a/app/test/run-state.test.ts +++ b/app/test/run-state.test.ts @@ -1255,3 +1255,35 @@ test("room with nothing to take back waits, and says once a day which pull reque r = await as(s, "sess-a", "room"); assert.match(r.err, /notify:/, "what holds them changed"); })); + +test("with prune set and the disk low, start prunes the checkout and the free slots, never a held one", () => + inSandbox(async (s) => { + world(s); + s.originCheckout(); + s.config("- prune: touch .pruned"); + await as(s, "sess-a", "start", "7", "feat/7"); + await as(s, "sess-b", "start", "8", "feat/8"); + await as(s, "sess-b", "finish", "nothing"); + // room enough: nothing is touched + let r = await s.run(runState, ["start", "9", "feat/9"], { CLAUDE_CODE_SESSION_ID: "sess-c", SD_FREE_GB: "50" }); + assert.equal(r.rc, 0, r.err); + assert.ok(!existsSync(join(s.work, "slots", "2", ".pruned"))); + await as(s, "sess-c", "finish", "nothing"); + // low: the checkout and free slot 2 are pruned; held slot 1 and the run's own slot are kept + r = await s.run(runState, ["start", "9", "feat/9"], { CLAUDE_CODE_SESSION_ID: "sess-c", SD_FREE_GB: "5" }); + assert.equal(r.rc, 0, r.err); + assert.match(r.err, /5 GiB free, under 20/); + const mine = /slot (\d+):/.exec(r.out)?.[1] ?? ""; + assert.ok(existsSync(join(s.work, "widgets", ".pruned")), "checkout not pruned"); + assert.ok(!existsSync(join(s.work, "slots", "1", ".pruned")), "pruned a held slot"); + assert.ok(!existsSync(join(s.work, "slots", mine, ".pruned")), "pruned the run's own slot"); + for (const k of ["2", "3"].filter((k) => k !== mine && existsSync(join(s.work, "slots", k)))) + assert.ok(existsSync(join(s.work, "slots", k, ".pruned")), `free slot ${k} not pruned`); + assert.deepEqual( + locks(s) + .filter((l) => l.startsWith("slot-")) + .sort(), + ["slot-1", `slot-${mine}`].sort(), + "a prune lock was left behind", + ); + })); diff --git a/docs/runs.md b/docs/runs.md index a29d7bd..f69c1d1 100644 --- a/docs/runs.md +++ b/docs/runs.md @@ -50,6 +50,11 @@ removes untracked files there, never ignored ones. It prefers the slot the item last used. `finish` detaches a saved slot, so the branch is free for any slot next time. +That build output fills the disk. With `prune` set and less than +`prune_below_gb` GiB free, `start` runs `prune` in the checkout and in every +other slot no run holds, taking each slot's lock while it runs; a held slot is +its run's, and is left alone. `prune` deletes only what a build makes again. + **A closed slot**, one `slots_closed` names, finishes what it holds and takes no new item: `start` passes it by, and the precheck counts only the open ones as free. It keeps its worktree and its build output, so opening it again is