omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / tools / quietlock / quiet-guard.sh
| 1 | #!/bin/sh |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | # quiet-guard.sh -- PROPOSED drop-in replacement for ~/.claude/hooks/quiet-guard.sh |
| 18 | # (Claude Code PreToolUse hook). The queen installs it; nothing here edits ~/.claude. |
| 19 | # |
| 20 | # ADVISORY ONLY. This hook is a courtesy filter; it can be fooled (quoted or |
| 21 | # escaped command names, `eval` of built strings, a script path held in a variable, |
| 22 | # programs that start builds without a shell script). It cannot stop work that |
| 23 | # is ALREADY RUNNING when a hold starts (a background suite launched before the |
| 24 | # hold): long suites must call `quietlock check` between stages themselves. The REAL lock is enforced at actual |
| 25 | # entry points: mk/quiet.mk (every omega make goal runs `quietlock check`), |
| 26 | # scripts run under `quietlock run --` / `quietlock hold`, and lanes.sh |
| 27 | # queue/flush/forge once wired to quietlock. Those never use text matching. |
| 28 | # A command this hook lets through by mistake is still stopped by make. |
| 29 | # |
| 30 | # Decision rules |
| 31 | # - DEPRECATED, transition only: QUIET_HOLDER=1 in the command text and an |
| 32 | # "allow <regex>" line in the flag still let a command through, as before. |
| 33 | # - Held or clear is decided by `quietlock check` (not by reading the flag here). |
| 34 | # A stale flag (holder pid dead AND expected_end passed) is first removed by |
| 35 | # `quietlock release-stale`, the same rule as before. |
| 36 | # - Only Bash tool calls are examined. Edit/Write/NotebookEdit and any other |
| 37 | # tool are never blocked. |
| 38 | # - The holder passes: QUIETLOCK_HOLD in this hook's environment, or a |
| 39 | # QUIETLOCK_HOLD=<id> assignment in the command, is handed to `quietlock check`. |
| 40 | # - Heavy = a build/test program at COMMAND POSITION of a shell segment, after |
| 41 | # removing heredoc bodies and quoted text, skipping VAR=value assignments and |
| 42 | # shell keywords (if then elif else do while until ...). After a known wrapper |
| 43 | # (env sudo nice ionice nohup setsid time timeout stdbuf taskset chrt xargs |
| 44 | # exec eval ...) ANY later heavy word blocks, so wrapper options need no |
| 45 | # parsing. For sh/bash/dash/zsh/ksh with any -<letters>c cluster (-c, -lc), |
| 46 | # the command text, quoted or not, is checked. So `echo "make test" >> x.md`, |
| 47 | # `sed -i 's/make/x/' a.md` and heredocs into handoff notes are never blocked. |
| 48 | # - Scripts and test programs (2026-10-08, three overlaps during a GB10 hold): a |
| 49 | # script the command runs (`bash|sh FILE`, `./FILE` or any path at command |
| 50 | # position, `source FILE`, a path after a wrapper) is read, comments dropped, |
| 51 | # and scanned by the same rules, following the scripts it runs up to 3 levels. |
| 52 | # Relative paths resolve against the tool call's cwd, `cd DIR` in the same |
| 53 | # command and the calling script's folder. Test programs are blocked by name: |
| 54 | # target/**/deps/<bin>, tests/<x>, test-*/*_test(s) files run by path, |
| 55 | # test-*.sh / *_test(s).sh / run_tests*.sh. `bash -n FILE` (syntax only) runs |
| 56 | # nothing. The refusal names the holder (from `quietlock check`). |
| 57 | # Needs: POSIX sh, awk, sed, grep, jq; quietlock on PATH or $QUIETLOCK_BIN. |
| 58 | input=$(cat) |
| 59 | tool=$(printf '%s' "$input" | jq -r '.tool_name // ""' 2>/dev/null) |
| 60 | [ "$tool" = Bash ] || exit 0 |
| 61 | cmd=$(printf '%s' "$input" | jq -r '.tool_input.command // ""' 2>/dev/null) |
| 62 | [ -n "$cmd" ] || exit 0 |
| 63 | |
| 64 | QL=${QUIETLOCK_BIN:-$(command -v quietlock 2>/dev/null || echo "$HOME/.local/bin/quietlock")} |
| 65 | hold=$(printf '%s\n' "$cmd" | grep -o 'QUIETLOCK_HOLD=[A-Za-z0-9._-]*' | head -1 | cut -d= -f2) |
| 66 | if command -v "$QL" >/dev/null 2>&1; then |
| 67 | "$QL" release-stale >/dev/null 2>&1 |
| 68 | if [ -n "$hold" ]; then |
| 69 | msg=$(QUIETLOCK_HOLD=$hold "$QL" check 2>&1) |
| 70 | else |
| 71 | msg=$("$QL" check 2>&1) |
| 72 | fi |
| 73 | [ $? = 0 ] && exit 0 |
| 74 | else |
| 75 | flag=${QUIETLOCK_DIR:-$HOME/workspace}/.spark-quiet |
| 76 | [ -f "$flag" ] || exit 0 |
| 77 | msg="quietlock is not installed; quiet flag present: $(head -1 "$flag")" |
| 78 | fi |
| 79 | |
| 80 | # DEPRECATED transition escapes from the old hook (remove after the transition): |
| 81 | # QUIET_HOLDER=1 in the command text, or an "allow <regex>" line in the flag. |
| 82 | flag=${QUIETLOCK_DIR:-$HOME/workspace}/.spark-quiet |
| 83 | case "$cmd" in *QUIET_HOLDER=1*) echo "quiet-guard: allowed by QUIET_HOLDER=1 (DEPRECATED, use quietlock hold)" >&2; exit 0;; esac |
| 84 | allow=$(grep -m1 "^allow " "$flag" 2>/dev/null | cut -d" " -f2-) |
| 85 | if [ -n "$allow" ] && printf "%s" "$cmd" | grep -Eq -- "$allow"; then |
| 86 | echo "quiet-guard: allowed by the flag allow line (DEPRECATED, use quietlock hold)" >&2; exit 0 |
| 87 | fi |
| 88 | |
| 89 | # The working directory of the tool call (relative script paths resolve against it). |
| 90 | cwd=$(printf '%s' "$input" | jq -r '.cwd // ""' 2>/dev/null) |
| 91 | [ -n "$cwd" ] || cwd=$PWD |
| 92 | |
| 93 | # scan_text DEPTH BASEDIR... (text on stdin): print what makes the text heavy, or nothing. |
| 94 | # Same rules for a typed command and for the text of a script it runs (2026-10-08: a script |
| 95 | # that runs cargo test inside, started as `bash scripts/x.sh`, passed the command-word check). |
| 96 | scan_text() { |
| 97 | _depth=$1; shift |
| 98 | # Strip heredoc bodies, collect `-c '...'` / `-c "..."` texts, strip quoted text. |
| 99 | # Comments are dropped only in script files (depth > 1): in a typed command a "#" may sit |
| 100 | # inside an unbalanced quote, and the command-word check never needed it. |
| 101 | _body=$(awk -v nocomment=$((_depth > 1)) ' |
| 102 | function delim(s, d) { |
| 103 | d = s; sub(/.*<<-?[ \t]*/, "", d); sub(/[ \t;|&].*/, "", d); gsub(/["\047\\]/, "", d); return d |
| 104 | } |
| 105 | stop != "" { t = $0; if (strip) sub(/^\t+/, "", t); if (t == stop) stop = ""; next } |
| 106 | nocomment && /^[ \t]*#/ { next } |
| 107 | { print } |
| 108 | /<<-?[ \t]*["\047]?[A-Za-z_][A-Za-z0-9_]*/ && $0 !~ /<<</ { strip = ($0 ~ /<<-/); stop = delim($0) }') |
| 109 | # Queueing is not running (queen 2026-10-01): a lanes.sh queue / queue-light / idea / ledger / |
| 110 | # brief / status call only writes a line for the forge, so its arguments (the queued command text) |
| 111 | # are data. Each such call is replaced by ':' up to the next unquoted separator, so any command |
| 112 | # AFTER it (`lanes.sh queue x d 'make'; make`) is still scanned. Only single-quoted args and |
| 113 | # double-quoted args without $( or backquote are swallowed; anything else stays and is scanned. |
| 114 | _body=$(printf '%s\n' "$_body" | sed -E \ |
| 115 | "s#(^|[;&|(][[:space:]]*)(([A-Za-z_][A-Za-z0-9_]*=[^ ;&|]* +)*)((ba|da)?sh +)?[^ ;&|'\"]*lanes\\.sh +(queue|queue-light|idea|ledger|brief|status)( +('[^']*'|\"[^\"\$\`]*\"|[^ ;&|'\"()\`\$]+))*#\\1\\2:#g") |
| 116 | # Quoted text after a shell's -<letters>c flag cluster (sh -c, bash -lc, dash -xc ...) is a |
| 117 | # command: collect it. Only after a shell name, so `grep -c "make" notes.md` stays allowed. |
| 118 | _inner=$(printf '%s\n' "$_body" | tr '\n' '\001' \ |
| 119 | | grep -a -o -E -e "(^|[^A-Za-z0-9_])(sh|bash|dash|zsh|ksh)( +-[A-Za-z]+)* +-[A-Za-z]*c[A-Za-z]* +'[^']*'" \ |
| 120 | -e '(^|[^A-Za-z0-9_])(sh|bash|dash|zsh|ksh)( +-[A-Za-z]+)* +-[A-Za-z]*c[A-Za-z]* +"[^"]*"' \ |
| 121 | | sed -e "s/^[^'\"]*.//" -e 's/.$//' | tr '\001' '\n') |
| 122 | # A quoted single word holding a slash is a path ("$HOME/x/run.sh"): keep it, unquoted. |
| 123 | _scan=$( { printf '%s\n' "$_body" | unquote_paths | strip_quotes; printf '%s\n' "$_inner" | unquote_paths | strip_quotes; } \ |
| 124 | | if [ "$_depth" -gt 1 ]; then sed -E 's/(^|[[:space:]])#.*$/\1/'; else cat; fi) |
| 125 | _out=$(printf '%s\n' "$_scan" | awk "$AWK_HEAVY") |
| 126 | _h=$(printf '%s\n' "$_out" | grep -v -E '^(SCRIPT|CD) ' | head -1) |
| 127 | if [ -n "$_h" ]; then printf '%s\n' "$_h"; return; fi |
| 128 | [ "$_depth" -lt 3 ] || return |
| 129 | _cds=$(printf '%s\n' "$_out" | sed -n 's/^CD //p') |
| 130 | printf '%s\n' "$_out" | sed -n 's/^SCRIPT //p' | head -20 | while IFS= read -r _w; do |
| 131 | _f=$(resolve "$_w" "$@" $_cds) || continue |
| 132 | _h=$(scan_text $((_depth + 1)) "$(dirname "$_f")" "$@" < "$_f") |
| 133 | if [ -n "$_h" ]; then |
| 134 | case $_h in *" (in script "*) printf '%s\n' "$_h";; *) printf '%s (in script %s)\n' "$_h" "$_f";; esac |
| 135 | break |
| 136 | fi |
| 137 | done |
| 138 | } |
| 139 | # Flatten newlines to \001 while stripping, so quotes may span lines; then restore. |
| 140 | strip_quotes() { tr '\n' '\001' | sed -e "s/'[^']*'//g" -e 's/"[^"]*"//g' | tr '\001' '\n'; } |
| 141 | unquote_paths() { sed -E -e "s#'([A-Za-z0-9_.~\${}+@%=,-]*/[A-Za-z0-9_./~\${}+@%=,-]*)'#\\1#g" -e "s#\"([A-Za-z0-9_.~\${}+@%=,-]*/[A-Za-z0-9_./~\${}+@%=,-]*)\"#\\1#g"; } |
| 142 | # resolve WORD BASEDIR...: print the readable script file WORD names (absolute, or relative to one |
| 143 | # of the base dirs), or fail. A script is a regular text file starting with #! or named *.sh/*.bash, |
| 144 | # under 1 MiB. ~ and $HOME / ${HOME} are expanded; any other $ makes the word unresolvable. |
| 145 | resolve() { |
| 146 | _w=$1; shift |
| 147 | case $_w in "~/"*) _w=$HOME/${_w#\~/};; "\$HOME/"*) _w=$HOME/${_w#\$HOME/};; "\${HOME}/"*) _w=$HOME/${_w#\$\{HOME\}/};; esac |
| 148 | case $_w in *'$'*|'') return 1;; esac |
| 149 | case $_w in /*) set -- "$_w";; *) _r=$_w; _l=""; for _b in "$@"; do |
| 150 | case $_b in "~/"*) _b=$HOME/${_b#\~/};; /*) ;; *) _b=$cwd/$_b;; esac; _l="$_l |
| 151 | $_b/$_r"; done; set -- $(printf '%s\n' "$_l" | sed '/^$/d');; |
| 152 | esac |
| 153 | for _p in "$@"; do |
| 154 | [ -f "$_p" ] && [ -r "$_p" ] || continue |
| 155 | [ "$(wc -c < "$_p")" -le 1048576 ] || continue |
| 156 | case $_p in *.sh|*.bash) printf '%s\n' "$_p"; return 0;; esac |
| 157 | [ "$(head -c 2 "$_p")" = '#!' ] && { printf '%s\n' "$_p"; return 0; } |
| 158 | done |
| 159 | return 1 |
| 160 | } |
| 161 | |
| 162 | # Per shell segment: skip VAR=value words and shell keywords; the next word is the command. |
| 163 | # - Known wrapper (env sudo nice ...): block if ANY later word is heavy (no per-option parsing); |
| 164 | # a later shell word is handled like a shell at command position; a later word with a slash is |
| 165 | # a script to look into. |
| 166 | # - Shell (sh bash dash zsh ksh) with a -<letters>c cluster: the next unquoted word is a command. |
| 167 | # A shell with a file argument runs that file: blocked by name (test program) or looked into |
| 168 | # (SCRIPT line); -n (syntax check only) runs nothing. |
| 169 | # - source / . FILE: the file is looked into. |
| 170 | # - A command word with a slash runs that file: blocked by name or looked into. |
| 171 | # - `cd DIR` segments are printed (CD lines) so `cd d && bash x.sh` finds d/x.sh. |
| 172 | # Test programs by name: target/**/deps/<bin>, tests/<x> or test-*/ *_test(s) files run by path, |
| 173 | # and test-*.sh / *_test(s).sh / run_tests*.sh scripts. |
| 174 | AWK_HEAVY=' |
| 175 | BEGIN { |
| 176 | wrap = " env sudo doas nice ionice nohup setsid time timeout stdbuf taskset chrt xargs exec command builtin eval watch " |
| 177 | kw = " if then elif else fi do done while until for in case esac ! { } " |
| 178 | shells = " sh bash dash zsh ksh " |
| 179 | interps = " python python3 node perl ruby " |
| 180 | } |
| 181 | function interpargs(k, m, j) { # w[k] is an interpreter (python3, $PY ...): its test file or test module |
| 182 | for (j = k + 1; j <= m; j++) { |
| 183 | if (w[j] == "-m" && w[j+1] ~ /^(pytest|unittest)$/) return w[k] " -m " w[j+1] |
| 184 | if (w[j] ~ /^-[A-Za-z0-9.]*[ecE]/) return "" # inline program (perl -pi -e, python3 -c): files are data |
| 185 | if (w[j] ~ /^[-<>0-9]/) continue |
| 186 | return testname(w[j]) ? w[j] " (test program, run by " w[k] ")" : "" |
| 187 | } |
| 188 | return "" |
| 189 | } |
| 190 | function isinterp(c) { return index(interps, " " base(c) " ") || c ~ /^\$[A-Za-z_]/ } |
| 191 | function base(s) { sub(/.*\//, "", s); return s } |
| 192 | function isheavy(c, nxt, notest, b) { |
| 193 | b = base(c) |
| 194 | if (b ~ /^(make|gmake|cmake|ninja|ctest|gcc|g\+\+|clang|clang\+\+|cc|rustc|verify_all\.sh|est_load.*|run_gate.*|run_.*gates\.sh)$/ || b ~ /^qemu-system/) return c |
| 195 | if (b == "cargo" && nxt ~ /^(build|test|run|bench|clippy|nextest)$/) return c " " nxt |
| 196 | if (c ~ /^\.\/test[-_a-z0-9]*$/) return c |
| 197 | if (!notest && testname(c)) return c " (test program)" |
| 198 | return "" |
| 199 | } |
| 200 | function testname(c, b) { # a test program or test script, by name |
| 201 | b = base(c) |
| 202 | if (c ~ /(^|\/)target\/(.*\/)?deps\/[^\/]+$/) return 1 |
| 203 | if (b ~ /^(test[-_].*|.*[-_]tests?|run[-_]?tests.*)\.(sh|bash|py)$/) return 1 |
| 204 | if (index(c, "/") && (c ~ /(^|\/)tests?\/[^\/]/ || b ~ /^tests?[-_]/ || b ~ /[-_]tests?$/)) return 1 |
| 205 | return 0 |
| 206 | } |
| 207 | function shellargs(k, m, j, h) { # w[k] is a shell; return heavy text or "" (prints SCRIPT) |
| 208 | for (j = k + 1; j <= m; j++) { |
| 209 | if (w[j] ~ /^-[A-Za-z]*c[A-Za-z]*$/) { h = isheavy(w[j+1], w[j+2]); return h == "" ? "" : h " (in " base(w[k]) " " w[j] ")" } |
| 210 | if (w[j] ~ /^-[A-Za-z]*n[A-Za-z]*$/) return "" |
| 211 | if (w[j] ~ /^[-+]o$/) { j++; continue } |
| 212 | if (w[j] ~ /^[-+]/) continue |
| 213 | if (w[j] == "<") { if (w[j+1] != "") print "SCRIPT " w[j+1]; return "" } # bash < FILE runs FILE |
| 214 | if (w[j] ~ /^<[^<]/) { print "SCRIPT " substr(w[j], 2); return "" } |
| 215 | if (w[j] ~ /^[0-9]*[<>]/) { if (w[j] ~ /^[0-9]*[<>]+&?$/) j++; continue } |
| 216 | if (testname(w[j])) return w[j] " (test script, run by " base(w[k]) ")" |
| 217 | print "SCRIPT " w[j]; return "" |
| 218 | } |
| 219 | return "" |
| 220 | } |
| 221 | { |
| 222 | # Text after ")" or "}" is not a command position for the new file rules (X=$(cd a)/tests/f, |
| 223 | # cmp <(...) tests/f, ${HOME}/tests/f): such segments start with \002 and get only the |
| 224 | # command-word check the hook always had. |
| 225 | s = $0 # ${NAME} -> $NAME first, so its "}" does not end a command |
| 226 | while (match(s, /\$\{[A-Za-z_][A-Za-z0-9_]*\}/)) s = substr(s, 1, RSTART) substr(s, RSTART + 2, RLENGTH - 3) substr(s, RSTART + RLENGTH) |
| 227 | $0 = s |
| 228 | gsub(/[)}]/, "\n\002") |
| 229 | gsub(/\$\(|`|\(|\{|&&|\|\||[;&|!]/, "\n") |
| 230 | n = split($0, seg, "\n") |
| 231 | for (i = 1; i <= n; i++) { |
| 232 | closed = (substr(seg[i], 1, 1) == "\002"); if (closed) seg[i] = substr(seg[i], 2) |
| 233 | m = split(seg[i], w, /[ \t]+/); k = 1 |
| 234 | # cargo -q test, cargo +nightly test: the subcommand is the first word that is not an option. |
| 235 | for (q = 1; q < m; q++) if (base(w[q]) == "cargo") { r = q + 1; while (r <= m && w[r] ~ /^[-+]/) r++; if (r > q + 1 && r <= m) w[q+1] = w[r] } |
| 236 | while (k <= m && (w[k] == "" || w[k] ~ /^[A-Za-z_][A-Za-z0-9_]*=/ || index(kw, " " w[k] " "))) k++ |
| 237 | if (k > m) continue |
| 238 | if (closed) { h = isheavy(w[k], w[k+1], 1); if (h != "") { print h; exit }; continue } |
| 239 | h = isheavy(w[k], w[k+1]); if (h != "") { print h; exit } |
| 240 | b = base(w[k]) |
| 241 | if ((b == "command" || b == "type" || b == "which") && w[k+1] ~ /^-[vVp]*$/) continue # lookup, not a run |
| 242 | if (b == "cd" && w[k+1] != "" && w[k+1] !~ /^-/) { print "CD " w[k+1]; continue } |
| 243 | if (b == "source" || b == ".") { if (w[k+1] != "") print "SCRIPT " w[k+1]; continue } |
| 244 | if (index(wrap, " " b " ")) { |
| 245 | # Any later heavy word blocks (as always). The file rules apply only to the wrapped |
| 246 | # command itself: the first word that is not an option, an option argument, a |
| 247 | # number (timeout 60, taskset 0-3), VAR=value, another wrapper or a redirect, so |
| 248 | # `timeout 5 cat tests/a.txt` and `nohup x > tests/out.log` stay allowed. |
| 249 | found = 0; skip = 0 |
| 250 | for (j = k + 1; j <= m; j++) { |
| 251 | h = isheavy(w[j], w[j+1], 1); if (h != "") { print h " (after " b ")"; exit } |
| 252 | if (found) continue |
| 253 | if (skip) { skip = 0; continue } # an option argument or redirect target (still checked above) |
| 254 | if (w[j] ~ /^[0-9]*[<>]/) { if (w[j] ~ /^[0-9]*[<>]+&?$/) skip = 1; continue } |
| 255 | if (w[j] ~ /^-/) { if (w[j] ~ /^-[ugncskIPLdp]$/) skip = 1; continue } |
| 256 | if (w[j] ~ /^[0-9][0-9a-fx.,:-]*[smhd]?$/ || w[j] ~ /^[A-Za-z_][A-Za-z0-9_]*=/ || index(wrap, " " base(w[j]) " ")) continue |
| 257 | found = 1 |
| 258 | if (index(shells, " " base(w[j]) " ")) { h = shellargs(j, m); if (h != "") { print h " (after " b ")"; exit }; continue } |
| 259 | if (isinterp(w[j])) { h = interpargs(j, m); if (h != "") { print h " (after " b ")"; exit }; continue } |
| 260 | if (testname(w[j])) { print w[j] " (test program) (after " b ")"; exit } |
| 261 | if (index(w[j], "/")) print "SCRIPT " w[j] |
| 262 | } |
| 263 | } else if (index(shells, " " b " ")) { |
| 264 | h = shellargs(k, m); if (h != "") { print h; exit } |
| 265 | } else if (isinterp(w[k])) { |
| 266 | h = interpargs(k, m); if (h != "") { print h; exit } |
| 267 | } else if (index(w[k], "/")) { |
| 268 | print "SCRIPT " w[k] |
| 269 | } |
| 270 | } |
| 271 | }' |
| 272 | heavy=$(printf '%s\n' "$cmd" | scan_text 1 "$cwd") |
| 273 | [ -n "$heavy" ] || exit 0 |
| 274 | echo "QUIETLOCK_REFUSED BLOCKED by quiet-guard: '$heavy' is a build/test command and the Spark quiet flag is held by someone else. $msg Do code-only work or wait. The holder runs its commands under 'quietlock hold' (or prefixes QUIETLOCK_HOLD=<its hold id>)." >&2 |
| 275 | exit 2 |