test: SD-P8 gate, differential, language probe and sabotage suite
test/gate-sd-p8.sh, six arms, cheapest first so a sabotage leg aimed at an early one does not pay for the expensive ones:
1 both shipped profiles, through public modules only
2 kind `book` refused by name, with two positive controls, plus a sweep of
every `make-document` call site in the tree rather than only the diff
3 a corpus census printing one row per manuscript, with an explicit
unclassified class, so its output length is its denominator
4 both languages END TO END through the real `#lang` path: two required
greens and four required reds, each named, plus the diagnostic each refusal
carries at the reader
5 the D8 differential
6 the full suite on the full four-entry load pathArm 5 replaces gate-sd-p3.sh arms 3 and 4, retired in the same commit under Fable's 2026-08-11 ruling and in the form that ruling records: they still PASS, with a message naming the retirement and pointing at the successor, and their transcripts stay in the tree. Both were measured GREEN on master 0b7c5ca in a detached worktree first, so the notice says what it retires instead of guessing.
The differential generates BOTH sides in one run -- a detached worktree at the base SHA and the working tree -- so it has no committed baseline to go stale, and it refuses with "baseline superseded" if HEAD stops descending from that base. Its claim is not that hashes match but that the ONLY thing that changed is the root symbol, and for the nineteen header-only manuscripts not even that:
17 identical, 3 root-only, 2 newly lowerable, 0 unexpected,
over 22 lowered of 32 found, vs 0b7c5catest/sd-p8-lowering-dump.sgl is what makes that assertable. A manuscript's byte offsets reach its lowered output in THREE places -- the syntax srclocs, the integers inside generated make-srcloc calls, and the generated binding name #%sigil/doc-expression:<offset> -- and each was found by the previous one's fix failing to close the diff.
Arm 4 asserts the #lang-layer message against the behaviour the compiler version it records is supposed to have: empty below sigil 0.19.1, carrying the reader's diagnostic at or above. Both branches assert, and an unparseable version is fatal rather than falling through to the weaker one.
test/sabotage-sd-p8.sh: five product reds, one instrument red, one coverage red and a positive control that runs the gate with nothing skipped. Every pinned literal this phase reworded is shown able to fail by perturbing the product, not the assertion. Restores come from saved copies verified by hash, because git checkout refuses a whole invocation on an untracked pathspec and the first version of this script therefore restored nothing while reporting success.
test/fixtures/sd-p8-base-chapter-root.sgd | 8 +++
test/fixtures/sd-p8-base-two-articles.sgd | 10 +++
test/fixtures/sd-p8-book-article-root.sgd | 7 +++
test/fixtures/sd-p8-book-chapter-root.sgd | 7 +++
test/fixtures/sd-p8-book-two-chapters.sgd | 10 +++
test/gate-sd-p3.sh | 230 +++++++++++++++++++++++++++++----------------------------------------
test/gate-sd-p8.sh | 502 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
test/sabotage-sd-p8.sh | 289 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
test/sd-p1-classification-probe.sgl | 14 +++++
test/sd-p8-language-probe.sgl | 109 +++++++++++++++++++++++++++++++++
test/sd-p8-lowering-dump.sgl | 285 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
11 files changed, 1337 insertions(+), 134 deletions(-)test/fixtures/sd-p8-base-chapter-root.sgdadded
#lang sigil/doc@chapter["A Chapter In The Base Language" "SD-P8"]{@p{This manuscript MUST be refused. `#lang sigil/doc` admits exactly one@code{@@article} since ruling D8; @code{@@chapter} belongs to@code{#lang sigil/doc/book}. Nothing here is malformed -- the grammar, theargument list and the body are all ordinary -- so the only thing that canreject it is the root policy, which is the point.}}test/fixtures/sd-p8-base-two-articles.sgdadded
#lang sigil/doc@article["First" "SD-P8"]{@p{This manuscript MUST be refused: the base profile's multiplicity is@code{one}, and a second root follows.}}@article["Second" "SD-P8"]{@p{The second root. Each half alone is a legal manuscript, so a parser thataccepted this one would be dropping a root rather than misreading a brokenfile.}}test/fixtures/sd-p8-book-article-root.sgdadded
#lang sigil/doc/book@article["An Article In The Book Language" "SD-P8"]{@p{This manuscript MUST be refused. @code{#lang sigil/doc/book} admits exactlyone @code{@@chapter}; @code{@@article} is the BASE language's root. The refusalmust name @code{sigil/doc/book}, because a genre language that reported@code{sigil/doc} would send an author to the wrong documentation.}}test/fixtures/sd-p8-book-chapter-root.sgdadded
#lang sigil/doc/book@chapter["A Chapter In The Book Language" "SD-P8"]{@p{The POSITIVE CONTROL for the four refusals beside it. Every other SD-P8fixture in this directory must be rejected, and a language that rejectedeverything would satisfy all four. This one must load, and its document kindmust be @code{chapter}.}}test/fixtures/sd-p8-book-two-chapters.sgdadded
#lang sigil/doc/book@chapter["First" "SD-P8"]{@p{This manuscript MUST be refused: the genre profile's multiplicity is@code{one}, and a second chapter follows. A book is assembled from severalone-chapter manuscripts by @code{make-print-publication}, never by puttingseveral chapters in one file.}}@chapter["Second" "SD-P8"]{@p{The second root.}}test/gate-sd-p3.shmodified
pass 2 "equality over $texts text / $codes code / $refs reference / $urls url-link / $paras paragraph nodes; locations at $located nodes"# ---------------------------------------------------------------- ARM 03# Real manuscripts: EQUALITY against a pre-SD-P3 baseline, not absence of# complaint. Most of the corpus must be byte-identical, and the ones that differ# must be EXACTLY the manuscripts an INDEPENDENT instrument -- the passive# boundary census -- says contain a non-boundary node. Two different# measurements of the same population have to agree.# RETIRED 2026-08-22 BY THE LEADER, AT SD-P8, UNDER FABLE'S RULING OF# 2026-08-11. This arm has NOT silently disappeared and its red has NOT been# hidden -- read this before concluding anything.## The arm DERIVES both numbers at run time; they are deliberately not written# here. An earlier version of this comment said "71 of 74", which was true of# the corpus when it was written and became wrong at 75 of 80 once the corpus# was re-derived and the population moved. Corrected by the leader at merge, on# the reviewer's note. The stale figure asserted nothing -- the assertions# always derived -- but a reader checking the gate against its own comment# would have found a mismatch and had to work out which one to believe.# WHAT IT DID. It dumped the complete lowered form sequence for a 16-manuscript# corpus and required each one's sha256 to equal a row in# `test/transcripts/sd-p3/lowering-before.txt`, a PRE-SD-P3 baseline, except for# the manuscripts an independent boundary census predicted would change. It was# GREEN on master `0b7c5ca`, measured in a detached worktree before this phase# touched anything:## This is exactly the H4 shape this phase recorded in the taxonomy: an# enumeration, or any count, is only true of the population it was taken from,# and carries an implicit "as of" that nobody writes down. A comment stating a# derived number is a second, un-derived copy of it that cannot be kept honest.# Prefer describing WHAT is asserted over restating the current value.baseline=test/transcripts/sd-p3/lowering-before.txtrg -q 'PRE_SD_P3_CONTROL_UNBOUND' "$baseline" \ || die "the BEFORE baseline carries no pre-SD-P3 positive control; it cannot be trusted"rg -q 'PRE_SD_P3_CONTROL_RESOLVES' "$baseline" \ || die "the BEFORE baseline does not record the probe seeing the binding when present"# The premise: the fixture arm 1 evaluates was REJECTED by the pre-SD-P3 tree,# and rejected for B4's reason rather than for any reason at all.rg -q 'PRE_SD_P3_BARE_PROSE_REJECTED REJECTED .*wrong semantic category' "$baseline" \ || die "the BEFORE baseline does not record the bare-prose fixture being rejected for the B4 reason"after_dump="$gate_dir/lowering-after.txt"SD_P3_COURSE_DIR="$course_dir" setsid -w "$sigil" -L src -L test eval \ -f test/sd-p3-lowering-dump.sgl >"$after_dump" 2>&1rg -q 'SD_P3_LOWERING_DUMP_COMPLETE' "$after_dump" || { tail -20 "$after_dump" >&2; die "the AFTER dump printed no completion marker"; }split_dir="$gate_dir/after-sections"mkdir -p "$split_dir"# The trailing SD_P3_LOWERING_DUMP_COMPLETE line is EXCLUDED from the sections.# It names the corpus size, so leaving it in makes the LAST manuscript's hash# depend on how many manuscripts were dumped -- a run without the course corpus# would then differ from the baseline at that one file for a reason that has# nothing to do with the lowering.awk -v d="$split_dir" '/^SD_P3_LOWERING_DUMP_COMPLETE/{next} /^=== /{name=$2} name!=""{print > (d "/" name)}' "$after_dump"after_hashes="$gate_dir/after.sha256": >"$after_hashes"for f in "$split_dir"/*; do printf '%s %s\n' "$(sha256sum "$f" | awk '{print $1}')" "${f##*/}" >>"$after_hashes"donesort -k2 "$after_hashes" -o "$after_hashes"baseline_hashes="$gate_dir/before.sha256"rg '^[0-9a-f]{64} ' "$baseline" | sort -k2 >"$baseline_hashes"# Coverage, stated rather than assumed: a run without the course corpus checks# fewer manuscripts and must SAY so instead of looking complete.covered=$(wc -l <"$after_hashes")recorded=$(wc -l <"$baseline_hashes")test "$covered" -gt 0 || die "the AFTER dump produced no manuscript sections"if [ "$covered" -lt "$recorded" ]; then echo "NOTE: the course corpus is absent; $covered of $recorded recorded manuscripts checked" >&2 if [ -n "$course_dir" ]; then die "SD_P3_COURSE_DIR is set but only $covered manuscripts were dumped" fifi# Names present on both sides, so a manuscript that vanished from the dump# cannot read as "identical".comm -12 <(awk '{print $2}' "$baseline_hashes") <(awk '{print $2}' "$after_hashes") \ >"$gate_dir/common-names"common=$(wc -l <"$gate_dir/common-names")test "$common" -eq "$covered" || die "the AFTER dump names $covered manuscripts, $common of which the baseline knows"changed="$gate_dir/changed-names": >"$changed"while read -r name; do b=$(awk -v n="$name" '$2==n{print $1}' "$baseline_hashes") a=$(awk -v n="$name" '$2==n{print $1}' "$after_hashes") test -n "$b" && test -n "$a" || die "missing hash for $name" if [ "$b" != "$a" ]; then echo "$name" >>"$changed"; fidone <"$gate_dir/common-names"# The independent expectation: exactly the manuscripts the census found hits in.census=test/transcripts/sd-p3/boundary-census-before.txtrg -q 'SD_P3_BOUNDARY_CENSUS_COMPLETE' "$census" || die "the census transcript is incomplete"# Fields: $4 examined, $6 non-boundary commands, $8 non-boundary expressions# (with a trailing paren, so it is coerced with +0). Indexing $4 here -- the# DENOMINATOR -- would name every manuscript instead of the ones with hits.awk '/^\(report /{ if ($6+0 != 0 || $8+0 != 0) { gsub(/"/,"",$2); n=split($2,p,"/"); print p[n] } }' \ "$census" | sort -u >"$gate_dir/census-names"comm -12 "$gate_dir/census-names" <(sort "$gate_dir/common-names") >"$gate_dir/expected-changed"if ! diff -u <(sort "$gate_dir/expected-changed") <(sort "$changed") >"$gate_dir/changed.diff"; then cat "$gate_dir/changed.diff" >&2 die "the manuscripts whose lowering changed are not the manuscripts the census predicted"fiidentical=$((covered - $(wc -l <"$changed")))test "$identical" -gt 0 || die "no manuscript was byte-identical; the comparison is not discriminating"pass 3 "lowering byte-identical for $identical of $covered manuscripts; the $(wc -l <"$changed") that changed are exactly the census's"# SD_P3_GATE_03 lowering byte-identical for 12 of 16 manuscripts;# the 4 that changed are exactly the census's## So what is being retired is a WORKING arm, not a broken one, and that is the# only honest way to describe it.## WHY IT IS GONE. Ruling D8 (David, 2026-08-22) moves `@chapter` out of# `#lang sigil/doc` into `#lang sigil/doc/book`, which edits the `#lang` line of# nineteen manuscripts. All 16 of this arm's corpus are among them. A header# line growing from 16 bytes to 21 moves every byte offset in the manuscript by# 5, so every one of those hashes changes -- measured before the migration was# written, by editing one fixture and re-running the dump:## minimal.sgd before 2b0e9c5dc784d267... after 49bd6d522d9b6f6e...# first location ["minimal.sgd" 2:1 byte=16 ...] -> [... byte=21 ...]## THE BASELINE CANNOT BE RESCUED, and it is worth saying exactly why, because# "just normalise it" is the obvious wrong answer: `lowering-before.txt` stores# HASHES, not sections. There is nothing on the BEFORE side to normalise. Nor# can it be regenerated -- it is a measurement of a tree at `4dcb41d0`, and# regenerating it from a later tree destroys its meaning rather than updating# it.## A SECOND, INDEPENDENT REASON it could not survive: the dump reaches lowering# through the three-argument `parse-document-forms`, which is hardcoded to# `sigil-doc-profile` (`src/sigil/doc/private/parser.sgl`). Once that profile's# root set is `(article)`, the dump refuses every `@chapter` manuscript whatever# its header says. Even a corpus with no header edits at all would have gone# red.## THE RULING IT IS RETIRED UNDER (Fable, 2026-08-11, recorded in the SD-P1 row# of topics/sigil-doc-implementation-plan, applied here by the leader# 2026-08-22): a differential gate's meaning is bound to the two endpoints it# compares. Standing gates assert invariants; differential gates certify# transitions, and a completed transition does not become a standing invariant# just because the check is still executable. Retiring it here is legitimate# BECAUSE it is ruled and recorded -- laundering is hiding a red, not retiring a# differential whose endpoints have moved.## SUCCESSOR, and it is strictly stronger: `test/gate-sd-p8.sh` arm 3. It# generates BOTH sides in one run -- a detached worktree at SD-P8's own base and# the working tree -- so it has no committed baseline to go stale, and it# refuses with "baseline superseded" if HEAD stops descending from that base. It# compares 21 manuscripts rather than 16, on a DERIVED population rather than an# inherited list, and its claim is not "the hashes match" but "the only thing# that changed is the root symbol, and for the nineteen header-only manuscripts# not even that" -- a hash equality tells you two numbers differ and nothing# about why.## EVIDENCE PRESERVED: `test/transcripts/sd-p3/lowering-before.txt` and# `boundary-census-before.txt` stay in the tree unmodified, and the `test -f`# preconditions at the top of this gate still assert they are present. They are# SD-P3's reviewed evidence; regenerating them from a later tree would destroy# their meaning.## STANDING PATTERN, restated for whoever changes lowering next: ship your own# before/after differential scoped to your own base, embed the base SHA in the# arm, and make a superseded base fail with "baseline superseded -- see ruling"# rather than with a bare diff. `test/gate-sd-p8.sh` arm 3 is a worked example.pass 3 "RETIRED 2026-08-22 at SD-P8 under Fable's 2026-08-11 ruling; superseded by test/gate-sd-p8.sh arm 3 (see comment)"# ---------------------------------------------------------------- ARM 04# The changed manuscripts changed for the reason claimed: one# `author-paragraph-run` site per non-boundary node the census found, per file.total_sites=0while read -r name; do # `|| true`: rg exits 1 when it finds nothing, and under `set -o pipefail` # a bare assignment would kill the gate BEFORE reaching the `die` below that # exists to explain exactly that case. sites=$( { rg -o 'author-paragraph-run' "$split_dir/$name" || true; } | wc -l) hits=$(awk -v n="$name" '/^\(report /{ gsub(/"/,"",$2); c=split($2,p,"/"); if (p[c]==n) print $6+0 + $8+0 }' "$census") test -n "$hits" || die "no census row for $name" # A LAW, not the coincidence. One run can contain several non-boundary # commands and still emit ONE site, and a non-boundary command nested inside # an already-folded one is counted by the census but emits none -- so # `sites == hits` is a property of THIS corpus, not of the change. What must # always hold is that a manuscript the census flagged emits at least one site # and never more than it has flagged nodes. test "$sites" -ge 1 \ || die "$name was flagged by the census with $hits non-boundary nodes but emits no paragraph-run site" test "$sites" -le "$hits" \ || die "$name emits $sites paragraph-run sites but the census found only $hits non-boundary nodes" # The corpus-specific equality is still REPORTED, so a change in it is # visible in the transcript rather than silently absorbed. if [ "$sites" -ne "$hits" ]; then echo "NOTE: $name emits $sites sites for $hits non-boundary nodes (runs sharing a paragraph)" >&2 fi total_sites=$((total_sites + sites))done <"$changed"test "$total_sites" -gt 0 || die "no paragraph-run sites found in the changed manuscripts"# And nowhere else: an unchanged manuscript must contain none.while read -r name; do # Fixed-string, whole-line: a manuscript name contains dots, and a regex # match would let `minimal.sgd` be satisfied by a different name. if ! grep -Fxq -- "$name" "$changed"; then test "$( { rg -o 'author-paragraph-run' "$split_dir/$name" || true; } | wc -l)" -eq 0 \ || die "$name is byte-identical yet contains a paragraph-run site" fidone <"$gate_dir/common-names"pass 4 "$total_sites paragraph-run sites, one per non-boundary node, none anywhere else"# RETIRED 2026-08-22, same disposition and same reasoning as arm 3 above, of# which it was a dependent rather than an independent check.## WHAT IT DID. It walked arm 3's CHANGED set and required each of those# manuscripts to emit between one and `hits` `author-paragraph-run` sites, where# `hits` came from the boundary census -- and required every UNCHANGED manuscript# to emit none. It was green on `0b7c5ca` alongside arm 3:## SD_P3_GATE_04 6 paragraph-run sites, one per non-boundary node,# none anywhere else## WHY IT CANNOT BE KEPT WHILE ARM 3 GOES. Its input IS arm 3's changed set. With# the whole corpus's hashes moved by the header migration, that set becomes every# manuscript, and the first one without a census row kills the arm at# `die "no census row for $name"`. Keeping it would not preserve its meaning; it# would only convert a retirement into a red whose message named the wrong# thing.## WHAT COVERS THE PROPERTY NOW. The property itself -- that SD-P3's# paragraph-run lowering is confined to non-boundary nodes -- is asserted# structurally by `test/test-prose-runs.sgl` (arm 1 of this gate, whose count is# derived from that suite) and by arm 2's node-by-node equality between the bare# and wrapped spellings, neither of which depends on a pre-SD-P3 hash table.# `test/gate-sd-p8.sh` arm 3 additionally shows that the D8 migration moved# NOTHING in the lowering of the manuscripts SD-P3 cared about, header aside,# which is the closest standing equivalent of what this arm certified.pass 4 "RETIRED 2026-08-22 at SD-P8 with arm 3, whose changed-set was its only input; property covered by arm 1, arm 2 and test/gate-sd-p8.sh arm 3 (see comment)"# ---------------------------------------------------------------- ARM 05# Regression: the suites that own the frozen behaviour, with their countstest/gate-sd-p8.shadded
#!/usr/bin/env bash# SD-P8 gate: ruling D8 -- generic base language, genres as languages.## Every arm prints its own marker and this script asserts on MARKERS, not on# exit status alone: a non-zero exit is equally consistent with a gate that# never ran (127 not-found, 126 not-executable), and this estate has been fooled# by both.## Counts checked for EQUALITY are DERIVED from an artifact rather than written# here. The exceptions are stated where they occur and are FLOORS, not# restatements of a value.## ARM 3 IS A DIFFERENTIAL AND IS SCOPED TO ITS OWN BASE. It generates BOTH sides# in one run -- a detached worktree at $base_sha, and this working tree -- so it# has no committed baseline to go stale, and it fails with "baseline superseded"# rather than a bare diff if its base is no longer an ancestor of HEAD. It is# the replacement `test/gate-sd-p3.sh` arms 3 and 4 name in their retirement# notices, built to the standing pattern Fable's 2026-08-11 ruling recorded for# the next lowering-changing phase.### A COINCIDENCE THIS PHASE LEANED ON, WRITTEN DOWN BECAUSE IT IS NOT DESIGN.# `@article` and `@chapter` are both 8 characters, and `article`/`chapter` both# 7. That is luck. It is what let the migration switch roots in# `test/test-reader.sgl`, `test/test-prose-runs.sgl` and# `test/sd-p1-classification-probe.sgl` with every location assertion, and the# three pinned positive controls in `test/gate-sd-p1.sh` arm 2, staying BYTE# IDENTICAL. Those controls are therefore LENGTH-SENSITIVE and nothing in them# says so: the next root rename, if it is not 8 characters, will redden that arm# for a reason that has nothing to do with what it checks. If you are here# because of that, this is your explanation.# THE ARMS ARE ORDERED CHEAPEST FIRST, deliberately. Arms 5 and 6 dominate the# runtime (a detached worktree plus two full lowering dumps, and the whole test# suite), and `set -e` stops at the first failure, so a sabotage leg aimed at an# early arm pays for none of that. That is not a convenience: a sabotage suite# nobody can afford to run is a sabotage suite nobody runs, and a gate that has# never been shown able to fail is a gate that has been shown to be quiet.set -euo pipefailrepo_dir="$(cd "$(dirname "$0")/.." && pwd)"cd "$repo_dir"mkdir -p "$repo_dir/build"# PROVENANCE. This differential's meaning is bound to this commit: it is the# tree D8 was executed against. If HEAD ever stops descending from it, the arm# says so in those words instead of printing a diff nobody can interpret.base_sha="${SD_P8_BASE_SHA:-0b7c5ca}"sigil="${SIGIL_D1_COMPILER:-$repo_dir/../task-sigil-language-readers/build/dev/bin/sigil}"redirects="${SIGIL_D1_REDIRECTS:-$repo_dir/dev-redirects.sgl}"gate_dir=$(mktemp -d "$repo_dir/build/sd-p8-gate.XXXXXX")export SIGIL_REDIRECTS="$redirects"export XDG_CACHE_HOME="$gate_dir/cache"pass() { printf 'SD_P8_GATE_%02d %s\n' "$1" "$2"; }die() { echo "$*" >&2; exit 1; }# An absent tool does not announce itself -- it impersonates a result. Every# external tool this gate leans on must EXIST before any of them is trusted.for tool in rg git awk sed sha256sum comm sort diff cmp setsid; do command -v "$tool" >/dev/null || die "$tool is required by this gate"donetest -x "$sigil" || die "dev compiler not found at $sigil"test -f src/sigil/doc/private/parser.sgltest -f src/sigil/doc/book/reader.sgl || die "the D8 genre language is missing"test -f test/test-root-profile.sgltest -f test/sd-p8-lowering-dump.sgltest -f test/sd-p8-language-probe.sgl# The gate's INPUT SET, recorded rather than assumed: a caret-ranged dependency# is an unrecorded input, and so is an unnamed compiler."$sigil" --versionecho " compiler: $sigil"echo " redirects: $redirects"echo " base_sha: $base_sha"echo " head: $(git rev-parse HEAD)"sxml_root="${SIGIL_SXML_ROOT:-$repo_dir/../sigil-sxml}"test -d "$sxml_root/src" || die "sigil-sxml source not found at $sxml_root/src"# ---------------------------------------------------------------- ARM 01# The two languages are what D8 ruled, asked through the PUBLIC modules only.## A delegating language must never need a private module, and the genre this# phase ships is the first proof of that inside this package rather than in a# test-only stand-in.probe_log="$gate_dir/profiles.log"cat >"$gate_dir/profile-probe.sgl" <<'PROBE'(import (sigil core) (sigil doc reader) (sigil doc book reader))(define (show label profile) (display label) (display " name=") (write (document-profile-name profile)) (display " roots=") (write (document-profile-roots profile)) (display " mult=") (write (document-profile-multiplicity profile)) (newline))(show "SD_P8_PROFILE base" sigil-doc-profile)(show "SD_P8_PROFILE book" sigil-doc-book-profile)PROBEsetsid -w "$sigil" -L src -L "$gate_dir" eval -f "$gate_dir/profile-probe.sgl" \ >"$probe_log" 2>&1 || { cat "$probe_log" >&2; die "arm 01: the profile probe did not run"; }rg -q 'SD_P8_PROFILE base name="sigil/doc" roots=\(article\) mult=one' "$probe_log" \ || { cat "$probe_log" >&2; die "arm 01: the base profile is not D8's"; }rg -q 'SD_P8_PROFILE book name="sigil/doc/book" roots=\(chapter\) mult=one' "$probe_log" \ || { cat "$probe_log" >&2; die "arm 01: the genre profile is not D8's"; }pass 1 "both shipped profiles are D8's, reached through public modules alone"# ---------------------------------------------------------------- ARM 02# The dropped kind, checked as a CLASS rather than as a spelling.## A refusal by name, a positive control that the two surviving kinds still# construct, and -- because a ruling that creates a rule must be checked against# the POPULATION and not only against the diff -- a sweep of every# `make-document` call site in the tree.kind_log="$gate_dir/kind.log"cat >"$gate_dir/kind-probe.sgl" <<'PROBE'(import (sigil core) (sigil syntax) (sigil doc))(define loc (make-srcloc "k.sgd" 1 1 0 1))(define (body) (list (make-paragraph (list (make-text "b" loc)) loc)));; The VALUE on success, not a marker: `(guard ... (thunk) "NO-RAISE")` discards;; what the thunk returned and reports success identically for every kind, which;; would make the two positive controls below unable to tell article from page.(define (outcome thunk) (guard (exn (else (exception-message exn))) (thunk)))(define (try kind) (display "SD_P8_KIND ") (display kind) (display " :: ") (write (outcome (lambda () (symbol->string (document-kind (make-document kind "T" (list "A") (body) loc)))))) (newline))(try 'article)(try 'chapter)(try 'book)(try 'page)PROBEsetsid -w "$sigil" -L src -L "$gate_dir" eval -f "$gate_dir/kind-probe.sgl" \ >"$kind_log" 2>&1 || { cat "$kind_log" >&2; die "arm 02: the kind probe did not run"; }rg -q '^SD_P8_KIND book :: "make-document: kind must be article or chapter"$' "$kind_log" \ || { cat "$kind_log" >&2; die "arm 02: make-document did not refuse the dropped kind by name"; }rg -q '^SD_P8_KIND page :: "make-document: kind must be article or chapter"$' "$kind_log" \ || { cat "$kind_log" >&2; die "arm 02: make-document did not refuse an unrelated kind, so the enum is not closed"; }# POSITIVE CONTROLS: a constructor that refused everything satisfies both above.rg -q '^SD_P8_KIND article :: "article"$' "$kind_log" \ || { cat "$kind_log" >&2; die "arm 02: article is no longer constructible -- folio's publication lowering depends on it"; }rg -q '^SD_P8_KIND chapter :: "chapter"$' "$kind_log" \ || { cat "$kind_log" >&2; die "arm 02: chapter is no longer constructible"; }# THE POPULATION, not the diff. Every `make-document` call site in shipped source# and in the test tree, classified; any that names `'book` is a finding.sites="$gate_dir/make-document-sites.txt"{ rg -n --no-heading "\(make-document " src test tools book || true; } >"$sites"site_count=$(wc -l <"$sites")test "$site_count" -ge 1 || die "arm 02: found no make-document call sites at all; the sweep examined nothing"book_sites=$( { rg -c "\(make-document '?book" "$sites" || true; } )test "${book_sites:-0}" -eq 0 \ || { rg -n "\(make-document '?book" "$sites" >&2; die "arm 02: a call site still constructs the dropped kind"; }pass 2 "kind book refused by name with 2 positive controls; $site_count make-document sites swept, 0 construct it"# ---------------------------------------------------------------- ARM 03# (ARM 05 below carries the differential; arms 2 and 3 are the cheap ones and# run first -- see the ordering note in this file's header.)# THE CORPUS CENSUS, with its denominator and an explicit not-checked class.## Every `.sgd` in the tree is classified by (declared language, root command) and# every one must land in a combination its own language admits. "No bad# manuscripts found" and "every manuscript was examined" are different claims;# this arm prints one row per manuscript so the output length is the second one.census="$gate_dir/census.txt": >"$census"total=0; base_lang=0; book_lang=0; other_lang=0; negative=0; bad=0while IFS= read -r m; do total=$((total + 1)) lang=$(head -1 "$m" | sed -n 's|^#lang \(.*\)$|\1|p') root=$( { rg -o -m1 '^@[a-z][a-z-]*' "$m" || echo "@NONE"; } | head -1) verdict=UNCHECKED case "$m" in */sd-p8-base-*|*/sd-p8-book-article-root.sgd|*/sd-p8-book-two-chapters.sgd) verdict=NEGATIVE-FIXTURE; negative=$((negative + 1)) ;; *) case "$lang::$root" in "sigil/doc::@article") verdict=OK; base_lang=$((base_lang + 1)) ;; "sigil/doc/book::@chapter") verdict=OK; book_lang=$((book_lang + 1)) ;; "sigil/doc::@note") verdict=OK-EXPLICIT-PROFILE; other_lang=$((other_lang + 1)) ;; "sd-p7/folio::@note") verdict=OK-OTHER-LANGUAGE; other_lang=$((other_lang + 1)) ;; *) verdict=ILLEGAL; bad=$((bad + 1)) ;; esac ;; esac printf '%-22s %-16s %-9s %s\n' "$verdict" "$lang" "$root" "$m" >>"$census"done < <(find book test -name '*.sgd' | sort)cat "$census"rows=$(wc -l <"$census")test "$rows" -eq "$total" || die "arm 03: examined $total manuscripts but printed $rows rows"test "$total" -ge 27 || die "arm 03: the census found only $total manuscripts; the population is wrong"test "$bad" -eq 0 || { rg '^ILLEGAL' "$census" >&2; die "arm 03: $bad manuscripts declare a language that does not admit their root"; }test "$( { rg -c '^UNCHECKED' "$census" || true; } )" = "" -o "$( { rg -c '^UNCHECKED' "$census" || echo 0; } )" -eq 0 \ || { rg '^UNCHECKED' "$census" >&2; die "arm 03: some manuscripts were not classified at all"; }test "$base_lang" -ge 1 || die "arm 03: no manuscript exercises the base language"test "$book_lang" -ge 15 || die "arm 03: only $book_lang manuscripts moved to the genre language"pass 3 "census $total manuscripts, 0 illegal, 0 unclassified :: $base_lang base, $book_lang book, $other_lang other-language, $negative negative fixtures"# ---------------------------------------------------------------- ARM 04# END TO END through the real `#lang` path: the two REQUIRED GREENS and the four# REQUIRED REDS, each named, plus the reader-level diagnostic every refusal# carries. See the probe's header for why the diagnostic has to be re-derived at# the reader (the compiler's `#lang` loader drops it -- measured, pre-existing).lang_log="$gate_dir/language.log"setsid -w "$sigil" -L src -L test eval -f test/sd-p8-language-probe.sgl \ >"$lang_log" 2>&1 || { cat "$lang_log" >&2; die "arm 04: the language probe did not run"; }# THE DENOMINATOR, derived from the probe's source and cross-checked against the# lines it printed. A probe whose cases were deleted still satisfies every# individual assertion below; only the count can see that.declared=$(rg -c '^ \(list "' test/sd-p8-language-probe.sgl)printed=$(rg -c '^SD_P8_LANG ' "$lang_log" || echo 0)test "$declared" -ge 6 || die "arm 04: the probe declares only $declared cases"test "$printed" -eq "$declared" \ || die "arm 04: the probe declares $declared cases but printed $printed"rg -q "SD_P8_LANGUAGE_PROBE_COMPLETE cases $declared matched $declared mismatched 0" "$lang_log" \ || { rg -n 'MISMATCH|SD_P8_LANGUAGE_PROBE_COMPLETE' "$lang_log" >&2 die "arm 04: not every case matched its expected verdict"; }# Each REQUIRED RED by name, with the diagnostic that must accompany it. The# language NAME in each message is the load-bearing part: a genre language# reporting `sigil/doc:` would send an author to the wrong documentation.rg -q '^SD_P8_DIAG base-refuses-chapter :: "sigil/doc: top-level author forms must precede exactly one @article"$' "$lang_log" \ || die "arm 04: the base language did not refuse @chapter with its own diagnostic"rg -q '^SD_P8_DIAG base-refuses-two :: "sigil/doc: top-level author forms must precede exactly one @article"$' "$lang_log" \ || die "arm 04: the base language did not refuse a second @article"rg -q '^SD_P8_DIAG book-refuses-article :: "sigil/doc/book: top-level author forms must precede exactly one @chapter"$' "$lang_log" \ || die "arm 04: the genre language did not refuse @article naming sigil/doc/book"rg -q '^SD_P8_DIAG book-refuses-two :: "sigil/doc/book: top-level author forms must precede exactly one @chapter"$' "$lang_log" \ || die "arm 04: the genre language did not refuse a second @chapter naming sigil/doc/book"# THE POSITIVE CONTROLS. Four refusals are satisfied by a reader that refuses# everything; these two are not.rg -q '^SD_P8_LANG base-accepts-article want=accept got=accept MATCH kind-or-error="article"$' "$lang_log" \ || die "arm 04: #lang sigil/doc did not accept its own root"rg -q '^SD_P8_LANG book-accepts-chapter want=accept got=accept MATCH kind-or-error="chapter"$' "$lang_log" \ || die "arm 04: #lang sigil/doc/book did not accept its own root"rg -q '^SD_P8_DIAG base-accepts-article :: "NO-RAISE"$' "$lang_log" \ || die "arm 04: the base reader raised on the manuscript it must accept"rg -q '^SD_P8_DIAG book-accepts-chapter :: "NO-RAISE"$' "$lang_log" \ || die "arm 04: the genre reader raised on the manuscript it must accept"# THE `#lang`-LAYER MESSAGE, and why this is a two-branch assertion rather than# one.## Until sigil 0.19.1 the compiler's language loader reported a reader's refusal# as `read-language-source failed for #lang <x>:` with NOTHING after the colon:# the error path read a buffer only C-raised errors populate, and readers are# written in Sigil. sigil commit 9a523988 ("fix(compiler): carry the reader's# own diagnostic into the #lang error") fixed it in v0.19.1. Measured 2026-08-22# against ONE tree with only the compiler varied:## 0.19.0 (b99b8f2d): ... for #lang sigil/doc:# 0.19.1 (3d0668a0): ... for #lang sigil/doc: error: sigil/doc: top-level# author forms must precede exactly one @chapter## This gate runs on the estate's PINNED dev compiler, which every other gate in# this repo shares, and moving it here alone would produce a measurement nobody# can compare against the others. So the expectation is chosen from the version# this gate already records -- and BOTH branches assert. A single "if it carries# text, the text must be right" would be vacuous on the older compiler, which is# precisely the shape that lets a gate pass for free.## An unparseable version FAILS rather than falling through to the older branch:# taking the weaker expectation because a `sed` did not match is the same# vacuous green wearing a different hat.checked_layer=0version_line=$("$sigil" --version 2>&1 | sed 's/\x1b\[[0-9;]*m//g')compiler_version=$(printf '%s' "$version_line" | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1)test -n "$compiler_version" \ || 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"as_number() { echo "$1" | awk -F. '{printf "%d%03d%03d", $1, $2, $3}'; }carries_floor=0.19.1if [ "$(as_number "$compiler_version")" -ge "$(as_number "$carries_floor")" ]; then lang_layer="carries"else lang_layer="empty"fiecho " #lang layer: compiler $compiler_version -> expecting the message to be '$lang_layer' (floor $carries_floor)"# One row per refusal case: language, and the diagnostic that language must# produce. The loop's length is the denominator -- a case dropped from it stops# being checked silently otherwise.while read -r case_name diag; do test -n "$case_name" || continue if [ "$lang_layer" = "carries" ]; then rg -q "^SD_P8_LANG $case_name .* kind-or-error=\".*: error: $diag\"\$" "$lang_log" \ || { rg -n "^SD_P8_LANG $case_name " "$lang_log" >&2 die "arm 04: on sigil $compiler_version the #lang error must carry '$diag', and for $case_name it does not"; } else rg -q "^SD_P8_LANG $case_name .* kind-or-error=\"read-language-source failed for #lang [a-z/]+: \"\$" "$lang_log" \ || { rg -n "^SD_P8_LANG $case_name " "$lang_log" >&2 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"; } fi checked_layer=$((checked_layer + 1))done <<'ROWS'base-refuses-chapter sigil/doc: top-level author forms must precede exactly one @articlebase-refuses-two sigil/doc: top-level author forms must precede exactly one @articlebook-refuses-article sigil/doc/book: top-level author forms must precede exactly one @chapterbook-refuses-two sigil/doc/book: top-level author forms must precede exactly one @chapterROWStest "${checked_layer:-0}" -eq 4 \ || die "arm 04: the #lang-layer loop checked ${checked_layer:-0} cases, not 4"pass 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"# ---------------------------------------------------------------- ARM 05# THE D8 DIFFERENTIAL. Both sides generated in one run.## The claim: across the whole manuscript corpus, the ONLY manuscripts whose# lowering changed are the ones whose ROOT COMMAND this phase changed, and for# each of those the change is EXACTLY the root symbol. The nineteen manuscripts# that took a header edit alone lower identically.## What makes that assertable is `test/sd-p8-lowering-dump.sgl`, which emits every# manuscript position relative to that manuscript's own header end. Read its# header: the header delta reaches the lowered output in THREE places, and the# third (the generated binding name `#%sigil/doc-expression:<offset>`) is one# nobody would predict from reading the parser.git rev-parse --verify "$base_sha^{commit}" >/dev/null 2>&1 \ || 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"git merge-base --is-ancestor "$base_sha" HEAD \ || 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"base_tree="$gate_dir/base-tree"cleanup() { git -C "$repo_dir" worktree remove --force "$base_tree" >/dev/null 2>&1 || true; }# Unconditional, not merely reached: an early exit between `worktree add` and the# end of this script would otherwise leave a stray worktree in a SHARED .git.trap cleanup EXIT INT TERMgit worktree add --detach "$base_tree" "$base_sha" >/dev/null 2>&1 \ || die "arm 05: could not create a detached worktree at $base_sha"# THE SAME FILE ON BOTH SIDES. An instrument that differs between the two sides# of a differential measures itself. The base tree predates this script, so it is# copied in -- and its hash is asserted equal afterwards, because "I copied it"# and "the copy is the same file" are different claims.cp test/sd-p8-lowering-dump.sgl "$base_tree/test/"here_hash=$(sha256sum test/sd-p8-lowering-dump.sgl | awk '{print $1}')there_hash=$(sha256sum "$base_tree/test/sd-p8-lowering-dump.sgl" | awk '{print $1}')test "$here_hash" = "$there_hash" || die "arm 05: the two sides are not running the same dump script"# `dev-redirects.sgl` resolves `../sigil` relative to the package root and the# base worktree is not a sibling of that checkout, so an absolute one is written.sigil_repo="$(cd "$repo_dir/../sigil" && pwd)"sxml_repo="$(cd "$sxml_root" && pwd)"cat >"$gate_dir/base-redirects.sgl" <<EOF(redirects repos: (list (for-repo url: "codeberg:sigil/sigil" use: (from-path dir: "$sigil_repo")) (for-repo url: "codeberg:sigil/sigil-sxml" use: (from-path dir: "$sxml_repo"))))EOFafter_dump="$gate_dir/after.txt"before_dump="$gate_dir/before.txt"setsid -w "$sigil" -L src -L test eval -f test/sd-p8-lowering-dump.sgl >"$after_dump" 2>&1 || true( cd "$base_tree" && SIGIL_REDIRECTS="$gate_dir/base-redirects.sgl" \ XDG_CACHE_HOME="$gate_dir/base-cache" \ setsid -w "$sigil" -L src -L test eval -f test/sd-p8-lowering-dump.sgl ) >"$before_dump" 2>&1 || truefor side in after before; do f="$gate_dir/$side.txt" rg -q '^SD_P8_LOWERING_DUMP_COMPLETE ' "$f" \ || { tail -25 "$f" >&2; die "arm 05: the $side dump printed no completion marker"; }done# One directory of sections per side. The completion marker is EXCLUDED: it names# the corpus size, so leaving it in would tie the LAST manuscript's section to how# many manuscripts were dumped -- a difference with nothing to do with lowering.# The MANIFEST/SKIPPED lines are excluded too: they carry the language id, the# header length and the root command, which is exactly what legitimately differs.split() { # $1 = dump, $2 = out dir mkdir -p "$2" awk -v o="$2" '/^SD_P8_/{next} /^--- /{name="";next} /^=== /{name=$2} name!=""{print > (o "/" name)}' "$1"}split "$after_dump" "$gate_dir/sec-after"split "$before_dump" "$gate_dir/sec-before"# The SECOND, INDEPENDENT INSTRUMENT. The root command of each manuscript is read# as TEXT by the dump's own scanner and printed on the MANIFEST line, so the set# of manuscripts that MUST differ is derived without asking the lowering -- the# thing under test -- what it thinks a root was.manifest() { # $1 = dump -> "name root" rg -o '^--- MANIFEST \S+ lang=\S+ header=\d+ root=\S+' "$1" \ | awk '{r=$NF; sub(/^root=/,"",r); print $3, r}' | sort}manifest "$after_dump" >"$gate_dir/roots-after"manifest "$before_dump" >"$gate_dir/roots-before"ledger="$gate_dir/ledger.txt": >"$ledger"identical=0; expected_change=0; unexpected=0; only_after=0; only_before=0; compared=0# ONE LINE PER MANUSCRIPT, not one line per failure: the output length is the# denominator, and a manuscript that quietly stopped being examined is visible# without anyone counting.for f in "$gate_dir/sec-after"/*; do name="${f##*/}" compared=$((compared + 1)) b="$gate_dir/sec-before/$name" root_a=$(awk -v n="$name" '$1==n{print $2}' "$gate_dir/roots-after") root_b=$(awk -v n="$name" '$1==n{print $2}' "$gate_dir/roots-before") if [ ! -f "$b" ]; then only_after=$((only_after + 1)) printf 'AFTER-ONLY %-40s root=%s (the base tree could not lower it)\n' "$name" "$root_a" >>"$ledger" continue fi if cmp -s "$f" "$b"; then identical=$((identical + 1)) printf 'IDENTICAL %-40s root=%s\n' "$name" "$root_a" >>"$ledger" test "$root_a" = "$root_b" \ || die "arm 05: $name lowers identically yet its root moved $root_b -> $root_a; the normalization is hiding a real difference" continue fi if [ "$root_a" = "$root_b" ]; then unexpected=$((unexpected + 1)) printf 'UNEXPECTED %-40s root=%s unchanged, lowering differs\n' "$name" "$root_a" >>"$ledger" continue fi # A root-changed manuscript must differ by EXACTLY the root symbol. Substituting # the old symbol for the new one in the BEFORE section must reproduce the AFTER # section byte for byte -- a far stronger statement than "it changed, as # predicted", and the one that would catch a migration that also moved content. old="${root_b#@}"; new="${root_a#@}" sed "s/\]\] $old>/]] $new>/g" "$b" >"$gate_dir/rewritten-$name" if cmp -s "$f" "$gate_dir/rewritten-$name"; then expected_change=$((expected_change + 1)) printf 'ROOT-ONLY %-40s root=%s -> %s, nothing else moved\n' "$name" "$root_b" "$root_a" >>"$ledger" else unexpected=$((unexpected + 1)) printf 'UNEXPECTED %-40s root=%s -> %s, and MORE than the root moved\n' "$name" "$root_b" "$root_a" >>"$ledger" fidonefor f in "$gate_dir/sec-before"/*; do name="${f##*/}" if [ ! -f "$gate_dir/sec-after/$name" ]; then only_before=$((only_before + 1)) printf 'BEFORE-ONLY %-40s (this tree can no longer lower it)\n' "$name" >>"$ledger" fidonecat "$ledger"# THE DENOMINATOR, stated against the dumps' own totals rather than against the# ledger the loop just wrote -- a loop cannot audit itself.after_found=$(rg -o 'SD_P8_LOWERING_DUMP_COMPLETE found \d+' "$after_dump" | awk '{print $NF}')after_compared=$(rg -o 'compared \d+' "$after_dump" | awk '{print $NF}')before_compared=$(rg -o 'compared \d+' "$before_dump" | awk '{print $NF}')test "$compared" -eq "$after_compared" \ || die "arm 05: the dump lowered $after_compared manuscripts but the ledger holds $compared"echo " denominator: after found $after_found, lowered $after_compared; before lowered $before_compared; ledger rows $((compared + only_before))"# A FLOOR, deliberately, not a restatement: adding a manuscript must not# desynchronise this, and a corpus that collapsed must not read as a pass.test "$identical" -ge 15 \ || die "arm 05: only $identical manuscripts lower identically; the migration moved more than the header"test "$expected_change" -ge 1 \ || die "arm 05: no manuscript changed its root, so the root-only comparison never ran and proves nothing"test "$unexpected" -eq 0 \ || { rg '^UNEXPECTED' "$ledger" >&2; die "arm 05: $unexpected manuscripts changed for a reason this phase did not intend"; }test "$only_before" -eq 0 \ || { rg '^BEFORE-ONLY' "$ledger" >&2; die "arm 05: this tree stopped being able to lower a manuscript the base tree could"; }pass 5 "differential vs $base_sha: $identical identical, $expected_change root-only, $only_after newly lowerable, 0 unexpected, over $compared lowered of $after_found found"# ---------------------------------------------------------------- ARM 06# REGRESSION. The full suite with the FULL load path -- four entries, because a# short path degrades into "failed to load", which the runner reports WITHOUT# failing the process. The file count is its own assertion for that reason.## `SD_P8_SKIP_REGRESSION` EXISTS FOR ONE CALLER: `test/sabotage-sd-p8.sh`, whose# legs would otherwise pay for this suite seven times over to reach a red in an# earlier arm. A skipped arm must never read as a passing arm, so it prints# SKIPPED rather than a pass line, and the script ends at SD_P8_GATE_PARTIAL# instead of SD_P8_GATE_GREEN. The sabotage suite's own positive control runs# the gate with this UNSET and requires the GREEN marker, so no leg can be# satisfied by a run that skipped anything.if [ -n "${SD_P8_SKIP_REGRESSION:-}" ]; then printf 'SD_P8_GATE_06 SKIPPED (SD_P8_SKIP_REGRESSION is set)\n' echo "SD_P8_GATE_PARTIAL arms 1-5 only; the regression arm did not run" exit 0firegression_log="$gate_dir/regression.log"regression_status=0setsid -w "$sigil" -L "$sxml_root/src" -L src -L test -L book test \ >"$regression_log" 2>&1 || regression_status=$?sed -i 's/\x1b\[[0-9;]*m//g' "$regression_log"grep -qE 'Tests: +[0-9]+ passed' "$regression_log" \ || { tail -40 "$regression_log" >&2; die "arm 06: could not read a suite summary -- the run may not have executed anything"; }files_line=$(grep -oE 'Files: +[0-9]+ of [0-9]+ run' "$regression_log" | tail -1 || true)test -n "$files_line" || { tail -40 "$regression_log" >&2; die "arm 06: the runner reported no file count"; }files_run=$(echo "$files_line" | grep -oE '[0-9]+' | head -1)files_found=$(echo "$files_line" | grep -oE '[0-9]+' | tail -1)test "$files_run" = "$files_found" || die "arm 06: only $files_run of $files_found test files ran"if grep -qE '[0-9]+ failed' "$regression_log"; then grep -nE '✗|failed' "$regression_log" | head -30 >&2 die "arm 06: a test failed"fitest "$regression_status" = "0" \ || { tail -40 "$regression_log" >&2; die "arm 06: the runner exited $regression_status with no failing test reported"; }# The root-profile suite's count DERIVED from its own source, and the two arms# this phase added named individually: a suite whose arms were deleted still# reports "all tests passed".expected_arms=$(rg -c '^\(test "' test/test-root-profile.sgl)rg -q "test-root-profile" "$regression_log" || die "arm 06: the root-profile suite did not run"rg -q "ok +$expected_arms passed +\./test/test-root-profile.sgl" "$regression_log" \ || { rg -n 'test-root-profile' "$regression_log" >&2 die "arm 06: the root-profile suite did not report $expected_arms of its $expected_arms arms"; }rg -q 'the book genre language is a sibling profile with its own diagnostics' "$regression_log" \ || die "arm 06: the genre-language arm is not in the suite that ran"rg -q "the base profile is D8's policy and D8's diagnostics, exactly" "$regression_log" \ || die "arm 06: the base-profile arm is not in the suite that ran"summary=$(grep -E 'Tests: +[0-9]+ passed' "$regression_log" | tail -1 | tr -s ' ')Showing the first 500 of 503 diff lines for this file. This diff is INCOMPLETE; read the file or clone the repository for the rest.
test/sabotage-sd-p8.shadded
#!/usr/bin/env bash# SD-P8 sabotage: prove `test/gate-sd-p8.sh` and the pinned literals it guards## WHY EVERY LEG HERE IS NEEDED, stated rather than assumed.## SD-P8 rewrote pinned diagnostic literals -- `@chapter` became `@article` in# two shipped messages and a third language's name appeared. A migration that# updates a pinned literal to match the new output proves NOTHING: the pin and# the product moved together and neither was ever tested against the other. So# every literal this phase changed has a leg below that perturbs the PRODUCT --# the profile, the derivation, the multiplicity -- and requires the pin to# redden. No leg edits an assertion to make it fail.## The one exception is deliberate and labelled: LEG 06 sabotages the# INSTRUMENT (the differential's normalization), because an instrument carries# the same defect rate as the product and gets none of the scrutiny.## EVERY LEG RESTORES BY HASH. A leg that restored the source but left the tree# subtly different would poison every later leg, and the last leg -- the# positive control -- is what proves the tree came back.set -euo pipefailrepo_dir="$(cd "$(dirname "$0")/.." && pwd)"cd "$repo_dir"work=$(mktemp -d "${TMPDIR:-/tmp}/sd-p8-sabotage.XXXXXX")PARSER=src/sigil/doc/private/parser.sglBOOK=src/sigil/doc/book/reader.sglMODEL=src/sigil/doc/model.sglDUMP=test/sd-p8-lowering-dump.sglPROBE=test/sd-p8-language-probe.sglFIXTURE=test/fixtures/minimal.sgddie() { echo "$*" >&2; exit 1; }# The SAME compiler the gate uses, named rather than inherited from PATH: a# sabotage driven by a different binary than the gate measures two things at# once.compiler="${SIGIL_D1_COMPILER:-$repo_dir/../task-sigil-language-readers/build/dev/bin/sigil}"sxml_root="${SIGIL_SXML_ROOT:-$repo_dir/../sigil-sxml}"for tool in rg sed awk sha256sum cmp git; do command -v "$tool" >/dev/null || die "$tool is required by this sabotage script"donetest -x "$compiler" || die "dev compiler not found at $compiler"test -d "$sxml_root/src" || die "sigil-sxml source not found at $sxml_root/src"# ------------------------------------------------------------- plant/restore## RESTORE FROM A SAVED COPY, NOT FROM `git checkout`.## The first version of this script restored with# `git checkout -- $PARSER $BOOK ... $DUMP $PROBE $FIXTURE`, and three of those# files were UNTRACKED at the time. `git checkout` refuses the whole invocation# on an unknown pathspec, so it restored NOTHING and said so on stderr while the# script carried on. The sabotaged parser survived leg 1, and the next probe run# in that worktree reported a diagnostic naming `@chapter` on a tree whose# source says `@article` -- a result that looks exactly like a stale build cache# and is not one. Found 2026-08-22, and it is the reason this block is longer# than it looks like it needs to be:## * a copy under $work is authoritative, so tracked and untracked files# restore the same way and a file's git status cannot change the outcome;# * every restore is verified BY HASH against that copy, per file, and a# mismatch is fatal rather than a warning;# * the tree is required CLEAN of these files before anything is planted, so# the copies are known-good.## A sabotage script that cannot restore is worse than no sabotage script: it# leaves a poisoned worktree behind and every later measurement in it is a lie.for f in "$PARSER" "$BOOK" "$MODEL" "$DUMP" "$PROBE" "$FIXTURE"; do test -f "$f" || die "$f does not exist; refusing to sabotage a tree that is not the one this script was written for" if git ls-files --error-unmatch "$f" >/dev/null 2>&1; then git diff --quiet -- "$f" \ || die "$f has uncommitted changes; refusing to sabotage over them" fi cp "$f" "$work/copy-$(echo "$f" | tr / _)" sha256sum "$f" | awk '{print $1}' >"$work/pristine-$(echo "$f" | tr / _)"donepristine_hash() { cat "$work/pristine-$(echo "$1" | tr / _)"; }pristine_copy() { echo "$work/copy-$(echo "$1" | tr / _)"; }# RETURNS non-zero rather than calling `die`. `die` ends in `exit`, and an# `exit` inside a function still ends the SHELL -- so an `if ! restore_all`# guard around a dying function can never take its own failure branch. That is# the same class of mistake as the `git checkout` one above: a recovery path# that cannot run.restore_all() { local f h failed=0 for f in "$PARSER" "$BOOK" "$MODEL" "$DUMP" "$PROBE" "$FIXTURE"; do cp "$(pristine_copy "$f")" "$f" || { echo "restore: could not copy back $f" >&2; failed=1; continue; } h=$(sha256sum "$f" | awk '{print $1}') if [ "$h" != "$(pristine_hash "$f")" ]; then echo "restore: $f came back as $h, not $(pristine_hash "$f")" >&2 failed=1 fi done return $failed}# THE TRAP IS LOUD. `restore_all || true` would hide exactly the failure that# motivated the block above: a restore that did not happen, followed by a quiet# exit and a poisoned worktree. If the restore fails here there is nothing left# to do but say so as clearly as possible, and NOT delete the copies that are# the only way back.on_exit() { local rc=$? if ! restore_all; then echo "!!! THE WORKTREE IS STILL SABOTAGED AND COULD NOT BE RESTORED." >&2 echo "!!! Pristine copies are kept at $work -- do NOT delete it." >&2 echo "!!! Restore by hand and verify by hash before trusting anything measured here." >&2 exit 2 fi rm -rf "$work" exit $rc}trap on_exit EXITtrap 'exit 130' INT TERM# ------------------------------------------------------------------ runners## The gate's own exit status is not the evidence -- its MESSAGE is. A leg that# only checked for a non-zero exit would score a gate that died in `mktemp` as# a successful sabotage.run_gate() { # $1 = log set +e SD_P8_SKIP_REGRESSION=1 bash test/gate-sd-p8.sh >"$1" 2>&1 local rc=$? set -e sed -i 's/\x1b\[[0-9;]*m//g' "$1" return $rc}# A COLD CACHE PER RUN, and it is not hygiene theatre. The shared# `~/.cache/sigil` serves previously-compiled modules, so a sabotage that edits# a source file can be defeated by the cache and report a convincing# red-that-wasn't -- or, worse here, a green-that-should-have-been-red. The# isolation is what makes each leg a measurement of the edit.run_suite() { # $1 = log set +e XDG_CACHE_HOME="$work/cache-$(basename "$1")" \ "$compiler" -L "$sxml_root/src" -L src -L test -L book \ test test/test-root-profile.sgl >"$1" 2>&1 local rc=$? set -e sed -i 's/\x1b\[[0-9;]*m//g' "$1" return $rc}leg_red() { # $1 = leg number, $2 = log, $3 = required substring rg -qF -- "$3" "$2" || { echo "LEG $1: expected a red naming '$3'; got:" >&2 tail -25 "$2" >&2 exit 1; } printf 'SD_P8_SABOTAGE_%02d RED %s\n' "$1" "$3"}# ---------------------------------------------------------------- LEG 01# PRODUCT. Put the base root set back to `(chapter)`.## This is the single edit D8 is: if the pinned literals in# `test/test-root-profile.sgl` cannot see it, they are decoration.echo "=== LEG 01 -- the base profile reverts to (chapter)"plant "$PARSER" \ '(make-document-profile "sigil/doc" .(article) .one)' \ "(make-document-profile \"sigil/doc\" '(chapter) 'one)"run_suite "$work/leg01.log" && die "LEG 01: the suite passed with the base root set reverted"leg_red 01 "$work/leg01.log" "the base profile is D8's policy and D8's diagnostics, exactly"rg -qF 'exactly one @chapter' "$work/leg01.log" \ || die "LEG 01: the suite reddened, but not on the derived diagnostic; the pin may be matching something else"restore_all || die "restore failed; the worktree is still sabotaged"# ---------------------------------------------------------------- LEG 02# PRODUCT. Give the genre language the BASE language's name.## The `sigil/doc/book:` prefix is the part of the genre's diagnostic that sends# an author to the right documentation, and it is derived from one string in one# place. This is the edit that would silently undo it.echo "=== LEG 02 -- the genre profile reports the base language's name"plant "$BOOK" \ '(make-document-profile "sigil/doc/book" .(chapter) .one)' \ "(make-document-profile \"sigil/doc\" '(chapter) 'one)"run_gate "$work/leg02.log" && die "LEG 02: the gate passed with the genre language misnamed"leg_red 02 "$work/leg02.log" "arm 04: the genre language did not refuse @article naming sigil/doc/book"restore_all || die "restore failed; the worktree is still sabotaged"# ---------------------------------------------------------------- LEG 03# PRODUCT. Widen the genre's multiplicity.## A REQUIRED RED becoming green is the failure mode a pass/fail count cannot# see: with `one-or-more`, the two-chapter manuscript is ACCEPTED and the gate# must say which case stopped being refused.echo "=== LEG 03 -- the genre accepts several chapters"plant "$BOOK" \ "'(chapter) 'one)" \ "'(chapter) 'one-or-more)"run_gate "$work/leg03.log" && die "LEG 03: the gate passed with the genre accepting two chapters"leg_red 03 "$work/leg03.log" "arm 04: not every case matched its expected verdict"rg -qF 'SD_P8_LANG book-refuses-two want=refuse got=accept MISMATCH' "$work/leg03.log" \ || die "LEG 03: the gate reddened, but not because the two-chapter manuscript was accepted"restore_all || die "restore failed; the worktree is still sabotaged"# ---------------------------------------------------------------- LEG 04# PRODUCT. Put the dropped kind back.echo "=== LEG 04 -- the model accepts kind book again"plant "$MODEL" \ "(memq kind '(article chapter))" \ "(memq kind '(article book chapter))"run_gate "$work/leg04.log" && die "LEG 04: the gate passed with kind book restored"leg_red 04 "$work/leg04.log" "arm 02: make-document did not refuse the dropped kind by name"restore_all || die "restore failed; the worktree is still sabotaged"# ---------------------------------------------------------------- LEG 05# PRODUCT, AND THE ONE THE DIFFERENTIAL EXISTS FOR.## Change a migrated manuscript in a way that is NOT a header shift. Arm 3 has so# far only ever seen the difference it expects; a differential that has only seen# the expected difference has not been shown to detect an unexpected one. The# edit is one word of prose in a manuscript whose header this phase moved, so it# is precisely the change the header normalization must NOT absorb.echo "=== LEG 05 -- a migrated manuscript gains a word of prose"plant "$FIXTURE" 'A small @em{useful} document' 'A small @em{useful} sabotaged document'run_gate "$work/leg05.log" && die "LEG 05: the gate passed with a migrated manuscript's content changed"leg_red 05 "$work/leg05.log" "manuscripts changed for a reason this phase did not intend"rg -qF 'UNEXPECTED minimal.sgd' "$work/leg05.log" \ || { rg -n 'UNEXPECTED|IDENTICAL minimal' "$work/leg05.log" >&2 die "LEG 05: arm 5 reddened, but did not name minimal.sgd as the manuscript that moved"; }restore_all || die "restore failed; the worktree is still sabotaged"# ---------------------------------------------------------------- LEG 06# INSTRUMENT. Remove the differential's `make-srcloc` normalization.## The normalization is what makes arm 3's equality claim possible, and an# instrument gets none of the scrutiny the product does. Without it, every# manuscript whose header moved differs in its embedded position DATA -- so the# gate must report a wall of UNEXPECTED rather than quietly passing.echo "=== LEG 06 -- the differential stops normalizing embedded positions"plant "$DUMP" \ '(and (pair? value)' \ '(and #f (pair? value)'run_gate "$work/leg06.log" && die "LEG 06: the gate passed with the position normalization disabled"leg_red 06 "$work/leg06.log" "manuscripts changed for a reason this phase did not intend"restore_all || die "restore failed; the worktree is still sabotaged"# ---------------------------------------------------------------- LEG 07# COVERAGE, not correctness. Delete cases from the language probe.## Every assertion in arm 4 is about an INDIVIDUAL case, so a probe whose cases# were deleted satisfies every one of them and prints `matched N mismatched 0`.# Only the denominator can see that, and this is the leg that proves the# denominator is real.echo "=== LEG 07 -- one of the probe's cases is deleted"plant "$PROBE" \ '(list "book-refuses-article"' \ ';; (list "book-refuses-article"'run_gate "$work/leg07.log" && die "LEG 07: the gate passed with a probe case deleted"leg_red 07 "$work/leg07.log" "arm 04: the probe declares only 5 cases"restore_all || die "restore failed; the worktree is still sabotaged"# ---------------------------------------------------------------- LEG 08# THE POSITIVE CONTROL, and it is not a formality.## Seven reds prove the gate can fire. They say nothing about whether it can# STAND DOWN -- a gate wedged red by leg 1's restore having gone wrong would# score every leg above as a success. This is also what proves the tree came# back: `restore_all` checks hashes, and this checks behaviour.echo "=== LEG 08 -- the restored tree is green"restore_all || die "restore failed; the worktree is still sabotaged"# THE FULL GATE, with SD_P8_SKIP_REGRESSION deliberately UNSET. Every leg above# ran with it set; if this control ran the same way, nothing here would ever have# executed the regression arm and "green" would be a claim about five arms# described as six.set +ebash test/gate-sd-p8.sh >"$work/leg08.log" 2>&1control_rc=$?set -esed -i 's/\x1b\[[0-9;]*m//g' "$work/leg08.log"test "$control_rc" -eq 0 || { tail -40 "$work/leg08.log" >&2 die "LEG 08: the restored tree is NOT green; every red above is uninterpretable"; }rg -q '^SD_P8_GATE_GREEN$' "$work/leg08.log" \ || { tail -20 "$work/leg08.log" >&2; die "LEG 08: the gate exited 0 without printing its green marker"; }rg -q 'SD_P8_GATE_PARTIAL' "$work/leg08.log" \ && die "LEG 08: the control run skipped an arm; it does not establish a full green"for n in 01 02 03 04 05 06; do rg -q "^SD_P8_GATE_$n " "$work/leg08.log" \ || die "LEG 08: arm $n printed no marker in the control run"doneprintf 'SD_P8_SABOTAGE_08 GREEN the restored tree passes all six arms\n'echo "SD_P8_SABOTAGE_COMPLETE 7 reds (5 product, 1 instrument, 1 coverage) and 1 positive control"test/sd-p1-classification-probe.sglmodified
;;; package root, and `test/gate-sd-p1.sh` asserts a positive control --;;; the pre-SD-P1 parser must NOT resolve `sigil-doc-profile` -- before it;;; believes any BEFORE transcript.;;; SD-P8 (2026-08-22) MOVED EVERY `@chapter` HERE TO `@article`, because ruling;;; D8 made `@article` the root `#lang sigil/doc` admits and this probe drives;;; the DEFAULT profile through `parse-document-source`.;;;;;; THE THREE POSITIVE CONTROLS BELOW STAYED BYTE-IDENTICAL TO THE PINNED;;; PRE-SD-P1 TRANSCRIPT, AND THAT IS LUCK, NOT DESIGN. `@article` and;;; `@chapter` are both 8 characters, so no byte offset in any case moved.;;; `test/gate-sd-p1.sh` arm 2 requires F, G and H to match;;; `test/transcripts/sd-p1/classification-before.txt` exactly, so that arm is;;; LENGTH-SENSITIVE to this file's root spelling and nothing in it says so. A;;; future root rename of a different length will redden it for a reason that;;; has nothing to do with the classification it checks. If you are reading this;;; because that happened: the controls are still correct, their OFFSETS moved.;;;(import (sigil core) (sigil doc) (sigil doc author) (sigil doc private author-runtime) (sigil doc private parser))test/sd-p8-language-probe.sgladded
;;; SD-P8 -- both shipped languages driven END TO END through the real `#lang`;;; path, plus the diagnostic each refusal carries at the reader.;;;;;; WHY BOTH HALVES ARE HERE, AND WHY NEITHER ALONE WOULD DO.;;;;;; The `#lang` half is the path an author takes: the compiler reads the header,;;; resolves `#lang a/b/c` to the module `(a b c reader)`, loads it and calls its;;; `read-language-source`. Driving parsed forms through runtime `eval` instead;;; would test the harness as much as the product -- SD-P1's headline finding;;; about keyword formals turned out to be exactly that instrument artifact. So;;; the accept/refuse verdicts below come from `load`.;;;;;; The reader half exists because of a MEASURED defect in the compiler, not out;;; of caution. A root-policy refusal reaching the `#lang` loader is reported as;;;;;; Error (syntax-error): read-language-source failed for #lang sigil/doc:;;;;;; with the underlying message EMPTY. The parser's diagnostic -- which names the;;; language and the root it wanted -- never reaches the author. Verified;;; pre-existing at the base SHA with a two-`@chapter` manuscript under the old;;; base profile, so it is not something D8 introduced; it is in the sigil;;; compiler's language-loading path, outside this package. It does mean that;;; "the refusal names what is wrong" CANNOT be asserted at the `#lang` layer;;; today, so each refusal is re-driven through;;; `parse-document-parts-with-profile` -- the same source, the same profile the;;; `#lang` line selects -- and the real diagnostic is printed for the gate to;;; assert on.;;;;;; Every case prints a line whether it passes or fails, and the completion;;; marker carries the count: the output length is the denominator, so a case;;; that silently stopped running cannot read as a case that passed.(import (sigil core) (sigil fs) (sigil language) (sigil doc) (sigil doc reader) (sigil doc book reader))(define (message thunk) (guard (exn (else (exception-message exn))) (thunk) "NO-RAISE"))(define (header-end-of source) (let loop ((i 0)) (cond ((>= i (string-length source)) i) ((char=? (string-ref source i) #\newline) (+ i 1)) (else (loop (+ i 1))))))(define (profile-for id) (cond ((string=? id "sigil/doc") sigil-doc-profile) ((string=? id "sigil/doc/book") sigil-doc-book-profile) (else #f)));; The `#lang` verdict, from `load`. `document` is bound into THIS module by a;; successful load, so an accepted manuscript can be asked for its kind.(define (lang-verdict path) (let ((raised (message (lambda () (load path))))) (if (string=? raised "NO-RAISE") (cons 'accept (symbol->string (document-kind (eval 'document)))) (cons 'refuse raised))));; The reader-level diagnostic for the SAME source under the SAME profile the;; `#lang` line names -- read out of the manuscript rather than passed in, so a;; case cannot be driven through a profile its header does not select.(define (reader-diagnostic path) (let* ((source (read-file-string path)) (id (language-header-id (inspect-language-source source path))) (profile (profile-for id))) (if (not profile) (string-append "NO-PROFILE-FOR " id) (message (lambda () (parse-document-parts-with-profile profile source path (header-end-of source)))))))(define cases (list ;; name path want want-kind (list "base-accepts-article" "test/fixtures/sd-p1-article-root.sgd" 'accept "article") (list "base-refuses-chapter" "test/fixtures/sd-p8-base-chapter-root.sgd" 'refuse "") (list "base-refuses-two" "test/fixtures/sd-p8-base-two-articles.sgd" 'refuse "") (list "book-accepts-chapter" "test/fixtures/sd-p8-book-chapter-root.sgd" 'accept "chapter") (list "book-refuses-article" "test/fixtures/sd-p8-book-article-root.sgd" 'refuse "") (list "book-refuses-two" "test/fixtures/sd-p8-book-two-chapters.sgd" 'refuse "")))(define ok 0)(define bad 0)(for-each (lambda (c) (let* ((name (car c)) (path (cadr c)) (want (caddr c)) (want-kind (cadddr c)) (verdict (lang-verdict path)) (got (car verdict)) (detail (cdr verdict)) (matched (and (eq? got want) (or (not (eq? want 'accept)) (string=? detail want-kind))))) (if matched (set! ok (+ ok 1)) (set! bad (+ bad 1))) (display "SD_P8_LANG ") (display name) (display " want=") (display want) (display " got=") (display got) (display (if matched " MATCH" " MISMATCH")) (display " kind-or-error=") (write detail) (newline) ;; The reader-level diagnostic, printed for EVERY case rather than only ;; the refusals: an accepted manuscript must produce `NO-RAISE` here, and ;; that is the positive control that stops the refusal assertions from ;; being satisfied by a reader that refuses everything. (display "SD_P8_DIAG ") (display name) (display " :: ") (write (reader-diagnostic path)) (newline))) cases)(display "SD_P8_LANGUAGE_PROBE_COMPLETE cases ")(display (length cases))(display " matched ") (display ok)(display " mismatched ") (display bad)(newline)test/sd-p8-lowering-dump.sgladded
;;; SD-P8 -- canonical dump of LOWERED output, normalized so that the `#lang`;;; header's LENGTH is invisible and nothing else is.;;;;;; WHY THIS EXISTS AND WHY IT IS NOT `test/sd-p3-lowering-dump.sgl`.;;;;;; SD-P3's dump is the AFTER half of a differential whose BEFORE half is a;;; committed table of sha256s taken from a pre-SD-P3 tree. D8 migrates the;;; `#lang` line of nineteen manuscripts, which moves every byte offset in each;;; of them by the header's length delta, so every one of those hashes changes.;;; The BEFORE half stores hashes and not sections, so no normalization can be;;; applied to it retroactively -- the differential cannot be rescued, only;;; retired. It is retired in `test/gate-sd-p3.sh` arms 3 and 4, under the;;; standing pattern Fable's 2026-08-11 ruling recorded for exactly this case,;;; and this file is the replacement that pattern asks for: SD-P8's own;;; differential, scoped to SD-P8's own base.;;;;;; TWO PROPERTIES THIS INSTRUMENT IS BUILT AROUND.;;;;;; * BOTH SIDES RUN THE SAME FILE. `test/gate-sd-p8.sh` copies this script;;; into a detached worktree at the base SHA and runs it there, then runs;;; it here. An instrument that differs between the two sides of a;;; differential measures itself.;;;;;; * EVERY LOCATION IS EMITTED RELATIVE TO THE MANUSCRIPT'S HEADER END, so;;; `#lang sigil/doc` (16 bytes) and `#lang sigil/doc/book` (21 bytes);;; produce identical offsets for identical prose. That is the ONE;;; difference this phase intends, and making it invisible is what leaves;;; the comparison sensitive to every difference it does NOT intend.;;; Generated locations (`<sigil/doc-generated>`, byte=0) are NOT shifted:;;; they are not manuscript positions and subtracting from them would be;;; arithmetic on a value that means nothing.;;;;;; The profile comes from the manuscript's own `#lang` id rather than from the;;; three-argument default-profile entry point. In the base tree `sigil/doc`;;; resolves to that tree's `sigil-doc-profile` (roots `(chapter)`); here it;;; resolves to this tree's (roots `(article)`). Both are the tree's own answer;;; to "what does this language admit", which is what makes the same file run;;; correctly on both sides. `sigil/doc/book` is constructed inline rather than;;; imported from `(sigil doc book reader)`, because that module does not exist;;; in the base tree and an import of it would make this script unrunnable;;; there -- which would silently reduce the differential to one side.;;;;;; Nothing is evaluated: lowering is a pure source-to-syntax transformation.(import (sigil core) (sigil fs) (sigil language) (sigil doc private parser));; ------------------------------------------------------------------ output(define header-end 0);; A manuscript location is emitted header-relative; a generated one is not a;; manuscript position and is emitted as it stands. The discriminator is the;; file name the location carries, so this cannot accidentally shift a location;; that has no manuscript behind it.(define (emit-location loc depth) (if (or (not loc) (> depth 40)) (display "#f") (let* ((file (srcloc-file loc)) (generated? (string=? file "<sigil/doc-generated>")) (position (srcloc-position loc))) (display "[") (write file) (display " ") (display (srcloc-line loc)) (display ":") (display (srcloc-column loc)) (display (if generated? " byte=" " rel=")) (display (if generated? position (- position header-end))) (display " span=") (display (srcloc-span loc)) (let ((origin (srcloc-origin loc))) (if origin (begin (display " <-") (emit-location origin (+ depth 1))) #f)) (display "]"))));; Walk the improper-list structure explicitly. `write` alone would not show;; syntax wrappers or locations, which are the part most likely to move.;;;; THE `make-srcloc` SPECIAL CASE IS NOT COSMETIC. The lowered forms carry the;; manuscript's byte offsets TWICE: once as the syntax objects' own srclocs,;; and once as literal integer DATA inside the `(make-srcloc <file> <line>;; <col> <pos> <span>)` calls the parser generates for `author-at`. Normalizing;; only the first leaves the second absolute, and every migrated manuscript;; then differs by the header delta in a place that looks like content. The;; discriminator is structural -- head symbol `make-srcloc`, six elements, and;; a file argument that is this manuscript rather than the generated marker --;; so no ordinary integer in a manuscript can be caught by it.(define (syntax-datum-of value) (if (syntax? value) (syntax-datum value) value))(define (generated-srcloc-call? value) (and (pair? value) (eq? (syntax-datum-of (car value)) 'make-srcloc) (= (length value) 6) (let ((file (syntax-datum-of (cadr value)))) (and (string? file) (not (string=? file "<sigil/doc-generated>"))))))(define expression-binding-prefix "#%sigil/doc-expression:")(define (expression-binding? name) (let ((n (string-length name)) (m (string-length expression-binding-prefix))) (and (> n m) (string=? (substring name 0 m) expression-binding-prefix))))(define (emit value) (cond ((syntax? value) (display "#<") (emit-location (syntax-srcloc value) 0) (display " ") (emit (syntax-datum value)) (display ">")) ((null? value) (display "()")) ((generated-srcloc-call? value) ;; Same rendering as the general list path, with element 4 -- the byte ;; position -- rewritten header-relative and RELABELLED, so a reader can ;; see which number was normalized rather than having to know. (display "(") (let loop ((rest value) (index 0)) (cond ((null? rest) #t) (else (if (= index 0) #t (display " ")) (if (= index 4) (begin (display "rel:") (display (- (syntax-datum-of (car rest)) header-end))) (emit (car rest))) (loop (cdr rest) (+ index 1))))) (display ")")) ((pair? value) (display "(") (let loop ((rest value) (first? #t)) (cond ((null? rest) #t) ((pair? rest) (if first? #t (display " ")) (emit (car rest)) (loop (cdr rest) #f)) (else (display " . ") (emit rest)))) (display ")")) ;; The parser spells a hoisted author expression's private binding ;; `#%sigil/doc-expression:<absolute byte offset>` ;; (`private/parser.sgl:595`). That is the THIRD place a manuscript's byte ;; offsets reach the lowered output -- after the syntax srclocs and the ;; `make-srcloc` data -- and it is the least obvious, because it wears the ;; clothes of a name rather than of a position. It is normalized for the ;; same reason as the other two, and it is worth knowing on its own: a ;; manuscript's generated binding names are NOT stable under an edit to its ;; `#lang` line. ((and (symbol? value) (expression-binding? (symbol->string value))) (display "#%sigil/doc-expression:rel:") (display (- (string->number (substring (symbol->string value) (string-length expression-binding-prefix) (string-length (symbol->string value)))) header-end))) (else (write value))));; ----------------------------------------------------------------- profiles;; `sigil/doc` is the TREE'S OWN base profile, deliberately: this script is run;; in two trees whose base languages differ, and asking each tree what its base;; language admits is the only way one file can be correct in both.(define book-profile (make-document-profile "sigil/doc/book" '(chapter) 'one))(define (profile-for id) (cond ((string=? id "sigil/doc") sigil-doc-profile) ((string=? id "sigil/doc/book") book-profile) (else #f)));; ------------------------------------------------------------------ corpus(define (basename path) (let loop ((i (string-length path))) (cond ((= i 0) path) ((char=? (string-ref path (- i 1)) #\/) (substring path i (string-length path))) (else (loop (- i 1))))))(define (has-suffix? name suffix) (let ((n (string-length name)) (m (string-length suffix))) (and (> n m) (string=? (substring name (- n m) n) suffix))));; `directory-list` order is filesystem order and is NOT stable across two;; checkouts. Sorting is what makes the two sides comparable line by line;; rather than only as sets.(define (string-before? a b) (let ((na (string-length a)) (nb (string-length b))) (let loop ((i 0)) (cond ((= i na) (> nb na)) ((= i nb) #f) ((char<? (string-ref a i) (string-ref b i)) #t) ((char<? (string-ref b i) (string-ref a i)) #f) (else (loop (+ i 1)))))))(define (sort-strings xs) (let insert-sort ((rest xs) (out '())) (if (null? rest) out (insert-sort (cdr rest) (let place ((ys out)) (cond ((null? ys) (list (car rest))) ((string-before? (car rest) (car ys)) (cons (car rest) ys)) (else (cons (car ys) (place (cdr ys))))))))))(define (sgd-files dir) (if (not (file-exists? dir)) '() (let loop ((entries (sort-strings (directory-list dir))) (out '())) (if (null? entries) (reverse out) (loop (cdr entries) (if (has-suffix? (car entries) ".sgd") (cons (string-append dir "/" (car entries)) out) out))))));; DERIVED, not inherited. SD-P3's corpus was an explicit list carrying its own;; instruction to be re-derived whenever the tree gained manuscripts, and it was;; not: eleven of the tree's .sgd files are absent from its baseline. A derived;; population cannot go stale that way. The two sides may legitimately hold;; different files -- this phase adds four -- so the GATE takes the intersection;; by name and accounts for the difference; this script's job is to report;; everything it finds, with a verdict per manuscript.(define manuscripts (append (sgd-files "book/practical-sigil") (sgd-files "test/fixtures")));; The first top-level `@command`, read as TEXT. This is a second, independent;; instrument: the gate uses it to predict which manuscripts must differ;; (the ones whose root command this phase changed) without asking the lowering;; -- the thing under test -- what it thinks the root was.(define (first-root-command source) (let ((n (string-length source))) (let loop ((i 0) (line-start #t)) (cond ((>= i n) "NONE") ((char=? (string-ref source i) #\newline) (loop (+ i 1) #t)) ((and line-start (char=? (string-ref source i) #\@) (< (+ i 1) n) (char-alphabetic? (string-ref source (+ i 1)))) (let scan ((j (+ i 1))) (if (and (< j n) (or (char-alphabetic? (string-ref source j)) (char=? (string-ref source j) #\-))) (scan (+ j 1)) (substring source (+ i 1) j)))) (else (loop (+ i 1) #f))))))(define compared 0)(define skipped 0)(define (dump path) (let* ((source (read-file-string path)) (name (basename path)) (header (inspect-language-source source name)) (id (language-header-id header)) (profile (profile-for id))) (set! header-end (language-header-end header)) ;; The MANIFEST line carries everything that legitimately differs between ;; the two trees -- the language id, the header length, the root command -- ;; and is deliberately OUTSIDE the compared section. Putting it inside would ;; make every migrated manuscript differ for the one reason this ;; differential exists to look past. (display "--- MANIFEST ") (display name) (display " lang=") (display id) (display " header=") (display header-end) (display " root=@") (display (first-root-command source)) (newline) (if (not profile) (begin (set! skipped (+ skipped 1)) (display "--- SKIPPED ") (display name) (display " reason=unknown-language:") (display id) (newline)) (let ((outcome (guard (exn (else (cons 'raised (exception-message exn)))) (cons 'forms (parse-document-forms-with-profile profile source name header-end))))) (if (eq? (car outcome) 'raised) (begin (set! skipped (+ skipped 1)) (display "--- SKIPPED ") (display name) (display " reason=raised:") (display (cdr outcome)) (newline)) (begin (set! compared (+ compared 1)) (display "=== ") (display name) (display " forms=") (display (length (cdr outcome))) (newline) (for-each (lambda (form) (emit form) (newline)) (cdr outcome))))))))(for-each dump manuscripts)(display "SD_P8_LOWERING_DUMP_COMPLETE found ")(display (length manuscripts))(display " compared ")(display compared)(display " skipped ")(display skipped)(newline)