AtlatestRepositorysigil-doc

sigil-doc / tree / testgate-sd-p7.sh

1#!/usr/bin/env bash
2# SD-P7 gate: the section-child predicate as a reader/profile parameter.
3#
4# Every arm prints its own marker and this script asserts on MARKERS, never on
5# exit status alone: a non-zero exit is equally consistent with a gate that
6# never ran (127 not-found, 126 not-executable), and this estate has been fooled
7# by both. Every count an arm needs is DERIVED from the artifact rather than
8# restated here, because a check that repeats a number the artifact already
9# knows goes stale (instrument-failure taxonomy shape 10, four instances in
10# SD-P1 alone).
11set -euo pipefail
12repo_dir="$(cd "$(dirname "$0")/.." && pwd)"
13cd "$repo_dir"
14mkdir -p "$repo_dir/build"
15sigil="${SIGIL_D1_COMPILER:-$repo_dir/../task-sigil-language-readers/build/dev/bin/sigil}"
16redirects="${SIGIL_D1_REDIRECTS:-$repo_dir/dev-redirects.sgl}"
17gate_dir=$(mktemp -d "$repo_dir/build/sd-p7-gate.XXXXXX")
18export SIGIL_REDIRECTS="$redirects"
19export XDG_CACHE_HOME="$gate_dir/cache"
21pass() { printf 'SD_P7_GATE_%02d %s\n' "$1" "$2"; }
22die() { echo "SD_P7_GATE_FAILED: $*" >&2; exit 1; }
24# Every external tool this gate depends on must EXIST. An absent tool degrades
25# an assertion into a weaker one that cannot fail, silently.
26for tool in grep sed sort diff wc git find; do
27 command -v "$tool" >/dev/null || die "$tool is required by this gate"
28done
29test -x "$sigil" || die "dev compiler not found at $sigil"
30for f in src/sigil/doc/model.sgl src/sigil/doc/author.sgl \
31 src/sigil/doc/private/author-runtime.sgl \
32 test/test-section-profile.sgl \
33 test/sd-p7/folio/model.sgl test/sd-p7/folio/author.sgl test/sd-p7/folio/reader.sgl \
34 test/fixtures/sd-p7-folio-note.sgd test/fixtures/sd-p7-folio-laundering.sgd \
35 test/transcripts/sd-p7/construction-before.txt; do
36 test -f "$f" || die "missing $f"
37done
38"$sigil" --version
40# ---------------------------------------------------------------- ARM 01
41# The suite. Its two printed denominators are asserted, and the core-witness
42# count is checked against a number DERIVED FROM `block-node?`'s own definition
43# rather than against a literal -- so adding a kind to `block-node?` without
44# adding a witness reddens here instead of leaving the anti-narrowing floor
45# silently short.
46suite_log="$gate_dir/section-profile-suite.log"
47"$sigil" -L src -L test -L book test test/test-section-profile.sgl > "$suite_log" 2>&1 \
48 || { cat "$suite_log"; die "arm 01: suite exited non-zero"; }
50# The runner colours its output, so an ANSI escape can sit between the spaces
51# and the digits and make a pattern match nothing (taxonomy shape 20). Strip
52# escapes before matching anything in this log.
53plain_log="$gate_dir/section-profile-suite.plain"
54sed 's/\x1b\[[0-9;]*m//g' "$suite_log" > "$plain_log"
56# `if`, not `cmd && { ... }`: under `set -e` a failing `&&` list at statement
57# level exits the script, so a HEALTHY grep (finds nothing) would end the gate
58# silently at exit 1 and read as a failure.
59if grep -qE '[0-9]+ failed' "$plain_log"; then
60 cat "$plain_log"; die "arm 01: a test failed"
61fi
62# The runner pads its summary (`Tests: 10 passed (10 total)`), so the pattern
63# needs ` +` and not ` `. Measured against a real run before being written here:
64# a single-space pattern matches nothing and returns a confident empty.
65suite_total=$(grep -oE 'Tests: +[0-9]+ passed \([0-9]+ total\)' "$plain_log" \
66 | grep -oE '\([0-9]+ total\)' | grep -oE '[0-9]+' || true)
67test -n "$suite_total" || { cat "$plain_log"; die "arm 01: could not read the suite's own total"; }
68# POSITIVE CONTROL on the pattern above: the number of `✓`-marked ARM lines in
69# the same log must agree with it. Two readings of one artifact that disagree
70# mean the pattern is describing something other than what it claims.
72# The runner also prints `✓ All tests passed!` as a banner, which is not an arm.
73# It is subtracted by NAME rather than by assuming exactly one such line.
75# THE ANCHOR ALLOWS LEADING WHITESPACE BECAUSE THE RUNNER INDENTS. A `^✓ `
76# pattern returns a confident ZERO against this artifact: arm lines are indented
77# four spaces and the summary banner two. Written first from a rendering that
78# had stripped the indentation, and corrected against `cat -A` of the real file.
79tick_lines=$(grep -cE '^[[:space:]]*✓ ' "$plain_log" || true)
80banner_lines=$(grep -cE '^[[:space:]]*✓ All tests passed' "$plain_log" || true)
81arm_count=$((tick_lines - banner_lines))
82test "$suite_total" = "$arm_count" \
83 || die "arm 01: runner total ($suite_total) disagrees with the arm count ($arm_count)"
85# `awk '{print $2}'`, not `grep -oE '[0-9]+'`. THE MARKER'S OWN NAME CONTAINS A
86# DIGIT: `SD_P7_SECTIONS_BUILT 1` yields "7" and "1" to a bare number-extractor,
87# and `test "7\n1" -ge 1` is a syntax error rather than a wrong answer, so this
88# failed safe. A marker that names its phase is a bad thing to run a number
89# extractor over.
90sections_built=$(grep -oE 'SD_P7_SECTIONS_BUILT [0-9]+' "$plain_log" | awk '{print $2}' | tail -1 || true)
91witnesses=$(grep -oE 'SD_P7_CORE_WITNESSES [0-9]+' "$plain_log" | awk '{print $2}' | tail -1 || true)
92test -n "$sections_built" || die "arm 01: the suite did not report SD_P7_SECTIONS_BUILT"
93test -n "$witnesses" || die "arm 01: the suite did not report SD_P7_CORE_WITNESSES"
94test "$sections_built" -ge 1 || die "arm 01: no section was built through a profile"
96# DERIVE the expected witness count from `block-node?` itself, so adding a kind
97# without adding a witness reddens here instead of leaving the anti-narrowing
98# floor silently short.
100# `sed '$d'` DROPS THE LAST LINE OF THE RANGE, and it is load-bearing: sed
101# ranges are inclusive, so the range ends ON `document-node?`'s opening line,
102# which itself contains `(document? x)` and `(inline-node? x)`. Without the
103# drop this derivation returns 17 for a 13-disjunct predicate -- measured, not
104# feared; it was 17 before this line existed.
105disjuncts=$(sed -n '/(define (block-node? x)/,/(define (document-node? x)/p' src/sigil/doc/model.sgl \
106 | sed '$d' \
107 | grep -oE '\([a-z-]+\? x\)' | grep -v '(block-node? x)' \
108 | sort -u | wc -l || true)
109test -n "$disjuncts" || die "arm 01: the block-node? disjunct derivation produced nothing"
110test "$disjuncts" -gt 0 || die "arm 01: derived zero disjuncts from block-node? -- the derivation is broken, not the code"
111test "$witnesses" = "$disjuncts" \
112 || die "arm 01: $witnesses core witnesses for $disjuncts block-node? disjuncts"
113pass 1 "section-profile suite ${suite_total}/${suite_total}, ${sections_built} sections built through a profile, ${witnesses} core witnesses = ${disjuncts} block-node? disjuncts"
115# THE SUCCESSOR MUST STILL EXIST, AND SOMETHING MUST ASSERT IT.
117# The retirement form deliberately keeps a retired arm PASSING, with a message
118# naming its successor, so that a red is never hidden. **That form has an
119# unstated precondition: the successor is live.** Chain two retirements and the
120# guarantee degrades silently -- a reader follows the pointer and lands on prose
121# explaining what used to be there, while the arm goes on printing a pass.
123# A retired arm is a PASSING arm, not a no-op: it still executes, so the check
124# belongs here, immediately before the pass it guards.
126# It pins the successor's CALL SITE (`pass N `) rather than its runtime marker,
127# because the call site is the static artifact a reader greps and the marker only
128# exists once the gate has run. A file-existence check would NOT do: the
129# successor lives in a file that still exists; it is the ARM that would be gone.
130# `grep`, not `rg`: this gate's tool preflight requires grep and not ripgrep.
131successor_arm_exists() { # $1 = file, $2 = arm number
132 test -f "$1" && grep -qE "^pass $2 " "$1"
135# ---------------------------------------------------------------- ARM 02
136# RETIRED 2026-08-23 BY THE LEADER, AT SD-P8, UNDER FABLE'S RULING OF
137# 2026-08-11. This arm has NOT silently disappeared and its red has NOT been
138# hidden -- read this before concluding anything.
140# WHAT IT DID. It regenerated `test/sd-p7-construction-dump.sh` and required the
141# CONSTRUCTED document of every manuscript in the tree -- `document->string`,
142# which carries every field and every node's full source location -- to be
143# byte-identical to `test/transcripts/sd-p7/construction-before.txt`. That is a
144# strong statement, and a genuinely different one from a lowering differential:
145# SD-P7 changed CONSTRUCTION (`make-section` and the author runtime's node
146# admission) and touched lowering not at all, so a lowering differential would
147# have been byte-identical under a completely broken SD-P7.
149# WHY IT IS GONE. Ruling D8 moves `@chapter` out of `#lang sigil/doc`, and this
150# arm is pinned to its endpoint in THREE independent ways, each of which alone
151# retires it. Measured, not argued -- `test/sd-p7-construction-dump.sh` was run
152# against this tree and compared to the committed baseline:
154# 1. THE EXCLUSION SET is `git diff --diff-filter=A <base> HEAD` compared for
155# EXACT equality against the dump's excluded paths. SD-P8 adds five
156# fixtures, so this dies first, before any document is compared.
157# 2. THE RECORD COUNT: the baseline header says `population: 22 manuscripts`;
158# this tree has 32 `.sgd` files and the dump compares 27.
159# 3. THE EMBEDDED POSITIONS. The baseline is full of ABSOLUTE byte offsets --
160# `("book/practical-sigil/01-useful-first-script.sgd" 78 4 1 2143)` -- and
161# a `#lang` line growing from 16 to 21 bytes shifts every one of them by 5
162# on all 17 header-migrated manuscripts.
164# WHAT THE MEASUREMENT ACTUALLY SHOWS, which is the part that matters for
165# whether anything is being hidden. Of the 22 manuscripts common to both sides,
166# once the location NUMBERS are normalised away:
168# 19 of 22 structurally IDENTICAL
169# 3 of 22 structurally different, and all three are D8 itself:
170# sd-p3-bare-prose.sgd (kind chapter) -> (kind article)
171# sd-p3-wrapped.sgd (kind chapter) -> (kind article)
172# sd-p1-article-root.sgd RAISED ... -> a real document, because D8 is
173# exactly what makes `@article` legal in the base
174# language; this is the phase's headline effect
175# showing up in the construction dump
177# So the property this arm protects is INTACT, and the arm cannot say so because
178# its baseline is pinned to offsets. That is endpoint drift, not a defect, and
179# it is the same disposition as `test/gate-sd-p3.sh` arms 3 and 4.
181# REGENERATING THE BASELINE IS THE WRONG ANSWER, for the same reason it was
182# wrong there: `construction-before.txt` is a PRE-SD-P7 endpoint. Regenerating
183# it against today's tree destroys the transition it certifies and leaves a
184# differential comparing the present to the present.
186# SUCCESSOR: `test/gate-sd-p8.sh` arm 06, which generates BOTH sides in one run
187# (a detached worktree at SD-P8's own base, and the working tree) and asserts on
188# STRUCTURE rather than on absolute byte positions -- deliberately, because this
189# retirement is the demonstration of why a baseline pinned to offsets is a trap.
191# EVIDENCE PRESERVED: `test/transcripts/sd-p7/construction-before.txt` stays in
192# the tree unmodified, and the `test -f` preconditions still assert it is
193# present. It is SD-P7's reviewed evidence.
194successor_arm_exists test/gate-sd-p8.sh 6 \
195 || die "arm 02: this retirement names test/gate-sd-p8.sh arm 6 as its successor, and that arm no longer exists.
196 The retirement form keeps a retired arm PASSING so that a red is never hidden, which assumes the successor is LIVE.
197 With the successor gone this arm asserts nothing and says so to nobody.
198 REMEDY: find what superseded gate-sd-p8 arm 6, re-point the notice above at it, and update this check -- or restore the arm.
199 Do NOT delete this check to make the gate green; that is the failure it exists to catch."
200pass 2 "RETIRED 2026-08-23 at SD-P8 under Fable's 2026-08-11 ruling; superseded by test/gate-sd-p8.sh arm 06, which asserts on structure rather than offsets (see comment)"
202# ---------------------------------------------------------------- ARM 03
203# THE DELEGATING-LANGUAGE CONTRACT. A profile is installed by binding the
204# runtime names the parser emits into generated syntax. That set is documented
205# in docs/section-profiles.md and in test/sd-p7/folio/author.sgl; if the parser
206# grows a fourth emission site, a delegating language silently stops installing
207# its profile there. So the set is DERIVED from the parser and compared.
209# TWO SETS, because they are two different obligations and the first version of
210# this arm conflated them and overclaimed.
212# (a) AUTHOR-RUNTIME names -- what a delegating prelude must BIND to install
213# its profile.
214# (b) CONSTRUCTOR names -- what a delegating prelude must keep REACHABLE,
215# which importing `(sigil doc)` and the core syntax library does.
217# The arm used to print "the parser emits exactly N runtime names" over set (a)
218# alone, which is false of the parser: it also emits `make-srcloc`, `make-text`
219# and `make-paragraph` by bare name into manuscript scope.
220emitted="$gate_dir/emitted-names"
221grep -o "'author-[a-z-]*" src/sigil/doc/private/parser.sgl | sed "s/^'//" | sort -u > "$emitted"
222test -s "$emitted" || die "arm 03: derived no author-runtime names from the parser -- the derivation is broken"
223documented="$gate_dir/documented-names"
224printf 'author-at\nauthor-normalize\nauthor-paragraph-run\n' | sort -u > "$documented"
225diff -u "$documented" "$emitted" > "$gate_dir/names.diff" \
226 || { cat "$gate_dir/names.diff"; die "arm 03: the parser's emitted author-runtime names have moved; update docs/section-profiles.md and every delegating prelude"; }
228emitted_ctors="$gate_dir/emitted-constructors"
229grep -oE "\(list 'make-[a-z-]+" src/sigil/doc/private/parser.sgl \
230 | sed "s/^(list '//" | sort -u > "$emitted_ctors"
231test -s "$emitted_ctors" || die "arm 03: derived no constructor names from the parser"
232documented_ctors="$gate_dir/documented-constructors"
233printf 'make-paragraph\nmake-srcloc\nmake-text\n' | sort -u > "$documented_ctors"
234diff -u "$documented_ctors" "$emitted_ctors" > "$gate_dir/ctors.diff" \
235 || { cat "$gate_dir/ctors.diff"; die "arm 03: the parser's emitted constructor names have moved; a delegating prelude must keep them reachable"; }
237# POSITIVE CONTROL: the derivation must be able to SEE a new emission site. The
238# probe inserts a REAL emission form, not a comment. That distinction is the
239# point -- the previous control appended `;; (list 'author-probe-name)` and so
240# only proved the grep can see a string in a comment, which is the one thing it
241# does not need to see.
242cat src/sigil/doc/private/parser.sgl > "$gate_dir/parser-probe.sgl"
243printf '(define (probe-emission state start end)\n (generated-call state start end (list (quote author-probe-name))))\n' \
244 >> "$gate_dir/parser-probe.sgl"
245grep -q "^ (generated-call state start end (list (quote author-probe-name))))$" "$gate_dir/parser-probe.sgl" \
246 || die "arm 03: the probe emission form did not land in the probe copy"
247# ...and the derivation must find it. The probe is spelled `(quote name)` rather
248# than `'name`, which the CURRENT pattern cannot see -- so this control is also
249# the demonstration of the arm's own blind spot, asserted rather than hoped.
250probe_quoted=$(grep -o "'author-[a-z-]*" "$gate_dir/parser-probe.sgl" | sed "s/^'//" | sort -u | wc -l)
251real_count=$(wc -l < "$emitted")
252test "$probe_quoted" = "$real_count" \
253 || die "arm 03: the (quote name) spelling was unexpectedly visible to the ' pattern"
254# The same probe, spelled the way the parser actually spells it, MUST be seen.
255sed -i "s/(list (quote author-probe-name))/(list 'author-probe-name)/" "$gate_dir/parser-probe.sgl"
256probe_count=$(grep -o "'author-[a-z-]*" "$gate_dir/parser-probe.sgl" | sed "s/^'//" | sort -u | wc -l)
257test "$probe_count" = "$((real_count + 1))" \
258 || die "arm 03: the name derivation cannot see an added emission -- it is not a check"
259# THE DERIVATION'S KNOWN BLIND SPOT, PINNED RATHER THAN DISCLAIMED.
261# Both diffs above are textual, so a name the parser BUILDS with `string->symbol`
262# is invisible to them. That is not hypothetical: the parser uses it twice.
263# Fable checked both and neither can reach either obligation set --
265# :534 the COMMAND NAME, taken from the manuscript's own `@name`, so it is
266# author vocabulary and never a runtime name this contract covers;
267# :580 the hoisted binding, which is constructed with the reader-private
268# `#%sigil/doc-expression:` prefix and so cannot collide either.
270# ...but "there are exactly two and both are safe" is a fact about today's
271# parser, and the arm should notice a third rather than a reader rediscovering
272# this limit later. So: derive the count, and require each site to carry one of
273# the two constructions that were actually checked.
274symbolic_sites=$(grep -c 'string->symbol' src/sigil/doc/private/parser.sgl || true)
275test "$symbolic_sites" = "2" \
276 || die "arm 03: the parser has $symbolic_sites string->symbol sites, not the 2 that were audited; a textual name derivation cannot see them, so re-check each before trusting this arm"
277grep -q 'cons (list (string->symbol name)' src/sigil/doc/private/parser.sgl \
278 || die "arm 03: the command-name string->symbol site has moved; re-audit it"
279grep -q '"#%sigil/doc-expression:"' src/sigil/doc/private/parser.sgl \
280 || die "arm 03: the hoisted-binding string->symbol site has moved; re-audit it"
282ctor_count=$(wc -l < "$emitted_ctors")
283pass 3 "parser emits $real_count author-runtime names and $ctor_count constructor names, all documented; derivation shown able to see a new emission and BLIND to (quote x); both $symbolic_sites string->symbol sites audited and pinned"
285# ---------------------------------------------------------------- ARM 04
286# REGRESSION. The full sigil-doc suite with the FULL load path.
288# "Full" is four entries, not three, and the fourth was learned the expensive
289# way. `-L src -L test -L book` is what the phase brief names, and with only
290# those the run reports `Files: 15 of 15 run, 4 failed to load` --
291# `module-import: library not found: (sigil sxml)`. `(sigil doc render html)`
292# imports `(sigil sxml)`, and `SIGIL_REDIRECTS` does not resolve it for these
293# dev runs. `test/gate-sd-p2.sh` already knew: it adds
294# `-L "$sigil_sxml_root/src"` for exactly the two suites that need it. This arm
295# does the same for all of them, and the path is asserted to EXIST rather than
296# added hopefully, because a missing `-L` degrades into "failed to load", which
297# the runner reports without failing the process.
299# The construction dump in arm 2 deliberately does NOT get this path. Its
300# baseline was produced without it, and an environment difference between the
301# two sides of a differential is a change in the measurement rather than in the
302# thing measured. None of the baseline's five raises mentions sxml, so nothing
303# is being hidden by that choice.
304sigil_sxml_root="${SIGIL_SXML_ROOT:-$repo_dir/../sigil-sxml}"
305test -f "$sigil_sxml_root/src/sigil/sxml.sgl" \
306 || die "arm 04: no sigil-sxml checkout at $sigil_sxml_root -- set SIGIL_SXML_ROOT"
307regression_log="$gate_dir/regression.log"
308# The exit status is CAPTURED rather than acted on immediately, so the analysis
309# below runs either way and reports WHICH test failed instead of only that the
310# process ended badly. The status is still asserted, at the end.
311regression_status=0
312"$sigil" -L "$sigil_sxml_root/src" -L src -L test -L book test > "$regression_log" 2>&1 || regression_status=$?
313sed -i 's/\x1b\[[0-9;]*m//g' "$regression_log"
314grep -qE 'Tests: +[0-9]+ passed' "$regression_log" \
315 || { tail -40 "$regression_log"; die "arm 04: could not read a suite summary -- the run may not have executed anything"; }
316# The file count is its own assertion: a load-path mistake makes files silently
317# fail to load rather than fail loudly, which is how 0.1.2 shipped.
318files_line=$(grep -oE 'Files: +[0-9]+ of [0-9]+ run' "$regression_log" | tail -1 || true)
319test -n "$files_line" || { tail -40 "$regression_log"; die "arm 04: the runner reported no file count"; }
320files_run=$(echo "$files_line" | grep -oE '[0-9]+' | head -1)
321files_found=$(echo "$files_line" | grep -oE '[0-9]+' | tail -1)
322test "$files_run" = "$files_found" \
323 || die "arm 04: only $files_run of $files_found test files ran"
324if grep -qE '[0-9]+ failed' "$regression_log"; then
325 grep -nE '✗|failed' "$regression_log" | head -30
326 die "arm 04: a test failed"
327fi
328test "$regression_status" = "0" \
329 || { tail -40 "$regression_log"; die "arm 04: the runner exited $regression_status with no failing test reported"; }
330regression_summary=$(grep -E 'Tests: +[0-9]+ passed' "$regression_log" | tail -1 | tr -s ' ')
331# The load path is REPORTED, not just used: a later reader needs to know which
332# four entries produced this green.
333pass 4 "full suite green with -L $sigil_sxml_root/src -L src -L test -L book :: $regression_summary, $files_line"
335echo "SD_P7_GATE_GREEN"