AtlatestRepositorysigil-doc
1
#!/usr/bin/env bash2
# SD-P3 sabotage driver.3
#4
# A gate that cannot demonstrate it fails is not a gate. Each leg below plants a5
# specific defect, proves the corresponding check goes RED, restores the file and6
# verifies the restoration BY CONTENT HASH -- never by the exit code of the7
# 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 a10
# red proves the suite executes the loaded product rather than a mirror of11
# 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 arm13
# 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 to16
# fail in two different ways. Leg 12 is the MERGE-SABOTAGE: it breaks a17
# property that only exists once SD-P2 and SD-P3 are both present.18
#19
# Restoration is a TRAP, not a step that must be reached: a leg that exits early20
# would otherwise leave the corrupted file in the worktree.21
set -euo pipefail22
repo_dir="$(cd "$(dirname "$0")/.." && pwd)"23
cd "$repo_dir"24
sigil="${SIGIL_D1_COMPILER:-$repo_dir/../task-sigil-language-readers/build/dev/bin/sigil}"25
export SIGIL_REDIRECTS="${SIGIL_D1_REDIRECTS:-$repo_dir/dev-redirects.sgl}"26
mkdir -p "$repo_dir/build"27
work=$(mktemp -d "$repo_dir/build/sd-p3-sabotage.XXXXXX")28
export XDG_CACHE_HOME="$work/cache"30
for tool in rg sha256sum awk sed python3; do31
command -v "$tool" >/dev/null || { echo "$tool is required" >&2; exit 1; }32
done33
test -x "$sigil" || { echo "dev compiler not found at $sigil" >&2; exit 1; }35
PARSER=src/sigil/doc/private/parser.sgl36
RUNTIME=src/sigil/doc/private/author-runtime.sgl37
SUITE=test/test-prose-runs.sgl38
GUARDED=("$PARSER" "$RUNTIME" "$SUITE")40
pristine="$work/pristine"41
mkdir -p "$pristine"42
for f in "${GUARDED[@]}"; do43
cp "$f" "$pristine/$(basename "$f")"44
done46
restore_all() {47
for f in "${GUARDED[@]}"; do48
cp "$pristine/$(basename "$f")" "$f"49
done50
}51
trap restore_all EXIT INT TERM53
verify_restored() {54
for f in "${GUARDED[@]}"; do55
a=$(sha256sum "$f" | awk '{print $1}')56
b=$(sha256sum "$pristine/$(basename "$f")" | awk '{print $1}')57
if [ "$a" != "$b" ]; then58
echo "RESTORE FAILED for $f ($a != $b)" >&2; exit 159
fi60
done61
}63
# `python3 - <file> <old> <new>`: an exact, unambiguous substring replacement64
# that refuses if the text is absent or appears more than once. A sed that65
# silently matches nothing produces a leg that tests the pristine code and66
# reports a convincing green.67
patch_file() {68
python3 - "$1" "$2" "$3" <<'PY'69
import sys70
path, old, new = sys.argv[1], sys.argv[2], sys.argv[3]71
s = open(path).read()72
n = s.count(old)73
if n != 1:74
sys.stderr.write(f"patch target appears {n} times in {path}; refusing\n")75
sys.exit(1)76
open(path, "w").write(s.replace(old, new))77
PY78
}80
# A red is not a red is not a red. There are two ways this suite can fail and81
# they are DIFFERENT FACTS, so each leg declares which one it expects and a leg82
# that fails the other way is treated as a failure of the leg:83
#84
# test <fragment> the suite RAN and the named test failed. Anchored on the85
# `✗` marker, because the runner echoes every test's name as86
# it runs -- pass or fail -- so a bare grep for the name87
# matches even when the suite went red somewhere else.88
# load <fragment> the suite could not LOAD, because the sabotage makes the89
# fixtures themselves unbuildable. That is the strongest red90
# available for leg 1, and it must not be mistaken for the91
# suite exploding on an unrelated error.92
suite_red() { # $1 = leg name, $2 = mode (test|load), $3 = expected fragment93
local log="$work/$1.log"94
if setsid -w "$sigil" -L src -L test test "$SUITE" >"$log" 2>&1; then95
echo "LEG $1: the suite PASSED under sabotage; the check cannot fail" >&296
tail -20 "$log" >&297
exit 198
fi99
sed -e 's/\x1b\[[0-9;]*m//g' "$log" >"$log.clean"100
case "$2" in101
test)102
rg -q "^\\s*✗\\s+.*$3" "$log.clean" || {103
echo "LEG $1: expected the suite to RUN and fail at '$3'" >&2104
rg -n '✗|LOAD ERROR|Tests:' "$log.clean" >&2 || true105
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'" >&2109
exit 1; }110
;;111
load)112
rg -q "LOAD ERROR.*$3" "$log.clean" || {113
echo "LEG $1: expected a LOAD failure mentioning '$3'" >&2114
rg -n '✗|LOAD ERROR|Tests:' "$log.clean" >&2 || true115
exit 1; }116
;;117
*) echo "LEG $1: unknown mode '$2'" >&2; exit 1 ;;118
esac119
printf 'SD_P3_SABOTAGE_%s RED (%s) %s\n' "$1" "$2" "$(rg -o 'Tests:.*' "$log.clean" | head -1)"120
}122
# A cheap corpus check: dump the in-repo manuscripts only and compare one123
# named manuscript's section hash against the committed baseline.124
corpus_hash() { # $1 = manuscript basename125
local dump="$work/corpus-$1.txt"126
setsid -w "$sigil" -L src -L test eval -f test/sd-p3-lowering-dump.sgl >"$dump" 2>&1127
awk -v d="$work" -v want="$1" '/^=== /{name=$2} name==want{print > (d "/section-" want)}' "$dump"128
sha256sum "$work/section-$1" | awk '{print $1}'129
}131
# `.txt`, not `.sha256`: SD-P2's rename arm audits that everything under132
# `test/transcripts` is a .txt evidence record, so its directory exclusion can133
# never hide live source. See the merge notes in tasks/sd-p3-inline-commands.134
baseline=test/transcripts/sd-p3/lowering-before.txt135
baseline_hash() { awk -v n="$1" '$2==n{print $1}' "$baseline"; }137
echo "=== LEG 1 -- the NAMED sabotage: restore lift-to-block"138
# Every command becomes a block child again, which is exactly the pre-SD-P3139
# behaviour B4 describes.140
patch_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-block145
# reproduces the exact error the pristine `acb9adf` tree gives the same fixture146
# (`PRE_SD_P3_BARE_PROSE_REJECTED` in the committed baseline).147
suite_red 01 load 'wrong semantic category'148
restore_all; verify_restored150
echo "=== 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 an152
# admonition on its own line between two prose lines "obviously" belongs to the153
# paragraph. This is the rule SD-P3 deliberately did NOT adopt, and the test154
# that forbids it must be the thing that catches it -- so this leg is expected155
# to red AT THAT TEST, not to break the document wholesale.156
patch_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))))'162
suite_red 02 test 'block-boundary command between prose lines'163
restore_all; verify_restored165
echo "=== LEG 11 -- fold EVERY command, boundary or not"166
# The blunt version of leg 2. Inverting the boundary test entirely folds167
# `@section` into its chapter's prose run, so its children are lowered inline168
# and `make-section` refuses them: the document cannot be built at all. A169
# different and stronger fact than leg 2's, and it is asserted as a different170
# fact rather than accepted as "well, it went red".171
patch_file "$PARSER" \172
'(define (block-position? state node)173
(block-boundary? (parser-state-source state) (cadddr node)))' \174
'(define (block-position? state node) #f)'175
suite_red 11 load 'make-section: child belongs to the wrong semantic category'176
restore_all; verify_restored178
echo "=== LEG 3 -- author-paragraph-run always makes a paragraph"179
# Drops the "no value is inline" branch, so `@tip{@p{...}}` nests a paragraph180
# inside a paragraph -- the defect that a static parser-side rule would have.181
patch_file "$RUNTIME" \182
' ((none inline-node? solid) (one-or-list solid))183
' \184
''185
suite_red 03 test 'run whose only value is a block node is spliced'186
restore_all; verify_restored188
echo "=== LEG 4 -- trim both edges of every run segment"189
patch_file "$PARSER" \190
' (if first? (trimmed-start state (caddr item) (cadddr item)) (caddr item))' \191
' (trimmed-start state (caddr item) (cadddr item))'192
suite_red 04 test 'equals the @p-wrapped spelling'193
restore_all; verify_restored195
echo "=== LEG 5 -- a run paragraph spans only its first item"196
patch_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))))'201
suite_red 05 test 'exact manuscript location'202
restore_all; verify_restored204
echo "=== LEG 6 -- a blank line no longer closes a run"205
patch_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)))'212
suite_red 06 test 'blank line still closes a run'213
restore_all; verify_restored215
echo "=== LEG 7 -- ASSERTION layer: an expected count is wrong"216
patch_file "$SUITE" \217
' (assert-equal 2 (count-tag bare-datum '"'"'code))' \218
' (assert-equal 3 (count-tag bare-datum '"'"'code))'219
suite_red 07 test 'equals the @p-wrapped spelling'220
restore_all; verify_restored222
echo "=== LEG 8 -- neuter ONLY the equality arm's input"223
# `strip-locations` returns nothing, so the equality compares two empty values224
# and passes for free. The DENOMINATORS are what must catch it. If this leg225
# came back green, the central arm of this phase would be vacuous.226
#227
# It also reddens "the comparator can see a difference when one exists", which228
# calls the same procedure -- so this neuters the equality arm's input plus its229
# own control, not the equality arm alone.230
patch_file "$SUITE" \231
'(define (strip-locations value)232
(cond' \233
'(define (strip-locations value)234
(cond235
((pair? value) '"'"'())'236
suite_red 08 test 'equals the @p-wrapped spelling'237
restore_all; verify_restored239
echo "=== LEG 9 -- product layer, corpus instrument: fold nothing at all"240
# The corpus arm has its own product-layer red: with no folding the lowering241
# reverts to the baseline, so a manuscript the census flagged must STOP242
# differing from it. A gate that only ever asks "did it change?" cannot notice.243
before=$(baseline_hash minimal.sgd)244
test -n "$before" || { echo "no baseline hash for minimal.sgd" >&2; exit 1; }245
after=$(corpus_hash minimal.sgd)246
test "$after" != "$before" || {247
echo "minimal.sgd already matches the baseline before sabotage; the probe is blind" >&2248
exit 1; }249
patch_file "$PARSER" \250
'(define (block-position? state node)251
(block-boundary? (parser-state-source state) (cadddr node)))' \252
'(define (block-position? state node) #t)'253
sabotaged=$(corpus_hash minimal.sgd)254
restore_all; verify_restored255
if [ "$sabotaged" != "$before" ]; then256
echo "LEG 09: lift-to-block did NOT reproduce the pre-SD-P3 lowering of minimal.sgd" >&2257
echo " baseline=$before sabotaged=$sabotaged after=$after" >&2258
exit 1259
fi260
printf 'SD_P3_SABOTAGE_09 RED minimal.sgd reverts to the exact pre-SD-P3 hash under lift-to-block\n'262
echo "=== 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 whose264
# VALUE is one character while its SPAN covers two, so re-deriving the value265
# from the slice turns `@@` back into two at-signs and the bare spelling stops266
# matching the `@p{...}` one.267
patch_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)))'272
suite_red 10 test 'equals the @p-wrapped spelling'273
restore_all; verify_restored275
echo "=== LEG 12 -- MERGE LAYER: a url-link is not treated as inline"276
# THE MERGE-SABOTAGE. Legs 1-11 exercise properties that existed on this277
# phase's own branch. This one exercises a property that exists ONLY on the278
# merged tree: SD-P2 added `@url` as an inline node, SD-P3 folds non-boundary279
# commands into paragraph runs, and neither phase's gate covered the280
# combination.281
#282
# The bar it answers comes from SD-P2's near-miss: an arm can stay green on two283
# independent trees while asserting nothing. Planting a defect that only the284
# 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".286
#287
# The plant: treat a url-link as NOT inline, so a run containing one becomes288
# "mixed" and is refused.289
#290
# EXPECTED MODE IS `load`, AND THE MEASUREMENT CORRECTED THE PREDICTION. I291
# predicted a `test`-mode red at the dedicated cross-phase test. What happens is292
# 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 suite294
# 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 one296
# test -- so the fragment pins the fixture and the exact position rather than a297
# test name.298
patch_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))'302
suite_red 12 load 'sd-p3-bare-prose.sgd:6:1'303
restore_all; verify_restored305
verify_restored306
echo "SD_P3_SABOTAGE_COMPLETE 12 legs red and restored"