AtlatestRepositorysigil-doc
1
#!/usr/bin/env bash2
# Regenerate the SD-P3 BEFORE baseline from a pristine pre-SD-P3 checkout.3
#4
# The baseline is a per-manuscript sha256 of the lowered form sequence. It has5
# to come from a tree that does NOT contain this phase's change, and proving6
# that is the whole difficulty:7
#8
# `sigil -L <dir>` does NOT shadow a package's own `src/`9
# (topics/sigil-load-path-does-not-shadow-package-src). A "before" run driven10
# with -L from this worktree silently measures the CURRENT parser and produces11
# a baseline identical to the after, which reads as "nothing changed".12
#13
# So the run happens with its cwd INSIDE a detached pristine worktree, and the14
# baseline carries a POSITIVE CONTROL proving that tree could not resolve15
# `author-paragraph-run` -- the binding this phase adds. The gate refuses to16
# compare against a baseline missing that line.17
#18
# The control is asserted in BOTH directions: absent in the pristine tree AND19
# present in this one. An "is it absent?" probe that is simply broken reports20
# absent everywhere.21
set -euo pipefail22
repo_dir="$(cd "$(dirname "$0")/.." && pwd)"23
cd "$repo_dir"24
# The base is the commit this branch sits on. It moved from `acb9adf` to25
# `4dcb41d0` when SD-P2 merged, and the BEFORE baseline has to move with it:26
# a byte-identity claim is only meaningful against the tree this phase is27
# actually a delta from.28
base_ref="${SD_P3_BASE_REF:-4dcb41d0}"29
sigil="${SIGIL_D1_COMPILER:-$repo_dir/../task-sigil-language-readers/build/dev/bin/sigil}"31
for tool in git sha256sum awk sed; do32
command -v "$tool" >/dev/null || { echo "$tool is required" >&2; exit 1; }33
done34
test -x "$sigil" || { echo "dev compiler not found at $sigil" >&2; exit 1; }36
work="$(mktemp -d "${TMPDIR:-/tmp}/sd-p3-before.XXXXXX")"37
pristine="$work/pristine"38
cleanup() {39
git -C "$repo_dir" worktree remove --force "$pristine" >/dev/null 2>&1 || true40
rm -rf "$work"41
}42
# Unconditional, not merely reached: an early exit between `worktree add` and43
# the end of the script would otherwise leave a stray worktree behind.44
trap cleanup EXIT INT TERM46
git -C "$repo_dir" worktree add --detach "$pristine" "$base_ref" >/dev/null48
# The pristine tree predates these two scripts, so they are copied in. Copying49
# the SAME dump script into both sides is what makes the two sides comparable.50
cp "$repo_dir/test/sd-p3-lowering-dump.sgl" "$pristine/test/"52
# `dev-redirects.sgl` resolves `../sigil` relative to the package root, and the53
# pristine worktree is not a sibling of that checkout. Write an absolute one.54
sigil_repo="$(cd "$repo_dir/../sigil" && pwd)"55
sxml_repo="$(cd "$repo_dir/../sigil-sxml" && pwd)"56
cat >"$work/redirects.sgl" <<EOF57
(redirects58
repos: (list59
(for-repo url: "codeberg:sigil/sigil"60
use: (from-path dir: "$sigil_repo"))61
(for-repo url: "codeberg:sigil/sigil-sxml"62
use: (from-path dir: "$sxml_repo"))))63
EOF65
probe="$work/control-probe.sgl"66
cat >"$probe" <<'EOF'67
(import (sigil core) (sigil doc private author-runtime))68
(if (procedure? author-paragraph-run)69
(begin (display "RESOLVED") (newline))70
(begin (display "NOT-A-PROCEDURE") (newline)))71
EOF73
run_probe() { # $1 = package root74
(cd "$1" && \75
SIGIL_REDIRECTS="$work/redirects.sgl" XDG_CACHE_HOME="$work/cache-$2" \76
"$sigil" -L src eval -f "$probe" 2>&1 || true)77
}79
pristine_probe="$(run_probe "$pristine" pristine)"80
current_probe="$(run_probe "$repo_dir" current)"82
# Direction 1: the pristine tree must NOT resolve the binding.83
if printf '%s' "$pristine_probe" | grep -q 'RESOLVED'; then84
echo "the pristine tree at $base_ref RESOLVES author-paragraph-run; it is not pre-SD-P3" >&285
exit 186
fi87
# Direction 2 -- the positive control on the control. Without it, a probe that88
# fails for an unrelated reason (bad redirects, missing compiler) reports89
# "absent" against every tree and the baseline means nothing.90
if ! printf '%s' "$current_probe" | grep -q 'RESOLVED'; then91
echo "the probe cannot see author-paragraph-run even in THIS tree; it is broken, not measuring" >&292
printf '%s\n' "$current_probe" >&293
exit 194
fi96
# THE PREMISE. This phase exists because a mid-paragraph inline command in bare97
# prose is rejected. Record that the fixture really is rejected by the tree that98
# predates the change -- otherwise "the fixture passes now" is compatible with a99
# fixture that would have passed all along.100
#101
# Unlike SD-P1's withdrawn criterion, this one is meaningful: the bare-prose102
# fixture uses no binding the pristine tree lacks, so it fails at EVALUATION on103
# the behaviour under test rather than at compile time on an absent name.104
premise="$work/premise.sgl"105
cat >"$premise" <<'EOF'106
;; The generated forms reference `author-at`, `author-normalize` and the model107
;; constructors, which the READER's prelude normally imports. A probe that omits108
;; them fails with `unbound variable 'author-at'` -- which is still a rejection,109
;; and would be recorded as one by any check that merely looked for failure. The110
;; `case` below therefore demands the B4 REASON, not just a non-zero outcome,111
;; and it caught exactly this on the first run.112
(import (sigil core) (sigil fs) (sigil language)113
(sigil doc) (sigil doc author) (sigil doc private author-runtime)114
(sigil doc private parser))115
(define source (read-file-string "test/fixtures/sd-p3-bare-prose.sgd"))116
(define header (inspect-language-source source "sd-p3-bare-prose.sgd"))117
(display118
(guard (exn (else (string-append "REJECTED " (exception-message exn))))119
(begin (eval (parse-document-source source "sd-p3-bare-prose.sgd"120
(language-header-end header)))121
"ACCEPTED")))122
(newline)123
EOF124
cp "$repo_dir/test/fixtures/sd-p3-bare-prose.sgd" "$pristine/test/fixtures/"125
premise_result="$(cd "$pristine" && \126
SIGIL_REDIRECTS="$work/redirects.sgl" XDG_CACHE_HOME="$work/cache-pristine" \127
"$sigil" -L src -L test eval -f "$premise" 2>&1 || true)"128
case "$premise_result" in129
*"REJECTED"*"wrong semantic category"*) ;;130
*) echo "the bare-prose fixture was NOT rejected at $base_ref for the B4 reason:" >&2131
printf '%s\n' "$premise_result" >&2132
exit 1 ;;133
esac135
dump="$work/before.txt"136
(cd "$pristine" && \137
SIGIL_REDIRECTS="$work/redirects.sgl" XDG_CACHE_HOME="$work/cache-pristine" \138
SD_P3_COURSE_DIR="${SD_P3_COURSE_DIR:-}" \139
"$sigil" -L src -L test eval -f test/sd-p3-lowering-dump.sgl) >"$dump"141
grep -q 'SD_P3_LOWERING_DUMP_COMPLETE' "$dump" || {142
echo "the pristine dump printed no completion marker" >&2; exit 1; }144
out="$repo_dir/test/transcripts/sd-p3/lowering-before.txt"145
mkdir -p "$(dirname "$out")"146
{147
echo "# SD-P3 BEFORE baseline: per-manuscript sha256 of the lowered form sequence."148
echo "# Produced by test/sd-p3-before-transcript.sh from a detached worktree at $base_ref,"149
echo "# with its cwd INSIDE that worktree (sigil -L does not shadow a package's own src/)."150
echo "PRE_SD_P3_CONTROL_UNBOUND author-paragraph-run does not resolve at $base_ref"151
echo "PRE_SD_P3_CONTROL_RESOLVES author-paragraph-run resolves in the working tree"152
printf 'PRE_SD_P3_BARE_PROSE_REJECTED %s\n' "$(printf '%s' "$premise_result" | tr '\n' ' ')"153
# The completion marker is excluded from the sections: it names the corpus154
# size, so including it would tie the LAST manuscript's hash to how many155
# manuscripts were dumped.156
awk '/^SD_P3_LOWERING_DUMP_COMPLETE/{next} /^=== /{name=$2} {print > ("'"$work"'/section-" (name==""?"header":name))}' "$dump"157
for f in "$work"/section-*; do158
name="${f##*/section-}"159
if [ "$name" = "header" ]; then continue; fi160
printf '%s %s\n' "$(sha256sum "$f" | awk '{print $1}')" "$name"161
done162
grep 'SD_P3_LOWERING_DUMP_COMPLETE' "$dump"163
} >"$out"165
echo "SD_P3_BEFORE_BASELINE_WRITTEN $out"166
awk '/^[0-9a-f]{64} /{n++} END{print "manuscripts hashed:", n}' "$out"