AtlatestRepositorysigil-doc

sigil-doc / tree / testsabotage-sd-p3.sh

1#!/usr/bin/env bash
2# SD-P3 sabotage driver.
3#
4# A gate that cannot demonstrate it fails is not a gate. Each leg below plants a
5# specific defect, proves the corresponding check goes RED, restores the file and
6# verifies the restoration BY CONTENT HASH -- never by the exit code of the
7# restore, which reports success just as cheerfully for a wipe.
8#
9# Legs 1-6 are PRODUCT-layer: they break the behaviour, not the assertion, so a
10# red proves the suite executes the loaded product rather than a mirror of
11# itself. Leg 7 is ASSERTION-layer. Leg 8 neuters the equality arm's INPUT (and,
12# unavoidably, its own control, which calls the same procedure) to prove that arm
13# can fail at all -- the lesson SD-P1's review taught about its own arm 5.
14# Leg 10 plants the escape-value defect adversarial review found in this phase.
15# Legs 2 and 11 break the boundary rule two different ways and are asserted to
16# fail in two different ways. Leg 12 is the MERGE-SABOTAGE: it breaks a
17# property that only exists once SD-P2 and SD-P3 are both present.
19# Restoration is a TRAP, not a step that must be reached: a leg that exits early
20# would otherwise leave the corrupted file in the worktree.
21set -euo pipefail
22repo_dir="$(cd "$(dirname "$0")/.." && pwd)"
23cd "$repo_dir"
24sigil="${SIGIL_D1_COMPILER:-$repo_dir/../task-sigil-language-readers/build/dev/bin/sigil}"
25export SIGIL_REDIRECTS="${SIGIL_D1_REDIRECTS:-$repo_dir/dev-redirects.sgl}"
26mkdir -p "$repo_dir/build"
27work=$(mktemp -d "$repo_dir/build/sd-p3-sabotage.XXXXXX")
28export XDG_CACHE_HOME="$work/cache"
30for tool in rg sha256sum awk sed python3; do
31 command -v "$tool" >/dev/null || { echo "$tool is required" >&2; exit 1; }
32done
33test -x "$sigil" || { echo "dev compiler not found at $sigil" >&2; exit 1; }
35PARSER=src/sigil/doc/private/parser.sgl
36RUNTIME=src/sigil/doc/private/author-runtime.sgl
37SUITE=test/test-prose-runs.sgl
38GUARDED=("$PARSER" "$RUNTIME" "$SUITE")
40pristine="$work/pristine"
41mkdir -p "$pristine"
42for f in "${GUARDED[@]}"; do
43 cp "$f" "$pristine/$(basename "$f")"
44done
46restore_all() {
47 for f in "${GUARDED[@]}"; do
48 cp "$pristine/$(basename "$f")" "$f"
49 done
51trap restore_all EXIT INT TERM
53verify_restored() {
54 for f in "${GUARDED[@]}"; do
55 a=$(sha256sum "$f" | awk '{print $1}')
56 b=$(sha256sum "$pristine/$(basename "$f")" | awk '{print $1}')
57 if [ "$a" != "$b" ]; then
58 echo "RESTORE FAILED for $f ($a != $b)" >&2; exit 1
59 fi
60 done
63# `python3 - <file> <old> <new>`: an exact, unambiguous substring replacement
64# that refuses if the text is absent or appears more than once. A sed that
65# silently matches nothing produces a leg that tests the pristine code and
66# reports a convincing green.
67patch_file() {
68 python3 - "$1" "$2" "$3" <<'PY'
69import sys
70path, old, new = sys.argv[1], sys.argv[2], sys.argv[3]
71s = open(path).read()
72n = s.count(old)
73if n != 1:
74 sys.stderr.write(f"patch target appears {n} times in {path}; refusing\n")
75 sys.exit(1)
76open(path, "w").write(s.replace(old, new))
77PY
80# A red is not a red is not a red. There are two ways this suite can fail and
81# they are DIFFERENT FACTS, so each leg declares which one it expects and a leg
82# that fails the other way is treated as a failure of the leg:
84# test <fragment> the suite RAN and the named test failed. Anchored on the
85# `✗` marker, because the runner echoes every test's name as
86# it runs -- pass or fail -- so a bare grep for the name
87# matches even when the suite went red somewhere else.
88# load <fragment> the suite could not LOAD, because the sabotage makes the
89# fixtures themselves unbuildable. That is the strongest red
90# available for leg 1, and it must not be mistaken for the
91# suite exploding on an unrelated error.
92suite_red() { # $1 = leg name, $2 = mode (test|load), $3 = expected fragment
93 local log="$work/$1.log"
94 if setsid -w "$sigil" -L src -L test test "$SUITE" >"$log" 2>&1; then
95 echo "LEG $1: the suite PASSED under sabotage; the check cannot fail" >&2
96 tail -20 "$log" >&2
97 exit 1
98 fi
99 sed -e 's/\x1b\[[0-9;]*m//g' "$log" >"$log.clean"
100 case "$2" in
101 test)
102 rg -q "^\\s*✗\\s+.*$3" "$log.clean" || {
103 echo "LEG $1: expected the suite to RUN and fail at '$3'" >&2
104 rg -n '✗|LOAD ERROR|Tests:' "$log.clean" >&2 || true
105 exit 1; }
106 # ... and it really ran: a load failure reports one synthetic test.
107 rg -q 'LOAD ERROR' "$log.clean" && {
108 echo "LEG $1: the suite failed to LOAD; it never reached '$3'" >&2
109 exit 1; }
110 ;;
111 load)
112 rg -q "LOAD ERROR.*$3" "$log.clean" || {
113 echo "LEG $1: expected a LOAD failure mentioning '$3'" >&2
114 rg -n '✗|LOAD ERROR|Tests:' "$log.clean" >&2 || true
115 exit 1; }
116 ;;
117 *) echo "LEG $1: unknown mode '$2'" >&2; exit 1 ;;
118 esac
119 printf 'SD_P3_SABOTAGE_%s RED (%s) %s\n' "$1" "$2" "$(rg -o 'Tests:.*' "$log.clean" | head -1)"
122# A cheap corpus check: dump the in-repo manuscripts only and compare one
123# named manuscript's section hash against the committed baseline.
124corpus_hash() { # $1 = manuscript basename
125 local dump="$work/corpus-$1.txt"
126 setsid -w "$sigil" -L src -L test eval -f test/sd-p3-lowering-dump.sgl >"$dump" 2>&1
127 awk -v d="$work" -v want="$1" '/^=== /{name=$2} name==want{print > (d "/section-" want)}' "$dump"
128 sha256sum "$work/section-$1" | awk '{print $1}'
131# `.txt`, not `.sha256`: SD-P2's rename arm audits that everything under
132# `test/transcripts` is a .txt evidence record, so its directory exclusion can
133# never hide live source. See the merge notes in tasks/sd-p3-inline-commands.
134baseline=test/transcripts/sd-p3/lowering-before.txt
135baseline_hash() { awk -v n="$1" '$2==n{print $1}' "$baseline"; }
137echo "=== LEG 1 -- the NAMED sabotage: restore lift-to-block"
138# Every command becomes a block child again, which is exactly the pre-SD-P3
139# behaviour B4 describes.
140patch_file "$PARSER" \
141 '(define (block-position? state node)
142 (block-boundary? (parser-state-source state) (cadddr node)))' \
143 '(define (block-position? state node) #t)'
144# The fixtures cannot be BUILT without this phase: restoring lift-to-block
145# reproduces the exact error the pristine `acb9adf` tree gives the same fixture
146# (`PRE_SD_P3_BARE_PROSE_REJECTED` in the committed baseline).
147suite_red 01 load 'wrong semantic category'
148restore_all; verify_restored
150echo "=== LEG 2 -- fold a NON-STRUCTURAL command that sits at a block boundary"
151# The tempting-but-wrong rule, planted as a defect: someone decides an
152# admonition on its own line between two prose lines "obviously" belongs to the
153# paragraph. This is the rule SD-P3 deliberately did NOT adopt, and the test
154# that forbids it must be the thing that catches it -- so this leg is expected
155# to red AT THAT TEST, not to break the document wholesale.
156patch_file "$PARSER" \
157 '(define (block-position? state node)
158 (block-boundary? (parser-state-source state) (cadddr node)))' \
159 '(define (block-position? state node)
160 (and (not (eq? (car node) (quote aside)))
161 (block-boundary? (parser-state-source state) (cadddr node))))'
162suite_red 02 test 'block-boundary command between prose lines'
163restore_all; verify_restored
165echo "=== LEG 11 -- fold EVERY command, boundary or not"
166# The blunt version of leg 2. Inverting the boundary test entirely folds
167# `@section` into its chapter's prose run, so its children are lowered inline
168# and `make-section` refuses them: the document cannot be built at all. A
169# different and stronger fact than leg 2's, and it is asserted as a different
170# fact rather than accepted as "well, it went red".
171patch_file "$PARSER" \
172 '(define (block-position? state node)
173 (block-boundary? (parser-state-source state) (cadddr node)))' \
174 '(define (block-position? state node) #f)'
175suite_red 11 load 'make-section: child belongs to the wrong semantic category'
176restore_all; verify_restored
178echo "=== LEG 3 -- author-paragraph-run always makes a paragraph"
179# Drops the "no value is inline" branch, so `@tip{@p{...}}` nests a paragraph
180# inside a paragraph -- the defect that a static parser-side rule would have.
181patch_file "$RUNTIME" \
182 ' ((none inline-node? solid) (one-or-list solid))
183' \
184 ''
185suite_red 03 test 'run whose only value is a block node is spliced'
186restore_all; verify_restored
188echo "=== LEG 4 -- trim both edges of every run segment"
189patch_file "$PARSER" \
190 ' (if first? (trimmed-start state (caddr item) (cadddr item)) (caddr item))' \
191 ' (trimmed-start state (caddr item) (cadddr item))'
192suite_red 04 test 'equals the @p-wrapped spelling'
193restore_all; verify_restored
195echo "=== LEG 5 -- a run paragraph spans only its first item"
196patch_file "$PARSER" \
197 ' (let ((start (car (car kept)))
198 (end (cadr (car (reverse kept)))))' \
199 ' (let ((start (car (car kept)))
200 (end (cadr (car kept))))'
201suite_red 05 test 'exact manuscript location'
202restore_all; verify_restored
204echo "=== LEG 6 -- a blank line no longer closes a run"
205patch_file "$PARSER" \
206 ' ((and (char=? (string-ref source i) #\newline)
207 (< (+ i 1) end) (char=? (string-ref source (+ i 1)) #\newline))
208 (loop (+ i 2) (+ i 2) (cons (list part i) out)))' \
209 ' ((and #f (char=? (string-ref source i) #\newline)
210 (< (+ i 1) end) (char=? (string-ref source (+ i 1)) #\newline))
211 (loop (+ i 2) (+ i 2) (cons (list part i) out)))'
212suite_red 06 test 'blank line still closes a run'
213restore_all; verify_restored
215echo "=== LEG 7 -- ASSERTION layer: an expected count is wrong"
216patch_file "$SUITE" \
217 ' (assert-equal 2 (count-tag bare-datum '"'"'code))' \
218 ' (assert-equal 3 (count-tag bare-datum '"'"'code))'
219suite_red 07 test 'equals the @p-wrapped spelling'
220restore_all; verify_restored
222echo "=== LEG 8 -- neuter ONLY the equality arm's input"
223# `strip-locations` returns nothing, so the equality compares two empty values
224# and passes for free. The DENOMINATORS are what must catch it. If this leg
225# came back green, the central arm of this phase would be vacuous.
227# It also reddens "the comparator can see a difference when one exists", which
228# calls the same procedure -- so this neuters the equality arm's input plus its
229# own control, not the equality arm alone.
230patch_file "$SUITE" \
231 '(define (strip-locations value)
232 (cond' \
233 '(define (strip-locations value)
234 (cond
235 ((pair? value) '"'"'())'
236suite_red 08 test 'equals the @p-wrapped spelling'
237restore_all; verify_restored
239echo "=== LEG 9 -- product layer, corpus instrument: fold nothing at all"
240# The corpus arm has its own product-layer red: with no folding the lowering
241# reverts to the baseline, so a manuscript the census flagged must STOP
242# differing from it. A gate that only ever asks "did it change?" cannot notice.
243before=$(baseline_hash minimal.sgd)
244test -n "$before" || { echo "no baseline hash for minimal.sgd" >&2; exit 1; }
245after=$(corpus_hash minimal.sgd)
246test "$after" != "$before" || {
247 echo "minimal.sgd already matches the baseline before sabotage; the probe is blind" >&2
248 exit 1; }
249patch_file "$PARSER" \
250 '(define (block-position? state node)
251 (block-boundary? (parser-state-source state) (cadddr node)))' \
252 '(define (block-position? state node) #t)'
253sabotaged=$(corpus_hash minimal.sgd)
254restore_all; verify_restored
255if [ "$sabotaged" != "$before" ]; then
256 echo "LEG 09: lift-to-block did NOT reproduce the pre-SD-P3 lowering of minimal.sgd" >&2
257 echo " baseline=$before sabotaged=$sabotaged after=$after" >&2
258 exit 1
259fi
260printf 'SD_P3_SABOTAGE_09 RED minimal.sgd reverts to the exact pre-SD-P3 hash under lift-to-block\n'
262echo "=== LEG 10 -- rebuild a run's prose value from the source slice"
263# The defect adversarial review found: a document escape is a text node whose
264# VALUE is one character while its SPAN covers two, so re-deriving the value
265# from the slice turns `@@` back into two at-signs and the bare spelling stops
266# matching the `@p{...}` one.
267patch_file "$PARSER" \
268 ' (if (and (= start (cadddr node)) (= end (fifth node)))
269 (caddr node)
270 (fold-prose (substring (parser-state-source state) start end))))' \
271 ' (fold-prose (substring (parser-state-source state) start end)))'
272suite_red 10 test 'equals the @p-wrapped spelling'
273restore_all; verify_restored
275echo "=== LEG 12 -- MERGE LAYER: a url-link is not treated as inline"
276# THE MERGE-SABOTAGE. Legs 1-11 exercise properties that existed on this
277# phase's own branch. This one exercises a property that exists ONLY on the
278# merged tree: SD-P2 added `@url` as an inline node, SD-P3 folds non-boundary
279# commands into paragraph runs, and neither phase's gate covered the
280# combination.
282# The bar it answers comes from SD-P2's near-miss: an arm can stay green on two
283# independent trees while asserting nothing. Planting a defect that only the
284# cross-phase test can see is what distinguishes "the merged tree is green"
285# from "the merged tree is green and something would notice if it were not".
287# The plant: treat a url-link as NOT inline, so a run containing one becomes
288# "mixed" and is refused.
290# EXPECTED MODE IS `load`, AND THE MEASUREMENT CORRECTED THE PREDICTION. I
291# predicted a `test`-mode red at the dedicated cross-phase test. What happens is
292# stronger: `test/fixtures/sd-p3-bare-prose.sgd` itself carries an `@url`
293# mid-paragraph, so the fixture cannot be BUILT under the plant and the suite
294# dies at load. The red therefore proves the cross-phase property is load-
295# bearing for the equality FIXTURE, not merely for a synthetic string inside one
296# test -- so the fragment pins the fixture and the exact position rather than a
297# test name.
298patch_file "$RUNTIME" \
299 ' ((every inline-node? flat) (make-paragraph flat loc))' \
300 ' ((every (lambda (n) (and (inline-node? n) (not (url-link? n)))) flat)
301 (make-paragraph flat loc))'
302suite_red 12 load 'sd-p3-bare-prose.sgd:6:1'
303restore_all; verify_restored
305verify_restored
306echo "SD_P3_SABOTAGE_COMPLETE 12 legs red and restored"