oficina / slides Public

Merge branch 'fix/wasm-input-stamp' into main

Merge branch 'fix/wasm-input-stamp' into main

The committed wasm carries an input stamp the gate checks on every host,
so a stale wasm fails on arm64 hosted CI too; x86 keeps the byte check.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev ab97638c3254bf2eb77c274aad6d71280f53026a parent ee29570parent eb0c867
9 files changed, +225 −24
  • .amadan/README.md +29 −7
  • .amadan/ci.d/10-gate +21 −7
  • AGENTS.md +9 −4
  • README.md +3 −2
  • internal/slides/core/testdata/slides_core.wasm.stamp +3 −0
  • internal/slides/pdfcore/testdata/slides_pdf.wasm.stamp +3 −0
  • scripts/build-pdf-wasm.sh +12 −2
  • scripts/build-wasm.sh +12 −2
  • scripts/wasm-stamp.sh +133 −0

CI passed — run details

diff --git a/.amadan/README.md b/.amadan/README.md
index edffefd..8ff536f 100644
--- a/.amadan/README.md
+++ b/.amadan/README.md
@@ -87,21 +87,43 @@ earlier version of this gate `git clean`ed it away before you could.
## Two things that fail silently
- **The gate fails if `internal/slides/core/testdata/slides_core.wasm`
- differs from a fresh build.** `go:embed` will otherwise embed a stale
- module and every Go test will pass against it. When it fails, rebuild
- and commit the artifact:
+ or `internal/slides/pdfcore/testdata/slides_pdf.wasm` is stale.**
+ `go:embed` will otherwise embed a stale module and every Go test will
+ pass against it. It checks each module two ways:
+
+ - **On every host**, `scripts/wasm-stamp.sh check slides_core` (and
+ `slides_pdf`) recomputes a SHA-256 over the files that decide the
+ module's bytes and compares it with the `.stamp` committed beside the
+ wasm. The script lists those files: the toolchain pin,
+ `.cargo/config.toml`, the build script, and the crate's manifest,
+ lockfile and `src/` (plus `core/vendor` for the core). It also checks
+ the committed wasm is the build the stamp was written for. A failure
+ reads "wasm is stale: inputs changed since it was built; run
+ scripts/build-wasm.sh".
+ - **On Linux amd64** it also rebuilds the module and compares bytes.
+
+ When it fails, rebuild, copy and commit the wasm with its stamp (the
+ build script writes the stamp):
```sh
./scripts/build-wasm.sh
cp core/target/wasm32-unknown-unknown/release/slides_core.wasm \
internal/slides/core/testdata/slides_core.wasm
+ git add internal/slides/core/testdata/slides_core.wasm*
```
- That comparison only works because `scripts/build-wasm.sh` remaps the
+ and the same with `scripts/build-pdf-wasm.sh`,
+ `core/pdf/target/wasm32-unknown-unknown/release/slides_pdf.wasm` and
+ `internal/slides/pdfcore/testdata/` for the PDF module.
+
+ The byte comparison only works because the build scripts remap the
registry and workspace path prefixes rustc would otherwise bake into the
- binary. Without it the artifact reproduces byte-for-byte only on the
- machine, and under the `$HOME`, that built it, and the `cmp` reports a
- stale artifact everywhere else.
+ binary, and build on Linux amd64 at fixed paths (bubblewrap on Linux,
+ the pinned Rust Docker image on a Mac). Cargo also hashes the host
+ architecture into crate identities, so no build on Linux arm64 (hosted
+ CI) reproduces the committed bytes; there the scripts build natively
+ for the tests, write no stamp, and the input stamp alone holds
+ freshness.
- **`cargo fmt --check` covers `core/`.** It was not in the gate before
2026-09-07, and by then three files had drifted; they were reformatted in
diff --git a/.amadan/ci.d/10-gate b/.amadan/ci.d/10-gate
index c3f99cc..b8aea6f 100755
--- a/.amadan/ci.d/10-gate
+++ b/.amadan/ci.d/10-gate
@@ -84,6 +84,14 @@ echo "=== rust: clippy"
# the drift Docs' gate hit when its two clippy runs disagreed.
cargo clippy --manifest-path core/Cargo.toml --all-targets -- -D warnings -D clippy::disallowed_methods
+# Before the build below, which on Linux amd64 rewrites the stamp: this
+# must read the committed one. The input hash is the same on every host,
+# so this is the freshness check that holds on arm64, where no build
+# reproduces the committed amd64 bytes. It also fails if the committed
+# wasm is not the module the stamp was written for.
+echo "=== the embedded wasm must be built from these sources"
+./scripts/wasm-stamp.sh check slides_core
+
echo "=== wasm: the artifact the host embeds"
# Before cargo test, not after. core/tests/no_host_imports.rs asserts the
# module imports nothing from the host by reading the wasm32 artifact off
@@ -96,8 +104,9 @@ echo "=== the embedded wasm must match a fresh build"
if [[ "$(uname -s)" == Linux && "$(uname -m)" != x86_64 ]]; then
# Cargo hashes the host architecture into crate identities, so no
# Linux arm64 build reproduces the committed amd64 bytes (the build
- # script builds natively here). Linux amd64 runs keep this check.
- echo "gate: SKIPPED on Linux/$(uname -m): internal/slides/core/testdata/slides_core.wasm is the canonical amd64 build, which no build on this host reproduces byte for byte"
+ # script builds natively here). The input stamp checked above holds
+ # freshness on this host; Linux amd64 runs check the bytes as well.
+ echo "gate: byte comparison skipped on Linux/$(uname -m): no build on this host reproduces the canonical amd64 bytes; the input stamp above holds freshness here"
elif ! cmp -s core/target/wasm32-unknown-unknown/release/slides_core.wasm internal/slides/core/testdata/slides_core.wasm; then
echo "gate: internal/slides/core/testdata/slides_core.wasm does not match a fresh build — rebuild and commit the wasm:" >&2
echo " ./scripts/build-wasm.sh && cp core/target/wasm32-unknown-unknown/release/slides_core.wasm internal/slides/core/testdata/slides_core.wasm" >&2
@@ -118,15 +127,19 @@ SLIDES_REQUIRE_WASM=1 cargo test --manifest-path core/Cargo.toml
# (the exact invocation scripts/build-pdf-wasm.sh runs, so nothing
# looser sails through), build the artifact BEFORE its own
# no_host_imports test reads it, and the byte comparison against the
-# committed copy go:embed actually ships. A stale slides_pdf.wasm is
-# the same trap as a stale slides_core.wasm: the Go tests pass against
-# whatever is committed, and only this cmp knows the difference.
+# committed copy go:embed actually ships, with the input stamp checked
+# before the build as for the core. A stale slides_pdf.wasm is the same
+# trap as a stale slides_core.wasm: the Go tests pass against whatever is
+# committed, and only these checks know the difference.
echo "=== rust: fmt (pdf)"
cargo fmt --manifest-path core/pdf/Cargo.toml --check
echo "=== rust: clippy (pdf)"
cargo clippy --manifest-path core/pdf/Cargo.toml --all-targets -- -D warnings
+echo "=== the embedded pdf wasm must be built from these sources"
+./scripts/wasm-stamp.sh check slides_pdf
+
echo "=== wasm: the pdf artifact the host embeds"
./scripts/build-pdf-wasm.sh
@@ -134,8 +147,9 @@ echo "=== the embedded pdf wasm must match a fresh build"
if [[ "$(uname -s)" == Linux && "$(uname -m)" != x86_64 ]]; then
# Cargo hashes the host architecture into crate identities, so no
# Linux arm64 build reproduces the committed amd64 bytes (the build
- # script builds natively here). Linux amd64 runs keep this check.
- echo "gate: SKIPPED on Linux/$(uname -m): internal/slides/pdfcore/testdata/slides_pdf.wasm is the canonical amd64 build, which no build on this host reproduces byte for byte"
+ # script builds natively here). The input stamp checked above holds
+ # freshness on this host; Linux amd64 runs check the bytes as well.
+ echo "gate: byte comparison skipped on Linux/$(uname -m): no build on this host reproduces the canonical amd64 bytes; the input stamp above holds freshness here"
elif ! cmp -s core/pdf/target/wasm32-unknown-unknown/release/slides_pdf.wasm internal/slides/pdfcore/testdata/slides_pdf.wasm; then
echo "gate: internal/slides/pdfcore/testdata/slides_pdf.wasm does not match a fresh build — rebuild and commit the wasm:" >&2
echo " ./scripts/build-pdf-wasm.sh && cp core/pdf/target/wasm32-unknown-unknown/release/slides_pdf.wasm internal/slides/pdfcore/testdata/slides_pdf.wasm" >&2
diff --git a/AGENTS.md b/AGENTS.md
index eabafad..cd899a9 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -167,7 +167,8 @@ deadline. `internal/slides/export.go` is the pipeline over it
including the notes pages, which are paint lists typeset here with the
fonts package. `scripts/build-pdf-wasm.sh` builds the module; the
artifact is committed at `internal/slides/pdfcore/testdata/slides_pdf.wasm`
-and the gate rebuilds and `cmp`s it exactly as it does the CRDT core's.
+and the gate checks its input stamp and, on Linux amd64, its bytes,
+exactly as it does the CRDT core's.
`internal/slides/exports.go` is everything around that pipeline: the
screens, the job, and the bytes.
@@ -457,9 +458,13 @@ rather than serving an editor that disagrees with the server.
embed a stale `internal/slides/core/testdata/slides_core.wasm` and the
Go tests will pass against it; the same is true of
`internal/slides/pdfcore/testdata/slides_pdf.wasm`. **The gate
- rebuilds and `cmp`s both** (`scripts/build-wasm.sh` and
- `scripts/build-pdf-wasm.sh`); when it fails, rebuild and commit the
- artifact rather than arguing with it.
+ checks both**: on every host it recomputes a hash of each module's
+ inputs (`scripts/wasm-stamp.sh`) and compares it with the `.stamp`
+ committed beside the wasm, and on Linux amd64 it also rebuilds and
+ `cmp`s the bytes. When it fails, run `scripts/build-wasm.sh` or
+ `scripts/build-pdf-wasm.sh`, copy the module from `core/target` or
+ `core/pdf/target` into its `testdata/`, and commit the wasm with its
+ `.stamp`, rather than arguing with it.
- **The core ABI is version 2 and the host's check is exact-match.** Add
or change an export in `core/src/abi.rs` and you bump both
`slides_core_abi_version()` in `core/src/lib.rs` and `abiVersion` in
diff --git a/README.md b/README.md
index 56d0793..01708f8 100644
--- a/README.md
+++ b/README.md
@@ -117,8 +117,9 @@ that no longer exists — it passes, and the two sides have already drifted.
./.amadan/ci.d/10-gate
```
-One script, run by CI and by you: rustfmt, clippy, the wasm build and a
-`cmp` against the committed artifact, the Rust tests, then the JS half
+One script, run by CI and by you: rustfmt, clippy, the wasm input stamps,
+the wasm build and (on Linux amd64) a `cmp` against the committed
+artifact, the Rust tests, then the JS half
(`npm ci`, `node --test`, the vectors regenerated and the bundle rebuilt,
both diffed against what is committed), then `make ci` (vet, gofmt, Go
tests, migration check). `make ci` on its own is the fast loop while you
diff --git a/internal/slides/core/testdata/slides_core.wasm.stamp b/internal/slides/core/testdata/slides_core.wasm.stamp
new file mode 100644
index 0000000..8c73e2d
--- /dev/null
+++ b/internal/slides/core/testdata/slides_core.wasm.stamp
@@ -0,0 +1,3 @@
+# Written by scripts/build-wasm.sh; checked by scripts/wasm-stamp.sh check slides_core.
+inputs 159462d97f58af78191bed659555d82468f2acf40cb54c703a49b3ff5a8e731c
+wasm e29842bf8249f3c981590288d48964b9207941da0524bdd731894da598656470
diff --git a/internal/slides/pdfcore/testdata/slides_pdf.wasm.stamp b/internal/slides/pdfcore/testdata/slides_pdf.wasm.stamp
new file mode 100644
index 0000000..ac71eab
--- /dev/null
+++ b/internal/slides/pdfcore/testdata/slides_pdf.wasm.stamp
@@ -0,0 +1,3 @@
+# Written by scripts/build-pdf-wasm.sh; checked by scripts/wasm-stamp.sh check slides_pdf.
+inputs de43b81d322600cf00c795d47ea6e609e354d4e82f4872ebeb44a3c41be074a4
+wasm 78f7dcd8e7fabeb1eef2b857ae432e681b176661c8b58d6b82b38621a1cf356b
diff --git a/scripts/build-pdf-wasm.sh b/scripts/build-pdf-wasm.sh
index 4d2474d..340cd0e 100755
--- a/scripts/build-pdf-wasm.sh
+++ b/scripts/build-pdf-wasm.sh
@@ -20,12 +20,16 @@ else
# Cargo's crate identities (-C metadata) also depend on the host
# architecture, so no build on a Linux arm64 host matches the committed
# amd64 bytes, and the amd64 image crashes under qemu there. Build
- # natively instead, for the tests that read the artifact; the gate skips
- # its byte-for-byte check on these hosts and keeps it on Linux amd64.
+ # natively instead, for the tests that read the artifact. That build
+ # writes no stamp; the gate checks freshness there by the input stamp
+ # alone, and on Linux amd64 by the stamp and the bytes.
if [[ "$(uname -s)" == Linux && "$(uname -m)" != x86_64 ]]; then
echo "build-pdf-wasm: Linux/$(uname -m) builds natively; the result is not the canonical amd64 build" >&2
exec ./scripts/build-pdf-wasm.sh --native-build
fi
+ # Hash the inputs before building, so an edit made while cargo runs
+ # cannot be stamped as built (scripts/wasm-stamp.sh says what is hashed).
+ inputs=$(./scripts/wasm-stamp.sh hash slides_pdf)
out="core/pdf/target/wasm32-unknown-unknown/release/slides_pdf.wasm"
mkdir -p "$(dirname "$out")"
temporary=$(mktemp "${out}.XXXXXX")
@@ -57,6 +61,12 @@ else
bash -ec 'rustup target add wasm32-unknown-unknown >&2; ./scripts/build-pdf-wasm.sh --canonical-build >&2; cat core/pdf/target/wasm32-unknown-unknown/release/slides_pdf.wasm' > "$temporary"
fi
mv "$temporary" "$out"
+ # The stamp goes beside the committed copy. Any host's gate checks it:
+ # the inputs must still hash to what was built, and the committed wasm
+ # must be this build, so a forgotten cp fails too.
+ ./scripts/wasm-stamp.sh write slides_pdf "$inputs"
+ echo "build-pdf-wasm: built $out ($(wc -c < "$out") bytes) and wrote internal/slides/pdfcore/testdata/slides_pdf.wasm.stamp" >&2
+ echo "build-pdf-wasm: to ship it, cp $out internal/slides/pdfcore/testdata/slides_pdf.wasm and commit both" >&2
exit 0
fi
# Ship only the PDF library. Fixed path remapping and stripped symbols keep
diff --git a/scripts/build-wasm.sh b/scripts/build-wasm.sh
index 5a535c4..295fc48 100755
--- a/scripts/build-wasm.sh
+++ b/scripts/build-wasm.sh
@@ -24,12 +24,16 @@ else
# Cargo's crate identities (-C metadata) also depend on the host
# architecture, so no build on a Linux arm64 host matches the committed
# amd64 bytes, and the amd64 image crashes under qemu there. Build
- # natively instead, for the tests that read the artifact; the gate skips
- # its byte-for-byte check on these hosts and keeps it on Linux amd64.
+ # natively instead, for the tests that read the artifact. That build
+ # writes no stamp; the gate checks freshness there by the input stamp
+ # alone, and on Linux amd64 by the stamp and the bytes.
if [[ "$(uname -s)" == Linux && "$(uname -m)" != x86_64 ]]; then
echo "build-wasm: Linux/$(uname -m) builds natively; the result is not the canonical amd64 build" >&2
exec ./scripts/build-wasm.sh --native-build
fi
+ # Hash the inputs before building, so an edit made while cargo runs
+ # cannot be stamped as built (scripts/wasm-stamp.sh says what is hashed).
+ inputs=$(./scripts/wasm-stamp.sh hash slides_core)
out="core/target/wasm32-unknown-unknown/release/slides_core.wasm"
mkdir -p "$(dirname "$out")"
temporary=$(mktemp "${out}.XXXXXX")
@@ -61,6 +65,12 @@ else
bash -ec 'rustup target add wasm32-unknown-unknown >&2; ./scripts/build-wasm.sh --canonical-build >&2; cat core/target/wasm32-unknown-unknown/release/slides_core.wasm' > "$temporary"
fi
mv "$temporary" "$out"
+ # The stamp goes beside the committed copy. Any host's gate checks it:
+ # the inputs must still hash to what was built, and the committed wasm
+ # must be this build, so a forgotten cp fails too.
+ ./scripts/wasm-stamp.sh write slides_core "$inputs"
+ echo "build-wasm: built $out ($(wc -c < "$out") bytes) and wrote internal/slides/core/testdata/slides_core.wasm.stamp" >&2
+ echo "build-wasm: to ship it, cp $out internal/slides/core/testdata/slides_core.wasm and commit both" >&2
exit 0
fi
# --lib only: the wasm module shipped to wazero is the library crate. Native
diff --git a/scripts/wasm-stamp.sh b/scripts/wasm-stamp.sh
new file mode 100755
index 0000000..e9cc551
--- /dev/null
+++ b/scripts/wasm-stamp.sh
@@ -0,0 +1,133 @@
+#!/bin/sh
+# The input stamp beside each committed wasm module: a SHA-256 of every
+# file that decides the module's bytes, recorded when the module is built
+# and recomputed by the gate on every host.
+#
+# Why it exists: the gate used to prove freshness only by rebuilding and
+# comparing bytes, and that comparison only holds on Linux amd64. Cargo
+# hashes the host architecture into each crate's -C metadata, so an arm64
+# build of the same sources differs by about a kilobyte. The input hash
+# does not depend on the host at all, so it holds everywhere; Linux amd64
+# keeps the byte comparison as well.
+#
+# scripts/wasm-stamp.sh hash NAME print the input hash
+# scripts/wasm-stamp.sh list NAME print each input and its hash
+# scripts/wasm-stamp.sh write NAME [HASH] stamp the module just built
+# scripts/wasm-stamp.sh check NAME fail if the stamp is stale
+#
+# The hash is over git's tracked files only (working-tree contents), so an
+# untracked scratch file never makes a stamp unreproducible. Each line is
+# "<sha256 of contents> <repo-relative path>", sorted bytewise; the input
+# hash is the SHA-256 of those lines. No timestamps, modes or absolute
+# paths go in, so Linux and macOS agree.
+#
+# POSIX sh plus git and sha256sum or shasum, so it runs on the hosted
+# runner and on a Mac alike.
+set -eu
+cd "$(dirname "$0")/.."
+
+# module NAME: sets inputs, built (the build's output), committed (the
+# go:embed copy) and builder (the script that builds it).
+module() {
+ case "$1" in
+ # Tests and clippy.toml are left out of both: neither changes a
+ # library's wasm.
+ slides_core)
+ builder=scripts/build-wasm.sh
+ built=core/target/wasm32-unknown-unknown/release/slides_core.wasm
+ committed=internal/slides/core/testdata/slides_core.wasm
+ # core/pdf is a separate crate with its own module, below; core/vendor
+ # is the fastrand the crate patches in.
+ inputs=".cargo/config.toml rust-toolchain.toml $builder
+ core/Cargo.toml core/Cargo.lock core/src core/vendor"
+ ;;
+ slides_pdf)
+ builder=scripts/build-pdf-wasm.sh
+ built=core/pdf/target/wasm32-unknown-unknown/release/slides_pdf.wasm
+ committed=internal/slides/pdfcore/testdata/slides_pdf.wasm
+ inputs=".cargo/config.toml rust-toolchain.toml $builder
+ core/pdf/Cargo.toml core/pdf/Cargo.lock core/pdf/src"
+ ;;
+ *)
+ echo "wasm-stamp: unknown module '$1' (known: slides_core, slides_pdf)" >&2
+ exit 2
+ ;;
+ esac
+ stamp="$committed.stamp"
+}
+
+if command -v sha256sum >/dev/null 2>&1; then
+ sha() { sha256sum | cut -d' ' -f1; }
+elif command -v shasum >/dev/null 2>&1; then
+ sha() { shasum -a 256 | cut -d' ' -f1; }
+else
+ echo "wasm-stamp: needs sha256sum or shasum" >&2
+ exit 1
+fi
+
+# Checked up front rather than inside listing: listing runs in a
+# pipeline, where an exit would end only the pipeline, not the script.
+preflight() {
+ git rev-parse --is-inside-work-tree >/dev/null 2>&1 \
+ || { echo "wasm-stamp: not a git checkout" >&2; exit 1; }
+ for input in $inputs; do
+ # A renamed input would otherwise drop out of the hash in silence.
+ [ -e "$input" ] || { echo "wasm-stamp: input $input does not exist; update scripts/wasm-stamp.sh" >&2; exit 1; }
+ done
+}
+
+listing() {
+ # shellcheck disable=SC2086 # $inputs is a word list on purpose
+ git -c core.quotepath=off ls-files --cached -- $inputs | LC_ALL=C sort -u |
+ while IFS= read -r path; do
+ [ -f "$path" ] || continue # deleted in the working tree
+ printf '%s %s\n' "$(sha < "$path")" "$path"
+ done
+}
+
+stamp_field() {
+ sed -n "s/^$1 //p" "$stamp"
+}
+
+command=${1:-}
+[ $# -ge 2 ] || { echo "usage: scripts/wasm-stamp.sh hash|list|write|check NAME" >&2; exit 2; }
+module "$2"
+preflight
+
+case "$command" in
+ hash)
+ listing | sha
+ ;;
+ list)
+ listing
+ ;;
+ write)
+ # The builder passes the hash it took before building, so an edit
+ # made while cargo ran cannot be stamped as built.
+ hash=${3:-$(listing | sha)}
+ [ -f "$built" ] || { echo "wasm-stamp: no build at $built" >&2; exit 1; }
+ {
+ echo "# Written by $builder; checked by scripts/wasm-stamp.sh check $2."
+ echo "inputs $hash"
+ echo "wasm $(sha < "$built")"
+ } > "$stamp"
+ ;;
+ check)
+ fix="run $builder, then cp $built $committed and commit it with $stamp"
+ [ -f "$stamp" ] || { echo "wasm-stamp: $stamp is missing; $fix" >&2; exit 1; }
+ if [ "$(listing | sha)" != "$(stamp_field inputs)" ]; then
+ echo "wasm-stamp: $committed: wasm is stale: inputs changed since it was built; run $builder" >&2
+ echo "wasm-stamp: then cp $built $committed and commit it with $stamp" >&2
+ exit 1
+ fi
+ if [ "$(sha < "$committed")" != "$(stamp_field wasm)" ]; then
+ echo "wasm-stamp: $committed is not the module $stamp was written for (built, but not copied?); $fix" >&2
+ exit 1
+ fi
+ echo "wasm-stamp: $committed was built from these inputs"
+ ;;
+ *)
+ echo "usage: scripts/wasm-stamp.sh hash|list|write|check NAME" >&2
+ exit 2
+ ;;
+esac