diff --git a/.claude/tasks/contracts/2026-10-25-rust-r5-6-rehearsal.md b/.claude/tasks/contracts/2026-10-25-rust-r5-6-rehearsal.md index 32c86d9a..a7877aa0 100644 --- a/.claude/tasks/contracts/2026-10-25-rust-r5-6-rehearsal.md +++ b/.claude/tasks/contracts/2026-10-25-rust-r5-6-rehearsal.md @@ -38,3 +38,15 @@ no oracle regenerated, nothing deleted. S8. **Gates on the switched state.** `pnpm rust:check`, `pnpm check:all`, RFC 8785, Node differential, alias security, the five consumers + load checks, authentic archive parity, N-1 on re-exported archives, snapshot-bearing reports, real-change rebase, the two generated artifacts; security audit; fresh verifier. + +## Clarifications + +- 2026-10-25 (owner « autorisation de correction M1, nouvelle répétition obligatoire »): scope extended by + `domain/archive/snapshot_rules.rs`; the M1 decision moves into `build_sheet_snapshot` and `assert_exportable` + (`pdf/write/input.rs`) returns to strict equality (unchanged from 943a25a). Rules added: an alias is granted only by + `LoadedSheet::recognises_historical_stamp` (loader-verified `v1` value, private state, recomputed from the current + document at the decision, registry pair, same sheet id); `"fingerprint": null` and malformed blocks refused; warnings + never block a `v1` fingerprint (`is_publishable`), errors do; `canonical_json` bounded at 128 levels with a controlled + error; the direct export API is tested, not only the application paths. Final state: worktree + `/tmp/claude-1000/-home-bchanot-Documents-faunosteo/3571c861-cd6e-4751-a25e-c26da95beb85/scratchpad/r56final` + (detached at 8e77e37 + the patch, seed switched by the patch). diff --git a/.claude/tasks/plans/r5-6-switch.patch b/.claude/tasks/plans/r5-6-switch.patch index 0ab16723..ba38ed9e 100644 --- a/.claude/tasks/plans/r5-6-switch.patch +++ b/.claude/tasks/plans/r5-6-switch.patch @@ -2699,85 +2699,110 @@ index 5af7cb3..f49eb78 100644 diff --git a/packages/core-wasm/tests/r5-6.n-1.test.ts b/packages/core-wasm/tests/r5-6.n-1.test.ts new file mode 100644 -index 0000000..accb865 +index 0000000..953fc1d --- /dev/null +++ b/packages/core-wasm/tests/r5-6.n-1.test.ts -@@ -0,0 +1,84 @@ +@@ -0,0 +1,121 @@ +/** -+ * R5.6 M1 (owner 2026-10-24, option 1) through the module and the frozen N-1 readers: an old -+ * PDF stamped with the registered historical value, re-exported against the switched sheet -+ * (its snapshot carries the canonical value), is written with the report's own stamp in the -+ * snapshot, and the N-1 scanner and Zod readers of the R3 build and 0.2.0 keep the snapshot, -+ * its labels, views and annotations (a rollback to that build loses nothing). The native side -+ * is `crates/core/tests/r5_6_switch.rs`. ++ * R5.6 M1 (owner 2026-10-25) through the module and the frozen N-1 readers: an old PDF stamped ++ * with the registered historical value, re-exported on the switched sheet, gets its snapshot ++ * from the core (`archive.snapshot` on the sheet handle: the loaded sheet verified its `v1` ++ * value, recomputed now), carrying the report's own stamp; the N-1 scanner and Zod readers of ++ * the R3 build and 0.2.0 keep that snapshot and its content. On an unverified v0 sheet the ++ * core's snapshot carries the sheet's value and the export refuses the old report. The native ++ * side is `crates/core/tests/r5_6_switch.rs`. + */ +import { readFileSync } from 'node:fs' +import { join } from 'node:path' +import { fileURLToPath } from 'node:url' + -+import { loadProofModule } from '../src/node' ++import { CoreRuntime } from '../src/index' ++import { loadProofModule, nodeCoreSource } from '../src/node' +import { readArchiveDocument, ReportArchiveEnvelopeSchema } from './n-1' +import { ReportMigrationMapSchema } from './n-1/domain' +import { readAttachedDocument } from './n-1/scanner/attachment/read' + +const ROOT = fileURLToPath(new URL('../../../', import.meta.url)) ++const SEED = join(ROOT, 'supabase/seed/anatomical-sheets/equine/draft') +const ALIAS = '27c7383c445c73dc2001ebf4794a3ea63d416910bbbaa0d7848dd720de7c2660' -+const CANONICAL = -+ '12d788727c1e532641bd5b3a7ecf39dbdd25e70350f8a76be5ca917b262874e2' ++const CANONICAL = '12d788727c1e532641bd5b3a7ecf39dbdd25e70350f8a76be5ca917b262874e2' ++const json = (path: string): unknown => JSON.parse(readFileSync(path, 'utf8')) + +interface Envelope { + report: { sheet: { documentSha256: string } } -+ sheetSnapshot: { -+ ref: { documentSha256: string } -+ document: unknown -+ assets: unknown -+ } ++ sheetSnapshot: { ref: { documentSha256: string }; document: unknown; assets: unknown } + branding: unknown + labels: unknown + locale: string +} + ++/** The bundle as `apps/app/vite/sheet-bundle.ts` builds it, with `ref` replaceable. */ ++function bundle(ref: unknown): string { ++ const manifest = json(join(SEED, 'assets.json')) as Array<{ ++ file: string ++ sha256: string ++ svg_asset_id: string ++ }> ++ const assets: Record = {} ++ const assetHashes: Record = {} ++ for (const asset of manifest) { ++ assets[asset.svg_asset_id] = readFileSync(join(SEED, 'assets', asset.file), 'utf8') ++ assetHashes[asset.svg_asset_id] = asset.sha256 ++ } ++ return JSON.stringify({ ++ status: 'draft', ++ ref, ++ document: json(join(SEED, 'document.json')), ++ migrationMap: json(join(SEED, 'report-migration-map.json')), ++ assets, ++ assetHashes, ++ }) ++} ++ ++async function coreSnapshot(ref: unknown, report: unknown): Promise { ++ const runtime = new CoreRuntime(nodeCoreSource('pkg')) ++ expect(await runtime.ready()).toBe(true) ++ const outcome = runtime ++ .sheet(bundle(ref)) ++ .call('archive.snapshot', JSON.stringify({ report })) ++ expect(outcome.ok).toBe(true) ++ return (outcome as { ok: true; value: Envelope['sheetSnapshot'] }).value ++} ++ ++function exportInput(old: Envelope, snapshot: unknown): string { ++ return JSON.stringify({ ++ report: old.report, ++ sheetSnapshot: snapshot, ++ branding: old.branding, ++ labels: old.labels, ++ locale: old.locale, ++ exportedAt: '2026-10-25T12:00:00.000Z', ++ }) ++} ++ ++async function oldEnvelope(): Promise { ++ const pdf = new Uint8Array( ++ readFileSync(join(ROOT, 'apps/app/tests/fixtures/pdf/r3/demo.fr.nologos.pdf')), ++ ) ++ return (await readAttachedDocument(pdf)) as Envelope ++} ++ +describe('R5.6 re-export of an old archive, read by the N-1 readers', () => { + it('writes one stamp and the frozen readers keep the snapshot and its content', async () => { + const proofs = await loadProofModule() -+ const pdf = new Uint8Array( -+ readFileSync( -+ join(ROOT, 'apps/app/tests/fixtures/pdf/r3/demo.fr.nologos.pdf'), -+ ), -+ ) -+ const old = (await readAttachedDocument(pdf)) as Envelope ++ const old = await oldEnvelope() + expect(old.report.sheet.documentSha256).toBe(ALIAS) -+ // What the switched sheet's snapshot carries: the same content, the canonical value. -+ const snapshot = { -+ ...old.sheetSnapshot, -+ ref: { ...old.sheetSnapshot.ref, documentSha256: CANONICAL }, -+ } -+ const input = JSON.stringify({ -+ report: old.report, -+ sheetSnapshot: snapshot, -+ branding: old.branding, -+ labels: old.labels, -+ locale: old.locale, -+ exportedAt: '2026-10-24T12:00:00.000Z', -+ }) -+ const rasters = JSON.stringify({ views: [] }) ++ const ref = json(join(SEED, 'sheet-ref.json')) as { document_sha256: string } ++ expect(ref.document_sha256).toBe(CANONICAL) ++ const snapshot = await coreSnapshot(ref, old.report) ++ expect(snapshot.ref.documentSha256).toBe(ALIAS) + const written = (await readAttachedDocument( -+ proofs.export_pdf_for_test(input, rasters), ++ proofs.export_pdf_for_test(exportInput(old, snapshot), JSON.stringify({ views: [] })), + )) as Envelope + expect(written.report.sheet.documentSha256).toBe(ALIAS) + expect(written.sheetSnapshot.ref.documentSha256).toBe(ALIAS) + expect(ReportArchiveEnvelopeSchema.safeParse(written).success).toBe(true) -+ const map = ReportMigrationMapSchema.parse( -+ JSON.parse( -+ readFileSync( -+ join( -+ ROOT, -+ 'supabase/seed/anatomical-sheets/equine/draft/report-migration-map.json', -+ ), -+ 'utf8', -+ ), -+ ), -+ ) ++ const map = ReportMigrationMapSchema.parse(json(join(SEED, 'report-migration-map.json'))) + const outcome = readArchiveDocument(written, map) + expect(outcome.snapshotRejected).toBeUndefined() + expect(outcome.archive).toBeDefined() @@ -2786,6 +2811,18 @@ index 0000000..accb865 + expect(written.labels).toEqual(old.labels) + expect(written.branding).toEqual(old.branding) + }, 60_000) ++ ++ it('refuses the old report on an unverified v0 sheet carrying the canonical value', async () => { ++ const proofs = await loadProofModule() ++ const old = await oldEnvelope() ++ const v0 = json(join(SEED, 'sheet-ref.json')) as Record ++ delete v0.fingerprint ++ const snapshot = await coreSnapshot(v0, old.report) ++ expect(snapshot.ref.documentSha256).toBe(CANONICAL) ++ expect(() => ++ proofs.export_pdf_for_test(exportInput(old, snapshot), JSON.stringify({ views: [] })), ++ ).toThrow(/sheet_mismatch/) ++ }, 60_000) +}) diff --git a/packages/domain/tests/r2-oracles.test.ts b/packages/domain/tests/r2-oracles.test.ts index e0593ef..2fcb5e5 100644