test: make every setsid invocation wait, and fix four gates that lied
The 22 setsid call sites under test/ split 16 bare against 6 that waited. A bare setsid <cmd> does not reliably block: util-linux forks when the setsid PROCESS is itself a process-group leader, which bash makes it under job control, and the parent returns 0 at once while the child runs on. Measured here under bash -i on a pty:
bare exit=0 elapsed_ms=5 logbytes=0
-w exit=0 elapsed_ms=3011 logbytes=16and in the if ! setsid ... form the bare version reported SUCCESS for a child that exited 1. This repo has no CI; every gate here is run by hand, so the interactive prompt is the normal case rather than the exotic one. All 16 now use -w; gate-sd-p3.sh was the in-repo precedent.
Note for whoever reads the original ticket: command substitution and pipelines block even on the fork branch, because the shell waits for EOF and the grandchild holds the write end (measured 3020ms). The racy shape is redirect-to-a-file. gate-d2.sh:25 and gate-d3.sh:25 were never racy.
FOUR GATES THAT COULD NOT FAIL, OR THAT LIED:
- gate-d2.sh's cache-reuse arm is an ABSENCE assertion -- stat, load the manuscript, stat again, require no change -- with nothing in front of it. On the fork branch the load has not run when the second stat is taken. Reproduced in that exact shape: bare -> GREEN on an artifact the child demonstrably had rewritten, -w -> RED. It now carries two controls asserting different things: that a rebuild lands at the watched path, and that the stat probe sees an in-place rewrite. Induced red confirmed.
- gate-d3.sh arm 2 asserted
rg -q '7|8|'. The empty alternative matches every non-empty line; measured, it acceptedERROR: everything exploded, a wrong example count, and a zero-length rendering. Both counts are now derived from the fixture (7 @example plus 1 @project-check), with controls in both directions. Neither end is anchored, because the eval echoes its own return value: the real line is7|8|531#<undefined>.
- gate-sd-p1.sh arm 1 asserted a hardcoded
11 passed (11 total)against a suite that declares and passes 12. It has been RED ON MASTER, accusing healthy product code -- confirmed by running a pristine copy fromgit show HEAD:. The count is now derived, with two assertions rather than one: every declared test must be COLLECTED, and every collected test must have PASSED. gate-sd-p4.sh:45 hardcodes 17 the same way and is the next to go stale; surveyed, not changed here.
- sabotage-sd-p1.sh's closing run -- the only thing in the driver proving red->green -- asserted nothing about its output and printed its completion marker regardless. It now requires the runner's own banner and summary with passed == total, no reported failures, and a floor derived from the legs' own collected counts. Five controls run before it.
Also: sabotage-sd-p1.sh:91 expanded $label inside the same local that bound it, so under set -u every leg wrote to $work/.log and printed label: unbound variable -- seven times in test/transcripts/sd-p1/ sabotage.txt. Split; the run now produces nine distinct per-leg logs and zero unbound-variable errors.
New test/gate-setsid-waits.sh keeps the class closed: it derives the population from every *.sh in the repo and refuses any invocation whose option run lacks -w/--wait. 9/9 planted bare forms flagged, including ones inside quoted strings, eval and sh -c; 0/8 waiting or inert forms misflagged. Its one blind spot -- invocation through a variable -- is stated in the file rather than left to be discovered.
Evidence, all on a quiet box with load recorded at both ends: gate-d2 green; gate-d3 D3_GATE_GREEN_25_25 including arm 10; gate-sd-p4 SD_P4_GATE_GREEN; gate-sd-p1 SD_P1_GATE_GREEN at 12/12; sabotage-sd-p1 8 legs red, closing suite green over 12 collected tests; sabotage-sd-p4 8 legs red, SD_P4_SABOTAGE_COMPLETE, all targets verified against HEAD by sha256 afterwards.
test/gate-d2.sh | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
test/gate-d3.sh | 53 +++++++++++++++++++++++++++++++++++++++++++++---
test/gate-sd-p1.sh | 47 ++++++++++++++++++++++++++++++++++++------
test/gate-sd-p4.sh | 6 +++---
test/gate-setsid-waits.sh | 185 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
test/sabotage-sd-p1.sh | 140 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
test/sabotage-sd-p4.sh | 4 ++--
7 files changed, 486 insertions(+), 22 deletions(-)test/gate-d2.shmodified
# Compile the package and run the focused contract first. The real-language# controls below then exercise the public #lang path against those exact bytes.setsid "$sigil" -L src -L test test test/test-reader.sgl --reportsetsid -w "$sigil" -L src -L test test test/test-reader.sgl --reportobserved=$(setsid "$sigil" -L src -L test eval '(begin (import (sigil doc)) (load "test/fixtures/practical-sigil-chapter.sgd") (display (document-title (eval (quote document)))) (display "|") (display (eval (quote tip-calls))) (display "|") (display (document->plain (eval (quote document)))))')observed=$(setsid -w "$sigil" -L src -L test eval '(begin (import (sigil doc)) (load "test/fixtures/practical-sigil-chapter.sgd") (display (document-title (eval (quote document)))) (display "|") (display (eval (quote tip-calls))) (display "|") (display (document->plain (eval (quote document)))))')case "$observed" in *"A Useful First Script|2|"*"macro-expanded phrase"*"computed inline expression"*"Computed Section 2"*) ;; *) echo "D2 authoring positive control failed" >&2; exit 1 ;;esacreader_sgb=$(find "$cache_dir/real" -path '*/sigil/doc/reader.sgb' -print -quit)test -n "$reader_sgb"# POSITIVE CONTROLS FOR THE ABSENCE ASSERTION BELOW -- RUN FIRST, DELIBERATELY.# Until they pass, the assertion they guard means nothing, so they do not run# after it.## `test "$before" = "$after"` below claims that loading the manuscript through# the public #lang path did NOT recompile the reader. That is an assertion that# NOTHING happened, and it is satisfied for free by anything that stops the# `eval` from running at all.## It was reachable, not hypothetical. Until 2026-08-22 that line was a bare# `setsid`, which forks and returns 0 at once whenever the setsid process is a# process-group leader -- which is what bash makes it at an interactive prompt,# and by hand at a prompt is the only way anything in this repo is run.## WHAT WAS MEASURED, STATED PRECISELY SO NOBODY READS IT AS "THIS GATE IS RED":# the measurement was of this ARM'S SHAPE -- stat, run a child that rewrites the# artifact, stat again -- with a synthetic 3-second child, under `bash -i` on a# pty. The bare form reported GREEN for an artifact the child demonstrably HAD# rewritten; `-w` reported RED. It says the shape can produce a false green. It# says nothing about whether the real manuscript recompiles the real reader, and# the assertion below is expected to hold.## `-w` closes the race. These two controls close the other half, and they assert# DIFFERENT things:## (a) A REBUILD LANDS AT THIS EXACT PATH. Without it, a recompile that wrote# somewhere else would leave `before = after` true while the very thing the# arm rules out had happened.# (b) THE `stat` PROBE SEES A REWRITE IN PLACE. That is the actual failure mode# -- an existing artifact overwritten, not a deleted one recreated -- and# (a) alone cannot show it, because a file that was removed and recreated# changes its stat triple by construction the moment it exists again.## Both work in a THROWAWAY COPY of the cache, so the working tree is untouched# and no restore step has to be reached. The copy is removed afterwards: it is# under `build/` and therefore invisible to git, which is not the same as free.control_cache="$cache_dir/control"cp -a "$cache_dir/real" "$control_cache"control_sgb=$(find "$control_cache" -path '*/sigil/doc/reader.sgb' -print -quit)test -n "$control_sgb" || { echo "positive control: no reader.sgb in the copied cache" >&2; exit 1; }# (b) first, because it needs no compiler and a failure here invalidates (a) too.inplace_before=$(stat -c '%y:%s:%i' "$control_sgb")printf '\0' >>"$control_sgb"inplace_after=$(stat -c '%y:%s:%i' "$control_sgb")test "$inplace_before" != "$inplace_after" || { echo "POSITIVE CONTROL (b) FAILED: the stat probe cannot see an in-place rewrite." >&2 echo " before: $inplace_before" >&2 echo " after: $inplace_after" >&2 echo " The 'before = after' assertion below therefore proves nothing." >&2; exit 1; }# (a) now: delete the compiled reader from the copy and re-run the same# manuscript against that cache. It must be written back to the same path.rm -f "$control_sgb"XDG_CACHE_HOME="$control_cache" setsid -w "$sigil" -L src -L test \ eval -f test/fixtures/practical-sigil-chapter.sgd >"$cache_dir/control.log" 2>&1 || { echo "positive control (a): the forced-recompile run failed" >&2 tail -30 "$cache_dir/control.log" >&2; exit 1; }test -f "$control_sgb" || { echo "POSITIVE CONTROL (a) FAILED: a recompile did not land at the path this arm watches." >&2 echo " expected: $control_sgb" >&2 echo " A rebuild could therefore happen without moving 'before'/'after' at all." >&2; exit 1; }rm -rf "$control_cache"echo "D2 cache-reuse probe armed: in-place rewrite visible to stat ($inplace_before -> $inplace_after); a rebuild lands at the watched path"# THE ASSERTION ITSELF, now that both halves of the probe have been shown to fire.before=$(stat -c '%y:%s:%i' "$reader_sgb")setsid "$sigil" -L src -L test eval -f test/fixtures/practical-sigil-chapter.sgdsetsid -w "$sigil" -L src -L test eval -f test/fixtures/practical-sigil-chapter.sgdafter=$(stat -c '%y:%s:%i' "$reader_sgb")test "$before" = "$after"echo "D2_REAL_LANGUAGE_GREEN"test/gate-d3.shmodified
rg -q "$pattern" "$log"}setsid "$sigil" -L src -L test test test/test-examples.sgl --report | tee "$gate_dir/model.log"setsid -w "$sigil" -L src -L test test test/test-examples.sgl --report | tee "$gate_dir/model.log"rg -q '5 passed' "$gate_dir/model.log"; pass 1 MODEL_5_5# Loading, traversal, and rendering are passive: the deliberately infinite# timeout subject is present but these operations terminate without a verifier.setsid "$sigil" -L src -L test eval \setsid -w "$sigil" -L src -L test eval \ '(begin (import (sigil doc) (sigil doc examples)) (load "test/fixtures/d3-examples.sgd") (define d (eval (quote document))) (display (length (document-examples d))) (display "|") (display (length (document-events d))) (display "|") (display (string-length (document->plain d))))' \ >"$gate_dir/passive.log"rg -q '7|8|' "$gate_dir/passive.log"; pass 2 PASSIVE_LOAD_TRAVERSE_RENDER# ARM 2'S ASSERTION WAS `rg -q '7|8|'`, WHICH MATCHES EVERY NON-EMPTY LINE.## The intent was the probe's literal `|`-delimited output. As a REGEX, `7|8|` is# "7, or 8, or the empty string", and the empty alternative matches anything.# Measured 2026-08-22: it matches `total nonsense here`, and it matches# `ERROR: everything exploded`. The only input it refused was a completely empty# file -- which is what the bare `setsid` above it made likely, so the race was# the only thing giving this arm any teeth at all. Making the command wait, on# its own, would have achieved nothing here.## BOTH COUNTS ARE NOW DERIVED FROM THE FIXTURE rather than written in, so a# fixture that gains an example fails here instead of going silently unchecked.# The event stream carries one event per example plus one per project-check.d3_fixture=test/fixtures/d3-examples.sgdexpect_examples=$(grep -cE '^@example\[' "$d3_fixture")expect_checks=$(grep -cE '^@project-check\[' "$d3_fixture")expect_events=$((expect_examples + expect_checks))test "$expect_examples" -ge 1 || { echo "derived 0 examples from $d3_fixture; the derivation is broken, not the product" >&2; exit 1; }# NEITHER END IS ANCHORED, and both halves of that are measured rather than# assumed. The compiler may emit chatter on the same stdout line the probe's# first `display` continues; and the `eval` ECHOES ITS OWN RETURN VALUE after the# three displays, so the real line is `7|8|531#<undefined>` and a `$` anchor# fails on it. A `[^0-9]` bound on each side is what makes `17|8|...` and# `...|8|0` both fail while the real output passes.passive_pattern="(^|[^0-9])${expect_examples}\|${expect_events}\|[1-9][0-9]*([^0-9]|\$)"# POSITIVE CONTROLS, BOTH DIRECTIONS AND THEN SOME. The pattern must REFUSE what# the old one accepted, ACCEPT the real observed line -- otherwise a pattern that# refuses everything satisfies the first check for free -- and refuse a zero# length, which is the shape a stalled or empty render produces.## The "known-good" control is the line this probe ACTUALLY EMITS, echoed return# value and all, not a tidied version of it. A control built from what the output# ought to look like is how a pattern ends up passing its own controls and# failing the product.printf 'ERROR: everything exploded\n' >"$gate_dir/passive-control-bad.log"if rg -q "$passive_pattern" "$gate_dir/passive-control-bad.log"; then echo "arm 2's pattern matches arbitrary output; it is not an assertion" >&2; exit 1; fiprintf '%s|%s|0#<undefined>\n' "$expect_examples" "$expect_events" >"$gate_dir/passive-control-zero.log"if rg -q "$passive_pattern" "$gate_dir/passive-control-zero.log"; then echo "arm 2's pattern accepts a zero-length rendering" >&2; exit 1; fiprintf '%s|%s|531#<undefined>\n' "$expect_examples" "$expect_events" >"$gate_dir/passive-control-ok.log"rg -q "$passive_pattern" "$gate_dir/passive-control-ok.log" || { echo "arm 2's pattern rejects the shape this probe really emits; it is a wall, not a check" >&2; exit 1; }rg -q "$passive_pattern" "$gate_dir/passive.log" || { echo "passive load/traverse/render did not report ${expect_examples}|${expect_events}|<non-zero length>" >&2 echo "observed:" >&2; cat "$gate_dir/passive.log" >&2; exit 1; }pass 2 "PASSIVE_LOAD_TRAVERSE_RENDER ${expect_examples} examples, ${expect_events} events, both derived from $d3_fixture"verify=("$sigil" -L src -L test tools/sigil-doc-verify.sgl test/fixtures/practical-sigil-chapter.sgd)"${verify[@]}" --no-cache --example stdout | tee "$gate_dir/stdout.log"test/gate-sd-p1.shmodified
suite_log="$gate_dir/root-profile-suite.log"# NOT `--report`: that flag captures the suite's stdout, and this arm asserts on# a denominator the suite PRINTS.if ! setsid "$sigil" -L src -L test test test/test-root-profile.sgl >"$suite_log" 2>&1; thenif ! setsid -w "$sigil" -L src -L test test test/test-root-profile.sgl >"$suite_log" 2>&1; then echo "root-profile suite exited non-zero" >&2; tail -40 "$suite_log" >&2; exit 1fi# Strip ANSI colour before matching, so the assertion depends on the runner's# words rather than on its escape sequences.sed -e 's/\x1b\[[0-9;]*m//g' "$suite_log" >"$suite_log.clean"rg -q 'Tests:\s+11 passed \(11 total\)' "$suite_log.clean" || { echo "root-profile suite did not report 11 of 11 tests" >&2# THE COUNT IS DERIVED FROM THE SUITE FILE, NOT WRITTEN IN.## This line asserted a hardcoded `Tests: 11 passed (11 total)`. A TWELFTH test# was added to test/test-root-profile.sgl and the literal went stale, so this# gate has been RED ON MASTER against a suite that passes 12/12 -- the gate# lying about a healthy product. Confirmed 2026-08-22 by running a pristine copy# built from `git show HEAD:`: exit 1, "did not report 11 of 11 tests", with the# runner reporting `Tests: 12 passed (12 total)` and `All tests passed`.## That is the same defect class as an assertion that cannot fail, pointing the# other way: a check that has stopped describing its subject. folio's# test/gate-f1a.sh states the rule in its own header and derives its count# exactly like this -- "Every count this gate checks is DERIVED FROM THE ARTIFACT# it describes, never written in as a literal. [...] That drift is the single# most common defect in this estate's gates." Adding a test must not require# editing a number here, and now it does not.## NOTE FOR WHOEVER TOUCHES THE SIBLING: test/gate-sd-p4.sh:45 hardcodes 17 the# same way. It still matches its suite, so it is green today and is the next one# to go stale. Surveyed, deliberately not changed in this branch.expected_tests=$(grep -cE '^\(test "' test/test-root-profile.sgl)test "$expected_tests" -ge 1 || { echo "derived 0 tests from test/test-root-profile.sgl; the DERIVATION is broken, not the suite" >&2 exit 1; }reported=$(rg -o 'Tests:[[:space:]]+[0-9]+ passed' "$suite_log.clean" | head -1 | grep -oE '[0-9]+' | head -1)collected=$(rg -o '\([0-9]+ total\)' "$suite_log.clean" | head -1 | grep -oE '[0-9]+' | head -1)test -n "$reported" -a -n "$collected" || { echo "root-profile suite printed no summary line" >&2 tail -40 "$suite_log.clean" >&2; exit 1; }# Every test the FILE declares must have been collected, and every test collected# must have passed. The first catches a test that silently stopped running; the# second catches a failure the summary would otherwise bury.test "$collected" = "$expected_tests" || { echo "test/test-root-profile.sgl declares $expected_tests tests but the runner collected $collected" >&2 tail -40 "$suite_log.clean" >&2; exit 1; }test "$reported" = "$collected" || { echo "root-profile suite reported $reported passed of $collected collected" >&2 tail -40 "$suite_log.clean" >&2; exit 1; }rg -q 'All tests passed' "$suite_log.clean" || { echo "root-profile suite did not report all tests passing" >&2test -n "$roots" || { echo "suite printed no root-count denominator" >&2; tail -40 "$suite_log.clean" >&2; exit 1; }test "$roots" -ge 2 || { echo "location walk covered only $roots roots" >&2; exit 1; }pass 1 "root-profile suite 11/11, locations verified at $roots roots"pass 1 "root-profile suite $reported/$expected_tests, locations verified at $roots roots"# ---------------------------------------------------------------- ARM 02# The classification change is CONFINED to the two colliding spellings. echo "the BEFORE transcript carries no pre-SD-P1 positive control; it cannot be trusted" >&2 exit 1; }after_log="$gate_dir/classification-after.txt"setsid "$sigil" -L src -L test eval -f test/sd-p1-classification-probe.sgl >"$after_log" 2>&1setsid -w "$sigil" -L src -L test eval -f test/sd-p1-classification-probe.sgl >"$after_log" 2>&1# Derived, not hardcoded -- see test/sd-p1-before-transcript.sh for why.cases=$(rg -c '^\(report "' test/sd-p1-classification-probe.sgl || echo 0)test "$cases" -ge 1 || { echo "could not count the probe's cases" >&2; exit 1; }(display "SD_P1_DELEGATION roots=") (display (length (cadr parts)))(display " forms=") (display (length (car parts))) (newline)PROBEsetsid "$sigil" -L src -L "$gate_dir" eval -f "$gate_dir/delegate-probe.sgl" >"$delegate_log" 2>&1 || {setsid -w "$sigil" -L src -L "$gate_dir" eval -f "$gate_dir/delegate-probe.sgl" >"$delegate_log" 2>&1 || { echo "delegation probe failed" >&2; cat "$delegate_log" >&2; exit 1; }rg -q 'SD_P1_DELEGATION roots=2 forms=0' "$delegate_log" || { echo "delegation probe did not deliver two roots from the public module" >&2test/gate-sd-p4.shmodified
# NOT `--report`: that flag captures the suite's stdout, and this arm asserts on# a denominator the suite PRINTS. Running with it made this gate exit silently# on a suite that had actually passed 14/14.if ! setsid "$sigil" -L src -L test test test/test-passive-parse.sgl >"$suite_log" 2>&1; thenif ! setsid -w "$sigil" -L src -L test test test/test-passive-parse.sgl >"$suite_log" 2>&1; then echo "passive suite exited non-zero" >&2; tail -40 "$suite_log" >&2; exit 1fi# Strip ANSI colour before matching, so the assertion depends on the runner's# (a) POSITIVE CONTROL FIRST. The fixture's markers must appear when the# document is genuinely evaluated through the real #lang path. Until this# passes, the absence assertion below means nothing.setsid "$sigil" -L src eval -f test/fixtures/sd-p4-witness.sgd >"$witness_control" 2>&1setsid -w "$sigil" -L src eval -f test/fixtures/sd-p4-witness.sgd >"$witness_control" 2>&1rg -q 'SDP4-WITNESS-AUTHOR-EXPRESSION-RAN' "$witness_control" || { echo "positive control: evaluating the fixture did NOT run its author expression" >&2 cat "$witness_control" >&2; exit 1; }# (b) THE ASSERTION. Passive inspection, in a fresh process importing only the# inspection surface, must run none of it.setsid "$sigil" -L src -L test eval -f test/sd-p4-witness-probe.sgl >"$witness_passive" 2>&1setsid -w "$sigil" -L src -L test eval -f test/sd-p4-witness-probe.sgl >"$witness_passive" 2>&1rg -q 'SD_P4_WITNESS_PROBE_INSPECTED' "$witness_passive" || { echo "witness probe did not run to completion" >&2; cat "$witness_passive" >&2; exit 1; }# ...and it must have actually traversed something.test/gate-setsid-waits.shadded
#!/usr/bin/env bash## No shell script in this repository may invoke the session wrapper without# telling it to wait.## WHY THIS IS A GATE AND NOT A ONE-TIME SWEEP.## `setsid <cmd> > log` does not reliably block. util-linux forks when the setsid# PROCESS is itself a process-group leader and the parent returns 0 at once while# the child runs on. Whether that happens is a property of the CALLING SHELL, not# of the command: bash puts every job in its own process group when job control# is on, so the same line waits inside a script and does NOT wait when a human# runs it at an interactive prompt. Measured on this host 2026-08-22, `bash -i`# over a pty:## bare exit=0 elapsed_ms=5 logbytes=0# -w exit=0 elapsed_ms=3011 logbytes=16## and in the `if ! ...` form the bare version reported SUCCESS for a child that# exited 1. Every gate in this repo is run by hand -- there is no CI here -- so# the interactive prompt is the normal case, not the exotic one.## A sweep fixes today's sites and cannot see tomorrow's. Every bare site this# repo ever had was written next to another one.## THE POPULATION IS DERIVED FROM THE ARTIFACT: every `*.sh` in the repository,# found by `find`, not a list anybody maintains. The number of files scanned is# printed, because a scan that found zero files would otherwise look identical to# a clean one.## WHAT THIS CANNOT SEE, stated rather than left for someone to discover:# invocation through a variable or an alias (`S=setsid; $S "$sigil" ...`). This# is a text scanner and that construct defeats it. It is not used anywhere in# this repo today; if it ever is, this gate will not notice, and that is a# genuine hole rather than a hypothetical one.## It DOES see invocations inside quoted strings -- `eval "..."`, `sh -c '...'`,# and lines that merely happen to contain an apostrophe. An earlier version# skipped anything with odd quote parity to its left, which meant one apostrophe# anywhere earlier on the line disarmed the check for a real, executed call. A# prose mention that gets flagged costs a rewording; a missed call site costs a# false green, so the strictness runs that way deliberately.set -euo pipefailrepo_dir="$(cd "$(dirname "$0")/.." && pwd)"cd "$repo_dir"command -v awk >/dev/null || { echo "awk is required by this gate" >&2; exit 1; }command -v find >/dev/null || { echo "find is required by this gate" >&2; exit 1; }work=$(mktemp -d)trap 'rm -rf "$work"' EXIT INT TERM# The detector. An occurrence is ACCEPTED only when `-w`/`--wait` appears among# the option tokens that immediately follow it, before the command word. `-f` /# `--fork` alone is NOT acceptance: `-f` FORCES the racy branch. Anything the# parser cannot account for is REPORTED rather than waved through -- a detector# that fails open is the thing it exists to catch.## Comment lines are skipped, and only comment lines: a `#`-led line cannot# execute, and this gate's own prose has to spell the bad pattern out.## `\`-continuations are JOINED first, so an invocation whose options sit on the# next physical line is read as one logical line rather than reported as bare.cat >"$work/detect.awk" <<'AWK'{ line = $0 while (line ~ /\\$/ && (getline nxt) > 0) { sub(/\\$/, "", line) sub(/^[[:space:]]+/, " ", nxt) line = line nxt } stripped = line sub(/^[ \t]*/, "", stripped) if (substr(stripped, 1, 1) == "#") next rest = line while (match(rest, /(^|[^-A-Za-z0-9_])setsid[ \t]+/)) { rest = substr(rest, RSTART + RLENGTH) waits = 0 tail = rest # Consume the leading option run. The trailing `[ \t]*$` alternative is what # lets an option that ends the line count -- without it, `... -w` as the last # token on a line was reported as bare. while (match(tail, /^-[^ \t]+([ \t]+|$)/)) { opt = substr(tail, RSTART, RLENGTH) sub(/[ \t]+$/, "", opt) if (opt == "--wait") waits = 1 else if (opt !~ /^--/ && opt ~ /w/) waits = 1 tail = substr(tail, RLENGTH + 1) } if (!waits) { print FILENAME ":" FNR ":" line } }}AWKscan() { # scan <dir> -- one record per bare occurrence find "$1" -name '.git' -prune -o -type f -name '*.sh' -print \ | sort \ | while IFS= read -r file; do awk -f "$work/detect.awk" "$file"; done}# --------------------------------------------------------------- CONTROLS# Planted into a throwaway directory, NEVER into the working tree, so there is# no restore step that has to be reached.## THE FIXTURES ARE ASSEMBLED, NEVER SPELLED. This gate scans its own repository,# and a fixture written out literally here would be found by that scan and# reported as a real call site -- a check matching its own prose. Building the# lines through `$s` means the offending text does not appear anywhere in this# file outside a comment.ctl="$work/controls"mkdir -p "$ctl"s=setsid{ printf '%s "$sigil" test suite.sgl >"$log" 2>&1\n' "$s" printf 'if ! %s "$sigil" eval -f probe.sgl >"$log"; then echo no; fi\n' "$s" printf 'out=$(%s -f "$sigil" eval "(display 1)")\n' "$s" printf '%s --fork "$sigil" build\n' "$s" printf ' indented=$(%s "$sigil" --version)\n' "$s" printf 'echo "the parser%ss output"; %s "$sigil" test suite.sgl\n' "'" "$s" printf 'eval "%s $sigil test >log"\n' "$s" printf "sh -c '%s \$sigil test'\n" "$s" printf '%s "$sigil" -w test suite.sgl\n' "$s"} >"$ctl/bad.sh"{ printf '%s -w "$sigil" test suite.sgl >"$log" 2>&1\n' "$s" printf '%s -f --wait "${verify[@]}" --example timeout\n' "$s" printf '%s --wait "$sigil" build\n' "$s" printf 'FOO=bar %s -w "$sigil" eval -f probe.sgl\n' "$s" printf '%s -fw "$sigil" eval -f probe.sgl\n' "$s" printf '%s -w\n' "$s" printf '%s \\\n -w "$sigil" test suite.sgl\n' "$s" printf '# %s "$sigil" test suite.sgl <- a comment, and comments do not run\n' "$s"} >"$ctl/good.sh"# The fixtures must contain what they claim to, or every control below passes# over an empty file.bad_lines=9good_forms=8good_physical=9 # one form deliberately spans two physical linestest "$(grep -c . "$ctl/bad.sh")" = "$bad_lines" || { echo "bad.sh fixture is not $bad_lines lines" >&2; exit 1; }test "$(grep -c . "$ctl/good.sh")" = "$good_physical" || { echo "good.sh fixture is not $good_physical physical lines" >&2; exit 1; }ctl_bare=$(scan "$ctl" || true)ctl_bad=$(printf '%s\n' "$ctl_bare" | grep -c '/bad\.sh:' || true)ctl_good=$(printf '%s\n' "$ctl_bare" | grep -c '/good\.sh:' || true)# Nine planted bare forms: plain, `if !`, `-f` only, `--fork` only, indented in a# substitution, one hidden behind an apostrophe, one inside an `eval` string, one# inside an `sh -c` string, and one where `-w` sits AFTER the command word and so# is an argument to the command rather than an option to the wrapper.test "$ctl_bad" = "$bad_lines" || { echo "POSITIVE CONTROL FAILED: detector flagged $ctl_bad of $bad_lines planted bare invocations" >&2 printf '%s\n' "$ctl_bare" | grep '/bad\.sh:' >&2 || true exit 1; }# Eight waiting or inert forms, including `-w` as the final token and `-w` on a# continuation line -- both of which an earlier version reported as bare.test "$ctl_good" = "0" || { echo "NEGATIVE CONTROL FAILED: detector flagged $ctl_good of $good_forms waiting/inert form(s) as bare" >&2 printf '%s\n' "$ctl_bare" | grep '/good\.sh:' >&2; exit 1; }echo "detector armed: $bad_lines/$bad_lines planted bare invocations flagged (quoted, eval'd and post-command -w included), 0/$good_forms waiting or inert forms misflagged"# --------------------------------------------------------------- POPULATIONfiles=$(find . -name '.git' -prune -o -type f -name '*.sh' -print | wc -l)test "$files" -ge 1 || { echo "scanned 0 shell scripts; the population derivation is broken" >&2; exit 1; }occurrences=$(grep -rn --include='*.sh' 'setsid' . 2>/dev/null | wc -l)echo "population: $files shell script(s); $occurrences line(s) mention it"bare=$(scan "." || true)if [ -n "$bare" ]; then echo "BARE INVOCATION(S) that do not reliably wait:" >&2 printf '%s\n' "$bare" | sed 's/^/ /' >&2 echo >&2 echo "VERIFY THIS GATE'S OWN PRECONDITIONS BEFORE ACTING ON THIS MESSAGE:" >&2 echo " - the detector's controls passed above, so it sees what it claims to see;" >&2 echo " - the line quoted is the logical line, with continuations already joined;" >&2 echo " - an occurrence inside a quoted string is reported deliberately, because" >&2 echo " eval and sh -c strings are real invocations. If yours is prose, reword it." >&2 echo "Then: pass -w so it waits and propagates the child's exit status, or drop the" >&2 echo "wrapper entirely -- the sigil test runner does not need a new session." >&2 echo "See folio topics/setsid-does-not-reliably-wait." >&2 exit 1fiecho "SETSID_WAITS_GREEN 0 bare invocation(s) across $files shell script(s), $occurrences line(s) mentioning it"test/sabotage-sd-p1.shmodified
test "$before_hash" != "$after_hash" || { echo "SABOTAGE DID NOT CHANGE $file" >&2; exit 1; }}# The COLLECTED test count, measured from the leg runs below rather than# written in. The closing green check at the end of this driver needs a# denominator; a literal one goes stale the first time somebody adds a test,# and a stale denominator in a check is how a gate stops describing its# subject and becomes a second thing to keep in sync.collected_total=0# expect_suite_red <label> [<expected failing test name fragment>]expect_suite_red() { local label="$1" expect="${2:-}" log="$work/$(echo "$label" | tr ' /' '__').log" status local label="$1" expect="${2:-}" status log # SPLIT DELIBERATELY. `local a="$1" b="$(... $a ...)"` expands EVERY # assignment before `local` runs, so `$a` is still UNBOUND at that point. # Under `set -u` this printed `label: unbound variable` and evaluated to # `$work/.log` for every leg, so all seven legs overwrote one another's # log and the per-leg evidence this driver is built around did not exist. # It is in the committed transcript seven times # (test/transcripts/sd-p1/sabotage.txt, reported at "line 84"). log="$work/$(printf '%s' "$label" | tr ' /' '__').log" set +e setsid "$sigil" -L src -L test test "$suite" >"$log" 2>&1 setsid -w "$sigil" -L src -L test test "$suite" >"$log" 2>&1 status=$? set -e sed -e 's/\x1b\[[0-9;]*m//g' "$log" >"$log.clean" rg -q '[0-9]+ passed, [0-9]+ failed' "$log.clean" || { echo "$label: no runner summary -- the suite did not run; this is NOT a demonstrated red" >&2 tail -40 "$log.clean" >&2; exit 1; } # Record this run's collected count (passed + failed) for the closing check. local summary p f summary=$(rg -o '[0-9]+ passed, [0-9]+ failed' "$log.clean" | head -1) p=${summary%% passed*} f=${summary#*, }; f=${f%% failed*} if [ $((p + f)) -gt "$collected_total" ]; then collected_total=$((p + f)); fi test "$status" -ne 0 || { echo "$label: SABOTAGE DID NOT TURN THE SUITE RED" >&2; exit 1; } rg -o '[0-9]+ passed, [0-9]+ failed' "$log.clean" | head -1 rg -o '✗ .*' "$log.clean" | head -6 || trueecho "while the gate's scoring and exit logic are left untouched. If arm 2"echo "still passes, it was passing for free."after_now="$work/after-now.txt"setsid "$sigil" -L src -L test eval -f test/sd-p1-classification-probe.sgl >"$after_now" 2>&1setsid -w "$sigil" -L src -L test eval -f test/sd-p1-classification-probe.sgl >"$after_now" 2>&1# DERIVED, not hardcoded. This exact guard shipped with a stale "8 cases" and# aborted leg 8 on a probe that had run all 9 correctly -- the fourth instance# of this hazard in one session. It failed SAFE (it refused to forge with inputfor f in "$parser" "$reader" "$suite" "$before"; do printf ' %s %s\n' "$(hash_of "$f")" "$f"donesetsid "$sigil" -L src -L test test "$suite" 2>&1 | sed -e 's/\x1b\[[0-9;]*m//g' | tail -8echo "SD_P1_SABOTAGE_COMPLETE 8 legs"# THE CLOSING RUN ASSERTED NOTHING ABOUT ITS OUTPUT UNTIL 2026-08-22, AND IT IS# THE ONLY THING IN THIS DRIVER THAT PROVES RED->GREEN.## It was `setsid "$sigil" ... | sed ... | tail -8`, with no marker check of any# kind, and `SD_P1_SABOTAGE_COMPLETE 8 legs` printed on the next line.## WHAT WAS AND WAS NOT WRONG WITH IT. The first version of this comment blamed a# `setsid` race, a leader acted on that, and it was wrong -- so the correction# lives here rather than being quietly dropped.## NOT wrong: the exit status. `set -o pipefail` is on, so a suite exiting# non-zero would have aborted the script. And a PIPELINE blocks even when# `setsid` takes its fork branch, because the shell waits for EOF on the pipe# and the grandchild holds the write end. Measured on the forced fork branch:# 3020 ms for the piped form against a 3-second child, versus 10 ms for the# same command redirected to a file. This line was never racy. The racy shape# is redirect-to-a-file, which is a different set of call sites.## Wrong, and still disqualifying: NOTHING ABOUT THE CONTENT WAS CHECKED. A# runner that exited 0 having collected zero tests, or having printed nothing# at all, satisfied every condition this block imposed, and the completion# marker went out regardless. A step whose whole job is to prove red->green has# to read what the runner actually said.## It now asserts the RUNNER'S OWN WORDS from a log on disk, requires the run to# exit 0, and -- because an assertion that has never been shown to fire cannot# certify anything -- is armed against BOTH a truncated log and a known-good# summary before the real run happens.closing_log="$work/closing-green.log"# assert_suite_green <plain-log> <minimum collected count># 0 = the log shows a complete green run; 1 = refuse, with the reason.assert_suite_green() { local log="$1" floor="$2" passed total rg -q 'All tests passed' "$log" || { echo " closing check: no 'All tests passed' banner" >&2; return 1; } passed=$(rg -o 'Tests:[[:space:]]+[0-9]+ passed' "$log" | head -1 | grep -oE '[0-9]+' | head -1) total=$(rg -o '\([0-9]+ total\)' "$log" | head -1 | grep -oE '[0-9]+' | head -1) test -n "$passed" -a -n "$total" || { echo " closing check: the runner printed no summary line" >&2; return 1; } # Self-consistency: every collected test must be a passing one. This needs no # literal and cannot go stale. test "$passed" = "$total" || { echo " closing check: $passed passed of $total collected" >&2; return 1; } # ...and a FLOOR, because "0 passed (0 total)" is self-consistent too. A bound # satisfied by a suite that collected nothing is not a bound. # # WHAT THIS FLOOR IS AND IS NOT. It is derived from the same suite it then # bounds -- the maximum collected count observed across the legs above -- so it # cannot go stale when a test is added, and it cannot become unsatisfiable # (sabotage reduces or preserves collection, never increases it). It also # cannot detect a suite that shrank: gut the file from 11 tests to 1 and every # leg still reddens on the survivor, the floor becomes 1, and this reports # "green over 1 collected tests". It catches total collapse to zero and nothing # weaker. `test/gate-sd-p1.sh` arm 1 is what pins the absolute count. test "$total" -ge "$floor" || { echo " closing check: collected $total, but the legs above collected $floor" >&2; return 1; } if rg -q '[1-9][0-9]* failed' "$log"; then echo " closing check: the runner reported failures" >&2; return 1; fi return 0}test "$collected_total" -ge 1 || { echo "no leg recorded a collected test count; the closing check has no denominator" >&2; exit 1; }# POSITIVE CONTROL 1 -- it must REFUSE the exact artifact the fork branch# produces. `Running tests...` and nothing else is the real 18-byte log from the# 2026-08-11 measurement.printf 'Running tests...\n' >"$closing_log.truncated"if assert_suite_green "$closing_log.truncated" "$collected_total" 2>/dev/null; then echo "CLOSING CHECK CANNOT FAIL: it accepted a truncated log" >&2; exit 1; fi# POSITIVE CONTROL 2 -- and it must ACCEPT a known-good summary, or control 1# passes for free: a check that refuses everything refuses truncation too.printf 'Tests: %s passed (%s total)\nAll tests passed\n' "$collected_total" "$collected_total" \ >"$closing_log.control"assert_suite_green "$closing_log.control" "$collected_total" || { echo "CLOSING CHECK REFUSES A KNOWN-GOOD SUMMARY -- it is a wall, not a check" >&2; exit 1; }# POSITIVE CONTROL 3 -- the floor must bite. A self-consistent empty run is the# shape a stalled or mis-filtered runner produces, and it must not be accepted.printf 'Tests: 0 passed (0 total)\nAll tests passed\n' >"$closing_log.empty"if assert_suite_green "$closing_log.empty" "$collected_total" 2>/dev/null; then echo "CLOSING CHECK CANNOT FAIL: it accepted a run that collected 0 tests" >&2; exit 1; fi# POSITIVE CONTROL 4 -- the `passed == total` arm must bite on its own. Controls# 1-3 are all refused by the banner or the floor before this arm is reached, so# without this one the arm could be deleted and every control would still pass.printf 'Tests: %s passed (%s total)\nAll tests passed\n' \ "$((collected_total - 1))" "$collected_total" >"$closing_log.short"if assert_suite_green "$closing_log.short" "$collected_total" 2>/dev/null; then echo "CLOSING CHECK CANNOT FAIL: it accepted a run where not every collected test passed" >&2 exit 1; fi# POSITIVE CONTROL 5 -- and so must the `N failed` arm, which controls 1-4 also# leave untouched: a log can be self-consistent, banner-carrying, at or above the# floor, AND still report failures.printf 'Tests: %s passed (%s total)\nAll tests passed\n2 failed\n' \ "$collected_total" "$collected_total" >"$closing_log.failing"if assert_suite_green "$closing_log.failing" "$collected_total" 2>/dev/null; then echo "CLOSING CHECK CANNOT FAIL: it accepted a log that reported failures" >&2 exit 1; fiecho "closing check armed: refuses a truncated log, an empty run, a partial pass and a reported failure; accepts a green summary"set +esetsid -w "$sigil" -L src -L test test "$suite" >"$closing_log" 2>&1closing_status=$?set -esed -e 's/\x1b\[[0-9;]*m//g' "$closing_log" >"$closing_log.clean"tail -8 "$closing_log.clean"test "$closing_status" -eq 0 || { echo "CLOSING RUN EXITED $closing_status on RESTORED sources -- red->green is NOT demonstrated" >&2 tail -40 "$closing_log.clean" >&2; exit 1; }assert_suite_green "$closing_log.clean" "$collected_total" || { echo "CLOSING RUN did not demonstrate red->green on restored sources" >&2 tail -40 "$closing_log.clean" >&2; exit 1; }echo "SD_P1_SABOTAGE_COMPLETE 8 legs, closing suite green over $collected_total collected tests"test/sabotage-sd-p4.shmodified
expect_suite_red() { local label="$1" target="$2" log="$3" set +e setsid "$sigil" -L src -L test test "$target" >"$log" 2>&1 setsid -w "$sigil" -L src -L test test "$target" >"$log" 2>&1 local status=$? set -e echo "--- $label: exit=$status" "expression: (if (and (not command?) (eq? head 'expr)) (begin (eval (syntax->datum (caddr node))) (caddr node)) #f)"witness_log="$work/leg1-witness.log"set +esetsid "$sigil" -L src -L test eval -f test/sd-p4-witness-probe.sgl >"$witness_log" 2>&1setsid -w "$sigil" -L src -L test eval -f test/sd-p4-witness-probe.sgl >"$witness_log" 2>&1set -eif rg -q 'SDP4-WITNESS-' "$witness_log"; then echo "LEG 1 RED (correct): author markers appeared during passive inspection:"