aien-architecture-mv
Canonical architecture for the AIEN ecosystem: flows, boundaries, ADRs, and the map from design to implementation repositories.
Brought by drakestapleton
aien-architecture-mv / tools / ops / quiet-build
| 1 | #!/bin/sh |
| 2 | # quiet-build CMD... Run a build or CPU-heavy job politely. |
| 3 | # Waits while a measurement is pending (flag files in /tmp/aien-quiet.want.d whose |
| 4 | # owner process is alive; at most 1 min, L405), then passes the gate /tmp/aien-quiet.gate (a measurement window |
| 5 | # at the front of the line holds it while the quiet lock drains; L322, 2026-10-21), then holds |
| 6 | # /tmp/aien-quiet.lock shared. Locks already held are never affected by the gate. |
| 7 | # L405 (2026-10-22, Drake: "stop chip locking"): correctness work never waits behind a timing |
| 8 | # lock for long. ALL waiting together (flags + gate + lock) is capped at SPARK_BUILD_MAX_WAIT |
| 9 | # seconds (default 1500 = 25 min: a timing window holds at most 20 min after at most 5 min of |
| 10 | # draining). Past the cap the job runs anyway at nice 19, without the lock, and the overrun is |
| 11 | # written to the open window (its numbers become "shared machine") and to |
| 12 | # ~/.local/state/quiet/intrusions.log. Every wait is logged in ~/.local/state/quiet/waits.log. |
| 13 | # SPARK_QUIET_LOCK / SPARK_QUIET_GATE / SPARK_QUIET_WANT / SPARK_WINDOWS_ROOT / SPARK_BUILD_MAX_WAIT override (tests only). |
| 14 | case "${1-}" in |
| 15 | ""|-h|--help|help) |
| 16 | sed -n '2,/^[^#]/{/^#/s/^# \{0,1\}//p}' "$0" |
| 17 | exit 0 ;; |
| 18 | esac |
| 19 | Q=${SPARK_QUIET_LOCK:-/tmp/aien-quiet.lock}; GATE=${SPARK_QUIET_GATE:-/tmp/aien-quiet.gate} |
| 20 | W=${SPARK_QUIET_WANT:-/tmp/aien-quiet.want.d}; WR=${SPARK_WINDOWS_ROOT:-$HOME/yardsticks/windows} |
| 21 | MAXW=${SPARK_BUILD_MAX_WAIT:-1500} |
| 22 | L=$HOME/.local/state/quiet; mkdir -p "$L" 2>/dev/null |
| 23 | t0=$(date +%s); left() { r=$(( MAXW - ($(date +%s) - t0) )); [ "$r" -gt 0 ] && echo "$r" || echo 0; } |
| 24 | # 1. pending measurements (flags), at most SPARK_FLAG_WAIT s (default 60; was 600 until L405: on 2026-10-22 |
| 25 | # one measurement queued 80 min held 14 new builds here 10 min each) and never past the cap |
| 26 | while [ $(( $(date +%s) - t0 )) -lt "${SPARK_FLAG_WAIT:-60}" ] && [ "$(left)" -gt 0 ]; do |
| 27 | live=0 |
| 28 | for f in "$W"/*; do |
| 29 | [ -e "$f" ] || continue |
| 30 | p=$(basename "$f" | sed 's/^legacy-//') |
| 31 | if ! kill -0 "$p" 2>/dev/null; then rm -f "$f" |
| 32 | # a measurement that has waited over SPARK_FLAG_MAX_AGE s (default 600) has had its chance; it should take a |
| 33 | # spark-window, which waits for a quiet machine (L405: one flag 80 min old paused every new build 60 s) |
| 34 | elif [ $(( $(date +%s) - $(stat -c %Y "$f" 2>/dev/null || date +%s) )) -lt "${SPARK_FLAG_MAX_AGE:-600}" ]; then live=1; fi |
| 35 | done |
| 36 | [ "$live" -eq 0 ] && break |
| 37 | sleep 5 |
| 38 | done |
| 39 | # 2. the gate (fail open: a stuck gate never blocks a build past the cap) |
| 40 | [ -e "$GATE" ] || { : > "$GATE"; chmod 666 "$GATE"; } 2>/dev/null |
| 41 | [ "$(left)" -gt 0 ] && flock -s -w "$(left)" "$GATE" true 2>/dev/null |
| 42 | # 3. the quiet lock, shared, on fd 9 (inherited by CMD, so it is held for CMD's life) |
| 43 | [ -e "$Q" ] || { : > "$Q"; chmod 666 "$Q"; } 2>/dev/null |
| 44 | exec 9<"$Q" |
| 45 | if flock -s -n 9 || { [ "$(left)" -gt 0 ] && flock -s -w "$(left)" 9; }; then |
| 46 | w=$(( $(date +%s) - t0 )) |
| 47 | printf '%s waited_s=%s how=lock %s\n' "$(date -u +%FT%TZ)" "$w" "$*" | cut -c1-300 >> "$L/waits.log" 2>/dev/null |
| 48 | exec "$@" |
| 49 | fi |
| 50 | exec 9<&- |
| 51 | w=$(( $(date +%s) - t0 )) |
| 52 | id=$(cat "$WR/OPEN" 2>/dev/null) |
| 53 | line="$(date -u +%FT%TZ) waited_s=$w cap_s=$MAXW window=${id:-none} pid=$$ running without the quiet lock at nice 19: $*" |
| 54 | printf '%s\n' "$line" | cut -c1-300 >> "$L/intrusions.log" 2>/dev/null |
| 55 | [ -n "$id" ] && [ -d "$WR/$id" ] && printf '%s\n' "$line" | cut -c1-300 >> "$WR/$id/intrusions" 2>/dev/null |
| 56 | printf '%s waited_s=%s how=bypass %s\n' "$(date -u +%FT%TZ)" "$w" "$*" | cut -c1-300 >> "$L/waits.log" 2>/dev/null |
| 57 | echo "quiet-build: waited ${w}s (cap ${MAXW}s) behind a timing lock; running anyway at nice 19 (L405)" >&2 |
| 58 | exec nice -n 19 "$@" |