From 4b6c481930c9661fbe5a59a1d85be3de0ca276a9 Mon Sep 17 00:00:00 2001 From: Jonathan Mohrbacher Date: Wed, 2 Dec 2026 15:14:27 -0500 Subject: [PATCH 1/3] [NO-JIRA] bazel-reap: reclaim idle container Bazel output bases under ~/.local/state/lgtm/mono/bazel The lgtm-mono docker container (the lgtm review service) runs Bazel with --output_user_root=/home/dev/.local/state/lgtm/mono/bazel, bind-mounted at the same path inside the container. It creates one output base (3-5 GB) per food-truck/mono PR worktree (/home/dev/projects/mono/.worktrees/lgtm-pr-). The service frees a base only when its PR merges, so unmerged or closed PRs leave bases behind forever, and bursts can fill the root disk. Adds a container-root pass to bazel-reap: - Root seam BAZEL_REAP_CONTAINER_BASE (default ~/.local/state/lgtm/mono/bazel); missing root is a clean no-op. - Policy: reap when idle >= 6h (seam BAZEL_REAP_CONTAINER_IDLE_MIN, default 360), regardless of disk pressure. - Candidates: direct children named exactly 32 lowercase hex chars; skips cache/, install/, _reaping-*, and non-hex. - Server liveness: server.pid.txt is ignored (pid is in container namespace). Uses /proc probe (bazelReapInUsePy) on host to check cwd and open fds. Probe failure ("error") keeps the base. Live-verified against lgtm-mono JVM. - Idle age: newest mtime among /command.log, /server/, and /DO_NOT_BUILD_HERE. If none exists, keep. - Deletion: rename-then-delete (_reaping-.*), retries stale _reaping-* dirs (including those left by lgtm-mono's merge reaper). Honours DRY_RUN. - Shared per-pass budget MAX_PER_PASS. - 9 new regression tests in users/dev/test-bazel-reaper.sh (66 -> 91 passing). Updated anti-vacuity pin in flake.nix bazel-reaper-tests check. --- flake.nix | 8 +- users/dev/disk-cleanup.nix | 271 +++++++++++++++++++++++++++++++-- users/dev/test-bazel-reaper.sh | 172 ++++++++++++++++++++- 3 files changed, 432 insertions(+), 19 deletions(-) diff --git a/flake.nix b/flake.nix index 17c44039..29b533e1 100644 --- a/flake.nix +++ b/flake.nix @@ -1562,15 +1562,15 @@ cd ${self} export HOME="$TMPDIR" bash users/dev/test-bazel-reaper.sh 2>&1 | tee "$TMPDIR/br.txt" - grep -q '^=== 66 passed, 0 failed ===' "$TMPDIR/br.txt" || { - echo "GATE FAILURE: bazel-reap suite did not reach its 66/0 tally." >&2 + grep -q '^=== 91 passed, 0 failed ===' "$TMPDIR/br.txt" || { + echo "GATE FAILURE: bazel-reap suite did not reach its 91/0 tally." >&2 exit 1 } # Same anti-vacuity pin as its neighbours: the tally is printed by the # suite, so a suite gutted to three assertions still prints a truthful # "3 passed, 0 failed". The count is what makes deletion visible. - [ "$(grep -c '^ PASS: ' "$TMPDIR/br.txt")" = 66 ] || { - echo "GATE FAILURE: expected 66 'PASS' lines, got" \ + [ "$(grep -c '^ PASS: ' "$TMPDIR/br.txt")" = 91 ] || { + echo "GATE FAILURE: expected 91 'PASS' lines, got" \ "$(grep -c '^ PASS: ' "$TMPDIR/br.txt")." >&2 exit 1 } diff --git a/users/dev/disk-cleanup.nix b/users/dev/disk-cleanup.nix index 016df72b..75506407 100644 --- a/users/dev/disk-cleanup.nix +++ b/users/dev/disk-cleanup.nix @@ -2791,6 +2791,45 @@ lib.mkMerge [ # workspace updated within the idle window (3h, or 30m for lgtm-pr-*), and # runs pathsInUsePy (/proc probe) immediately before renaming to protect any # in-flight compiler or build processes inside the base. + # + # 2026-12-02 AMENDMENT: THE CONTAINER TIER (LGTM-MONO SERVICE BASES). + # + # WHY THIS TIER EXISTS. + # The lgtm-mono docker container (the lgtm review service) runs Bazel with + # `--output_user_root=/home/dev/.local/state/lgtm/mono/bazel`. That is host + # disk, bind-mounted at the same path inside the container. It creates one + # output base /<32-hex md5 of workspace path>/ per food-truck/mono PR + # worktree (/home/dev/projects/mono/.worktrees/lgtm-pr-), each 3-5 GB. + # The service frees a base only when its PR merges, and nothing else touches + # this root: the nightly cleanup_bazel and bazel-reap's other tiers only know + # ~/.cache/bazel/_bazel_$USER. An unmerged or closed PR would leave its base + # behind forever, and during active review bursts this root grew to 36 GB in + # 30 minutes, filling cloudbox's disk to 100% in the past. + # + # WHY PID LIVENESS IS NOT USED. + # `/server/server.pid.txt` records the PID in the container's PID + # namespace. That number means nothing on the host -- checking it with + # `kill -0` checks a completely unrelated host process or non-existent PID. + # Instead, server liveness is determined via the host /proc probe + # (bazelReapInUsePy / pathsInUsePy), which inspects cwd and open fds across all + # host processes (including the container JVM). If the probe reports in_use + # or fails ("error"), the base is KEPT. + # + # WHY THIS TIER IS NOT PRESSURE-GATED (THE 6 H POLICY). + # Approved policy (Jonathan): reap a base in that root when it has been idle + # for more than 6 hours (360 minutes, seam BAZEL_REAP_CONTAINER_IDLE_MIN) AND + # no bazel server (or any process) is using it, using rename-then-delete, + # regardless of disk pressure. Unlike developer workspaces where an agent + # might be actively coding, review bases belong to finished or paused PR + # reviews. If not merged, they sit indefinitely. Reaping them after 6 hours + # of inactivity prevents unbounded accumulation regardless of disk usage. + # + # IDLE AGE AND GUARDS. + # Idle age is judged by the newest mtime among `/command.log`, + # `/server/`, and `/DO_NOT_BUILD_HERE`. If none of them exists, + # the base is KEPT (cannot prove idle age). The session-table guard + # (sessionOwnsPy) does not apply here because container sessions run in + # docker and are not recorded in the host opencode.db or goose sessions.db. home.file.".local/bin/bazel-reap" = { executable = true; text = '' @@ -2805,6 +2844,7 @@ lib.mkMerge [ # Seams. Nothing shipped overrides these; the suite does. BASE="''${BAZEL_REAP_BASE:-$HOME/.cache/bazel/_bazel_$(whoami)}" + CONTAINER_BASE="''${BAZEL_REAP_CONTAINER_BASE:-$HOME/.local/state/lgtm/mono/bazel}" # Rehearsal: classify exactly as a real pass would, delete nothing, and # say "would reap". Same seam and same rationale as TMP_SCRATCH_DRY_RUN. DRY_RUN="''${BAZEL_REAP_DRY_RUN:-}" @@ -2827,6 +2867,9 @@ lib.mkMerge [ IDLE_MIN="''${BAZEL_REAP_IDLE_MIN:-180}" case "$IDLE_MIN" in ""|*[!0-9]*) IDLE_MIN=180 ;; esac + CONTAINER_IDLE_MIN="''${BAZEL_REAP_CONTAINER_IDLE_MIN:-360}" + case "$CONTAINER_IDLE_MIN" in ""|*[!0-9]*) CONTAINER_IDLE_MIN=360 ;; esac + used_pct="''${BAZEL_REAP_USED_PCT:-}" if [ -n "$used_pct" ]; then used_pct="''${used_pct%%%}" @@ -2850,8 +2893,6 @@ lib.mkMerge [ log() { printf '[bazel-reap] %s\n' "$*"; } - [ -d "$BASE" ] || exit 0 - reaped=0 freed_kb=0 @@ -2865,17 +2906,18 @@ lib.mkMerge [ [ ! -e "$tree" ] } - # --- 1. Finish what an earlier pass started. ------------------------- - # Staged trees are garbage by construction, so this needs no liveness - # reasoning at all -- that decision was made before the rename. - for tree in "$BASE"/"$TRASH_PREFIX"*; do - [ -d "$tree" ] || continue - if purge_tree "$tree"; then - log "purged staged leftover $(basename "$tree")" - else - log "WARN: staged leftover $(basename "$tree") did not fully delete; will retry" - fi - done + if [ -d "$BASE" ]; then + # --- 1. Finish what an earlier pass started in BASE. ----------------- + # Staged trees are garbage by construction, so this needs no liveness + # reasoning at all -- that decision was made before the rename. + for tree in "$BASE"/"$TRASH_PREFIX"*; do + [ -d "$tree" ] || continue + if purge_tree "$tree"; then + log "purged staged leftover $(basename "$tree")" + else + log "WARN: staged leftover $(basename "$tree") did not fully delete; will retry" + fi + done orphan_candidates=() pressure_candidates=() @@ -3161,6 +3203,209 @@ lib.mkMerge [ log "WARN: staged $name but could not fully delete it; will retry next pass" fi done + fi + + # --- 5. Container root pass ($CONTAINER_BASE). ----------------------- + if [ -d "$CONTAINER_BASE" ]; then + # 5a. Finish what an earlier pass started in CONTAINER_BASE. + # Staged trees are garbage by construction. Note: the lgtm-mono service's + # own merge-time reaper uses the same _reaping- prefix on purpose. + for tree in "$CONTAINER_BASE"/"$TRASH_PREFIX"*; do + [ -d "$tree" ] || continue + if purge_tree "$tree"; then + log "purged staged leftover $(basename "$tree") (container)" + else + log "WARN: staged leftover $(basename "$tree") (container) did not fully delete; will retry" + fi + done + + container_candidates=() + + # 5b. Classify each container output base. + for entry in "$CONTAINER_BASE"/*; do + [ -d "$entry" ] || continue + name=$(basename "$entry") + + case "$name" in + install|cache) continue ;; + "$TRASH_PREFIX"*) continue ;; + esac + + # Candidates are only direct children whose names are exactly 32 lowercase hex characters. + if ! [[ "$name" =~ ^[0-9a-f]{32}$ ]]; then + continue + fi + + # Idle age: use the newest mtime among /command.log, /server/, + # and /DO_NOT_BUILD_HERE. If none of them exists, KEEP. + newest_mtime="" + has_target=0 + stat_failed=0 + for probe_file in "$entry/command.log" "$entry/server" "$entry/DO_NOT_BUILD_HERE"; do + [ -e "$probe_file" ] || continue + has_target=1 + mt=$(stat -c %Y "$probe_file" 2>/dev/null || true) + case "$mt" in + ""|*[!0-9]*) + stat_failed=1 + break + ;; + *) + if [ -z "$newest_mtime" ] || [ "$mt" -gt "$newest_mtime" ]; then + newest_mtime="$mt" + fi + ;; + esac + done + + if [ "$has_target" -eq 0 ]; then + log "keep $name (container: no timestamp files)" + continue + fi + + if [ "$stat_failed" -ne 0 ] || [ -z "$newest_mtime" ]; then + log "keep $name (container: could not stat timestamps)" + continue + fi + + now=$(date +%s) + idle_sec=$((now - newest_mtime)) + [ "$idle_sec" -lt 0 ] && idle_sec=0 + idle_min=$((idle_sec / 60)) + + if [ "$idle_min" -lt "$CONTAINER_IDLE_MIN" ]; then + log "keep $name (container: idle ''${idle_min}m < ''${CONTAINER_IDLE_MIN}m)" + continue + fi + + # Server liveness: do NOT trust server/server.pid.txt. Its pid is in the + # container's pid namespace and means nothing on the host. Treat the base + # as in use if the /proc probe says the base path is in use; that catches + # the server JVM's cwd and open fds, which the host's /proc shows at the + # same path. Probe failure ("error") means KEEP. + # Note: the session-table guard (sessionOwnsPy) does not apply here because + # container sessions are not in host session DBs. + proc_dir="''${BAZEL_REAP_PROC:-/proc}" + in_use_verdict=$(${pkgs.python3}/bin/python3 ${bazelReapInUsePy} "$proc_dir" "$entry" 2>/dev/null) + case "$in_use_verdict" in + in_use) + log "keep $name (container: in use by a live process)" + continue + ;; + free) + ;; + *) + log "keep $name (container: error probing /proc before action)" + continue + ;; + esac + + container_candidates+=("$idle_sec|$name|$entry") + done + + # 5c. Drain container candidates (oldest first) with remaining quota. + sorted_container=() + if [ "''${#container_candidates[@]}" -gt 0 ]; then + while IFS= read -r line; do + [ -n "$line" ] && sorted_container+=("$line") + done < <(printf '%s\n' "''${container_candidates[@]}" | sort -t '|' -k1,1nr) + fi + + for item in "''${sorted_container[@]:-}"; do + [ -n "$item" ] || continue + IFS='|' read -r idle_sec name entry <<<"$item" + + if [ "$reaped" -ge "$MAX_PER_PASS" ]; then + log "rate limit reached ($MAX_PER_PASS this pass); $name waits for the next one" + break + fi + + idle_min=$((idle_sec / 60)) + if [ "$idle_min" -ge 60 ]; then + idle_desc="$((idle_min / 60))h" + else + idle_desc="''${idle_min}m" + fi + reap_desc="container: idle $idle_desc" + + if [ -n "$DRY_RUN" ]; then + size_kb=$(du -sk "$entry" 2>/dev/null | cut -f1) + case "''${size_kb:-}" in ""|*[!0-9]*) size_kb=0 ;; esac + log "would reap $name ($((size_kb / 1024)) MB, $reap_desc)" + reaped=$((reaped + 1)) + freed_kb=$((freed_kb + size_kb)) + continue + fi + + # Pre-rename re-check for container candidate: + # 1. Re-check /proc probe immediately before rename + proc_dir="''${BAZEL_REAP_PROC:-/proc}" + in_use_verdict=$(${pkgs.python3}/bin/python3 ${bazelReapInUsePy} "$proc_dir" "$entry" 2>/dev/null) + case "$in_use_verdict" in + in_use) + log "keep $name (container: in use by a live process)" + continue + ;; + free) + ;; + *) + log "keep $name (container: error probing /proc before action)" + continue + ;; + esac + + # 2. Re-check idle age in case a command ran while earlier bases were deleting + newest_mtime="" + has_target=0 + stat_failed=0 + for probe_file in "$entry/command.log" "$entry/server" "$entry/DO_NOT_BUILD_HERE"; do + [ -e "$probe_file" ] || continue + has_target=1 + mt=$(stat -c %Y "$probe_file" 2>/dev/null || true) + case "$mt" in + ""|*[!0-9]*) + stat_failed=1 + break + ;; + *) + if [ -z "$newest_mtime" ] || [ "$mt" -gt "$newest_mtime" ]; then + newest_mtime="$mt" + fi + ;; + esac + done + + if [ "$has_target" -eq 0 ] || [ "$stat_failed" -ne 0 ] || [ -z "$newest_mtime" ]; then + log "keep $name (container: timestamps changed before action)" + continue + fi + + now=$(date +%s) + idle_sec=$((now - newest_mtime)) + [ "$idle_sec" -lt 0 ] && idle_sec=0 + idle_min=$((idle_sec / 60)) + if [ "$idle_min" -lt "$CONTAINER_IDLE_MIN" ]; then + log "keep $name (container: became active before action)" + continue + fi + + staged="$CONTAINER_BASE/$TRASH_PREFIX$name.$$" + if ! mv "$entry" "$staged" 2>/dev/null; then + log "WARN: could not stage $name (container) for deletion; skipping" + continue + fi + + reaped=$((reaped + 1)) + size_kb=$(du -sk "$staged" 2>/dev/null | cut -f1) + case "''${size_kb:-}" in ""|*[!0-9]*) size_kb=0 ;; esac + if purge_tree "$staged"; then + log "reaped $name ($((size_kb / 1024)) MB, $reap_desc)" + freed_kb=$((freed_kb + size_kb)) + else + log "WARN: staged $name (container) but could not fully delete it; will retry next pass" + fi + done + fi if [ "$reaped" -gt 0 ]; then verb="would free" diff --git a/users/dev/test-bazel-reaper.sh b/users/dev/test-bazel-reaper.sh index cf3a7c28..792139a0 100644 --- a/users/dev/test-bazel-reaper.sh +++ b/users/dev/test-bazel-reaper.sh @@ -46,6 +46,10 @@ set -uo pipefail repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)" tmpdir="$(mktemp -d)" +# Isolate BAZEL_REAP_CONTAINER_BASE by default so tests never touch the live +# host root ($HOME/.local/state/lgtm/mono/bazel). +export BAZEL_REAP_CONTAINER_BASE="$tmpdir/no-container-base" + pids=() cleanup() { for p in "${pids[@]:-}"; do @@ -152,12 +156,28 @@ PYEOF run_reaper() { local root="$1"; shift - # PATH_CLAIMS_DIR: never the real ~/.local/state/path-claims; a test that - # wants claims passes its own (later assignments win in env). + # PATH_CLAIMS_DIR / BAZEL_REAP_CONTAINER_BASE: never the real host paths; + # a test that wants them passes its own (later assignments win in env). env BAZEL_REAP_BASE="$root" BAZEL_REAP_USED_PCT="${BAZEL_REAP_USED_PCT-0}" \ + BAZEL_REAP_CONTAINER_BASE="${BAZEL_REAP_CONTAINER_BASE:-$tmpdir/no-container-base}" \ PATH_CLAIMS_DIR="$tmpdir/no-path-claims" "$@" "$script" 2>&1 } +new_container_root() { + local r + r="$(mktemp -d "$tmpdir/croot.XXXXXX")" + printf '%s' "$r" +} + +make_container_base() { + local root="$1" name="$2" age="$3" + local base + base="$(make_base "$root" "$name" "/workspace/not/used")" + echo "log" > "$base/command.log" + touch -d "$age ago" "$base/command.log" "$base/server" "$base/DO_NOT_BUILD_HERE" + printf '%s' "$base" +} + # --------------------------------------------------------------------------- # The KEEP branches. These are the ones that matter. # --------------------------------------------------------------------------- @@ -689,6 +709,145 @@ test_script_is_directly_executable() { "$([ "$(head -c2 "$script")" = '#!' ] && echo yes || echo no)" } +# --------------------------------------------------------------------------- +# Container root tier ($BAZEL_REAP_CONTAINER_BASE). +# --------------------------------------------------------------------------- + +test_container_idle_base_reaped() { + echo "TEST: container base idle > 6h is reaped" + local root croot base out + root="$(new_root)" + croot="$(new_container_root)" + base="$(make_container_base "$croot" 20000000000000000000000000000001 "7 hours")" + out="$(run_reaper "$root" BAZEL_REAP_CONTAINER_BASE="$croot")" + check "container base reaped" "$([ ! -d "$base" ] && echo yes || echo no)" + check "no residue anywhere under container root" \ + "$([ -z "$(find "$croot" -name 'tclIndex' 2>/dev/null)" ] && echo yes || echo no)" + check "reports container reap" \ + "$(grep -q 'reaped 20000000.*container: idle 7h' <<<"$out" && echo yes || echo no)" +} + +test_container_recent_base_kept() { + echo "TEST: container base idle 1h (< 6h) is kept" + local root croot base out + root="$(new_root)" + croot="$(new_container_root)" + base="$(make_container_base "$croot" 20000000000000000000000000000002 "1 hour")" + out="$(run_reaper "$root" BAZEL_REAP_CONTAINER_BASE="$croot")" + check "recent container base survives" "$([ -d "$base" ] && echo yes || echo no)" + check "logged keep with idle reason" \ + "$(grep -q 'keep 20000000.*container: idle.*< 360m' <<<"$out" && echo yes || echo no)" +} + +test_container_in_use_kept() { + echo "TEST: container base in use by process is kept (/proc probe fixture)" + local root croot base out pid execroot + root="$(new_root)" + croot="$(new_container_root)" + base="$(make_container_base "$croot" 20000000000000000000000000000003 "7 hours")" + + execroot="$base/execroot/mono" + mkdir -p "$execroot" + ( cd "$execroot" && sleep 300 ) & + pid=$! + pids+=("$pid") + + out="$(run_reaper "$root" BAZEL_REAP_CONTAINER_BASE="$croot")" + kill "$pid" 2>/dev/null || true + wait "$pid" 2>/dev/null || true + + check "in-use container base survives" "$([ -d "$base" ] && echo yes || echo no)" + check "logged in use by live process" \ + "$(grep -q 'keep 20000000.*container: in use by a live process' <<<"$out" && echo yes || echo no)" +} + +test_container_non_hex_cache_install_untouched() { + echo "TEST: container root ignores non-hex, cache/ and install/" + local root croot out + root="$(new_root)" + croot="$(new_container_root)" + mkdir -p "$croot/cache" "$croot/install" "$croot/not-32-hex" \ + "$croot/1234567890abcdef" "$croot/2000000000000000000000000000000G" \ + "$croot/200000000000000000000000000000041" + touch -d "7 hours ago" "$croot/cache" "$croot/install" "$croot/not-32-hex" \ + "$croot/1234567890abcdef" "$croot/2000000000000000000000000000000G" \ + "$croot/200000000000000000000000000000041" + out="$(run_reaper "$root" BAZEL_REAP_CONTAINER_BASE="$croot")" + check "cache/ untouched" "$([ -d "$croot/cache" ] && echo yes || echo no)" + check "install/ untouched" "$([ -d "$croot/install" ] && echo yes || echo no)" + check "not-32-hex untouched" "$([ -d "$croot/not-32-hex" ] && echo yes || echo no)" + check "short hex untouched" "$([ -d "$croot/1234567890abcdef" ] && echo yes || echo no)" + check "non-hex char untouched" "$([ -d "$croot/2000000000000000000000000000000G" ] && echo yes || echo no)" + check "long hex untouched" "$([ -d "$croot/200000000000000000000000000000041" ] && echo yes || echo no)" +} + +test_container_dry_run_deletes_nothing() { + echo "TEST: container dry run deletes nothing" + local root croot base out + root="$(new_root)" + croot="$(new_container_root)" + base="$(make_container_base "$croot" 20000000000000000000000000000005 "7 hours")" + out="$(run_reaper "$root" BAZEL_REAP_CONTAINER_BASE="$croot" BAZEL_REAP_DRY_RUN=1)" + check "container base still present after rehearsal" "$([ -d "$base" ] && echo yes || echo no)" + check "says 'would reap' container base" \ + "$(grep -q 'would reap 20000000.*container: idle' <<<"$out" && echo yes || echo no)" + check "does not claim to have reaped" \ + "$(! grep -q '^\[bazel-reap\] reaped' <<<"$out" && echo yes || echo no)" +} + +test_container_missing_root_is_fine() { + echo "TEST: absent container base dir is a clean no-op" + local root out rc + root="$(new_root)" + out="$(run_reaper "$root" BAZEL_REAP_CONTAINER_BASE="$tmpdir/nonexistent-container-root")" + rc=$? + check "exits 0 with missing container root" "$([ "$rc" = 0 ] && echo yes || echo no)" +} + +test_container_shares_budget_with_other_tiers() { + echo "TEST: container pass shares MAX_PER_PASS budget with existing tiers" + local root croot base_orphan base_c1 base_c2 out + root="$(new_root)" + croot="$(new_container_root)" + base_orphan="$(make_base "$root" 00000000000000000000000000000001 "$root/gone")" + base_c1="$(make_container_base "$croot" 20000000000000000000000000000001 "8 hours")" + base_c2="$(make_container_base "$croot" 20000000000000000000000000000002 "7 hours")" + out="$(run_reaper "$root" BAZEL_REAP_CONTAINER_BASE="$croot" BAZEL_REAP_MAX=2)" + check "orphan reaped" "$([ ! -d "$base_orphan" ] && echo yes || echo no)" + check "older container base reaped" "$([ ! -d "$base_c1" ] && echo yes || echo no)" + check "newer container base survives" "$([ -d "$base_c2" ] && echo yes || echo no)" + check "says rate limit reached for container candidate" \ + "$(grep -q 'rate limit reached (2 this pass); 20000000000000000000000000000002 waits' <<<"$out" && echo yes || echo no)" +} + +test_container_resumes_staged_leftover() { + echo "TEST: staged _reaping- leftover in container root is purged" + local root croot staged out + root="$(new_root)" + croot="$(new_container_root)" + staged="$croot/_reaping-99999999999999999999999999999999.4242" + mkdir -p "$staged/external/lib" + echo payload > "$staged/external/lib/thing" + chmod a-w "$staged/external/lib" + out="$(run_reaper "$root" BAZEL_REAP_CONTAINER_BASE="$croot")" + check "staged container leftover is purged" "$([ ! -e "$staged" ] && echo yes || echo no)" + check "reports purged staged leftover" \ + "$(grep -q 'purged staged leftover _reaping-99999999.*(container)' <<<"$out" && echo yes || echo no)" +} + +test_container_missing_timestamps_kept() { + echo "TEST: container base with no timestamp files is kept" + local root croot base out + root="$(new_root)" + croot="$(new_container_root)" + base="$croot/20000000000000000000000000000006" + mkdir -p "$base/sub" + out="$(run_reaper "$root" BAZEL_REAP_CONTAINER_BASE="$croot")" + check "base with no timestamp files survives" "$([ -d "$base" ] && echo yes || echo no)" + check "logged no timestamp files" \ + "$(grep -q 'keep 20000000.*container: no timestamp files' <<<"$out" && echo yes || echo no)" +} + # --------------------------------------------------------------------------- echo "=== bazel-reap regression tests ===" @@ -724,6 +883,15 @@ test_goose_session_stale_in_workspace_allows_reap_under_pressure test_goose_db_unreadable_keeps_base_under_pressure test_proc_in_use_keeps_base_under_pressure test_proc_error_keeps_base_under_pressure +test_container_idle_base_reaped +test_container_recent_base_kept +test_container_in_use_kept +test_container_non_hex_cache_install_untouched +test_container_dry_run_deletes_nothing +test_container_missing_root_is_fine +test_container_shares_budget_with_other_tiers +test_container_resumes_staged_leftover +test_container_missing_timestamps_kept echo echo "=== $pass_count passed, $fail_count failed ===" From 8ba419bf17b1fcfdad7eaddbba62758f40ef3f93 Mon Sep 17 00:00:00 2001 From: Jonathan Mohrbacher Date: Wed, 2 Dec 2026 15:40:25 -0500 Subject: [PATCH 2/3] [NO-JIRA] bazel-reap: review fixes for the container tier Skip symlinks (chmod -R would follow one to its target); test the real live signal (an open fd in the base, not a cwd); say plainly the tier bounds accumulation and is not burst relief. Refs: lgtm-oqw2.77 --- flake.nix | 8 ++++---- users/dev/disk-cleanup.nix | 14 +++++++++++--- users/dev/test-bazel-reaper.sh | 24 ++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/flake.nix b/flake.nix index 29b533e1..9185dbf8 100644 --- a/flake.nix +++ b/flake.nix @@ -1562,15 +1562,15 @@ cd ${self} export HOME="$TMPDIR" bash users/dev/test-bazel-reaper.sh 2>&1 | tee "$TMPDIR/br.txt" - grep -q '^=== 91 passed, 0 failed ===' "$TMPDIR/br.txt" || { - echo "GATE FAILURE: bazel-reap suite did not reach its 91/0 tally." >&2 + grep -q '^=== 93 passed, 0 failed ===' "$TMPDIR/br.txt" || { + echo "GATE FAILURE: bazel-reap suite did not reach its 93/0 tally." >&2 exit 1 } # Same anti-vacuity pin as its neighbours: the tally is printed by the # suite, so a suite gutted to three assertions still prints a truthful # "3 passed, 0 failed". The count is what makes deletion visible. - [ "$(grep -c '^ PASS: ' "$TMPDIR/br.txt")" = 91 ] || { - echo "GATE FAILURE: expected 91 'PASS' lines, got" \ + [ "$(grep -c '^ PASS: ' "$TMPDIR/br.txt")" = 93 ] || { + echo "GATE FAILURE: expected 93 'PASS' lines, got" \ "$(grep -c '^ PASS: ' "$TMPDIR/br.txt")." >&2 exit 1 } diff --git a/users/dev/disk-cleanup.nix b/users/dev/disk-cleanup.nix index 75506407..dd08eda6 100644 --- a/users/dev/disk-cleanup.nix +++ b/users/dev/disk-cleanup.nix @@ -2823,6 +2823,10 @@ lib.mkMerge [ # might be actively coding, review bases belong to finished or paused PR # reviews. If not merged, they sit indefinitely. Reaping them after 6 hours # of inactivity prevents unbounded accumulation regardless of disk usage. + # It bounds ACCUMULATION; it is not burst relief. A base under 6 h old is + # reclaimed by nothing but its PR's merge, at any disk level, and the + # pressure tier above does not scan this root. Whether pressure should + # shorten the window here is an open follow-up, not decided. # # IDLE AGE AND GUARDS. # Idle age is judged by the newest mtime among `/command.log`, @@ -3211,6 +3215,7 @@ lib.mkMerge [ # Staged trees are garbage by construction. Note: the lgtm-mono service's # own merge-time reaper uses the same _reaping- prefix on purpose. for tree in "$CONTAINER_BASE"/"$TRASH_PREFIX"*; do + [ -L "$tree" ] && continue [ -d "$tree" ] || continue if purge_tree "$tree"; then log "purged staged leftover $(basename "$tree") (container)" @@ -3223,6 +3228,8 @@ lib.mkMerge [ # 5b. Classify each container output base. for entry in "$CONTAINER_BASE"/*; do + # A symlink is never a base: chmod -R would follow it to its target. + [ -L "$entry" ] && continue [ -d "$entry" ] || continue name=$(basename "$entry") @@ -3280,9 +3287,10 @@ lib.mkMerge [ # Server liveness: do NOT trust server/server.pid.txt. Its pid is in the # container's pid namespace and means nothing on the host. Treat the base - # as in use if the /proc probe says the base path is in use; that catches - # the server JVM's cwd and open fds, which the host's /proc shows at the - # same path. Probe failure ("error") means KEEP. + # as in use if the /proc probe says the base path is in use. Measured live + # (2026-12-02): the container's server JVM has its cwd in the WORKTREE, and + # what it holds in the base is open fds (server/jvm.out, java.log.*; the + # client also holds `lock`), which the host's /proc shows at the same path. Probe failure ("error") means KEEP. # Note: the session-table guard (sessionOwnsPy) does not apply here because # container sessions are not in host session DBs. proc_dir="''${BAZEL_REAP_PROC:-/proc}" diff --git a/users/dev/test-bazel-reaper.sh b/users/dev/test-bazel-reaper.sh index 792139a0..516d3c4d 100644 --- a/users/dev/test-bazel-reaper.sh +++ b/users/dev/test-bazel-reaper.sh @@ -761,6 +761,29 @@ test_container_in_use_kept() { "$(grep -q 'keep 20000000.*container: in use by a live process' <<<"$out" && echo yes || echo no)" } +test_container_fd_in_use_kept() { + echo "TEST: container base held only by an open fd is kept (the live JVM signal)" + local root croot base out pid + root="$(new_root)" + croot="$(new_container_root)" + base="$(make_container_base "$croot" 20000000000000000000000000000013 "7 hours")" + + # The file must exist before the age is set: creating it would bump server/'s mtime. + : >"$base/server/jvm.out" + touch -d "7 hours ago" "$base/server" + ( exec 3>>"$base/server/jvm.out"; cd / && sleep 300 ) & + pid=$! + pids+=("$pid") + + out="$(run_reaper "$root" BAZEL_REAP_CONTAINER_BASE="$croot")" + kill "$pid" 2>/dev/null || true + wait "$pid" 2>/dev/null || true + + check "in-use container base survives" "$([ -d "$base" ] && echo yes || echo no)" + check "logged in use by live process" \ + "$(grep -q 'keep 20000000.*container: in use by a live process' <<<"$out" && echo yes || echo no)" +} + test_container_non_hex_cache_install_untouched() { echo "TEST: container root ignores non-hex, cache/ and install/" local root croot out @@ -886,6 +909,7 @@ test_proc_error_keeps_base_under_pressure test_container_idle_base_reaped test_container_recent_base_kept test_container_in_use_kept +test_container_fd_in_use_kept test_container_non_hex_cache_install_untouched test_container_dry_run_deletes_nothing test_container_missing_root_is_fine From f2aa8e336bbe8bdf98b1c2ba3f8952e16a0be805 Mon Sep 17 00:00:00 2001 From: Jonathan Mohrbacher Date: Wed, 2 Dec 2026 15:54:28 -0500 Subject: [PATCH 3/3] [NO-JIRA] bazel-reap: under disk pressure, the container tier uses the lgtm-pr-* 30m window Measured the hour food-truck/mono moved into the container: 21 bases of 3.4 GB in 45 minutes, one per new PR head, box at 82%. The old service's lgtm-pr-* bases (which these replace one for one) were already reaped at 30 min idle under pressure; the container tier now matches that, and keeps 6 h otherwise. Refs: lgtm-oqw2.77 --- flake.nix | 8 ++++---- users/dev/disk-cleanup.nix | 22 +++++++++++++++------- users/dev/test-bazel-reaper.sh | 18 ++++++++++++++++++ 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/flake.nix b/flake.nix index 9185dbf8..ea05ad5e 100644 --- a/flake.nix +++ b/flake.nix @@ -1562,15 +1562,15 @@ cd ${self} export HOME="$TMPDIR" bash users/dev/test-bazel-reaper.sh 2>&1 | tee "$TMPDIR/br.txt" - grep -q '^=== 93 passed, 0 failed ===' "$TMPDIR/br.txt" || { - echo "GATE FAILURE: bazel-reap suite did not reach its 93/0 tally." >&2 + grep -q '^=== 98 passed, 0 failed ===' "$TMPDIR/br.txt" || { + echo "GATE FAILURE: bazel-reap suite did not reach its 98/0 tally." >&2 exit 1 } # Same anti-vacuity pin as its neighbours: the tally is printed by the # suite, so a suite gutted to three assertions still prints a truthful # "3 passed, 0 failed". The count is what makes deletion visible. - [ "$(grep -c '^ PASS: ' "$TMPDIR/br.txt")" = 93 ] || { - echo "GATE FAILURE: expected 93 'PASS' lines, got" \ + [ "$(grep -c '^ PASS: ' "$TMPDIR/br.txt")" = 98 ] || { + echo "GATE FAILURE: expected 98 'PASS' lines, got" \ "$(grep -c '^ PASS: ' "$TMPDIR/br.txt")." >&2 exit 1 } diff --git a/users/dev/disk-cleanup.nix b/users/dev/disk-cleanup.nix index dd08eda6..88180943 100644 --- a/users/dev/disk-cleanup.nix +++ b/users/dev/disk-cleanup.nix @@ -2823,10 +2823,12 @@ lib.mkMerge [ # might be actively coding, review bases belong to finished or paused PR # reviews. If not merged, they sit indefinitely. Reaping them after 6 hours # of inactivity prevents unbounded accumulation regardless of disk usage. - # It bounds ACCUMULATION; it is not burst relief. A base under 6 h old is - # reclaimed by nothing but its PR's merge, at any disk level, and the - # pressure tier above does not scan this root. Whether pressure should - # shorten the window here is an open follow-up, not decided. + # UNDER PRESSURE (root used >= PRESSURE_PCT), the window shrinks to + # LGTM_IDLE_MIN (30 min), the rule the pressure tier already applies to the + # lgtm-pr-* bases of the old lgtm service, which these bases replace one for + # one. Without it the tier bounds accumulation but gives no burst relief: + # measured the hour food-truck/mono moved here (2026-12-02), 21 bases of + # 3.4 GB appeared in 45 minutes, one per new PR head, with the box at 82%. # # IDLE AGE AND GUARDS. # Idle age is judged by the newest mtime among `/command.log`, @@ -2891,6 +2893,12 @@ lib.mkMerge [ ;; esac + # The container tier's window: 6 h, or the lgtm-pr-* window under pressure. + container_idle_min="$CONTAINER_IDLE_MIN" + if [ "$under_pressure" -eq 1 ] && [ "$LGTM_IDLE_MIN" -lt "$container_idle_min" ]; then + container_idle_min="$LGTM_IDLE_MIN" + fi + # Staging prefix. NOT dot-prefixed: see the header. Must not collide with # a bazel md5 base name, which is 32 hex characters. TRASH_PREFIX="_reaping-" @@ -3280,8 +3288,8 @@ lib.mkMerge [ [ "$idle_sec" -lt 0 ] && idle_sec=0 idle_min=$((idle_sec / 60)) - if [ "$idle_min" -lt "$CONTAINER_IDLE_MIN" ]; then - log "keep $name (container: idle ''${idle_min}m < ''${CONTAINER_IDLE_MIN}m)" + if [ "$idle_min" -lt "$container_idle_min" ]; then + log "keep $name (container: idle ''${idle_min}m < ''${container_idle_min}m)" continue fi @@ -3392,7 +3400,7 @@ lib.mkMerge [ idle_sec=$((now - newest_mtime)) [ "$idle_sec" -lt 0 ] && idle_sec=0 idle_min=$((idle_sec / 60)) - if [ "$idle_min" -lt "$CONTAINER_IDLE_MIN" ]; then + if [ "$idle_min" -lt "$container_idle_min" ]; then log "keep $name (container: became active before action)" continue fi diff --git a/users/dev/test-bazel-reaper.sh b/users/dev/test-bazel-reaper.sh index 516d3c4d..797a3d26 100644 --- a/users/dev/test-bazel-reaper.sh +++ b/users/dev/test-bazel-reaper.sh @@ -739,6 +739,23 @@ test_container_recent_base_kept() { "$(grep -q 'keep 20000000.*container: idle.*< 360m' <<<"$out" && echo yes || echo no)" } +test_container_pressure_shortens_window() { + echo "TEST: under pressure a container base idle 45m is reaped; at 50% it is kept" + local root croot base out + root="$(new_root)" + croot="$(new_container_root)" + base="$(make_container_base "$croot" 20000000000000000000000000000021 "45 minutes")" + out="$(run_reaper "$root" BAZEL_REAP_CONTAINER_BASE="$croot" BAZEL_REAP_USED_PCT=50)" + check "45m base kept without pressure" "$([ -d "$base" ] && echo yes || echo no)" + check "logged the 6h window" "$(grep -q 'container: idle.*< 360m' <<<"$out" && echo yes || echo no)" + out="$(run_reaper "$root" BAZEL_REAP_CONTAINER_BASE="$croot" BAZEL_REAP_USED_PCT=90)" + check "45m base reaped under pressure" "$([ -d "$base" ] && echo no || echo yes)" + base="$(make_container_base "$croot" 20000000000000000000000000000022 "10 minutes")" + out="$(run_reaper "$root" BAZEL_REAP_CONTAINER_BASE="$croot" BAZEL_REAP_USED_PCT=90)" + check "10m base kept under pressure" "$([ -d "$base" ] && echo yes || echo no)" + check "logged the 30m window" "$(grep -q 'container: idle.*< 30m' <<<"$out" && echo yes || echo no)" +} + test_container_in_use_kept() { echo "TEST: container base in use by process is kept (/proc probe fixture)" local root croot base out pid execroot @@ -908,6 +925,7 @@ test_proc_in_use_keeps_base_under_pressure test_proc_error_keeps_base_under_pressure test_container_idle_base_reaped test_container_recent_base_kept +test_container_pressure_shortens_window test_container_in_use_kept test_container_fd_in_use_kept test_container_non_hex_cache_install_untouched