interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / tests / quietlock / run.sh

717 lines · 44923 bytes Raw Blame History
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# tests/quietlock/run.sh -- negative tests for tools/quietlock (HD-13).
18# POSIX sh. Uses a private temp state dir via QUIETLOCK_DIR; never touches the
19# real ~/workspace/.spark-quiet. Each test lists the code change ("mutation")
20# that makes it fail, so a removed guard cannot pass silently.
21set -u
22HERE=$(cd "$(dirname "$0")/../.." && pwd)
23T=$(mktemp -d "${TMPDIR:-/tmp}/quietlock-test.XXXXXX") || exit 1
24trap 'rm -rf "$T"' EXIT INT TERM
25S=$T/state
26mkdir -p "$S"
27QUIETLOCK_DIR=$S
28export QUIETLOCK_DIR
29# Hermetic: nothing inherited from the runner may change a verdict. The forge that ran round 6 had
30# QUIET_HOLDER=1 in its environment, which (correctly) lets legacy flags through and so failed
31# T10, T15, H7 and H7c. Tests that want these variables set them per command.
32unset QUIETLOCK_HOLD QUIET_HOLDER QUIETLOCK_BIN QUIETLOCK_TEST QUIETLOCK_TEST_MINUTE_SECONDS QUIETLOCK_INSTALL_FAILPOINT
33unset REQUEUE_MAX FORGE_WAIT_SECONDS LANES_LIGHT_IDLE_MIN
34case "$S" in "$HOME"/workspace|"$HOME"/workspace/) echo "FAIL safety: state dir is the real one"; exit 1;; esac
35
36FLAG=$S/.spark-quiet
37HIST=$S/.spark-quiet.history
38APPR=$S/.spark-quiet-approval
39PAST=2000-01-01T00:00:00Z
40FUTURE=2099-01-01T00:00:00Z
41Q=$T/quietlock
42MK="make -s -C $HERE OUT_DIR=$T/build"
43
44${CC:-gcc} -std=gnu11 -Wall -Wextra -Werror -O2 -D_GNU_SOURCE -o "$Q" "$HERE/tools/quietlock/quietlock.c" \
45 || { echo "FAIL build quietlock"; exit 1; }
46
47FAILS=0
48N=0
49check() {
50 N=$((N + 1))
51 if eval "$2"; then echo "ok $1"; else echo "FAIL $1"; FAILS=$((FAILS + 1)); fi
52}
53reset() { rm -f "$FLAG" "$APPR" "$T"/ran*; }
54dead_pid() {
55 sh -c 'exit 0' &
56 p=$!
57 wait "$p"
58 if kill -0 "$p" 2>/dev/null; then echo 0; else echo "$p"; fi
59}
60live_flag() { # someone else holds, pid = this shell (alive)
61 echo "OTHER quietlock test start=$PAST expected_end=$FUTURE pid=$$ hold=qOTHER-1-0" > "$FLAG"
62}
63token() { printf '# POLICY FILE, NOT CRYPTOGRAPHIC (test)\nowner=%s\nmax_minutes=%s\nexpires_at=%s\n' "$1" "$2" "$3" > "$APPR"; }
64
65# --- T0 bootstrap: `make quietlock` builds the tool even while someone else holds.
66# Mutation killed: drop the QUIETLOCK_BOOT_GOALS filter in mk/quiet.mk (check runs for every goal).
67reset; live_flag
68out=$($MK quietlock 2>&1); rc=$?
69check "T0 make quietlock bootstraps while held by another" '[ $rc = 0 ] && [ -x "$T/build/quietlock" ]'
70
71# --- T1 second hold refused while the first is live (nested hold).
72# Mutation killed: in cmd_hold, skip the `if (f.present)` refusal AND publish with rename() instead of link().
73reset
74"$Q" hold --owner A --minutes 1 -- "$Q" hold --owner B --minutes 1 -- touch "$T/ran1" 2>"$T/err1"; rc=$?
75check "T1 second hold refused while first live (exit 75)" '[ $rc = 75 ] && [ ! -e "$T/ran1" ]'
76check "T1 first hold released its flag on exit" '[ ! -e "$FLAG" ]'
77# T1b same against an old-format flag held by a live pid.
78# Mutation killed: same as T1.
79echo "M18 gate16 desc start=$PAST expected_end=$FUTURE pid=$$" > "$FLAG"
80"$Q" hold --owner B --minutes 1 -- touch "$T/ran1b" 2>/dev/null; rc=$?
81check "T1b hold refused over an old-format live flag" '[ $rc = 75 ] && [ ! -e "$T/ran1b" ] && grep -q "pid=$$" "$FLAG"'
82
83# --- T2 make goal refused while held by someone else.
84# Mutation killed: delete the $(error ...) in mk/quiet.mk, delete mk/quiet.mk, or make cmd_check return 0.
85reset; live_flag
86out=$($MK quietlock-check 2>&1); rc=$?
87check "T2 make goal refused while held by another" '[ $rc != 0 ] && echo "$out" | grep -q "HELD" && ! echo "$out" | grep -q "clear for this make run"'
88# T2b a wrong QUIETLOCK_HOLD does not open the lock.
89# Mutation killed: cmd_check accepting any non-empty QUIETLOCK_HOLD.
90out=$(QUIETLOCK_HOLD=qWRONG $MK quietlock-check 2>&1); rc=$?
91check "T2b make refused with a wrong QUIETLOCK_HOLD" '[ $rc != 0 ] && ! echo "$out" | grep -q "clear for this make run"'
92# T2c control: clear flag lets make through (proves T2 is not an unconditional failure).
93# Mutation killed: cmd_check always returning 75.
94reset
95out=$($MK quietlock-check 2>&1); rc=$?
96check "T2c make passes when no flag" '[ $rc = 0 ] && echo "$out" | grep -q "clear for this make run"'
97# T2d run -- CMD refused while held, runs when clear.
98# Mutation killed: cmd `run` exec'ing without calling cmd_check.
99live_flag
100"$Q" run -- touch "$T/ran2d" 2>/dev/null; rc=$?
101check "T2d run refused while held" '[ $rc = 75 ] && [ ! -e "$T/ran2d" ]'
102reset
103"$Q" run -- touch "$T/ran2e"; rc=$?
104check "T2e run executes when clear" '[ $rc = 0 ] && [ -e "$T/ran2e" ]'
105
106# --- T3 the holder's own command passes.
107# Mutation killed: drop setenv("QUIETLOCK_HOLD") in the child, or drop the hold-id match in cmd_check.
108reset
109out=$("$Q" hold --owner A --minutes 1 -- $MK quietlock-check 2>&1); rc=$?
110check "T3 holder's own make passes" '[ $rc = 0 ] && echo "$out" | grep -q "clear for this make run"'
111"$Q" hold --owner A --minutes 1 -- "$Q" check; rc=$?
112check "T3b holder's own check exits 0" '[ $rc = 0 ]'
113"$Q" hold --owner A --minutes 1 -- sh -c 'exit 7'; rc=$?
114check "T3c hold returns the command exit status and releases" '[ $rc = 7 ] && [ ! -e "$FLAG" ]'
115# T3d flag written by hold stays readable by quiet-guard.sh / lanes.sh (same shell parsing).
116# Mutation killed: changing the flag line (drop pid= / expected_end= / move owner out of field 1).
117"$Q" hold --owner A --minutes 5 --reason "two words" -- sh -c 'head -1 "$1" > "$2"; echo $PPID > "$3"' sh "$FLAG" "$T/line" "$T/ppid"
118l=$(cat "$T/line"); p=$(echo "$l" | grep -o "pid=[0-9]*" | cut -d= -f2); e=$(echo "$l" | grep -o "expected_end=[^ ]*" | cut -d= -f2)
119o=$(echo "$l" | awk '{print $1" "$2" "$3}')
120check "T3d flag line compatible with quiet-guard parsing" '[ "$p" = "$(cat "$T/ppid")" ] && [ "$o" = "A quietlock two_words" ] && [ $(( $(date -u -d "$e" +%s) - $(date -u +%s) )) -gt 200 ]'
121
122# --- T4 21-minute hold refused without a token; 20 accepted.
123# Mutations killed: removing the minutes > 20 check (21 runs); changing it to >= 20 (20 refused).
124reset
125"$Q" hold --owner A --minutes 21 -- touch "$T/ran4" 2>/dev/null; rc=$?
126check "T4 21-minute hold refused without token (exit 77)" '[ $rc = 77 ] && [ ! -e "$T/ran4" ] && [ ! -e "$FLAG" ]'
127check "T4 refusal logged to history" 'grep -q "REFUSED 21-minute hold for A" "$HIST"'
128"$Q" hold --owner A --minutes 20 -- touch "$T/ran4b"; rc=$?
129check "T4b 20-minute hold needs no token" '[ $rc = 0 ] && [ -e "$T/ran4b" ]'
130
131# --- T5 refused with expired / wrong-owner / too-small token.
132# Mutations killed: drop the expires_at test; drop the owner compare; drop the max_minutes compare.
133reset; token A 30 "$PAST"
134"$Q" hold --owner A --minutes 25 -- touch "$T/ran5a" 2>/dev/null; rc=$?
135check "T5a expired token refused" '[ $rc = 77 ] && [ ! -e "$T/ran5a" ]'
136token B 30 "$FUTURE"
137"$Q" hold --owner A --minutes 25 -- touch "$T/ran5b" 2>/dev/null; rc=$?
138check "T5b wrong-owner token refused" '[ $rc = 77 ] && [ ! -e "$T/ran5b" ]'
139token A 24 "$FUTURE"
140"$Q" hold --owner A --minutes 25 -- touch "$T/ran5c" 2>/dev/null; rc=$?
141check "T5c too-small token refused" '[ $rc = 77 ] && [ ! -e "$T/ran5c" ]'
142
143# --- T6 accepted with a valid token; the use is logged.
144# Mutations killed: approval_ok always failing; removing the history() "USED" line.
145token A 30 "$FUTURE"
146"$Q" hold --owner A --minutes 25 -- touch "$T/ran6"; rc=$?
147check "T6 valid token accepted" '[ $rc = 0 ] && [ -e "$T/ran6" ] && [ ! -e "$FLAG" ]'
148check "T6 token use appended to history" 'grep -q "approval token USED for 25-minute hold by A" "$HIST"'
149
150# --- T7 release-stale refuses when the pid is alive (even past expected_end).
151# Mutation killed: flag_stale() ignoring pid_alive().
152reset
153echo "M18 gate16 desc start=$PAST expected_end=$PAST pid=$$" > "$FLAG"
154"$Q" release-stale >/dev/null; rc=$?
155check "T7 release-stale refuses live pid" '[ $rc = 3 ] && [ -e "$FLAG" ]'
156
157# --- T8 release-stale refuses when pid dead but expected_end not passed.
158# Mutation killed: flag_stale() ignoring expected_end.
159D=$(dead_pid)
160echo "M18 gate16 desc start=$PAST expected_end=$FUTURE pid=$D" > "$FLAG"
161"$Q" release-stale >/dev/null; rc=$?
162check "T8 release-stale refuses dead pid before end" '[ "$D" != 0 ] && [ $rc = 3 ] && [ -e "$FLAG" ]'
163
164# --- T9 release-stale releases when pid dead AND end passed.
165# Mutation killed: release-stale never unlinking / flag_stale always 0.
166echo "M18 gate16 desc start=$PAST expected_end=$PAST pid=$D" > "$FLAG"
167"$Q" release-stale >/dev/null; rc=$?
168check "T9 release-stale releases dead + past" '[ $rc = 0 ] && [ ! -e "$FLAG" ] && grep -q "released stale flag: M18 gate16 desc" "$HIST"'
169
170# --- T10 old-format pid line still parsed (no hold= token, pid= last).
171# Mutation killed: parsing pid only at a fixed field, or only when hold= is present.
172echo "M18 gate16 desc start=$PAST expected_end=$FUTURE pid=$$" > "$FLAG"
173"$Q" check 2>"$T/err10"; rc=$?
174check "T10 old-format live flag blocks check with parsed pid" '[ $rc = 75 ] && grep -q "pid=$$ alive=yes" "$T/err10" && grep -q "holder=.M18 gate16 desc." "$T/err10"'
175echo "M18 gate16 desc start=$PAST expected_end=$PAST pid=$D" > "$FLAG"
176"$Q" check 2>/dev/null; rc=$?
177check "T10b old-format stale flag counts as clear for check" '[ $rc = 0 ]'
178
179# --- T2f refusals carry the fixed marker the forge keys on.
180# Mutation killed: dropping "QUIETLOCK_REFUSED" from the cmd_check message.
181live_flag
182"$Q" check 2>"$T/err2f"; rc=$?
183check "T2f refusal prints QUIETLOCK_REFUSED and exits 75" '[ $rc = 75 ] && grep -q QUIETLOCK_REFUSED "$T/err2f"'
184
185# --- T11 no state dir (GitHub CI, fresh machine) is clear, never an error.
186# Mutation killed: removing the state_dir_missing() branch in cmd_check (lock_take dies, exit 1).
187out=$(QUIETLOCK_DIR=$T/nonexistent $MK quietlock-check 2>&1); rc=$?
188check "T11 make passes with no state dir" '[ $rc = 0 ] && echo "$out" | grep -q "clear for this make run" && [ ! -e "$T/nonexistent" ]'
189QUIETLOCK_DIR=$T/nonexistent "$Q" release-stale >/dev/null; rc=$?
190check "T11b release-stale with no state dir says no flag" '[ $rc = 0 ] && [ ! -e "$T/nonexistent" ]'
191
192# --- T11c/d tool cannot be built: continue with NOTICE when no flag, refuse when a flag exists.
193# Mutations killed: always continuing on build failure (T11d); always refusing (T11c).
194reset
195out=$(make -s -C "$HERE" OUT_DIR="$T/build2" CC=false quietlock-check 2>&1); rc=$?
196check "T11c unbuildable tool + no flag: NOTICE and continue" '[ $rc = 0 ] && echo "$out" | grep -q NOTICE'
197live_flag
198out=$(make -s -C "$HERE" OUT_DIR="$T/build2" CC=false quietlock-check 2>&1); rc=$?
199check "T11d unbuildable tool + flag: refused" '[ $rc != 0 ] && echo "$out" | grep -q QUIETLOCK_REFUSED'
200
201# --- T11e on-demand build writes a temp file and renames it (no half-written binary).
202# Mutation killed: compiling straight to $(QUIETLOCK_BIN) in mk/quiet.mk.
203reset
204printf '#!/bin/sh\necho "$@" >> "%s"\nexec %s "$@"\n' "$T/cc.log" "${CC:-gcc}" > "$T/ccspy"; chmod +x "$T/ccspy"
205out=$(make -s -C "$HERE" OUT_DIR="$T/build3" CC="$T/ccspy" quietlock-check 2>&1); rc=$?
206check "T11e on-demand build goes through a temp name" '[ $rc = 0 ] && grep -q -- "-o $T/build3/quietlock.tmp.[0-9]" "$T/cc.log" && [ -x "$T/build3/quietlock" ] && ! ls "$T/build3" | grep -q tmp'
207
208# --- T12 legacy QUIET_HOLDER=1 passes only a legacy flag (no hold=), logged as deprecated.
209# Mutations killed: removing the legacy branch (T12 fails); dropping its `!f.hold[0]` guard (T12b fails).
210echo "M18 gate16 desc start=$PAST expected_end=$FUTURE pid=$$" > "$FLAG"
211QUIET_HOLDER=1 "$Q" check 2>/dev/null; rc=$?
212check "T12 QUIET_HOLDER=1 passes a legacy flag, logged" '[ $rc = 0 ] && grep -q "DEPRECATED QUIET_HOLDER=1" "$HIST"'
213live_flag
214QUIET_HOLDER=1 "$Q" check 2>/dev/null; rc=$?
215check "T12b QUIET_HOLDER=1 does not pass a quietlock hold" '[ $rc = 75 ]'
216
217# --- T13 overrun (Drake 2026-10-01: expected_end is ADVISORY). A 1-"minute" hold around a longer
218# command KEEPS the flag past expected_end while the command runs, logs one "overrun" warning, and
219# releases the flag only when the command exits. The command is never killed.
220# (QUIETLOCK_TEST_MINUTE_SECONDS=2 shortens a minute to 2 s; honoured only with QUIETLOCK_TEST=1.)
221# Mutations killed: releasing the flag at expected_end (T13a flag gone, check 0, second hold wins);
222# dropping the overrun warning (T13a grep fails); not releasing at command exit (T13b flag stays);
223# killing the child at overrun (T13a kill -0 fails, T13b rc/done fail).
224reset
225QUIETLOCK_TEST=1 QUIETLOCK_TEST_MINUTE_SECONDS=2 "$Q" hold --owner A --minutes 1 -- \
226 sh -c 'touch "$1/started"; while [ ! -e "$1/go" ]; do sleep 1; done; touch "$1/done"' sh "$T" 2>"$T/t13.err" &
227hp=$!
228i=0; while [ $i -lt 20 ] && ! grep -q "overrun: hold .* passed expected_end" "$HIST" 2>/dev/null; do sleep 1; i=$((i + 1)); done
229"$Q" check 2>/dev/null; crc=$?
230"$Q" release-stale >/dev/null 2>&1; src=$?
231"$Q" hold --owner B --minutes 1 -- true 2>/dev/null; hrc=$?
232check "T13a past expected_end with a live holder: flag still held, overrun logged, others refused" \
233 'grep -q "overrun: hold .* passed expected_end while command still running; flag KEPT" "$HIST" && grep -q "WARNING overrun" "$T/t13.err" && [ -e "$FLAG" ] && grep -q "^A quietlock " "$FLAG" && [ $crc = 75 ] && [ $src = 3 ] && [ $hrc = 75 ] && [ -e "$T/started" ] && [ ! -e "$T/done" ] && kill -0 $hp 2>/dev/null'
234touch "$T/go"; wait $hp; rc=$?
235check "T13b holder exits after overrun: flag released and logged" \
236 '[ $rc = 0 ] && [ -e "$T/done" ] && [ ! -e "$FLAG" ] && grep -q "released when its command exited after overrun (command exit 0)" "$HIST"'
237"$Q" check 2>/dev/null; crc=$?
238check "T13c after the holder exits others are not blocked" '[ $crc = 0 ]'
239rm -f "$T/started" "$T/go" "$T/done" "$T/t13.err"
240
241# --- T13d/T13e the holder is quietlock OR its job (Drake 2026-10-01: release only when the holding job's
242# process exits). hold records job=<pid> jobgroup=<pgid> (the job runs in its own process group). If
243# quietlock itself is killed (-9) while the job runs on past expected_end, the flag stays held; once the
244# job ends, release-stale clears it.
245# Mutations killed: holder_alive() checking only pid= (T13d: release-stale releases, check 0);
246# no record_job() (T13d record check); release-stale ignoring a dead job group (T13e flag stays).
247reset
248QUIETLOCK_TEST=1 QUIETLOCK_TEST_MINUTE_SECONDS=2 "$Q" hold --owner A --minutes 1 -- \
249 sh -c 'touch "$1/started"; while [ ! -e "$1/go" ]; do sleep 1; done; touch "$1/done"' sh "$T" 2>/dev/null &
250hp=$!
251i=0; while [ $i -lt 20 ] && [ ! -e "$T/started" ]; do sleep 1; i=$((i + 1)); done
252jp=$(grep -o ' job=[0-9]*' "$FLAG" 2>/dev/null | cut -d= -f2)
253jg=$(grep -o ' jobgroup=[0-9]*' "$FLAG" 2>/dev/null | cut -d= -f2)
254check "T13d flag records the job pid and its own process group" '[ -n "$jp" ] && [ "$jg" = "$jp" ] && kill -0 "$jp" 2>/dev/null'
255kill -9 $hp; wait $hp 2>/dev/null
256sleep 3 # past expected_end (2 s after start)
257"$Q" check 2>/dev/null; crc=$?
258"$Q" release-stale >/dev/null 2>&1; src=$?
259check "T13d quietlock killed, job running past expected_end: flag held, check 75, release-stale refuses" \
260 '! kill -0 $hp 2>/dev/null && [ -e "$FLAG" ] && [ $crc = 75 ] && [ $src = 3 ] && [ ! -e "$T/done" ]'
261touch "$T/go"
262i=0; while [ $i -lt 20 ] && ! "$Q" release-stale >/dev/null 2>&1; do sleep 1; i=$((i + 1)); done
263"$Q" check 2>/dev/null; crc=$?
264check "T13e job ended: release-stale cleared the flag" \
265 '[ -e "$T/done" ] && [ ! -e "$FLAG" ] && [ $crc = 0 ] && grep -q "released stale flag: A quietlock .* job=$jp jobgroup=$jp" "$HIST"'
266rm -f "$T/started" "$T/go" "$T/done"
267
268# --- T13f the job exits but leaves a background child in its process group: the hold stays until the
269# last group member ends (nothing is killed), then the flag is released.
270# Mutations killed: releasing as soon as the job pid is reaped (no group wait), or group_alive()
271# returning 0 (flag gone, check 0, second hold runs); killing the leftover child (no done file).
272reset
273"$Q" hold --owner A --minutes 5 -- \
274 sh -c '( while [ ! -e "$1/go" ]; do sleep 1; done; touch "$1/done" ) </dev/null >/dev/null 2>&1 & touch "$1/started"; exit 0' sh "$T" 2>/dev/null &
275hp=$!
276i=0; while [ $i -lt 20 ] && ! grep -q "job exited (exit 0) but its group .* still has processes" "$HIST" 2>/dev/null; do sleep 1; i=$((i + 1)); done
277"$Q" check 2>/dev/null; crc=$?
278"$Q" release-stale >/dev/null 2>&1; src=$?
279"$Q" hold --owner B --minutes 1 -- touch "$T/ran13f" 2>/dev/null; hrc=$?
280check "T13f job exited, child left in its group: flag held, check 75, release-stale 3, second hold refused" \
281 '[ -e "$T/started" ] && grep -q "job exited (exit 0) but its group" "$HIST" && [ -e "$FLAG" ] && [ $crc = 75 ] && [ $src = 3 ] && [ $hrc = 75 ] && [ ! -e "$T/ran13f" ] && [ ! -e "$T/done" ] && kill -0 $hp 2>/dev/null'
282touch "$T/go"; wait $hp; rc=$?
283"$Q" check 2>/dev/null; crc=$?
284check "T13f last group member ended: flag released, child was not killed" \
285 '[ $rc = 0 ] && [ -e "$T/done" ] && [ ! -e "$FLAG" ] && [ $crc = 0 ] && grep -q "last process of group" "$HIST"'
286rm -f "$T/started" "$T/go" "$T/done" "$T/ran13f"
287
288# --- T14 expected_end without Z never counts as passed.
289# Mutation killed: making the trailing Z optional in parse_iso.
290D2=$(dead_pid)
291echo "M18 gate16 desc start=$PAST expected_end=2000-01-01T00:00:00 pid=$D2" > "$FLAG"
292"$Q" release-stale >/dev/null; rc=$?
293check "T14 no-Z expected_end is never stale" '[ "$D2" != 0 ] && [ $rc = 3 ] && [ -e "$FLAG" ]'
294
295# --- T15 malformed flag (no pid=, no expected_end=) blocks and is never stale.
296# Mutation killed: treating a missing pid as dead, or a missing end as passed.
297echo "qemu_ck_store_test 3033916" > "$FLAG"
298"$Q" check 2>/dev/null; crc=$?
299"$Q" release-stale >/dev/null; rc=$?
300check "T15 malformed flag blocks and is not released" '[ $crc = 75 ] && [ $rc = 3 ] && [ -e "$FLAG" ]'
301
302# --- T16 a hold never deletes a flag that is not its own.
303# Mutation killed: release_mine() unlinking without comparing the hold id.
304reset
305"$Q" hold --owner A --minutes 1 -- sh -c 'rm -f "$1"; echo "FOREIGN x y start=$3 expected_end=$4 pid=$2 hold=qFOREIGN" > "$1"' sh "$FLAG" "$$" "$PAST" "$FUTURE"
306check "T16 foreign flag left alone at hold exit" 'grep -q "hold=qFOREIGN" "$FLAG"'
307
308# --- T17 SIGTERM to hold is forwarded to the command and the flag is released.
309# Mutations killed: no signal handler (quietlock dies, flag stays); handler not forwarding (rc 0 after 30 s).
310reset
311"$Q" hold --owner A --minutes 5 -- sh -c 'touch "$1"; exec sleep 30' sh "$T/ready" 2>/dev/null &
312hp=$!
313i=0; while [ $i -lt 20 ] && [ ! -e "$T/ready" ]; do sleep 1; i=$((i + 1)); done
314kill -TERM $hp; wait $hp; rc=$?
315check "T17 SIGTERM forwarded, flag released" '[ $rc = 143 ] && [ ! -e "$FLAG" ]'
316
317# ===== Proposed hook tools/quietlock/quiet-guard.sh (stdin = Claude Code hook JSON) =====
318HOOK=$HERE/tools/quietlock/quiet-guard.sh
319if ! command -v jq >/dev/null 2>&1; then
320 check "H0 jq available for hook tests" 'false'
321else
322hookrun() { # $1 = JSON; extra env via caller
323 printf '%s' "$1" | QUIETLOCK_BIN=$Q sh "$HOOK" 2>"$T/hookerr"
324}
325bash_json() { jq -n --arg c "$1" '{tool_name:"Bash",tool_input:{command:$c}}'; }
326reset; live_flag
327
328# H1 Edit/Write calls are never blocked, even with build words in the text.
329# Mutation killed: scanning the whole tool_input JSON (old text matching) instead of Bash commands only.
330hookrun "$(jq -n '{tool_name:"Edit",tool_input:{file_path:"notes.md",old_string:"a",new_string:"make test\ncargo build"}}')"; rc=$?
331check "H1 Edit of an .md mentioning make test allowed" '[ $rc = 0 ]'
332hookrun "$(jq -n '{tool_name:"Write",tool_input:{file_path:"x.md",content:"make -C x"}}')"; rc=$?
333check "H1b Write mentioning make allowed" '[ $rc = 0 ]'
334
335# H2 writing notes is never blocked.
336# Mutations killed: no quote stripping (H2, H2d); matching words anywhere instead of command position (H2b);
337# no heredoc stripping (H2c); per-line quote stripping (H2e).
338hookrun "$(bash_json 'echo "remember: make test before merge" >> ~/handoffs/x.md')"; rc=$?
339check "H2 echo \"... make ...\" >> handoff.md allowed" '[ $rc = 0 ]'
340hookrun "$(bash_json 'echo then run make test >> notes.md')"; rc=$?
341check "H2b unquoted make as an echo argument allowed" '[ $rc = 0 ]'
342hookrun "$(bash_json "$(printf 'cat > notes.md <<%sEOF%s\nmake test\ncargo build\nEOF' "'" "'")")"; rc=$?
343check "H2c heredoc into .md allowed" '[ $rc = 0 ]'
344hookrun "$(bash_json "sed -i 's/make all/make test/' doc.md")"; rc=$?
345check "H2d sed -i on .md allowed" '[ $rc = 0 ]'
346hookrun "$(bash_json "$(printf 'echo "first line\nmake test\nlast" >> notes.md')")"; rc=$?
347check "H2e multi-line quoted text allowed" '[ $rc = 0 ]'
348
349# H3 real heavy commands are blocked while held.
350# Mutations killed: hook always exiting 0 / not consulting quietlock (H3); no segment split (H3b);
351# no -c extraction (H3c); no wrapper/assignment skipping (H3d); cargo subcommand rule removed (H3e).
352hookrun "$(bash_json 'make -C x')"; rc=$?
353check "H3 make -C x blocked while held" '[ $rc = 2 ] && grep -q QUIETLOCK_REFUSED "$T/hookerr"'
354hookrun "$(bash_json 'cd x && make test')"; rc=$?
355check "H3b cd x && make test blocked" '[ $rc = 2 ]'
356hookrun "$(bash_json "bash -c 'make test'")"; rc=$?
357check "H3c bash -c 'make test' blocked" '[ $rc = 2 ]'
358hookrun "$(bash_json 'env FOO=1 nice -n 5 make all')"; rc=$?
359check "H3d env/nice wrapped make blocked" '[ $rc = 2 ]'
360hookrun "$(bash_json 'cargo build --release')"; rc=$?
361check "H3e cargo build blocked" '[ $rc = 2 ]'
362
363# H4 the holder passes (command prefix or hook env), a wrong id does not.
364# Mutation killed: not handing the extracted/env QUIETLOCK_HOLD to quietlock check.
365hookrun "$(bash_json 'QUIETLOCK_HOLD=qOTHER-1-0 make -C x')"; rc=$?
366check "H4 holder's QUIETLOCK_HOLD prefix allowed" '[ $rc = 0 ]'
367hookrun "$(bash_json 'QUIETLOCK_HOLD=qWRONG make -C x')"; rc=$?
368check "H4b wrong QUIETLOCK_HOLD blocked" '[ $rc = 2 ]'
369printf '%s' "$(bash_json 'make -C x')" | QUIETLOCK_HOLD=qOTHER-1-0 QUIETLOCK_BIN=$Q sh "$HOOK" 2>/dev/null; rc=$?
370check "H4c holder's QUIETLOCK_HOLD in hook env allowed" '[ $rc = 0 ]'
371
372# H5 control: clear flag lets heavy commands through.
373# Mutation killed: blocking heavy commands without asking quietlock.
374reset
375hookrun "$(bash_json 'make -C x')"; rc=$?
376check "H5 make allowed when clear" '[ $rc = 0 ]'
377
378# H6 stale flag is released through quietlock release-stale, then allowed.
379# Mutation killed: dropping the release-stale call (flag stays).
380D3=$(dead_pid)
381echo "M18 gate16 desc start=$PAST expected_end=$PAST pid=$D3" > "$FLAG"
382hookrun "$(bash_json 'make -C x')"; rc=$?
383check "H6 stale flag released by the hook via quietlock" '[ $rc = 0 ] && [ ! -e "$FLAG" ] && grep -q "released stale flag: M18 gate16 desc start=$PAST expected_end=$PAST pid=$D3" "$HIST"'
384
385# H7 DEPRECATED transition escapes still work in the hook.
386# Mutations killed: removing the QUIET_HOLDER=1 case (H7); removing the allow-line check (H7b); allow line matching everything (H7c).
387echo "M18 gate16 desc start=$PAST expected_end=$FUTURE pid=$$" > "$FLAG"
388hookrun "$(bash_json 'QUIET_HOLDER=1 make -C x')"; rc=$?
389check "H7 QUIET_HOLDER=1 allowed, marked deprecated" '[ $rc = 0 ] && grep -q DEPRECATED "$T/hookerr"'
390printf 'allow ^make -C special\n' >> "$FLAG"
391hookrun "$(bash_json 'make -C special')"; rc=$?
392check "H7b allow line lets a matching command through" '[ $rc = 0 ]'
393hookrun "$(bash_json 'make -C other')"; rc=$?
394check "H7c allow line does not let others through" '[ $rc = 2 ]'
395
396# H8 round-3 bypasses are closed (each blocked while held).
397# Mutations killed: -c extraction only for the exact "-c" flag (bash -lc, sh -xc); dropping the shells
398# branch (bash -c make unquoted); dropping the keyword skip (if/for/while/until/else, a regression against
399# the old hook); replacing the wrapper "any later heavy word" rule with option skipping (sudo -u x,
400# taskset -c 0-3, chrt -f 10, timeout -s KILL 60, env -u VAR, xargs -n1, stdbuf -oL, ionice -c 3).
401reset; live_flag
402expect_block() {
403 hookrun "$(bash_json "$1")"; rc=$?
404 check "H8 blocked: $1" '[ $rc = 2 ] && grep -q QUIETLOCK_REFUSED "$T/hookerr"'
405}
406expect_allow() {
407 hookrun "$(bash_json "$1")"; rc=$?
408 check "H9 allowed: $1" '[ $rc = 0 ]'
409}
410expect_block 'bash -lc "make"'
411expect_block 'bash -c make'
412expect_block "sh -xc 'make all'"
413expect_block 'if make; then :; fi'
414expect_block 'for i in 1; do make; done'
415expect_block 'while make; do :; done'
416expect_block 'until make; do :; done'
417expect_block 'if true; then :; else make; fi'
418expect_block 'sudo -u x make'
419expect_block 'taskset -c 0-3 make'
420expect_block 'chrt -f 10 make'
421expect_block 'ionice -c 3 make'
422expect_block 'stdbuf -oL make'
423expect_block 'timeout -s KILL 60 make'
424expect_block 'xargs -n1 make'
425expect_block 'env -u VAR make'
426# H8b the original probes from the first review stay blocked.
427# Mutations killed: dropping assignment skipping (x=1 make), the "(" segment break ((make)),
428# basename matching (/usr/bin/make), the && split (cd d && make), or env/nice/time from the wrapper list.
429expect_block 'env make'
430expect_block 'env FOO=1 make'
431expect_block 'nice make'
432expect_block 'nice -n 5 make'
433expect_block '(make)'
434expect_block 'x=1 make'
435expect_block 'time make'
436expect_block 'time -p make'
437expect_block '/usr/bin/make'
438expect_block 'cd d && make'
439# H9b `command -v make` is a lookup, not a run.
440# Mutation killed: removing the command/type -v exception (command is a wrapper, so it would block).
441expect_allow 'command -v make'
442# H9 controls: lookalikes that are not builds stay allowed.
443# Mutations killed: collecting "-c '...'" text after any command, not only a shell (grep -c);
444# wrapper rule blocking on any word instead of a heavy word (sudo cat); scanning echo arguments (for/echo).
445expect_allow 'grep -c "make" notes.md'
446expect_allow 'sudo cat notes.md'
447expect_allow "bash -c 'echo hi' >> notes.md"
448expect_allow 'for f in a b; do echo make; done >> notes.md'
449# H10 queueing is not running: lanes.sh queue/queue-light/idea/ledger/brief/status are allowed while
450# the flag is held, whatever build words the queued text holds (queen 2026-10-01).
451# Mutations killed: no exemption (bash -c "make" inside the queued text is collected as a shell -c
452# command and blocks); exemption matched anywhere in the text like the live hook (H10f-H10h would pass);
453# exemption swallowing everything to end of line instead of stopping at a separator (H10f, H10g).
454expect_allow "lanes.sh queue X d 'make test'"
455expect_allow "\$HOME/.claude/skills/orchestrate-lanes/lanes.sh queue-light HD-13 ~/w 'cargo build'"
456expect_allow "bash ~/.claude/skills/orchestrate-lanes/lanes.sh queue X d 'bash -c \"make all\"'"
457expect_allow "lanes.sh idea X 'run make later'"
458expect_allow 'lanes.sh status'
459expect_block "lanes.sh queue X d 'make test'; make"
460expect_block 'lanes.sh queue X d x && cargo build'
461expect_block "echo lanes.sh queue X d y; make"
462# H10i `lanes.sh flush` is not a build word: the hook lets it start; the forge itself then waits for a
463# clear flag before every job (I9). Mutation killed: treating flush as heavy would stall the forge.
464expect_allow 'lanes.sh flush'
465# H11 script runs and test programs (2026-10-08 GB10 hold overlaps: `bash scripts/test-linked-compose.sh`,
466# which runs cargo test inside, a directly-run test binary). While someone else holds: a script whose
467# text runs a build/test/QEMU program is blocked however it is started (shell + file, ./path, source,
468# behind a wrapper, quoted path, one script calling another), and test programs are blocked by name.
469# Mutations killed: no script content scan (H11a-H11i); content scan without comment/quote stripping
470# (H11 allowed harmless.sh); no name rule for test scripts / target/*/deps / tests/ (H11j-H11o);
471# no recursion into called scripts (H11h); no cwd resolution (H11c); refusal without the holder (H11a).
472SD=$T/scripts-h11
473mkdir -p "$SD/sub"
474printf '#!/bin/sh\nset -e\nfor t in a b; do\n cargo test "$@" -- --test-threads=1 2>&1 | tee -a log\ndone\n' > "$SD/heavy.sh"
475printf '#!/bin/sh\n# runs the heavy one\nbash %s/heavy.sh "$@"\n' "$SD" > "$SD/outer.sh"
476printf '#!/bin/bash\ntimeout 60 qemu-system-aarch64 -M virt -nographic\n' > "$SD/qemu.sh"
477printf '#!/bin/sh\ncd x && make -j8 all\n' > "$SD/mk.sh"
478printf "#!/bin/sh\n# make sure the notes exist; don't run make here\necho \"make test later\" >> notes.md\nprintf 'cargo build\\\\n' > todo.txt\ngrep -c make notes.md\n" > "$SD/harmless.sh"
479cp "$SD/heavy.sh" "$SD/sub/run-it.sh"
480printf '#!/bin/sh\nbash run-it.sh "$@"\n' > "$SD/sub/outer-rel.sh"
481chmod +x "$SD"/*.sh "$SD/sub/run-it.sh"
482cwd_json() { jq -n --arg c "$1" --arg d "$2" '{tool_name:"Bash",cwd:$d,tool_input:{command:$c}}'; }
483reset; live_flag
484h11_block() { # $1 name, $2 command, [$3 cwd]
485 if [ -n "${3:-}" ]; then hookrun "$(cwd_json "$2" "$3")"; else hookrun "$(bash_json "$2")"; fi; rc=$?
486 check "H11 blocked ($1): $2" '[ $rc = 2 ] && grep -q QUIETLOCK_REFUSED "$T/hookerr" && grep -q "holder=.OTHER quietlock test" "$T/hookerr"'
487}
488h11_allow() {
489 if [ -n "${3:-}" ]; then hookrun "$(cwd_json "$2" "$3")"; else hookrun "$(bash_json "$2")"; fi; rc=$?
490 check "H11 allowed ($1): $2" '[ $rc = 0 ]'
491}
492h11_block a "bash $SD/heavy.sh"
493h11_block a "sh $SD/heavy.sh --nocapture"
494h11_block b "$SD/heavy.sh"
495h11_block c "./heavy.sh" "$SD"
496h11_block c "bash sub/run-it.sh" "$SD"
497h11_block d "cd $SD && bash heavy.sh"
498h11_block e "timeout 600 bash $SD/heavy.sh"
499h11_block e "nohup bash $SD/heavy.sh > log 2>&1 &"
500h11_block f "source $SD/heavy.sh"
501h11_block f ". $SD/heavy.sh"
502h11_block g "bash \"$SD/heavy.sh\""
503h11_block h "bash $SD/outer.sh"
504h11_block i "bash $SD/qemu.sh"
505h11_block i "sh $SD/mk.sh"
506h11_block j "bash scripts/test-linked-compose.sh /a /b /c" "$T"
507h11_block j "cd ~/w/sc && bash scripts/test-linked-compose.sh" "$T"
508h11_block k "./target/release/deps/aien_daemon-0123abcd --include-ignored" "$T"
509h11_block k "target/aarch64-unknown-linux-gnu/debug/deps/x-1" "$T"
510h11_block l "tests/quietlock/run.sh" "$T"
511h11_block l "sh tests/run.sh" "$T"
512h11_block m "./build/store_test" "$T"
513h11_block n "timeout 900 ./target/release/deps/m19-77aa" "$T"
514h11_block o "bash scripts/run_tests.sh" "$T"
515h11_allow p "bash $SD/harmless.sh"
516h11_allow p "$SD/harmless.sh"
517h11_allow q "cat $SD/heavy.sh"
518h11_allow q "grep -n cargo $SD/heavy.sh tests/x.txt"
519h11_allow q "sed -n 1,5p scripts/test-linked-compose.sh" "$T"
520h11_allow q "ls target/release/deps/ tests/"
521h11_allow q "git diff -- tests/ scripts/test-x.sh"
522h11_allow v "git diff origin/main...pr -- crates ':!**/tests/**' | grep x"
523h11_allow v "cmp <(git show origin/main:specs/keys/k) ~/w/native/tests/fixtures/keys/k"
524h11_allow v "X=\$(cd a && pwd)/tests/runtime/m.c; cat \$X"
525h11_allow v "cat \${HOME}/w/tests/notes.txt"
526# Review of #345: a wrapper's later words are not all programs; only the wrapped command is.
527h11_allow x "timeout 5 cat tests/a.txt"
528h11_allow x "sudo -u bob cat tests/a.txt"
529h11_allow x "nice -n5 grep x tests/a.txt"
530h11_allow x "env GIT_PAGER=cat git diff tests/a.txt"
531h11_allow x "xargs -0 cat tests/a.txt"
532h11_allow x "watch -n 2 cat tests/a.txt"
533h11_allow x "nohup sleep 1 > tests/out.log 2>&1 &"
534h11_block y "sudo -u bob ./target/release/deps/x-1" "$T"
535h11_block y "timeout -s KILL 60 bash $SD/heavy.sh"
536h11_block y "taskset -c 0-3 nice -n 5 tests/run.sh" "$T"
537h11_block z "bash < $SD/heavy.sh"
538h11_block z "bash -s <$SD/heavy.sh"
539h11_block z "cargo -q test --release"
540h11_block z "cargo +nightly test"
541h11_block z "timeout 900 \$PY bench/tools/test_runner_offline.py" "$T"
542h11_block z "python3 -u tests/test_x.py" "$T"
543h11_block z "python3 -m pytest -q" "$T"
544h11_allow z "perl -0777 -pi -e s/a/b/ tests/x.rs bench/tools/test_y.py" "$T"
545h11_allow z "python3 -c pass tests/test_x.py" "$T"
546h11_allow z "python3 tools/report.py tests/a.txt" "$T"
547h11_allow z "timeout 5 \$PY tools/report.py" "$T"
548h11_block z "bash $SD/sub/outer-rel.sh" "$T"
549h11_block w "bash \${HOME}/w/scripts/test-x.sh"
550h11_allow r "bash -n $SD/heavy.sh"
551h11_allow s "quietlock hold --owner me --minutes 5 -- bash $SD/heavy.sh"
552h11_allow t "QUIETLOCK_HOLD=qOTHER-1-0 bash $SD/heavy.sh"
553h11_allow t "QUIETLOCK_HOLD=qOTHER-1-0 ./target/release/deps/x-1" "$T"
554reset
555h11_allow u "bash $SD/heavy.sh"
556h11_allow u "./target/release/deps/x-1" "$T"
557fi
558
559
560# ===== Installer + lanes.sh patch (tools/quietlock/install.sh, lanes-quietlock.patch) =====
561# Runs against a FAKE $HOME under $T holding the PINNED copies tools/quietlock/testdata/lanes.sh.base
562# and quiet-guard.sh.base (the live files the patch was made against). Never reads or touches ~/.claude
563# for pass/fail. The only look at the live lanes.sh is the informational note below, which never fails.
564TD=$HERE/tools/quietlock/testdata
565LIVE_LANES=$HOME/.claude/skills/orchestrate-lanes/lanes.sh
566if [ -f "$LIVE_LANES" ]; then
567 if grep -qF 'HD-13: the quiet flag is a real lock' "$LIVE_LANES"; then
568 echo "NOTE applies to live lanes.sh: already patched"
569 elif patch -F 0 --dry-run -s -o /dev/null "$LIVE_LANES" < "$HERE/tools/quietlock/lanes-quietlock.patch" >/dev/null 2>&1; then
570 echo "NOTE applies to live lanes.sh: yes"
571 else
572 echo "NOTE applies to live lanes.sh: no (live file changed; regenerate the patch before installing)"
573 fi
574else
575 echo "NOTE applies to live lanes.sh: no live lanes.sh on this host"
576fi
577
578mkhome() { # $1 = fake home; pinned lanes.sh + pinned hook, no quietlock binary
579 rm -rf "$1"
580 mkdir -p "$1/.claude/hooks" "$1/.claude/skills/orchestrate-lanes" "$1/workspace"
581 cp "$TD/lanes.sh.base" "$1/.claude/skills/orchestrate-lanes/lanes.sh"
582 cp "$TD/quiet-guard.sh.base" "$1/.claude/hooks/quiet-guard.sh"
583}
584# Run lanes.sh from fake home $F with its quietlock, never the caller's state dir or hold.
585fl() { ( unset QUIETLOCK_DIR QUIETLOCK_HOLD; HOME=$F; QUIETLOCK_BIN=$F/.local/bin/quietlock; export HOME QUIETLOCK_BIN; "$@" ); }
586# Every flush / flush-light runs with LANES_LIGHT_IDLE_MIN=0 (exit as soon as the queue is empty) and
587# under a bounded wait: timeout(1) signals only its own process group (the fake-home lanes.sh and the
588# jobs it started in $T), never anything outside the test tree. An overrun is recorded as FAIL.
589# Mutation killed: dropping LANES_LIGHT_IDLE_MIN=0 (the persistent light loop idles 30 min; the bound trips).
590bounded() { # $1 seconds, $2 test name, $3 log file, rest = lanes.sh command
591 bn=$1; bname=$2; blog=$3; shift 3
592 fl timeout -k 5 "$bn" env LANES_LIGHT_IDLE_MIN=0 "$@" >"$blog" 2>&1; brc=$?
593 check "$bname finished within ${bn}s (no hang)" '[ $brc != 124 ] && [ $brc != 137 ]'
594}
595F=$T/fakehome
596mkhome "$F"
597FL=$F/.claude/skills/orchestrate-lanes/lanes.sh
598FH=$F/.claude/hooks/quiet-guard.sh
599
600# I1 --dry-run changes nothing and leaves no temp files.
601# Mutations killed: dry-run doing the swap; staged *.tmp.* files left behind (no cleanup trap).
602( HOME=$F; export HOME; sh "$HERE/tools/quietlock/install.sh" --dry-run ) >"$T/i1.log" 2>&1; rc=$?
603check "I1 install --dry-run changes nothing" '[ $rc = 0 ] && cmp -s "$FL" "$TD/lanes.sh.base" && cmp -s "$FH" "$TD/quiet-guard.sh.base" && [ ! -e "$F/.local/bin/quietlock" ] && [ -z "$(find "$F" -name "*.bak.*" -o -name "*.tmp.*" -o -name "*.rej" -o -name "*.orig")" ]'
604
605# I2 install builds the tool, backs up both files, installs the hook, patches lanes.sh with no bare rm of the flag.
606# Mutations killed: skipping the backup; skipping the patch; a patch that keeps rm -f "$FLAG".
607( HOME=$F; export HOME; sh "$HERE/tools/quietlock/install.sh" ) >"$T/i2.log" 2>&1; rc=$?
608check "I2 install succeeds" '[ $rc = 0 ] && [ -x "$F/.local/bin/quietlock" ]'
609check "I2b backups equal the originals" 'cmp -s "$(ls "$FL".bak.* | head -1)" "$TD/lanes.sh.base" && cmp -s "$(ls "$FH".bak.* | head -1)" "$TD/quiet-guard.sh.base"'
610check "I2c hook installed, lanes.sh patched, no rm of the flag" 'cmp -s "$HERE/tools/quietlock/quiet-guard.sh" "$FH" && grep -q "HD-13: the quiet flag is a real lock" "$FL" && ! grep -q "rm -f \"\$FLAG\"" "$FL" && bash -n "$FL"'
611check "I2d no temp, .rej or .orig files left" '[ -z "$(find "$F" -name "*.tmp.*" -o -name "*.rej" -o -name "*.orig")" ]'
612
613# I3 re-running is idempotent (patch not applied twice).
614# Mutation killed: removing the already-patched marker check.
615( HOME=$F; export HOME; sh "$HERE/tools/quietlock/install.sh" ) >"$T/i3.log" 2>&1; rc=$?
616check "I3 second install leaves lanes.sh patched once" '[ $rc = 0 ] && [ "$(grep -c "HD-13: the quiet flag is a real lock" "$FL")" = 1 ]'
617
618# I4 patched `lanes.sh release-stale` goes through quietlock: live flag kept, stale flag released
619# with quietlock's history line.
620# Mutation killed: the old read-then-rm body (its history line has no "quietlock:" prefix).
621FF=$F/workspace/.spark-quiet
622echo "OTHER quietlock test start=$PAST expected_end=$FUTURE pid=$$ hold=qOTHER-1-0" > "$FF"
623fl bash "$FL" release-stale >/dev/null 2>&1
624check "I4 patched release-stale leaves a live flag" '[ -e "$FF" ]'
625D4=$(dead_pid)
626echo "OLD x y start=$PAST expected_end=$PAST pid=$D4" > "$FF"
627fl bash "$FL" release-stale >/dev/null 2>&1
628check "I4b patched release-stale releases through quietlock" '[ ! -e "$FF" ] && grep -q "quietlock: released stale flag: OLD x y" "$F/workspace/.spark-quiet.history"'
629
630# I5 patched light flush verdicts (queen round-5 ruling 2). REFUSED_QUIET only when the job exits 75
631# AND its final log line is quietlock's own refusal; capped requeues; everything else is judged by rc.
632# L1 true -> PASS. L2 exit 3 -> FAIL(rc=3).
633# L3 exit 75 with no quietlock line -> FAIL(rc=75), not requeued.
634# L4 a failing suite printing the marker text in a label -> FAIL(rc=1), not requeued.
635# L6 marker printed, then other output, exit 75 -> FAIL(rc=75) (marker not on the final line).
636# L5 a real quietlock refusal (other holder's live flag, then the job clears it to keep the test
637# short) -> REFUSED_QUIET, requeued as L5@r1..@r3, then REFUSED_QUIET_GAVE_UP (cap 3), no more requeue.
638# Mutations killed: grepping the marker anywhere in the log (L4, L6 would requeue); rc 75 alone (L3);
639# no cap (L5 loops forever / no GAVE_UP); counter not carried in the requeued line (never gives up).
640rm -f "$FF"
641R=$F/workspace/test-queue-results.md
642L5='echo "OTHER quietlock t start=x expected_end=2099-01-01T00:00:00Z pid=$PPID hold=qX-1-0" > "$HOME/workspace/.spark-quiet"; "$QUIETLOCK_BIN" check; rc=$?; rm -f "$HOME/workspace/.spark-quiet"; exit $rc'
643{
644 printf 'L1|%s|true\n' "$T"
645 printf 'L2|%s|exit 3\n' "$T"
646 printf 'L3|%s|exit 75\n' "$T"
647 printf 'L4|%s|echo "label: QUIETLOCK_REFUSED handling test"; exit 1\n' "$T"
648 printf 'L6|%s|echo "quietlock: QUIETLOCK_REFUSED fake"; echo more; exit 75\n' "$T"
649 printf 'L5|%s|%s\n' "$T" "$L5"
650} > "$F/workspace/.test-queue-light"
651bounded 180 I5 "$T/i5.log" env FORGE_WAIT_SECONDS=1 bash "$FL" flush-light
652check "I5 light flush PASS / FAIL verdicts" 'grep -q "^- L1: PASS" "$R" && grep -q "^- L2: FAIL(rc=3)" "$R" && grep -q "^- L3: FAIL(rc=75)" "$R"'
653check "I5b failing suite printing the marker is FAIL, not requeued" 'grep -q "^- L4: FAIL(rc=1)" "$R" && grep -q "^- L6: FAIL(rc=75)" "$R" && ! grep -q "^- L[346]: REFUSED" "$R"'
654check "I5c real refusal requeued 3 times, then REFUSED_QUIET_GAVE_UP" '[ "$(grep -c "^- L5: REFUSED_QUIET " "$R")" = 3 ] && [ "$(grep -c "^- L5: REFUSED_QUIET_GAVE_UP " "$R")" = 1 ]'
655check "I5d nothing left queued, light loop exited" '[ ! -s "$F/workspace/.test-queue-light" ] && [ ! -s "$F/workspace/.test-queue-light.requeue" ] && grep -q "idle 0 min, exiting" "$T/i5.log"'
656
657# I6 patched main flush: NO per-job hold (queen round-5 ruling 1). With the flag clear the job runs
658# with no flag on disk and no QUIETLOCK_HOLD, and a make goal behind the mk/ gate passes
659# (the forge is the allowed runner when clear).
660# Mutation killed: the round-4 per-job `quietlock hold` (flag present / QUIETLOCK_HOLD set during the job).
661printf 'M1|%s|test -z "$QUIETLOCK_HOLD" && test ! -e "$HOME/workspace/.spark-quiet"\n' "$T" > "$F/workspace/.test-queue"
662printf 'M2|%s|make -s -C %s OUT_DIR=%s quietlock-check\n' "$HERE" "$HERE" "$T/build" >> "$F/workspace/.test-queue"
663bounded 300 I6 "$T/i6.log" bash "$FL" flush
664check "I6 main flush job ran with no forge hold" 'grep -q "^- M1: PASS" "$R" && [ ! -e "$FF" ]'
665check "I6b forge make job passes the mk/ gate when clear" 'grep -q "^- M2: PASS" "$R"'
666
667# I9 the forge waits for a clear flag before each job (no hold, no override). A flag held by another,
668# live, holder blocks the job until that holder is gone and its expected_end has passed; then the
669# stale flag is released through quietlock and the job runs.
670# Mutations killed: dropping ql_wait_clear (the job would run while the holder is still alive:
671# the job records whether the holder pid was alive when it started); looping on check alone (stale counts
672# as clear, so the dead holder's flag would stay on disk with no release in the history).
673HP=$(sh -c 'sleep 4 >/dev/null 2>&1 & echo $!') # reparented, so no zombie that kill -0 would still see
674echo "OTHER quietlock t start=$PAST expected_end=$PAST pid=$HP hold=qOTHER-9-0" > "$FF"
675printf 'M9|%s|if kill -0 %s 2>/dev/null; then echo HOLDER_ALIVE; exit 9; fi\n' "$T" "$HP" > "$F/workspace/.test-queue"
676bounded 120 I9 "$T/i9.log" env FORGE_WAIT_SECONDS=1 bash "$FL" flush
677check "I9 main forge waited for the other holder, then ran" 'grep -q "^- M9: PASS" "$R" && [ ! -e "$FF" ] && grep -q "released stale flag: OTHER quietlock t" "$F/workspace/.spark-quiet.history"'
678
679# I10 the forge is never a holder (inspector R7-1/R7-2): with QUIET_HOLDER=1 and a QUIETLOCK_HOLD
680# exported into the forge (as found in a live flush-light environment) and a LIVE legacy flag
681# (no hold=, e.g. an aienos QEMU noclobber hold), neither the main nor the light forge runs the job.
682# The forge is expected to keep waiting; timeout(1) ends it (only its own process group) after 8 s.
683# Mutation killed: removing the unset in ql_ok and the env -u on the wait and the job (the legacy
684# escape lets the wait and `quietlock run` pass, so the job runs and leaves its marker file).
685echo "qemu_ck_net_test 1 noclobber start=$PAST expected_end=$FUTURE pid=$$" > "$FF"
686printf 'M10|%s|touch %s/ran10main\n' "$T" "$T" > "$F/workspace/.test-queue"
687fl timeout -k 2 8 env LANES_LIGHT_IDLE_MIN=0 FORGE_WAIT_SECONDS=1 QUIET_HOLDER=1 QUIETLOCK_HOLD=qANY-1-0 bash "$FL" flush >"$T/i10.log" 2>&1; rc=$?
688check "I10 main forge with QUIET_HOLDER=1 does not run past a live legacy flag" '[ $rc = 124 ] && [ ! -e "$T/ran10main" ] && [ -e "$FF" ]'
689printf 'L10|%s|touch %s/ran10light\n' "$T" "$T" > "$F/workspace/.test-queue-light"
690fl timeout -k 2 8 env LANES_LIGHT_IDLE_MIN=0 FORGE_WAIT_SECONDS=1 QUIET_HOLDER=1 QUIETLOCK_HOLD=qANY-1-0 bash "$FL" flush-light >"$T/i10b.log" 2>&1; rc=$?
691check "I10b light forge with QUIET_HOLDER=1 does not run past a live legacy flag" '[ $rc = 124 ] && [ ! -e "$T/ran10light" ] && [ -e "$FF" ]'
692rm -f "$FF" "$F/workspace/.test-queue" "$F/workspace/.test-queue-light"
693
694# I7 installer transaction: a failure at any step leaves lanes.sh and the hook exactly as before,
695# no new binary, and no temp/.rej/.orig files (queen round-5 ruling 4).
696# Mutations killed: swapping before staging/patching (patch failpoint would leave a half state);
697# no rollback (swap-lanes / postcheck would leave the new hook); no cleanup trap (temp files left).
698for fpn in build patch swap-hook swap-lanes postcheck; do
699 G=$T/fh-$fpn
700 mkhome "$G"
701 ( HOME=$G; QUIETLOCK_INSTALL_FAILPOINT=$fpn; export HOME QUIETLOCK_INSTALL_FAILPOINT; sh "$HERE/tools/quietlock/install.sh" ) >"$T/i7-$fpn.log" 2>&1; rc=$?
702 check "I7 failure at $fpn rolls back: both files unchanged, no binary, no leftovers" '[ $rc != 0 ] && cmp -s "$G/.claude/skills/orchestrate-lanes/lanes.sh" "$TD/lanes.sh.base" && cmp -s "$G/.claude/hooks/quiet-guard.sh" "$TD/quiet-guard.sh.base" && [ ! -e "$G/.local/bin/quietlock" ] && [ -z "$(find "$G" -name "*.tmp.*" -o -name "*.rej" -o -name "*.orig")" ]'
703done
704
705# I8 a stale patch (lanes.sh changed since the patch was made) is refused before anything changes.
706# Mutation killed: applying with fuzz / skipping the live dry-run.
707G=$T/fh-stale
708mkhome "$G"
709sed -i "0,/^ sleep 60\$/s// sleep 61/" "$G/.claude/skills/orchestrate-lanes/lanes.sh" # a line the patch removes
710cp "$G/.claude/skills/orchestrate-lanes/lanes.sh" "$T/stale.before"
711( HOME=$G; export HOME; sh "$HERE/tools/quietlock/install.sh" ) >"$T/i8.log" 2>&1; rc=$?
712check "I8 stale lanes.sh refused, nothing changed" '[ $rc = 1 ] && grep -q "does not apply" "$T/i8.log" && cmp -s "$G/.claude/skills/orchestrate-lanes/lanes.sh" "$T/stale.before" && cmp -s "$G/.claude/hooks/quiet-guard.sh" "$TD/quiet-guard.sh.base" && [ ! -e "$G/.local/bin/quietlock" ]'
713reset
714echo "quietlock tests (host): $N checks, $FAILS failed"
715[ "$FAILS" = 0 ] && echo "QUIETLOCK_TESTS_PASS host" && exit 0
716echo "QUIETLOCK_TESTS_FAIL host"
717exit 1