AtlatestRepositorysigil-doc
1
#!/usr/bin/env bash2
# SD-P4 gate: passive parse API, parser-state localization, argument-group3
# diagnostics.4
#5
# Every arm prints its own marker and the script asserts on MARKERS, not on6
# exit status alone: a non-zero exit is equally consistent with a gate that7
# never ran (127 not-found, 126 not-executable), and this estate has been8
# fooled by both.9
set -euo pipefail10
repo_dir="$(cd "$(dirname "$0")/.." && pwd)"11
cd "$repo_dir"12
mkdir -p "$repo_dir/build"13
sigil="${SIGIL_D1_COMPILER:-$repo_dir/../task-sigil-language-readers/build/dev/bin/sigil}"14
redirects="${SIGIL_D1_REDIRECTS:-$repo_dir/dev-redirects.sgl}"15
gate_dir=$(mktemp -d "$repo_dir/build/sd-p4-gate.XXXXXX")16
export SIGIL_REDIRECTS="$redirects"17
export XDG_CACHE_HOME="$gate_dir/cache"19
pass() { printf 'SD_P4_GATE_%02d %s\n' "$1" "$2"; }21
# Every external tool this gate depends on must EXIST, or an absent tool22
# silently degrades an assertion into a weaker one that cannot fail.23
command -v rg >/dev/null || { echo "rg is required by this gate" >&2; exit 1; }24
test -x "$sigil" || { echo "dev compiler not found at $sigil" >&2; exit 1; }25
test -f src/sigil/doc/private/parser.sgl26
test -f src/sigil/doc/inspect.sgl27
test -f test/test-passive-parse.sgl28
test -f test/fixtures/sd-p4-witness.sgd29
"$sigil" --version31
# ---------------------------------------------------------------- ARM 0132
# The passive suite: span round-trips against independently recomputed byte and33
# scalar offsets, per-field argument spans, pseudo-kind collisions, body kinds,34
# root-policy independence, malformed-source diagnostic equivalence, and the35
# state-localization behaviour (interleaved and nested parses).36
suite_log="$gate_dir/passive-suite.log"37
# NOT `--report`: that flag captures the suite's stdout, and this arm asserts on38
# a denominator the suite PRINTS. Running with it made this gate exit silently39
# on a suite that had actually passed 14/14.40
if ! setsid -w "$sigil" -L src -L test test test/test-passive-parse.sgl >"$suite_log" 2>&1; then41
echo "passive suite exited non-zero" >&2; tail -40 "$suite_log" >&2; exit 142
fi43
# Strip ANSI colour before matching, so the assertion depends on the runner's44
# words rather than on its escape sequences.45
sed -e 's/\x1b\[[0-9;]*m//g' "$suite_log" >"$suite_log.clean"46
# THE COUNT IS DERIVED FROM THE SUITE FILE, NOT WRITTEN IN.47
#48
# This line asserted a hardcoded `Tests: 17 passed (17 total)`. It matched, so49
# it was green -- and green is exactly what a stale literal looks like right up50
# 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 to52
# test/test-root-profile.sgl, and that gate sat RED ON MASTER accusing a suite53
# that passes 12/12 -- the gate lying about healthy product code. It cost a54
# pristine-HEAD attribution run before anyone could even tell whose diff broke55
# it. Nothing about this file made it the second one rather than the first.56
#57
# folio's test/gate-f1a.sh states the rule in its own header and derives its58
# count exactly like this: "Every count this gate checks is DERIVED FROM THE59
# ARTIFACT it describes, never written in as a literal. [...] That drift is the60
# single most common defect in this estate's gates." Adding a test must not61
# require editing a number here, and now it does not.62
#63
# EVERY `|| true` BELOW IS LOAD-BEARING, NOT DEFENSIVE NOISE. Under `set -e` a64
# command substitution whose command fails ABORTS THE SCRIPT; `grep -c` exits 165
# on zero matches and so does `rg`. Without them, the very inputs these guards66
# 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 by69
# twice. The derivation case was found by sabotage; the summary case was found70
# by adversarial review, and on that input this instrumentation had been71
# strictly WORSE than the hardcoded literal it replaced, which did print a72
# message and a log tail.73
# THE COMPLETENESS CHECK COMES FIRST, and deliberately so. The anchor is74
# column-0 `(test "` forms; a test INDENTED inside another form is still75
# collected by the runner and invisible to it. Indenting ALL of them trips the76
# floor below, which then says "the DERIVATION is broken" -- true, but the least77
# specific true thing available. Indenting ONE trips neither the floor nor78
# anything else, and surfaces as "declares 16 but collected 17": the gate79
# accusing a healthy suite for a defect in its own anchor, which is a narrower80
# instance of exactly what this file was changed to stop doing. So ask about81
# completeness before asking about the count, and name the real defect.82
indented_tests=$(grep -cE '^[[:space:]]+\(test "' test/test-passive-parse.sgl || true)83
test "$indented_tests" -eq 0 || {84
echo "test/test-passive-parse.sgl has $indented_tests INDENTED (test \"...\" forms;" >&285
echo "the column-0 derivation cannot see them, so the DERIVATION is incomplete" >&286
grep -nE '^[[:space:]]+\(test "' test/test-passive-parse.sgl >&2; exit 1; }87
expected_tests=$(grep -cE '^\(test "' test/test-passive-parse.sgl || true)88
test "$expected_tests" -ge 1 || {89
echo "derived 0 tests from test/test-passive-parse.sgl; the DERIVATION is broken, not the suite" >&290
exit 1; }91
# >>> ARM1_SUMMARY_ASSERTIONS BEGIN92
# test/sabotage-sd-p4-counts.sh LEG 2b slices these exact lines out of THIS file93
# and runs them against synthetic summaries under the same `set -euo pipefail`.94
# That is deliberate: a retyped copy would agree with this block by95
# construction. Keep the block self-contained -- it may read only96
# `$suite_log.clean` and `$expected_tests`.97
reported=$(rg -o 'Tests:[[:space:]]+[0-9]+ passed' "$suite_log.clean" | head -1 | grep -oE '[0-9]+' | head -1 || true)98
collected=$(rg -o '\([0-9]+ total\)' "$suite_log.clean" | head -1 | grep -oE '[0-9]+' | head -1 || true)99
test -n "$reported" -a -n "$collected" || {100
echo "passive suite printed no summary line" >&2101
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 test104
# that silently stopped running -- and every test collected must have PASSED,105
# which catches a failure the summary would otherwise bury.106
test "$collected" = "$expected_tests" || {107
echo "test/test-passive-parse.sgl declares $expected_tests tests but the runner collected $collected" >&2108
tail -40 "$suite_log.clean" >&2; exit 1; }109
test "$reported" = "$collected" || {110
echo "passive suite reported $reported passed of $collected collected" >&2111
tail -40 "$suite_log.clean" >&2; exit 1; }112
# <<< ARM1_SUMMARY_ASSERTIONS END113
rg -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 depth116
# and, more usefully, puts the denominator in the GATE transcript. The117
# load-bearing assertion is the suite's.118
# Take field 2, NOT the first digit run: the marker's own name contains a digit119
# ("SD_P4"), and matching `\d+` against the whole line silently yielded 4.120
checked=$(rg -o "SD_P4_NODES_CHECKED [0-9]+" "$suite_log.clean" | head -1 | awk '{print $2}' || true)121
test -n "$checked" || {122
echo "suite printed no node-count denominator" >&2; tail -40 "$suite_log" >&2; exit 1; }123
test "$checked" -ge 120 || { echo "span walk covered only $checked nodes" >&2; exit 1; }124
pass 1 "passive suite $reported passed / $collected collected / $expected_tests declared, spans verified at $checked nodes"126
# ---------------------------------------------------------------- ARM 02127
# No-execution witness, WITH its positive control in the same arm.128
witness_passive="$gate_dir/witness-passive.log"129
witness_control="$gate_dir/witness-control.log"131
# (a) POSITIVE CONTROL FIRST. The fixture's markers must appear when the132
# document is genuinely evaluated through the real #lang path. Until this133
# passes, the absence assertion below means nothing.134
setsid -w "$sigil" -L src eval -f test/fixtures/sd-p4-witness.sgd >"$witness_control" 2>&1135
rg -q 'SDP4-WITNESS-AUTHOR-EXPRESSION-RAN' "$witness_control" || {136
echo "positive control: evaluating the fixture did NOT run its author expression" >&2137
cat "$witness_control" >&2; exit 1; }138
rg -q 'SDP4-WITNESS-AUTHOR-COMMAND-RAN' "$witness_control" || {139
echo "positive control: evaluating the fixture did NOT run its author command" >&2140
cat "$witness_control" >&2; exit 1; }142
# (b) THE ASSERTION. Passive inspection, in a fresh process importing only the143
# inspection surface, must run none of it.144
setsid -w "$sigil" -L src -L test eval -f test/sd-p4-witness-probe.sgl >"$witness_passive" 2>&1145
rg -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.148
rg -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; }150
if rg -q 'SDP4-WITNESS-' "$witness_passive"; then151
echo "AUTHOR CODE EXECUTED DURING PASSIVE INSPECTION" >&2; cat "$witness_passive" >&2; exit 1152
fi153
# Code bodies stay inert on BOTH paths.154
if rg -q 'SDP4-WITNESS-CODE-BODY-RAN' "$witness_control" "$witness_passive"; then155
echo "a @code body was evaluated" >&2; exit 1156
fi157
pass 2 "no-execution witness armed (control ran the author code; inspection did not)"159
# ---------------------------------------------------------------- ARM 03160
# No module-level parser state remains, with a positive control proving the161
# probe can see a `set!` when one IS present. An absence assertion whose probe162
# was never shown to fire is the estate's most common vacuous gate.163
if rg -n '\(set!' src/sigil/doc/private/parser.sgl; then164
echo "module-level set! remains in the parser" >&2; exit 1165
fi166
control_copy="$gate_dir/parser-set-control.sgl"167
cp src/sigil/doc/private/parser.sgl "$control_copy"168
printf '\n(set! positive-control-binding 1)\n' >>"$control_copy"169
rg -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. Match172
# the IMPORT FORM, not the file: an earlier version of this arm matched a173
# comment that merely named `(sigil doc model)` in prose and reported a174
# violation that did not exist.175
imports=$(rg '^\s*\(import' src/sigil/doc/inspect.sgl || true)176
test -n "$imports" || {177
echo "no import form found -- the probe cannot see what it asserts about" >&2; exit 1; }178
if printf '%s\n' "$imports" | rg -q '\(sigil doc (author|model|private author-runtime)\)'; then179
echo "the inspection surface imports an evaluation-side module" >&2180
printf '%s\n' "$imports" >&2; exit 1181
fi182
# Positive control on the SAME extraction: the import it does have must be183
# visible to this probe, or the absence above is unfalsifiable.184
printf '%s\n' "$imports" | rg -q '\(sigil doc private parser\)' || {185
echo "import probe cannot see the imports it is asserting about" >&2; exit 1; }186
pass 3 "no module-level parser state; inspection surface imports no evaluation-side module"188
# ---------------------------------------------------------------- ARM 04189
# Existing suites, unchanged in what they assert. gate-d1.sh runs gate-d0.sh.190
test/gate-d1.sh191
test/gate-d2.sh192
test/gate-d3.sh193
pass 4 "D0/D1/D2/D3 regression green"195
echo "SD_P4_GATE_GREEN"