AtlatestRepositorysigil-doc

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

1#!/usr/bin/env bash
2# SD-P1 gate: the root container as a reader/profile parameter (review B2).
3#
4# Every arm prints its own marker and this script asserts on MARKERS, not 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
7# fooled by both.
8set -euo pipefail
9repo_dir="$(cd "$(dirname "$0")/.." && pwd)"
10cd "$repo_dir"
11mkdir -p "$repo_dir/build"
12sigil="${SIGIL_D1_COMPILER:-$repo_dir/../task-sigil-language-readers/build/dev/bin/sigil}"
13redirects="${SIGIL_D1_REDIRECTS:-$repo_dir/dev-redirects.sgl}"
14gate_dir=$(mktemp -d "$repo_dir/build/sd-p1-gate.XXXXXX")
15export SIGIL_REDIRECTS="$redirects"
16export XDG_CACHE_HOME="$gate_dir/cache"
18pass() { printf 'SD_P1_GATE_%02d %s\n' "$1" "$2"; }
20# Every external tool this gate depends on must EXIST. An absent tool silently
21# degrades an assertion into a weaker one that cannot fail.
22for tool in rg git diff awk sed; do
23 command -v "$tool" >/dev/null || { echo "$tool is required by this gate" >&2; exit 1; }
24done
25test -x "$sigil" || { echo "dev compiler not found at $sigil" >&2; exit 1; }
26test -f src/sigil/doc/private/parser.sgl
27test -f src/sigil/doc/reader.sgl
28test -f test/test-root-profile.sgl
29test -f test/fixtures/sd-p1-folio-roots.sgd
30test -f test/fixtures/sd-p1-article-root.sgd
31test -f test/transcripts/sd-p1/classification-before.txt
32"$sigil" --version
34# ---------------------------------------------------------------- ARM 01
35# The root-profile suite: exact locations for a fixture profile with a
36# different root set, the allowlist as a class in both directions,
37# multiplicity, the default profile's byte-identical diagnostics, article/book
38# reachability, constructor validation, aggregate ownership, the slot-count
39# classification, and the delegating reader's form sequence.
40suite_log="$gate_dir/root-profile-suite.log"
41# NOT `--report`: that flag captures the suite's stdout, and this arm asserts on
42# a denominator the suite PRINTS.
43if ! setsid -w "$sigil" -L src -L test test test/test-root-profile.sgl >"$suite_log" 2>&1; then
44 echo "root-profile suite exited non-zero" >&2; tail -40 "$suite_log" >&2; exit 1
45fi
46# Strip ANSI colour before matching, so the assertion depends on the runner's
47# words rather than on its escape sequences.
48sed -e 's/\x1b\[[0-9;]*m//g' "$suite_log" >"$suite_log.clean"
49# THE COUNT IS DERIVED FROM THE SUITE FILE, NOT WRITTEN IN.
51# This line asserted a hardcoded `Tests: 11 passed (11 total)`. A TWELFTH test
52# was added to test/test-root-profile.sgl and the literal went stale, so this
53# gate has been RED ON MASTER against a suite that passes 12/12 -- the gate
54# lying about a healthy product. Confirmed 2026-08-22 by running a pristine copy
55# built from `git show HEAD:`: exit 1, "did not report 11 of 11 tests", with the
56# runner reporting `Tests: 12 passed (12 total)` and `All tests passed`.
58# That is the same defect class as an assertion that cannot fail, pointing the
59# other way: a check that has stopped describing its subject. folio's
60# test/gate-f1a.sh states the rule in its own header and derives its count
61# exactly like this -- "Every count this gate checks is DERIVED FROM THE ARTIFACT
62# it describes, never written in as a literal. [...] That drift is the single
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.
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.
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.
79expected_tests=$(grep -cE '^\(test "' test/test-root-profile.sgl || true)
80test "$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; }
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.
89reported=$(rg -o 'Tests:[[:space:]]+[0-9]+ passed' "$suite_log.clean" | head -1 | grep -oE '[0-9]+' | head -1 || true)
90collected=$(rg -o '\([0-9]+ total\)' "$suite_log.clean" | head -1 | grep -oE '[0-9]+' | head -1 || true)
91test -n "$reported" -a -n "$collected" || {
92 echo "root-profile suite printed no summary line" >&2
93 tail -40 "$suite_log.clean" >&2; exit 1; }
94# Every test the FILE declares must have been collected, and every test collected
95# must have passed. The first catches a test that silently stopped running; the
96# second catches a failure the summary would otherwise bury.
97test "$collected" = "$expected_tests" || {
98 echo "test/test-root-profile.sgl declares $expected_tests tests but the runner collected $collected" >&2
99 tail -40 "$suite_log.clean" >&2; exit 1; }
100test "$reported" = "$collected" || {
101 echo "root-profile suite reported $reported passed of $collected collected" >&2
102 tail -40 "$suite_log.clean" >&2; exit 1; }
103rg -q 'All tests passed' "$suite_log.clean" || {
104 echo "root-profile suite did not report all tests passing" >&2
105 tail -40 "$suite_log.clean" >&2; exit 1; }
106# The location denominator. Take field 2, not the first digit run: the marker's
107# own name contains digits.
108roots=$(rg -o 'SD_P1_ROOTS_CHECKED [0-9]+' "$suite_log.clean" | head -1 | awk '{print $2}' || true)
109test -n "$roots" || {
110 echo "suite printed no root-count denominator" >&2; tail -40 "$suite_log.clean" >&2; exit 1; }
111test "$roots" -ge 2 || { echo "location walk covered only $roots roots" >&2; exit 1; }
112pass 1 "root-profile suite $reported/$expected_tests, locations verified at $roots roots"
114# ---------------------------------------------------------------- ARM 02
115# The classification change is CONFINED to the two colliding spellings.
117# `test/transcripts/sd-p1/classification-before.txt` was produced by the
118# pre-SD-P1 parser running in its OWN package root. That detail is load-bearing:
119# `sigil -L <other>/src` invoked from THIS package root does NOT shadow this
120# package's `src/`, so a "before" run driven that way silently measures the
121# CURRENT parser. The BEFORE transcript therefore carries a positive control
122# proving the tree it came from could not resolve `sigil-doc-profile`, and this
123# arm refuses to compare against a transcript missing it.
124before="test/transcripts/sd-p1/classification-before.txt"
125rg -q 'PRE_SD_P1_CONTROL_UNBOUND' "$before" || {
126 echo "the BEFORE transcript carries no pre-SD-P1 positive control; it cannot be trusted" >&2
127 exit 1; }
128after_log="$gate_dir/classification-after.txt"
129setsid -w "$sigil" -L src -L test eval -f test/sd-p1-classification-probe.sgl >"$after_log" 2>&1
130# Derived, not hardcoded -- see test/sd-p1-before-transcript.sh for why.
131cases=$(rg -c '^\(report "' test/sd-p1-classification-probe.sgl || echo 0)
132test "$cases" -ge 1 || { echo "could not count the probe's cases" >&2; exit 1; }
133rg -q "SD_P1_CLASSIFICATION_PROBE_COMPLETE $cases cases" "$after_log" || {
134 echo "the AFTER probe did not run all $cases cases" >&2; cat "$after_log" >&2; exit 1; }
136case_line() { rg -o "SD_P1_CASE $1 :: .*" "$2" | head -1; }
137changed=0
138for label in A-text-command-at-top-level B-expr-command-at-top-level \
139 C-text-command-at-block-position D-expr-command-at-block-position \
140 E-text-command-inline I-text-command-inside-inline-code; do
141 b=$(case_line "$label" "$before"); a=$(case_line "$label" "$after_log")
142 test -n "$b" -a -n "$a" || { echo "missing case $label in one transcript" >&2; exit 1; }
143 if [ "$b" != "$a" ]; then changed=$((changed + 1)); fi
144done
145# At least one colliding spelling must have CHANGED, or this phase did nothing
146# to the classification and the arm is decorative.
147test "$changed" -ge 1 || {
148 echo "no colliding spelling changed behaviour; the classification fix is inert" >&2
149 diff "$before" "$after_log" >&2 || true; exit 1; }
150# ...and every POSITIVE CONTROL must be byte-identical, or the change was not
151# confined to the collision.
152for label in F-control-em-inline G-control-bare-prose-block \
153 H-control-author-expression-at-top-level; do
154 b=$(case_line "$label" "$before"); a=$(case_line "$label" "$after_log")
155 test -n "$b" -a -n "$a" || { echo "missing control $label in one transcript" >&2; exit 1; }
156 test "$b" = "$a" || {
157 echo "CONTROL $label CHANGED -- the classification fix is not confined:" >&2
158 echo " before: $b" >&2; echo " after: $a" >&2; exit 1; }
159done
160# Deliberately NOT copied into test/transcripts/: a reviewer re-executing this
161# gate must not have its worktree dirtied by doing so. The path is printed for
162# whoever is recording evidence.
163echo " classification-after: $after_log"
164pass 2 "classification change confined: $changed of 6 collision cases moved, 3 of 3 controls identical"
166# ---------------------------------------------------------------- ARM 03
167# RETIRED 2026-08-11 BY FABLE'S RULING, at SD-P3's merge, by the leader. Same
168# disposition and same reasoning as arm 5 below. This arm has NOT silently
169# disappeared and its red has NOT been hidden -- read this before concluding
170# anything.
172# What it did: asserted that 21 named out-of-lane files were byte-unchanged
173# against SD-P1's base `c41979c`, so that a green elsewhere in this gate could
174# not be explained by something outside the parser lane having moved. It
175# carried its own positive control: `src/sigil/doc/private/parser.sgl` MUST
176# differ from the base, or the whole untouched-file probe was passing for free.
177# That control was well built and is the reason the arm was trustworthy while
178# its endpoints held.
180# Why it is gone: it is a DIFFERENTIAL scoped to a transition that has
181# completed, not a standing invariant. Three of its 21 guarded files have since
182# moved for reasons the estate asked for, measured on master at `d38f30b`:
184# src/sigil/doc/model.sgl +92 -6 (SD-P2, url-link)
185# src/sigil/doc/author.sgl +11 -4 (SD-P3, prose runs)
186# src/sigil/doc/render/html.sgl +34 (SD-P2)
188# From those merges onward the arm reports RED for intended change. It was
189# already red on master itself before SD-P3 merged -- proven by the SD-P3
190# worker in a pristine worktree containing zero SD-P3 content
191# (`test/transcripts/sd-p3/merge/gate-sd-p1-arm3-red-on-master.txt`), which is
192# what established the red as SD-P2's and not SD-P3's. Attribution was settled
193# before retirement, deliberately: retiring a red you have not attributed is
194# how a real defect gets laundered.
196# THE RULING (Fable, 2026-08-11): a differential gate's meaning is bound to the
197# two endpoints it compares. Standing gates assert invariants; differential
198# gates certify transitions, and a completed transition does not become a
199# standing invariant just because the check is still executable. A sweep of all
200# 21 harnesses in this tree found the class has exactly four members and no
201# fifth.
203# WHAT IS LOST, stated plainly rather than waved off: no arm now asserts that a
204# future parser-lane change leaves the renderers and the passive suite alone.
205# That guarantee was always scoped to `c41979c` and cannot be rebased without
206# choosing new endpoints, which is a phase's job and not this file's. The
207# STANDING PATTERN for the next lowering-changing phase is the one recorded at
208# arm 5: ship your own before/after differential scoped to your own base, and
209# embed the baseline's provenance marker and intended base SHA in the arm, so a
210# superseded baseline fails with "baseline superseded -- see ruling" rather
211# than a bare diff.
213# EVIDENCE PRESERVED: the transcripts under `test/transcripts/sd-p1/` stay in
214# the tree unmodified. Regenerating them from a later tree would destroy their
215# meaning rather than update it.
216pass 3 "RETIRED by Fable's ruling 2026-08-11; differential endpoints have moved (see comment)"
218# ---------------------------------------------------------------- ARM 04
219# A delegating language needs only the PUBLIC module. This runs in a fresh
220# process importing `(sigil doc reader)` and nothing else from sigil-doc, and
221# parses a folio-shaped manuscript through a profile it constructs itself.
222delegate_log="$gate_dir/delegation.log"
223cat >"$gate_dir/delegate-probe.sgl" <<'PROBE'
224(import (sigil core) (sigil fs) (sigil doc reader))
225(define profile (make-document-profile "folio/doc" '(note project) 'one-or-more))
226(define path "test/fixtures/sd-p1-folio-roots.sgd")
227(define parts (parse-document-parts-with-profile profile (read-file-string path) path 16))
228(display "SD_P1_DELEGATION roots=") (display (length (cadr parts)))
229(display " forms=") (display (length (car parts))) (newline)
230PROBE
231setsid -w "$sigil" -L src -L "$gate_dir" eval -f "$gate_dir/delegate-probe.sgl" >"$delegate_log" 2>&1 || {
232 echo "delegation probe failed" >&2; cat "$delegate_log" >&2; exit 1; }
233rg -q 'SD_P1_DELEGATION roots=2 forms=0' "$delegate_log" || {
234 echo "delegation probe did not deliver two roots from the public module" >&2
235 cat "$delegate_log" >&2; exit 1; }
236pass 4 "a folio-shaped profile parses through (sigil doc reader) alone"
238# ---------------------------------------------------------------- ARM 05
239# RETIRED 2026-08-11 BY FABLE'S RULING. This arm has NOT silently disappeared
240# and its red has NOT been hidden -- read this before concluding anything.
242# What it did: dumped the complete lowered form sequence for 10 real
243# manuscripts and required byte-equality with `test/transcripts/sd-p1/
244# lowering-before.txt`, a PRE-SD-P1 baseline. That certified a TRANSITION --
245# `c41979c` -> `acb9adf`, "SD-P1 changed nothing for default-profile
246# manuscripts" -- and the certification is complete: executed by the SD-P1
247# worker, then reproduced end to end by the reviewer with independently
248# regenerated artifacts on both sides (570,669 bytes compared).
250# Why it is gone: SD-P3 (`feat/inline-commands-bare-prose`) INTENDS to change
251# the lowering of 2 of those 10 manuscripts, so from that commit onward this
252# arm reports RED for a change the estate asked for. Measured on that branch:
253# 8 of 10 identical, differing in exactly `minimal.sgd` and
254# `semantic-structures.sgd` -- the same two SD-P3's independent passive census
255# flags, so two unrelated instruments agree on the population.
257# THE RULING (Fable, 2026-08-11, recorded in the SD-P1 row of
258# topics/sigil-doc-implementation-plan): a differential gate's meaning is bound
259# to the two endpoints it compares. Standing gates assert invariants;
260# differential gates certify transitions, and a completed transition does not
261# become a standing invariant just because the check is still executable.
262# Retiring it here is legitimate BECAUSE it is ruled and recorded -- laundering
263# is hiding a red, not retiring a differential whose endpoints have moved.
265# SUCCESSOR, and it is strictly stronger: `test/gate-sd-p3.sh` arm 3 compares
266# 79 manuscripts (a DERIVED population, not an inherited list) against a
267# pre-SD-P3 baseline carrying three provenance controls, and cross-checks the
268# set of changed manuscripts against a second, independent instrument.
270# EVIDENCE PRESERVED: `test/transcripts/sd-p1/lowering-before.txt` and
271# `lowering-after.txt` stay in the tree unmodified. They are SD-P1's reviewed
272# evidence and a regenerating them from a later tree would destroy their
273# meaning rather than update it.
275# STANDING PATTERN for the next lowering-changing phase: ship your own
276# before/after differential scoped to your own base, and embed the baseline's
277# provenance marker and intended base SHA in the arm, so a superseded baseline
278# fails with "baseline superseded -- see ruling" rather than a bare diff.
279pass 5 "RETIRED by Fable's ruling 2026-08-11; superseded by gate-sd-p3.sh arm 3"
281# ---------------------------------------------------------------- ARM 06
282# Existing suites, unchanged in what they assert. gate-sd-p4.sh runs the passive
283# suite, the no-execution witness, the parser-state arms, and D0/D1/D2/D3.
285# `SD_P1_SKIP_REGRESSION` exists for ONE caller: `test/sabotage-sd-p1.sh`, whose
286# leg 7 must re-run arm 2 and would otherwise pay the full ~30 minute
287# regression to do it. A skipped run CANNOT be mistaken for a full one -- it
288# prints a different final marker, and every consumer of this gate asserts on
289# `SD_P1_GATE_GREEN`.
290if [ "${SD_P1_SKIP_REGRESSION:-0}" = "1" ]; then
291 echo "SD_P1_GATE_PARTIAL regression arm skipped by request"
292 exit 0
293fi
294test/gate-sd-p4.sh
295pass 6 "SD-P4 + D0/D1/D2/D3 regression green"
297echo "SD_P1_GATE_GREEN"