AtlatestRepositorysigil-doc
1
#!/usr/bin/env bash2
# SD-P2 / D13 rename-completeness arm, standalone so it can be sabotaged and3
# re-run on its own without a half-hour compile.4
#5
# This is an ABSENCE assertion, which is the cheapest kind of gate to get wrong:6
# it passes for free when the search is pointed at nothing, when the pattern is7
# subtly wrong, or when the tool is missing. So the arm proves three things8
# before it is allowed to mean anything:9
#10
# 1. the searcher exists at all;11
# 2. the file population it searches is non-empty and roughly the expected size;12
# 3. the searcher CAN see the old spelling, demonstrated against a planted13
# occurrence, immediately before asserting the old spelling is absent.14
#15
# Without (3) this arm would report the rename complete on a machine where the16
# pattern had rotted, which is precisely how a partial rename ships looking fine.17
set -euo pipefail19
repo_dir="$(cd "$(dirname "$0")/.." && pwd)"20
cd "$repo_dir"21
command -v rg >/dev/null || { echo "gate-sd-p2-rename: rg is missing" >&2; exit 1; }22
work="$(mktemp -d)"23
trap 'rm -rf "$work"' EXIT25
roots=(src test book docs tools)26
# This script and the sabotage harness both CONTAIN the old spelling, in their27
# own search pattern and their own planted-occurrence fixture. They are the only28
# three entries excluded, each of which MUST contain the old spelling by29
# construction, named here rather than matched by a wildcard, with the count30
# asserted so the list cannot be quietly widened into a hiding place:31
#32
# - this script, which carries the old spelling in its own search pattern;33
# - the sabotage harness, which carries it in its planted defects;34
# - test/transcripts, the committed sabotage evidence, whose whole purpose is35
# to record runs in which the old spelling WAS present.36
#37
# Excluding a directory is a bigger claim than excluding a file, so it is38
# audited below rather than trusted: everything under it must be a .txt39
# evidence record, so it can never become a hiding place for live source.40
excluded=(test/gate-sd-p2-rename.sh test/sabotage-sd-p2.sh test/transcripts)41
test "${#excluded[@]}" -eq 3 \42
|| { echo "gate-sd-p2-rename: exclusion list has grown; that is how a rename hides" >&2; exit 1; }43
non_evidence="$(rg --files test/transcripts | rg -v '\.txt$' || true)"44
test -z "$non_evidence" \45
|| { echo "gate-sd-p2-rename: non-evidence files under the excluded transcripts directory:" >&246
echo "$non_evidence" >&247
echo "the exclusion would hide live source; refusing" >&2; exit 1; }48
globs=()49
for path in "${excluded[@]}"; do globs+=(--glob "!$path" --glob "!$path/**"); done51
population="$(rg --files "${globs[@]}" "${roots[@]}" | wc -l | tr -d ' ')"52
test "$population" -gt 30 \53
|| { echo "gate-sd-p2-rename: search population is $population; refusing to assert an absence" >&2; exit 1; }55
# WHAT THIS ARM ACTUALLY CLAIMS: the ADMONITION is no longer spelled `note`.56
# NOT that the token `note` is absent - D13 renamed the admonition precisely so57
# that `@note` could become the FOLIO ROOT CONTAINER, so `@note` reappearing as a58
# root is the rename SUCCEEDING. SD-P1 and SD-P4 landed exactly that on master59
# (test/fixtures/sd-p1-folio-roots.sgd, test/fixtures/sd-p4-note-root.sgd,60
# test/test-root-profile.sgl, docs/root-profiles.md), and an arm asserting the61
# bare token would go red on 28 legitimate occurrences.62
#63
# So the patterns below discriminate the admonition from the container:64
#65
# - `make-admonition 'note` the admonition kind, unambiguous66
# - `(memq kind '(note` the model's kind allowlist. The bare `'(note`67
# pattern is NOT usable: master legitimately68
# carries `'(note project)` for root profiles,69
# which is a different vocabulary70
# - `(define (note ` the author binding71
#72
# and in manuscripts only, `@note` NOT preceded by `@` (which would be an escaped73
# `@@note` inside `@code`) and NOT followed by `[` (a folio root always takes74
# ["id" ...]; an admonition never does).75
#76
# `@end note` is deliberately NOT forbidden: the folio root and the admonition77
# share that delimiter, so it stopped being a discriminator the moment the78
# container landed.79
code_spelling="make-admonition 'note|\(memq kind '\(note|\(define \(note "80
manuscript_spelling='(^|[^@])@note([^\[]|$)'82
# The searcher must be shown to SEE each spelling before its absence means83
# anything, and each planted line must be seen by the pattern that owns it.84
printf '%s\n' "(make-admonition 'note #f children loc)" \85
"(memq kind '(note tip warning))" \86
"(define (note . body) ...)" >"$work/planted-code.txt"87
printf '%s\n' '@note' '@note{body}' >"$work/planted-manuscript.sgd"88
code_hits="$(rg -c -e "$code_spelling" "$work/planted-code.txt" || true)"89
test "$code_hits" = "3" \90
|| { echo "gate-sd-p2-rename: code searcher saw $code_hits/3 planted occurrences; it cannot be trusted to report an absence" >&2; exit 1; }91
manuscript_hits="$(rg -c -e "$manuscript_spelling" "$work/planted-manuscript.sgd" || true)"92
test "$manuscript_hits" = "2" \93
|| { echo "gate-sd-p2-rename: manuscript searcher saw $manuscript_hits/2 planted occurrences; it cannot be trusted to report an absence" >&2; exit 1; }94
# and it must NOT see the folio container or an escaped mention, or it would95
# report a false positive on legitimate SD-P1/SD-P4 vocabulary96
printf '%s\n' '@note["topics/alpha" title: "Alpha"]' '@code{@@note}' '@end note' \97
>"$work/legitimate.sgd"98
if rg -q -e "$manuscript_spelling" "$work/legitimate.sgd"; then99
echo "gate-sd-p2-rename: the manuscript pattern matches the folio root container; it would forbid what D13 was for" >&2100
rg -n -e "$manuscript_spelling" "$work/legitimate.sgd" >&2101
exit 1102
fi104
# THE ABSENCE ASSERTIONS THEMSELVES. Everything above only establishes that the105
# searchers work; these two are what actually inspect the tree. An earlier106
# revision of this script lost these blocks in an edit and still printed107
# RENAME_COMPLETE - green on its own branch AND green on the merged tree, both108
# meaningless, because the arm was asserting nothing. The rename sabotage legs109
# are what caught it.110
if rg -n "${globs[@]}" -e "$code_spelling" "${roots[@]}"; then111
echo 'gate-sd-p2-rename: the admonition kind or binding is still spelled note above' >&2112
exit 1113
fi114
if rg -n "${globs[@]}" --glob '*.sgd' -e "$manuscript_spelling" "${roots[@]}"; then115
echo 'gate-sd-p2-rename: a manuscript still uses @note as an admonition above' >&2116
exit 1117
fi119
# The renamed binding and kind must be PRESENT, not merely the old ones absent.120
rg -q "\(define \(aside " src/sigil/doc/author.sgl \121
|| { echo "gate-sd-p2-rename: author.sgl does not define aside" >&2; exit 1; }122
rg -q "memq kind '\(aside" src/sigil/doc/model.sgl \123
|| { echo "gate-sd-p2-rename: model.sgl kind allowlist does not carry aside" >&2; exit 1; }125
# The new spelling must actually be present, or an empty tree would pass.126
aside_files="$(rg -l "${globs[@]}" -e '@aside|@end aside|'"'"'aside' "${roots[@]}" | wc -l | tr -d ' ')"127
test "$aside_files" -ge 3 \128
|| { echo "gate-sd-p2-rename: expected the new spelling in at least 3 files, found $aside_files" >&2; exit 1; }130
echo "SD_P2_04_RENAME_COMPLETE population=$population planted=code-3/3+manuscript-2/2+container-not-matched aside_files=$aside_files excluded=${excluded[*]} transcripts-audited=evidence-only"