Commit54c824daRecorded22 Aug 2026Repositorysigil-doc

test: derive gate-sd-p4's test count, and make both gates' floors reachable

Message

gate-sd-p4.sh ARM 01 asserted a hardcoded Tests: 17 passed (17 total). It matched, so it was green -- and green is what a stale literal looks like right up until someone adds a test. Its sibling had already gone the whole way: gate-sd-p1.sh hardcoded 11, a twelfth test was added to test-root-profile.sgl, and that gate sat red on master accusing a suite passing 12/12. This one was next.

ARM 01 now derives the count from the suite file and makes TWO assertions where there was one: every test the FILE declares must have been COLLECTED, which catches a test that silently stopped running, and every test collected must have PASSED, which catches a failure the summary would otherwise bury. pass 1 prints all three derived numbers, so the gate's transcript shows what it measured:

  SD_P4_GATE_01 passive suite 17 passed / 17 collected / 17 declared, ...

Adding a test no longer requires editing a number here.

TWO DEAD GUARDS FIXED, in both gates. Under set -euo pipefail a command substitution whose command fails aborts the script; grep -c and rg both exit 1 on zero matches. So the derivation floor ("the DERIVATION is broken, not the suite") and the no-summary guard ("passive suite printed no summary line") both died at their own assignments and never spoke. Exit 1 with no message is indistinguishable from a gate that never ran, which is the confusion this file's header says the estate has been fooled by twice -- and on a summary-less suite the derived form was therefore STRICTLY WORSE than the hardcoded literal it replaced, which did print a message and a log tail. Demonstrated by removing the two words again:

  fixed   rc=1 output=[passive suite printed no summary line]
  broken  rc=1 output=[]

A COMPLETENESS CHECK runs before the count. The anchor is column-0 (test " forms, so a test indented inside another form is still collected and invisible to it. Indenting one surfaces as "declares 16 but collected 17" -- the gate accusing a healthy suite for a defect in its own anchor, which is the same lie the literal told, one size smaller.

test/sabotage-sd-p4-counts.sh drives one red per assertion:

  LEG 1  product   declared > collected -> "declares 18 tests but the runner
                   collected 17"
  LEG 2  product   a test FAILS -> red, but via the pre-existing exit-status
                   check; the runner exits non-zero, so assertion two is not
                   reachable this way. Reported rather than dressed up.
  LEG 2b assertion the gate's summary assertions SLICED OUT OF THE GATE FILE by
                   sentinel comments and run under the same shell options, on
                   ANSI-coloured fixtures: fires on a buried failure, fires on a
                   missing summary, stands down on a healthy one. Slicing rather
                   than retyping is the point -- a copy agrees by construction,
                   and the first version of this leg certified semantics the
                   gate did not have.
  LEG 3  assertion the derivation's anchor defeated while the suite still passes
                   -> "the DERIVATION is broken, not the suite"
  LEG 4  assertion ONE form indented, which the floor cannot see
                   -> "has 1 INDENTED (test ..." forms"

Every restore is verified by SHA256 under a trap that fires on abort as well as exit, and every plant asserts it moved the thing it claims to move.

WHAT CHANGES IN gate-sd-p1.sh, and why it is in a gate-sd-p4 commit. Three things, none of them cosmetic:

  • expected_tests=$(grep -cE ... test-root-profile.sgl) gains || true. It shipped the dead derivation floor described above. This is the line gate-sd-p4.sh was told to copy, so copying it faithfully reproduced the defect -- which is how it was found at all.
  • reported=$(rg ...) and collected=$(rg ...) gain || true, the same dead-guard fix on the same class of input.
  • The NOTE FOR WHOEVER TOUCHES THE SIBLING comment is rewritten. It pointed at gate-sd-p4.sh's hardcoded 17 as an outstanding defect; that defect is fixed by this commit, so the pointer would have been a lie. It now names the CLASS that outlived both instances rather than the instance.

Each || true carries its reason in a comment directly above it, so blame on those lines lands on the mechanism rather than on this ticket.

Fixing a defect class in one file while leaving its source in the file next door is the sibling-gate pattern this repo keeps paying for: gate-sd-p1.sh went red on master for a whole ticket's worth of work because the lesson from a sibling never crossed over.

Changed
 test/gate-sd-p1.sh            |  28 ++++++++++++++----
 test/gate-sd-p4.sh            |  72 +++++++++++++++++++++++++++++++++++++++++++--
 test/sabotage-sd-p4-counts.sh | 261 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 352 insertions(+), 9 deletions(-)
Diff
test/gate-sd-p1.shmodified
@@ -63,15 +63,31 @@ sed -e 's/\x1b\[[0-9;]*m//g' "$suite_log" >"$suite_log.clean"
63
# most common defect in this estate's gates." Adding a test must not require
64
# editing a number here, and now it does not.
65
#
−66
# NOTE FOR WHOEVER TOUCHES THE SIBLING: test/gate-sd-p4.sh:45 hardcodes 17 the
−67
# same way. It still matches its suite, so it is green today and is the next one
−68
# to go stale. Surveyed, deliberately not changed in this branch.
−69
expected_tests=$(grep -cE '^\(test "' test/test-root-profile.sgl)
+66
# NOTE FOR WHOEVER TOUCHES THE SIBLING: test/gate-sd-p4.sh hardcoded 17 in
+67
# exactly this way and was fixed the same way, so this pointer no longer names
+68
# an outstanding defect. It is kept because the DEFECT CLASS outlived both
+69
# instances: a count written in rather than derived goes stale silently and the
+70
# gate then accuses healthy product code. Before adding a count to any gate in
+71
# this directory, derive it from the artifact it describes.
+72
#
+73
# `|| true` is load-bearing. Under `set -e` a command substitution whose command
+74
# fails ABORTS THE SCRIPT, and `grep -c` exits 1 when it matches nothing, so
+75
# without it a broken derivation kills this gate at this line and the guard
+76
# below never speaks. Exit 1 with no message is indistinguishable from a gate
+77
# that never ran. Same fix, same reason, in test/gate-sd-p4.sh; found there by
+78
# sabotage rather than by reading either file.
+79
expected_tests=$(grep -cE '^\(test "' test/test-root-profile.sgl || true)
80
test "$expected_tests" -ge 1 || {
81
echo "derived 0 tests from test/test-root-profile.sgl; the DERIVATION is broken, not the suite" >&2
82
exit 1; }
−73
reported=$(rg -o 'Tests:[[:space:]]+[0-9]+ passed' "$suite_log.clean" | head -1 | grep -oE '[0-9]+' | head -1)
−74
collected=$(rg -o '\([0-9]+ total\)' "$suite_log.clean" | head -1 | grep -oE '[0-9]+' | head -1)
+83
# `|| true` for the reason given above the derivation, and on the same class of
+84
# input: `rg` exits 1 when it matches nothing, `pipefail` propagates that out of
+85
# the substitution, and `set -e` would kill the gate on THIS line before the
+86
# guard below could say the suite printed no summary. Exit 1 with no message is
+87
# the least informative red available. Found by adversarial review of
+88
# test/gate-sd-p4.sh's equivalent block, not by running either gate.
+89
reported=$(rg -o 'Tests:[[:space:]]+[0-9]+ passed' "$suite_log.clean" | head -1 | grep -oE '[0-9]+' | head -1 || true)
+90
collected=$(rg -o '\([0-9]+ total\)' "$suite_log.clean" | head -1 | grep -oE '[0-9]+' | head -1 || true)
91
test -n "$reported" -a -n "$collected" || {
92
echo "root-profile suite printed no summary line" >&2
93
tail -40 "$suite_log.clean" >&2; exit 1; }
test/gate-sd-p4.shmodified
@@ -24,6 +24,7 @@ command -v rg >/dev/null || { echo "rg is required by this gate" >&2; exit 1; }
24
test -x "$sigil" || { echo "dev compiler not found at $sigil" >&2; exit 1; }
25
test -f src/sigil/doc/private/parser.sgl
26
test -f src/sigil/doc/inspect.sgl
+27
test -f test/test-passive-parse.sgl
28
test -f test/fixtures/sd-p4-witness.sgd
29
"$sigil" --version
30
@@ -42,8 +43,73 @@ fi
43
# Strip ANSI colour before matching, so the assertion depends on the runner's
44
# words rather than on its escape sequences.
45
sed -e 's/\x1b\[[0-9;]*m//g' "$suite_log" >"$suite_log.clean"
−45
rg -q 'Tests:\s+17 passed \(17 total\)' "$suite_log.clean" || {
−46
echo "passive suite did not report 17 of 17 tests" >&2; tail -40 "$suite_log.clean" >&2; exit 1; }
+46
# THE COUNT IS DERIVED FROM THE SUITE FILE, NOT WRITTEN IN.
+47
#
+48
# This line asserted a hardcoded `Tests: 17 passed (17 total)`. It matched, so
+49
# it was green -- and green is exactly what a stale literal looks like right up
+50
# until someone adds a test. Its sibling had already gone the whole way:
+51
# test/gate-sd-p1.sh hardcoded 11, a TWELFTH test was added to
+52
# test/test-root-profile.sgl, and that gate sat RED ON MASTER accusing a suite
+53
# that passes 12/12 -- the gate lying about healthy product code. It cost a
+54
# pristine-HEAD attribution run before anyone could even tell whose diff broke
+55
# it. Nothing about this file made it the second one rather than the first.
+56
#
+57
# folio's test/gate-f1a.sh states the rule in its own header and derives its
+58
# count exactly like this: "Every count this gate checks is DERIVED FROM THE
+59
# ARTIFACT it describes, never written in as a literal. [...] That drift is the
+60
# single most common defect in this estate's gates." Adding a test must not
+61
# require editing a number here, and now it does not.
+62
#
+63
# EVERY `|| true` BELOW IS LOAD-BEARING, NOT DEFENSIVE NOISE. Under `set -e` a
+64
# command substitution whose command fails ABORTS THE SCRIPT; `grep -c` exits 1
+65
# on zero matches and so does `rg`. Without them, the very inputs these guards
+66
# exist to describe kill the gate at the assignment and no guard ever speaks --
+67
# exit 1 with no message, which is indistinguishable from the gate not running,
+68
# the exact confusion this file's header says the estate has been fooled by
+69
# twice. The derivation case was found by sabotage; the summary case was found
+70
# by adversarial review, and on that input this instrumentation had been
+71
# strictly WORSE than the hardcoded literal it replaced, which did print a
+72
# message and a log tail.
+73
# THE COMPLETENESS CHECK COMES FIRST, and deliberately so. The anchor is
+74
# column-0 `(test "` forms; a test INDENTED inside another form is still
+75
# collected by the runner and invisible to it. Indenting ALL of them trips the
+76
# floor below, which then says "the DERIVATION is broken" -- true, but the least
+77
# specific true thing available. Indenting ONE trips neither the floor nor
+78
# anything else, and surfaces as "declares 16 but collected 17": the gate
+79
# accusing a healthy suite for a defect in its own anchor, which is a narrower
+80
# instance of exactly what this file was changed to stop doing. So ask about
+81
# completeness before asking about the count, and name the real defect.
+82
indented_tests=$(grep -cE '^[[:space:]]+\(test "' test/test-passive-parse.sgl || true)
+83
test "$indented_tests" -eq 0 || {
+84
echo "test/test-passive-parse.sgl has $indented_tests INDENTED (test \"...\" forms;" >&2
+85
echo "the column-0 derivation cannot see them, so the DERIVATION is incomplete" >&2
+86
grep -nE '^[[:space:]]+\(test "' test/test-passive-parse.sgl >&2; exit 1; }
+87
expected_tests=$(grep -cE '^\(test "' test/test-passive-parse.sgl || true)
+88
test "$expected_tests" -ge 1 || {
+89
echo "derived 0 tests from test/test-passive-parse.sgl; the DERIVATION is broken, not the suite" >&2
+90
exit 1; }
+91
# >>> ARM1_SUMMARY_ASSERTIONS BEGIN
+92
# test/sabotage-sd-p4-counts.sh LEG 2b slices these exact lines out of THIS file
+93
# and runs them against synthetic summaries under the same `set -euo pipefail`.
+94
# That is deliberate: a retyped copy would agree with this block by
+95
# construction. Keep the block self-contained -- it may read only
+96
# `$suite_log.clean` and `$expected_tests`.
+97
reported=$(rg -o 'Tests:[[:space:]]+[0-9]+ passed' "$suite_log.clean" | head -1 | grep -oE '[0-9]+' | head -1 || true)
+98
collected=$(rg -o '\([0-9]+ total\)' "$suite_log.clean" | head -1 | grep -oE '[0-9]+' | head -1 || true)
+99
test -n "$reported" -a -n "$collected" || {
+100
echo "passive suite printed no summary line" >&2
+101
tail -40 "$suite_log.clean" >&2; exit 1; }
+102
# TWO assertions, not one. A single derived count catches neither case cleanly:
+103
# every test the FILE declares must have been COLLECTED -- which catches a test
+104
# that silently stopped running -- and every test collected must have PASSED,
+105
# which catches a failure the summary would otherwise bury.
+106
test "$collected" = "$expected_tests" || {
+107
echo "test/test-passive-parse.sgl declares $expected_tests tests but the runner collected $collected" >&2
+108
tail -40 "$suite_log.clean" >&2; exit 1; }
+109
test "$reported" = "$collected" || {
+110
echo "passive suite reported $reported passed of $collected collected" >&2
+111
tail -40 "$suite_log.clean" >&2; exit 1; }
+112
# <<< ARM1_SUMMARY_ASSERTIONS END
113
rg -q 'All tests passed' "$suite_log.clean" || {
114
echo "passive suite did not report all tests passing" >&2; tail -40 "$suite_log.clean" >&2; exit 1; }
115
# The suite asserts this floor itself; reproducing it here is defence in depth
@@ -55,7 +121,7 @@ checked=$(rg -o "SD_P4_NODES_CHECKED [0-9]+" "$suite_log.clean" | head -1 | awk
121
test -n "$checked" || {
122
echo "suite printed no node-count denominator" >&2; tail -40 "$suite_log" >&2; exit 1; }
123
test "$checked" -ge 120 || { echo "span walk covered only $checked nodes" >&2; exit 1; }
−58
pass 1 "passive suite 17/17, spans verified at $checked nodes"
+124
pass 1 "passive suite $reported passed / $collected collected / $expected_tests declared, spans verified at $checked nodes"
125
126
# ---------------------------------------------------------------- ARM 02
127
# No-execution witness, WITH its positive control in the same arm.
test/sabotage-sd-p4-counts.shadded
@@ -0,0 +1,261 @@
+1
#!/usr/bin/env bash
+2
# Sabotage driver for gate-sd-p4.sh ARM 01's DERIVED test count.
+3
#
+4
# ARM 01 used to assert a hardcoded `Tests: 17 passed (17 total)`. It now derives
+5
# the declared count from test/test-passive-parse.sgl and makes TWO assertions:
+6
# declared == collected, and collected == passed. A derived count never shown to
+7
# DISCRIMINATE is a literal with extra steps, so this drives a red for each of
+8
# them, plus one for the derivation itself.
+9
#
+10
# LEG 1 product layer. A `(test "` form is planted at column 0 INSIDE a
+11
# function that is never called, so the derivation counts it and the
+12
# runner never collects it -- the shape of "a test silently stopped
+13
# running", which is what assertion one exists for.
+14
# LEG 2 product layer. A real toplevel test is planted that FAILS. It goes
+15
# red, but through the gate's pre-existing exit-status check rather
+16
# than assertion two, because the runner exits non-zero. Asserted on
+17
# that message, and reported as what it is rather than dressed up.
+18
# LEG 2b assertion layer. The gate's summary assertions are SLICED OUT OF THE
+19
# GATE FILE by its sentinel comments and run against synthetic
+20
# summaries under the same `set -euo pipefail`. Slicing rather than
+21
# retyping is the point: a retyped copy agrees with the gate by
+22
# construction, and the shell options differ, which is how the first
+23
# version of this leg certified semantics the gate did not have.
+24
# Includes an ANSI-coloured summary, because the real path reaches
+25
# these assertions only through the gate's colour strip.
+26
# LEG 3 assertion layer. The derivation's anchor is defeated while the suite
+27
# still passes -- the product is untouched -- and the gate must blame
+28
# the derivation rather than accuse the suite.
+29
# LEG 4 assertion layer. ONE form is indented, which the floor cannot see: the
+30
# derivation drops by one while the runner still collects them all, so
+31
# without the completeness check the gate accuses a healthy suite.
+32
#
+33
# Attribution anchors on output the PASSING path cannot produce: the gate prints
+34
# `SD_P4_GATE_01` only when arm 1 passes, so EVERY leg requires its specific
+35
# error text AND the absence of that marker. A bare non-zero exit proves
+36
# nothing; 126 and 127 produce one too.
+37
#
+38
# COST: the legs are cheap (each dies inside arm 1), but the POSITIVE CONTROL at
+39
# the end runs the gate to completion, including arm 4's book rebuild. Budget
+40
# roughly fifteen minutes for the run as a whole.
+41
#
+42
# The suite is restored from a copy and verified BY HASH under a trap that fires
+43
# on abort as well as exit. A sibling of this script has no trap, and an abort
+44
# between the damage and the repair left that corruption in the worktree.
+45
#
+46
# EVERY `|| true` here is load-bearing for the same reason it is in the gate:
+47
# under `set -e` a command substitution whose command fails aborts the script,
+48
# and `grep -c`/`rg` exit 1 on zero matches. Without them a plant that failed to
+49
# take would kill this driver at the assignment, and its SETUP-FAILED message --
+50
# the one that exists to say the leg is vacuous -- would never print. Three of
+51
# them were missing in the first version of this file, found by adversarial
+52
# review rather than by running it, because the plants all happened to work.
+53
set -euo pipefail
+54
repo_dir="$(cd "$(dirname "$0")/.." && pwd)"
+55
cd "$repo_dir"
+56
suite=test/test-passive-parse.sgl
+57
gate=test/gate-sd-p4.sh
+58
+59
command -v rg >/dev/null || { echo "SETUP-FAILED: rg is required" >&2; exit 2; }
+60
test -f "$suite" || { echo "SETUP-FAILED: $suite is missing" >&2; exit 2; }
+61
test -x "$gate" || { echo "SETUP-FAILED: $gate is missing or not executable" >&2; exit 2; }
+62
+63
work=$(mktemp -d "${TMPDIR:-/tmp}/sd-p4-counts-sabotage.XXXXXX")
+64
pristine="$work/suite.pristine"
+65
cp "$suite" "$pristine"
+66
base_sha=$(sha256sum "$suite" | awk '{print $1}')
+67
echo "baseline $suite sha256=$base_sha"
+68
+69
restore() {
+70
cp "$pristine" "$suite"
+71
local now; now=$(sha256sum "$suite" | awk '{print $1}')
+72
if [ "$now" = "$base_sha" ]; then
+73
echo "RESTORED $suite (sha256 matches baseline)"
+74
else
+75
echo "RESTORE FAILED: $suite is $now, baseline was $base_sha" >&2
+76
fi
+77
}
+78
# Restore FIRST, then clear scratch. Never the other way round: $pristine lives
+79
# in $work.
+80
cleanup() { restore; rm -rf "$work"; }
+81
trap cleanup EXIT INT TERM
+82
+83
declared_now() { grep -cE '^\(test "' "$suite" || true; }
+84
base_declared=$(declared_now)
+85
test "$base_declared" -ge 1 || {
+86
echo "SETUP-FAILED: the pristine suite derives $base_declared tests; nothing to sabotage" >&2
+87
exit 2; }
+88
echo "baseline derivation: $base_declared declared tests"
+89
echo
+90
+91
# Runs the gate and reports arm 1's verdict.
+92
expect_arm1_red() {
+93
local name="$1" expect="$2" log="$work/$1.log" rc=0
+94
"$gate" >"$log" 2>&1 || rc=$?
+95
echo "--- $name: derivation now reads $(declared_now); gate exit=$rc"
+96
if [ "$rc" -eq 0 ]; then
+97
echo " NOT RED: the gate passed. $name did not discriminate." >&2; return 1; fi
+98
if rg -q 'SD_P4_GATE_01' "$log"; then
+99
echo " MIS-ATTRIBUTED: arm 1 printed its pass marker, so the gate died later." >&2
+100
tail -5 "$log" >&2; return 1; fi
+101
if rg -qF "$expect" "$log"; then
+102
echo " RED, attributed to arm 1, and SD_P4_GATE_01 absent:"
+103
rg -NF "$expect" "$log" | head -2 | sed 's/^/ /' || true
+104
else
+105
echo " RED but with the WRONG message. Expected: $expect" >&2
+106
tail -12 "$log" >&2; return 1; fi
+107
}
+108
+109
# ---------------------------------------------------------------------- LEG 1
+110
echo "=== LEG 1 (product): declared > collected -- a test that stopped running ==="
+111
restore
+112
cat >>"$suite" <<'PLANT'
+113
+114
;; --- sabotage-sd-p4-counts LEG 1: declared at column 0, never collected ---
+115
(define (sd-p4-counts-sabotage-never-called)
+116
(test "sd-p4 counts sabotage -- declared, never collected"
+117
(assert-true #t)))
+118
PLANT
+119
planted=$(declared_now)
+120
test "$planted" -eq $(( base_declared + 1 )) || {
+121
echo "SETUP-FAILED: the plant did not move the derivation ($base_declared -> $planted)" >&2
+122
exit 2; }
+123
echo "plant verified: derivation moved $base_declared -> $planted"
+124
expect_arm1_red LEG1 "declares $planted tests but the runner collected $base_declared"
+125
echo
+126
+127
# ---------------------------------------------------------------------- LEG 2
+128
echo "=== LEG 2 (product): a collected test FAILS ==="
+129
restore
+130
cat >>"$suite" <<'PLANT'
+131
+132
;; --- sabotage-sd-p4-counts LEG 2: collected and failing ---
+133
(test "sd-p4 counts sabotage -- collected and failing"
+134
(assert-true #f))
+135
PLANT
+136
leg2_declared=$(declared_now)
+137
test "$leg2_declared" -eq $(( base_declared + 1 )) || {
+138
echo "SETUP-FAILED: the LEG 2 plant did not reach the suite" >&2; exit 2; }
+139
expect_arm1_red LEG2 "passive suite exited non-zero"
+140
echo " NOTE: the runner exits non-zero on a failing test, so the gate's"
+141
echo " pre-existing exit-status check fires before assertion two is reached."
+142
echo " Assertion two therefore guards a runner that reports a failure while"
+143
echo " exiting clean. LEG 2b exercises it on exactly that input."
+144
echo
+145
+146
# --------------------------------------------------------------------- LEG 2b
+147
echo "=== LEG 2b (assertion layer): the gate's OWN summary assertions, sliced ==="
+148
restore
+149
arm1="$work/arm1-assertions.sh"
+150
{
+151
echo 'set -euo pipefail'
+152
sed -n '/^# >>> ARM1_SUMMARY_ASSERTIONS BEGIN$/,/^# <<< ARM1_SUMMARY_ASSERTIONS END$/p' "$gate"
+153
echo 'echo "green ($reported/$collected/$expected_tests)"'
+154
} >"$arm1"
+155
# The slice must actually contain the assertions, or this leg silently tests an
+156
# empty script and passes.
+157
for needle in 'reported=' 'collected=' 'test "$collected" = "$expected_tests"' 'test "$reported" = "$collected"'; do
+158
grep -qF -- "$needle" "$arm1" || {
+159
echo "SETUP-FAILED: the sentinel slice from $gate is missing: $needle" >&2
+160
echo " (did the ARM1_SUMMARY_ASSERTIONS markers move?)" >&2; exit 2; }
+161
done
+162
echo "sliced $(grep -cvE '^\s*#|^\s*$' "$arm1") code lines out of $gate between its sentinels"
+163
+164
# Synthetic summaries, ANSI-coloured and then put through the GATE'S OWN strip,
+165
# because that is the only way the real path reaches these assertions.
+166
strip() { sed -e 's/\x1b\[[0-9;]*m//g' "$1" >"$2"; }
+167
n="$base_declared"; m=$(( base_declared - 1 ))
+168
printf ' \033[32mTests:\033[0m %d passed, 1 failed \033[2m(%d total)\033[0m\n' "$m" "$n" >"$work/buried.raw"
+169
printf ' \033[32mTests:\033[0m %d passed \033[2m(%d total)\033[0m\n' "$n" "$n" >"$work/healthy.raw"
+170
printf ' \033[2mno summary here at all\033[0m\n' >"$work/nosummary.raw"
+171
for f in buried healthy nosummary; do strip "$work/$f.raw" "$work/$f.clean"; done
+172
grep -q $'\033' "$work/buried.raw" || { echo "SETUP-FAILED: the coloured fixture carries no ANSI" >&2; exit 2; }
+173
grep -q $'\033' "$work/buried.clean" && { echo "SETUP-FAILED: the strip left ANSI behind" >&2; exit 2; }
+174
+175
run_arm1() { # $1=clean-log $2=expected_tests -> the sliced block's WHOLE output
+176
# NOT `tail -1`. Each failing branch in the gate prints its message and then
+177
# dumps `tail -40` of the log, so the last line is log content and the message
+178
# is buried above it. Matching on the last line reported LEG 2b as failing when
+179
# the assertion had in fact fired correctly -- the leg accused a working gate.
+180
suite_log="${1%.clean}" expected_tests="$2" bash "$arm1" 2>&1 || true
+181
}
+182
saw() { printf '%s' "$1" | grep -qF -- "$2"; }
+183
buried=$(run_arm1 "$work/buried.clean" "$n")
+184
healthy=$(run_arm1 "$work/healthy.clean" "$n")
+185
nosummary=$(run_arm1 "$work/nosummary.clean" "$n")
+186
saw "$buried" "reported $m passed of $n collected" && \
+187
echo " coloured buried-failure summary -> fired: reported $m passed of $n collected" || {
+188
echo " LEG 2b FAILED: assertion two did not fire on a buried failure. It said:" >&2
+189
printf '%s\n' "$buried" | head -5 >&2; exit 1; }
+190
saw "$healthy" "green ($n/$n/$n)" && \
+191
echo " coloured healthy summary -> stood down: green ($n/$n/$n)" || {
+192
echo " LEG 2b POSITIVE CONTROL FAILED: the assertions fired on a healthy summary:" >&2
+193
printf '%s\n' "$healthy" | head -5 >&2; exit 1; }
+194
saw "$nosummary" "printed no summary line" && \
+195
echo " no summary at all -> fired: printed no summary line" || {
+196
echo " LEG 2b FAILED: a missing summary did not produce the no-summary message." >&2
+197
echo " That is the guard that dies silently without its \`|| true\`. It said:" >&2
+198
printf '%s\n' "$nosummary" | head -5 >&2; exit 1; }
+199
echo " the gate's own assertions fire on a buried failure and on a missing summary,"
+200
echo " and stand down on a healthy one."
+201
echo
+202
+203
# ---------------------------------------------------------------------- LEG 3
+204
echo "=== LEG 3 (assertion layer): the DERIVATION itself is broken ==="
+205
restore
+206
# INDENT every `(test "` by one space. The suite still parses and still passes --
+207
# the PRODUCT is untouched -- but the derivation's anchor stops matching. The
+208
# first version of this leg commented the forms out with `;;`, which orphaned
+209
# their closing parens and broke the suite's LOAD; the gate's exit-status check
+210
# fired first and the driver correctly reported WRONG-MESSAGE rather than a red.
+211
sed -i 's|^(test "| (test "|' "$suite"
+212
zero=$(declared_now)
+213
test "$zero" -eq 0 || { echo "SETUP-FAILED: derivation still yields $zero, not 0" >&2; exit 2; }
+214
still=$(grep -cE '^ \(test "' "$suite" || true)
+215
test "$still" -eq "$base_declared" || {
+216
echo "SETUP-FAILED: indenting lost tests ($base_declared -> $still); that is a PRODUCT break," >&2
+217
echo " which would make this an assertion-layer leg in name only" >&2; exit 2; }
+218
echo "plant verified: derivation yields 0 while the suite still declares $still tests"
+219
# The indent guard fires before the floor, and names the real defect.
+220
expect_arm1_red LEG3 "INDENTED (test"
+221
echo
+222
+223
# ---------------------------------------------------------------------- LEG 4
+224
echo "=== LEG 4 (assertion layer): a PARTIAL derivation miss -- one test indented ==="
+225
restore
+226
# The case the floor cannot see. Indent exactly ONE form: the derivation drops to
+227
# N-1 while the runner still collects N, so without the completeness check the
+228
# gate reports "declares 16 tests but the runner collected 17" -- accusing a
+229
# suite that is perfectly healthy for a defect in the gate's own anchor. That is
+230
# the same lie the hardcoded literal told, one size smaller, and it is why the
+231
# completeness check runs before the count.
+232
sed -i '0,/^(test "/s|^(test "| (test "|' "$suite"
+233
one_off=$(declared_now)
+234
test "$one_off" -eq $(( base_declared - 1 )) || {
+235
echo "SETUP-FAILED: expected the derivation to drop by exactly one ($base_declared -> $one_off)" >&2
+236
exit 2; }
+237
indented=$(grep -cE '^[[:space:]]+\(test "' "$suite" || true)
+238
test "$indented" -eq 1 || {
+239
echo "SETUP-FAILED: expected exactly 1 indented form, found $indented" >&2; exit 2; }
+240
echo "plant verified: derivation $base_declared -> $one_off with 1 form indented; the suite still declares $base_declared"
+241
expect_arm1_red LEG4 "has 1 INDENTED"
+242
echo
+243
+244
# --------------------------------------------------------- POSITIVE CONTROL
+245
echo "=== POSITIVE CONTROL: arm 1 must go GREEN again on the restored suite ==="
+246
echo " (this one runs the gate to completion; arm 4 rebuilds the book)"
+247
restore
+248
ctl="$work/control.log"; ctl_rc=0
+249
"$gate" >"$ctl" 2>&1 || ctl_rc=$?
+250
if rg -q 'SD_P4_GATE_01' "$ctl"; then
+251
rg -N 'SD_P4_GATE_0' "$ctl" | sed 's/^/ /'
+252
if [ "$ctl_rc" -ne 0 ]; then
+253
echo " arm 1 GREEN; the gate exited $ctl_rc in a LATER arm, which does not affect"
+254
echo " the arm-1 verdicts above. Run the gate on its own for a full green."
+255
fi
+256
else
+257
echo "POSITIVE CONTROL FAILED: arm 1 did not pass on the restored suite (exit $ctl_rc)" >&2
+258
tail -20 "$ctl" >&2; exit 1
+259
fi
+260
echo
+261
echo "SD_P4_COUNTS_SABOTAGE_COMPLETE"