Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 '^=== 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")" = 66 ] || {
echo "GATE FAILURE: expected 66 '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
}
Expand Down
287 changes: 274 additions & 13 deletions users/dev/disk-cleanup.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2791,6 +2791,51 @@ 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 <root>/<32-hex md5 of workspace path>/ per food-truck/mono PR
# worktree (/home/dev/projects/mono/.worktrees/lgtm-pr-<N>), 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.
# `<base>/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.
# 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 `<base>/command.log`,
# `<base>/server/`, and `<base>/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 = ''
Expand All @@ -2805,6 +2850,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:-}"
Expand All @@ -2827,6 +2873,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%%%}"
Expand All @@ -2844,14 +2893,18 @@ 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-"

log() { printf '[bazel-reap] %s\n' "$*"; }

[ -d "$BASE" ] || exit 0

reaped=0
freed_kb=0

Expand All @@ -2865,17 +2918,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=()
Expand Down Expand Up @@ -3161,6 +3215,213 @@ 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
[ -L "$tree" ] && continue
[ -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
# A symlink is never a base: chmod -R would follow it to its target.
[ -L "$entry" ] && continue
[ -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 <base>/command.log, <base>/server/,
# and <base>/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. 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}"
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"
Expand Down
Loading
Loading