AtlatestRepositorysigil-doc

sigil-doc / tree / testgate-d3.sh

1#!/usr/bin/env bash
2set -euo pipefail
3repo_dir="$(cd "$(dirname "$0")/.." && pwd)"
4cd "$repo_dir"
5mkdir -p "$repo_dir/build"
6sigil="${SIGIL_D1_COMPILER:-$repo_dir/../task-sigil-language-readers/build/dev/bin/sigil}"
7redirects="${SIGIL_D1_REDIRECTS:-$repo_dir/dev-redirects.sgl}"
8gate_dir=$(mktemp -d "$repo_dir/build/d3-gate.XXXXXX")
9export SIGIL_REDIRECTS="$redirects"
10export SIGIL_BIN="$sigil"
11export XDG_CACHE_HOME="$gate_dir/cache"
12export SIGIL_BYTECODE_BASE="${SIGIL_BYTECODE_BASE:-$(test/build-d3-bytecode-base.sh)}"
14pass() { printf 'D3_GATE_%02d %s\n' "$1" "$2"; }
15expect_fail() {
16 local pattern="$1" log="$2"; shift 2
17 set +e
18 "$@" >"$log" 2>&1
19 local status=$?
20 set -e
21 test "$status" -ne 0
22 rg -q "$pattern" "$log"
25setsid -w "$sigil" -L src -L test test test/test-examples.sgl --report | tee "$gate_dir/model.log"
26rg -q '5 passed' "$gate_dir/model.log"; pass 1 MODEL_5_5
28# Loading, traversal, and rendering are passive: the deliberately infinite
29# timeout subject is present but these operations terminate without a verifier.
30setsid -w "$sigil" -L src -L test eval \
31 '(begin (import (sigil doc) (sigil doc examples)) (load "test/fixtures/d3-examples.sgd") (define d (eval (quote document))) (display (length (document-examples d))) (display "|") (display (length (document-events d))) (display "|") (display (string-length (document->plain d))))' \
32 >"$gate_dir/passive.log"
33# ARM 2'S ASSERTION WAS `rg -q '7|8|'`, WHICH MATCHES EVERY NON-EMPTY LINE.
35# The intent was the probe's literal `|`-delimited output. As a REGEX, `7|8|` is
36# "7, or 8, or the empty string", and the empty alternative matches anything.
37# Measured 2026-08-22: it matches `total nonsense here`, and it matches
38# `ERROR: everything exploded`. The only input it refused was a completely empty
39# file -- which is what the bare `setsid` above it made likely, so the race was
40# the only thing giving this arm any teeth at all. Making the command wait, on
41# its own, would have achieved nothing here.
43# BOTH COUNTS ARE NOW DERIVED FROM THE FIXTURE rather than written in, so a
44# fixture that gains an example fails here instead of going silently unchecked.
45# The event stream carries one event per example plus one per project-check.
46d3_fixture=test/fixtures/d3-examples.sgd
47expect_examples=$(grep -cE '^@example\[' "$d3_fixture")
48expect_checks=$(grep -cE '^@project-check\[' "$d3_fixture")
49expect_events=$((expect_examples + expect_checks))
50test "$expect_examples" -ge 1 || {
51 echo "derived 0 examples from $d3_fixture; the derivation is broken, not the product" >&2; exit 1; }
52# NEITHER END IS ANCHORED, and both halves of that are measured rather than
53# assumed. The compiler may emit chatter on the same stdout line the probe's
54# first `display` continues; and the `eval` ECHOES ITS OWN RETURN VALUE after the
55# three displays, so the real line is `7|8|531#<undefined>` and a `$` anchor
56# fails on it. A `[^0-9]` bound on each side is what makes `17|8|...` and
57# `...|8|0` both fail while the real output passes.
58passive_pattern="(^|[^0-9])${expect_examples}\|${expect_events}\|[1-9][0-9]*([^0-9]|\$)"
59# POSITIVE CONTROLS, BOTH DIRECTIONS AND THEN SOME. The pattern must REFUSE what
60# the old one accepted, ACCEPT the real observed line -- otherwise a pattern that
61# refuses everything satisfies the first check for free -- and refuse a zero
62# length, which is the shape a stalled or empty render produces.
64# The "known-good" control is the line this probe ACTUALLY EMITS, echoed return
65# value and all, not a tidied version of it. A control built from what the output
66# ought to look like is how a pattern ends up passing its own controls and
67# failing the product.
68printf 'ERROR: everything exploded\n' >"$gate_dir/passive-control-bad.log"
69if rg -q "$passive_pattern" "$gate_dir/passive-control-bad.log"; then
70 echo "arm 2's pattern matches arbitrary output; it is not an assertion" >&2; exit 1; fi
71printf '%s|%s|0#<undefined>\n' "$expect_examples" "$expect_events" >"$gate_dir/passive-control-zero.log"
72if rg -q "$passive_pattern" "$gate_dir/passive-control-zero.log"; then
73 echo "arm 2's pattern accepts a zero-length rendering" >&2; exit 1; fi
74printf '%s|%s|531#<undefined>\n' "$expect_examples" "$expect_events" >"$gate_dir/passive-control-ok.log"
75rg -q "$passive_pattern" "$gate_dir/passive-control-ok.log" || {
76 echo "arm 2's pattern rejects the shape this probe really emits; it is a wall, not a check" >&2; exit 1; }
77rg -q "$passive_pattern" "$gate_dir/passive.log" || {
78 echo "passive load/traverse/render did not report ${expect_examples}|${expect_events}|<non-zero length>" >&2
79 echo "observed:" >&2; cat "$gate_dir/passive.log" >&2; exit 1; }
80pass 2 "PASSIVE_LOAD_TRAVERSE_RENDER ${expect_examples} examples, ${expect_events} events, both derived from $d3_fixture"
82verify=("$sigil" -L src -L test tools/sigil-doc-verify.sgl test/fixtures/practical-sigil-chapter.sgd)
83"${verify[@]}" --no-cache --example stdout | tee "$gate_dir/stdout.log"
84rg -q 'stdout: verified' "$gate_dir/stdout.log"; pass 3 STDOUT_EXACT
85"${verify[@]}" --no-cache --example value | tee "$gate_dir/value.log"
86rg -q 'value: verified' "$gate_dir/value.log"; pass 4 VALUE_EXACT
87"${verify[@]}" --no-cache --example expected-error | tee "$gate_dir/error.log"
88rg -q 'expected-error: verified' "$gate_dir/error.log"; pass 5 ERROR_EXACT
89set +e
90setsid -f --wait "${verify[@]}" --no-cache --example timeout | tee "$gate_dir/timeout.log"
91timeout_status=${PIPESTATUS[0]}
92set -e
93printf 'D3_TIMEOUT_STATUS=%s\n' "$timeout_status"
94test "$timeout_status" -eq 0
95rg -q 'timeout: verified' "$gate_dir/timeout.log"; pass 6 PROCESS_GROUP_TIMEOUT
96"${verify[@]}" --no-cache --example session-one | tee "$gate_dir/session1.log"
97rg -q 'session-one: verified' "$gate_dir/session1.log"; pass 7 SESSION_CHECKPOINT_ONE
98"${verify[@]}" --no-cache --example session-two | tee "$gate_dir/session2.log"
99rg -q 'session-two: verified' "$gate_dir/session2.log"; pass 8 SESSION_FRESH_CUMULATIVE
100"${verify[@]}" --no-cache --example file-source | tee "$gate_dir/file.log"
101rg -q 'file-source: verified' "$gate_dir/file.log"; pass 9 CONTAINED_FILE_SOURCE
102"${verify[@]}" --no-cache --project d3-project | tee "$gate_dir/project.log"
103rg -q 'd3-project: verified' "$gate_dir/project.log"; pass 10 CLEAN_PROJECT_ARTIFACT
105# Successful results alone cache. A second identical verification is a hit;
106# --no-cache remains visibly truthful.
107"${verify[@]}" --example stdout | tee "$gate_dir/cache-cold.log"
108rg -q 'stdout: verified' "$gate_dir/cache-cold.log"; pass 11 CACHE_COLD
109"${verify[@]}" --example stdout | tee "$gate_dir/cache-warm.log"
110rg -q 'stdout: cached' "$gate_dir/cache-warm.log"; pass 12 CACHE_HIT
111"${verify[@]}" --no-cache --example stdout | tee "$gate_dir/no-cache.log"
112rg -q 'stdout: verified' "$gate_dir/no-cache.log"; pass 13 NO_CACHE_TRUTH
114# Public target and manifest boundaries are explicit in the focused suite and
115# the command surface is reserved package-locally without core CLI surgery.
116rg -q "unsupported targets" test/test-examples.sgl; pass 14 BYTECODE_ONLY
117rg -q 'files:.*package.sgl' test/fixtures/d3-examples.sgd; pass 15 DECLARED_MANIFEST
118rg -q 'sigil doc verify MANUSCRIPT' src/sigil/doc/verify.sgl; pass 16 RESERVED_CLI_CONTRACT
119rg -q 'not a security sandbox' src/sigil/doc/verify.sgl; pass 17 HONEST_AUTHORITY
121for log in stdout value error timeout session1 session2 file project; do
122 rg -q 'verified-count: 1' "$gate_dir/$log.log"
123done
124pass 18 ACTUAL_VERIFICATION_COUNTS
126cli=("$sigil" -L src -L test tools/sigil-doc-verify.sgl)
127expect_fail 'matched 0 examples' "$gate_dir/select-missing.log" \
128 "${cli[@]}" test/fixtures/d3-examples.sgd --example absent
129expect_fail 'unknown option' "$gate_dir/select-unknown.log" \
130 "${cli[@]}" test/fixtures/d3-examples.sgd --wat
131expect_fail 'duplicate option' "$gate_dir/select-duplicate-option.log" \
132 "${cli[@]}" test/fixtures/d3-examples.sgd --example stdout --example value
133expect_fail 'duplicate id same' "$gate_dir/select-duplicate-id.log" \
134 "${cli[@]}" test/fixtures/d3-duplicate.sgd --example same
135expect_fail 'selected zero examples/project checks' "$gate_dir/select-zero.log" \
136 "${cli[@]}" test/fixtures/d3-empty.sgd
137pass 19 STRICT_SELECTION
139symlink_root="$gate_dir/symlink-root"
140mkdir -p "$symlink_root/actual-project"
141cp -R "$repo_dir/test/fixtures/d3-project/." "$symlink_root/actual-project/"
142ln -s actual-project "$symlink_root/d3-project"
143expect_fail 'declared path contains a symlink' "$gate_dir/symlink-file.log" \
144 "${cli[@]}" test/fixtures/d3-examples.sgd --no-cache --example file-source --root "$symlink_root"
145expect_fail 'declared path contains a symlink' "$gate_dir/symlink-project.log" \
146 "${cli[@]}" test/fixtures/d3-examples.sgd --no-cache --project d3-project --root "$symlink_root"
147rg -q 'd3-examples.sgd:' "$gate_dir/symlink-file.log"
148pass 20 NO_SYMLINK_TRAVERSAL
150expect_fail 'got marker "".*exit:' "$gate_dir/fake-exit.log" \
151 "${cli[@]}" test/fixtures/d3-adversarial.sgd --no-cache --example fake-exit
152expect_fail 'got marker "".*exit:' "$gate_dir/forge-file.log" \
153 "${cli[@]}" test/fixtures/d3-adversarial.sgd --example forge-file
154expect_fail 'Sigil exception mismatch' "$gate_dir/forge-inherited-fds.log" \
155 "${cli[@]}" test/fixtures/d3-adversarial.sgd --example forge-inherited-fds
156expect_fail 'got marker "".*exit:' "$gate_dir/enumerate-descriptors.log" \
157 "${cli[@]}" test/fixtures/d3-adversarial.sgd --example enumerate-descriptors
158expect_fail 'Sigil exception mismatch' "$gate_dir/forge-lexical-state.log" \
159 "${cli[@]}" test/fixtures/d3-adversarial.sgd --example forge-lexical-state
160expect_fail 'got marker "".*exit:' "$gate_dir/forge-streams.log" \
161 "${cli[@]}" test/fixtures/d3-adversarial.sgd --example forge-streams
162expect_fail 'got marker "".*exit:' "$gate_dir/dedicated-exit.log" \
163 "${cli[@]}" test/fixtures/d3-adversarial.sgd --example dedicated-exit
164# Failed forgeries never create a successful verification cache record.
165expect_fail 'got marker "".*exit:' "$gate_dir/forge-file-again.log" \
166 "${cli[@]}" test/fixtures/d3-adversarial.sgd --example forge-file
167# `if rg; then fail`, never a bare `! rg` -- bash exempts `!`-inverted
168# commands from `set -e`, so the bare form could never fail (inert 2026-08-24).
169# Subject presence: expect_fail above dies if the log was not written.
170if rg -q 'cached' "$gate_dir/forge-file-again.log"; then
171 echo 'D3: a failed forgery produced a successful verification cache record' >&2
172 exit 1
173fi
174expect_fail 'got marker "".*signal:' "$gate_dir/signal.log" \
175 setsid -f --wait "${cli[@]}" test/fixtures/d3-adversarial.sgd --no-cache --example signal
176# The private decoder rejects stale nonces and symlinks even when their payload
177# otherwise has the exact structured class/message.
178channel_probe=$("$sigil" -L src -L test eval \
179 '(begin (import (sigil core) (sigil fs) (sigil process) (sigil doc private exception-channel))
180 (define d (make-temp-directory)) (process-run "chmod" "700" d)
181 (define p (string-append d "/record")) (define q (string-append d "/link"))
182 (write-file-string p "(\"stale\" error \"expected boom\")") (process-run "chmod" "600" p)
183 (make-symlink p q)
184 (define uid (stat-uid (file-stat d #f)))
185 (display (list (read-authenticated-exception-record p "fresh" uid)
186 (read-authenticated-exception-record q "stale" uid)))
187 (delete-file q) (delete-file p) (delete-directory d))')
188case "$channel_probe" in *"(#f #f)"*) ;; *) exit 1 ;; esac
189pass 21 EXCEPTION_EXIT_SIGNAL_CLASSIFICATION
191SECONDS=0
192expect_fail 'stdout exceeded active 1 MiB cap' "$gate_dir/stdout-cap.log" \
193 "${cli[@]}" test/fixtures/d3-adversarial.sgd --no-cache --example stdout-cap
194expect_fail 'stderr exceeded active 1 MiB cap' "$gate_dir/stderr-cap.log" \
195 "${cli[@]}" test/fixtures/d3-adversarial.sgd --no-cache --example stderr-cap
196test "$SECONDS" -lt 30
197pass 22 ACTIVE_BOUNDED_OUTPUT
199expect_fail "must explicitly declare backend: 'bytecode" "$gate_dir/backend-mismatch.log" \
200 "${cli[@]}" test/fixtures/d3-backend-mismatch.sgd --no-cache --project d3-project
201pass 23 CONFIG_BACKEND_MATCH
202rg -q 'validate-bytecode-artifact' src/sigil/doc/verify.sgl
203rg -q 'd3-project: verified' "$gate_dir/project.log"
204unrelated_artifact="$gate_dir/unrelated-bytecode-artifact"
205printf 'deterministic non-bundle identity control\n' >"$unrelated_artifact"
206artifact_probe=$(D3_UNRELATED_ARTIFACT="$unrelated_artifact" \
207 "$sigil" -L src -L test eval \
208 '(begin (import (sigil doc verify))
209 (display (bytecode-bundle-base-matches?
210 (getenv "D3_UNRELATED_ARTIFACT") (getenv "SIGIL_BYTECODE_BASE"))))')
211case "$artifact_probe" in *"#f"*) ;; *) exit 1 ;; esac
212pass 24 BYTECODE_ARTIFACT_IDENTITY
214test/gate-d0.sh >/dev/null
215test/gate-d2.sh >/dev/null
216pass 25 D0_D2_REGRESSIONS
217echo D3_GATE_GREEN_25_25