AtlatestRepositorysigil-doc

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

1#!/usr/bin/env bash
2# SD-P8 gate: ruling D8 -- generic base language, genres as languages.
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 fooled
7# by both.
8#
9# Counts checked for EQUALITY are DERIVED from an artifact rather than written
10# here. The exceptions are stated where they occur and are FLOORS, not
11# restatements of a value.
13# ARM 5 IS A DIFFERENTIAL AND IS SCOPED TO ITS OWN BASE. It generates BOTH sides
14# in one run -- a detached worktree at $base_sha, and this working tree -- so it
15# has no committed baseline to go stale, and it fails with "baseline superseded"
16# rather than a bare diff if its base is no longer an ancestor of HEAD. It is
17# the replacement `test/gate-sd-p3.sh` arms 3 and 4 name in their retirement
18# notices, built to the standing pattern Fable's 2026-08-11 ruling recorded for
19# the next lowering-changing phase.
22# A COINCIDENCE THIS PHASE LEANED ON, WRITTEN DOWN BECAUSE IT IS NOT DESIGN.
23# `@article` and `@chapter` are both 8 characters, and `article`/`chapter` both
24# 7. That is luck. It is what let the migration switch roots in
25# `test/test-reader.sgl`, `test/test-prose-runs.sgl` and
26# `test/sd-p1-classification-probe.sgl` with every location assertion, and the
27# three pinned positive controls in `test/gate-sd-p1.sh` arm 2, staying BYTE
28# IDENTICAL. Those controls are therefore LENGTH-SENSITIVE and nothing in them
29# says so: the next root rename, if it is not 8 characters, will redden that arm
30# for a reason that has nothing to do with what it checks. If you are here
31# because of that, this is your explanation.
32# THE ARMS ARE ORDERED CHEAPEST FIRST, deliberately. Arms 5 and 6 dominate the
33# runtime (a detached worktree plus two full lowering dumps, and the whole test
34# suite), and `set -e` stops at the first failure, so a sabotage leg aimed at an
35# early arm pays for none of that. That is not a convenience: a sabotage suite
36# nobody can afford to run is a sabotage suite nobody runs, and a gate that has
37# never been shown able to fail is a gate that has been shown to be quiet.
38set -euo pipefail
39repo_dir="$(cd "$(dirname "$0")/.." && pwd)"
40cd "$repo_dir"
41mkdir -p "$repo_dir/build"
43# PROVENANCE. This differential's meaning is bound to this commit: it is the
44# tree D8 was executed against. If HEAD ever stops descending from it, the arm
45# says so in those words instead of printing a diff nobody can interpret.
46# THE BASE IS PINNED AND IS DELIBERATELY NOT `master`. `0b7c5ca` is the tree D8
47# was executed against; master moved past it on 2026-08-23 while this branch was
48# in flight. This differential certifies the D8 TRANSITION, not "everything since
49# last Tuesday", and re-pointing it at a moving master would reintroduce exactly
50# the endpoint drift that retired `gate-sd-p3.sh` arms 3-4 and `gate-sd-p7.sh`
51# arm 2. The is-ancestor check below is what keeps it honest: it still holds
52# after a rebase onto a later master, and refuses with "baseline superseded" if
53# it ever stops.
54base_sha="${SD_P8_BASE_SHA:-0b7c5ca}"
56sigil="${SIGIL_D1_COMPILER:-$repo_dir/../task-sigil-language-readers/build/dev/bin/sigil}"
57redirects="${SIGIL_D1_REDIRECTS:-$repo_dir/dev-redirects.sgl}"
58gate_dir=$(mktemp -d "$repo_dir/build/sd-p8-gate.XXXXXX")
59export SIGIL_REDIRECTS="$redirects"
60export XDG_CACHE_HOME="$gate_dir/cache"
62# ZERO-PADDED OUTPUT, BASE-10 INPUT. `%02d` on a zero-padded argument is a trap:
63# bash reads `08` as octal, raises "invalid octal number", prints 00 and returns
64# NON-ZERO -- which under `set -e` killed the sabotage suite immediately after
65# leg 8 printed a mislabelled marker, so leg 9 (the positive control) never ran.
66# `10#` forces base 10, so the markers stay padded and a padded caller is safe.
67pass() { printf 'SD_P8_GATE_%02d %s\n' "$((10#$1))" "$2"; }
68die() { echo "$*" >&2; exit 1; }
70# An absent tool does not announce itself -- it impersonates a result. Every
71# external tool this gate leans on must EXIST before any of them is trusted.
72for tool in rg git awk sed sha256sum comm sort diff cmp setsid; do
73 command -v "$tool" >/dev/null || die "$tool is required by this gate"
74done
75test -x "$sigil" || die "dev compiler not found at $sigil"
76test -f src/sigil/doc/private/parser.sgl
77test -f src/sigil/doc/book/reader.sgl || die "the D8 genre language is missing"
78test -f test/test-root-profile.sgl
79test -f test/sd-p8-lowering-dump.sgl
80test -f test/sd-p8-language-probe.sgl
81# The gate's INPUT SET, recorded rather than assumed: a caret-ranged dependency
82# is an unrecorded input, and so is an unnamed compiler.
83"$sigil" --version
84echo " compiler: $sigil"
85echo " redirects: $redirects"
86echo " base_sha: $base_sha"
87# THE TREE STATE, NOT JUST THE COMMIT. This gate measures the WORKING TREE, and
88# printing only `head:` would name a commit it is not testing -- a differential
89# whose transcript is one `git stash` away from describing a different tree than
90# it measured. `test/sd-p7-construction-dump.sh` already does this properly and
91# it is where the shape is borrowed from. Uncommitted paths are LISTED, not
92# counted, because "3 modified paths" tells a later reader nothing about whether
93# the modification was in the product or in the gate.
94echo " head: $(git rev-parse HEAD)"
95echo " tree: $(git status --porcelain | wc -l) uncommitted path(s)"
96git status --porcelain | sed 's/^/ tree-dirty: /'
98sxml_root="${SIGIL_SXML_ROOT:-$repo_dir/../sigil-sxml}"
99test -d "$sxml_root/src" || die "sigil-sxml source not found at $sxml_root/src"
101# ---------------------------------------------------------------- ARM 01
102# The two languages are what D8 ruled, asked through the PUBLIC modules only.
104# A delegating language must never need a private module, and the genre this
105# phase ships is the first proof of that inside this package rather than in a
106# test-only stand-in.
107probe_log="$gate_dir/profiles.log"
108cat >"$gate_dir/profile-probe.sgl" <<'PROBE'
109(import (sigil core) (sigil doc reader) (sigil doc book reader))
110(define (show label profile)
111 (display label)
112 (display " name=") (write (document-profile-name profile))
113 (display " roots=") (write (document-profile-roots profile))
114 (display " mult=") (write (document-profile-multiplicity profile))
115 (newline))
116(show "SD_P8_PROFILE base" sigil-doc-profile)
117(show "SD_P8_PROFILE book" sigil-doc-book-profile)
118PROBE
119setsid -w "$sigil" -L src -L "$gate_dir" eval -f "$gate_dir/profile-probe.sgl" \
120 >"$probe_log" 2>&1 || { cat "$probe_log" >&2; die "arm 01: the profile probe did not run"; }
121rg -q 'SD_P8_PROFILE base name="sigil/doc" roots=\(article\) mult=one' "$probe_log" \
122 || { cat "$probe_log" >&2; die "arm 01: the base profile is not D8's"; }
123rg -q 'SD_P8_PROFILE book name="sigil/doc/book" roots=\(chapter\) mult=one' "$probe_log" \
124 || { cat "$probe_log" >&2; die "arm 01: the genre profile is not D8's"; }
125pass 1 "both shipped profiles are D8's, reached through public modules alone"
127# ---------------------------------------------------------------- ARM 02
128# The dropped kind, checked as a CLASS rather than as a spelling.
130# A refusal by name, a positive control that the two surviving kinds still
131# construct, and -- because a ruling that creates a rule must be checked against
132# the POPULATION and not only against the diff -- a sweep of every
133# `make-document` call site in the tree.
134kind_log="$gate_dir/kind.log"
135cat >"$gate_dir/kind-probe.sgl" <<'PROBE'
136(import (sigil core) (sigil syntax) (sigil doc))
137(define loc (make-srcloc "k.sgd" 1 1 0 1))
138(define (body) (list (make-paragraph (list (make-text "b" loc)) loc)))
139;; The VALUE on success, not a marker: `(guard ... (thunk) "NO-RAISE")` discards
140;; what the thunk returned and reports success identically for every kind, which
141;; would make the two positive controls below unable to tell article from page.
142(define (outcome thunk) (guard (exn (else (exception-message exn))) (thunk)))
143(define (try kind)
144 (display "SD_P8_KIND ") (display kind) (display " :: ")
145 (write (outcome (lambda () (symbol->string (document-kind (make-document kind "T" (list "A") (body) loc))))))
146 (newline))
147(try 'article)
148(try 'chapter)
149(try 'book)
150(try 'page)
151PROBE
152setsid -w "$sigil" -L src -L "$gate_dir" eval -f "$gate_dir/kind-probe.sgl" \
153 >"$kind_log" 2>&1 || { cat "$kind_log" >&2; die "arm 02: the kind probe did not run"; }
154rg -q '^SD_P8_KIND book :: "make-document: kind must be article or chapter"$' "$kind_log" \
155 || { cat "$kind_log" >&2; die "arm 02: make-document did not refuse the dropped kind by name"; }
156rg -q '^SD_P8_KIND page :: "make-document: kind must be article or chapter"$' "$kind_log" \
157 || { cat "$kind_log" >&2; die "arm 02: make-document did not refuse an unrelated kind, so the enum is not closed"; }
158# POSITIVE CONTROLS: a constructor that refused everything satisfies both above.
159rg -q '^SD_P8_KIND article :: "article"$' "$kind_log" \
160 || { cat "$kind_log" >&2; die "arm 02: article is no longer constructible -- folio's publication lowering depends on it"; }
161rg -q '^SD_P8_KIND chapter :: "chapter"$' "$kind_log" \
162 || { cat "$kind_log" >&2; die "arm 02: chapter is no longer constructible"; }
163# THE POPULATION, NOT THE DIFF -- and it needs an exception class, which the
164# first version of this sweep did not have.
166# WHAT WENT WRONG, because it is the interesting part. The sweep was
167# "every `(make-document` site in the tree; any naming `'book` is a finding",
168# and it fired on `test/test-root-profile.sgl`, where two sites construct
169# `'book` DELIBERATELY inside `assert-error` to prove the kind is refused. The
170# sweep could not tell a violation from the test that guards against
171# violations, so it was red on a healthy tree. It was never seen green: the
172# sabotage suite reached this arm before any full gate run did, on a leg aimed
173# at something else entirely.
175# The repair is not to exclude `test/`, which would gut the sweep. It is to
176# CLASSIFY every site and print one row each, so the output length is the
177# denominator and an unclassified site fails loudly:
179# * in `src/`, `tools/` or `book/` -- shipped or product code -- a `'book`
180# site is a violation, full stop;
181# * in `test/` it is legitimate ONLY as a refusal test, which means the site
182# sits inside an `assert-error` or a captured-message form;
183# * anything else is UNCLASSIFIED and fails, because "I could not tell" and
184# "it is fine" must not print the same way.
186# And at least one NEGATIVE-TEST site is REQUIRED. Without one, nothing in the
187# tree exercises the refusal at a call site and this arm would be asserting an
188# absence with no positive control -- which passes for free.
189sites="$gate_dir/make-document-sites.txt"
190{ rg -n --no-heading "\(make-document " src test tools book || true; } >"$sites"
191site_count=$(wc -l <"$sites")
192# A FLOOR, labelled: this is not a restatement of the current count, it is the
193# level below which the sweep has plainly lost its population.
194test "$site_count" -ge 20 \
195 || die "arm 02: the sweep found only $site_count make-document sites; it is not examining the tree"
197book_ledger="$gate_dir/book-kind-sites.txt"
198: >"$book_ledger"
199violations=0; negative_tests=0; unclassified=0
200while IFS= read -r hit; do
201 test -n "$hit" || continue
202 path="${hit%%:*}"
203 rest="${hit#*:}"
204 lineno="${rest%%:*}"
205 case "$path" in
206 src/*|tools/*|book/*)
207 violations=$((violations + 1))
208 printf 'VIOLATION %s:%s (product code must not construct the dropped kind)\n' \
209 "$path" "$lineno" >>"$book_ledger" ;;
210 test/*)
211 # The site's own line plus one either side: a refusal test wraps the
212 # construction in a form that CAPTURES the raise.
213 window=$(sed -n "$((lineno > 1 ? lineno - 1 : 1)),$((lineno + 1))p" "$path")
214 if printf '%s' "$window" | rg -q 'assert-error|message \(lambda'; then
215 negative_tests=$((negative_tests + 1))
216 printf 'NEGATIVE-TEST %s:%s (constructs it to prove it is refused)\n' \
217 "$path" "$lineno" >>"$book_ledger"
218 else
219 unclassified=$((unclassified + 1))
220 printf 'UNCLASSIFIED %s:%s (names the dropped kind and is not guarded by a refusal assertion)\n' \
221 "$path" "$lineno" >>"$book_ledger"
222 fi ;;
223 *)
224 unclassified=$((unclassified + 1))
225 printf 'UNCLASSIFIED %s:%s (outside every classified directory)\n' \
226 "$path" "$lineno" >>"$book_ledger" ;;
227 esac
228done < <( { rg -n --no-heading "\(make-document '?book" src test tools book || true; } )
229cat "$book_ledger"
230book_rows=$(wc -l <"$book_ledger")
231book_hits=$( { rg -c "\(make-document '?book" "$sites" || true; } )
232test "$book_rows" -eq "${book_hits:-0}" \
233 || die "arm 02: found ${book_hits:-0} sites naming the dropped kind but classified $book_rows of them"
234test "$violations" -eq 0 \
235 || { rg '^VIOLATION' "$book_ledger" >&2; die "arm 02: $violations product call sites still construct the dropped kind"; }
236test "$unclassified" -eq 0 \
237 || { rg '^UNCLASSIFIED' "$book_ledger" >&2; die "arm 02: $unclassified sites name the dropped kind and could not be classified"; }
238test "$negative_tests" -ge 1 \
239 || die "arm 02: no test constructs the dropped kind to prove it is refused, so this sweep's absence claim has no positive control"
240pass 2 "kind book refused by name with 2 positive controls; $site_count make-document sites swept, ${book_hits:-0} name the dropped kind and all $negative_tests are refusal tests, 0 in product code"
242# ---------------------------------------------------------------- ARM 03
243# (ARM 05 below carries the differential; arms 2 and 3 are the cheap ones and
244# run first -- see the ordering note in this file's header.)
245# THE CORPUS CENSUS, with its denominator and an explicit not-checked class.
247# Every `.sgd` in the tree is classified by (declared language, root command) and
248# every one must land in a combination its own language admits. "No bad
249# manuscripts found" and "every manuscript was examined" are different claims;
250# this arm prints one row per manuscript so the output length is the second one.
251census="$gate_dir/census.txt"
252: >"$census"
253total=0; base_lang=0; book_lang=0; other_lang=0; negative=0; bad=0; unknown_lang=0
254while IFS= read -r m; do
255 total=$((total + 1))
256 lang=$(head -1 "$m" | sed -n 's|^#lang \(.*\)$|\1|p')
257 root=$( { rg -o -m1 '^@[a-z][a-z-]*' "$m" || echo "@NONE"; } | head -1)
258 # THREE CLASSES, AND `UNCHECKED` HAS TO BE REACHABLE OR THE PASS LINE LIES.
259 #
260 # The first version defaulted `verdict=UNCHECKED` and then overwrote it on
261 # EVERY branch -- the inner catch-all made everything either OK or ILLEGAL --
262 # so "0 unclassified" was vacuously true. A guard against a state the code
263 # makes unreachable is not a denominator, it is decoration.
264 #
265 # The class that genuinely exists is: a language THIS GATE DOES NOT KNOW.
266 # That is different from a manuscript being wrong, and conflating the two is
267 # the actual risk here -- a `#lang folio/doc` fixture added later would be
268 # reported ILLEGAL, which is false, or would need this arm silently widened.
269 # So an unknown language is UNCHECKED and FAILS, which forces whoever adds a
270 # language to say what its legal roots are.
271 verdict=UNCHECKED
272 case "$m" in
273 */sd-p8-base-*|*/sd-p8-book-article-root.sgd|*/sd-p8-book-two-chapters.sgd)
274 verdict=NEGATIVE-FIXTURE; negative=$((negative + 1)) ;;
275 *)
276 case "$lang" in
277 "sigil/doc"|"sigil/doc/book"|"sd-p7/folio")
278 case "$lang::$root" in
279 "sigil/doc::@article") verdict=OK; base_lang=$((base_lang + 1)) ;;
280 "sigil/doc/book::@chapter") verdict=OK; book_lang=$((book_lang + 1)) ;;
281 "sigil/doc::@note") verdict=OK-EXPLICIT-PROFILE; other_lang=$((other_lang + 1)) ;;
282 "sd-p7/folio::@note") verdict=OK-OTHER-LANGUAGE; other_lang=$((other_lang + 1)) ;;
283 *) verdict=ILLEGAL; bad=$((bad + 1)) ;;
284 esac ;;
285 *) verdict=UNCHECKED; unknown_lang=$((unknown_lang + 1)) ;;
286 esac ;;
287 esac
288 printf '%-22s %-16s %-9s %s\n' "$verdict" "$lang" "$root" "$m" >>"$census"
289done < <(find book test -name '*.sgd' | sort)
290cat "$census"
291rows=$(wc -l <"$census")
292test "$rows" -eq "$total" || die "arm 03: examined $total manuscripts but printed $rows rows"
293test "$total" -ge 27 || die "arm 03: the census found only $total manuscripts; the population is wrong"
294test "$bad" -eq 0 || { rg '^ILLEGAL' "$census" >&2; die "arm 03: $bad manuscripts declare a language that does not admit their root"; }
295# The `unknown language` class, counted from the rows and cross-checked against
296# the loop's own counter -- two independently produced numbers, because a loop
297# cannot audit itself.
298unchecked=$( { rg -c '^UNCHECKED' "$census" || true; } )
299test "${unchecked:-0}" -eq "$unknown_lang" \
300 || die "arm 03: $unknown_lang unknown-language manuscripts counted but ${unchecked:-0} rows printed"
301test "${unchecked:-0}" -eq 0 \
302 || { rg '^UNCHECKED' "$census" >&2
303 die "arm 03: ${unchecked} manuscripts declare a language this arm does not know, so it cannot say whether their root is legal; widen the arm deliberately or fix the manuscript"; }
304test "$base_lang" -ge 1 || die "arm 03: no manuscript exercises the base language"
305test "$book_lang" -ge 15 || die "arm 03: only $book_lang manuscripts moved to the genre language"
306pass 3 "census $total manuscripts, 0 illegal, 0 of an unknown language :: $base_lang base, $book_lang book, $other_lang other-language, $negative negative fixtures"
308# ---------------------------------------------------------------- ARM 04
309# END TO END through the real `#lang` path: the two REQUIRED GREENS and the four
310# REQUIRED REDS, each named, plus the reader-level diagnostic every refusal
311# carries. See the probe's header for why the diagnostic has to be re-derived at
312# the reader (the compiler's `#lang` loader drops it -- measured, pre-existing).
313lang_log="$gate_dir/language.log"
314setsid -w "$sigil" -L src -L test eval -f test/sd-p8-language-probe.sgl \
315 >"$lang_log" 2>&1 || { cat "$lang_log" >&2; die "arm 04: the language probe did not run"; }
316# THE DENOMINATOR, derived from the probe's source and cross-checked against the
317# lines it printed. A probe whose cases were deleted still satisfies every
318# individual assertion below; only the count can see that.
319declared=$(rg -c '^ \(list "' test/sd-p8-language-probe.sgl)
320printed=$(rg -c '^SD_P8_LANG ' "$lang_log" || echo 0)
321test "$declared" -ge 6 || die "arm 04: the probe declares only $declared cases"
322test "$printed" -eq "$declared" \
323 || die "arm 04: the probe declares $declared cases but printed $printed"
324rg -q "SD_P8_LANGUAGE_PROBE_COMPLETE cases $declared matched $declared mismatched 0" "$lang_log" \
325 || { rg -n 'MISMATCH|SD_P8_LANGUAGE_PROBE_COMPLETE' "$lang_log" >&2
326 die "arm 04: not every case matched its expected verdict"; }
327# Each REQUIRED RED by name, with the diagnostic that must accompany it. The
328# language NAME in each message is the load-bearing part: a genre language
329# reporting `sigil/doc:` would send an author to the wrong documentation.
330rg -q '^SD_P8_DIAG base-refuses-chapter :: "sigil/doc: top-level author forms must precede exactly one @article"$' "$lang_log" \
331 || die "arm 04: the base language did not refuse @chapter with its own diagnostic"
332rg -q '^SD_P8_DIAG base-refuses-two :: "sigil/doc: top-level author forms must precede exactly one @article"$' "$lang_log" \
333 || die "arm 04: the base language did not refuse a second @article"
334rg -q '^SD_P8_DIAG book-refuses-article :: "sigil/doc/book: top-level author forms must precede exactly one @chapter"$' "$lang_log" \
335 || die "arm 04: the genre language did not refuse @article naming sigil/doc/book"
336rg -q '^SD_P8_DIAG book-refuses-two :: "sigil/doc/book: top-level author forms must precede exactly one @chapter"$' "$lang_log" \
337 || die "arm 04: the genre language did not refuse a second @chapter naming sigil/doc/book"
338# THE POSITIVE CONTROLS. Four refusals are satisfied by a reader that refuses
339# everything; these two are not.
340rg -q '^SD_P8_LANG base-accepts-article want=accept got=accept MATCH kind-or-error="article"$' "$lang_log" \
341 || die "arm 04: #lang sigil/doc did not accept its own root"
342rg -q '^SD_P8_LANG book-accepts-chapter want=accept got=accept MATCH kind-or-error="chapter"$' "$lang_log" \
343 || die "arm 04: #lang sigil/doc/book did not accept its own root"
344rg -q '^SD_P8_DIAG base-accepts-article :: "NO-RAISE"$' "$lang_log" \
345 || die "arm 04: the base reader raised on the manuscript it must accept"
346rg -q '^SD_P8_DIAG book-accepts-chapter :: "NO-RAISE"$' "$lang_log" \
347 || die "arm 04: the genre reader raised on the manuscript it must accept"
348# THE `#lang`-LAYER MESSAGE, and why this is a two-branch assertion rather than
349# one.
351# Until sigil 0.19.1 the compiler's language loader reported a reader's refusal
352# as `read-language-source failed for #lang <x>:` with NOTHING after the colon:
353# the error path read a buffer only C-raised errors populate, and readers are
354# written in Sigil. sigil commit 9a523988 ("fix(compiler): carry the reader's
355# own diagnostic into the #lang error") fixed it in v0.19.1. Measured 2026-08-22
356# against ONE tree with only the compiler varied:
358# 0.19.0 (b99b8f2d): ... for #lang sigil/doc:
359# 0.19.1 (3d0668a0): ... for #lang sigil/doc: error: sigil/doc: top-level
360# author forms must precede exactly one @chapter
362# This gate runs on the estate's PINNED dev compiler, which every other gate in
363# this repo shares, and moving it here alone would produce a measurement nobody
364# can compare against the others. So the expectation is chosen from the version
365# this gate already records -- and BOTH branches assert. A single "if it carries
366# text, the text must be right" would be vacuous on the older compiler, which is
367# precisely the shape that lets a gate pass for free.
369# An unparseable version FAILS rather than falling through to the older branch:
370# taking the weaker expectation because a `sed` did not match is the same
371# vacuous green wearing a different hat.
372checked_layer=0
373version_line=$("$sigil" --version 2>&1 | sed 's/\x1b\[[0-9;]*m//g')
374compiler_version=$(printf '%s' "$version_line" | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1)
375test -n "$compiler_version" \
376 || die "arm 04: could not parse a version out of '$version_line'; the #lang-layer expectation cannot be chosen, and guessing it would make this arm vacuous"
377as_number() { echo "$1" | awk -F. '{printf "%d%03d%03d", $1, $2, $3}'; }
378carries_floor=0.19.1
379if [ "$(as_number "$compiler_version")" -ge "$(as_number "$carries_floor")" ]; then
380 lang_layer="carries"
381else
382 lang_layer="empty"
383fi
384echo " #lang layer: compiler $compiler_version -> expecting the message to be '$lang_layer' (floor $carries_floor)"
385# One row per refusal case: language, and the diagnostic that language must
386# produce. The loop's length is the denominator -- a case dropped from it stops
387# being checked silently otherwise.
388while read -r case_name diag; do
389 test -n "$case_name" || continue
390 if [ "$lang_layer" = "carries" ]; then
391 rg -q "^SD_P8_LANG $case_name .* kind-or-error=\".*: error: $diag\"\$" "$lang_log" \
392 || { rg -n "^SD_P8_LANG $case_name " "$lang_log" >&2
393 die "arm 04: on sigil $compiler_version the #lang error must carry '$diag', and for $case_name it does not"; }
394 else
395 rg -q "^SD_P8_LANG $case_name .* kind-or-error=\"read-language-source failed for #lang [a-z/]+: \"\$" "$lang_log" \
396 || { rg -n "^SD_P8_LANG $case_name " "$lang_log" >&2
397 die "arm 04: sigil $compiler_version predates $carries_floor, so the #lang error must be empty after the colon; for $case_name it is not -- re-read this block before changing anything, the expectation may simply be stale"; }
398 fi
399 checked_layer=$((checked_layer + 1))
400done <<'ROWS'
401base-refuses-chapter sigil/doc: top-level author forms must precede exactly one @article
402base-refuses-two sigil/doc: top-level author forms must precede exactly one @article
403book-refuses-article sigil/doc/book: top-level author forms must precede exactly one @chapter
404book-refuses-two sigil/doc/book: top-level author forms must precede exactly one @chapter
405ROWS
406test "${checked_layer:-0}" -eq 4 \
407 || die "arm 04: the #lang-layer loop checked ${checked_layer:-0} cases, not 4"
408pass 4 "$printed of $declared #lang cases matched: 4 required reds named by language and root, 2 required greens; #lang layer '$lang_layer' on sigil $compiler_version, $checked_layer of 4 checked"
410# ---------------------------------------------------------------- ARM 05
411# THE D8 DIFFERENTIAL. Both sides generated in one run.
413# The claim: across the whole manuscript corpus, the ONLY manuscripts whose
414# lowering changed are the ones whose ROOT COMMAND this phase changed, and for
415# each of those the change is EXACTLY the root symbol. The 17 manuscripts
416# that took a header edit alone lower identically.
418# What makes that assertable is `test/sd-p8-lowering-dump.sgl`, which emits every
419# manuscript position relative to that manuscript's own header end. Read its
420# header: the header delta reaches the lowered output in THREE places, and the
421# third (the generated binding name `#%sigil/doc-expression:<offset>`) is one
422# nobody would predict from reading the parser.
423git rev-parse --verify "$base_sha^{commit}" >/dev/null 2>&1 \
424 || die "arm 05: baseline superseded -- base $base_sha is not a commit in this repository; see the retirement notice in test/gate-sd-p3.sh"
425git merge-base --is-ancestor "$base_sha" HEAD \
426 || die "arm 05: baseline superseded -- HEAD no longer descends from $base_sha, so this differential compares nothing this branch is a delta from; re-scope it to the new base and record the ruling, as gate-sd-p1.sh arms 3 and 5 did"
428base_tree="$gate_dir/base-tree"
429cleanup() { git -C "$repo_dir" worktree remove --force "$base_tree" >/dev/null 2>&1 || true; }
430# Unconditional, not merely reached: an early exit between `worktree add` and the
431# end of this script would otherwise leave a stray worktree in a SHARED .git.
432trap cleanup EXIT INT TERM
433git worktree add --detach "$base_tree" "$base_sha" >/dev/null 2>&1 \
434 || die "arm 05: could not create a detached worktree at $base_sha"
436# THE SAME FILE ON BOTH SIDES. An instrument that differs between the two sides
437# of a differential measures itself. The base tree predates this script, so it is
438# copied in -- and its hash is asserted equal afterwards, because "I copied it"
439# and "the copy is the same file" are different claims.
440cp test/sd-p8-lowering-dump.sgl "$base_tree/test/"
441here_hash=$(sha256sum test/sd-p8-lowering-dump.sgl | awk '{print $1}')
442there_hash=$(sha256sum "$base_tree/test/sd-p8-lowering-dump.sgl" | awk '{print $1}')
443test "$here_hash" = "$there_hash" || die "arm 05: the two sides are not running the same dump script"
445# `dev-redirects.sgl` resolves `../sigil` relative to the package root and the
446# base worktree is not a sibling of that checkout, so an absolute one is written.
447sigil_repo="$(cd "$repo_dir/../sigil" && pwd)"
448sxml_repo="$(cd "$sxml_root" && pwd)"
449cat >"$gate_dir/base-redirects.sgl" <<EOF
450(redirects
451 repos: (list
452 (for-repo url: "codeberg:sigil/sigil"
453 use: (from-path dir: "$sigil_repo"))
454 (for-repo url: "codeberg:sigil/sigil-sxml"
455 use: (from-path dir: "$sxml_repo"))))
456EOF
458after_dump="$gate_dir/after.txt"
459before_dump="$gate_dir/before.txt"
460setsid -w "$sigil" -L src -L test eval -f test/sd-p8-lowering-dump.sgl >"$after_dump" 2>&1 || true
461( cd "$base_tree" && SIGIL_REDIRECTS="$gate_dir/base-redirects.sgl" \
462 XDG_CACHE_HOME="$gate_dir/base-cache" \
463 setsid -w "$sigil" -L src -L test eval -f test/sd-p8-lowering-dump.sgl ) >"$before_dump" 2>&1 || true
464for side in after before; do
465 f="$gate_dir/$side.txt"
466 rg -q '^SD_P8_LOWERING_DUMP_COMPLETE ' "$f" \
467 || { tail -25 "$f" >&2; die "arm 05: the $side dump printed no completion marker"; }
468done
470# One directory of sections per side. The completion marker is EXCLUDED: it names
471# the corpus size, so leaving it in would tie the LAST manuscript's section to how
472# many manuscripts were dumped -- a difference with nothing to do with lowering.
473# The MANIFEST/SKIPPED lines are excluded too: they carry the language id, the
474# header length and the root command, which is exactly what legitimately differs.
475split() { # $1 = dump, $2 = out dir
476 mkdir -p "$2"
477 awk -v o="$2" '/^SD_P8_/{next} /^--- /{name="";next} /^=== /{name=$2} name!=""{print > (o "/" name)}' "$1"
479split "$after_dump" "$gate_dir/sec-after"
480split "$before_dump" "$gate_dir/sec-before"
482# The SECOND, INDEPENDENT INSTRUMENT. The root command of each manuscript is read
483# as TEXT by the dump's own scanner and printed on the MANIFEST line, so the set
484# of manuscripts that MUST differ is derived without asking the lowering -- the
485# thing under test -- what it thinks a root was.
486manifest() { # $1 = dump -> "name root"
487 rg -o '^--- MANIFEST \S+ lang=\S+ header=\d+ root=\S+' "$1" \
488 | awk '{r=$NF; sub(/^root=/,"",r); print $3, r}' | sort
490manifest "$after_dump" >"$gate_dir/roots-after"
491manifest "$before_dump" >"$gate_dir/roots-before"
493ledger="$gate_dir/ledger.txt"
494: >"$ledger"
495identical=0; expected_change=0; unexpected=0; only_after=0; only_before=0; compared=0
496# ONE LINE PER MANUSCRIPT, not one line per failure: the output length is the
497# denominator, and a manuscript that quietly stopped being examined is visible
498# without anyone counting.
499for f in "$gate_dir/sec-after"/*; do
500 name="${f##*/}"
501 compared=$((compared + 1))
502 b="$gate_dir/sec-before/$name"
503 root_a=$(awk -v n="$name" '$1==n{print $2}' "$gate_dir/roots-after")
504 root_b=$(awk -v n="$name" '$1==n{print $2}' "$gate_dir/roots-before")
505 if [ ! -f "$b" ]; then
506 only_after=$((only_after + 1))
507 printf 'AFTER-ONLY %-40s root=%s (the base tree could not lower it)\n' "$name" "$root_a" >>"$ledger"
508 continue
509 fi
510 if cmp -s "$f" "$b"; then
511 identical=$((identical + 1))
512 printf 'IDENTICAL %-40s root=%s\n' "$name" "$root_a" >>"$ledger"
513 test "$root_a" = "$root_b" \
514 || die "arm 05: $name lowers identically yet its root moved $root_b -> $root_a; the normalization is hiding a real difference"
515 continue
516 fi
517 if [ "$root_a" = "$root_b" ]; then
518 unexpected=$((unexpected + 1))
519 printf 'UNEXPECTED %-40s root=%s unchanged, lowering differs\n' "$name" "$root_a" >>"$ledger"
520 continue
521 fi
522 # A root-changed manuscript must differ by EXACTLY the root symbol. Substituting
523 # the old symbol for the new one in the BEFORE section must reproduce the AFTER
524 # section byte for byte -- a far stronger statement than "it changed, as
525 # predicted", and the one that would catch a migration that also moved content.
526 old="${root_b#@}"; new="${root_a#@}"
527 # THE SUBSTITUTION MUST BE UNAMBIGUOUS BEFORE IT IS TRUSTED, and it must be
528 # BOUNDED. A rewrite that could match something other than the root symbol
529 # would MASK a real difference, which is the one direction a differential must
530 # never fail in. Three conditions:
531 #
532 # * after the change the old spelling must be gone entirely, and before it
533 # the new spelling must not have existed -- so the mapping is a bijection
534 # on this pair of sections;
535 # * the rewrite must change SOMETHING, or the comparison below is between
536 # the two sections unmodified and a genuine root-only change would read as
537 # UNEXPECTED for the wrong reason;
538 # * and it must change AT MOST as many occurrences as a root can account
539 # for. A `/g` substitution over the whole section would also rewrite a
540 # NESTED generated `chapter` head, so a manuscript that changed its root
541 # AND some other command of the same name would report "nothing else
542 # moved". The root appears twice in a lowered manuscript -- once in the
543 # generated command head and once in the `author-flatten` command-name
544 # argument -- so more than two is a second construct, not the root.
545 if [ "$( { rg -cF "]] $new>" "$b" || true; } )" != "" ] \
546 || [ "$( { rg -cF "]] $old>" "$f" || true; } )" != "" ]; then
547 unexpected=$((unexpected + 1))
548 printf 'UNEXPECTED %-40s root=%s -> %s, but the two spellings overlap; refusing to rewrite\n' \
549 "$name" "$root_b" "$root_a" >>"$ledger"
550 continue
551 fi
552 occurrences=$( { rg -oF "]] $old>" "$b" || true; } | wc -l)
553 if [ "$occurrences" -lt 1 ] || [ "$occurrences" -gt 2 ]; then
554 unexpected=$((unexpected + 1))
555 printf 'UNEXPECTED %-40s root=%s -> %s, but %s generated `%s` heads is not a root alone; refusing to rewrite\n' \
556 "$name" "$root_b" "$root_a" "$occurrences" "$old" >>"$ledger"
557 continue
558 fi
559 sed "s/\]\] $old>/]] $new>/g" "$b" >"$gate_dir/rewritten-$name"
560 cmp -s "$b" "$gate_dir/rewritten-$name" \
561 && die "arm 05: the root rewrite for $name changed nothing, so its root symbol is not where this arm thinks it is"
562 if cmp -s "$f" "$gate_dir/rewritten-$name"; then
563 expected_change=$((expected_change + 1))
564 printf 'ROOT-ONLY %-40s root=%s -> %s, nothing else moved\n' "$name" "$root_b" "$root_a" >>"$ledger"
565 else
566 unexpected=$((unexpected + 1))
567 printf 'UNEXPECTED %-40s root=%s -> %s, and MORE than the root moved\n' "$name" "$root_b" "$root_a" >>"$ledger"
568 fi
569done
570for f in "$gate_dir/sec-before"/*; do
571 name="${f##*/}"
572 if [ ! -f "$gate_dir/sec-after/$name" ]; then
573 only_before=$((only_before + 1))
574 printf 'BEFORE-ONLY %-40s (this tree can no longer lower it)\n' "$name" >>"$ledger"
575 fi
576done
577cat "$ledger"
579# THE DENOMINATOR, stated against the dumps' own totals rather than against the
580# ledger the loop just wrote -- a loop cannot audit itself.
581after_found=$(rg -o 'SD_P8_LOWERING_DUMP_COMPLETE found \d+' "$after_dump" | awk '{print $NF}')
582after_compared=$(rg -o 'compared \d+' "$after_dump" | awk '{print $NF}')
583before_compared=$(rg -o 'compared \d+' "$before_dump" | awk '{print $NF}')
584test "$compared" -eq "$after_compared" \
585 || die "arm 05: the dump lowered $after_compared manuscripts but the ledger holds $compared"
586echo " denominator: after found $after_found, lowered $after_compared; before lowered $before_compared; ledger rows $((compared + only_before))"
588# `only_after` and `skipped` ARE ASSERTED, and the first version of this arm did
589# not assert either. A count nobody asserts on is a count nobody is checking:
590# `test/fixtures/sd-p1-article-root.sgd` had its whole body rewritten by this
591# phase (+10 -9) and passed through this ledger as `AFTER-ONLY ... the base tree
592# could not lower it`, which is TRUE and says nothing about its content. The
593# after-only class is legitimate but it is not free: each member must be named
594# in advance, so a manuscript joining it is a decision rather than an accident.
596# Both expected members are here for a REASON, not merely listed:
597# sd-p1-article-root.sgd root `@article`; the base profile at the base SHA
598# was `(chapter)` and refused it. D8 is exactly the
599# change that makes it lowerable, so its appearance
600# here is the phase's headline effect, not a gap.
601# sd-p8-book-chapter-root.sgd added by this phase; no base-side counterpart
602# can exist.
603expected_after_only="$gate_dir/expected-after-only"
604printf 'sd-p1-article-root.sgd\nsd-p8-book-chapter-root.sgd\n' | sort >"$expected_after_only"
605actual_after_only="$gate_dir/actual-after-only"
606{ rg '^AFTER-ONLY' "$ledger" || true; } | awk '{print $2}' | sort >"$actual_after_only"
607diff -u "$expected_after_only" "$actual_after_only" >"$gate_dir/after-only.diff" \
608 || { cat "$gate_dir/after-only.diff" >&2
609 die "arm 05: the set of manuscripts only the current tree can lower is not the set this phase intends"; }
610test "$only_after" -eq "$(wc -l <"$expected_after_only")" \
611 || die "arm 05: $only_after after-only rows for $(wc -l <"$expected_after_only") expected names"
612# A CONTENT REWRITE OF AN AFTER-ONLY MANUSCRIPT IS STILL A CHANGE, and the
613# differential structurally cannot see it (there is no before-side section to
614# compare). Saying so is the honest disposition; hiding it behind a passing
615# ledger row is not. The suite is what covers these: `test/test-root-profile.sgl`
616# evaluates `sd-p1-article-root.sgd` through the shipped base profile and asserts
617# its kind and title, and `test/sd-p8-language-probe.sgl` loads
618# `sd-p8-book-chapter-root.sgd` through the real `#lang` path.
619echo " after-only: $only_after manuscripts, named in advance; their CONTENT is not covered by this differential and is covered by the suite and the language probe instead"
620# THE SKIPPED SETS, COMPARED BY NAME. The first version of this check compared
621# COUNTS and required `after <= before`, which was simply wrong: this phase adds
622# four manuscripts that are DELIBERATELY unlowerable (the negative fixtures the
623# language probe drives), so the after side legitimately skips more. The gate
624# caught it loudly on its first run with "something stopped being lowerable",
625# which was false -- a count cannot tell "a manuscript regressed" from "a
626# manuscript was added to be refused". Only the names can.
628# Two directions, both asserted:
629# * a manuscript the base tree could LOWER must not have become skippable --
630# that is the regression this check exists for;
631# * a manuscript newly skipped must be one this phase ADDED, which is derived
632# from git rather than listed here.
633skipped_names() { rg -o '^--- SKIPPED \S+' "$1" | awk '{print $3}' | sort; }
634skipped_names "$after_dump" >"$gate_dir/skipped-after"
635skipped_names "$before_dump" >"$gate_dir/skipped-before"
636comm -13 "$gate_dir/skipped-before" "$gate_dir/skipped-after" >"$gate_dir/newly-skipped"
637comm -23 "$gate_dir/skipped-before" "$gate_dir/skipped-after" >"$gate_dir/no-longer-skipped"
638git diff --name-only --diff-filter=A "$base_sha" HEAD -- '*.sgd' \
639 | sed 's|.*/||' | sort >"$gate_dir/added-manuscripts"
640while read -r n; do
641 test -n "$n" || continue
642 grep -Fxq -- "$n" "$gate_dir/added-manuscripts" \
643 || { echo "newly skipped: $n" >&2
644 die "arm 05: $n is skipped by this tree and was not skipped by the base tree, and this phase did not add it -- it stopped being lowerable"; }
645done <"$gate_dir/newly-skipped"
646# The other direction is a GAIN, not a fault, but it must still be named: a
647# manuscript that stopped being skipped is one D8 made lowerable, and it has to
648# be in the after-only set the arm already pins.
649while read -r n; do
650 test -n "$n" || continue
651 grep -Fxq -- "$n" "$expected_after_only" \
652 || { echo "no longer skipped: $n" >&2
653 die "arm 05: $n became lowerable and is not in this arm's expected after-only set"; }
654done <"$gate_dir/no-longer-skipped"
655echo " skipped: after $(wc -l <"$gate_dir/skipped-after"), before $(wc -l <"$gate_dir/skipped-before"); $(wc -l <"$gate_dir/newly-skipped") newly skipped (all added by this phase), $(wc -l <"$gate_dir/no-longer-skipped") newly lowerable"
657# A FLOOR, deliberately, not a restatement: adding a manuscript must not
658# desynchronise this, and a corpus that collapsed must not read as a pass.
659test "$identical" -ge 15 \
660 || die "arm 05: only $identical manuscripts lower identically; the migration moved more than the header"
661test "$expected_change" -ge 1 \
662 || die "arm 05: no manuscript changed its root, so the root-only comparison never ran and proves nothing"
663test "$unexpected" -eq 0 \
664 || { rg '^UNEXPECTED' "$ledger" >&2; die "arm 05: $unexpected manuscripts changed for a reason this phase did not intend"; }
665test "$only_before" -eq 0 \
666 || { rg '^BEFORE-ONLY' "$ledger" >&2; die "arm 05: this tree stopped being able to lower a manuscript the base tree could"; }
667pass 5 "differential vs $base_sha: $identical identical, $expected_change root-only, $only_after newly lowerable, 0 unexpected, over $compared lowered of $after_found found"
669# ---------------------------------------------------------------- ARM 06
670# THE CONSTRUCTION DIFFERENTIAL, replacing `test/gate-sd-p7.sh` arm 02.
672# Arm 5 above compares LOWERING -- emitted syntax. This compares the CONSTRUCTED
673# document, which is a different property and the one SD-P7 owns: a lowering
674# differential is byte-identical under a completely broken content profile. When
675# arm 02 of gate-sd-p7 was retired (its baseline is pinned to absolute byte
676# offsets that a `#lang` edit shifts), that property lost its standing check, and
677# retiring without replacing loses a property.
679# THE ONE DESIGN DECISION, and it is the lesson of the retirement it replaces:
680# THIS ARM ASSERTS ON STRUCTURE, NOT ON POSITIONS. `document->string` embeds
681# every node's `(file pos line col span)`. Comparing those verbatim is precisely
682# what made the old baseline unmaintainable, so every location TUPLE belonging
683# to a manuscript is normalised to the marker `L` before comparison. What
684# remains is the document's shape and every non-positional field: kinds, titles,
685# authors, ids, text, the tree.
687# That is deliberately weaker than the arm it replaces, and the weakening is
688# named rather than glossed: THIS ARM CANNOT SEE A PURE LOCATION REGRESSION.
689# Locations are covered instead by arm 5 (which asserts them header-relative,
690# so it sees any shift that is not the header's) and by
691# `test/test-root-profile.sgl`, whose location assertions recompute every
692# expected byte offset, line and column independently.
693before_c="$gate_dir/construction-before.txt"
694after_c="$gate_dir/construction-after.txt"
695setsid -w bash test/sd-p7-construction-dump.sh "$after_c" >"$gate_dir/construction-after.log" 2>&1 \
696 || { tail -20 "$gate_dir/construction-after.log" >&2; die "arm 06: the construction dump failed on this tree"; }
697( cd "$base_tree" \
698 && cp "$repo_dir/test/sd-p7-construction-dump.sh" test/ \
699 && SIGIL_D1_REDIRECTS="$gate_dir/base-redirects.sgl" \
700 SIGIL_D1_COMPILER="$sigil" \
701 setsid -w bash test/sd-p7-construction-dump.sh "$before_c" ) \
702 >"$gate_dir/construction-before.log" 2>&1 \
703 || { tail -20 "$gate_dir/construction-before.log" >&2; die "arm 06: the construction dump failed on the base tree"; }
704for side in "$before_c" "$after_c"; do
705 rg -q 'SD_P7_CONSTRUCTION_DUMP_COMPLETE' "$side" \
706 || { tail -10 "$side" >&2; die "arm 06: a construction dump printed no completion marker"; }
707done
709# The location normaliser, and the marker line excluded for the reason SD-P3's
710# gate learned the hard way: it names the corpus size, so leaving it in ties the
711# LAST manuscript's record to how many manuscripts were dumped.
712split_construction() { # $1 = dump, $2 = out dir
713 mkdir -p "$2"
714 grep -v '^SD_P7_CONSTRUCTION_DUMP_COMPLETE' "$1" \
715 | sed -E 's/("[^"]*\.sgd") [0-9]+ [0-9]+ [0-9]+ [0-9]+/\1 L/g' \
716 | awk -v o="$2" '/^=== /{n=$2; gsub("/","_",n)} n!=""{print > (o "/" n)}'
718split_construction "$before_c" "$gate_dir/con-before"
719split_construction "$after_c" "$gate_dir/con-after"
721# THE NORMALISER MUST BE SHOWN TO HAVE FIRED. A `sed` that matched nothing would
722# make this a verbatim comparison wearing a normaliser's name -- and it would
723# then fail for exactly the reason the retired arm did, with a message about
724# structure.
726# CHECK THE NORMALISED OUTPUT, NOT THE RAW DUMP. The first version of this guard
727# grepped `$after_c`, which is the dump BEFORE `split_construction` runs sed over
728# it, so the marker could never appear there and the guard reddened on a healthy
729# tree. Caught by the guard itself on its first run, which is the only reason a
730# check aimed at one file rather than another is visible at all.
731normalised=$( { rg -c ' L\)' "$gate_dir"/con-after/* || true; } | awk -F: '{n+=$NF} END{print n+0}')
732test "${normalised:-0}" -ge 1 \
733 || die "arm 06: the location normaliser matched nothing in the split records; it is not normalising, so this comparison is not the one described"
735con_ledger="$gate_dir/construction-ledger.txt"
736: >"$con_ledger"
737con_same=0; con_diff=0; con_after_only=0; con_before_only=0
738for f in "$gate_dir/con-before"/*; do
739 name="${f##*/}"
740 a="$gate_dir/con-after/$name"
741 if [ ! -f "$a" ]; then
742 con_before_only=$((con_before_only + 1))
743 printf 'BEFORE-ONLY %s (this tree no longer constructs it)\n' "$name" >>"$con_ledger"
744 elif cmp -s "$f" "$a"; then
745 con_same=$((con_same + 1))
746 printf 'IDENTICAL %s\n' "$name" >>"$con_ledger"
747 else
748 con_diff=$((con_diff + 1))
749 printf 'DIFFERS %s\n' "$name" >>"$con_ledger"
750 fi
751done
752for f in "$gate_dir/con-after"/*; do
753 name="${f##*/}"
754 test -f "$gate_dir/con-before/$name" || {
755 con_after_only=$((con_after_only + 1))
756 printf 'AFTER-ONLY %s (added by this phase)\n' "$name" >>"$con_ledger"; }
757done
758cat "$con_ledger"
760# THE EXPECTED DIFFERENCES, NAMED. Anything else is a finding. All three are D8
761# itself: two manuscripts whose root command changed, so their document KIND
762# changed with it, and one that the base tree could not construct at all because
763# its `@article` root was illegal there -- which is the whole point of the phase.
764expected_con="$gate_dir/expected-construction-diffs"
765printf 'test_fixtures_sd-p1-article-root.sgd\ntest_fixtures_sd-p3-bare-prose.sgd\ntest_fixtures_sd-p3-wrapped.sgd\n' | sort >"$expected_con"
766{ rg '^DIFFERS' "$con_ledger" || true; } | awk '{print $2}' | sort >"$gate_dir/actual-construction-diffs"
767diff -u "$expected_con" "$gate_dir/actual-construction-diffs" >"$gate_dir/construction.diff" \
768 || { cat "$gate_dir/construction.diff" >&2
769 die "arm 06: the constructed documents that changed are not the ones this phase intends"; }
770# And the two kind flips are asserted BY NAME, so "it differs" cannot stand in
771# for "it differs in the way D8 predicts".
772for m in sd-p3-bare-prose sd-p3-wrapped; do
773 rg -q '\(document \(kind chapter\)' "$gate_dir/con-before/test_fixtures_$m.sgd" \
774 || die "arm 06: $m was not a chapter at the base, so the kind-flip claim is not about what changed"
775 rg -q '\(document \(kind article\)' "$gate_dir/con-after/test_fixtures_$m.sgd" \
776 || die "arm 06: $m is not an article now"
777done
778rg -q 'RAISED' "$gate_dir/con-before/test_fixtures_sd-p1-article-root.sgd" \
779 || die "arm 06: the base tree DID construct sd-p1-article-root.sgd, so D8 is not what made it constructible"
780rg -q '\(document \(kind article\)' "$gate_dir/con-after/test_fixtures_sd-p1-article-root.sgd" \
781 || die "arm 06: sd-p1-article-root.sgd still does not construct"
782test "$con_before_only" -eq 0 \
783 || { rg '^BEFORE-ONLY' "$con_ledger" >&2; die "arm 06: this tree stopped constructing a manuscript the base tree built"; }
784# A FLOOR, labelled: not a restatement of the current count.
785test "$con_same" -ge 15 \
786 || die "arm 06: only $con_same constructed documents are unchanged; the migration moved more than D8 intends"
787pass 6 "construction differential vs $base_sha: $con_same identical, $con_diff changed and all named, $con_after_only added, 0 lost; locations normalised (see comment: this arm cannot see a pure location regression, arm 5 and the suite cover that)"
789# ---------------------------------------------------------------- ARM 07
790# REGRESSION. The full suite with the FULL load path -- four entries, because a
791# short path degrades into "failed to load", which the runner reports WITHOUT
792# failing the process. The file count is its own assertion for that reason.
794# `SD_P8_SKIP_REGRESSION` EXISTS FOR ONE CALLER: `test/sabotage-sd-p8.sh`, whose
795# legs would otherwise pay for this suite seven times over to reach a red in an
796# earlier arm. A skipped arm must never read as a passing arm, so it prints
797# SKIPPED rather than a pass line, and the script ends at SD_P8_GATE_PARTIAL
798# instead of SD_P8_GATE_GREEN. The sabotage suite's own positive control runs
799# the gate with this UNSET and requires the GREEN marker, so no leg can be
800# satisfied by a run that skipped anything.
801if [ -n "${SD_P8_SKIP_REGRESSION:-}" ]; then
802 printf 'SD_P8_GATE_07 SKIPPED (SD_P8_SKIP_REGRESSION is set)\n'
803 echo "SD_P8_GATE_PARTIAL arms 1-6 only; the regression arm did not run"
804 exit 0
805fi
806regression_log="$gate_dir/regression.log"
807regression_status=0
808setsid -w "$sigil" -L "$sxml_root/src" -L src -L test -L book test \
809 >"$regression_log" 2>&1 || regression_status=$?
810sed -i 's/\x1b\[[0-9;]*m//g' "$regression_log"
811grep -qE 'Tests: +[0-9]+ passed' "$regression_log" \
812 || { tail -40 "$regression_log" >&2; die "arm 07: could not read a suite summary -- the run may not have executed anything"; }
813files_line=$(grep -oE 'Files: +[0-9]+ of [0-9]+ run' "$regression_log" | tail -1 || true)
814test -n "$files_line" || { tail -40 "$regression_log" >&2; die "arm 07: the runner reported no file count"; }
815files_run=$(echo "$files_line" | grep -oE '[0-9]+' | head -1)
816files_found=$(echo "$files_line" | grep -oE '[0-9]+' | tail -1)
817test "$files_run" = "$files_found" || die "arm 07: only $files_run of $files_found test files ran"
818if grep -qE '[0-9]+ failed' "$regression_log"; then
819 grep -nE '✗|failed' "$regression_log" | head -30 >&2
820 die "arm 07: a test failed"
821fi
822test "$regression_status" = "0" \
823 || { tail -40 "$regression_log" >&2; die "arm 07: the runner exited $regression_status with no failing test reported"; }
824# The root-profile suite's count DERIVED from its own source, and the two arms
825# this phase added named individually: a suite whose arms were deleted still
826# reports "all tests passed".
827expected_arms=$(rg -c '^\(test "' test/test-root-profile.sgl)
828rg -q "test-root-profile" "$regression_log" || die "arm 07: the root-profile suite did not run"
829rg -q "ok +$expected_arms passed +\./test/test-root-profile.sgl" "$regression_log" \
830 || { rg -n 'test-root-profile' "$regression_log" >&2
831 die "arm 07: the root-profile suite did not report $expected_arms of its $expected_arms arms"; }
832rg -q 'the book genre language is a sibling profile with its own diagnostics' "$regression_log" \
833 || die "arm 07: the genre-language arm is not in the suite that ran"
834rg -q "the base profile is D8's policy and D8's diagnostics, exactly" "$regression_log" \
835 || die "arm 07: the base-profile arm is not in the suite that ran"
836summary=$(grep -E 'Tests: +[0-9]+ passed' "$regression_log" | tail -1 | tr -s ' ')
837pass 7 "full suite green with -L $sxml_root/src -L src -L test -L book :: $summary, $files_line, root-profile $expected_arms/$expected_arms"
839echo "SD_P8_GATE_GREEN"