oficina / docs 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 8218aca64051dfa53a833e00ed4dfd4ebfc5a415 parent 6698f71parent c914e80
6 files changed, +181 −12
  • .amadan/README.md +26 −7
  • .amadan/ci.d/10-gate +11 −2
  • AGENTS.md +4 −1
  • internal/core/testdata/docs_core.wasm.stamp +3 −0
  • scripts/build-wasm.sh +12 −2
  • scripts/wasm-stamp.sh +125 −0

CI passed — run details

diff --git a/.amadan/README.md b/.amadan/README.md
index fc429c4..63fd279 100644
--- a/.amadan/README.md
+++ b/.amadan/README.md
@@ -40,9 +40,23 @@ Both were fixed on `DOCS-T1` on 2026-08-30 by editing
`~/.config/systemd/user/amadan-runner.service` and restarting. A restart
abandons any job already running, so check it is idle first.
-The gate fails if `internal/core/testdata/docs_core.wasm` differs from a
-fresh build, because `go:embed` will otherwise embed a stale module and the
-tests will pass against it.
+The gate fails if `internal/core/testdata/docs_core.wasm` is stale, because
+`go:embed` will otherwise embed a stale module and the tests will pass
+against it. It checks this two ways:
+
+- **On every host**, `scripts/wasm-stamp.sh check docs_core` recomputes a
+ SHA-256 over the files that decide the module's bytes and compares it with
+ `internal/core/testdata/docs_core.wasm.stamp`. The script lists those files:
+ the toolchain pin, `.cargo/config.toml`, `scripts/build-wasm.sh`, the
+ crate's manifest, lockfile and `src/`, and the vendored crates it patches
+ in. 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.
+
+To refresh the module, run `./scripts/build-wasm.sh`, which writes the stamp,
+then copy `core/target/wasm32-unknown-unknown/release/docs_core.wasm` to
+`internal/core/testdata/` and commit the wasm and its `.stamp` together.
It also fails if regenerating the Yjs fixtures changes them. Fixtures are
the wire contract, not an output: a deliberate rule change regenerates them
@@ -77,7 +91,12 @@ terminal instead of mailed.
The embedded WASM is built on Linux amd64 using fixed `/work`,
`/usr/local/cargo` and `/usr/local/rustup` paths. Rust/Cargo's crate identities
otherwise change code ordering across hosts, even with remapped source paths.
-Linux amd64 uses unprivileged bubblewrap when available; other hosts use the
-pinned Rust Docker image. Install Docker on development Macs. Both the artifact
-builder and containment tests use this path; the freshness comparison remains
-byte-for-byte. Docker caches and writable targets are separate from source.
+Linux amd64 uses unprivileged bubblewrap when available; Macs use the pinned
+Rust Docker image, so install Docker on development Macs. Docker caches and
+writable targets are separate from source.
+
+Cargo also hashes the host architecture into crate identities, so no build on
+Linux arm64 (hosted CI) reproduces the committed amd64 bytes; it is about a
+kilobyte larger. There `scripts/build-wasm.sh` builds natively with the
+host's toolchain, for the tests that read the artifact, and writes no stamp.
+The gate holds freshness on arm64 with the input stamp alone.
diff --git a/.amadan/ci.d/10-gate b/.amadan/ci.d/10-gate
index b25e836..53e2a5a 100755
--- a/.amadan/ci.d/10-gate
+++ b/.amadan/ci.d/10-gate
@@ -228,6 +228,14 @@ cargo fmt --manifest-path core/Cargo.toml --check
# it silently reported PASS-via-SKIP unless some other test happened
# to build the artifact as a side effect first. Building here removes
# the race instead of hoping to win it.
+# 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 docs_core
+
echo "=== wasm: clippy, then the artifact the host embeds"
./scripts/build-wasm.sh
@@ -244,8 +252,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/core/testdata/docs_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/docs_core.wasm internal/core/testdata/docs_core.wasm; then
echo "gate: internal/core/testdata/docs_core.wasm is stale. Re-copy it." >&2
exit 1
diff --git a/AGENTS.md b/AGENTS.md
index 165533b..df11476 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -250,7 +250,10 @@ an incident to clean up instead of a merge that simply does not happen.
branch discussion — see below. **CI is `.amadan/ci.d/10-gate`**, triggered
by a push to the remote, and it is the same script you can run locally. It
covers the Rust crate, the wasm artifact, Yjs wire compatibility in both
-directions, and the Go build and tests. `amadan ci status -branch <n>`
+directions, and the Go build and tests. When you change anything under
+`core/`, run `./scripts/build-wasm.sh`, copy the module into
+`internal/core/testdata/` and commit it with its `.stamp`; the gate checks
+that stamp on every host (`.amadan/README.md` explains why). `amadan ci status -branch <n>`
exit 0 is the merge gate; exit 2 is pending and exit 3 means nothing ran,
and neither is green.
diff --git a/internal/core/testdata/docs_core.wasm.stamp b/internal/core/testdata/docs_core.wasm.stamp
new file mode 100644
index 0000000..fa62d7a
--- /dev/null
+++ b/internal/core/testdata/docs_core.wasm.stamp
@@ -0,0 +1,3 @@
+# Written by scripts/build-wasm.sh; checked by scripts/wasm-stamp.sh check docs_core.
+inputs c48962fb7dd354989e109e32df2ded0dd48e855ba0b151f4e99b472d1c03dec3
+wasm 733201b2c1ffa64570f20abb04b766d51d0389aa32e5f6ed65fd5074b7120288
diff --git a/scripts/build-wasm.sh b/scripts/build-wasm.sh
index 1d70bd2..f182076 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 docs_core)
out="core/target/wasm32-unknown-unknown/release/docs_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/docs_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 docs_core "$inputs"
+ echo "build-wasm: built $out ($(wc -c < "$out") bytes) and wrote internal/core/testdata/docs_core.wasm.stamp" >&2
+ echo "build-wasm: to ship it, cp $out internal/core/testdata/docs_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..a51d83e
--- /dev/null
+++ b/scripts/wasm-stamp.sh
@@ -0,0 +1,125 @@
+#!/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
+ docs_core)
+ builder=scripts/build-wasm.sh
+ built=core/target/wasm32-unknown-unknown/release/docs_core.wasm
+ committed=internal/core/testdata/docs_core.wasm
+ # core/tests and clippy.toml are left out: neither changes the
+ # library's wasm. native/vendor/yrs is the yrs the crate patches in.
+ inputs=".cargo/config.toml rust-toolchain.toml $builder
+ core/Cargo.toml core/Cargo.lock core/src core/vendor
+ native/vendor/yrs-0.23.5"
+ ;;
+ *)
+ echo "wasm-stamp: unknown module '$1' (known: docs_core)" >&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