AtlatestRepositorysigil-doc

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

1#!/usr/bin/env bash
2# SD-P4 gate: passive parse API, parser-state localization, argument-group
3# diagnostics.
4#
5# Every arm prints its own marker and the script asserts on MARKERS, not on
6# exit status alone: a non-zero exit is equally consistent with a gate that
7# never ran (127 not-found, 126 not-executable), and this estate has been
8# fooled by both.
9set -euo pipefail
10repo_dir="$(cd "$(dirname "$0")/.." && pwd)"
11cd "$repo_dir"
12mkdir -p "$repo_dir/build"
13sigil="${SIGIL_D1_COMPILER:-$repo_dir/../task-sigil-language-readers/build/dev/bin/sigil}"
14redirects="${SIGIL_D1_REDIRECTS:-$repo_dir/dev-redirects.sgl}"
15gate_dir=$(mktemp -d "$repo_dir/build/sd-p4-gate.XXXXXX")
16export SIGIL_REDIRECTS="$redirects"
17export XDG_CACHE_HOME="$gate_dir/cache"
19pass() { printf 'SD_P4_GATE_%02d %s\n' "$1" "$2"; }
21# Every external tool this gate depends on must EXIST, or an absent tool
22# silently degrades an assertion into a weaker one that cannot fail.
23command -v rg >/dev/null || { echo "rg is required by this gate" >&2; exit 1; }
24test -x "$sigil" || { echo "dev compiler not found at $sigil" >&2; exit 1; }
25test -f src/sigil/doc/private/parser.sgl
26test -f src/sigil/doc/inspect.sgl
27test -f test/test-passive-parse.sgl
28test -f test/fixtures/sd-p4-witness.sgd
29"$sigil" --version
31# ---------------------------------------------------------------- ARM 01
32# The passive suite: span round-trips against independently recomputed byte and
33# scalar offsets, per-field argument spans, pseudo-kind collisions, body kinds,
34# root-policy independence, malformed-source diagnostic equivalence, and the
35# state-localization behaviour (interleaved and nested parses).
36suite_log="$gate_dir/passive-suite.log"
37# NOT `--report`: that flag captures the suite's stdout, and this arm asserts on
38# a denominator the suite PRINTS. Running with it made this gate exit silently
39# on a suite that had actually passed 14/14.
40if ! setsid -w "$sigil" -L src -L test test test/test-passive-parse.sgl >"$suite_log" 2>&1; then
41 echo "passive suite exited non-zero" >&2; tail -40 "$suite_log" >&2; exit 1
42fi
43# Strip ANSI colour before matching, so the assertion depends on the runner's
44# words rather than on its escape sequences.
45sed -e 's/\x1b\[[0-9;]*m//g' "$suite_log" >"$suite_log.clean"
46# THE COUNT IS DERIVED FROM THE SUITE FILE, NOT WRITTEN IN.
48# This line asserted a hardcoded `Tests: 17 passed (17 total)`. It matched, so
49# it was green -- and green is exactly what a stale literal looks like right up
50# until someone adds a test. Its sibling had already gone the whole way:
51# test/gate-sd-p1.sh hardcoded 11, a TWELFTH test was added to
52# test/test-root-profile.sgl, and that gate sat RED ON MASTER accusing a suite
53# that passes 12/12 -- the gate lying about healthy product code. It cost a
54# pristine-HEAD attribution run before anyone could even tell whose diff broke
55# it. Nothing about this file made it the second one rather than the first.
57# folio's test/gate-f1a.sh states the rule in its own header and derives its
58# count exactly like this: "Every count this gate checks is DERIVED FROM THE
59# ARTIFACT it describes, never written in as a literal. [...] That drift is the
60# single most common defect in this estate's gates." Adding a test must not
61# require editing a number here, and now it does not.
63# EVERY `|| true` BELOW IS LOAD-BEARING, NOT DEFENSIVE NOISE. Under `set -e` a
64# command substitution whose command fails ABORTS THE SCRIPT; `grep -c` exits 1
65# on zero matches and so does `rg`. Without them, the very inputs these guards
66# exist to describe kill the gate at the assignment and no guard ever speaks --
67# exit 1 with no message, which is indistinguishable from the gate not running,
68# the exact confusion this file's header says the estate has been fooled by
69# twice. The derivation case was found by sabotage; the summary case was found
70# by adversarial review, and on that input this instrumentation had been
71# strictly WORSE than the hardcoded literal it replaced, which did print a
72# message and a log tail.
73# THE COMPLETENESS CHECK COMES FIRST, and deliberately so. The anchor is
74# column-0 `(test "` forms; a test INDENTED inside another form is still
75# collected by the runner and invisible to it. Indenting ALL of them trips the
76# floor below, which then says "the DERIVATION is broken" -- true, but the least
77# specific true thing available. Indenting ONE trips neither the floor nor
78# anything else, and surfaces as "declares 16 but collected 17": the gate
79# accusing a healthy suite for a defect in its own anchor, which is a narrower
80# instance of exactly what this file was changed to stop doing. So ask about
81# completeness before asking about the count, and name the real defect.
82indented_tests=$(grep -cE '^[[:space:]]+\(test "' test/test-passive-parse.sgl || true)
83test "$indented_tests" -eq 0 || {
84 echo "test/test-passive-parse.sgl has $indented_tests INDENTED (test \"...\" forms;" >&2
85 echo "the column-0 derivation cannot see them, so the DERIVATION is incomplete" >&2
86 grep -nE '^[[:space:]]+\(test "' test/test-passive-parse.sgl >&2; exit 1; }
87expected_tests=$(grep -cE '^\(test "' test/test-passive-parse.sgl || true)
88test "$expected_tests" -ge 1 || {
89 echo "derived 0 tests from test/test-passive-parse.sgl; the DERIVATION is broken, not the suite" >&2
90 exit 1; }
91# >>> ARM1_SUMMARY_ASSERTIONS BEGIN
92# test/sabotage-sd-p4-counts.sh LEG 2b slices these exact lines out of THIS file
93# and runs them against synthetic summaries under the same `set -euo pipefail`.
94# That is deliberate: a retyped copy would agree with this block by
95# construction. Keep the block self-contained -- it may read only
96# `$suite_log.clean` and `$expected_tests`.
97reported=$(rg -o 'Tests:[[:space:]]+[0-9]+ passed' "$suite_log.clean" | head -1 | grep -oE '[0-9]+' | head -1 || true)
98collected=$(rg -o '\([0-9]+ total\)' "$suite_log.clean" | head -1 | grep -oE '[0-9]+' | head -1 || true)
99test -n "$reported" -a -n "$collected" || {
100 echo "passive suite printed no summary line" >&2
101 tail -40 "$suite_log.clean" >&2; exit 1; }
102# TWO assertions, not one. A single derived count catches neither case cleanly:
103# every test the FILE declares must have been COLLECTED -- which catches a test
104# that silently stopped running -- and every test collected must have PASSED,
105# which catches a failure the summary would otherwise bury.
106test "$collected" = "$expected_tests" || {
107 echo "test/test-passive-parse.sgl declares $expected_tests tests but the runner collected $collected" >&2
108 tail -40 "$suite_log.clean" >&2; exit 1; }
109test "$reported" = "$collected" || {
110 echo "passive suite reported $reported passed of $collected collected" >&2
111 tail -40 "$suite_log.clean" >&2; exit 1; }
112# <<< ARM1_SUMMARY_ASSERTIONS END
113rg -q 'All tests passed' "$suite_log.clean" || {
114 echo "passive suite did not report all tests passing" >&2; tail -40 "$suite_log.clean" >&2; exit 1; }
115# The suite asserts this floor itself; reproducing it here is defence in depth
116# and, more usefully, puts the denominator in the GATE transcript. The
117# load-bearing assertion is the suite's.
118# Take field 2, NOT the first digit run: the marker's own name contains a digit
119# ("SD_P4"), and matching `\d+` against the whole line silently yielded 4.
120checked=$(rg -o "SD_P4_NODES_CHECKED [0-9]+" "$suite_log.clean" | head -1 | awk '{print $2}' || true)
121test -n "$checked" || {
122 echo "suite printed no node-count denominator" >&2; tail -40 "$suite_log" >&2; exit 1; }
123test "$checked" -ge 120 || { echo "span walk covered only $checked nodes" >&2; exit 1; }
124pass 1 "passive suite $reported passed / $collected collected / $expected_tests declared, spans verified at $checked nodes"
126# ---------------------------------------------------------------- ARM 02
127# No-execution witness, WITH its positive control in the same arm.
128witness_passive="$gate_dir/witness-passive.log"
129witness_control="$gate_dir/witness-control.log"
131# (a) POSITIVE CONTROL FIRST. The fixture's markers must appear when the
132# document is genuinely evaluated through the real #lang path. Until this
133# passes, the absence assertion below means nothing.
134setsid -w "$sigil" -L src eval -f test/fixtures/sd-p4-witness.sgd >"$witness_control" 2>&1
135rg -q 'SDP4-WITNESS-AUTHOR-EXPRESSION-RAN' "$witness_control" || {
136 echo "positive control: evaluating the fixture did NOT run its author expression" >&2
137 cat "$witness_control" >&2; exit 1; }
138rg -q 'SDP4-WITNESS-AUTHOR-COMMAND-RAN' "$witness_control" || {
139 echo "positive control: evaluating the fixture did NOT run its author command" >&2
140 cat "$witness_control" >&2; exit 1; }
142# (b) THE ASSERTION. Passive inspection, in a fresh process importing only the
143# inspection surface, must run none of it.
144setsid -w "$sigil" -L src -L test eval -f test/sd-p4-witness-probe.sgl >"$witness_passive" 2>&1
145rg -q 'SD_P4_WITNESS_PROBE_INSPECTED' "$witness_passive" || {
146 echo "witness probe did not run to completion" >&2; cat "$witness_passive" >&2; exit 1; }
147# ...and it must have actually traversed something.
148rg -q 'SD_P4_WITNESS_PROBE_INSPECTED nodes=([1-9]\d*) commands=([1-9]\d*) author-expressions=([1-9]\d*)' "$witness_passive" || {
149 echo "witness probe reported an empty traversal" >&2; cat "$witness_passive" >&2; exit 1; }
150if rg -q 'SDP4-WITNESS-' "$witness_passive"; then
151 echo "AUTHOR CODE EXECUTED DURING PASSIVE INSPECTION" >&2; cat "$witness_passive" >&2; exit 1
152fi
153# Code bodies stay inert on BOTH paths.
154if rg -q 'SDP4-WITNESS-CODE-BODY-RAN' "$witness_control" "$witness_passive"; then
155 echo "a @code body was evaluated" >&2; exit 1
156fi
157pass 2 "no-execution witness armed (control ran the author code; inspection did not)"
159# ---------------------------------------------------------------- ARM 03
160# No module-level parser state remains, with a positive control proving the
161# probe can see a `set!` when one IS present. An absence assertion whose probe
162# was never shown to fire is the estate's most common vacuous gate.
163if rg -n '\(set!' src/sigil/doc/private/parser.sgl; then
164 echo "module-level set! remains in the parser" >&2; exit 1
165fi
166control_copy="$gate_dir/parser-set-control.sgl"
167cp src/sigil/doc/private/parser.sgl "$control_copy"
168printf '\n(set! positive-control-binding 1)\n' >>"$control_copy"
169rg -q '\(set!' "$control_copy" || {
170 echo "the set! probe cannot see a set! that IS present" >&2; exit 1; }
171# The inspection surface must not reach the author runtime or the model. Match
172# the IMPORT FORM, not the file: an earlier version of this arm matched a
173# comment that merely named `(sigil doc model)` in prose and reported a
174# violation that did not exist.
175imports=$(rg '^\s*\(import' src/sigil/doc/inspect.sgl || true)
176test -n "$imports" || {
177 echo "no import form found -- the probe cannot see what it asserts about" >&2; exit 1; }
178if printf '%s\n' "$imports" | rg -q '\(sigil doc (author|model|private author-runtime)\)'; then
179 echo "the inspection surface imports an evaluation-side module" >&2
180 printf '%s\n' "$imports" >&2; exit 1
181fi
182# Positive control on the SAME extraction: the import it does have must be
183# visible to this probe, or the absence above is unfalsifiable.
184printf '%s\n' "$imports" | rg -q '\(sigil doc private parser\)' || {
185 echo "import probe cannot see the imports it is asserting about" >&2; exit 1; }
186pass 3 "no module-level parser state; inspection surface imports no evaluation-side module"
188# ---------------------------------------------------------------- ARM 04
189# Existing suites, unchanged in what they assert. gate-d1.sh runs gate-d0.sh.
190test/gate-d1.sh
191test/gate-d2.sh
192test/gate-d3.sh
193pass 4 "D0/D1/D2/D3 regression green"
195echo "SD_P4_GATE_GREEN"