Files
lucisv/tools/guard.sh
T
lucioandClaude Opus 5.5 92875e6c6d
ci / checks (push) Successful in 1m34s
ci / corpus (push) Successful in 38s
TODO §65.3: tools/gates.sh, the release gate; guard.sh enforces memory at every depth and runs with a closed stdin
guard.sh fixes (both ours): the memory sum never saw a process three or
more levels down the tree (a doubled space became an empty entry in the
`ps -p` list, which macOS rejects whole), so cargo -> rustc -> linker and
test-spawned servers were never memory-limited — QA re-proved 1.5 GB at
depth 3/4 missed by master, killed in 2-4 s now; a closed stdin skipped
the command silently, now /dev/null. The stall itself is the host's
(syspolicyd on fresh unsigned binaries).

tools/gates.sh runs fmt, clippy, the test build, one suite per workspace
crate, the doc-tests, the corpus gate and the node tests as separate
guarded steps with kept logs and per-step limits; a guard-killed step is
retried once and shown as KILLED -> retried; usage errors exit 2 (lead
fixes after QA: comments/one-line/glob members, an empty crate list, a
missing option value, an empty selection). RELEASING.md step 3, CLAUDE.md
and the README's Building section point at it.

Ten consecutive green runs recorded (healthy host); full gate on master:
PASS, 15 steps, 5 127 tests, corpus 814/820. QA (Sonnet): PASS.
docs/features/release-gates.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-19 06:13:28 -07:00

80 lines
2.7 KiB
Bash
Executable File

#!/usr/bin/env bash
# Run a command under a memory and wall-clock guard.
#
# Usage: tools/guard.sh [-m MAX_GB] [-t MAX_SECONDS] -- <command...>
# tools/guard.sh -m 8 -t 900 -- cargo test --workspace
#
# Every second the resident set of the command's whole process tree is
# summed; if it exceeds MAX_GB (default 8) or the clock exceeds MAX_SECONDS
# (default 1800) the tree is killed (TERM, then KILL) and the script exits
# 137 with a line saying why. Exists because a runaway test or sweep once
# consumed 100 GB and took the machine down (2026-09-03): agents run every
# test suite, fuzzer and corpus/opentitan sweep through this.
#
# macOS has no enforced `ulimit -v`, so the guard is a poller, not a cap:
# a process that allocates faster than one second's worth of headroom can
# still overshoot briefly. Standard tooling only (ps, awk, kill).
set -u
max_gb=8
max_s=1800
while [ $# -gt 0 ]; do
case $1 in
-m) max_gb=$2; shift 2 ;;
-t) max_s=$2; shift 2 ;;
--) shift; break ;;
*) echo "usage: $0 [-m MAX_GB] [-t MAX_SECONDS] -- <command...>" >&2; exit 2 ;;
esac
done
[ $# -gt 0 ] || { echo "guard.sh: no command" >&2; exit 2; }
# Keep the command's stdin: a plain `cmd &` in a non-interactive shell
# gives the job /dev/null, which silently empties any stdin-fed `lucisv`.
# A CLOSED stdin (`<&-`, some launchers) would make both redirections fail
# and the command never run (exit 1, nothing executed); it gets /dev/null.
{ exec 3<&0; } 2>/dev/null || exec 3</dev/null
"$@" <&3 &
root=$!
start=$(date +%s)
# The command's process tree: descendants by parent pid, transitively.
# Single spaces only: the list becomes `ps -p a,b,c`, and macOS ps rejects
# the WHOLE list on an empty element ("a,,b") — which a doubled space made
# for any tree three levels deep (cargo → rustc → ld, a test spawning
# lucisv), summing 0 kB and silently disabling the memory limit (§65.3).
tree() {
local all="$1" next
while :; do
next=$(ps -axo pid=,ppid= | awk -v set=" $all " '
{ if (index(set, " " $2 " ") > 0 && index(set, " " $1 " ") == 0) printf " %s", $1 }')
[ -z "$next" ] && break
all="$all$next"
done
echo "$all"
}
kill_tree() {
local pids; pids=$(tree "$root")
kill -TERM $pids 2>/dev/null
sleep 2
kill -KILL $pids 2>/dev/null
}
max_kb=$((max_gb * 1024 * 1024))
while kill -0 "$root" 2>/dev/null; do
pids=$(tree "$root")
rss=$(ps -o rss= -p $(echo "$pids" | tr ' ' ',') 2>/dev/null | awk '{s+=$1} END {print s+0}')
now=$(date +%s)
if [ "$rss" -gt "$max_kb" ]; then
echo "guard.sh: memory ${rss} kB exceeded ${max_gb} GB — killing: $*" >&2
kill_tree; exit 137
fi
if [ $((now - start)) -gt "$max_s" ]; then
echo "guard.sh: ${max_s} s exceeded — killing: $*" >&2
kill_tree; exit 137
fi
sleep 1
done
wait "$root"