protected-path snapshots copy + leak .sandcastle go-cache/npm-cache into /tmp (quota exhaustion); orphaned snapshot dirs never reaped #320

Closed
opened 2026-10-30 17:57:02 +00:00 by matou-build-agent · 3 comments

Origin: routed from consumer ticket Matou/matou-app#761 (ADR 0180 — vendored .sandcastle/ files are read-only in consumers; the fix belongs here in the factory core). The swarm run that surfaced it: matou-app Actions run 44228, red at sandcastle run (workers).

Symptom

On a swarm host, /tmp is a 7.5G tmpfs mounted usrquota. Two leaked protected-path snapshot directories (2.7G + 2.4G, ~71k blobs each) filled that quota, so an unrelated session-capture docker cp died with write /tmp/...jsonl: disk quota exceeded and redded the run's post-step. The worker itself finished cleanly.

Root cause — two stacked defects

(a) pp_snapshot copies the host caches. PP_EXEMPT_GLOBS_DEFAULT in protected-paths-lib.sh exempts .sandcastle/logs/*, .state/*, worktrees/*, pnpm-store/*, nix-store/* and the __pycache__ trees — but not .sandcastle/go-cache/* (gomobile/go-build, ~2.4G) or .sandcastle/npm-cache/* (~374M). Both are gitignored host-side caches (.sandcastle/.gitignore), exactly the runtime class the comment block says is exempt; they were simply missed. So pp_snapshot sha256s and cps ~2.8G of cache into /tmp twice per run (before + after snapshot) — ~5.6G against a 7.5G quota, enough to exhaust it alone.

(b) the snapshots are never reliably reaped. verify_pp_snapshot / verify_protected_paths create them with two bare mktemp -d dirs, removed only by run-swarm.sh's EXIT trap — which a SIGKILL (or an abort between the two snapshots) cannot run. The two leaked dirs survived two days.

Fix (patch below, applies clean to HEAD a2f2379)

  1. Add .sandcastle/go-cache/* .sandcastle/npm-cache/* to PP_EXEMPT_GLOBS_DEFAULT, anchored under .sandcastle/ like the other cache entries.
  2. Root both snapshots under one per-run dir (VERIFY_PP_RUNDIR = <tmproot>/run-<pid>-*) that the EXIT trap removes in a single rm, and verify_pp_snapshot reaps on entry any run-<pid>-* dir whose owning PID is dead (the only way to clean up after a SIGKILL that no trap catches). PID reuse only delays a reap; a live peer's snapshot is never touched. VERIFY_PP_TMPROOT overrides the tmproot for the offline test.

Tests extended: protected-paths-lib-test.sh asserts both caches are exempt and survive a churn-plus-machinery-edit run; verify-lib-test.sh asserts the before-snapshot lives under the reapable per-run tree and the reaper removes a dead-PID orphan while sparing a live peer.

Proof (run on this patch against HEAD):

protected-paths-lib: 63 checks passed
verify-lib: 13 groups passed
run-swarm-stages: 13 groups passed

Red-before-green confirmed on the consumer checkout with byte-identical code (the new cache-exempt assertion and the "before-snapshot must live under the reapable per-run tree" assertion both fail before the lib/verify-lib edits).

Not included — ticket's fix item (3), deliberately deferred

matou-app#761 also proposed failing loud when pp_snapshot cannot write a blob, reasoning a truncated before-snapshot makes "every unwritten path read as appeared on the next compare." That rationale is incorrect: pp_snapshot always writes the index line (digest+key+path); only the blob cp is || true, and pp_attributed compares index digests, not blobs — so a failed blob write does not flip a path to appeared. The real (secondary) effect is reduced rollback fidelity (pp_rollback's restore cp silently no-ops). With (a) landed the snapshot is tiny, so quota-driven blob failures are no longer realistic, and converting pp_snapshot to fail loud is a behavior change that could red runs on a genuinely full disk — left out of this leak-fix for a maintainer to weigh separately.

Patch

diff --git a/protected-paths-lib.sh b/protected-paths-lib.sh
index a80e0207..9117fdbf 100755
--- a/protected-paths-lib.sh
+++ b/protected-paths-lib.sh
@@ -72,7 +72,10 @@ PP_PROTECTED_DIRS_DEFAULT=".sandcastle .forgejo"
 # The benign host-side CACHE trees under `.sandcastle/` are exempt too (#284(a),
 # the deferred half of #272): `.sandcastle/pnpm-store/*`, `.sandcastle/nix-store/*`
 # and the regenerated Python bytecode `.sandcastle/__pycache__/*` plus the
-# per-subpackage `.sandcastle/*/__pycache__/*` (swarm-db.py and the TUI). All are
+# per-subpackage `.sandcastle/*/__pycache__/*` (swarm-db.py and the TUI), and the
+# `.sandcastle/go-cache/*` (gomobile/go-build) and `.sandcastle/npm-cache/*`
+# host-side caches (#761 — originally missed here, so pp_snapshot cp'd ~2.8G of
+# cache blobs into /tmp twice per run and exhausted the tmpfs user quota). All are
 # gitignored by `.sandcastle/.gitignore`, mounted or regenerated under the
 # protected tree exactly like logs/.state/worktrees — the same low-risk runtime
 # class, not machinery. They stay ANCHORED under `.sandcastle/` (never a bare
@@ -81,7 +84,7 @@ PP_PROTECTED_DIRS_DEFAULT=".sandcastle .forgejo"
 # are #272's OTHER deferred half — they were NOT folded in here: exempting them
 # is a protected-boundary POSTURE change (it removes host secrets from what the
 # boundary rolls back), a one-way door ADR 0174 reserves for a human (#284(b)).
-PP_EXEMPT_GLOBS_DEFAULT="*matou-heal-* */matou-heal-* .sandcastle/logs/* .sandcastle/.state/* .sandcastle/worktrees/* .sandcastle/pnpm-store/* .sandcastle/nix-store/* .sandcastle/__pycache__/* .sandcastle/*/__pycache__/*"
+PP_EXEMPT_GLOBS_DEFAULT="*matou-heal-* */matou-heal-* .sandcastle/logs/* .sandcastle/.state/* .sandcastle/worktrees/* .sandcastle/pnpm-store/* .sandcastle/nix-store/* .sandcastle/go-cache/* .sandcastle/npm-cache/* .sandcastle/__pycache__/* .sandcastle/*/__pycache__/*"
 
 # The escape path, spelled out verbatim so every enforcement failure routes a
 # worker that was RIGHT the machinery needs changing instead of letting it
diff --git a/run-swarm.sh b/run-swarm.sh
index 854227d4..29254839 100755
--- a/run-swarm.sh
+++ b/run-swarm.sh
@@ -193,7 +193,7 @@ on_exit() {
   verify_watch_stop
   [ -n "$worker_births" ] && rm -f "$worker_births" 2>/dev/null || true
   [ -n "${sandcastle_attempts:-}" ] && rm -f "$sandcastle_attempts" 2>/dev/null || true   # #283 attempt capture
-  rm -rf "${VERIFY_PP_BEFORE:-}" "${VERIFY_PP_AFTER:-}" 2>/dev/null || true   # #445 snapshots
+  rm -rf "${VERIFY_PP_RUNDIR:-}" "${VERIFY_PP_BEFORE:-}" "${VERIFY_PP_AFTER:-}" 2>/dev/null || true   # #445 snapshots (#761: one per-run dir holds both)
   verdict_write "$ec"
   # Stamp the executing host onto the on-failure verdict (#135). verdict_write
   # only wrote a file on a NON-zero exit, so a clean run leaves none and this is
diff --git a/tests/protected-paths-lib-test.sh b/tests/protected-paths-lib-test.sh
index 89c62a0a..f2962a99 100755
--- a/tests/protected-paths-lib-test.sh
+++ b/tests/protected-paths-lib-test.sh
@@ -176,6 +176,14 @@ pp_is_protected ".sandcastle/__pycache__/swarm_db.cpython-312.pyc" \
   && fail ".sandcastle/__pycache__/ is regenerated bytecode, NOT machinery"; ok
 pp_is_protected ".sandcastle/tui/__pycache__/board.cpython-312.pyc" \
   && fail "a per-subpackage __pycache__/ is regenerated bytecode, NOT machinery"; ok
+# #761: the gomobile/go-build and npm host-side caches (`.sandcastle/.gitignore`
+# lists `go-cache/` and `npm-cache/`) are the same never-committed runtime class
+# — they were simply missed here, so pp_snapshot cp'd ~2.8G of cache blobs into
+# /tmp twice per run and exhausted the tmpfs user quota.
+pp_is_protected ".sandcastle/go-cache/abc123/-/x.a" \
+  && fail ".sandcastle/go-cache/ is a host-side cache, NOT machinery (#761)"; ok
+pp_is_protected ".sandcastle/npm-cache/_cacache/content-v2/sha512/ab/cd/ef" \
+  && fail ".sandcastle/npm-cache/ is a host-side cache, NOT machinery (#761)"; ok
 pp_is_protected ".sandcastle/run-swarm.sh" \
   || fail "machinery under .sandcastle/ stays protected alongside the caches"; ok
 # A worker-bearing run churns all three caches AND edits real machinery: only the
@@ -183,11 +191,14 @@ pp_is_protected ".sandcastle/run-swarm.sh" \
 root="$(newroot)"
 pp_snapshot "$root" "$before"
 mkdir -p "$root/.sandcastle/pnpm-store/v3" "$root/.sandcastle/nix-store/abc-foo" \
-         "$root/.sandcastle/__pycache__" "$root/.sandcastle/tui/__pycache__"
+         "$root/.sandcastle/__pycache__" "$root/.sandcastle/tui/__pycache__" \
+         "$root/.sandcastle/go-cache/abc" "$root/.sandcastle/npm-cache/_cacache"
 printf 'pkg\n'  > "$root/.sandcastle/pnpm-store/v3/index"
 printf 'elf\n'  > "$root/.sandcastle/nix-store/abc-foo/bin"
 printf 'pyc\n'  > "$root/.sandcastle/__pycache__/swarm_db.cpython-312.pyc"
 printf 'pyc\n'  > "$root/.sandcastle/tui/__pycache__/board.cpython-312.pyc"
+printf 'obj\n'  > "$root/.sandcastle/go-cache/abc/x.a"
+printf 'tgz\n'  > "$root/.sandcastle/npm-cache/_cacache/content"
 printf 'rm -rf /\n' > "$root/.sandcastle/evil.sh"
 pp_snapshot "$root" "$after"
 got="$(pp_attributed "$before" "$after")"
@@ -199,6 +210,8 @@ pp_enforce "$root" "$before" "$after" >/dev/null 2>&1 \
 [ -e "$root/.sandcastle/nix-store/abc-foo/bin" ] || fail "nix-store/ must not be rolled back"; ok
 [ -e "$root/.sandcastle/__pycache__/swarm_db.cpython-312.pyc" ] || fail "__pycache__/ must not be rolled back"; ok
 [ -e "$root/.sandcastle/tui/__pycache__/board.cpython-312.pyc" ] || fail "a nested __pycache__/ must not be rolled back"; ok
+[ -e "$root/.sandcastle/go-cache/abc/x.a" ] || fail "go-cache/ must not be rolled back (#761)"; ok
+[ -e "$root/.sandcastle/npm-cache/_cacache/content" ] || fail "npm-cache/ must not be rolled back (#761)"; ok
 [ -e "$root/.sandcastle/evil.sh" ] && fail "the machinery edit must be rolled back"; ok
 rm -rf "$root"
 
diff --git a/tests/verify-lib-test.sh b/tests/verify-lib-test.sh
index 4072a26e..9d51b92f 100644
--- a/tests/verify-lib-test.sh
+++ b/tests/verify-lib-test.sh
@@ -24,6 +24,9 @@ fail() { echo "FAIL: $1" >&2; exit 1; }
 pass=0
 tmp="$(mktemp -d)"; trap 'rm -rf "$tmp"' EXIT
 mkdir -p "$tmp/bin"
+# Sandbox the protected-path snapshot tmproot under $tmp so the suite never
+# writes into the real /tmp tmpfs (and so the reaper group below is isolated) — #761.
+export VERIFY_PP_TMPROOT="$tmp/pp-snapshots"
 
 . "$sc/runlog-lib.sh"
 . "$sc/fence-lib.sh"
@@ -223,4 +226,27 @@ grep -q 'push origin HEAD:main' "$GIT_LOG" && fail "a breach must NEVER reach ma
 grep -q 'protected-path-violation' "$tmp/db.log" || fail "the violation must be traced in swarm.db"
 pass=$((pass+1))
 
+# ── 3. snapshot dirs are rooted in one reapable per-run tree (#761) ──────────
+# A run SIGKILLed between the before- and after-snapshot leaks its snapshot dir —
+# no EXIT trap catches SIGKILL. Two of those (2.7G + 2.4G of cp'd cache blobs)
+# survived two days and walled the /tmp tmpfs user quota, redding an unrelated
+# post-step. The snapshots now live under one tmproot in a `run-<pid>-*` dir that
+# the NEXT run's reaper removes once that owning PID is dead.
+rm -rf "$VERIFY_PP_TMPROOT"
+PP_ENFORCE=1 verify_pp_snapshot "$ws"
+case "$VERIFY_PP_BEFORE" in
+  "$VERIFY_PP_TMPROOT"/run-$$-*/before) : ;;
+  *) fail "the before-snapshot must live under the reapable per-run tree, got $VERIFY_PP_BEFORE" ;;
+esac
+[ -d "$VERIFY_PP_BEFORE" ] || fail "the before-snapshot dir must exist under the per-run tree"
+pass=$((pass+1))
+
+# the reaper removes an orphan left by a DEAD run while sparing a live peer.
+dead=999999; while kill -0 "$dead" 2>/dev/null; do dead=$((dead+1)); done
+mkdir -p "$VERIFY_PP_TMPROOT/run-$dead-orphan/blobs" "$VERIFY_PP_TMPROOT/run-$$-live/blobs"
+PP_ENFORCE=1 verify_pp_snapshot "$ws"   # calls the reaper on entry
+[ ! -d "$VERIFY_PP_TMPROOT/run-$dead-orphan" ] || fail "a dead run's orphaned snapshot must be reaped (#761)"
+[ -d "$VERIFY_PP_TMPROOT/run-$$-live" ] || fail "a live run's snapshot must NOT be reaped (PID still alive)"
+pass=$((pass+1))
+
 echo "verify-lib: $pass groups passed"
diff --git a/verify-lib.sh b/verify-lib.sh
index deb0dd89..4fb132ae 100644
--- a/verify-lib.sh
+++ b/verify-lib.sh
@@ -36,7 +36,9 @@ VERIFY_FENCE_PID=""
 # already failed loud by then. Starts at 1 so a caller that never starts a
 # watcher (a unit test driving verify_workers directly) still gets the check.
 VERIFY_WATCHING=1
-# The protected-path snapshots (#445); the EXIT trap removes both.
+# The protected-path snapshots (#445); both live under the one per-run dir
+# VERIFY_PP_RUNDIR, which the EXIT trap removes in a single rm (#761).
+VERIFY_PP_RUNDIR=""
 VERIFY_PP_BEFORE=""
 VERIFY_PP_AFTER=""
 # Set to 1 by verify_protected_paths when a worker breached the boundary (#193).
@@ -168,16 +170,47 @@ _verify_pp_enforced() {
   fi
 }
 
+# _verify_pp_tmproot — the per-host parent dir under which EVERY run's before/
+# after protected-path snapshot lives. Rooting them in one place (rather than two
+# bare `mktemp -d` dirs) is what makes an orphan reapable: a run SIGKILLed between
+# the two snapshots leaks its dir — no EXIT trap catches SIGKILL — and before #761
+# those were 2.7G+2.4G of cp'd cache blobs that survived two days and walled the
+# /tmp tmpfs user quota. Overridable (VERIFY_PP_TMPROOT) for the offline test.
+_verify_pp_tmproot() { printf '%s' "${VERIFY_PP_TMPROOT:-${TMPDIR:-/tmp}/matou-pp-snapshots}"; }
+
+# _verify_pp_reap — best-effort removal of snapshot dirs orphaned by a DEAD run:
+# a `run-<pid>-*` child of the tmproot whose owning run-swarm PID is no longer
+# alive. PID reuse only DELAYS a reap (a live unrelated PID keeps the dir one more
+# cycle); it never reaps a running peer's snapshot. Never reds a run.
+_verify_pp_reap() {
+  local root d pid
+  root="$(_verify_pp_tmproot)"
+  [ -d "$root" ] || return 0
+  for d in "$root"/run-*; do
+    [ -d "$d" ] || continue
+    pid="${d##*/run-}"; pid="${pid%%-*}"
+    case "$pid" in ''|*[!0-9]*) continue ;; esac
+    kill -0 "$pid" 2>/dev/null && continue   # owner still alive — its own run owns it
+    rm -rf "$d" 2>/dev/null || true
+  done
+}
+
 # verify_pp_snapshot <workspace> — fingerprint the machinery NOW, before a single
 # worker runs. Snapshotting here — after the pre-existing dirty state is whatever
 # it is — means another session's uncommitted work is the baseline we compare
 # against and is never rolled back as collateral. Best-effort: a snapshot we
 # cannot take never reds the run (verify_protected_paths then finds nothing to
-# compare and no-ops).
+# compare and no-ops). The before/after dirs share ONE per-run dir
+# (VERIFY_PP_RUNDIR) so the run teardown reaps both with a single rm, and any dir
+# an earlier dead run orphaned is reaped on entry here (#761).
 verify_pp_snapshot() {
   VERIFY_PP_BEFORE=""
   _verify_pp_enforced || return 0
-  VERIFY_PP_BEFORE="$(mktemp -d)"
+  _verify_pp_reap
+  local root; root="$(_verify_pp_tmproot)"
+  mkdir -p "$root" 2>/dev/null || true
+  VERIFY_PP_RUNDIR="$(mktemp -d "$root/run-$$-XXXXXX" 2>/dev/null || mktemp -d)"
+  VERIFY_PP_BEFORE="$VERIFY_PP_RUNDIR/before"
   pp_snapshot "$1" "$VERIFY_PP_BEFORE" 2>/dev/null || true
 }
 
@@ -190,7 +223,13 @@ verify_protected_paths() {
   local workspace="$1" run_db_id="$2" repo_slug="$3" banner paths rescue
   VERIFY_PP_VIOLATION=""
   _verify_pp_enforced && command -v pp_enforce >/dev/null 2>&1 || return 0
-  VERIFY_PP_AFTER="$(mktemp -d)"
+  # Share the per-run dir with the before-snapshot so one teardown rm reaps both;
+  # fall back to a bare mktemp only if the snapshot stage never set it (#761).
+  if [ -n "${VERIFY_PP_RUNDIR:-}" ]; then
+    VERIFY_PP_AFTER="$VERIFY_PP_RUNDIR/after"
+  else
+    VERIFY_PP_AFTER="$(mktemp -d)"
+  fi
   pp_snapshot "$workspace" "$VERIFY_PP_AFTER" 2>/dev/null || true
   banner="$(mktemp)"
   if paths="$(pp_enforce "$workspace" "$VERIFY_PP_BEFORE" "$VERIFY_PP_AFTER" 2>"$banner")" \
**Origin:** routed from consumer ticket `Matou/matou-app#761` (ADR 0180 — vendored `.sandcastle/` files are read-only in consumers; the fix belongs here in the factory core). The swarm run that surfaced it: matou-app Actions run 44228, red at `sandcastle run (workers)`. ## Symptom On a swarm host, `/tmp` is a **7.5G tmpfs mounted `usrquota`**. Two leaked protected-path snapshot directories (2.7G + 2.4G, ~71k blobs each) filled that quota, so an unrelated session-capture `docker cp` died with `write /tmp/...jsonl: disk quota exceeded` and redded the run's post-step. The worker itself finished cleanly. ## Root cause — two stacked defects **(a) `pp_snapshot` copies the host caches.** `PP_EXEMPT_GLOBS_DEFAULT` in `protected-paths-lib.sh` exempts `.sandcastle/logs/*`, `.state/*`, `worktrees/*`, `pnpm-store/*`, `nix-store/*` and the `__pycache__` trees — but **not** `.sandcastle/go-cache/*` (gomobile/go-build, ~2.4G) or `.sandcastle/npm-cache/*` (~374M). Both are gitignored host-side caches (`.sandcastle/.gitignore`), exactly the runtime class the comment block says is exempt; they were simply missed. So `pp_snapshot` sha256s and `cp`s ~2.8G of cache into `/tmp` **twice per run** (before + after snapshot) — ~5.6G against a 7.5G quota, enough to exhaust it alone. **(b) the snapshots are never reliably reaped.** `verify_pp_snapshot` / `verify_protected_paths` create them with two bare `mktemp -d` dirs, removed only by `run-swarm.sh`'s `EXIT` trap — which a `SIGKILL` (or an abort between the two snapshots) cannot run. The two leaked dirs survived two days. ## Fix (patch below, applies clean to HEAD `a2f2379`) 1. Add `.sandcastle/go-cache/* .sandcastle/npm-cache/*` to `PP_EXEMPT_GLOBS_DEFAULT`, anchored under `.sandcastle/` like the other cache entries. 2. Root both snapshots under one per-run dir (`VERIFY_PP_RUNDIR = <tmproot>/run-<pid>-*`) that the `EXIT` trap removes in a single `rm`, and `verify_pp_snapshot` **reaps on entry** any `run-<pid>-*` dir whose owning PID is dead (the only way to clean up after a `SIGKILL` that no trap catches). PID reuse only delays a reap; a live peer's snapshot is never touched. `VERIFY_PP_TMPROOT` overrides the tmproot for the offline test. Tests extended: `protected-paths-lib-test.sh` asserts both caches are exempt and survive a churn-plus-machinery-edit run; `verify-lib-test.sh` asserts the before-snapshot lives under the reapable per-run tree and the reaper removes a dead-PID orphan while sparing a live peer. **Proof (run on this patch against HEAD):** ``` protected-paths-lib: 63 checks passed verify-lib: 13 groups passed run-swarm-stages: 13 groups passed ``` Red-before-green confirmed on the consumer checkout with byte-identical code (the new cache-exempt assertion and the "before-snapshot must live under the reapable per-run tree" assertion both fail before the lib/verify-lib edits). ## Not included — ticket's fix item (3), deliberately deferred matou-app#761 also proposed failing loud when `pp_snapshot` cannot write a blob, reasoning a truncated before-snapshot makes "every unwritten path read as `appeared` on the next compare." **That rationale is incorrect:** `pp_snapshot` always writes the `index` line (digest+key+path); only the blob `cp` is `|| true`, and `pp_attributed` compares index digests, not blobs — so a failed blob write does *not* flip a path to `appeared`. The real (secondary) effect is reduced *rollback fidelity* (`pp_rollback`'s restore `cp` silently no-ops). With (a) landed the snapshot is tiny, so quota-driven blob failures are no longer realistic, and converting `pp_snapshot` to fail loud is a behavior change that could red runs on a genuinely full disk — left out of this leak-fix for a maintainer to weigh separately. ## Patch ```diff diff --git a/protected-paths-lib.sh b/protected-paths-lib.sh index a80e0207..9117fdbf 100755 --- a/protected-paths-lib.sh +++ b/protected-paths-lib.sh @@ -72,7 +72,10 @@ PP_PROTECTED_DIRS_DEFAULT=".sandcastle .forgejo" # The benign host-side CACHE trees under `.sandcastle/` are exempt too (#284(a), # the deferred half of #272): `.sandcastle/pnpm-store/*`, `.sandcastle/nix-store/*` # and the regenerated Python bytecode `.sandcastle/__pycache__/*` plus the -# per-subpackage `.sandcastle/*/__pycache__/*` (swarm-db.py and the TUI). All are +# per-subpackage `.sandcastle/*/__pycache__/*` (swarm-db.py and the TUI), and the +# `.sandcastle/go-cache/*` (gomobile/go-build) and `.sandcastle/npm-cache/*` +# host-side caches (#761 — originally missed here, so pp_snapshot cp'd ~2.8G of +# cache blobs into /tmp twice per run and exhausted the tmpfs user quota). All are # gitignored by `.sandcastle/.gitignore`, mounted or regenerated under the # protected tree exactly like logs/.state/worktrees — the same low-risk runtime # class, not machinery. They stay ANCHORED under `.sandcastle/` (never a bare @@ -81,7 +84,7 @@ PP_PROTECTED_DIRS_DEFAULT=".sandcastle .forgejo" # are #272's OTHER deferred half — they were NOT folded in here: exempting them # is a protected-boundary POSTURE change (it removes host secrets from what the # boundary rolls back), a one-way door ADR 0174 reserves for a human (#284(b)). -PP_EXEMPT_GLOBS_DEFAULT="*matou-heal-* */matou-heal-* .sandcastle/logs/* .sandcastle/.state/* .sandcastle/worktrees/* .sandcastle/pnpm-store/* .sandcastle/nix-store/* .sandcastle/__pycache__/* .sandcastle/*/__pycache__/*" +PP_EXEMPT_GLOBS_DEFAULT="*matou-heal-* */matou-heal-* .sandcastle/logs/* .sandcastle/.state/* .sandcastle/worktrees/* .sandcastle/pnpm-store/* .sandcastle/nix-store/* .sandcastle/go-cache/* .sandcastle/npm-cache/* .sandcastle/__pycache__/* .sandcastle/*/__pycache__/*" # The escape path, spelled out verbatim so every enforcement failure routes a # worker that was RIGHT the machinery needs changing instead of letting it diff --git a/run-swarm.sh b/run-swarm.sh index 854227d4..29254839 100755 --- a/run-swarm.sh +++ b/run-swarm.sh @@ -193,7 +193,7 @@ on_exit() { verify_watch_stop [ -n "$worker_births" ] && rm -f "$worker_births" 2>/dev/null || true [ -n "${sandcastle_attempts:-}" ] && rm -f "$sandcastle_attempts" 2>/dev/null || true # #283 attempt capture - rm -rf "${VERIFY_PP_BEFORE:-}" "${VERIFY_PP_AFTER:-}" 2>/dev/null || true # #445 snapshots + rm -rf "${VERIFY_PP_RUNDIR:-}" "${VERIFY_PP_BEFORE:-}" "${VERIFY_PP_AFTER:-}" 2>/dev/null || true # #445 snapshots (#761: one per-run dir holds both) verdict_write "$ec" # Stamp the executing host onto the on-failure verdict (#135). verdict_write # only wrote a file on a NON-zero exit, so a clean run leaves none and this is diff --git a/tests/protected-paths-lib-test.sh b/tests/protected-paths-lib-test.sh index 89c62a0a..f2962a99 100755 --- a/tests/protected-paths-lib-test.sh +++ b/tests/protected-paths-lib-test.sh @@ -176,6 +176,14 @@ pp_is_protected ".sandcastle/__pycache__/swarm_db.cpython-312.pyc" \ && fail ".sandcastle/__pycache__/ is regenerated bytecode, NOT machinery"; ok pp_is_protected ".sandcastle/tui/__pycache__/board.cpython-312.pyc" \ && fail "a per-subpackage __pycache__/ is regenerated bytecode, NOT machinery"; ok +# #761: the gomobile/go-build and npm host-side caches (`.sandcastle/.gitignore` +# lists `go-cache/` and `npm-cache/`) are the same never-committed runtime class +# — they were simply missed here, so pp_snapshot cp'd ~2.8G of cache blobs into +# /tmp twice per run and exhausted the tmpfs user quota. +pp_is_protected ".sandcastle/go-cache/abc123/-/x.a" \ + && fail ".sandcastle/go-cache/ is a host-side cache, NOT machinery (#761)"; ok +pp_is_protected ".sandcastle/npm-cache/_cacache/content-v2/sha512/ab/cd/ef" \ + && fail ".sandcastle/npm-cache/ is a host-side cache, NOT machinery (#761)"; ok pp_is_protected ".sandcastle/run-swarm.sh" \ || fail "machinery under .sandcastle/ stays protected alongside the caches"; ok # A worker-bearing run churns all three caches AND edits real machinery: only the @@ -183,11 +191,14 @@ pp_is_protected ".sandcastle/run-swarm.sh" \ root="$(newroot)" pp_snapshot "$root" "$before" mkdir -p "$root/.sandcastle/pnpm-store/v3" "$root/.sandcastle/nix-store/abc-foo" \ - "$root/.sandcastle/__pycache__" "$root/.sandcastle/tui/__pycache__" + "$root/.sandcastle/__pycache__" "$root/.sandcastle/tui/__pycache__" \ + "$root/.sandcastle/go-cache/abc" "$root/.sandcastle/npm-cache/_cacache" printf 'pkg\n' > "$root/.sandcastle/pnpm-store/v3/index" printf 'elf\n' > "$root/.sandcastle/nix-store/abc-foo/bin" printf 'pyc\n' > "$root/.sandcastle/__pycache__/swarm_db.cpython-312.pyc" printf 'pyc\n' > "$root/.sandcastle/tui/__pycache__/board.cpython-312.pyc" +printf 'obj\n' > "$root/.sandcastle/go-cache/abc/x.a" +printf 'tgz\n' > "$root/.sandcastle/npm-cache/_cacache/content" printf 'rm -rf /\n' > "$root/.sandcastle/evil.sh" pp_snapshot "$root" "$after" got="$(pp_attributed "$before" "$after")" @@ -199,6 +210,8 @@ pp_enforce "$root" "$before" "$after" >/dev/null 2>&1 \ [ -e "$root/.sandcastle/nix-store/abc-foo/bin" ] || fail "nix-store/ must not be rolled back"; ok [ -e "$root/.sandcastle/__pycache__/swarm_db.cpython-312.pyc" ] || fail "__pycache__/ must not be rolled back"; ok [ -e "$root/.sandcastle/tui/__pycache__/board.cpython-312.pyc" ] || fail "a nested __pycache__/ must not be rolled back"; ok +[ -e "$root/.sandcastle/go-cache/abc/x.a" ] || fail "go-cache/ must not be rolled back (#761)"; ok +[ -e "$root/.sandcastle/npm-cache/_cacache/content" ] || fail "npm-cache/ must not be rolled back (#761)"; ok [ -e "$root/.sandcastle/evil.sh" ] && fail "the machinery edit must be rolled back"; ok rm -rf "$root" diff --git a/tests/verify-lib-test.sh b/tests/verify-lib-test.sh index 4072a26e..9d51b92f 100644 --- a/tests/verify-lib-test.sh +++ b/tests/verify-lib-test.sh @@ -24,6 +24,9 @@ fail() { echo "FAIL: $1" >&2; exit 1; } pass=0 tmp="$(mktemp -d)"; trap 'rm -rf "$tmp"' EXIT mkdir -p "$tmp/bin" +# Sandbox the protected-path snapshot tmproot under $tmp so the suite never +# writes into the real /tmp tmpfs (and so the reaper group below is isolated) — #761. +export VERIFY_PP_TMPROOT="$tmp/pp-snapshots" . "$sc/runlog-lib.sh" . "$sc/fence-lib.sh" @@ -223,4 +226,27 @@ grep -q 'push origin HEAD:main' "$GIT_LOG" && fail "a breach must NEVER reach ma grep -q 'protected-path-violation' "$tmp/db.log" || fail "the violation must be traced in swarm.db" pass=$((pass+1)) +# ── 3. snapshot dirs are rooted in one reapable per-run tree (#761) ────────── +# A run SIGKILLed between the before- and after-snapshot leaks its snapshot dir — +# no EXIT trap catches SIGKILL. Two of those (2.7G + 2.4G of cp'd cache blobs) +# survived two days and walled the /tmp tmpfs user quota, redding an unrelated +# post-step. The snapshots now live under one tmproot in a `run-<pid>-*` dir that +# the NEXT run's reaper removes once that owning PID is dead. +rm -rf "$VERIFY_PP_TMPROOT" +PP_ENFORCE=1 verify_pp_snapshot "$ws" +case "$VERIFY_PP_BEFORE" in + "$VERIFY_PP_TMPROOT"/run-$$-*/before) : ;; + *) fail "the before-snapshot must live under the reapable per-run tree, got $VERIFY_PP_BEFORE" ;; +esac +[ -d "$VERIFY_PP_BEFORE" ] || fail "the before-snapshot dir must exist under the per-run tree" +pass=$((pass+1)) + +# the reaper removes an orphan left by a DEAD run while sparing a live peer. +dead=999999; while kill -0 "$dead" 2>/dev/null; do dead=$((dead+1)); done +mkdir -p "$VERIFY_PP_TMPROOT/run-$dead-orphan/blobs" "$VERIFY_PP_TMPROOT/run-$$-live/blobs" +PP_ENFORCE=1 verify_pp_snapshot "$ws" # calls the reaper on entry +[ ! -d "$VERIFY_PP_TMPROOT/run-$dead-orphan" ] || fail "a dead run's orphaned snapshot must be reaped (#761)" +[ -d "$VERIFY_PP_TMPROOT/run-$$-live" ] || fail "a live run's snapshot must NOT be reaped (PID still alive)" +pass=$((pass+1)) + echo "verify-lib: $pass groups passed" diff --git a/verify-lib.sh b/verify-lib.sh index deb0dd89..4fb132ae 100644 --- a/verify-lib.sh +++ b/verify-lib.sh @@ -36,7 +36,9 @@ VERIFY_FENCE_PID="" # already failed loud by then. Starts at 1 so a caller that never starts a # watcher (a unit test driving verify_workers directly) still gets the check. VERIFY_WATCHING=1 -# The protected-path snapshots (#445); the EXIT trap removes both. +# The protected-path snapshots (#445); both live under the one per-run dir +# VERIFY_PP_RUNDIR, which the EXIT trap removes in a single rm (#761). +VERIFY_PP_RUNDIR="" VERIFY_PP_BEFORE="" VERIFY_PP_AFTER="" # Set to 1 by verify_protected_paths when a worker breached the boundary (#193). @@ -168,16 +170,47 @@ _verify_pp_enforced() { fi } +# _verify_pp_tmproot — the per-host parent dir under which EVERY run's before/ +# after protected-path snapshot lives. Rooting them in one place (rather than two +# bare `mktemp -d` dirs) is what makes an orphan reapable: a run SIGKILLed between +# the two snapshots leaks its dir — no EXIT trap catches SIGKILL — and before #761 +# those were 2.7G+2.4G of cp'd cache blobs that survived two days and walled the +# /tmp tmpfs user quota. Overridable (VERIFY_PP_TMPROOT) for the offline test. +_verify_pp_tmproot() { printf '%s' "${VERIFY_PP_TMPROOT:-${TMPDIR:-/tmp}/matou-pp-snapshots}"; } + +# _verify_pp_reap — best-effort removal of snapshot dirs orphaned by a DEAD run: +# a `run-<pid>-*` child of the tmproot whose owning run-swarm PID is no longer +# alive. PID reuse only DELAYS a reap (a live unrelated PID keeps the dir one more +# cycle); it never reaps a running peer's snapshot. Never reds a run. +_verify_pp_reap() { + local root d pid + root="$(_verify_pp_tmproot)" + [ -d "$root" ] || return 0 + for d in "$root"/run-*; do + [ -d "$d" ] || continue + pid="${d##*/run-}"; pid="${pid%%-*}" + case "$pid" in ''|*[!0-9]*) continue ;; esac + kill -0 "$pid" 2>/dev/null && continue # owner still alive — its own run owns it + rm -rf "$d" 2>/dev/null || true + done +} + # verify_pp_snapshot <workspace> — fingerprint the machinery NOW, before a single # worker runs. Snapshotting here — after the pre-existing dirty state is whatever # it is — means another session's uncommitted work is the baseline we compare # against and is never rolled back as collateral. Best-effort: a snapshot we # cannot take never reds the run (verify_protected_paths then finds nothing to -# compare and no-ops). +# compare and no-ops). The before/after dirs share ONE per-run dir +# (VERIFY_PP_RUNDIR) so the run teardown reaps both with a single rm, and any dir +# an earlier dead run orphaned is reaped on entry here (#761). verify_pp_snapshot() { VERIFY_PP_BEFORE="" _verify_pp_enforced || return 0 - VERIFY_PP_BEFORE="$(mktemp -d)" + _verify_pp_reap + local root; root="$(_verify_pp_tmproot)" + mkdir -p "$root" 2>/dev/null || true + VERIFY_PP_RUNDIR="$(mktemp -d "$root/run-$$-XXXXXX" 2>/dev/null || mktemp -d)" + VERIFY_PP_BEFORE="$VERIFY_PP_RUNDIR/before" pp_snapshot "$1" "$VERIFY_PP_BEFORE" 2>/dev/null || true } @@ -190,7 +223,13 @@ verify_protected_paths() { local workspace="$1" run_db_id="$2" repo_slug="$3" banner paths rescue VERIFY_PP_VIOLATION="" _verify_pp_enforced && command -v pp_enforce >/dev/null 2>&1 || return 0 - VERIFY_PP_AFTER="$(mktemp -d)" + # Share the per-run dir with the before-snapshot so one teardown rm reaps both; + # fall back to a bare mktemp only if the snapshot stage never set it (#761). + if [ -n "${VERIFY_PP_RUNDIR:-}" ]; then + VERIFY_PP_AFTER="$VERIFY_PP_RUNDIR/after" + else + VERIFY_PP_AFTER="$(mktemp -d)" + fi pp_snapshot "$workspace" "$VERIFY_PP_AFTER" 2>/dev/null || true banner="$(mktemp)" if paths="$(pp_enforce "$workspace" "$VERIFY_PP_BEFORE" "$VERIFY_PP_AFTER" 2>"$banner")" \ ```
Author
Member

swarm-claim host=bens-mac-04 run=25400
(automated multi-host claim — lowest live claim id works this ticket)

swarm-claim host=bens-mac-04 run=25400 (automated multi-host claim — lowest live claim id works this ticket)
Author
Member

⏩ independent reviewer for #320 — verdict unauthenticated

review JSON
{
  "verdict": "unauthenticated",
  "findings": []
}
:fast_forward: **independent reviewer** for #320 — verdict `unauthenticated` <details><summary>review JSON</summary> ```json { "verdict": "unauthenticated", "findings": [] } ``` </details>
Author
Member

close-report for #320 — status success

✅ close-report gates passed — every claim verified against the pushed worker branch sandcastle/worker/20261030-125730-11b08b (pre-land — the host promotes it to main only after its post-run verify, #193/#283); issue closed.

envelope
{
  "issue": 320,
  "status": "success",
  "commits": [
    "20ce86bb01c5402bc468235fcea834712f143267"
  ],
  "changed_files": [
    "protected-paths-lib.sh",
    "run-swarm.sh",
    "verify-lib.sh",
    "tests/protected-paths-lib-test.sh",
    "tests/verify-lib-test.sh",
    "docs/journal/2026-10-30-320-pp-snapshot-cache-leak.md"
  ],
  "tests": [
    {
      "command": "bash tests/protected-paths-lib-test.sh",
      "exit_code": 0
    },
    {
      "command": "bash tests/verify-lib-test.sh",
      "exit_code": 0
    },
    {
      "command": "bash tests/run-swarm-stages-test.sh",
      "exit_code": 0
    },
    {
      "command": "bash tests/docs-budget-test.sh",
      "exit_code": 0
    }
  ],
  "criteria": {
    "(a) .sandcastle/go-cache/* and .sandcastle/npm-cache/* are exempt from the protected-path boundary (so pp_snapshot stops cp'ing ~2.8G of cache into /tmp twice per run)": "protected-paths-lib.sh PP_EXEMPT_GLOBS_DEFAULT adds both globs, anchored under .sandcastle/; tests/protected-paths-lib-test.sh asserts pp_is_protected is false for both and that a churn-plus-machinery-edit run leaves both caches un-rolled-back while rolling back the machinery edit (63 checks passed)",
    "(b) the before/after snapshots share one per-run dir the EXIT trap reaps in a single rm": "verify-lib.sh VERIFY_PP_RUNDIR=<tmproot>/run-$$-* holds both before/after; run-swarm.sh on_exit rm -rf's VERIFY_PP_RUNDIR; tests/verify-lib-test.sh asserts the before-snapshot lives under the reapable per-run tree (verify-lib: 13 groups passed)",
    "(b) a SIGKILL-orphaned snapshot dir from a dead run is reaped on entry while a live peer is spared": "verify-lib.sh _verify_pp_reap removes run-<pid>-* dirs whose owning PID is dead; tests/verify-lib-test.sh asserts a dead-PID orphan is reaped and a live-PID peer is NOT",
    "VERIFY_PP_TMPROOT overrides the tmproot for the offline test": "verify-lib.sh _verify_pp_tmproot honours VERIFY_PP_TMPROOT; tests/verify-lib-test.sh exports it under $tmp",
    "proof matches the ticket (protected-paths-lib 63 checks, verify-lib 13 groups, run-swarm-stages 13 groups)": "all three tests exit 0 with the stated counts; full offline suite green except tests/fleet-tui-test.sh and tests/floor-server-test.sh which fail identically on clean HEAD a2f2379 (pre-existing, outside this slice)"
  },
  "summary": "pp_snapshot no longer copies the .sandcastle go-cache/npm-cache host caches into /tmp and both protected-path snapshots now live in one reapable per-run dir that an entry-reaper clears when the owning PID is dead, so a SIGKILL orphan can no longer wall the /tmp tmpfs quota.",
  "blockers": []
}
**close-report** for #320 — status `success` :white_check_mark: **close-report gates passed** — every claim verified against the pushed worker branch `sandcastle/worker/20261030-125730-11b08b` (pre-land — the host promotes it to `main` only after its post-run verify, #193/#283); issue closed. <details><summary>envelope</summary> ```json { "issue": 320, "status": "success", "commits": [ "20ce86bb01c5402bc468235fcea834712f143267" ], "changed_files": [ "protected-paths-lib.sh", "run-swarm.sh", "verify-lib.sh", "tests/protected-paths-lib-test.sh", "tests/verify-lib-test.sh", "docs/journal/2026-10-30-320-pp-snapshot-cache-leak.md" ], "tests": [ { "command": "bash tests/protected-paths-lib-test.sh", "exit_code": 0 }, { "command": "bash tests/verify-lib-test.sh", "exit_code": 0 }, { "command": "bash tests/run-swarm-stages-test.sh", "exit_code": 0 }, { "command": "bash tests/docs-budget-test.sh", "exit_code": 0 } ], "criteria": { "(a) .sandcastle/go-cache/* and .sandcastle/npm-cache/* are exempt from the protected-path boundary (so pp_snapshot stops cp'ing ~2.8G of cache into /tmp twice per run)": "protected-paths-lib.sh PP_EXEMPT_GLOBS_DEFAULT adds both globs, anchored under .sandcastle/; tests/protected-paths-lib-test.sh asserts pp_is_protected is false for both and that a churn-plus-machinery-edit run leaves both caches un-rolled-back while rolling back the machinery edit (63 checks passed)", "(b) the before/after snapshots share one per-run dir the EXIT trap reaps in a single rm": "verify-lib.sh VERIFY_PP_RUNDIR=<tmproot>/run-$$-* holds both before/after; run-swarm.sh on_exit rm -rf's VERIFY_PP_RUNDIR; tests/verify-lib-test.sh asserts the before-snapshot lives under the reapable per-run tree (verify-lib: 13 groups passed)", "(b) a SIGKILL-orphaned snapshot dir from a dead run is reaped on entry while a live peer is spared": "verify-lib.sh _verify_pp_reap removes run-<pid>-* dirs whose owning PID is dead; tests/verify-lib-test.sh asserts a dead-PID orphan is reaped and a live-PID peer is NOT", "VERIFY_PP_TMPROOT overrides the tmproot for the offline test": "verify-lib.sh _verify_pp_tmproot honours VERIFY_PP_TMPROOT; tests/verify-lib-test.sh exports it under $tmp", "proof matches the ticket (protected-paths-lib 63 checks, verify-lib 13 groups, run-swarm-stages 13 groups)": "all three tests exit 0 with the stated counts; full offline suite green except tests/fleet-tui-test.sh and tests/floor-server-test.sh which fail identically on clean HEAD a2f2379 (pre-existing, outside this slice)" }, "summary": "pp_snapshot no longer copies the .sandcastle go-cache/npm-cache host caches into /tmp and both protected-path snapshots now live in one reapable per-run dir that an entry-reaper clears when the owning PID is dead, so a SIGKILL orphan can no longer wall the /tmp tmpfs quota.", "blockers": [] } ``` </details>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Matou/dev-factory#320
No description provided.