docs(docs): R2 — RUST.md §8 (migrated components, operations, runtime, census, proofs, measurements, behaviour differences, risks), CLAUDE.md, DEPLOY.md (CSP before an R2 image, never built on the VPS), WORKSPACE, TEST_STRATEGY, packages README, CHANGELOG

This commit is contained in:
bchanot committed 2026-10-14 00:51:44 +02:00
1 parent 7be94d4ce6
commit 8e8c58efca
7 files changed
+306 -76

No files matched your search

+213 -30
View File
@@ -1,13 +1,15 @@
# Rust in the Faunosteo monorepo
Status: plan R0 (foundation, validated 2026-10-13) and R1 (the Step 8 worker,
2026-10-13, `feature/step-08-files-foundation`). 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). The frontend does not load the browser module before R2.
Status: plan R0 (foundation, validated 2026-10-13), R1 (the Step 8 worker,
validated 2026-10-13) and R2 (the domain core, delivered 2026-10-14 on
`feature/rust-r2-domain-core`, awaiting the owner's validation). 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
@@ -109,10 +111,10 @@ 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 only
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 loader, generated types (committed), vitest proofs
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
@@ -124,23 +126,26 @@ under a 10 s CPU limit.
### 2.1 Boundary rules in code (review III.3)
- `faunosteo_core::facade::call(op, json) -> json`: one operation name, one
JSON text in, the outcome envelope out (`{"ok":true,"value":…}` or
`{"ok":false,"kind":…,"detail":…}`), never a panic on content. R0 ops:
`core_version`, `pretty_json`, `sheet_ref`, `report_zone`.
- `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`; `JsPrettyFormatter` is `serde_json`'s pretty formatter
with that rule; `to_js_pretty` and `pretty_js_round_trip` produce
`JSON.stringify(v, null, 2)` byte for byte (proof §5.2).
- `faunosteo_core::domain::report`: `SheetRef`, `ReportZone`, `Vertebra`,
`LateralSide`, `Unresolved`, field order of the Zod schemas, optional
fields `skip_serializing_if = "Option::is_none"`, nullable fields plain
`Option`, unknown keys ignored where Zod strips them. `cargo test -p
faunosteo-core --features ts` writes the TypeScript types to
`packages/core-wasm/types` (committed; CI fails on drift).
- `packages/core-wasm/src/index.ts`: `loadCore()` instantiates a **fresh**
module each time (cache-busting import of the glue), `callCore()` parses
the envelope, `isCoreBuilt()` lets the proofs skip when `pkg/` is absent.
`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, lone surrogates → U+FFFD
(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)
@@ -419,7 +424,8 @@ pnpm rust:builder && pnpm rust:check:docker && pnpm wasm:build:docker # the sa
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)
pnpm check:all # everything above but the CSP proof, in one command
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
@@ -431,9 +437,9 @@ pnpm check:all # everything above bu
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 (static glue URL with a
cache-busting query under Vite, or a Worker per instance) is designed, not
built: R2 builds and tests it.
- 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;
@@ -444,3 +450,180 @@ pnpm check:all # everything above bu
- 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 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.
- 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,180,153 B raw (image) / 1,180,281 B (native), **372,275 B served gzip** (budget 600 KB) |
| glue | 10,171 B |
| load (compile + instantiate) | 8 ms |
| sheet handle + index data (bundle 0.5 MB) | 22 ms (TypeScript parse + index on the same input: 12 ms) |
| `zone.click` | 0.04 ms |
| `draft.restore` (demo report) | 0.26 ms |
| `archive.import` (demo envelope, 184 KiB) | 2.6 ms (TypeScript layered read, without rebase: 1.0 ms) |
| `archive.snapshot` | 0.4 ms |
| linear memory after the editor path | 4.0 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 382 KB more (glue + gzip module) once per session.
### 8.7 Behaviour differences (declared)
1. **L1**: a lone UTF-16 surrogate escape in a draft or an attachment reads
as U+FFFD instead of being kept (TypeScript kept it, Rust strings cannot
hold it). Owner confirmation requested.
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`.
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. An unreadable draft that cannot be set aside (storage full) keeps the
editor on the degraded notice, whose text speaks of the engine: wording to
revisit if the case matters.
### 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 (372 KB of the 600 KB budget at R2; the
`url` crate's IDNA tables are part of it): R3/R4 must keep measuring.
- The documented CSP refuses the footer's external credit logo
(`img-src`), an older matter seen by the proof.
+4 -1
View File
@@ -18,7 +18,10 @@ Status: Step 0 deliverable, 2026-10-08. Current state: 32 files under
| PDF regression | vitest golden tests: text extraction of generated PDFs, attachment roundtrip, raster hash on fixed inputs | `packages/pdf/tests` | CI |
| SQL/TS contract | generated types diff check | CI | every PR touching `supabase/` |
| Rust core and facade | `cargo test` (unit + fixture tests reading the repository JSON in place), `clippy -D warnings`, `cargo deny`; ts-rs types drift check | `crates/*`, `pnpm rust:check` (native, `scripts/rust-check.sh`; same file in the builder image), `pnpm check:all` | before every Rust commit and every merge; CI job `rust` prepared, no runner yet |
| Rust/TS contract | vitest proofs on the built module: byte equality with `JSON.stringify(…, null, 2)` on the real documents, optional/nullable shape read by the current Zod schemas, panic and stack traps, served size budget | `packages/core-wasm/tests` (skipped with a notice when `pkg/` is absent) | `pnpm check:all` (after the native `wasm:build`); CI `check` prepared |
| Rust/TS contract | vitest on the built module: byte equality with `JSON.stringify(…, null, 2)` on the real documents, ts-rs types against the Zod output types, the runtime (lazy load, trap → poison → offline re-instantiation, handle rebuild, cap, retry), panic and stack traps on `pkg-proofs`, served size budget and measurements | `packages/core-wasm/tests` (refused, never skipped, when `pkg/` or `pkg-proofs/` is missing or stale) | `pnpm check:all` (after the native `wasm:build`); CI `check` prepared |
| Freeze-then-switch oracles (R2) | outputs of the TypeScript frozen before the switch and checked by cargo: Zod output of the reference documents (byte-equal), 84 report / archive cases, 765 Zod verdicts, 49 editor cases, 26 sheet conformance fixtures (also inlined into the generated pgTAP `011`, drift-checked), the three archive envelopes (deep-equal through the app path) | `crates/core/tests/{oracles,conformance,fixtures}`, generators in `packages/domain/tests/r2-*.test.ts` and `apps/app/tests/unit/archive.envelopes.test.ts` (`MAKE_R2_ORACLES` / `MAKE_R2_FREEZE`, never regenerated without an owner decision) | every Rust commit (`pnpm rust:check`), `pnpm check:all` |
| N-1 reader (permanent) | every draft and archive the core-fed application writes parses with the Zod schemas of the last TypeScript build (`packages/core-wasm/tests/n-1`, develop 1a7ee7d) | `apps/app/tests/unit/n-1.reader.test.ts`, `archive.envelopes.test.ts` | every commit (`pnpm test:run`) |
| CSP on the image (R2) | the real app image behind the DEPLOY.md CSP in headless Chromium: with `'wasm-unsafe-eval'` the core runs the editor path, without it the degraded state and an untouched draft; no module request on `/login`, `/app/account`, `/` | `docker/csp-proof/app-image.sh` | before a merge touching the core loading or the image; manual (needs Chromium) |
| Worker sandbox | integration tests against the musl binary that ships: fixture corpus through the full sandbox, refusals, SIGSYS on a denied syscall, output cap, `RLIMIT_AS` abort, `RLIMIT_CPU`, deadline; seccomp allowlist derived by `strace` and compared | `crates/worker/tests/sandbox.rs`, Dockerfile stage `test` (`pnpm worker:test`, Docker only, never native) | every `crates/worker` commit, `pnpm check:all`; CI `docker` job prepared |
| Worker job loop | native unit tests on a fake backend and a fake decoder: call order claim → download → decode → upload → commit → purge, every child outcome mapped to a closed code, no deletion on a refused commit, lease left on transport failures, sweeper targets and acks, key validation, config and secret redaction | `crates/worker/tests/job_loop.rs`, `crates/worker/src/*` unit tests (`pnpm rust:check`) | every Rust commit |
| Files e2e (real stack) | the worker container against the local Supabase stack: clean upload and owner-only download (Bob, anon, quarantine, direct final-key write refused), replacement and purge after the grace (queue clock shifted through psql), late signed-upload bytes swept, hostile uploads rejected with closed codes, removal and drained queue | `packages/supabase/tests/e2e/avatar-flow.e2e.ts` (`pnpm e2e:avatar`, `scripts/avatar-e2e.sh`) | every Storage, worker or avatar change; not in `check:all` (needs the stack) |
+4 -2
View File
@@ -43,8 +43,10 @@ root with `crates/core` (library), `crates/wasm` (browser facade, built into
runs on the native toolchain of the development machine (`pnpm rust:check`,
`pnpm wasm:build`; user-level rustup, channel of `rust-toolchain.toml`) or,
for a reproducible run, through the builder image (`:docker` variants); the
pnpm workspace gains `packages/core-wasm` (loader, generated types, proofs),
whose tests skip with a notice when the module has not been built.
pnpm workspace gains `packages/core-wasm` (runtime, generated types, the N-1
reader, tests). Since R2 the application depends on it at runtime (the sheet
pages load the module lazily) and the `core-wasm`, `app` and `supabase`
suites refuse to run without a fresh `pnpm wasm:build` (BUILD_STAMP).
| `supabase/` | migrations, functions, seed, tests | plan Step 3 |
## 2. Why this layout now