Files
faunosteo/docs/development/RUST.md
T

1504 lines
100 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Rust in the Faunosteo monorepo
Status: plan R0 (foundation, validated 2026-10-13), R1 (the Step 8 worker,
validated 2026-10-13) and R2 (the domain core, validated and merged 2026-10-14) and R3 (the anatomy core,
validated after the owner's LAN acceptance and merged 2026-10-17). Rule and
roadmap: `RUST_ARCHITECTURE_REVIEW.md` (rule "Rust by default", Part III
boundary rules, Part IV the worker, Part VI phases R0–R5, Annex B R0 scope,
Annex C owner amendments). R0 shipped the toolchain, the workspace skeleton,
the worker's sandboxed child, the browser module loader and seven
experimental proofs; R1 ships the worker's job loop and sweeper for the
avatar pipeline (`STORAGE.md` §3); R2 moves the application's domain rules
into `crates/core` and switches the application to them (§8).
## 1. Toolchain: native on the development machine, reproducible in Docker, never on the VPS
Three environments, three roles (owner decision of 2026-10-13, review Annex C
addendum). The development machine is a self-hosted Ubuntu server (native,
not WSL, not the VPS) with Docker rootless.
| Environment | Role | Rust |
|---|---|---|
| Ubuntu development machine, native | daily development (`cargo check / test / build / clippy / fmt`), the browser module build, every check before a merge (`pnpm check:all`) | user-level `rustup` in `~/.cargo` and `~/.rustup`, channel pinned by `rust-toolchain.toml` |
| Docker on that machine | the Supabase stack, the worker's sandbox tests and seccomp derivation, integration tests, the application and worker images, and the reproducible run of the same Rust scripts through the builder image | inside images only |
| Production VPS | runs images; never compiles | none |
### 1.1 Native toolchain (the primary path)
Everything lives in the user's home: no global package, no `sudo`. Checked on
2026-10-13 (Ubuntu 26.04.1, x86_64): `build-essential` (gcc 15, the C
compiler that builds the worker's vendored libwebp), `libc6-dev`,
`pkg-config`, `libssl-dev` and `strace` were already installed; no system
package is missing.
```bash
# once: rustup, the user-level installer (https://rustup.rs); ~/.bashrc and ~/.profile source ~/.cargo/env
rustup show # from the repository: installs 1.99.0 + rustfmt + clippy
rustup target add wasm32-unknown-unknown # browser build of crates/wasm
cargo install wasm-bindgen-cli --version 0.2.129 --locked # = the wasm-bindgen crate in Cargo.lock
cargo install cargo-deny --version 0.20.2 --locked
```
`rustup show` run inside the repository installs the channel of
`rust-toolchain.toml` (rustup auto-install), so `cargo` here is always
1.99.0 whatever the default toolchain of the machine. Non-interactive
shells (editor tasks, git hooks, agent shells) skip `~/.bashrc` and start
without `~/.cargo/bin` on PATH: the scripts source `scripts/rust-env.sh`,
which prepends it when `cargo` is not found.
| Piece | Native | Builder image | Pinned by |
|---|---|---|---|
| Rust 1.99.0, edition 2024, rustfmt, clippy | `rustup show` (auto-install) | `FROM rust:1.99.0-bookworm` + `rustup component add` | `rust-toolchain.toml` |
| `wasm32-unknown-unknown` | `rustup target add` | `RUN rustup target add` | same channel |
| `wasm-bindgen-cli` 0.2.129 | `cargo install --locked` | same | `wasm-bindgen = "=0.2.129"` in `Cargo.toml`; `scripts/wasm-build.sh` refuses to run when `Cargo.lock` and the CLI on PATH differ |
| `cargo-deny` 0.20.2 | `cargo install --locked` | same | `deny.toml` |
| worker builder (musl, libwebp) | not native: sandbox tests run in Docker only | `crates/worker/Dockerfile` (`FROM rust:1.99.0-alpine3.24`, `build-base`) | same channel |
Commands (root `package.json`; every Rust script is one file run natively
or inside the builder image):
```bash
pnpm rust:check # scripts/rust-check.sh, native: fmt --check, clippy -D warnings (all targets),
# cargo test (core, wasm, worker unit tests), ts-rs export, cargo deny licenses/bans/sources
pnpm wasm:build # scripts/wasm-build.sh, native: crates/wasm → packages/core-wasm/pkg (git-ignored), sizes printed
pnpm check:all # scripts/check-all.sh: the pre-merge gate (§4): rust:check, types drift, wasm:build,
# pnpm check, application image, worker:test, worker:image started read-only
pnpm rust:check:docker # the same rust-check.sh inside the builder image (reproducible run)
pnpm wasm:build:docker # the same wasm-build.sh inside the builder image
pnpm rust:builder # builds faunosteo_rust_builder:1.99.0 once (about 6 minutes cold)
pnpm worker:test # Docker only: crates/worker/Dockerfile stage test, 16 sandbox tests + seccomp derivation
pnpm worker:image # Docker only: runtime image faunosteo_worker:dev (distroless/static:nonroot)
bash docker/rust-run.sh cargo <anything> # any cargo command in the builder
cargo check | test | build | clippy | fmt # the inner loop, from the repository root
```
The native `target/` (git-ignored, `.dockerignore`d) and the builder's named
volumes `faunosteo_cargo_registry` / `faunosteo_rust_target` are two
separate caches; neither invalidates the other.
### 1.2 What stays in Docker, and why
- The worker's sandbox tests (`crates/worker/tests/sandbox.rs`) are gated by
the `test-hooks` feature: a native `cargo test` compiles that target empty
and runs the worker's unit tests only. The seccomp allowlist is derived by
`strace` on the **musl static binary that ships**, inside the Alpine
`test` stage, and glibc would need other syscalls (`brk`, `mprotect`,
`futex`): running the sandbox natively would either kill a legitimate
decode or prove nothing about the shipped binary. `pnpm worker:test` is
the only path, and the worker keeps running in its container.
- The Supabase stack, the application image, the worker image and the
integration tests on them: `pnpm db:start`, `docker build -t faunosteo .`,
`pnpm worker:image`.
- The builder image stays as the reproducible alternative (same compiler,
same tools, a clean environment) and as the reference a future runner
uses; it is not needed for a compile or a test on the development
machine.
Exposure change, stated for the record: a native build runs the build
scripts and proc-macros of the dependencies under the user account (the
builder container ran them with the repository mounted read-write, so the
difference is the rest of the home directory). Mitigations in place:
`--locked` everywhere, `deny.toml` `[sources]` limited to crates.io with
unknown registries and git sources denied, `cargo deny check licenses bans
sources` in every check.
## 2. Workspace
```text
Cargo.toml workspace (resolver 3), pinned [workspace.dependencies], lints, profiles
rust-toolchain.toml channel 1.99.0, components rustfmt + clippy
rustfmt.toml edition 2024, max_width 100
clippy.toml disallowed-methods: std::time::*::now, std::fs::*, std::env::var* (the core)
deny.toml licences allow-list, bans (duplicates warn, wildcards deny, path deps allowed), sources
.cargo/config.toml wasm shadow stack 1 MiB (explicit), TS_RS_EXPORT_DIR = packages/core-wasm/types
crates/core faunosteo-core: lib, no I/O (lint), serde + serde_json + url (Zod's .url())
crates/wasm faunosteo-wasm: cdylib facade over core (wasm-bindgen)
crates/worker faunosteo-worker: bin + lib (image, libwebp-sys, seccompiler, rlimit)
packages/core-wasm runtime (lazy, poison, recovery), Node source, generated types (committed), N-1 reader, tests
```
Lints: `unwrap_used`, `expect_used` and `disallowed_methods` are errors in the
libraries (tests allow them explicitly at the top of the file); `unsafe_code`
is forbidden everywhere except the worker (libwebp FFI, each block with a
`SAFETY` comment). Dependencies are optimised even in debug builds
(`profile.dev.package."*"`) because the sandbox tests decode 4096² images
under a 10 s CPU limit.
### 2.1 Boundary rules in code (review III.3)
- `faunosteo_core::facade::call(op, json) -> json` and
`call_with_sheet(sheet, op, json)`: one operation name, one JSON text in,
the outcome envelope out (`{"ok":true,"value":…}` or
`{"ok":false,"kind":…,"detail":…[, "found":<number>]}`), never a panic on
content. The operations of R2 are listed in §8.2 (`crates/core/src/ops.rs`).
- `faunosteo_core::json`: `js_number(f64)` prints floats as ECMAScript
`Number::toString`; `to_js_pretty` / `to_js_compact` produce
`JSON.stringify(v, null, 2)` / `JSON.stringify(v)` byte for byte (proof
§5.2). Every facade output goes through them.
- `faunosteo_core::domain::js`: the JavaScript and Zod semantics the
contracts depend on (`JSON.parse` duplicate keys, a lone surrogate escape
refused as `lone_surrogate` (L1, §8.7), `.int()` as `JsInt`, UTF-16
lengths, the id / region / colour /
sha256 / base64 / uuid / URL patterns, optional fields absent and never
`null`, nullable fields required). Fields are declared in the Zod schema
order (D-R11). `cargo test -p faunosteo-core --features ts` writes the
TypeScript types to `packages/core-wasm/types` (committed; the drift gate
of `check:all` fails on any change).
- `packages/core-wasm`: `CoreRuntime` (browser-safe) and the Node source
(`@faunosteo/core-wasm/node`); a fresh instance is a new glue module record
(cache-busting import), never the trapped one (§8.3).
## 3. The worker (R0 sandboxed child, R1 job loop)
`faunosteo-worker` is one static musl binary with subcommands dispatched by
a plain `main` (no async runtime, no thread), so `decode` installs its
sandbox in a single-threaded process. R1 (Step 8) adds the keyed parent:
`run` polls `claim_avatar_jobs` through PostgREST every 2 s, downloads the
quarantine object, feeds it to a `decode` child, uploads the 512 × 512 WebP
at the final key, commits the row (`complete_avatar_job` with the lease
token), deletes the quarantine object, and sweeps (expired uploads, the
purge queue, quarantine leftovers) every 10 minutes. Modules: `config` (env,
secret newtype that no Debug output prints, JWT expiry), `log` (JSON lines,
field allowlist, child stderr escaped and cut at 200 bytes), `keys` (strict
four-segment key shapes validated before any URL), `backend` (trait, closed
`BackendError`), `http` (ureq 3 on rustls + ring with the bundled roots, 30 s
global / 5 s connect timeouts, no redirect, `P0001` bodies → closed
refusals, the Storage API's HTTP 400 + `statusCode` 404 → 404), `decoder`,
`job` (the STORAGE.md §3.4 mapping), `sweep` (§3.5), `watchdog` (per-stage
deadlines, exit 21 on overrun; `Broken` child → exit 20), `run`
(`WORKER_ONCE`, `--check`). Library choice recorded against the review's
Part V list: `ureq` instead of `tokio` + `reqwest`, hand-written JSON lines
instead of `tracing`, a 25-line base64url decoder instead of `base64`: the
loop is sequential, nothing needs an async runtime, and the keyed process
keeps a smaller dependency tree (34 → 84 crates, `aws-lc-rs` absent, no
cmake on Alpine). The decode child's traced syscall set did not change with
the parent's TLS code (§5.4 re-run at R1: the same seven). Image 11.6 MB
(8.7 MB at R0: ring + rustls). Tests: `crates/worker/tests/job_loop.rs` (11,
fake backend + fake decoder) and the module unit tests run natively; the
real-stack end-to-end runs through `pnpm e2e:avatar` (`STORAGE.md` §6).
The sandboxed child, unchanged from R0:
- `decode`: refuses a non-empty environment; checks `Threads: 1` in
`/proc/self/status`; disables core dumps (`PR_SET_DUMPABLE 0`,
`RLIMIT_CORE 0`: a dump would hold the uploaded photo); `RLIMIT_AS`
512 MiB, `RLIMIT_CPU` 10 s soft / 12 s hard (soft = hard made the kernel
SIGKILL first, finding §5.4); seccomp allowlist installed with TSYNC,
`KillProcess` on anything else, with argument filters: `tkill`/`tgkill`
may only send SIGABRT to the child's own pid (never a signal to the keyed
parent or a sibling), `mmap` may not map `PROT_EXEC`; then reads
≤ 5 MiB on stdin, sniffs the format (JPEG, PNG, WebP), decodes with
`image::Limits` (sides ≤ 4096, alloc 256 MiB), applies the EXIF
orientation, `resize_to_fill` 512 × 512, encodes lossy WebP (q 82) through
`libwebp-sys` (C, this process only), writes stdout. Exit codes in
`exit_codes.rs` (10–19, never 1, 101 or a signal number); every decode-time
error maps to a content code (`unsupported_type`, `decode_failed`,
`dimensions`, `resource_limit`), so no upload can produce a broken-environment
code.
- the parent side (`spawn::Spawner`, used by the tests now and by R1):
`env_clear`, stdin/stdout/stderr piped, output read up to 512 KiB (the
child is killed past it), 20 s deadline, classification of every exit
(`Produced`, `Rejected{code}` for the content codes
(`exit_codes::is_content_rejection`, an explicit list), `Broken{code}` when
the child could not run properly (usage, non-empty environment, sandbox
failure, encode or output failure, internal: R1 alerts instead of blaming
the upload; no content can produce it), `Signalled{signal}`,
`OutputOverflow`, `Timeout`, `Failed`). The
parent **never decodes**: `webp_check::inspect` parses the RIFF container
only (chunk allowlist `VP8X`, `VP8 `, `VP8L`, `ALPH`; the encoder's structure
enforced: optional `VP8X` first, optional `ALPH` only in an extended file,
exactly one frame; canvas size from the headers, the frame size must agree
with the canvas; any `EXIF`, `XMP `, `ICCP` or unknown chunk refuses the
output; owner amendment of D-R4). The
child's stderr (≤ 4 KiB) is child-controlled text: R1 never logs it raw
(LRN-044).
- `run --check`: prints the resolved configuration without secrets (which
variables are set, the linked libwebp version, refused below 1.6.0, the
JWT expiry of the service key); exit 0 when startable or when no worker
variable is set at all, 2 on a partial configuration. `run` starts the loop
(STORAGE.md §3.2 for the variables).
- `version`: binary, workspace and libwebp versions.
- test hooks (`--features test-hooks`, never in a release build): fixtures
generated in memory (no binary in git), `--no-seccomp` (rlimits only,
prints the `SECCOMP-MARKER` line for the strace derivation), `--violate`
(`getcwd` after the filter), `--flood`, `--alloc-gib n`, `--spin`,
`print-allowlist`.
Image: `crates/worker/Dockerfile`, stages `build` (release, static),
`test` (sandbox tests and `scripts/derive-seccomp.sh` on the **release
profile** with the `test-hooks` feature, so the binary under test differs
from the shipped one by the feature-gated hooks only), `runtime`
(`gcr.io/distroless/static-debian12:nonroot`, `USER nonroot`, `ENTRYPOINT
/faunosteo-worker`, `CMD run --check`). Measured 2026-10-12: image 8.7 MB (one static
binary), `run --check` exits 0 with `--read-only --cap-drop ALL
--security-opt no-new-privileges:true`.
## 4. Checks before a merge, and the workflow files (prepared, no CI active)
No CI/CD runs on the forge today (2026-10-13): no runner, no secret, no
registry. The YAML files under `.gitea/workflows/` are kept as prepared
material for that separate decision; a green workflow is never claimed
until one has actually run on the forge. Until then the gate is local:
```bash
pnpm check:all # scripts/check-all.sh, stops at the first failure
```
Stages, in order: `scripts/rust-check.sh` (native), the ts-rs types drift
(`git status --porcelain --untracked-files=all -- packages/core-wasm/types`
must be empty: a modified or a new type file both fail, whatever the git
configuration), `cargo deny check advisories`
(warn-only: out of the blocking path by design, review VIII.3, but printed
here because no nightly job runs yet; needs the network), `scripts/wasm-build.sh`
(native), `pnpm check` (typecheck, tests including the `core-wasm` proofs on
the module just built and the pgTAP suite on PGlite, lint, build), the
application image, `pnpm worker:test` (sandbox tests and seccomp derivation
on the musl binary), `pnpm worker:image` started once with `--read-only
--cap-drop ALL`. The local Supabase stack is not required; `pnpm db:test`
is the real-stack complement after a change under `supabase/`.
Workflow files, each mapping onto those stages so a future runner calls the
same scripts:
- `ci.yml` (push on main/develop, pull requests): job `rust` (builder image,
`scripts/rust-check.sh`, types drift, `scripts/wasm-build.sh`), job `check`
(module built the same way, then the Node pipeline), job `docker`
(application image, worker `test` stage, worker runtime started read-only),
job `supabase` (real stack, pgTAP, generated types diff).
- `rust-advisories.yml`: nightly and manual `cargo deny check advisories`,
out of the blocking path on purpose.
- `release-images.yml`: manual only; the dispatch input is a typed choice
re-validated through an environment variable; the RustSec and npm
(`pnpm audit --prod`) advisories gates run first in a job without
secrets; a named ephemeral `buildx` builder (`--builder`, no silent
fallback) with its own cache builds the images; the registry token
reaches the login step only, into a per-job `DOCKER_CONFIG` under
`RUNNER_TEMP` created before any step and removed by a literal path;
digests read from the build metadata; `actions/checkout` pinned to a
commit. The `if: github.ref` line is not a boundary (push access and the
token's scope are; a dedicated release runner label is the Step 27
operation). It would push `faunosteo_app:<sha>` and
`faunosteo_worker:<sha>` and print their digests. Deploys nothing
(DEPLOY.md "Image deploy by digest").
Target of the future CI/CD step (planned, not yet built; recorded so the
scripts stay compatible): tests on every push and pull request, Rust and
TypeScript checks, PostgreSQL/RLS tests, Docker builds, security checks,
branch verification before a merge, image publication only in an
explicitly authorised workflow.
Not measured: the forge runner's cache behaviour, cold build times and the
workflow syntax (job-level `env`, `GITHUB_ENV` path) have never run. The
builder image build took about 6 minutes cold on the development host; both
image builds reuse BuildKit cache mounts (`faunosteo_cargo_registry_musl`,
`faunosteo_target_musl`).
## 5. R0 proofs (2026-10-12, development host, Docker 29.8 rootless, kernel 7.0)
Each proof is a permanent test; the numbers are what the run produced.
### 5.1 Panic strategy on wasm32 (`packages/core-wasm/tests/panic.test.ts`)
- A Rust panic reaches JavaScript as `WebAssembly.RuntimeError` (`unreachable`):
the module is panic = abort, `catch_unwind` cannot be used there.
- Exhausting the 1 MiB shadow stack (2 000 000 frames of 128 bytes) traps
(`RuntimeError`) instead of corrupting memory.
- After a stack-exhaustion trap, the next call on the same instance fails in
the allocator (`memory access out of bounds`, deterministic). After a
plain panic the same instance *still answers*, with a stack pointer that
was never unwound: the dangerous case. Rule for R2: no instance is reused
after any trap; a fresh instance is a new module record (the wasm-bindgen
glue caches its instance in a module variable, and the ESM cache returns
the same module for the same URL: `loadCore()` imports with a cache-busting
query; in the browser the glue must be a static asset imported the same
way, or the core runs in a Worker recreated on trap).
### 5.2 Serialisation contract (`tests/serialisation.test.ts`, `crates/core/tests`)
- Byte equality with `JSON.stringify(JSON.parse(text), null, 2)` on
`report-v3.demo.json`, `report-v2.full.json`, the 296 KB equine
`document.json` and the 84 KB `report-migration-map.json`, plus a number
corpus (`1523`, `1715.4285714285713`, `0.85`, `1e+21`, `1e-7`,
`0.000001`, `-0` → `0`, `123456789012345680000`, `5e-324`,
`1.7976931348623157e+308`).
- Finding: `serde_json`'s default float parser is a documented fast path
that can be one ULP off; the first run printed `1715.428571428571` and
`123456789012345670000`. The `float_roundtrip` feature (enabled in
`Cargo.toml`) fixes it; the module grew from 187 KB to 225 KB raw.
- Optional fields absent, nullable fields `null`, unknown keys ignored,
`unresolved: false` refused; the current `ReportZoneSchema` and
`SheetRefSchema` (Zod 3, `@faunosteo/domain`) parse what the core writes.
### 5.3 musl static binary with libwebp (`crates/worker/Dockerfile`)
Builds on `rust:1.99.0-alpine3.24` with `build-base`; `libwebp-sys` 0.14.4
compiles its vendored libwebp with `cc`; `WebPGetEncoderVersion()` reports
**1.6.0** (logged by `run --check`, test
`the_linked_libwebp_is_recent_enough`). The runtime image carries the one
static binary.
### 5.4 seccomp allowlist, derived and proven (`crates/worker/tests/sandbox.rs`)
- Derivation (`scripts/derive-seccomp.sh`, strace over png, jpeg, webp, a
4096 × 4096 JPEG and a 3000 × 3000 PNG, after the marker): exactly
`read write mmap munmap mremap exit_group getrandom`. musl's mallocng used
neither `brk` nor `mprotect`; no `futex` in a single-threaded decode. The
allowlist is that set plus the abort path (`rt_sigaction`,
`rt_sigprocmask`, `getpid`, `gettid`, `tkill`, `tgkill`, `exit`).
- Proven on the release-profile musl binary with the test hooks, inside
Docker's default seccomp profile: 16 tests green (`png_jpeg_and_webp_decode_through_the_full_sandbox`,
`inputs_at_the_caps_decode_within_the_limits` (4096²), dimensions 32 px
and 20 000² header, GIF and SVG refused, truncated PNG, non-empty
environment refused, `--violate` → SIGSYS 31, `--flood` → output cut,
`--alloc-gib 2` → SIGABRT 6 under `RLIMIT_AS`, `--spin` → parent deadline,
`--spin` → SIGXCPU 24 at the CPU soft limit, libwebp ≥ 1.6.0; negative
proofs of the argument filters: `--signal-parent` (`tkill` to the parent)
and `--mmap-exec` → SIGSYS; an `iCCP` bomb of 300 MB inflated → a content
rejection, never a broken-environment outcome).
- Findings: with `RLIMIT_CPU` soft = hard the kernel sends SIGKILL (hard
limit checked first), indistinguishable from the parent's deadline kill;
the soft limit is now two seconds below the hard one. The 4096² decode
exceeded 10 s of CPU in an unoptimised debug build: dependencies are
optimised in every profile.
### 5.5 CSP with `'wasm-unsafe-eval'` (`docker/csp-proof/run.sh`)
Served `packages/core-wasm/pkg` through `nginx:1.31-alpine` with
`docker/nginx.conf` plus the exact `Content-Security-Policy` of
`DEPLOY.md`, loaded by headless Chrome for Testing 156:
| Header | Result |
|---|---|
| production (`script-src 'self'`) | `FAIL WebAssembly.instantiateStreaming(): Compiling or instantiating WebAssembly module violates the following Content Security policy directive because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive: "script-src 'self'"` |
| `script-src 'self' 'wasm-unsafe-eval'` | `OK 0.1.0` (module instantiated, `core_version()` answered) |
The keyword allows WebAssembly compilation only: an inline `<script>`
variant stayed blocked under both headers. nginx serves `.wasm` as
`application/wasm` out of the box.
### 5.6 Served size and instantiate time (`tests/size.test.ts`)
`faunosteo_wasm_bg.wasm`: 224 713 bytes raw, 85 396 bytes gzip-9 (with
`float_roundtrip`; 186 805 / 72 917 without), instantiate 2.7 ms in Node 24.
The D-R13 budget (600 KB compressed) is asserted by the test from R0 on.
### 5.7 Runner caches and build times
Not measurable from this host (no forge runner here). Recorded instead:
builder image cold build about 6 minutes; worker test stage cold about 2
minutes, warm under 30 s with the BuildKit cache mounts; the wasm build
under 10 s warm. The first run on the forge records its own numbers here.
### 5.8 Native toolchain run (2026-10-13, Ubuntu 26.04.1, rustup 1.29.1, 16 cores)
Same proofs, native toolchain (`rustc 1.99.0 (b940084d7 2026-09-28)`,
`cargo 1.99.0`, `clippy 0.1.99`, `rustfmt 1.10.0-stable`, `wasm-bindgen
0.2.129`, `cargo-deny 0.20.2`), compared with the builder image runs above:
| Measure | Native | Builder image (R0) |
|---|---|---|
| cold `cargo build --workspace --all-targets` (libwebp compiled by gcc 15) | 31.5 s | not timed at R0 |
| cold wasm release build | 4.3 s | under 10 s warm |
| `scripts/rust-check.sh` warm (fmt, clippy, tests, ts export, deny) | 32 s | 2.2 s warm volumes (same script, same results) |
| cargo tests | core 10 + 2, worker unit 9, core with `ts` 14 + 2, all green | identical counts |
| committed ts-rs types | unchanged after the export | unchanged |
| `faunosteo_wasm_bg.wasm` raw | 224 777 B | 224 713 B |
| gzip -9 | 84 924 B (`gzip`), 85 390 B (the vitest measure) | 84 927 B / 85 396 B |
| sha256 | `2d559a6b…bb3a` | `451d49c1…a752` |
| core-wasm proofs | 14 passed, instantiate 3.9 ms | 14 passed, 2.7 ms |
The 64-byte difference and the distinct hashes come from the source paths
embedded in the panic locations (`/home/<user>/Documents/faunosteo/…`
natively, `/w/…` in the builder): the module is not bit-reproducible across
build roots without `--remap-path-prefix`, which R0 does not set. The
builder image is the reference when two builds must match byte for byte.
`cargo deny` prints the same warnings in both environments
(`license-not-encountered` for six allowed licences, duplicate `miniz_oxide`
and `syn`): policy `warn`, not failures.
## 6. Reproduction
```bash
pnpm rust:check # §2, §5.2 (cargo side), native
pnpm wasm:build && pnpm --filter @faunosteo/core-wasm test:run # §5.1, §5.2, §5.6, native module
pnpm rust:builder && pnpm rust:check:docker && pnpm wasm:build:docker # the same, reproducible in the builder
pnpm worker:test # §5.3, §5.4 (tests + derivation in the build log)
pnpm worker:image && docker run --rm --read-only --cap-drop ALL faunosteo_worker:dev # §3
CHROME=<chromium binary> bash docker/csp-proof/run.sh # §5.5 (playwright-core on NODE_PATH)
CHROME=<chromium> PLAYWRIGHT_NODE_MODULES=<dir> bash docker/csp-proof/app-image.sh # §8.5, the R2 image
pnpm check:all # everything above but the CSP proofs, in one command
```
## 7. What stays hypothetical after R0
- Follow-ups from the R0 security audit, owned by R1 unless stated: base
images pinned by digest in the three Dockerfiles (today by tag, as the
existing `Dockerfile`); a verification decode of the child's output in a
second sandboxed child (the RIFF check validates the container, not the
bitstream); the shared BuildKit cache mounts of the CI builds (the
release workflow already uses its own builder); `actions/*@v4` tags in
`ci.yml` (pre-existing practice) to pin by commit at Step 27.
- The browser-side fresh-instance mechanism is built at R2 (§8.3): the glue
is a Vite asset imported with a query per instance, one spare record
imported in advance.
- HTTPS from the worker: R1 links `ureq` with rustls and the bundled
`webpki-roots` (distroless has no CA bundle; none is needed), but every
run so far targets the local stack over plain HTTP on the Docker network;
the first managed (HTTPS) target proves the roots.
- The forge runner's cache and timings (§5.7), and the workflow files
themselves: no runner exists, none of the three workflows has ever run
(§4).
- The allowlist is x86_64; an aarch64 host would need its own derivation
(`target_arch` selects the seccomp architecture, the syscall numbers come
from `libc`).
## 8. R2: the domain core in Rust, used by the application (2026-10-14)
Plan and contract: `.claude/tasks/plans/2026-10-13-rust-r2-domain-core.md`
(revision 2 after three plan challenges). The application's domain rules run
in `crates/core`, reached through `crates/wasm` and `packages/core-wasm`;
React, the DOM, the forms, Zustand and supabase-js stay TypeScript.
### 8.1 What runs in Rust, and who calls it
| Former TypeScript | Rust (`crates/core/src`) | Application consumer |
|---|---|---|
| sheet document schema (Zod, strict) | `domain/sheet/schema.rs` | `SheetHandle` load (`useSheet`), snapshot gate, publish check |
| `validateSheetDocument` (21 rules), `isPublishable`, `createEmptySheetDocument` | `domain/sheet/{validate,check,create}.rs` | `sheet.check` → `publishSheetVersion` (packages/supabase), snapshot integrity |
| `buildSheetIndex` (closures over Maps) | `domain/sheet/{index,index_data}.rs`, `SheetHandle` | `useSheet` → `SheetIndex` (precomputed tables, no call in a render) |
| report v3 / v2 / identity / migration map (Zod) | `domain/report/{v3,v2,identity,migration_map}.rs` | every op that takes or returns a report |
| `migrateReportV2ToV3`, `readReportDocument` | `domain/report/read.rs` | draft restore, import |
| `rebaseReportOnSheet` (gated, D-R12) | `domain/report/rebase.rs` | draft restore, import |
| snapshot / branding / labels / envelope schemas, `buildSheetSnapshot`, `assertSnapshot`, `readArchiveDocument` (gated, D-R12) | `domain/archive/*` | export (`archive.snapshot`), import (`archive.import`), draft restore (archive kept beside the draft) |
| `services/zoneInteraction.ts` (click decision, 7 reducers, `collectVertebrae`, `syncVertebrae`) | `domain/editor/zone.rs` | `EditorRoute` click chain, `ObservationPickers`, zone chips |
| `FindingsTreePicker.toggleLeaf` (Finding builder) | `domain/editor/finding.rs` | `ObservationCard` → findings picker |
| `VertebraPicker` toggle / all / none | `domain/editor/vertebra.rs` | `VertebraPicker` tap handlers |
| store `newReport`, `emptyObservation`, add / remove / move | `domain/editor/report_ops.rs` | `reportStore` actions (answer `{ ok }`) |
| `persistence` draft / archive parsing | `domain/session.rs` (`restore_draft`) | `reportStore.restoreDraft` (quarantine on content kinds) |
| engine `readReportArchive` (layered part) | `domain/session.rs` (`import_archive`) | `HomeRoute` import after the engine's byte scan |
### 8.2 Facade operations (`crates/core/src/ops.rs`, `facade.rs`)
Free (`call`): `sheet.check`, `report.new`, `report.add_observation`,
`report.remove_observation`, `report.move_observation`, `vertebra.select`;
R0 proofs kept: `core_version`, `pretty_json`, `sheet_ref`, `report_zone`.
On the session's sheet (`SheetHandle.call`): `index_data`, `zone.click`,
`zone.add_sides`, `zone.remove_at`, `vertebra.initial`, `vertebra.sync`,
`finding.toggle`, `draft.restore`, `archive.import`, `archive.snapshot`.
Closed failure kinds: `invalid_json`, `invalid_shape`, `unknown_op`,
`internal`, `invalid_sheet` (handle), `invalid` / `unsupported_version` /
`rebase_failed` (restore), `incompatible_version` (with `found`) /
`no_attachment` / `invalid` (import), `unknown_annotation`, `unknown_zone`,
`unknown_node`, `invalid_index`, `invalid_count` (editor); the runtime adds
`core_unavailable`. Input contracts are exported as TypeScript types.
### 8.3 Runtime in the browser (`packages/core-wasm/src/runtime.ts`, `apps/app/src/core/`)
- Lazy: the glue (10 KB) and the module are Vite assets (`?url`), never in
a route chunk; nothing is fetched before `ready()`. Callers: the editor
(`useSheet`), the new-report submit, the PDF import. `/login`, `/app/*`,
Mon compte and Home never request it (proven, §8.5); Home and `/new` read
the sheet availability from the bundle alone.
- Synchronous calls once ready. A `WebAssembly.RuntimeError` poisons the
instance: the call answers `internal`, the instance is dropped, a fresh one
is created from the compiled module kept in memory and a glue record
imported in advance (no network needed), at most 3 times per page; calls
made meanwhile answer `core_unavailable`. Sheet handles carry the instance
generation and are rebuilt from the retained payload, never called on a
dropped instance. A recovery that fails below the cap (network lost, spare
record missing) is not final: the next call or `ready()` restarts the
runtime, reusing the compiled module; a restart is not a recovery and does
not touch the counter. A failed first load is restarted by `retry()` only.
`subscribe(listener)` reports status changes after a microtask (a throwing
listener changes nothing); the pages follow `stopped` through it.
- A load failure (CSP, network, old browser) gives `core_unavailable`; the
editor and the new-report page show the notice with "Réessayer" (the retry
clears the runtime, `useSheet` and provider caches), never redirect, never
read or write the draft. Once the engine stopped, the notice is the stopped
one (§9.4 item 7), without a retry.
- Editor, storage full while the engine runs: the `editor.save_failed` banner
gains the engine-free "Télécharger une copie" button, and a `beforeunload`
guard (`app/useUnsavedGuard.ts`, called once in `App`, on every page) asks
before a reload, a close or a full-page redirect while the report is only
in memory.
- Two builds: `pkg/` ships (no trap export, checked by `scripts/wasm-build.sh`
and a test), `pkg-proofs/` carries the trap exports for the tests. Both
carry `BUILD_STAMP` (hash of the Rust sources); the vitest global setup of
`core-wasm`, `app` and `supabase` refuses to run on a missing or stale
module (`pnpm wasm:build` first).
### 8.4 TypeScript deleted, kept, frozen (census)
- Deleted at R2: `apps/app/src/services/zoneInteraction.ts` and its suite
(13 cases ported to `crates/core/tests/editor_rules.rs`), the Finding
builder of `FindingsTreePicker`, the selection rules of `VertebraPicker`,
the store's report construction and list rules, the Zod parsing in
`persistence.ts` and `sheets/provider.ts`, the domain re-exports of
`services/sheetIndex.ts`, `hydrateReport`, the publish gate's
`assertPublishable` call.
- Kept TypeScript, each with its constraint (review II.2 class C): display
helpers over core data (`zoneLabel`, `describeZone`, `zoneTrees`,
`unresolvedFindings`, the findings picker's `matches` / `isLeafSelected` /
`countSelectedUnder`, the vertebra grid extent and chip state, import
message lines, `isIdentityComplete`, `pickLabel` = `pickLocaleLabel`);
form glue (D-R7: identity, texts, comment, severity and free-findings
assignments, `FreeTextFindingInput` trimming, the practitioner prefill
schema of `practitionerCache.ts`); storage adapters (`persistence.ts`,
quarantine writes); `isObservable` (a type guard); the `SheetIndex` class
(reads precomputed tables only); the runtime and its sources (Vite asset
URLs, dynamic import).
- Frozen TypeScript, still used, never extended: `packages/domain` (PDF
engine and `apps/app/scripts/compare-reports.ts` until R4, pipeline until
R5), `packages/pdf` (byte scan and writer, R4), `packages/anatomy/render`
(canvas and engine, R3). The `report:compare` CLI reads the sheet with
the frozen domain (`apps/app/scripts/compare/sheet.ts`).
- Census: `grep -rn "@faunosteo/domain" apps/app/src` returns nothing,
enforced by ESLint (`no-restricted-imports` on `apps/app/src`).
### 8.5 Proofs and tests (2026-10-14)
- Cargo: 135 core tests (contracts byte-equal to the Zod output on the
curated and legacy sheets, the demo report and the three frozen archive
envelopes; the ported domain suites; 49 editor cases, 84 report and
archive cases and 765 Zod verdicts frozen from the TypeScript; 26
conformance fixtures; the 612 legacy leaves), 32 wasm + worker unit tests.
- Node: 906 tests (`app` 313 on the real core, `core-wasm` 25 incl. the
runtime, trap, size and measurement tests, `supabase` 39 incl. the publish
gate on the real core, `domain` 262 incl. the oracle generators, `pdf`
137, `anatomy` 130); the 39 step6 goldens and the seed golden unchanged.
- SQL: `supabase/tests/011_sheet_conformance.sql` generated from the
fixtures (cargo drift test), PGlite 12/12 files, real stack 446/446.
- N-1: the Zod schemas of develop 1a7ee7d (`packages/core-wasm/tests/n-1`)
parse every draft and archive the core-fed application writes; the three
frozen envelopes are deep-equal to the R2 exports.
- CSP on the real image (`docker/csp-proof/app-image.sh`, Chrome for Testing
156, DEPLOY.md header): with `'wasm-unsafe-eval'` a report is created and a
zone click is applied by the core with no script or wasm violation; without
it the browser refuses the module (`script-src wasm-eval`), the editor
shows the notice, stays on `/editor` and leaves the draft byte-identical;
`/login`, `/app/account` and `/` request neither the glue nor the module
(15/15). Recovery after a forced trap checked in the browser on the dev
server and the production build; real traps on the proofs build in Node.
- `pnpm check:all`: green in 2 min 17 s.
### 8.6 Measurements (development machine, Node 24, Chrome 156)
| Measure | Value |
|---|---|
| shipped module | 1,044,559 B raw (native), **340,154 B gzip -9** (budget 600 KB); before the by-reference deserialisation of the security follow-up: 1,180,153 B raw / 372,275 B served by the image; after L1 (no rewrite pass, §8.7): 1,040,784 B raw / 339,933 B gzip -9 |
| glue | 10,171 B |
| load (compile + instantiate) | 6 to 8 ms |
| sheet handle + index data (bundle 0.5 MB) | 22 to 27 ms (TypeScript parse + index on the same input: 12 to 15 ms) |
| `zone.click` | 0.03 ms |
| `draft.restore` (demo report) | 0.26 ms |
| `archive.import` (demo envelope, 184 KiB) | 2.4 ms (TypeScript layered read, without rebase: 1.0 to 1.1 ms) |
| `archive.snapshot` | 0.4 to 0.5 ms |
| linear memory after the editor path | 4.1 MiB |
| app image | 104 MB (wasm stage: the Rust toolchain stays in the builder) |
Pages without the core load nothing more than before R2; the sheet pages
fetch about 351 KB more (glue + gzip module) once per session. JSON inputs
are deserialised by reference (`T::deserialize(&Value)`): the imported
attachment is never copied as a whole value (its strings are copied once
into the typed contract), and one deserialiser instead of two keeps the
module smaller. Operation inputs (`parse_input`) keep the parsed value alive
while their strings are copied out: a transient, linear peak on inputs the
application itself builds.
### 8.7 Behaviour differences (declared)
1. **L1** (owner decision 2026-10-14, BDR-051): a JSON text that escapes a
lone UTF-16 surrogate is refused whole with the closed kind
`lone_surrogate`, never altered (TypeScript kept the surrogate; a Rust
string cannot hold it, and the U+FFFD replacement delivered first was
refused). serde_json rejects the escape like any other error and
`js::parse_json` classifies the failure with a strict escape scan, run on
failures only. A draft goes to `osteo:quarantine:draft` verbatim with the
existing quarantine notice; an archive kept beside a draft goes to
`osteo:quarantine:archive`; an import is refused with
`home.import.error_lone_surrogate` and the file is not modified; an
editor action on a report holding one (only a pasted malformed text can
bring it) is refused with `core.action_failed` and the report is left as
it was; the publish gate reads it as `invalid_shape`. Keeping such a
string byte for byte would need a JSON reader of our own and a lossless
string type across the contracts (rejected for R2, the refusal leaves
that door open: nothing is lost). Invariant of the boundary: callers pass
`JSON.stringify` output only, which escapes a lone surrogate as `\uXXXX`;
a raw JavaScript string handed to a wasm-bindgen `&str` parameter would be
re-encoded with U+FFFD before the core sees it. The `pretty_json` proof
op reads with serde_json directly and refuses such text as `invalid_json`.
2. Drafts and the attachment's report are written in contract order instead
of insertion order (D-R11); values unchanged, the N-1 reader parses them.
3. An unreadable draft or archive is set aside under `osteo:quarantine:*`
with the `editor.draft_quarantined` alert, instead of being dropped
silently; a draft rebased with unresolved items keeps its raw text under
`osteo:quarantine:draft-pre-rebase`. An unreadable draft goes there with
the archive kept beside it, both or neither. A slot already holding
another text is never replaced. Owner rule of 2026-10-14 (maintenance B):
no session text is removed or overwritten implicitly when a save or a
set-aside fails. Such a text stays in place and is held: nothing is
opened over it (`RestoreResult` `held`: an unreadable draft, its
companion archive, an unreadable archive, or the original of a draft to
rebase), and the editor shows the resolution panel (download an exact
copy, delete permanently or open the adapted version after a
confirmation, retry). A new report or an import refuses to start
(`reason: 'held'`, alert, then the panel) while such a text is held, and
first reads through the core any draft this page never opened: an
unreadable one is set aside before the new session replaces it, a
readable one is replaced as before. R3 final checks (2026-10-17): a draft
the engine traps on (`draft.restore` answering `internal`, an out-of-memory
trap on a very large draft) is held too, as the `engine_failure` row with
its archive as `companion` (`session.held.notice_engine`), kept as is; the
store remembers the (draft, archive) pair that trapped and answers held
without calling the core again, from the editor, New report and Import
alike (`ActionResult.engine`, alert `session.held_blocked_engine`), until
the panel's "Réessayer" (`retryTrapped`) or a confirmed discard; other core
failures keep their behaviour (`core_unavailable`: the reload screen). New
report and the Home file pick ask first (`ReplaceSessionDialog`: cancel,
open the bilan in progress, replace) whenever a report is in memory or a
draft sits in the slot. The draft mirror is written only
when the report changes (an unrelated store change never removes it);
a refused write keeps the previous text and shows the `editor.save_failed`
banner until a write succeeds; a refused archive write at a new session
removes the replaced session's archive rather than leave it paired with
the new draft, and shows the same banner (the tab's copy lacks it). The
rescue download and the PDF export keep their object URL alive 30 s
(WebKit cancels a download whose URL is revoked at once; both go through
`downloadBlob`, `apps/app/src/lib/download.ts`).
4. A core that cannot load leaves the editor on the degraded notice
(before R2 a failed sheet chunk left it on "loading").
5. Home and `/new` no longer load the sheet into memory on mount.
6. Inputs the interface never produced get typed errors where the
TypeScript would throw or corrupt a list (`invalid_index` on a move past
the end, `invalid_count` beyond 65,535 vertebrae, `unknown_*`).
7. A click on an existing spine zone no longer writes an identical
observation back (the value is the same).
8. The publish gate throws `SheetPublishError` with closed kinds
(`invalid_shape`, `not_publishable` with the same message as before,
`validator_unavailable`), never a raw ZodError.
9. Superseded by item 3 (maintenance B, 2026-10-14): an unreadable draft
that cannot be set aside shows the resolution panel, no longer the
engine notice.
### 8.8 Residual risks
- The import reads up to the 16 MiB attachment cap inside the long-lived
instance (linear memory never shrinks) until R4 moves the reader into a
terminated Web Worker.
- No React error boundary: every core entry point catches, but an uncaught
error elsewhere still blanks the page (unchanged since before R2).
- The CSP proof runs Chromium only (no Firefox or WebKit build on this
machine): the owner's manual run of the image on other browsers and the
phone covers Safari and Firefox.
- The module grows with each phase (340 KB of the 600 KB budget at R2; the
`url` crate's IDNA tables are part of it): R3/R4 must keep measuring.
- The quarantine slots (`osteo:quarantine:*`) keep verbatim copies of drafts
(practitioner, owner and animal data) until the tab closes, the same
exposure as `osteo:draft` itself (sessionStorage, tab and origin scoped);
nothing clears them on a new report, by design (they exist to survive it).
They have no interface yet: the practitioner cannot see or download a
set-aside text, and it disappears when the tab closes (owner decision
2026-10-14: follow-up, not part of the maintenance fix).
- "New report" from Home replaces the tab's readable draft without asking,
as before R2 (a single draft per tab, the PDF is the saved copy); the
maintenance fix only guards texts that are unreadable or held.
- Base images by tag, the new `rust:1.99.0-bookworm` included (digest pinning
is the Step 27 follow-up of R0).
- The documented CSP refuses the footer's external credit logo
(`img-src`), an older matter seen by the proof.
## 9. R3: the anatomy core in Rust, drawn by the editor canvas (2026-10-15)
Plan and contract: `.claude/tasks/plans/2026-10-14-rust-r3-anatomy-core.md`
(revision 2 after three plan challenges, 32 findings folded). Status: validated by
the owner's LAN acceptance (functional and graphical) and merged into `develop` on 2026-10-17. Residual risks are
tracked in the project's blocker registry (BLK-011, BLK-012).
### 9.1 What runs in Rust, and who calls it
| Former TypeScript | Rust (`crates/core/src/anatomy`) | Consumer |
|---|---|---|
| `apps/app/src/anatomy/drawing.ts` denylist + `DOMParser` parse of the asset (deleted) | `svg/{prescan,tree,sanitize,refs,number,serialize}.rs`, `svg::read_svg` | the sheet load (`LoadedSheet`, each drawing read once) |
| `packages/anatomy/src/render/layer.ts` (`renderAnnotationLayer`, `viewBoxOf`) | `layer.rs` | `view.canvas`; the highlight |
| the canvas composition of `drawing.ts` (fresh root, viewBox rules, layer last) | `compose.rs` `compose_canvas` | `view.canvas` → `SheetIndex.canvasSvg` → `AnatomyCanvas` |
| `packages/anatomy/src/render/compose.ts` (`composeViewSvg`, `highlightLayer`, halo) | `compose.rs` `compose_highlight` | none yet: proven against the engine (O3), wired at R4 in R4's snapshot-based call |
| `packages/pdf/src/svg/sanitize.ts` allowlist and value rules | `svg/sanitize.rs` (same lists, same rules, declared additions 9.4) | the canvas now; the engine keeps its TypeScript copy until R4 |
One facade operation on the sheet handle: `view.canvas { viewId } → { svg }`,
failures `unknown_view`, `no_asset`, `svg_rejected` (reason in `detail`:
`too_large`, `doctype`, `malformed`, `not_svg`, `too_many_elements`,
`too_deep`, `too_many_uses`, `too_complex`, `reference_cycle`, or
`removed <names>` when the allowlist would take something out of a product
drawing), `invalid_character`. The result is constant for a loaded sheet:
`SheetIndex` memoises it per view; `AnatomyCanvas` parses it with `DOMParser`,
imports it and restyles `data-state` in place as before.
### 9.2 The SVG reader (`read_svg`)
1. Bounds before any parser (`prescan.rs`, a markup state machine: comments,
CDATA, PIs and quoted values skipped by the grammar, each closer searched
after its whole opener so `<!-->` cannot hide markup, no regex): 2 MiB, a
leading BOM refused, `<!DOCTYPE`/`<!ENTITY` anywhere, depth 64, 20,000
elements, 256 attributes per element and 100,000 in all, 16 namespace
declarations, 100,000 raw `<` and 200,000 raw `=`. Reason: roxmltree 0.21.1
builds its tree recursively (measured: a 1 MiB stack overflows between
1,000 and 3,000 nesting levels; the iterative fix is unreleased), and it
pre-allocates on the raw `<`/`=` counts, checks duplicate attributes
quadratically and copies inherited namespaces per declaring element.
2. roxmltree 0.21.1 (DTD refused, node limit 200,000 as a backstop) into the
core's own tree, names as written (the allowlist matches qualified names
like the engine).
3. The allowlist (`sanitize.rs`): the engine's 28 elements, 74 attributes, 27
presentation attributes and value rules (no backslash, every `url(` local,
`href` to `#id` only, `style` without forbidden tokens or functions, the
`stdDeviation` clamp with JavaScript `Number()` reproduced by hand), and the
`<use>` count cap 256.
4. The reference graph (`refs.rs`): tree edges plus every `href`/`xlink:href`
and `url(#id)` to the first element of that id; a cycle is refused, and so
is an expanded size over 50,000 elements (each reference adds its target's
expanded subtree, saturating, iterative, early exit).
5. The serialiser (`serialize.rs`): XML 1.0, `& < > "` and `\t \n \r` as
references in attributes, `& < >` and `\r` in text; a re-read gives the
same tree. A composed view over 4 MiB is refused.
### 9.3 Proofs and tests (oracles frozen with the TypeScript first)
- O1 (`apps/app/tests/fixtures/goldens/r3/canvas.*.json`, 47c5f49): the canvas
tree of each view and the layer of 14 state sets per view (the three
reference reports). Green on the Rust composition in cargo
(`tests/anatomy_compose.rs`), in the app suite on the mounted canvas
(`canvas.trees.test.tsx`) and in Chromium 156 on the live page
(`scripts/visual-canvas.mjs dom`): equal for both views.
- O2 (`oracles/r3/layer.json`): 8 layer trees with attribute order (draft +
an edge document: twin radius floor, `1e+21`, path geometry, circle trigger
without a name, colour fallback, laterality): exact, order included.
- O3 (`oracles/r3/highlight.json`): 36 highlighted compositions (every
observation and view of the three reports, an existing nested `<defs>`, a
detail view with its own asset and no viewBox): tree-equal.
- O4/O5 (`oracles/r3/sanitiser.json`): 42 hostile files
(`tests/fixtures/svg/hostile`) and 10 generated cases: equal to the
TypeScript verdict or a declared one (9.4); the 4 product SVGs (horse
drawing, partner logo, 2 public logos) untouched, tree-equal. Mutation
checks run on the oracle tests (a cap, the namespace rule, the twin floor,
the halo placement): each mutant failed the suite.
- Security audit pass 1: PASS with one MEDIUM in the prescan (a closer
searched from inside its own opener: `<!-->` let crafted markup hide 5,000
nesting levels or 3,000 attributes, then roxmltree overflowed the stack),
fixed with regression cases in cargo and through the shipped wasm; two LOW
(Unicode-spaced `href`, root painting attributes) fixed the same way. Pass 2
on the fix: PASS (about 4 million differential documents, prescan against
roxmltree: no undercount, no cap breach, no stack overflow on a 64 KiB stack;
the old desync and its variants answer `too_deep` on the shipped module); its
LOW (the same Unicode-space tolerance inside `url(`) fixed.
- Shipped wasm (`packages/core-wasm/tests/anatomy.test.ts`): the corpus plus
floods (99,000 nesting levels, 20,001 nodes, 150,000 `<` in a comment, 257
attributes, 2 MiB + 1, a 12 × 20 `<use>` chain, 1.9 MiB of `>`), each in a
fresh instance (50 cases): a closed outcome, no trap, slowest 24 to 37 ms over runs, largest linear
memory growth 10.1 MiB.
- Runtime: the `SheetHandle` constructor's closed refusal (`<kind>: <detail>`
from a plain `Error`) is `invalid_sheet`; any other throw (RangeError,
InternalError, a trap) poisons the instance (`runtime.test.ts`).
- Visual, Chromium 156 (`scripts/visual-canvas.mjs`): 30 captures before
(TypeScript canvas) and after (Rust), desktop 1280 × 800 and phone
390 × 844, no selection, each demo observation active, hover, main and
head: 30/30 pixel-identical. The method's own noise floor: two captures of
the same build 30/30 identical; its classifier flags a 10 × 10 change as a
regression and a one-pixel edge nudge as anti-aliasing.
- The Step 6 goldens (39) and the seed and review-drawing goldens are
untouched: the engine and the pipeline still run the TypeScript renderer.
### 9.4 Behaviour differences (declared; none touches a product SVG)
1. An element outside the SVG namespace is removed (`<path xmlns="…">`), and
`xlink:href` must be in the xlink namespace whatever its prefix.
2. Comments and processing instructions are dropped; non-whitespace text is
kept only in `title`/`desc`, each at most 4 KiB (longer: refused whole).
3. The root loses `class` and `style` (an inline SVG lives under the page's
CSS: `class="fixed inset-0 …"` would cover the application) and
`transform`, `filter`, `mask`, `clip-path` (they can paint outside the
canvas box); an `id` outside `[A-Za-z_][\w.:-]*` is removed; an `href` is
local only when it reads `#name` once ASCII whitespace is stripped, as the
URL parser strips it (the TypeScript also trimmed Unicode spaces, so a
no-break space before `#a` passed and resolved as a relative URL); inside
`url(` only CSS whitespace is skipped before `#`, for the same reason.
4. A reference cycle, an expansion over 50,000 elements, and the floods of
9.2 step 1 are refused; the TypeScript accepted them (size counted on the
raw text, refused subtrees included).
5. A product drawing the allowlist would alter is refused at sheet load for
the views on that asset (`svg_rejected`, `removed …`); the TypeScript canvas
dropped such parts silently.
6. A label or path the document holds with a code point XML 1.0 cannot carry
is refused (`invalid_character`); the DOM path drew it.
7. When the canvas cannot be drawn, it says why, inside the canvas (owner
decision 2026-10-15): the engine did not run (`core_unavailable`,
`internal`: the existing `sheet.core_unavailable` notice with "Réessayer",
the retry builds again on a fresh instance), the engine stopped after its
three recoveries (`CoreRuntime.stopped`: no retry anywhere; the canvas shows
the stopped text, and `CoreStoppedNotice` sits in the editor's banner slot
in place of the storage-full text, at the top of Home and New report, and
replaces the full-page `CoreUnavailable` retry. Two variants: the tab's
copy of the report is saved, `core.stopped` with "Recharger la page"; the
tab's storage refused the last write (`saveFailed`), `core.stopped_unsaved`
with "Télécharger une copie" (the in-memory report as JSON, no engine
needed), never the reload button. Action, export, import and new-report
alerts say the same text instead of "try again"; "Réessayer" on the canvas
restarts the engine before building again) or the view itself cannot be
drawn (`svg_rejected`,
`no_asset`, `unknown_view`, `invalid_character`, a parse failure: the
`canvas.view_unavailable` notice, no retry since nothing would change). The
technical kind and detail stay in the core's outcome and the DEV console;
the refused drawing and the session are left as they are. Before R3 a
failure there left the canvas blank (an uncaught error).
8. A trigger without a name carries the opened view's name in each language
(owner decision 2026-10-15: until then both name attributes and its title
held the French name). Display only; the frozen oracles stay as frozen and
the tests apply this declared change before comparing.
9. R3 final checks (owner decisions 2026-10-17). The rescue copy (the
"Télécharger une copie" JSON, byte format of `osteo:draft`) is importable:
the Home picker accepts it beside the PDF, routed by content from the first
KiB (`%PDF-` goes the PDF path), refused above `MAX_ATTACHMENT_BYTES`,
decoded as strict UTF-8, read by the core's `draft.restore` (no archive) and
opened like an import; a copy the core refuses is `home.import.error_invalid_copy`
(file and session untouched), a rebased one shows `editor.draft_rebased`.
The copy lacks the archive envelope, which the original PDF keeps. A draft
that traps the engine opens the resolution panel and is read once (§8.7
item 3); importing the copy of a trapping draft runs the same input again.
### 9.5 Measurements
| Measure | Value |
|---|---|
| shipped module | 1,188,310 B raw, **395,613 B gzip -9** at 23895b9 (+55.7 KB over R2; budget 600 KB; the app image's build root gives 395,541 B) |
| sheet handle with the drawing read (Node) | 8 to 10 ms |
| `view.canvas` main, first (Node) | 1.2 to 1.4 ms; head warm median 0.85 ms; 143 KiB of SVG |
| Chromium, preview build, CPU × 4, medians of 3 | editor to canvas 503 ms (476 ms before R3); detail open 143 ms (125 ms before R3, runs 109 to 157); browser parse + import of the canvas text 21 ms (22 ms) |
| 50 detail open/close cycles, CPU × 4 | JS heap +0.72 MiB (+0.89 MiB before R3), DOM nodes +0 (+0) |
### 9.6 TypeScript deleted, kept
- Deleted: the canvas denylist `sanitizeSvg`, `parseDrawing`, the parse
cache, `assetOfView`, the states input of the canvas build, the
`assets` field of `SheetIndex`, the app's dependency on
`@faunosteo/anatomy`.
- Kept, with their consumers: `packages/anatomy/src/render/**` (PDF engine
until R4, curation pipeline until R5), `packages/pdf/src/svg/**` (engine
until R4), the R3 oracle generators (`packages/anatomy/tests/
r3.layer-oracles.test.ts`, `packages/pdf/tests/r3.{highlight,sanitiser}-
oracles.test.ts`: they still pass on the TypeScript and keep the freeze
honest until R4/R5).
### 9.7 Residual risks
- The engine (PDF) still sanitises with the TypeScript allowlist and has no
reference budget: the `<use>` expansion gate of `PDF_IMPORT_HARDENING.md`
§5 is met in the core, closed for the PDF path only when R4 switches it.
- roxmltree's recursion: safe behind the prescan depth cap (differentially
fuzzed, 9.3); pin the release with the iterative parser when it ships.
- References are checked inside one drawing; in the page `#id` resolves against
the whole document (the detail modal imports a second copy of the drawing,
as before R3). Drawings with their own assets (Step 13) should get ids
scoped per view before external uploads are drawn.
- Firefox and WebKit: not run here (no browser build); the owner's LAN run on
the phone covers WebKit.
- The worker's TLS path has not run against a real HTTPS endpoint (every run
targets the local stack over HTTP): exercise it before the deployment that
needs it.
- WASM budget: 396 KB of 600 KB used; R4 adds the PDF reader and writer.
## 10. R4: the PDF core in Rust (in progress; R4.1 freeze done 2026-10-18)
Plan and contract: `.claude/tasks/plans/2026-10-17-rust-r4-pdf-core.md`
(revision 2, validated by the owner 2026-10-18 with the conditions of its
§A.6). Branch `feature/rust-r4-pdf-core`, cut from develop at 66909b6. This
section is a draft until the switch: it records what R4.1 froze and the
TypeScript baselines every later slice is measured against.
### 10.1 R4.1: what is frozen, and where (no Rust yet)
Every oracle below was generated by the TypeScript code at the commits
listed, behind `MAKE_R4_ORACLES=1`; without the flag the same test asserts
equality with the committed file. None of them is regenerated without the
owner (plan §B.5.1). The Step 6, R2 and R3 goldens and the R3 oracles are
byte-identical to develop (`git diff --stat develop..HEAD` on those paths is
empty).
| Oracle | Content | Files | Commit |
| ---------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------- | ------- |
| O11, O13 | frozen TS attachment scanner as the N-1 PDF reader (6 tests); synthetic reports L (60 observations, 120 views) and CAP (500, 1,000) | `packages/core-wasm/tests/n-1/scanner/`, `apps/app/tests/fixtures/r4/reports/{L,CAP}.json` | 7ecbee9 |
| O8 | differential tolerance corpus on the full R3 import path, 51 PDFs with the outcome and message of today | `crates/core/tests/oracles/r4/tolerance.json`, `apps/app/tests/fixtures/pdf/r4/tolerance/` | 7a42979 |
| O1, O2, O3 | 12 draw logs (every text, image, line, rectangle, link at full precision); 12 R3-build PDFs plus the real-raster export of `v2full` observation 1 (two PNGs and a manifest) and the store-path PDF with its report; 3 pre-R2 envelope PDFs | `apps/app/tests/fixtures/goldens/r4/draw.*.json`, `apps/app/tests/fixtures/pdf/r3/`, `apps/app/tests/fixtures/pdf/step6/envelope.*.pdf` | 87b8873 |
| O7, O9 | 155 reader inputs as bytes with their outcome (regex-semantics rows included); 24 zlib framing inputs, 26 verdict rows of `inflateFlate` in Node | `crates/core/tests/oracles/r4/{read,inflate}.json`, `apps/app/tests/fixtures/pdf/r4/{read,inflate}/` | 96df15e |
| O4, O5, O6 | text (`measure` 8 rows with units per glyph and a 200-row width sample, `wrapLines` 5, `safeText` 2, `upper` 4), AFM widths of Helvetica, Helvetica-Bold and Times-Roman over WinAnsi, dates (grid of 4 locales × forms, 22 others), 108 PNG gate and decode cases over 103 files | `crates/core/tests/oracles/r4/{text,afm,dates,png}.json`, `crates/core/tests/fixtures/png/r4/` | 516342b |
| O10 | 230 `report:compare` invocations over 14 scenarios, 72 rows declared `r4Declared`; the test ledger | `crates/core/tests/oracles/r4/compare/`, `crates/core/tests/pdf/LEDGER.tsv`, `scripts/r4-ledger.mjs` | 4bdfda5 |
| O9 browsers, O12 | O9 verdicts in Chromium, Firefox and WebKit; the browser harness and the TS baselines of 10.2 | `crates/core/tests/oracles/r4/inflate.browsers.json`, `scripts/browser/` | 4439fd2 |
Ledger (`crates/core/tests/pdf/LEDGER.tsv`): 299 TypeScript cases at the
freeze, 150 `ported`, 28 `replaced`, 88 `kept`, 33 `deleted`; the census
check of plan §B.6 runs from R4.2 on.
Weight added to the repository by the freeze: 421 blobs, 11.63 MB raw,
5.92 MB stored (plan §A.2 OD-7 said about 5 MB; the overshoot is the
real-raster set and the browser results file, 0.65 MB raw, 45 KB stored).
`.gitleaks.toml` allowlists one generated file, `compare/compare.json`,
whose fixture names contain `key` next to sha256 digests.
Findings of the freeze, each one a declared difference or a port
constraint for the later slices (none changes the TypeScript):
- UPNG deviates from the PNG specification on two inputs (interlaced
filter-3 first row; 16-bit grey tRNS alpha offset); the gate accepts a
palette image without PLTE and pdf-lib then throws; `raster-bad-adler` is
refused by the gate but UPNG decodes it. R4.4 follows the oracles.
- `safeText` maps U+0178 to `ydieresis` (159) and drops the soft hyphen;
`numberToString(1e-7)` gives `0.00000009999999999999999`;
`formatDate("0000-01-01")` gives « 1 janvier 1900 » (years 0 to 99,
arbitration A3: unchanged).
- O8: the decoy-first attachment that R3 imports will be refused by R4
(OD-4 class, to present before the switch); an invalid byte between
tokens, a `/V` number followed by a string and a depth of 65 all give
`attachment_stripped` today.
- The APAFML notice is not in `@pdf-lib/standard-fonts`; R4.4 decides the
AFM source.
- The zlib-dependent verdicts of O9 depend on Node's bundled zlib
(1.3.2.1); the browser columns agree on every row (10.2).
### 10.2 TypeScript baselines (2026-10-18, `scripts/browser/results/2026-10-18-ts-baseline.json`)
Harness: `scripts/browser/` (README there), Playwright 1.64.0 outside the
repository, build `SHEET_BUNDLE=draft` at 87b8873 (the app, engine and
module sources are identical at 516342b, the recorded HEAD), served on
127.0.0.1 with the DEPLOY.md CSP plus `'wasm-unsafe-eval'`, locale fr.
Engines: Chromium headless shell 156.0.8078.4, Firefox 157.0, WebKit 27.2
(WPE MiniBrowser); none is a branded browser or a phone. Host: i7-11700B,
15.1 GB RAM, 8.4 GB available at start, no CPU throttling.
**The 4 GiB RSS watchdog is not a guaranteed memory limit.** It samples the
browser's process tree every 50 ms, so a faster allocation can overshoot
before the kill; summed VmRSS counts shared pages once per process; a
process outside the tree is not seen. It keeps the shared host away from
the kernel OOM killer; `stopped_by_ceiling` is a recorded result.
Export (TS engine). Medians over 3 runs, range in brackets; CAP one run.
"PNG blobs" counts every blob the rasteriser encoded, the first being the
partner SVG logo; the time runs on the page clock from the click to the
PDF Blob's object URL.
| Engine | Report | Outcome | Export s | PDF MB | PNG blobs | View PNG sum MB | View PNG KB | Stopped at |
| -------- | ------ | ------------------ | ---------------- | ------ | --------- | --------------- | ----------- | ----------------------------- |
| Chromium | demo | downloaded | 1.4 (1.4–1.4) | 2.7 | 8 | 3.7 | 508–614 | |
| Chromium | L | downloaded | 19.4 (19.4–19.5) | 41.9 | 121 | 65.8 | 508–614 | |
| Chromium | CAP | stopped_by_ceiling | | | | | | 4,102 MB at 101 s, ≥ 857 PNGs |
| Firefox | demo | downloaded | 1.6 (1.6–1.8) | 2.7 | 8 | 3.2 | 462–492 | |
| Firefox | L | downloaded | 35.3 (24.8–37.0) | 42.1 | 121 | 56.0 | 462–492 | |
| Firefox | CAP | stopped_by_ceiling | | | | | | 4,111 MB at 73 s, ≥ 577 PNGs |
| WebKit | demo | downloaded | 2.6 (2.6–2.6) | 2.4 | 8 | 2.4 | 345–371 | |
| WebKit | L | downloaded | 52.2 (52.2–52.2) | 36.8 | 121 | 42.1 | 345–371 | |
| WebKit | CAP | stopped_by_ceiling | | | | | | 4,101 MB at 384 s, ≥ 973 PNGs |
Export memory, MB. "Summed" is the browser tree, "Content" the content
processes. "Settled": forced GC through CDP plus 1 s in Chromium, 5 s idle
in Firefox and WebKit; the PDF Blob is still alive through its object URL
for 30 s (`download.ts`), so "after" includes it.
| Engine | Report | Summed before / peak / after | Content before / peak / after | JS heap before → after (CDP) |
| -------- | ------ | ------------------------------- | ------------------------------- | ---------------------------- |
| Chromium | demo | 473 / 640 (626–642) / 499 | 155 / 313 / 171 | 7.4 → 9.4 |
| Chromium | L | 475 / 1,128 (1,110–1,137) / 566 | 158 / 805 (787–814) / 201 | 8.1 → 11.1 |
| Chromium | CAP | 502 / 4,102 (stop) / – | 182 / 3,751 / – | 13.2 → – |
| Firefox | demo | 778 / 974 / 815 | 152 / 359 / 228 | |
| Firefox | L | 791 / 1,932 (1,929–1,998) / 943 | 160 / 1,351 (1,346–1,420) / 327 | |
| Firefox | CAP | 834 / 4,111 (stop) / – | 198 / 3,522 / – | |
| WebKit | demo | 499 / 815 / 778 | 306 / 617 / 581 | |
| WebKit | L | 512 / 1,370 (1,368–1,471) / 743 | 316 / 1,157 (1,154–1,209) / 510 | |
| WebKit | CAP | 574 / 4,101 (stop) / – | 375 / 3,895 / – | |
What the export rows say for the plan: CAP (1,000 views) does not complete
in any engine under 4 GiB with the TS engine, as §B.4 predicted; L costs
0.8 to 1.35 GB of content-process peak; browser PNGs are 0.35 to 0.61 MB
per view (WebKit about 40 % smaller than Chromium, hence the PDF sizes),
against the 0.54 MB resvg figure of §B.4.
Import (TS reader), 226 files × 3 engines (678 runs), a fresh context per
file; the duration runs on the Node clock from the file being set to the
first of editor, alert or inline error. No file gets a different outcome or
message in any engine. Every v2, v3, step6 and R3 PDF opens the editor in
39 to 112 ms. The largest growth is `tolerance/values-1m.pdf` (+105 / +153
/ +102 MB summed); the slowest inputs are the 20 to 50 MiB generated ones
(170 to 309 ms, all `error_no_attachment`). Chromium shows one long task on
9 files (52 to 173 ms), all among the 10 to 50 MiB inputs, `values-1m` and
`612-leaves`; Firefox and WebKit do not expose long tasks.
| Engine | Group (files) | Outcome | Duration ms median (range) | Max summed RSS growth MB | Max content growth MB |
| -------- | ------------------------------ | -------------------- | -------------------------- | ------------------------ | --------------------- |
| Chromium | base (3) / r3 (14) / step6 (3) | editor | 53 / 63 / 61 | 28 / 44 / 39 | 23 / 38 / 33 |
| Chromium | tolerance (51) | editor 33, error 18 | 57 (23–122) | 105 | 99 |
| Chromium | read (155) | editor 23, error 132 | 66 (22–309) | 75 | 68 |
| Firefox | base / r3 / step6 | editor | 39 / 73 / 69 | 63 / 54 / 52 | 14 / 33 / 32 |
| Firefox | tolerance / read | as Chromium | 53 (25–153) / 78 (25–286) | 153 / 86 | 131 / 61 |
| WebKit | base / r3 / step6 | editor | 52 / 62 / 66 | 52 / 54 / 53 | 50 / 52 / 51 |
| WebKit | tolerance / read | as Chromium | 59 (23–141) / 75 (22–238) | 102 / 101 | 100 / 99 |
O9 in the browsers: the 26 rows give the same verdict in Chromium 156,
Firefox 157 and WebKit 27.2 as in Node 24.21.0 (8 non-null, 18 null).
Not measured: CAP export time and PDF size (ceiling first); JS heap in
Firefox and WebKit (CDP is Chromium only, and Chromium's figure leaves out
ArrayBuffer memory); memory after an import settles (the §B.3 check belongs
to the R4 worker proof); real browsers and phones (J1 and the owner's
iPhone run). Seen on every page: the known `img-src` refusal of the
footer's external credit logo; no script or wasm violation.
### 10.3 R4.2: the reader in Rust (2026-10-18, no import switched yet)
What runs in Rust (`crates/core/src/pdf`, feature `pdf-read`, enabled by the wasm
facade, never by the worker): the byte scan of `scan.ts` (`read/scan.rs`,
`strings.rs`, `stamp.rs`, the JavaScript regex semantics reproduced by hand
and pinned by the `semantics.*` rows of O7), the inflation on miniz_oxide
(`read/inflate.rs`, non-wrapping output buffer growing to the limit plus one,
trailing bytes refused, Adler-32 checked: the verdicts of
`DecompressionStream`), the JavaScript-path side effects of the probe
(`json_js.rs`: TextDecoder parity with one BOM dropped, overflow to `null`,
the lone surrogate neutralised for the probe only, the depth and value
pre-scan), the probe itself (`read/probe.rs`, one 16 MiB budget shared by the
candidates, a cancelled inflation not charged) and `pdf::import::import_pdf`,
which hands the text to the unchanged R2 session import. On the facade:
`SheetHandle.import_pdf(bytes)` with the kinds of `PdfImportKind` (ts-rs).
Nothing in the application calls it yet (R4.3).
Parity (every suite green on the committed oracles, `cargo test -p
faunosteo-core --features pdf-read` and the core-wasm Node suites):
| Oracle | Rust check | Result |
|---|---|---|
| O7 `findEmbeddedFiles`, `hasArchiveStamp`, `decodePdfString` | `pdf_scan` | 144 PDF inputs, 178 candidates, 19 string rows, all equal |
| O9 and the O7 flate rows | `pdf_inflate` | 26 + 12 verdicts equal to Node; the browser columns agree on every row |
| TextDecoder lossy decode | `pdf_json_js` on `fixtures/pdf/r4/utf8-lossy.json` (400 cases, `scripts/r4-utf8-oracle.mjs`) | 0 difference |
| O7 `locateAttachment`, `readAttachedDocument`, `readReportArchive` | `pdf_import` | 144 inputs equal (candidate, name, offset, bytes digest, parsed-object digest; origin, counters, snapshot rejection, report digest) |
| O8, the full R3 import path, 51 PDFs | `pdf_import` natively and `pdf-import.test.ts` through the shipped module | 47 equal, 4 declared differences below |
| Reference PDFs (Step 5, Step 6, R3, O2) | `pdf-import.test.ts` | every one imports |
Declared differences, all on hostile files (plan §A.3):
- OD-4, three O8 cases (`surrogate-content-overwritten`,
`surrogate-version-overwritten`, `two-files-decoy-first`): the text holds a
lone UTF-16 surrogate escape in a value that a later duplicate key
overwrote. `JSON.parse` kept the last value, so the stringified object the
core received no longer held the escape and R3 imported the file; the core
refuses the text whole (`lone_surrogate`), nothing altered. Why the refusal
is right: a Rust string cannot carry a lone surrogate, so reading such a
text means either altering it silently (L1 forbids it) or reading only the
parts that happen to survive duplicate-key resolution, which is reading a
document other than the one in the file. In `two-files-decoy-first` R3 did
not import the real envelope at all: it imported the decoy (conclusion
« Leurre ») that sorted first. No legitimate Faunosteo document is
affected: every PDF the product wrote comes from `JSON.stringify` of one
object, which never emits a duplicate key, and a lone surrogate in a kept
value is already refused by R3 (`error_lone_surrogate`); the proof is the
O2, O3, O7 and Step 5 reference set above, imported identically, and the
O8 corpus where the only lone-surrogate cases that import in R3 are the
three duplicate-key constructions.
- OD-3, one O8 case (`values-1m`): over the value cap (below). It is skipped
like a candidate nested too deep, so the kind follows the stamp
(`attachment_stripped` here, as `depth-65` already answers in R3).
- The `archiveSha256` column of the O7 `readReportArchive` rows is not
compared: the frozen TS domain stringified its Zod output, keys in schema
order, where the core keeps the document's order (R2); the R3 application
stored the core's form, which O8 pins and matches.
Memory protections (OD-3), measured in the shipped module, one fresh
instance per case (`packages/core-wasm/tests/pdf-import.budget.test.ts`,
`R4_BUDGET_ALL=1` for the slow classes; values from `cargo test --test
pdf_budget -- --nocapture`):
| Document | Bytes | Values (`json_js::prescan`) |
|---|---|---|
| demo fr logos (R3 export) | 305,938 | 2,559 |
| v2full fr logos (R3 export) | 298,035 | 2,232 |
| 612 leaves (O7 export) | 739,316 | 23,803 |
| L envelope (60 observations, 120 views, logos) | 403,016 | 6,740 |
| CAP envelope (500 observations, 1,000 views, logos) | 1,164,658 | 39,652 |
| hostile `values-1m.pdf` | 2,006,973 | 1,000,364 |
The parser's tree costs about 100 bytes of linear memory per value (the
final `Vec` doubling included). Cap off, a 16 MiB token-dense document
(8.4 million values) takes 784 MiB of linear memory and 0.9 s in Node, what
BLK-012 observed; every shape tried (numbers, empty strings, nesting at the
depth cap, the rescue copy) lands at the same 784 MiB, under the 1 GiB
ceiling, so the cap is the guard and the ceiling the backstop.
Thresholds set (to confirm before R4.3):
- `json_js::MAX_JSON_VALUES = 400,000`, ten times the CAP envelope (the plan's
rule), 17 times the 612-leaf export; applied in the attachment probe and in
`draft.restore` (the rescue copy and the tab's draft: an over-budget text is
`invalid`, set aside verbatim, never dropped). Worst case left: about 40 MiB
of tree on top of the file copy.
- `--max-memory` 1 GiB on the module (`.cargo/config.toml`): an allocation
past it traps (`allocate_for_test` on the proofs build: 256 MiB returns,
1,100 MiB throws `RuntimeError`), and `CoreRuntime` poisons and re-creates
the instance as for any trap. The ceiling applies to the export too; the
R4.6 measurements must stay well below it (CAP export estimated at about
0.4 GB in §B.4).
Per class, cap on (Node, i7-11700B, shipped module; "linear" is the
instance's growth, which never shrinks; "RSS" the Node process):
| Case | Outcome | ms | Linear MiB | Node RSS MiB |
|---|---|---|---|---|
| L envelope (394 KB) | ok | 11 | 0.6 | 0.4 |
| CAP envelope (1,138 KB) | ok | 40 | 10.0 | 14.9 |
| demo fr logos (R3 export, 237 KB) | ok | 7 | 0.0 | 1.3 |
| 612 leaves (O7 export) | ok | 23 | 2.8 | 5.4 |
| values-1m (O8 hostile, 2 MB attachment) | attachment_stripped | 11 | 3.1 | 3.0 |
| 16 MiB dense numbers (8.4 M values) | attachment_stripped | 103 | 16.1 | 16.0 |
| 16 MiB dense strings (5.6 M values) | attachment_stripped | 85 | 16.1 | 16.0 |
| 16 MiB at depth 64 | attachment_stripped | 92 | 16.1 | 16.0 |
| 16 MiB dense numbers as a rescue copy | invalid | 152 | 48.1 | 80.1 |
| 16 MiB at depth 65 as a rescue copy | invalid | 128 | 48.1 | 80.3 |
| 16 MiB inflation (zeros, not JSON) | attachment_stripped | 14 | 31.3 | 31.0 |
| 16 MiB + 1 inflation (bomb, refused) | attachment_stripped | 19 | 63.4 | 47.0 |
| 32 bombs (17 MiB each) before the attachment | attachment_stripped | 181 | 63.9 | 47.5 |
| 50 MiB file, attachment at the end | no_attachment | 73 | 50.0 | 50.0 |
Same classes, cap off: values-1m 109 ms and 98 MiB; the three 16 MiB
documents 0.6 to 0.9 s and 784 MiB; the rescue copy 0.4 s and 792 MiB. W4
(≤ 256 MiB linear on every hostile class) holds with the cap on; the 16 MiB
file copy and the inflation buffer dominate what is left.
Module: 1,251,849 B raw, 421,940 B gzip -9 (W1: +26 KB over R3, budget
600 KB, report line 550 KB).
Residual risks and open points for the owner (R4.2 checkpoint):
- The thresholds above are set from fixtures at the collection caps; a report
far denser than CAP per observation (hundreds of findings on each of 500
observations) would be refused as a hostile file. 400,000 values is 17
times the densest export seen (612 leaves on one observation).
- The ceiling is a module-wide constant: it bounds the import worker and the
main instance alike. If an R4.6 export measurement approaches it, the value
must be revisited, never the cap.
- The Node numbers measure the module, not a browser: the browser run of the
same classes (content-process RSS, worker terminated, GC) is in 10.4.
- Found by the independent security gate of R4.2 and fixed (593893a): the
value count stopped at the depth cap, so a rescue copy nested 65 deep hid
its 8 million values from the budget (792 MiB built, the cap-off figure).
The count now runs to the end whatever the depth verdict, and the budget
covers the archive text of `draft.restore` too. Reported, not blocking:
the TypeScript import path and `archive.import` carry no budget until
R4.3 replaces them (BLK-012, pre-existing); a 50 MiB file with 32 stream
heads walks its whitespace 32 times (783 ms against 269 ms, bounded by
`MAX_CANDIDATES`, as the TypeScript); the inflation buffer's capacity
reaches 2 × limit during its last growth (the 63 MiB row); the 50 MiB
size check runs after the glue copied the bytes, so the main thread's
pre-check must stay on the R4.3 worker path.
- The probe parses the candidate twice (version check, then the session),
as the TypeScript did; the peak is one tree since the first is dropped. A
single parse would need the gated session reader to take a value.
### 10.4 R4.2: the Rust import in the browsers (2026-10-18, `scripts/browser/results/2026-10-18-r4-import.json`)
Harness `scripts/browser/r4-import.mjs` (README there): the shape plan §B.1.5
gives the application at R4.3, a module worker created for one import, the
compiled `WebAssembly.Module` posted by the main thread, the `File` read in
the worker, one `SheetHandle.import_pdf`, the worker terminated; the same
226 files, engines, groups and RSS metrics as the TypeScript baseline of
10.2 (`compare-imports.mjs` prints both side by side). Chromium 156 headless
shell, Firefox 157, WebKit 27.2 (WPE), headless only; the 4 GiB RSS watchdog
is not a guaranteed memory limit. Module `ba070c29…`, 1,252,727 B, memory
maximum 16,384 pages (1 GiB).
Agreement: 666 of 678 (engine, file) pairs give the same outcome as the TS
import (editor ↔ `ok`, error ↔ a failure kind). The 12 others are the four
declared files in each engine: `values-1m` (OD-3), `surrogate-content-
overwritten`, `surrogate-version-overwritten`, `two-files-decoy-first`
(OD-4). Mapped to the §B.1.8 messages, no other difference.
Legitimate groups, TS → Rust (median ms on the Node clock, with the Rust
page clock after the slash; max RSS growth MiB summed / content):
| Engine | base | r3 | step6 | tolerance | read |
|---|---|---|---|---|---|
| Chromium ms | 53 → 65 / 32 | 63 → 80 / 43 | 61 → 65 / 41 | 57 → 63 / 31 | 66 → 68 / 29 |
| Chromium MiB | 28 / 23 → 17 / 15 | 44 / 38 → 16 / 16 | 39 / 33 → 17 / 17 | 105 / 99 → 18 / 16 | 75 / 68 → 117 / 115 |
| Firefox ms | 39 → 67 / 22 | 73 → 55 / 30 | 69 → 54 / 28 | 53 → 46 / 21 | 78 → 45 / 21 |
| Firefox MiB | 63 / 14 → 52 / 9 | 54 / 33 → 60 / 35 | 52 / 32 → 52 / 26 | 153 / 131 → 58 / 35 | 86 / 61 → 154 / 127 |
| WebKit ms | 52 → 45 / 25 | 62 → 55 / 37 | 66 → 50 / 33 | 59 → 44 / 26 | 75 → 43 / 24 |
| WebKit MiB | 52 / 50 → 61 / 59 | 54 / 52 → 71 / 67 | 53 / 51 → 60 / 59 | 102 / 100 → 70 / 67 | 101 / 99 → 324 / 299 |
The durations are not strictly like for like: the TS baseline ran the whole
application (the core loads during the import), the Rust page compiles the
module at page load (8 to 25 ms) and builds a `SheetHandle` per import
(about 21 ms in Chromium). The hostile `tolerance` group costs a fifth of
the TS memory under Rust. The `read` group costs more: its 20 to 50 MiB
inputs are held twice in the worker, the `File`'s ArrayBuffer and the glue's
copy in linear memory (54 MiB), and WebKit adds more on top
(`size-exactly-50mib`: Chromium 117 against 56 MiB, Firefox 154 against 79,
WebKit 324 against 76). R4.3 releases the buffer after the copy or reads the
file in chunks. Chromium main-thread long tasks during the imports: 0 (W5).
Hostile classes, Rust (Chromium / Firefox / WebKit; linear memory is the
instance's, the same in every engine; the GC delta is Chromium's content
process after the worker is terminated and a GC forced):
| Class | Outcome | Page ms | Summed peak MiB | Content peak MiB | Linear MiB | GC delta MiB |
|---|---|---|---|---|---|---|
| L envelope | ok | 46 / 36 / 45 | 4 / 11 / 9 | 6 / 9 / 7 | 4.6 | 11.7 |
| CAP envelope | ok | 88 / 77 / 83 | 31 / 20 / 39 | 29 / 18 / 35 | 13.9 | 14.2 |
| 16 MiB dense numbers | attachment_stripped | 183 / 199 / 130 | 45 / 47 / 124 | 47 / 45 / 114 | 20.0 | 11.8 |
| 16 MiB dense strings | attachment_stripped | 153 / 188 / 122 | 43 / 48 / 122 | 47 / 45 / 113 | 20.0 | 11.1 |
| 16 MiB at depth 64 | attachment_stripped | 180 / 198 / 136 | 45 / 47 / 123 | 48 / 45 / 114 | 20.0 | 12.2 |
| 16 MiB rescue copy | invalid | 178 / 190 / 164 | 113 / 145 / 234 | 115 / 143 / 225 | 52.0 | 15.4 |
| 16 MiB inflation | attachment_stripped | 43 / 37 / 43 | 20 / 12 / 13 | 18 / 10 / 11 | 35.3 | 11.1 |
| 16 MiB + 1 bomb | attachment_stripped | 50 / 44 / 49 | 5 / 8 / 10 | 7 / 7 / 8 | 67.3 | 12.1 |
| 32 bombs | attachment_stripped | 218 / 221 / 203 | 68 / 64 / 81 | 66 / 61 / 77 | 67.9 | 12.6 |
| 50 MiB file | no_attachment | 176 / 217 / 127 | 113 / 115 / 317 | 115 / 113 / 295 | 53.9 | 11.3 |
- W4: linear memory at most 67.9 MiB on every hostile class (budget 256).
- The ceiling: in the three engines, `allocate_for_test(256)` returns and
`allocate_for_test(1100)` throws `RuntimeError` (proofs build, linear
257 MiB). Cap off, the 16 MiB dense document reaches 788 MiB of linear
memory and 817 to 851 MiB of content-process peak in about 1 s; with the
cap, 45 to 114 MiB.
- Worker start, median (max): Chromium 6.3 (40.6) ms, Firefox 10.0 (20.0),
WebKit 5.0 (8.0). W2: the L envelope imports in 46 ms page-to-result in
Chromium, CAP in 88 ms. The structured clone of the compiled Module works
in the three engines (236 of 236 workers ready from the clone). No CSP
violation in any page or worker under the production policy with
`'wasm-unsafe-eval'`.
- The §B.3 "content process back within 10 MiB after GC" check is not met
as written on a first import: 3 of 238 Chromium runs; the median delta is
11.7 MiB (7.3 to 13.3; hostile max 15.4). It is a one-time cost, not a
leak: three imports in one page give +11.6, +1.9, −0.2 MiB (L) and +11.0,
+1.4, +0.2 (dense); an empty module worker leaves about 2 MiB, an
instantiated one 3.7, the cloned Module is not the cause (the same without
it). Firefox and WebKit got 5 s idle only (indicative: Firefox +4.8 then
flat, WebKit +24.5 / +43.2 then down to −3). Owner decision: the criterion
counts the first import, or the growth per import.
- Seen once in a WebKit smoke run before the full run: 2 of 3 repeated
imports of the 16 MiB dense document ended with a worker `error` event,
text not captured then; not reproduced in two targeted reruns nor in the
full run (0 failures across 242 WebKit workers). Error text is captured
now; to watch at J1.
Not measured: real devices and browsers (J1), forced GC in Firefox and
WebKit, CPU throttling, the hostile classes on the TS side, the negative
CSP proof (without the keyword), the application's own worker (R4.3).
### 10.5 R4.3: the import in a disposable worker (2026-10-18, application switched)
What runs where (`apps/app/src/core/worker/`, plan §B.1.5 with one amendment
below): the page refuses up front when its own engine stopped, routes on the
first KiB and checks the sizes as before, then creates a module worker
(`import.worker.ts`, the glue bundled statically into a hashed `/assets/`
chunk that `script-src 'self'` allows), posts the task with the bundled
sheet text and its runtime's compiled `WebAssembly.Module` (structured
clone: 236 of 236 workers in R4.2e, every engine), reads the file in 1 MiB
chunks with a BYOB reader and streams them to the worker (`chunk` /
`ack` with the buffer transferred both ways: the page holds one chunk, the
worker's `FileBytes` sink the only whole copy, no byte parsed on the page),
posts `end`, and reads the first of `result`, `failed`, an `error` event
(never masked), the start timer (10 s to `ready`), the task timer (20 s,
paused while the page is hidden; the J1 hypothesis) or a cancel; the worker
is terminated in every case. The worker answers `ready`, builds its
`SheetHandle`, runs `import_pdf_bytes` or `restore_copy_bytes` (strict
UTF-8, one BOM, the OD-3 budget) and answers the core's envelope; every
throwable becomes one `failed` with its phase and kind. The page then loads
its own index for `hydrateImport`, as before; a result is dropped when the
session changed since the pick, leaving Home cancels the import, one import
runs at a time, and the worker chunk is prefetched once the editor has
loaded the engine so an offline import still starts.
Amendment to §B.1.5 item 3 ("the `File` itself, never read on the main
thread"): the file is read on the page, chunk by chunk, because reading a
file inside a WebKit worker costs about 180 MiB of content-process RSS
whatever the method (`arrayBuffer`, default stream, BYOB; 14 MiB on the
page, 13 to 15 in Chromium and Firefox workers), measured in R4.3a on a
50 MiB file: worker read 272 to 316 MiB against 76 for the TypeScript, page
read feeding the worker's sink 106. The isolation stays: untrusted bytes are
parsed in the worker only; the page moves buffers.
Messages (§B.1.8, OD-5), pinned on every file of the frozen corpora by
`routes.HomeRoute.messages.test.tsx` through the real worker logic
in-process: O8 51 PDFs (47 as R3, the four declared), the stored O7 inputs
(over 130), the O2, O3 and Step 5 reference PDFs (editor), the 612-leaf
export (editor); a worker trap, a timeout or a file the page cannot read is
« Erreur de lecture du PDF. », a worker that cannot start is the engine
message, and the page's own runtime stays `ready` after a worker trap.
Suites: handler and client in-process (`core.importWorker`,
`core.importClient`), the six existing Home / core-failure suites migrated
to the real worker logic (53 cases, titles kept), 458 app tests.
Memory, large files (summed / content RSS growth MiB; TS reader · Rust,
worker read, R4.3a page · Rust, page read, R4.3d application build):
| File | Chromium | Firefox | WebKit |
|---|---|---|---|
| 50 MiB file (`size-exactly-50mib`) | 56/55 · 65/66 · 75/73 | 79/59 · 67/65 · 103/78 | 76/60 · 272/251 · 145/126 |
| 32 MiB (`32-raw-1mib-brackets`) | 46/40 · 48/48 · 75/70 | 53/52 · 49/47 · 88/62 | 50/39 · 185/170 · 124/112 |
| 20 MiB (`32-open-streams`) | 27/26 · 38/36 · 61/55 | 26/25 · 35/34 · 63/44 | 35/28 · 127/116 · 96/88 |
| 10 MiB (`one-10mib`, opens) | 58/53 · 26/26 · 35/33 | 61/38 · 26/25 · 58/36 | 79/73 · 81/74 · 76/72 |
| 16 MiB rescue copy (page) | – · 33/33 · – | – · 29/30 · – | – · 133/124 · – |
| CAP envelope (page) | – · 16/17 · – | – · 19/16 · – | – · 41/37 · – |
The streaming sink removed the file's JavaScript copy; the page-side read
brought WebKit from 272 to 145 MiB on a 50 MiB file (the probe without the
application around it gave 106; the application build adds about 40, three
samples 141 to 158). What remains in WebKit is its own cost of the module's
linear memory (the sink alone, no file read, measured at 81 MiB for 55 MiB
of linear memory) plus the worker; it cannot be removed without scanning
the file without holding it, which the reader's contract (§B.1.2, one 16 MiB
shared budget over candidates found anywhere in the file) does not allow
cheaply. Note the 50 MiB cap is older than R4 (`MAX_PDF_BYTES`): an export of
about 140 observations with rasters reaches it; L (121 views, 42 MB) imports.
Application build, the same 226 files and engines as the TypeScript baseline
(R4.3d, `scripts/browser/results/2026-10-18-r4-app-import.json`, dist of
e659924 built with `SHEET_BUNDLE=draft`; the hardenings of 8ddf185 are not in
that build, they change no outcome): outcome and message identical on 666 of
678 pairs, the 12 others the four declared files; 0 page errors; W5: 0
Chromium long task over 50 ms (the TypeScript showed 91 and 173 ms); small
files cost 25 to 35 ms more (the worker start and its sheet handle per
import); the `tolerance` group costs half the TypeScript's memory. Duration
medians, TS → Rust (ms): Chromium base 53 → 80, r3 63 → 100, step6 61 → 90,
tolerance 57 → 82, read 66 → 75; Firefox 39 → 71, 73 → 107, 69 → 104,
53 → 79, 78 → 81; WebKit 52 → 80, 62 → 89, 66 → 91, 59 → 78, 75 → 77.
CSP (`r4-app-csp.mjs`): with the keyword the reference PDF opens the editor
in the three engines (the known footer image violation only); without it,
« Le moteur de lecture n'a pas pu être chargé. Réessayez l'import. » in the
three engines, the button enabled again, no page error, every worker closed;
Chromium and Firefox report the worker's `wasm-eval` refusal through
`report-uri`, WebKit reports nothing for a worker.
Leak series on the application (R4.3d, content RSS, init = first import,
then six imports of the same document with `page.goto('/')` and the replace
dialog between them, 2 s idle each, 5 s at the end; init delta / growth over
the last five / the same after the final idle, three repetitions): Chromium
(forced GC) L 49 / 7.6, 9.7, 8.0 / 1.6, 3.6, 0.5; dense 10.6 / 2.1 ×3 / −0.6;
copy 42 / 6.8, 11.0, 4.8 / −3.5, 1.1, −4.4, per-import deltas at or under 2 MiB
after init. Firefox (idle only) L 72 / 42, 35, 44 / −5, −7, −13; dense 36 /
8.8, 9.1, 10.7 / −6; copy 60 / −2.6, 17.3, −5.0 / −21: a GC sawtooth reclaimed
by the final reading, as its navigation-only control. WebKit (idle only) L
102 to 110 / 53, 67, 53 / 44, 65, 46; dense 44 / 38, 37, 34 / 40; copy 88 /
69, 35, 44 / 61, 35, 41: above the 10 MiB tolerance in every series, but the
navigation-only control (same readings, no pick) grows 27 MiB too (about
5 MiB per same-tab navigation, the page cache), and a 12-import series on one
page levels off at 365 to 382 MiB; a surplus over the control remains (about
2 MiB per import for the refused document, 6 for one that opens the editor)
that cannot be called reclaimable without a forced GC. Twenty picks of the
dense document on one WebKit Home page: 20 workers created and closed, 0
page error, content 330 → 379 MiB, peak 405.
Leak series (R4.3a measurement page, content RSS, init = first import):
Chromium (GC forced) init 9 to 11 MiB, growth over the next five within
±0.5 on the three documents, three repetitions each; Firefox (idle only)
init 5 to 7, swings of ±3 from its own GC timing, final idle delta 0;
WebKit (idle only) init 17 to 38, the 16 MiB documents warm up in one or two
steps (dense +17 to +20 once; copy +12 to +15 then +17 to +19) and drop 3 to
6 MiB at the fifth import, so the copy misses the 10 MiB window as written
(+12.7 to +14.1 over the last five of six) while a 12-import series levels
off (−2.3 over its last five). Twenty consecutive dense imports in WebKit:
all refused, no worker `error` event (the one seen in an R4.2e smoke run did
not reproduce).
J1 device run (2026-10-18, `R4_J1.md`, owner decisions BDR-059: both
WebKit reserves accepted provisionally, PDF cap kept at 50 MiB): the owner
reports the functional checks they executed on a computer as passed, no
failure; iPhone / Safari iOS could not be run and is deferred, so it stays
not tested and the final acceptance of R4.3 waits for it (OD-6). The
browser and version, the checks covered, the timings (J1.8, J1.12) and the
memory observations (J1.9, J1.10) are recorded in `R4_J1_RESULTS.md` as the
owner confirms them. Headless engines never stand for Safari or a phone.
Residual risks and open points (R4.3, for the J1 checkpoint):
- WebKit, 50 MiB file: 145 MiB of content growth against 76 for the
TypeScript (BDR-058 gate "resolved or decided with numbers"): the owner
decides between accepting it (an absolute peak of about 150 MiB, below the
worker-read 324), lowering the PDF cap, or a streaming scanner later.
- WebKit leak criterion: not demonstrable headless without a forced GC
(above); the one-page series levels off; the device run at J1 gives the
real signal (Safari's memory warning or a reload).
- A hidden tab pauses the task timer by design (§B.1.5 item 3): a hostile
file keeps the worker's CPU and up to about 51 MiB busy in the background
until the tab is shown again; the core's own bounds (R4.2) are the only
limit then.
- The page reads the first KiB of the file to route it (unchanged since
R3); nothing else of the file is parsed on the page.
- The `IMPORT_TIMEOUT_MS` 20 s and the 10 s start timer are hypotheses to
confirm on the owner's devices (plan §B.3 rule).
- The import is 25 to 35 ms slower on small files (worker start and sheet
handle per import); posting the handle's work is not worth a shared
worker, which would keep hostile memory alive between imports.
- The TypeScript reader stays in `packages/pdf` until R4.8 (parity and the
full switch); `apps/app/src/pdf/import.ts` is now used by tests only.
### 10.6 R4.4: the layout in Rust (2026-10-18, nothing wired yet)
What runs in Rust (`crates/core/src/pdf/write`, feature `pdf-write`, enabled
by the wasm facade and linked into the module only once R4.5 and R4.6 call
it; the shipped size is unchanged until then): the text rules of
`packages/pdf/src/text.ts` and `render/text.ts` (`text.rs`: `safe_text`
with the Cc, Cf, Zl and Zp classes frozen from V8 in O4, not Rust's tables;
`js_upper` from the O4 runs and specials; `js_trim` and the JS `\s` set;
`measure` = Σ (WX or 250) + KPX over WinAnsi bytes, then × size / 1000 in
that order; `wrap_lines` with JS `split(/\s+/)` semantics, empty words
included), the AFM metrics and Unicode tables generated from O5 and O4 by
`scripts/r4-afm-data.mjs` (`afm_data.rs`, 82 KB, deterministic), the dates
of `render/dates.ts` (`dates.rs`: `Date.UTC` with years 0 to 99 mapped to
1900 to 1999 as A3 keeps them, proleptic Gregorian, fr-FR and en-US long
forms), the input checks of `export.ts` (`input.rs`: branding and labels
through their schemas, the snapshot gate with every asset through the R3
SVG reader, sheet mismatch, 500 observations and 8 views per observation)
and the four layouts (`layout/{header,footer,observation,conclusion}.rs`)
into a `DrawLog` (`draw_log.rs`: every text, image, line, rectangle call
with its arguments at full precision, per page with the footer last, the
links by page, the images in embed order with their pixel size read from
the PNG header; the PNG gate and decode are R4.5).
Parity (cargo, `pdf_text` and `pdf_layout`):
| Oracle | Check | Result |
|---|---|---|
| O4 `measure` | 2,333 rows × 3 fonts × 8 sizes | 55,992 widths equal; 4,800 recorded widths bit for bit |
| O4 `wrapLines` | 73 cases × 2 sizes × 8 widths | 1,168 wraps equal |
| O4 `safeText` | every code point 0..0x10FFFF (the 2,048 surrogates excepted: not a Rust `char`; their row is `question`) | 79 ranges equal |
| O4 `upper` | every scalar value | 1,580 changed code points equal; Rust's `to_uppercase` (Unicode 17) agrees with V8 on all of them |
| O5 | every WX by byte and every kerning pair | equal; WinAnsi pairs: Helvetica 1,295, Helvetica-Bold 1,181, Times-Roman 986 |
| O6 | 6,468 grid cells × 2 locales, 22 others | equal (1,355 refused cells) |
| O1 | 12 runs (3 reports × 2 locales × 2 logo modes), inputs = the envelope of each run's own R3 PDF, rasters = the goldens' stubs | 2,124 calls equal at full precision, embeds and links equal |
| Step 6 `text.*` and `images.*` | 24 goldens derived from the log in the engine's global call order (page contents, then footers), two decimals | equal |
Subtleties the port had to carry: U+00AD is WinAnsi (`hyphen`) but Cf, so
dropped, while U+00A0 is kept; kerning goes by glyph name, so bytes 32/160,
45/173 and 255/159 share their pairs; a leading or trailing whitespace run
gives an empty word to the wrap, which joins as a trailing space when it
fits; words are split on raw JS whitespace before `safeText` runs (U+200B is
dropped but not `\s`); `Date.UTC` applies the leap rule to the mapped year
(0000-02-29 is refused, 1900 is not leap); the first-page link order of the
saved file is per page (header, then footer), whatever the call order; the
Step 6 text golden is in global call order, footers after every page.
Declared differences: none in the oracles. The snapshot assets go through
the R3 SVG reader where the TypeScript ran its sanitiser (declared at R3,
RUST.md §9.4, plan §A.3): a foreign archive the sanitiser cleaned and the
reader refuses is `snapshot_invalid` with the reader's reason in the detail
(the detail reaches the DEV console only).
Cost (release build, i7-11700B): L (60 observations) 61 pages, 2,377 calls,
124 embeds, checks 1 ms, layout 3 ms; CAP (500 observations) 501 pages,
19,625 calls, 1,004 embeds, checks 1 ms, layout 24 ms (debug: 29 and 222 ms).
Residual risks and open points (R4.4):
- The PNG header alone sizes an image (`embedPng`'s `width` and `height`);
the UPNG acceptance rules and the branding PNG gate (`pngIssue`) are R4.5,
with the O6 PNG cases (108). A PNG the header reads and UPNG refuses is
not detected before R4.5.
- `rasterSizeOf` (the raster request size from a viewBox, JS `Number`
semantics) and `numberToString` are pinned in O4 but ported at R4.5 / R4.6
with the content emitter and the session.
- The module size impact of the writer is measured when R4.5 links it
(W1, the `afm_data` tables are 82 KB of source, the compiled tables about
12 KB).