diff --git a/CHANGELOG.md b/CHANGELOG.md index fc14cfc3..cdc36c73 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,10 +8,10 @@ Versions follow [Semantic Versioning](https://semver.org/). ### Changed (Rust R2, domain core, 2026-10-14) -- **The application's domain rules run in Rust** (`crates/core`, through the wasm facade and `@faunosteo/core-wasm`): sheet, report and archive contracts with the Zod semantics reproduced, the 21 sheet integrity rules and the publish check, the sheet index, the v2 → v3 migration, the rebase on the loaded sheet, the sheet snapshot, the layered archive reading, the editor's click chain and reducers, the findings and vertebra rules, report creation and the observation list operations. Consumers switched: the editor, the pickers, the store, the draft restore, the PDF import (after the engine's byte scan) and export (snapshot), the Supabase publish gate (fails closed). The module (372 KB gzip) loads only on the sheet pages; sign-in, the `/app` space and Home never request it. Without `'wasm-unsafe-eval'` in the CSP the editor shows a degraded notice with a retry and leaves the session's draft untouched (DEPLOY.md: set the keyword before deploying an R2 image; R2 images are never built on the VPS). +- **The application's domain rules run in Rust** (`crates/core`, through the wasm facade and `@faunosteo/core-wasm`): sheet, report and archive contracts with the Zod semantics reproduced, the 21 sheet integrity rules and the publish check, the sheet index, the v2 → v3 migration, the rebase on the loaded sheet, the sheet snapshot, the layered archive reading, the editor's click chain and reducers, the findings and vertebra rules, report creation and the observation list operations. Consumers switched: the editor, the pickers, the store, the draft restore, the PDF import (after the engine's byte scan) and export (snapshot), the Supabase publish gate (fails closed). The module (341 KB gzip) loads only on the sheet pages; sign-in, the `/app` space and Home never request it. Without `'wasm-unsafe-eval'` in the CSP the editor shows a degraded notice with a retry and leaves the session's draft untouched (DEPLOY.md: set the keyword before deploying an R2 image; R2 images are never built on the VPS). - **Session data never dropped silently**: an unreadable draft or archive is set aside under `osteo:quarantine:*` with a notice; a draft rebased with unresolved items keeps its raw text. - Behaviour differences, declared in `docs/development/RUST.md` §8.7: drafts and attachments written in contract order (values unchanged, the previous build reads them, D-R11); a lone UTF-16 surrogate escape reads as U+FFFD (L1, to confirm); typed errors on inputs the interface never produced. -- Proofs: contracts byte-equal to the Zod output on the reference documents, the three archive envelopes frozen before the switch and deep-equal after, 1,000+ cases frozen from the TypeScript, permanent N-1 reader test, generated pgTAP conformance file (real stack 446/446), CSP proof on the real image. No interface, archive format, database or graphic change; release 0.2.0 untouched. +- Proofs: contracts byte-equal to the Zod output on the reference documents, the three archive envelopes frozen before the switch and deep-equal after, 924 cases and verdicts frozen from the TypeScript, permanent N-1 reader test, generated pgTAP conformance file (real stack 446/446), CSP proof on the real image. No interface, archive format, database or graphic change; release 0.2.0 untouched. ### Changed (development environment, 2026-10-13) diff --git a/crates/core/src/domain/archive/read.rs b/crates/core/src/domain/archive/read.rs index c8c73789..8f06b607 100644 --- a/crates/core/src/domain/archive/read.rs +++ b/crates/core/src/domain/archive/read.rs @@ -4,7 +4,7 @@ //! wrapper is then validated whole or dropped with a reason. Accepted shapes: a bare Report v2 //! or v3, or an archive envelope of version 1. -use serde::Serialize; +use serde::{Deserialize, Serialize}; use serde_json::Value; #[cfg(feature = "ts")] use ts_rs::TS; @@ -95,8 +95,8 @@ fn read_envelope_v1( data: &Value, report: &Report, ) -> Result { - let wrapper: EnvelopeWrapper = - serde_json::from_value(data.clone()).map_err(|_| SnapshotRejection::Schema)?; + // Deserialised by reference: an attachment can be megabytes, never copy the whole value. + let wrapper = EnvelopeWrapper::deserialize(data).map_err(|_| SnapshotRejection::Schema)?; let snapshot = assert_snapshot(data.get("sheetSnapshot").unwrap_or(&Value::Null))?; if !snapshot_matches(&snapshot, report) { return Err(SnapshotRejection::SheetMismatch); @@ -121,5 +121,5 @@ fn snapshot_matches(snapshot: &SheetSnapshot, report: &Report) -> bool { /// `ReportArchiveEnvelopeSchema.parse`: the shape of a whole envelope (the snapshot's shape /// only, no caps nor integrity), as the session keeps it. pub fn parse_envelope(data: &Value) -> Result { - serde_json::from_value(data.clone()) + ReportArchiveEnvelope::deserialize(data) } diff --git a/crates/core/src/domain/archive/snapshot_rules.rs b/crates/core/src/domain/archive/snapshot_rules.rs index 0ac11d7f..3e068b4c 100644 --- a/crates/core/src/domain/archive/snapshot_rules.rs +++ b/crates/core/src/domain/archive/snapshot_rules.rs @@ -7,7 +7,7 @@ use std::collections::{HashMap, HashSet}; -use serde::Serialize; +use serde::{Deserialize, Serialize}; use serde_json::Value; #[cfg(feature = "ts")] use ts_rs::TS; @@ -195,8 +195,7 @@ fn snapshot_assets(sheet: &LoadedSheet) -> Vec { /// caps, integrity rules and every view reaching a carried asset (`integrity`). pub fn assert_snapshot(input: &Value) -> Result { assert_finding_depth(input)?; - let snapshot: SheetSnapshot = - serde_json::from_value(input.clone()).map_err(|_| SnapshotRejection::Schema)?; + let snapshot = SheetSnapshot::deserialize(input).map_err(|_| SnapshotRejection::Schema)?; assert_caps(&snapshot)?; assert_integrity(&snapshot)?; Ok(snapshot) diff --git a/crates/core/src/domain/report/read.rs b/crates/core/src/domain/report/read.rs index 28d153c0..e3fb57fe 100644 --- a/crates/core/src/domain/report/read.rs +++ b/crates/core/src/domain/report/read.rs @@ -6,7 +6,7 @@ //! truthiness of the original is kept on purpose: an empty `detailSourceId` means the main //! view, `requiresVetReferral: false` is dropped, `vertebrae: []` is kept. -use serde::Serialize; +use serde::{Deserialize, Serialize}; use serde_json::Value; #[cfg(feature = "ts")] use ts_rs::TS; @@ -56,7 +56,7 @@ pub fn read_report_document( ) -> Result { let version = version_of(data); if version == Some(3.0) { - let report: Report = serde_json::from_value(data.clone()).map_err(ReadFailure::invalid)?; + let report = Report::deserialize(data).map_err(ReadFailure::invalid)?; return Ok(MigrationOutcome { report, unresolved_zones: 0, @@ -64,8 +64,7 @@ pub fn read_report_document( }); } if version == Some(2.0) { - let report: ReportV2 = - serde_json::from_value(data.clone()).map_err(ReadFailure::invalid)?; + let report = ReportV2::deserialize(data).map_err(ReadFailure::invalid)?; return migrate_report_v2_to_v3(&report, map); } Err(ReadFailure::UnsupportedVersion(version)) @@ -108,7 +107,7 @@ pub fn migrate_report_v2_to_v3( /// `ReportSchema.parse` of a report built in code: every constraint checked again. pub(crate) fn reparse(report: &Report) -> Result { let value = serde_json::to_value(report).map_err(ReadFailure::invalid)?; - serde_json::from_value(value).map_err(ReadFailure::invalid) + Report::deserialize(&value).map_err(ReadFailure::invalid) } fn unresolved_counts(report: &Report) -> (u32, u32) { diff --git a/crates/core/src/domain/sheet/check.rs b/crates/core/src/domain/sheet/check.rs index d15e4d26..ee6f4e29 100644 --- a/crates/core/src/domain/sheet/check.rs +++ b/crates/core/src/domain/sheet/check.rs @@ -20,7 +20,7 @@ pub struct SheetCheck { /// Strict parse then rules; a document the shape refuses is an error, never a check. pub fn check_sheet_document(value: Value) -> Result { - let document: AnatomicalSheetDocument = serde_json::from_value(value)?; + let document = AnatomicalSheetDocument::deserialize(&value)?; let issues = validate_sheet_document(&document); let publishable = is_publishable(&issues); Ok(SheetCheck { diff --git a/crates/core/src/domain/sheet/create.rs b/crates/core/src/domain/sheet/create.rs index 9f6817bd..1c680433 100644 --- a/crates/core/src/domain/sheet/create.rs +++ b/crates/core/src/domain/sheet/create.rs @@ -1,6 +1,7 @@ //! Starting point of a new sheet (`anatomical-sheet/create.ts`): shape-valid so it can be //! saved as a draft at once, but not publishable until the main view gets its SVG asset. +use serde::Deserialize; use serde_json::json; use super::schema::{AnatomicalSheetDocument, LocalizedText, SHEET_DOCUMENT_SCHEMA_VERSION}; @@ -13,7 +14,7 @@ pub fn create_empty_sheet_document( species_id: &str, name: &LocalizedText, ) -> Result { - serde_json::from_value(json!({ + AnatomicalSheetDocument::deserialize(&json!({ "schema_version": SHEET_DOCUMENT_SCHEMA_VERSION, "species_id": species_id, "name": name, diff --git a/crates/core/src/domain/sheet/loaded.rs b/crates/core/src/domain/sheet/loaded.rs index dcbdc9aa..7629a759 100644 --- a/crates/core/src/domain/sheet/loaded.rs +++ b/crates/core/src/domain/sheet/loaded.rs @@ -75,8 +75,8 @@ impl From for LoadError { impl LoadedSheet { /// `parseBundledSheet` then `buildSheetIndex`. pub fn from_bundle(data: BundledSheetData) -> Result { - let document: AnatomicalSheetDocument = serde_json::from_value(data.document)?; - let migration_map: ReportMigrationMap = serde_json::from_value(data.migration_map)?; + let document = AnatomicalSheetDocument::deserialize(&data.document)?; + let migration_map = ReportMigrationMap::deserialize(&data.migration_map)?; let index = SheetIndex::build(&document)?; let r = data.reference; Ok(Self { diff --git a/crates/core/src/facade.rs b/crates/core/src/facade.rs index ecc93ede..bd3ce393 100644 --- a/crates/core/src/facade.rs +++ b/crates/core/src/facade.rs @@ -68,7 +68,7 @@ fn unknown(op: &str) -> Failure { /// Parses the JSON text of an operation input into its contract type. pub(crate) fn parse_input(input: &str) -> Result { let value = parse_json(input).map_err(|error| Failure::new("invalid_json", error))?; - serde_json::from_value(value).map_err(|error| Failure::new("invalid_shape", error)) + T::deserialize(&value).map_err(|error| Failure::new("invalid_shape", error)) } pub(crate) fn to_value(value: &T) -> Outcome { diff --git a/docs/development/RUST.md b/docs/development/RUST.md index 2da69adf..345696c4 100644 --- a/docs/development/RUST.md +++ b/docs/development/RUST.md @@ -573,19 +573,22 @@ Closed failure kinds: `invalid_json`, `invalid_shape`, `unknown_op`, | Measure | Value | |---|---| -| shipped module | 1,180,153 B raw (image) / 1,180,281 B (native), **372,275 B served gzip** (budget 600 KB) | +| shipped module | 1,046,498 B raw (native), **340,929 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 | | 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 | +| 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.6 ms (TypeScript layered read, without rebase: 1.0 ms) | -| `archive.snapshot` | 0.4 ms | -| linear memory after the editor path | 4.0 MiB | +| `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 382 KB more (glue + gzip module) once per session. +fetch about 351 KB more (glue + gzip module) once per session. Every JSON +input is deserialised by reference (`T::deserialize(&Value)`), never through +a copy of the value: a large attachment is not duplicated in linear memory, +and one deserialiser instead of two keeps the module smaller. ### 8.7 Behaviour differences (declared) @@ -623,7 +626,13 @@ fetch 382 KB more (glue + gzip module) once per session. - 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 +- The module grows with each phase (341 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). +- 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. diff --git a/scripts/wasm-build.sh b/scripts/wasm-build.sh index aed2ae90..473e4fb7 100755 --- a/scripts/wasm-build.sh +++ b/scripts/wasm-build.sh @@ -23,21 +23,23 @@ fi # Two modules from the same sources: pkg/ is the one the application ships (no trap export), # pkg-proofs/ adds the R0 trap exports for the poison-and-reinstantiate tests only (R2 plan P10). +# The caller removes the previous output on its literal path first (never through a variable). build_module() { local out="$1" shift cargo build -p faunosteo-wasm --release --target wasm32-unknown-unknown --locked "$@" - rm -rf "$out" wasm-bindgen --target web --out-dir "$out" --out-name faunosteo_wasm \ target/wasm32-unknown-unknown/release/faunosteo_wasm.wasm } # WASM_BUILD_SHIPPED_ONLY=1 (the application image) builds pkg/ alone; the stamp needs node, # absent from that stage, and only the test suites read it. +rm -rf packages/core-wasm/pkg if [ "${WASM_BUILD_SHIPPED_ONLY:-0}" = 1 ]; then build_module packages/core-wasm/pkg else stamp="$(node scripts/wasm-stamp.mjs)" + rm -rf packages/core-wasm/pkg-proofs build_module packages/core-wasm/pkg-proofs --features proofs echo "$stamp" > packages/core-wasm/pkg-proofs/BUILD_STAMP build_module packages/core-wasm/pkg