feat(domain): R5.6 prepared, fingerprint convention and registry inert, switch patch
canonical_json (RFC 8785), sheet_fingerprint behind feature fingerprint, TRUSTED_ALIASES (one equine pair), equivalence and check_declared; tests against the RFC vectors, the V8 numbers, an independent Node reference and the archives. No consumer wired: the switch is a rehearsed patch under .claude/tasks/plans.
This commit is contained in:
1 parent
1f8873dbce
commit
3b9a395d43
12 files changed
+1004
-1
No files matched your search
@@ -350,3 +350,8 @@ R5.4: `draft/{labels,changelog,review_report,drawing,overlay,app_review,svg_comp
|
||||
- BDR-069 serde: 3 v3 shapes refused at reader entry; all R2-R4/PDF/N-1 suites green unchanged.
|
||||
- Found: DEV stack on CLI default JWT secret + 54321 LAN-open → LAN service_role (ENVIRONMENTS §3.6, owner decision). SQL confirm characterised, unchanged.
|
||||
- R5.6 plan written, 4 decisions open. STOP.
|
||||
|
||||
## 2026-10-24 (R5.6 prep)
|
||||
- Owner: R5.5 validated, D-R5.6 decided (compiled registry authority, 5 consumers, Rust sole generator at switch). DEV key risk accepted temporarily, separate hardening task.
|
||||
- Fingerprint module inert + 9 tests; Rust draft value = frozen 12d7…74e2 first try. Switch as rehearsed patch (green), tree restored, --check 0 diff.
|
||||
- Rehearsal found: fingerprint before drawings bound (order), loaded.rs/facade.rs outside approved files. STOP before switch.
|
||||
@@ -339,8 +339,13 @@ feat/findings-tree-v2 merged (FindingsTreePicker tracked in HEAD); all work belo
|
||||
- [x] `confirm_sheet_asset_upload` characterised (no SQL change), DEV keys + 54321 documented (ENVIRONMENTS.md §3.6)
|
||||
- [x] R5.6 plan `.claude/tasks/plans/2026-10-24-rust-r5-6-fingerprint-switch.md` (4 decisions open)
|
||||
- [x] gates: round 1 check:all 03:39:24, security PASS (2 MEDIUM, 6 LOW), verifier ECARTS(2) → positional-parent bypass fixed (1ec9e1b); round 2 check:all 03:51:55, verifier K3/K4 behaviour MET, 2 formal gaps → contract Clarification (owner to confirm) → checkpoint, STOP (no R5.6 without signal)
|
||||
- [ ] R5.6 preparation (owner 2026-10-24 « arbitrage D-R5.6 »; nothing active)
|
||||
- [x] `domain/sheet/fingerprint.rs` + `sheet_fingerprint` tests (RFC, V8, Node differential, draft 12d7…, registry, census 123/124 alias)
|
||||
- [x] switch patch `.claude/tasks/plans/r5-6-switch.patch` rehearsed (rust:check + check:all 14:32:05 green), files restored
|
||||
- [ ] gates: check:all, security, fresh verifier → checkpoint, STOP (switch on signal)
|
||||
- [ ] R5.6 D-R5 switch (convention, aliases, membership test in gated readers with per-file go, seed regenerated once by the Rust tool, `.prettierignore`, CHANGELOG)
|
||||
- [ ] R5.7 consumer census, domain fixtures moved, packages deleted, xmldom + prettier out of the lockfile, docs, ledger; `pnpm check:all`; merge only on the owner's signal
|
||||
- [ ] DEV hardening (owner 2026-10-24, risk accepted temporarily, not scheduled): Supabase on CLI demo keys + 54321 LAN-open (rootless, UFW allow incoming, no 54321 rule) → own JWT secret/signing keys via env() or narrower 54321 rule before any untrusted device or user (ENVIRONMENTS.md §3.6); never these keys in production; `confirm_sheet_asset_upload` object check before public exposure
|
||||
- [ ] separate chore (D-R7): form constraints module + PGlite `pg_constraint`/config.toml parity test
|
||||
- [ ] Release 0.2.0 (branch release/0.2.0 = Steps 3–6, version.txt + package versions 0.2.0, CHANGELOG [0.2.0] 2026-10-11): deploy-trigger check done 2026-10-12 (CI = branches main/develop only, no tag trigger, no deploy job, docker job builds `faunosteo:ci` locally; hooks push only; DEPLOY.md deploy = manual on the VPS). Default 0.2.0 build carries NO sheet (SHEET_BUNDLE=none) → the live local tool must not be redeployed from main without `--build-arg SHEET_BUNDLE=draft`. Finish via /release-candidate (finish + tag span, tag push on explicit go); CHANGELOG back-merge conflict expected (Unreleased Step 7 above [0.2.0]). Awaiting the user's go.
|
||||
---
|
||||
|
||||
@@ -83,3 +83,23 @@ rules, the migration behaviour for a real content change. No view, zone, region,
|
||||
|
||||
The migration-map cascade debt (§11.12) stays open: no rename in this switch. Step 13 (`anatomical_sheet_versions` storing a
|
||||
fingerprint) is later. No merge, no release, no deploy.
|
||||
|
||||
## E. Owner arbitration 2026-10-24 and preparation state
|
||||
|
||||
Decided: convention confirmed; compiled registry the only authority, bound to sheet + content; five consumers
|
||||
(adds `pdf/compare/lines.rs`); Rust the only generator, `pnpm import:equine` → Rust, TypeScript kept as
|
||||
`import:equine:legacy` until R5.7; no visible change, no migration message. Prepared (RUST.md §11.19):
|
||||
module + tests committed inert; the switch as `.claude/tasks/plans/r5-6-switch.patch`, rehearsed green.
|
||||
|
||||
Still to decide at the switch signal:
|
||||
1. `loaded.rs` + `facade.rs` (sheet load refuses a contradicting block): outside the five approved files,
|
||||
not in CLAUDE.md's gated list (bundled sheet, not user data). Needed for "a contradiction is refused".
|
||||
2. The two `curation_draft` cases move to a declared rule for `sheet-ref.json` and `report-migration-map.json`
|
||||
(fingerprint field and block only), the R5.1 oracles untouched.
|
||||
|
||||
Switch procedure (on the signal): apply the patch; move the fingerprint after the drawings bound; declared
|
||||
rule in `curation_draft`; `pnpm import:equine` → `scripts/import-equine.sh`, TS as `import:equine:legacy`;
|
||||
`import-equine --out` into an empty folder, diff reviewed (only the two files of §11.19), copied over `draft/`;
|
||||
`--check` 0 differences; `pnpm rust:check`, `pnpm check:all`, security, fresh verifier; owner device check
|
||||
(an old PDF opens with no message). Rollback: `git revert` of the switch commits (seed back to `27c7…`, nothing
|
||||
outside git to undo); an archive exported meanwhile with `12d7…` would then rebase (DEV only).
|
||||
@@ -0,0 +1,336 @@
|
||||
diff --git a/.prettierignore b/.prettierignore
|
||||
index a9140e4..aaeef54 100644
|
||||
--- a/.prettierignore
|
||||
+++ b/.prettierignore
|
||||
@@ -7,3 +7,6 @@ packages/core-wasm/types/
|
||||
packages/core-wasm/pkg/
|
||||
packages/core-wasm/pkg-proofs/
|
||||
packages/core-wasm/tests/n-1/
|
||||
+# Written by the Rust pipeline (`pnpm r5:import-equine`, R5.6): byte-exact, never reformatted.
|
||||
+supabase/seed/anatomical-sheets/equine/legacy-import/
|
||||
+supabase/seed/anatomical-sheets/equine/draft/
|
||||
diff --git a/crates/core/src/curation/draft/pipeline.rs b/crates/core/src/curation/draft/pipeline.rs
|
||||
index 19126aa..12bcaf5 100644
|
||||
--- a/crates/core/src/curation/draft/pipeline.rs
|
||||
+++ b/crates/core/src/curation/draft/pipeline.rs
|
||||
@@ -8,6 +8,9 @@
|
||||
//! asset id) and copies them, and never re-cleans the legacy drawing. The review drawings and
|
||||
//! the two Markdown reports are R5.4.
|
||||
|
||||
+use crate::domain::sheet::fingerprint::{
|
||||
+ FINGERPRINT_CONVENTION, TRUSTED_ALIASES, sheet_fingerprint,
|
||||
+};
|
||||
use serde_json::{Map, Value, json};
|
||||
|
||||
use crate::json::to_js_compact;
|
||||
@@ -165,14 +168,25 @@ pub fn build_equine_draft(input: &DraftInput<'_>) -> Result<DraftResult, Pipelin
|
||||
})
|
||||
}
|
||||
|
||||
-/// What a report references: the sheet and the exact document bytes (the historical rule,
|
||||
-/// kept until the R5.6 switch).
|
||||
-fn sheet_ref(draft: &DraftResult, document_json: &str) -> SheetRef {
|
||||
- SheetRef {
|
||||
+/// What a report references: the sheet and its `sha256-rfc8785-validated-v1` fingerprint
|
||||
+/// (R5.6; the pre-switch rule hashed the formatted bytes, kept as a registered alias).
|
||||
+fn sheet_ref(draft: &DraftResult, document_json: &str) -> Result<SheetRef, PipelineError> {
|
||||
+ let fingerprint = sheet_fingerprint(document_json)
|
||||
+ .map_err(|error| fail(format!("document fingerprint: {error:?}")))?;
|
||||
+ Ok(SheetRef {
|
||||
sheet_id: draft.sheet_id.clone(),
|
||||
version_id: None,
|
||||
- document_sha256: sha256_hex(document_json.as_bytes()),
|
||||
- }
|
||||
+ document_sha256: fingerprint,
|
||||
+ })
|
||||
+}
|
||||
+
|
||||
+/// The registered aliases of this sheet's canonical value (descriptive in `sheet-ref.json`).
|
||||
+fn registered_aliases(reference: &SheetRef) -> Vec<&'static str> {
|
||||
+ TRUSTED_ALIASES
|
||||
+ .iter()
|
||||
+ .filter(|e| e.sheet_id == reference.sheet_id && e.canonical == reference.document_sha256)
|
||||
+ .map(|e| e.alias)
|
||||
+ .collect()
|
||||
}
|
||||
|
||||
/// The migration map (`report-migration-map.json` value) and its stats.
|
||||
@@ -307,7 +321,7 @@ pub fn render_draft_artifacts(
|
||||
legacy_anomalies: &[Anomaly],
|
||||
) -> Result<Vec<(String, String)>, PipelineError> {
|
||||
let document_json = format_json(&draft.document)?;
|
||||
- let reference = sheet_ref(draft, &document_json);
|
||||
+ let reference = sheet_ref(draft, &document_json)?;
|
||||
let (map, migration_stats) = draft_migration_map(draft, &reference)?;
|
||||
let assets = serde_json::to_value(&draft.assets)
|
||||
.map_err(|error| fail(format!("assets.json: {error}")))?;
|
||||
@@ -317,6 +331,10 @@ pub fn render_draft_artifacts(
|
||||
"document_sha256": reference.document_sha256,
|
||||
"schema_version": draft.document.get("schema_version"),
|
||||
"species_id": draft.document.get("species_id"),
|
||||
+ "fingerprint": {
|
||||
+ "convention": FINGERPRINT_CONVENTION,
|
||||
+ "aliases": registered_aliases(&reference),
|
||||
+ },
|
||||
});
|
||||
let changelog = render_changelog(
|
||||
&draft.records,
|
||||
diff --git a/crates/core/src/domain/archive/read.rs b/crates/core/src/domain/archive/read.rs
|
||||
index 5419b9d..677fe52 100644
|
||||
--- a/crates/core/src/domain/archive/read.rs
|
||||
+++ b/crates/core/src/domain/archive/read.rs
|
||||
@@ -4,6 +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 crate::domain::sheet::fingerprint::equivalence;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
#[cfg(feature = "ts")]
|
||||
@@ -115,7 +116,12 @@ fn read_envelope_v1(
|
||||
|
||||
fn snapshot_matches(snapshot: &SheetSnapshot, report: &Report) -> bool {
|
||||
snapshot.reference.sheet_id == report.sheet.sheet_id
|
||||
- && snapshot.reference.document_sha256 == report.sheet.document_sha256
|
||||
+ && equivalence(
|
||||
+ &report.sheet.sheet_id,
|
||||
+ &snapshot.reference.document_sha256,
|
||||
+ &report.sheet.document_sha256,
|
||||
+ )
|
||||
+ .same_document()
|
||||
}
|
||||
|
||||
/// `ReportArchiveEnvelopeSchema.parse`: the shape of a whole envelope (the snapshot's shape
|
||||
diff --git a/crates/core/src/domain/report/rebase.rs b/crates/core/src/domain/report/rebase.rs
|
||||
index 9281642..b5012be 100644
|
||||
--- a/crates/core/src/domain/report/rebase.rs
|
||||
+++ b/crates/core/src/domain/report/rebase.rs
|
||||
@@ -5,6 +5,7 @@
|
||||
//! snapshot when the report carries none, and the report now references the loaded sheet.
|
||||
//! Nothing happens when the report already sits on the loaded sheet.
|
||||
|
||||
+use crate::domain::sheet::fingerprint::equivalence;
|
||||
use serde::Serialize;
|
||||
#[cfg(feature = "ts")]
|
||||
use ts_rs::TS;
|
||||
@@ -37,7 +38,13 @@ pub fn rebase_report_on_sheet(
|
||||
sheet: &LoadedSheet,
|
||||
archived: Option<&SheetSnapshot>,
|
||||
) -> Result<RebaseOutcome, ReadFailure> {
|
||||
- if report.sheet.document_sha256 == sheet.reference.document_sha256 {
|
||||
+ // Same document: equal fingerprints, or the registry's pair for this very sheet (R5.6).
|
||||
+ let same_sheet = report.sheet.sheet_id == sheet.reference.sheet_id;
|
||||
+ let stamped = &report.sheet.document_sha256;
|
||||
+ let current = &sheet.reference.document_sha256;
|
||||
+ if stamped == current
|
||||
+ || (same_sheet && equivalence(&sheet.reference.sheet_id, stamped, current).same_document())
|
||||
+ {
|
||||
return Ok(RebaseOutcome {
|
||||
report: report.clone(),
|
||||
unresolved_zones: 0,
|
||||
diff --git a/crates/core/src/domain/sheet/loaded.rs b/crates/core/src/domain/sheet/loaded.rs
|
||||
index 4065978..aa88299 100644
|
||||
--- a/crates/core/src/domain/sheet/loaded.rs
|
||||
+++ b/crates/core/src/domain/sheet/loaded.rs
|
||||
@@ -2,6 +2,7 @@
|
||||
//! read once from the bundled payload, then indexed. Its document and migration map pass the
|
||||
//! strict schemas; the provenance fields come from the build and are taken as they are.
|
||||
|
||||
+use super::fingerprint::{AliasRefusal, check_declared};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use serde::Deserialize;
|
||||
@@ -33,6 +34,15 @@ pub struct BundledRef {
|
||||
pub document_sha256: String,
|
||||
pub schema_version: JsInt,
|
||||
pub species_id: String,
|
||||
+ /// Descriptive (R5.6): checked against the core's registry at load, never trusted alone.
|
||||
+ #[serde(default)]
|
||||
+ pub fingerprint: Option<DeclaredFingerprint>,
|
||||
+}
|
||||
+
|
||||
+#[derive(Debug, Clone, Deserialize)]
|
||||
+pub struct DeclaredFingerprint {
|
||||
+ pub convention: String,
|
||||
+ pub aliases: Vec<String>,
|
||||
}
|
||||
|
||||
/// A loaded, indexed sheet.
|
||||
@@ -76,6 +86,8 @@ pub enum LoadError {
|
||||
Shape(serde_json::Error),
|
||||
/// The document has no main view.
|
||||
NoMainView,
|
||||
+ /// The declared fingerprint block contradicts the core's registry (R5.6).
|
||||
+ Fingerprint(AliasRefusal),
|
||||
}
|
||||
|
||||
impl From<serde_json::Error> for LoadError {
|
||||
@@ -102,6 +114,15 @@ impl LoadedSheet {
|
||||
.map(|(id, text)| (id.clone(), read_drawing(text)))
|
||||
.collect();
|
||||
let r = data.reference;
|
||||
+ if let Some(declared) = &r.fingerprint {
|
||||
+ check_declared(
|
||||
+ &r.sheet_id,
|
||||
+ &r.document_sha256,
|
||||
+ &declared.convention,
|
||||
+ &declared.aliases,
|
||||
+ )
|
||||
+ .map_err(LoadError::Fingerprint)?;
|
||||
+ }
|
||||
Ok(Self {
|
||||
status: data.status,
|
||||
reference: SheetRef {
|
||||
diff --git a/crates/core/src/facade.rs b/crates/core/src/facade.rs
|
||||
index f0c6997..7f73cff 100644
|
||||
--- a/crates/core/src/facade.rs
|
||||
+++ b/crates/core/src/facade.rs
|
||||
@@ -53,6 +53,7 @@ pub fn load_sheet(bundle: &str) -> Result<LoadedSheet, Failure> {
|
||||
LoadedSheet::from_bundle(data).map_err(|error| match error {
|
||||
LoadError::Shape(error) => Failure::new("invalid_sheet", error),
|
||||
LoadError::NoMainView => Failure::new("invalid_sheet", "the sheet has no main view"),
|
||||
+ LoadError::Fingerprint(refusal) => Failure::new("invalid_sheet", format!("{refusal:?}")),
|
||||
})
|
||||
}
|
||||
|
||||
diff --git a/crates/core/src/pdf/compare/lines.rs b/crates/core/src/pdf/compare/lines.rs
|
||||
index 35e8dd2..4dfc313 100644
|
||||
--- a/crates/core/src/pdf/compare/lines.rs
|
||||
+++ b/crates/core/src/pdf/compare/lines.rs
|
||||
@@ -2,6 +2,7 @@
|
||||
//! current label next to the archived one, `!! UNRESOLVED` with the reason where the sheet
|
||||
//! cannot place an item. Every untrusted string goes through [`esc`].
|
||||
|
||||
+use crate::domain::sheet::fingerprint::{Equivalence, equivalence};
|
||||
use serde::Deserialize;
|
||||
use serde_json::Value;
|
||||
|
||||
@@ -346,8 +347,23 @@ fn archive_line(outcome: &ArchiveReadOutcome) -> Vec<String> {
|
||||
|
||||
fn sheet_line(report: &Report, sheet: &LoadedSheet) -> String {
|
||||
let reference = &sheet.reference;
|
||||
- let same_sheet = report.sheet.sheet_id == reference.sheet_id
|
||||
- && report.sheet.document_sha256 == reference.document_sha256;
|
||||
+ let same = equivalence(
|
||||
+ &reference.sheet_id,
|
||||
+ &report.sheet.document_sha256,
|
||||
+ &reference.document_sha256,
|
||||
+ );
|
||||
+ let same_sheet = report.sheet.sheet_id == reference.sheet_id && same.same_document();
|
||||
+ if same_sheet && same == Equivalence::TrustedAlias {
|
||||
+ // Same content under the registered historical fingerprint: both raw values shown.
|
||||
+ let stamped: String = report.sheet.document_sha256.chars().take(12).collect();
|
||||
+ let current: String = reference.document_sha256.chars().take(12).collect();
|
||||
+ return format!(
|
||||
+ "sheet {} (matches the bundled sheet through the trusted alias: report sha {}…, sheet sha {}…)",
|
||||
+ esc(&reference.sheet_id),
|
||||
+ esc(&stamped),
|
||||
+ esc(¤t)
|
||||
+ );
|
||||
+ }
|
||||
if same_sheet {
|
||||
let status = match sheet.status {
|
||||
SheetStatus::Draft => "draft",
|
||||
diff --git a/crates/core/src/pdf/write/input.rs b/crates/core/src/pdf/write/input.rs
|
||||
index 120249a..6a4bfe5 100644
|
||||
--- a/crates/core/src/pdf/write/input.rs
|
||||
+++ b/crates/core/src/pdf/write/input.rs
|
||||
@@ -2,6 +2,7 @@
|
||||
//! branding and the labels through their schemas again, the snapshot gate with every asset
|
||||
//! accepted by the SVG reader, the report's sheet and the page caps. Today's error kinds.
|
||||
|
||||
+use crate::domain::sheet::fingerprint::equivalence;
|
||||
use std::collections::HashMap;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -203,9 +204,12 @@ fn reason_of(rejection: SnapshotRejection) -> String {
|
||||
/// Steps 2 and 3: the report belongs to this sheet and fits the page budget.
|
||||
fn assert_exportable(report: &Report, snapshot: &SheetSnapshot) -> Result<(), ExportError> {
|
||||
let reference = &snapshot.reference;
|
||||
- if report.sheet.sheet_id != reference.sheet_id
|
||||
- || report.sheet.document_sha256 != reference.document_sha256
|
||||
- {
|
||||
+ let same = equivalence(
|
||||
+ &reference.sheet_id,
|
||||
+ &report.sheet.document_sha256,
|
||||
+ &reference.document_sha256,
|
||||
+ );
|
||||
+ if report.sheet.sheet_id != reference.sheet_id || !same.same_document() {
|
||||
return Err(error(ExportErrorKind::SheetMismatch, ""));
|
||||
}
|
||||
if report.observations.len() > MAX_OBSERVATIONS {
|
||||
diff --git a/crates/core/tests/fingerprint_consumers.rs b/crates/core/tests/fingerprint_consumers.rs
|
||||
new file mode 100644
|
||||
index 0000000..bda5a2e
|
||||
--- /dev/null
|
||||
+++ b/crates/core/tests/fingerprint_consumers.rs
|
||||
@@ -0,0 +1,69 @@
|
||||
+//! R5.6 switch rehearsal (part of `.claude/tasks/plans/r5-6-consumers.patch`, not committed
|
||||
+//! active): with the loaded sheet stamped with the canonical `v1` value, a report or an archive
|
||||
+//! stamped with the registered historical value is the same document (no rebase, archive kept);
|
||||
+//! a real content change, a forged value or another sheet still differ.
|
||||
+#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)]
|
||||
+
|
||||
+mod archive_support;
|
||||
+mod common;
|
||||
+
|
||||
+use archive_support::{draft_map, draft_sheet, json_file};
|
||||
+use faunosteo_core::domain::archive::read_archive_document;
|
||||
+use faunosteo_core::domain::report::{Report, rebase_report_on_sheet};
|
||||
+use faunosteo_core::domain::sheet::LoadedSheet;
|
||||
+use serde_json::Value;
|
||||
+
|
||||
+const ALIAS: &str = "27c7383c445c73dc2001ebf4794a3ea63d416910bbbaa0d7848dd720de7c2660";
|
||||
+const CANONICAL: &str = "12d788727c1e532641bd5b3a7ecf39dbdd25e70350f8a76be5ca917b262874e2";
|
||||
+
|
||||
+fn switched_sheet() -> LoadedSheet {
|
||||
+ let mut sheet = draft_sheet();
|
||||
+ assert_eq!(sheet.reference.document_sha256, ALIAS);
|
||||
+ sheet.reference.document_sha256 = CANONICAL.to_owned();
|
||||
+ sheet
|
||||
+}
|
||||
+
|
||||
+fn demo(sha: &str) -> Report {
|
||||
+ let mut value = json_file("apps/app/tests/fixtures/report-v3.demo.json");
|
||||
+ value["sheet"]["documentSha256"] = Value::from(sha);
|
||||
+ serde_json::from_value(value).unwrap()
|
||||
+}
|
||||
+
|
||||
+#[test]
|
||||
+fn a_report_on_the_historical_value_is_not_rebased_and_keeps_its_stamp() {
|
||||
+ let outcome = rebase_report_on_sheet(&demo(ALIAS), &switched_sheet(), None).unwrap();
|
||||
+ assert!(!outcome.rebased);
|
||||
+ assert_eq!(outcome.report.sheet.document_sha256, ALIAS);
|
||||
+}
|
||||
+
|
||||
+#[test]
|
||||
+fn a_real_change_or_a_forged_value_still_rebases() {
|
||||
+ for sha in ["a".repeat(64), "b".repeat(64)] {
|
||||
+ let outcome = rebase_report_on_sheet(&demo(&sha), &switched_sheet(), None).unwrap();
|
||||
+ assert!(outcome.rebased, "{sha}");
|
||||
+ }
|
||||
+}
|
||||
+
|
||||
+#[test]
|
||||
+fn an_archive_pairing_the_alias_and_the_canonical_value_is_kept_and_a_forged_pair_is_not() {
|
||||
+ let base = json_file("apps/app/tests/fixtures/goldens/r2/envelope.demo.fr.nologos.json");
|
||||
+ let map = draft_map();
|
||||
+ let mut paired = base.clone();
|
||||
+ paired["sheetSnapshot"]["ref"]["documentSha256"] = Value::from(CANONICAL);
|
||||
+ assert!(
|
||||
+ read_archive_document(&paired, &map)
|
||||
+ .unwrap()
|
||||
+ .archive
|
||||
+ .is_some()
|
||||
+ );
|
||||
+ let mut forged = base.clone();
|
||||
+ forged["sheetSnapshot"]["ref"]["documentSha256"] = Value::from("c".repeat(64));
|
||||
+ let read = read_archive_document(&forged, &map).unwrap();
|
||||
+ assert!(read.archive.is_none() && read.snapshot_rejected.is_some());
|
||||
+ assert!(
|
||||
+ read_archive_document(&base, &map)
|
||||
+ .unwrap()
|
||||
+ .archive
|
||||
+ .is_some()
|
||||
+ );
|
||||
+}
|
||||
@@ -24,7 +24,10 @@ pdf-write = ["pdf-read"]
|
||||
cli = ["pdf-read", "pdf-write"]
|
||||
# The anatomical pipeline (R5): legacy importer, curation, generated files and their
|
||||
# formatting, the `import-equine` tool. Never enabled by the wasm facade or the worker.
|
||||
curation = ["dep:sha2", "dep:icu_normalizer"]
|
||||
curation = ["fingerprint", "dep:icu_normalizer"]
|
||||
# The sheet fingerprint `sha256-rfc8785-validated-v1` (R5.6, prepared, not active): the hashing
|
||||
# half of `domain::sheet::fingerprint`; the registry and the comparison compile without it.
|
||||
fingerprint = ["dep:sha2"]
|
||||
# The sheet-asset upload tool (R5.5): the `upload-sheet-assets` binary, a local-stack operator
|
||||
# tool. The rules are in `curation::upload`; only the binary reaches the network.
|
||||
upload = ["curation", "dep:ureq"]
|
||||
@@ -151,3 +154,7 @@ required-features = ["curation"]
|
||||
[[test]]
|
||||
name = "upload_tool"
|
||||
required-features = ["upload"]
|
||||
|
||||
[[test]]
|
||||
name = "sheet_fingerprint"
|
||||
required-features = ["fingerprint", "pdf-read"]
|
||||
@@ -0,0 +1,223 @@
|
||||
//! The sheet fingerprint convention `sha256-rfc8785-validated-v1` (D-R5.6, owner 2026-10-24,
|
||||
//! BDR-068) and the trusted alias registry. PREPARED, NOT ACTIVE: no reader, writer or
|
||||
//! generated file uses this module until the owner's switch signal (RUST.md §11.19).
|
||||
//!
|
||||
//! The value is the SHA-256 of the RFC 8785 (JCS) text of the validated document: the strict
|
||||
//! typed reading of the R2 contract (`AnatomicalSheetDocument`, Zod's defaults filled, unknown
|
||||
//! keys dropped) with zero integrity issue, serialised back. JCS: members sorted recursively by
|
||||
//! the UTF-16 code units of their names, numbers as ECMAScript `Number::toString`, strings with
|
||||
//! the `JSON.stringify` escape set and every other character kept as is (no Unicode
|
||||
//! normalisation), no whitespace, UTF-8. `v1` freezes these rules and the validation.
|
||||
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::json::js_number;
|
||||
|
||||
/// The convention's name, as `sheet-ref.json` will declare it at the switch.
|
||||
pub const FINGERPRINT_CONVENTION: &str = "sha256-rfc8785-validated-v1";
|
||||
/// The pre-switch convention: SHA-256 of the Prettier-formatted `document.json` bytes.
|
||||
pub const LEGACY_CONVENTION: &str = "sha256-prettier-file-bytes-v0";
|
||||
|
||||
/// One trusted equivalence: under `sheet_id`, the content whose canonical fingerprint is
|
||||
/// `canonical` was stamped `alias` under `alias_convention`. Bound to the sheet and to that
|
||||
/// content, never global; built from the frozen oracles, never from an archive, a bundle or the
|
||||
/// database.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct TrustedAlias {
|
||||
pub sheet_id: &'static str,
|
||||
pub canonical: &'static str,
|
||||
pub alias: &'static str,
|
||||
pub alias_convention: &'static str,
|
||||
}
|
||||
|
||||
/// The registry, the only authority over equivalences. Equine draft: `alias` is the committed
|
||||
/// `sheet-ref.json` value and `oracles/r5/fingerprints.json` `today_file_bytes`; `canonical` is
|
||||
/// the same oracle's `b_rfc8785_validated`, recomputed by `sheet_fingerprint` (test).
|
||||
pub const TRUSTED_ALIASES: &[TrustedAlias] = &[TrustedAlias {
|
||||
sheet_id: "503dc301-a8b1-50d4-9e44-0919b2fc2916",
|
||||
canonical: "12d788727c1e532641bd5b3a7ecf39dbdd25e70350f8a76be5ca917b262874e2",
|
||||
alias: "27c7383c445c73dc2001ebf4794a3ea63d416910bbbaa0d7848dd720de7c2660",
|
||||
alias_convention: LEGACY_CONVENTION,
|
||||
}];
|
||||
|
||||
/// How two fingerprints of one sheet relate. The raw values stay with the caller, so a
|
||||
/// diagnostic can still print both.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Equivalence {
|
||||
Identical,
|
||||
/// Different values the registry binds to the same content of this sheet.
|
||||
TrustedAlias,
|
||||
Different,
|
||||
}
|
||||
|
||||
impl Equivalence {
|
||||
pub fn same_document(self) -> bool {
|
||||
self != Self::Different
|
||||
}
|
||||
}
|
||||
|
||||
/// `a` and `b`, two fingerprints stamped for `sheet_id`: equal, a registered pair of that sheet
|
||||
/// (either way round), or different. A real content change is never registered, so it stays
|
||||
/// `Different` and rebases as before.
|
||||
pub fn equivalence(sheet_id: &str, a: &str, b: &str) -> Equivalence {
|
||||
if a == b {
|
||||
return Equivalence::Identical;
|
||||
}
|
||||
let paired = TRUSTED_ALIASES.iter().any(|entry| {
|
||||
entry.sheet_id == sheet_id
|
||||
&& ((entry.canonical == a && entry.alias == b)
|
||||
|| (entry.canonical == b && entry.alias == a))
|
||||
});
|
||||
if paired {
|
||||
Equivalence::TrustedAlias
|
||||
} else {
|
||||
Equivalence::Different
|
||||
}
|
||||
}
|
||||
|
||||
/// Why declared metadata contradicts the registry.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum AliasRefusal {
|
||||
/// The declared convention is not `v1`.
|
||||
UnknownConvention(String),
|
||||
/// A declared alias the registry does not bind to this sheet and canonical value.
|
||||
Untrusted(String),
|
||||
}
|
||||
|
||||
/// Checks what a `sheet-ref.json` declares (descriptive only): the convention must be `v1` and
|
||||
/// every listed alias must be registered for this sheet and this canonical value. A
|
||||
/// declaration never creates an equivalence.
|
||||
pub fn check_declared(
|
||||
sheet_id: &str,
|
||||
canonical: &str,
|
||||
convention: &str,
|
||||
aliases: &[String],
|
||||
) -> Result<(), AliasRefusal> {
|
||||
if convention != FINGERPRINT_CONVENTION {
|
||||
return Err(AliasRefusal::UnknownConvention(convention.to_owned()));
|
||||
}
|
||||
for alias in aliases {
|
||||
let trusted = TRUSTED_ALIASES.iter().any(|entry| {
|
||||
entry.sheet_id == sheet_id && entry.canonical == canonical && entry.alias == alias
|
||||
});
|
||||
if !trusted {
|
||||
return Err(AliasRefusal::Untrusted(alias.clone()));
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// RFC 8785 text of a JSON value. `None` for a number with no I-JSON form (never produced by
|
||||
/// the core's parser, which refuses non-finite values).
|
||||
pub fn canonical_json(value: &Value) -> Option<String> {
|
||||
let mut out = String::new();
|
||||
write_value(value, &mut out)?;
|
||||
Some(out)
|
||||
}
|
||||
|
||||
fn write_value(value: &Value, out: &mut String) -> Option<()> {
|
||||
match value {
|
||||
Value::Null => out.push_str("null"),
|
||||
Value::Bool(flag) => out.push_str(if *flag { "true" } else { "false" }),
|
||||
Value::Number(number) => {
|
||||
let float = number.as_f64().filter(|f| f.is_finite())?;
|
||||
out.push_str(&js_number(float));
|
||||
}
|
||||
Value::String(text) => write_string(text, out),
|
||||
Value::Array(items) => {
|
||||
out.push('[');
|
||||
for (index, item) in items.iter().enumerate() {
|
||||
if index > 0 {
|
||||
out.push(',');
|
||||
}
|
||||
write_value(item, out)?;
|
||||
}
|
||||
out.push(']');
|
||||
}
|
||||
Value::Object(map) => write_object(map, out)?,
|
||||
}
|
||||
Some(())
|
||||
}
|
||||
|
||||
fn write_object(map: &serde_json::Map<String, Value>, out: &mut String) -> Option<()> {
|
||||
let mut entries: Vec<(Vec<u16>, &String, &Value)> = map
|
||||
.iter()
|
||||
.map(|(key, item)| (key.encode_utf16().collect(), key, item))
|
||||
.collect();
|
||||
entries.sort_by(|left, right| left.0.cmp(&right.0));
|
||||
out.push('{');
|
||||
for (index, (_, key, item)) in entries.iter().enumerate() {
|
||||
if index > 0 {
|
||||
out.push(',');
|
||||
}
|
||||
write_string(key, out);
|
||||
out.push(':');
|
||||
write_value(item, out)?;
|
||||
}
|
||||
out.push('}');
|
||||
Some(())
|
||||
}
|
||||
|
||||
/// `JSON.stringify` of a string: `"` `\` and the controls escaped (short forms where JSON has
|
||||
/// them, `\u00xx` lowercase otherwise), everything else verbatim.
|
||||
fn write_string(text: &str, out: &mut String) {
|
||||
out.push('"');
|
||||
for character in text.chars() {
|
||||
match character {
|
||||
'"' => out.push_str("\\\""),
|
||||
'\\' => out.push_str("\\\\"),
|
||||
'\u{8}' => out.push_str("\\b"),
|
||||
'\u{c}' => out.push_str("\\f"),
|
||||
'\n' => out.push_str("\\n"),
|
||||
'\r' => out.push_str("\\r"),
|
||||
'\t' => out.push_str("\\t"),
|
||||
control if u32::from(control) < 0x20 => {
|
||||
out.push_str(&format!("\\u{:04x}", u32::from(control)));
|
||||
}
|
||||
other => out.push(other),
|
||||
}
|
||||
}
|
||||
out.push('"');
|
||||
}
|
||||
|
||||
#[cfg(feature = "fingerprint")]
|
||||
pub use hashing::{FingerprintError, sheet_fingerprint};
|
||||
|
||||
#[cfg(feature = "fingerprint")]
|
||||
mod hashing {
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
use super::canonical_json;
|
||||
use crate::domain::js::parse_json;
|
||||
use crate::domain::sheet::schema::AnatomicalSheetDocument;
|
||||
use crate::domain::sheet::validate::validate_sheet_document;
|
||||
|
||||
/// Why a document has no fingerprint: an invalid sheet is never stamped.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum FingerprintError {
|
||||
Parse(String),
|
||||
Shape(String),
|
||||
/// The integrity rules found issues (their count).
|
||||
Invalid(usize),
|
||||
Canonical,
|
||||
}
|
||||
|
||||
/// `sha256-rfc8785-validated-v1` of a document text: strict parse, R2 shape, zero
|
||||
/// integrity issue, then JCS of the typed reading serialised back.
|
||||
pub fn sheet_fingerprint(text: &str) -> Result<String, FingerprintError> {
|
||||
let value = parse_json(text).map_err(|e| FingerprintError::Parse(e.to_string()))?;
|
||||
let document: AnatomicalSheetDocument =
|
||||
serde_json::from_value(value).map_err(|e| FingerprintError::Shape(e.to_string()))?;
|
||||
let issues = validate_sheet_document(&document);
|
||||
if !issues.is_empty() {
|
||||
return Err(FingerprintError::Invalid(issues.len()));
|
||||
}
|
||||
let validated =
|
||||
serde_json::to_value(&document).map_err(|e| FingerprintError::Shape(e.to_string()))?;
|
||||
let canonical = canonical_json(&validated).ok_or(FingerprintError::Canonical)?;
|
||||
Ok(Sha256::digest(canonical.as_bytes())
|
||||
.iter()
|
||||
.map(|byte| format!("{byte:02x}"))
|
||||
.collect())
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
pub mod check;
|
||||
pub mod create;
|
||||
pub mod fingerprint;
|
||||
pub mod index;
|
||||
pub mod index_data;
|
||||
pub mod loaded;
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
{
|
||||
"frozen_from": "Node v24.21.0",
|
||||
"cases": [
|
||||
{
|
||||
"input": "[1e21, 1e-7, 1E+2, 0.1, -0, 5e-324, 1.7976931348623157e308, 123456789012345678901, 9007199254740993, 100, 1.5e-6, 0.000001, 1e20, 999999999999999999999]",
|
||||
"canonical": "[1e+21,1e-7,100,0.1,0,5e-324,1.7976931348623157e+308,123456789012345680000,9007199254740992,100,0.0000015,0.000001,100000000000000000000,1e+21]",
|
||||
"sha256": "454757d41a64c2a76e8fd9c555ce7f7732241ac8d192a731ccb880d5cdde1f25"
|
||||
},
|
||||
{
|
||||
"input": "{\"\\ud83d\\ude00\": 1, \"\\ufb33\": 2, \"\\u00e9\": 3, \"e\\u0301\": 4, \"Z\": 5, \"a\": 6, \"\": 7, \"aa\": 8, \"\\u0080\": 9}",
|
||||
"canonical": "{\"\":7,\"Z\":5,\"a\":6,\"aa\":8,\"é\":4,\"\":9,\"é\":3,\"😀\":1,\"דּ\":2}",
|
||||
"sha256": "781d348d29b6dc2fcb75ed58f847fbc19ebb6f2dd526e5aa67e130a074a7200a"
|
||||
},
|
||||
{
|
||||
"input": "{\"s\": \"\\u0000\\u0001\\u001f\\b\\f\\n\\r\\t\\\"\\\\/\\u007f\\u2028\\u2029\\u00e9e\\u0301\\ud83d\\ude00\"}",
|
||||
"canonical": "{\"s\":\"\\u0000\\u0001\\u001f\\b\\f\\n\\r\\t\\\"\\\\/
éé😀\"}",
|
||||
"sha256": "ecaac6f9c64eb5f89acb33f7f98b840e3c07b8c91551e184af89aa1da702d0bb"
|
||||
},
|
||||
{
|
||||
"input": "{\"b\": {\"z\": [true, false, null, {\"y\": 1, \"x\": [ ]}], \"a\": {}}, \"a\": [[[]]]}",
|
||||
"canonical": "{\"a\":[[[]]],\"b\":{\"a\":{},\"z\":[true,false,null,{\"x\":[],\"y\":1}]}}",
|
||||
"sha256": "326c7cd13dafbbe0412b029969bf0b2994bdeea90313ca59b2cc4c5015c15447"
|
||||
},
|
||||
{
|
||||
"input": "{\"geometry\": {\"cx\": 743.0451049804688, \"cy\": 469.1115417480469, \"r\": 12}, \"name\": {\"fr\": \"Atlas (C1)\", \"en\": \"Atlas\"}}",
|
||||
"canonical": "{\"geometry\":{\"cx\":743.0451049804688,\"cy\":469.1115417480469,\"r\":12},\"name\":{\"en\":\"Atlas\",\"fr\":\"Atlas (C1)\"}}",
|
||||
"sha256": "0fa3704e87fb9f065b20bdd8d70c66705a20a33366f6ae8546adeb83ac1053f0"
|
||||
},
|
||||
{
|
||||
"input": "{\"k\": 1, \"k\": 2}",
|
||||
"canonical": "{\"k\":2}",
|
||||
"sha256": "1ddca3d1f7a33ce87c75de54e2b6a7ee0e52cf11cb8b8041e102b8477d5b1a23"
|
||||
},
|
||||
{
|
||||
"input": "[-1.0, 2.50, 1e0, -1e-0, 0.1e1, 12345678.9, 4.35, 0.3, 1e-6, 1e-5]",
|
||||
"canonical": "[-1,2.5,1,-1,1,12345678.9,4.35,0.3,0.000001,0.00001]",
|
||||
"sha256": "a2d500ce5093d2214d6e7b78163853211d5c8a43317ca81f0eac4f7aebd650e5"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,256 @@
|
||||
//! R5.6 preparation (owner 2026-10-24 « arbitrage D-R5.6 »): the convention
|
||||
//! `sha256-rfc8785-validated-v1` and the trusted alias registry, PREPARED, NOT ACTIVE. The RFC
|
||||
//! 8785 vectors, the V8 number oracle, the independent Node differential, the draft's frozen
|
||||
//! value, the registry's contract and a census of the historical archives (JSON fixtures and
|
||||
//! every fixture PDF's attachment) under the comparison the five consumers will use.
|
||||
#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)]
|
||||
|
||||
mod common;
|
||||
|
||||
use faunosteo_core::domain::js::parse_json;
|
||||
use faunosteo_core::domain::sheet::fingerprint::{
|
||||
AliasRefusal, Equivalence, FINGERPRINT_CONVENTION, FingerprintError, TRUSTED_ALIASES,
|
||||
canonical_json, check_declared, equivalence, sheet_fingerprint,
|
||||
};
|
||||
use faunosteo_core::pdf::read::probe::{ProbeLimits, read_attached_document};
|
||||
use serde_json::Value;
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
const SHEET: &str = "503dc301-a8b1-50d4-9e44-0919b2fc2916";
|
||||
const ALIAS: &str = "27c7383c445c73dc2001ebf4794a3ea63d416910bbbaa0d7848dd720de7c2660";
|
||||
const CANONICAL: &str = "12d788727c1e532641bd5b3a7ecf39dbdd25e70350f8a76be5ca917b262874e2";
|
||||
|
||||
fn oracle(name: &str) -> Value {
|
||||
serde_json::from_str(&common::repo_file(&format!(
|
||||
"crates/core/tests/oracles/r5/{name}"
|
||||
)))
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn sha256(text: &str) -> String {
|
||||
Sha256::digest(text.as_bytes())
|
||||
.iter()
|
||||
.map(|b| format!("{b:02x}"))
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_rfc_8785_vectors_are_reproduced() {
|
||||
let vectors = oracle("fingerprints.json")["rfc8785_vectors"].clone();
|
||||
for vector in vectors.as_array().unwrap() {
|
||||
let input = parse_json(vector["input"].as_str().unwrap()).unwrap();
|
||||
assert_eq!(
|
||||
canonical_json(&input).unwrap(),
|
||||
vector["canonical"].as_str().unwrap()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn numbers_print_as_v8_on_the_whole_oracle() {
|
||||
let rows = oracle("numbers.json")["rows"].clone();
|
||||
let rows = rows.as_array().unwrap();
|
||||
assert_eq!(rows.len(), 44_322);
|
||||
for row in rows {
|
||||
let bits = u64::from_str_radix(row[0].as_str().unwrap(), 16).unwrap();
|
||||
let value = Value::from(f64::from_bits(bits));
|
||||
assert_eq!(
|
||||
canonical_json(&value).unwrap(),
|
||||
row[1].as_str().unwrap(),
|
||||
"{bits:016x}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_independent_node_reference_agrees_case_by_case() {
|
||||
let frozen = oracle("fingerprint-differential.json");
|
||||
for case in frozen["cases"].as_array().unwrap() {
|
||||
let input = parse_json(case["input"].as_str().unwrap()).unwrap();
|
||||
let canonical = canonical_json(&input).unwrap();
|
||||
assert_eq!(canonical, case["canonical"].as_str().unwrap());
|
||||
assert_eq!(sha256(&canonical), case["sha256"].as_str().unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unicode_is_kept_and_ordered_by_utf16_units() {
|
||||
// U+FB33 sorts after U+1F600 by code point, before it by UTF-16 units (0xFB33 > 0xD83D).
|
||||
let value = parse_json(r#"{"דּ":1,"😀":2,"é":3,"é":4}"#).unwrap();
|
||||
assert_eq!(
|
||||
canonical_json(&value).unwrap(),
|
||||
"{\"e\u{301}\":4,\"\u{e9}\":3,\"\u{1F600}\":2,\"\u{FB33}\":1}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_draft_gives_the_frozen_validated_value() {
|
||||
let text = common::repo_file("supabase/seed/anatomical-sheets/equine/draft/document.json");
|
||||
assert_eq!(sha256(&text), ALIAS);
|
||||
assert_eq!(sheet_fingerprint(&text).unwrap(), CANONICAL);
|
||||
let artifacts = oracle("fingerprints.json")["artifacts"].clone();
|
||||
let draft = artifacts
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.find(|a| a["file"].as_str().unwrap().ends_with("draft/document.json"))
|
||||
.unwrap()
|
||||
.clone();
|
||||
assert_eq!(draft["b_rfc8785_validated"], CANONICAL);
|
||||
assert_eq!(draft["today_file_bytes"], ALIAS);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_invalid_or_malformed_document_has_no_fingerprint() {
|
||||
let text = common::repo_file("supabase/seed/anatomical-sheets/equine/draft/document.json");
|
||||
let mut value: Value = serde_json::from_str(&text).unwrap();
|
||||
let first = value["annotations"][0]["id"].clone();
|
||||
value["annotations"][1]["id"] = first;
|
||||
let duplicated = serde_json::to_string(&value).unwrap();
|
||||
assert!(matches!(sheet_fingerprint(&duplicated), Err(FingerprintError::Invalid(n)) if n > 0));
|
||||
assert!(matches!(
|
||||
sheet_fingerprint("[1]"),
|
||||
Err(FingerprintError::Shape(_))
|
||||
));
|
||||
assert!(matches!(
|
||||
sheet_fingerprint("{"),
|
||||
Err(FingerprintError::Parse(_))
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_registry_holds_one_pair_bound_to_the_equine_sheet() {
|
||||
assert_eq!(TRUSTED_ALIASES.len(), 1);
|
||||
assert_eq!(FINGERPRINT_CONVENTION, "sha256-rfc8785-validated-v1");
|
||||
assert_eq!(
|
||||
equivalence(SHEET, CANONICAL, CANONICAL),
|
||||
Equivalence::Identical
|
||||
);
|
||||
assert_eq!(
|
||||
equivalence(SHEET, CANONICAL, ALIAS),
|
||||
Equivalence::TrustedAlias
|
||||
);
|
||||
assert_eq!(
|
||||
equivalence(SHEET, ALIAS, CANONICAL),
|
||||
Equivalence::TrustedAlias
|
||||
);
|
||||
let other_sheet = "00000000-0000-0000-0000-000000000000";
|
||||
assert_eq!(
|
||||
equivalence(other_sheet, CANONICAL, ALIAS),
|
||||
Equivalence::Different
|
||||
);
|
||||
let other_content = "a".repeat(64);
|
||||
assert_eq!(
|
||||
equivalence(SHEET, &other_content, ALIAS),
|
||||
Equivalence::Different
|
||||
);
|
||||
assert_eq!(
|
||||
equivalence(SHEET, &other_content, CANONICAL),
|
||||
Equivalence::Different
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn declared_metadata_never_creates_an_equivalence() {
|
||||
let aliases = vec![ALIAS.to_owned()];
|
||||
assert_eq!(
|
||||
check_declared(SHEET, CANONICAL, FINGERPRINT_CONVENTION, &aliases),
|
||||
Ok(())
|
||||
);
|
||||
assert_eq!(
|
||||
check_declared(SHEET, CANONICAL, FINGERPRINT_CONVENTION, &[]),
|
||||
Ok(())
|
||||
);
|
||||
let forged = vec!["b".repeat(64)];
|
||||
assert_eq!(
|
||||
check_declared(SHEET, CANONICAL, FINGERPRINT_CONVENTION, &forged),
|
||||
Err(AliasRefusal::Untrusted("b".repeat(64)))
|
||||
);
|
||||
let foreign = "00000000-0000-0000-0000-000000000000";
|
||||
assert!(check_declared(foreign, CANONICAL, FINGERPRINT_CONVENTION, &aliases).is_err());
|
||||
assert!(check_declared(SHEET, &"c".repeat(64), FINGERPRINT_CONVENTION, &aliases).is_err());
|
||||
assert!(
|
||||
check_declared(
|
||||
SHEET,
|
||||
ALIAS,
|
||||
FINGERPRINT_CONVENTION,
|
||||
&[CANONICAL.to_owned()]
|
||||
)
|
||||
.is_err()
|
||||
);
|
||||
assert_eq!(
|
||||
check_declared(SHEET, CANONICAL, "sha256-prettier-file-bytes-v0", &aliases),
|
||||
Err(AliasRefusal::UnknownConvention(
|
||||
"sha256-prettier-file-bytes-v0".into()
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
/// Every `sheet` reference of a report found in `value` (bare report or envelope).
|
||||
fn stamps(value: &Value, found: &mut Vec<(String, String)>) {
|
||||
match value {
|
||||
Value::Object(map) => {
|
||||
if let (Some(id), Some(sha)) = (
|
||||
map.get("sheetId").and_then(Value::as_str),
|
||||
map.get("documentSha256").and_then(Value::as_str),
|
||||
) {
|
||||
found.push((id.to_owned(), sha.to_owned()));
|
||||
}
|
||||
map.values().for_each(|item| stamps(item, found));
|
||||
}
|
||||
Value::Array(items) => items.iter().for_each(|item| stamps(item, found)),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn files(dir: &std::path::Path, out: &mut Vec<std::path::PathBuf>) {
|
||||
for entry in std::fs::read_dir(dir).unwrap() {
|
||||
let path = entry.unwrap().path();
|
||||
if path.is_dir() {
|
||||
files(&path, out);
|
||||
} else {
|
||||
out.push(path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn historical_archives_open_as_the_same_document_only_through_the_registered_pair() {
|
||||
let mut paths = Vec::new();
|
||||
files(
|
||||
std::path::Path::new(&common::repo_path("apps/app/tests/fixtures")),
|
||||
&mut paths,
|
||||
);
|
||||
let mut found = Vec::new();
|
||||
let mut pdfs = 0;
|
||||
for path in &paths {
|
||||
let bytes = std::fs::read(path).unwrap();
|
||||
let text = match path.extension().and_then(|e| e.to_str()) {
|
||||
Some("json") => String::from_utf8(bytes).ok(),
|
||||
Some("pdf") => read_attached_document(&bytes, ProbeLimits::UNBOUNDED)
|
||||
.ok()
|
||||
.map(|located| {
|
||||
pdfs += 1;
|
||||
located.text.into_owned()
|
||||
}),
|
||||
_ => None,
|
||||
};
|
||||
if let Some(value) = text.and_then(|t: String| serde_json::from_str::<Value>(&t).ok()) {
|
||||
stamps(&value, &mut found);
|
||||
}
|
||||
}
|
||||
assert!(pdfs > 50, "{pdfs} attachments read");
|
||||
let mut same = 0;
|
||||
for (sheet, sha) in &found {
|
||||
let verdict = equivalence(sheet, sha, CANONICAL);
|
||||
let expected = sheet == SHEET && sha == ALIAS;
|
||||
assert_eq!(verdict.same_document(), expected, "{sheet} {sha}");
|
||||
same += usize::from(expected);
|
||||
}
|
||||
// Every authentic stamp of the equine draft is the alias; the synthetic ones stay different.
|
||||
println!(
|
||||
"census: {pdfs} PDF attachments, {} stamps, {same} alias",
|
||||
found.len()
|
||||
);
|
||||
assert!(same > 30, "{same} of {}", found.len());
|
||||
}
|
||||
@@ -3453,3 +3453,53 @@ Closing gates round 2 (1ec9e1b): `pnpm check:all` green 03:51:55; targeted fresh
|
||||
behaviour (23 bypass probes refused, a census of 157 repository JSON files finds no authentic shape refused,
|
||||
`--features cli` 261 passed), ECARTS(2) formal: the `parse_envelope` error type and an audit-prose FLOOR line,
|
||||
both settled by a contract Clarification submitted to the owner.
|
||||
|
||||
### 11.19 R5.6 prepared: the fingerprint convention and its registry, not active (2026-10-24)
|
||||
|
||||
Owner brief « Validation R5.5, arbitrage D-R5.6 et préparation de la bascule ». Convention
|
||||
`sha256-rfc8785-validated-v1` confirmed; the core's compiled registry is the only alias authority; five
|
||||
consumers approved (`report/rebase.rs`, `archive/read.rs`, `session.rs`, `pdf/write/input.rs`,
|
||||
`pdf/compare/lines.rs`); Rust becomes the only generator at the switch. **Nothing is active**: no reader,
|
||||
writer or generated file uses the new module; `pnpm r5:import-equine --check` gives 0 differences and
|
||||
`documentSha256` stays `27c7383c…2660`.
|
||||
|
||||
**Committed (inert).** `domain/sheet/fingerprint.rs`: `canonical_json` (RFC 8785: UTF-16 member order,
|
||||
`js_number`, the `JSON.stringify` escape set, no normalisation, I-JSON numbers only), `sheet_fingerprint`
|
||||
(feature `fingerprint`: strict parse, R2 shape, zero integrity issue, JCS of the typed reading, SHA-256),
|
||||
`TRUSTED_ALIASES` (one entry: sheet `503dc301-…`, canonical `12d788727c1e532641bd5b3a7ecf39dbdd25e70350f8a76be5ca917b262874e2`,
|
||||
alias `27c7383c445c73dc2001ebf4794a3ea63d416910bbbaa0d7848dd720de7c2660`, convention of the alias
|
||||
`sha256-prettier-file-bytes-v0`), `equivalence` (Identical, TrustedAlias, Different; the pair must belong to
|
||||
the same sheet, either way round; raw values stay with the caller) and `check_declared` (a `sheet-ref.json`
|
||||
block must name `v1` and list only registered aliases of that sheet and canonical value).
|
||||
Tests `sheet_fingerprint` (9): the RFC vectors, the 44,322-row V8 number oracle, the independent Node
|
||||
reference (`scripts/r5-fingerprint-differential.mjs`, own canonicaliser, frozen once into
|
||||
`oracles/r5/fingerprint-differential.json`, 7 cases: ES number edges, UTF-16 key order, escapes, nesting,
|
||||
repeated keys), the draft's value recomputed equal to the frozen `b_rfc8785_validated`, invalid and
|
||||
malformed documents refused, the registry contract, and the census of the historical archives (147 PDF
|
||||
attachments and the JSON fixtures: 124 sheet stamps, 123 are the alias and open as the same document, the one
|
||||
other stays different).
|
||||
|
||||
**Prepared, not applied: `.claude/tasks/plans/r5-6-switch.patch`** (applies cleanly on this commit). The
|
||||
five consumers through `equivalence` (rebase skipped on the registered pair of the same sheet, the stamp kept;
|
||||
archive snapshot pair; export sheet match; `report-compare` prints both raw values for an alias match; `session.rs`
|
||||
needs no own change, it goes through the rebase), the generator stamping `sheet_fingerprint` and writing the
|
||||
descriptive block, the sheet load refusing a block that contradicts the registry (`loaded.rs`, `facade.rs`:
|
||||
`invalid_sheet`), `.prettierignore` on `legacy-import/` and `draft/`, and the rehearsal suite
|
||||
`fingerprint_consumers` (3: alias not rebased and stamp kept, real change and forged value rebased, archive pair
|
||||
kept and forged pair rejected). Rehearsal with the patch applied: `pnpm rust:check` green, `pnpm check:all` green
|
||||
14:32:05, then the files restored.
|
||||
|
||||
**Seed at the switch (measured with the patch, written to a scratch folder, never to the seed):**
|
||||
|
||||
| File | Before | After | Why | Check | Rollback |
|
||||
|---|---|---|---|---|---|
|
||||
| `draft/sheet-ref.json` | sha256 `2c0716f1…b285`, `document_sha256` `27c7…2660` | sha256 `03e7f6ab…623b`, `document_sha256` `12d7…74e2` + block `fingerprint` {`convention`, `aliases: [27c7…2660]`} | the convention | `--check` byte-exact against the regenerated folder; the load check | `git revert` |
|
||||
| `draft/report-migration-map.json` | sha256 `4139c51d…fce9`, `sheet.documentSha256` `27c7…` | sha256 `0e991bc2…2db`, `12d7…` | a migrated v2 report is stamped with the current value | same | `git revert` |
|
||||
| the 16 other files | unchanged | unchanged (`document.json`, `horse.svg`, `equine_draft.sql` compared byte for byte) | no content change | `--check` | n/a |
|
||||
|
||||
Found by the rehearsal, to settle in the switch: four suites fail with the generator switched. Two expect it
|
||||
(`curation_draft`: the draft files byte for byte against the TypeScript ledger, and the historical fingerprint
|
||||
test): at the switch they compare the two files under a declared rule (the fingerprint field and block only),
|
||||
the oracles untouched. Two are an order defect of the rehearsal (`curation_review`, drawings bound tests): the
|
||||
fingerprint validates the document before the drawings' resource bound applies; the switch computes it after
|
||||
that bound.
|
||||
@@ -0,0 +1,56 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* R5.6 preparation: the independent Node reference of RFC 8785 for the Rust canonicaliser
|
||||
* (`crates/core/src/domain/sheet/fingerprint.rs`). Its own `canonicalize` (JSON.parse,
|
||||
* JSON.stringify, `<` on strings: UTF-16 order), written apart from the Rust and from the R5.1
|
||||
* study script. Writes `crates/core/tests/oracles/r5/fingerprint-differential.json` once, only
|
||||
* with MAKE_R5_ORACLES=1 and only when the file is absent (an oracle is added, never rewritten).
|
||||
*
|
||||
* MAKE_R5_ORACLES=1 node scripts/r5-fingerprint-differential.mjs
|
||||
*/
|
||||
import { createHash } from 'node:crypto'
|
||||
import { existsSync, writeFileSync } from 'node:fs'
|
||||
import { dirname, resolve } from 'node:path'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
|
||||
const OUT = resolve(
|
||||
dirname(fileURLToPath(import.meta.url)),
|
||||
'../crates/core/tests/oracles/r5/fingerprint-differential.json',
|
||||
)
|
||||
|
||||
function canonicalize(value) {
|
||||
if (value === null || typeof value !== 'object') return JSON.stringify(value)
|
||||
if (Array.isArray(value)) return `[${value.map(canonicalize).join(',')}]`
|
||||
const keys = Object.keys(value).sort((a, b) => (a < b ? -1 : a > b ? 1 : 0))
|
||||
return `{${keys.map((k) => `${JSON.stringify(k)}:${canonicalize(value[k])}`).join(',')}}`
|
||||
}
|
||||
|
||||
const INPUTS = [
|
||||
'[1e21, 1e-7, 1E+2, 0.1, -0, 5e-324, 1.7976931348623157e308, 123456789012345678901, 9007199254740993, 100, 1.5e-6, 0.000001, 1e20, 999999999999999999999]',
|
||||
'{"\\ud83d\\ude00": 1, "\\ufb33": 2, "\\u00e9": 3, "e\\u0301": 4, "Z": 5, "a": 6, "": 7, "aa": 8, "\\u0080": 9}',
|
||||
'{"s": "\\u0000\\u0001\\u001f\\b\\f\\n\\r\\t\\"\\\\/\\u007f\\u2028\\u2029\\u00e9e\\u0301\\ud83d\\ude00"}',
|
||||
'{"b": {"z": [true, false, null, {"y": 1, "x": [ ]}], "a": {}}, "a": [[[]]]}',
|
||||
'{"geometry": {"cx": 743.0451049804688, "cy": 469.1115417480469, "r": 12}, "name": {"fr": "Atlas (C1)", "en": "Atlas"}}',
|
||||
'{"k": 1, "k": 2}',
|
||||
'[-1.0, 2.50, 1e0, -1e-0, 0.1e1, 12345678.9, 4.35, 0.3, 1e-6, 1e-5]',
|
||||
]
|
||||
|
||||
const cases = INPUTS.map((input) => {
|
||||
const canonical = canonicalize(JSON.parse(input))
|
||||
return {
|
||||
input,
|
||||
canonical,
|
||||
sha256: createHash('sha256').update(canonical, 'utf8').digest('hex'),
|
||||
}
|
||||
})
|
||||
|
||||
if (process.env.MAKE_R5_ORACLES !== '1') {
|
||||
console.log(JSON.stringify(cases, null, 2))
|
||||
} else if (existsSync(OUT)) {
|
||||
console.error(`${OUT} exists: an oracle is never rewritten`)
|
||||
process.exit(1)
|
||||
} else {
|
||||
const frozen = { frozen_from: `Node ${process.version}`, cases }
|
||||
writeFileSync(OUT, `${JSON.stringify(frozen, null, 2)}\n`)
|
||||
console.log(`wrote ${cases.length} cases`)
|
||||
}
|
||||
@@ -33,6 +33,10 @@ cargo test -p faunosteo-core --features curation --locked
|
||||
# against a loopback stub of the local stack (`upload_tool`); never built into the module.
|
||||
cargo clippy -p faunosteo-core --all-targets --features upload --locked -- -D warnings
|
||||
cargo test -p faunosteo-core --features upload --locked
|
||||
# The sheet fingerprint (R5.6, prepared, not active): RFC 8785, the V8 and Node oracles, the
|
||||
# registry, and the census of the fixture archives (their attachments need the PDF reader).
|
||||
cargo clippy -p faunosteo-core --all-targets --features fingerprint,pdf-read --locked -- -D warnings
|
||||
cargo test -p faunosteo-core --features fingerprint,pdf-read --locked --test sheet_fingerprint
|
||||
# Exports the TypeScript types of the contracts (ts-rs) into packages/core-wasm/types.
|
||||
# `git status --porcelain --untracked-files=all -- packages/core-wasm/types` must stay
|
||||
# empty afterwards (scripts/check-all.sh, .gitea/workflows/ci.yml): the committed types
|
||||
|
||||
Reference in new issue
Block a user