AtlatestRepositorysigil-doc
1
#!/usr/bin/env bash2
set -euo pipefail3
repo_dir="$(cd "$(dirname "$0")/.." && pwd)"4
cd "$repo_dir"5
mkdir -p "$repo_dir/build"6
sigil="${SIGIL_D1_COMPILER:-$repo_dir/../task-sigil-language-readers/build/dev/bin/sigil}"7
redirects="${SIGIL_D1_REDIRECTS:-$repo_dir/dev-redirects.sgl}"8
gate_dir=$(mktemp -d "$repo_dir/build/d3-gate.XXXXXX")9
export SIGIL_REDIRECTS="$redirects"10
export SIGIL_BIN="$sigil"11
export XDG_CACHE_HOME="$gate_dir/cache"12
export SIGIL_BYTECODE_BASE="${SIGIL_BYTECODE_BASE:-$(test/build-d3-bytecode-base.sh)}"14
pass() { printf 'D3_GATE_%02d %s\n' "$1" "$2"; }15
expect_fail() {16
local pattern="$1" log="$2"; shift 217
set +e18
"$@" >"$log" 2>&119
local status=$?20
set -e21
test "$status" -ne 022
rg -q "$pattern" "$log"23
}25
setsid -w "$sigil" -L src -L test test test/test-examples.sgl --report | tee "$gate_dir/model.log"26
rg -q '5 passed' "$gate_dir/model.log"; pass 1 MODEL_5_528
# Loading, traversal, and rendering are passive: the deliberately infinite29
# timeout subject is present but these operations terminate without a verifier.30
setsid -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.34
#35
# The intent was the probe's literal `|`-delimited output. As a REGEX, `7|8|` is36
# "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 matches38
# `ERROR: everything exploded`. The only input it refused was a completely empty39
# file -- which is what the bare `setsid` above it made likely, so the race was40
# the only thing giving this arm any teeth at all. Making the command wait, on41
# its own, would have achieved nothing here.42
#43
# BOTH COUNTS ARE NOW DERIVED FROM THE FIXTURE rather than written in, so a44
# 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.46
d3_fixture=test/fixtures/d3-examples.sgd47
expect_examples=$(grep -cE '^@example\[' "$d3_fixture")48
expect_checks=$(grep -cE '^@project-check\[' "$d3_fixture")49
expect_events=$((expect_examples + expect_checks))50
test "$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 than53
# assumed. The compiler may emit chatter on the same stdout line the probe's54
# first `display` continues; and the `eval` ECHOES ITS OWN RETURN VALUE after the55
# three displays, so the real line is `7|8|531#<undefined>` and a `$` anchor56
# fails on it. A `[^0-9]` bound on each side is what makes `17|8|...` and57
# `...|8|0` both fail while the real output passes.58
passive_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 what60
# the old one accepted, ACCEPT the real observed line -- otherwise a pattern that61
# refuses everything satisfies the first check for free -- and refuse a zero62
# length, which is the shape a stalled or empty render produces.63
#64
# The "known-good" control is the line this probe ACTUALLY EMITS, echoed return65
# value and all, not a tidied version of it. A control built from what the output66
# ought to look like is how a pattern ends up passing its own controls and67
# failing the product.68
printf 'ERROR: everything exploded\n' >"$gate_dir/passive-control-bad.log"69
if rg -q "$passive_pattern" "$gate_dir/passive-control-bad.log"; then70
echo "arm 2's pattern matches arbitrary output; it is not an assertion" >&2; exit 1; fi71
printf '%s|%s|0#<undefined>\n' "$expect_examples" "$expect_events" >"$gate_dir/passive-control-zero.log"72
if rg -q "$passive_pattern" "$gate_dir/passive-control-zero.log"; then73
echo "arm 2's pattern accepts a zero-length rendering" >&2; exit 1; fi74
printf '%s|%s|531#<undefined>\n' "$expect_examples" "$expect_events" >"$gate_dir/passive-control-ok.log"75
rg -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; }77
rg -q "$passive_pattern" "$gate_dir/passive.log" || {78
echo "passive load/traverse/render did not report ${expect_examples}|${expect_events}|<non-zero length>" >&279
echo "observed:" >&2; cat "$gate_dir/passive.log" >&2; exit 1; }80
pass 2 "PASSIVE_LOAD_TRAVERSE_RENDER ${expect_examples} examples, ${expect_events} events, both derived from $d3_fixture"82
verify=("$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"84
rg -q 'stdout: verified' "$gate_dir/stdout.log"; pass 3 STDOUT_EXACT85
"${verify[@]}" --no-cache --example value | tee "$gate_dir/value.log"86
rg -q 'value: verified' "$gate_dir/value.log"; pass 4 VALUE_EXACT87
"${verify[@]}" --no-cache --example expected-error | tee "$gate_dir/error.log"88
rg -q 'expected-error: verified' "$gate_dir/error.log"; pass 5 ERROR_EXACT89
set +e90
setsid -f --wait "${verify[@]}" --no-cache --example timeout | tee "$gate_dir/timeout.log"91
timeout_status=${PIPESTATUS[0]}92
set -e93
printf 'D3_TIMEOUT_STATUS=%s\n' "$timeout_status"94
test "$timeout_status" -eq 095
rg -q 'timeout: verified' "$gate_dir/timeout.log"; pass 6 PROCESS_GROUP_TIMEOUT96
"${verify[@]}" --no-cache --example session-one | tee "$gate_dir/session1.log"97
rg -q 'session-one: verified' "$gate_dir/session1.log"; pass 7 SESSION_CHECKPOINT_ONE98
"${verify[@]}" --no-cache --example session-two | tee "$gate_dir/session2.log"99
rg -q 'session-two: verified' "$gate_dir/session2.log"; pass 8 SESSION_FRESH_CUMULATIVE100
"${verify[@]}" --no-cache --example file-source | tee "$gate_dir/file.log"101
rg -q 'file-source: verified' "$gate_dir/file.log"; pass 9 CONTAINED_FILE_SOURCE102
"${verify[@]}" --no-cache --project d3-project | tee "$gate_dir/project.log"103
rg -q 'd3-project: verified' "$gate_dir/project.log"; pass 10 CLEAN_PROJECT_ARTIFACT105
# 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"108
rg -q 'stdout: verified' "$gate_dir/cache-cold.log"; pass 11 CACHE_COLD109
"${verify[@]}" --example stdout | tee "$gate_dir/cache-warm.log"110
rg -q 'stdout: cached' "$gate_dir/cache-warm.log"; pass 12 CACHE_HIT111
"${verify[@]}" --no-cache --example stdout | tee "$gate_dir/no-cache.log"112
rg -q 'stdout: verified' "$gate_dir/no-cache.log"; pass 13 NO_CACHE_TRUTH114
# Public target and manifest boundaries are explicit in the focused suite and115
# the command surface is reserved package-locally without core CLI surgery.116
rg -q "unsupported targets" test/test-examples.sgl; pass 14 BYTECODE_ONLY117
rg -q 'files:.*package.sgl' test/fixtures/d3-examples.sgd; pass 15 DECLARED_MANIFEST118
rg -q 'sigil doc verify MANUSCRIPT' src/sigil/doc/verify.sgl; pass 16 RESERVED_CLI_CONTRACT119
rg -q 'not a security sandbox' src/sigil/doc/verify.sgl; pass 17 HONEST_AUTHORITY121
for log in stdout value error timeout session1 session2 file project; do122
rg -q 'verified-count: 1' "$gate_dir/$log.log"123
done124
pass 18 ACTUAL_VERIFICATION_COUNTS126
cli=("$sigil" -L src -L test tools/sigil-doc-verify.sgl)127
expect_fail 'matched 0 examples' "$gate_dir/select-missing.log" \128
"${cli[@]}" test/fixtures/d3-examples.sgd --example absent129
expect_fail 'unknown option' "$gate_dir/select-unknown.log" \130
"${cli[@]}" test/fixtures/d3-examples.sgd --wat131
expect_fail 'duplicate option' "$gate_dir/select-duplicate-option.log" \132
"${cli[@]}" test/fixtures/d3-examples.sgd --example stdout --example value133
expect_fail 'duplicate id same' "$gate_dir/select-duplicate-id.log" \134
"${cli[@]}" test/fixtures/d3-duplicate.sgd --example same135
expect_fail 'selected zero examples/project checks' "$gate_dir/select-zero.log" \136
"${cli[@]}" test/fixtures/d3-empty.sgd137
pass 19 STRICT_SELECTION139
symlink_root="$gate_dir/symlink-root"140
mkdir -p "$symlink_root/actual-project"141
cp -R "$repo_dir/test/fixtures/d3-project/." "$symlink_root/actual-project/"142
ln -s actual-project "$symlink_root/d3-project"143
expect_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"145
expect_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"147
rg -q 'd3-examples.sgd:' "$gate_dir/symlink-file.log"148
pass 20 NO_SYMLINK_TRAVERSAL150
expect_fail 'got marker "".*exit:' "$gate_dir/fake-exit.log" \151
"${cli[@]}" test/fixtures/d3-adversarial.sgd --no-cache --example fake-exit152
expect_fail 'got marker "".*exit:' "$gate_dir/forge-file.log" \153
"${cli[@]}" test/fixtures/d3-adversarial.sgd --example forge-file154
expect_fail 'Sigil exception mismatch' "$gate_dir/forge-inherited-fds.log" \155
"${cli[@]}" test/fixtures/d3-adversarial.sgd --example forge-inherited-fds156
expect_fail 'got marker "".*exit:' "$gate_dir/enumerate-descriptors.log" \157
"${cli[@]}" test/fixtures/d3-adversarial.sgd --example enumerate-descriptors158
expect_fail 'Sigil exception mismatch' "$gate_dir/forge-lexical-state.log" \159
"${cli[@]}" test/fixtures/d3-adversarial.sgd --example forge-lexical-state160
expect_fail 'got marker "".*exit:' "$gate_dir/forge-streams.log" \161
"${cli[@]}" test/fixtures/d3-adversarial.sgd --example forge-streams162
expect_fail 'got marker "".*exit:' "$gate_dir/dedicated-exit.log" \163
"${cli[@]}" test/fixtures/d3-adversarial.sgd --example dedicated-exit164
# Failed forgeries never create a successful verification cache record.165
expect_fail 'got marker "".*exit:' "$gate_dir/forge-file-again.log" \166
"${cli[@]}" test/fixtures/d3-adversarial.sgd --example forge-file167
# `if rg; then fail`, never a bare `! rg` -- bash exempts `!`-inverted168
# 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.170
if rg -q 'cached' "$gate_dir/forge-file-again.log"; then171
echo 'D3: a failed forgery produced a successful verification cache record' >&2172
exit 1173
fi174
expect_fail 'got marker "".*signal:' "$gate_dir/signal.log" \175
setsid -f --wait "${cli[@]}" test/fixtures/d3-adversarial.sgd --no-cache --example signal176
# The private decoder rejects stale nonces and symlinks even when their payload177
# otherwise has the exact structured class/message.178
channel_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))')188
case "$channel_probe" in *"(#f #f)"*) ;; *) exit 1 ;; esac189
pass 21 EXCEPTION_EXIT_SIGNAL_CLASSIFICATION191
SECONDS=0192
expect_fail 'stdout exceeded active 1 MiB cap' "$gate_dir/stdout-cap.log" \193
"${cli[@]}" test/fixtures/d3-adversarial.sgd --no-cache --example stdout-cap194
expect_fail 'stderr exceeded active 1 MiB cap' "$gate_dir/stderr-cap.log" \195
"${cli[@]}" test/fixtures/d3-adversarial.sgd --no-cache --example stderr-cap196
test "$SECONDS" -lt 30197
pass 22 ACTIVE_BOUNDED_OUTPUT199
expect_fail "must explicitly declare backend: 'bytecode" "$gate_dir/backend-mismatch.log" \200
"${cli[@]}" test/fixtures/d3-backend-mismatch.sgd --no-cache --project d3-project201
pass 23 CONFIG_BACKEND_MATCH202
rg -q 'validate-bytecode-artifact' src/sigil/doc/verify.sgl203
rg -q 'd3-project: verified' "$gate_dir/project.log"204
unrelated_artifact="$gate_dir/unrelated-bytecode-artifact"205
printf 'deterministic non-bundle identity control\n' >"$unrelated_artifact"206
artifact_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"))))')211
case "$artifact_probe" in *"#f"*) ;; *) exit 1 ;; esac212
pass 24 BYTECODE_ARTIFACT_IDENTITY214
test/gate-d0.sh >/dev/null215
test/gate-d2.sh >/dev/null216
pass 25 D0_D2_REGRESSIONS217
echo D3_GATE_GREEN_25_25