AtlatestRepositorysigil-doc

sigil-doc / tree / testsd-p3-before-transcript.sh

1#!/usr/bin/env bash
2# 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 has
5# to come from a tree that does NOT contain this phase's change, and proving
6# 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 driven
10# with -L from this worktree silently measures the CURRENT parser and produces
11# a baseline identical to the after, which reads as "nothing changed".
13# So the run happens with its cwd INSIDE a detached pristine worktree, and the
14# baseline carries a POSITIVE CONTROL proving that tree could not resolve
15# `author-paragraph-run` -- the binding this phase adds. The gate refuses to
16# compare against a baseline missing that line.
18# The control is asserted in BOTH directions: absent in the pristine tree AND
19# present in this one. An "is it absent?" probe that is simply broken reports
20# absent everywhere.
21set -euo pipefail
22repo_dir="$(cd "$(dirname "$0")/.." && pwd)"
23cd "$repo_dir"
24# The base is the commit this branch sits on. It moved from `acb9adf` to
25# `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 is
27# actually a delta from.
28base_ref="${SD_P3_BASE_REF:-4dcb41d0}"
29sigil="${SIGIL_D1_COMPILER:-$repo_dir/../task-sigil-language-readers/build/dev/bin/sigil}"
31for tool in git sha256sum awk sed; do
32 command -v "$tool" >/dev/null || { echo "$tool is required" >&2; exit 1; }
33done
34test -x "$sigil" || { echo "dev compiler not found at $sigil" >&2; exit 1; }
36work="$(mktemp -d "${TMPDIR:-/tmp}/sd-p3-before.XXXXXX")"
37pristine="$work/pristine"
38cleanup() {
39 git -C "$repo_dir" worktree remove --force "$pristine" >/dev/null 2>&1 || true
40 rm -rf "$work"
42# Unconditional, not merely reached: an early exit between `worktree add` and
43# the end of the script would otherwise leave a stray worktree behind.
44trap cleanup EXIT INT TERM
46git -C "$repo_dir" worktree add --detach "$pristine" "$base_ref" >/dev/null
48# The pristine tree predates these two scripts, so they are copied in. Copying
49# the SAME dump script into both sides is what makes the two sides comparable.
50cp "$repo_dir/test/sd-p3-lowering-dump.sgl" "$pristine/test/"
52# `dev-redirects.sgl` resolves `../sigil` relative to the package root, and the
53# pristine worktree is not a sibling of that checkout. Write an absolute one.
54sigil_repo="$(cd "$repo_dir/../sigil" && pwd)"
55sxml_repo="$(cd "$repo_dir/../sigil-sxml" && pwd)"
56cat >"$work/redirects.sgl" <<EOF
57(redirects
58 repos: (list
59 (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"))))
63EOF
65probe="$work/control-probe.sgl"
66cat >"$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)))
71EOF
73run_probe() { # $1 = package root
74 (cd "$1" && \
75 SIGIL_REDIRECTS="$work/redirects.sgl" XDG_CACHE_HOME="$work/cache-$2" \
76 "$sigil" -L src eval -f "$probe" 2>&1 || true)
79pristine_probe="$(run_probe "$pristine" pristine)"
80current_probe="$(run_probe "$repo_dir" current)"
82# Direction 1: the pristine tree must NOT resolve the binding.
83if printf '%s' "$pristine_probe" | grep -q 'RESOLVED'; then
84 echo "the pristine tree at $base_ref RESOLVES author-paragraph-run; it is not pre-SD-P3" >&2
85 exit 1
86fi
87# Direction 2 -- the positive control on the control. Without it, a probe that
88# fails for an unrelated reason (bad redirects, missing compiler) reports
89# "absent" against every tree and the baseline means nothing.
90if ! printf '%s' "$current_probe" | grep -q 'RESOLVED'; then
91 echo "the probe cannot see author-paragraph-run even in THIS tree; it is broken, not measuring" >&2
92 printf '%s\n' "$current_probe" >&2
93 exit 1
94fi
96# THE PREMISE. This phase exists because a mid-paragraph inline command in bare
97# prose is rejected. Record that the fixture really is rejected by the tree that
98# predates the change -- otherwise "the fixture passes now" is compatible with a
99# fixture that would have passed all along.
101# Unlike SD-P1's withdrawn criterion, this one is meaningful: the bare-prose
102# fixture uses no binding the pristine tree lacks, so it fails at EVALUATION on
103# the behaviour under test rather than at compile time on an absent name.
104premise="$work/premise.sgl"
105cat >"$premise" <<'EOF'
106;; The generated forms reference `author-at`, `author-normalize` and the model
107;; constructors, which the READER's prelude normally imports. A probe that omits
108;; 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. The
110;; `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(display
118 (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)
123EOF
124cp "$repo_dir/test/fixtures/sd-p3-bare-prose.sgd" "$pristine/test/fixtures/"
125premise_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)"
128case "$premise_result" in
129 *"REJECTED"*"wrong semantic category"*) ;;
130 *) echo "the bare-prose fixture was NOT rejected at $base_ref for the B4 reason:" >&2
131 printf '%s\n' "$premise_result" >&2
132 exit 1 ;;
133esac
135dump="$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"
141grep -q 'SD_P3_LOWERING_DUMP_COMPLETE' "$dump" || {
142 echo "the pristine dump printed no completion marker" >&2; exit 1; }
144out="$repo_dir/test/transcripts/sd-p3/lowering-before.txt"
145mkdir -p "$(dirname "$out")"
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 corpus
154 # size, so including it would tie the LAST manuscript's hash to how many
155 # 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-*; do
158 name="${f##*/section-}"
159 if [ "$name" = "header" ]; then continue; fi
160 printf '%s %s\n' "$(sha256sum "$f" | awk '{print $1}')" "$name"
161 done
162 grep 'SD_P3_LOWERING_DUMP_COMPLETE' "$dump"
163} >"$out"
165echo "SD_P3_BEFORE_BASELINE_WRITTEN $out"
166awk '/^[0-9a-f]{64} /{n++} END{print "manuscripts hashed:", n}' "$out"