feat(app): R2 switch — the application runs its domain rules in the Rust core

Consumers now fed by the core (crates/core through @faunosteo/core-wasm):
- the sheet: the bundled payload is parsed and indexed by the core
  (SheetHandle, SheetIndexData); SheetIndex reads precomputed tables only;
- the store: new report, add / remove / move observation, draft restore
  (v2 migration + rebase) through the core, actions answering { ok };
- the editor click chain (zone.click), laterality and vertebra pickers
  (zone.add_sides, vertebra.initial / sync / select), the findings picker
  (finding.toggle by clicked leaf position), zone removal;
- the import: engine byte scan (TypeScript until R4), then the core's
  layered reading and rebase in one call; the export: the core builds the
  sheet snapshot, the frozen TypeScript engine writes the PDF (R4).
Lazy core: Home and the new-report form read the sheet availability from
the bundle and load the module on import / submit only; the auth shell
never references it. Degraded states (core_unavailable, invalid_sheet)
show the validated notice with a retry and never redirect or touch the
draft; an unreadable draft or archive is set aside under
osteo:quarantine:* (only content kinds; a failed write keeps it in place
and the editor stays), a rebase that flags items keeps the raw pre-rebase
draft. Four strings, wording validated by the owner.
Deleted: services/zoneInteraction.ts and its suite (13 cases ported to
cargo), the TypeScript Finding builder and vertebra selection rules of the
pickers, store rules, persistence parsing, hydrateReport. ESLint forbids
@faunosteo/domain in apps/app/src; the report:compare CLI keeps the frozen
domain until R4 (scripts/compare/sheet.ts). App suite 313/313 on the real
core (24 new tests: quarantine, degraded editor, core failures, N-1
reader); step6 goldens and the frozen envelopes unchanged.
This commit is contained in:
bchanot committed 2026-10-14 00:41:34 +02:00
1 parent 794dfd9d0b
commit 74fcb37564
65 files changed
+2227 -1678

No files matched your search

+47
View File
@@ -0,0 +1,47 @@
import type { CoreOutcome } from '@faunosteo/core-wasm'
import type {
MoveDirection,
NewReportDraft,
Report,
SheetRef,
Vertebra,
VertebraAction,
} from '@faunosteo/core-wasm/types'
import { coreRuntime } from './runtime'
/**
* The report rules that need no sheet (R2): creation and the observation list operations
* of the store, the vertebra picker's selection. Ids and time come from the caller.
*/
function run<T>(op: string, input: unknown): CoreOutcome<T> {
return coreRuntime().call<T>(op, JSON.stringify(input))
}
export interface NewReportInput {
draft: NewReportDraft
sheet: SheetRef
id: string
createdAt: string
/** Id of the empty observation a draft without any starts with. */
observationId: string
}
export const reportOps = {
newReport: (input: NewReportInput): CoreOutcome<Report> =>
run('report.new', input),
addObservation: (report: Report, id: string): CoreOutcome<Report> =>
run('report.add_observation', { report, id }),
removeObservation: (report: Report, index: number): CoreOutcome<Report> =>
run('report.remove_observation', { report, index }),
moveObservation: (
report: Report,
index: number,
direction: MoveDirection,
): CoreOutcome<Report> =>
run('report.move_observation', { report, index, direction }),
selectVertebrae: (
selection: readonly Vertebra[],
action: VertebraAction,
): CoreOutcome<Vertebra[]> => run('vertebra.select', { selection, action }),
}
+46
View File
@@ -0,0 +1,46 @@
import glueUrl from '@faunosteo/core-wasm/pkg/faunosteo_wasm.js?url'
import wasmUrl from '@faunosteo/core-wasm/pkg/faunosteo_wasm_bg.wasm?url'
import {
CoreRuntime,
type CoreSource,
type GlueModule,
} from '@faunosteo/core-wasm'
/**
* The application's core runtime (Rust compiled to WebAssembly, R2). Vite emits the glue
* and the `.wasm` as hashed assets that no route chunk imports: nothing is fetched until a
* sheet path or an import asks `ready()` (review III.3 rule 6). The production CSP must
* carry `'wasm-unsafe-eval'` (DEPLOY.md); without it the load fails and the editor shows
* the degraded state.
*/
async function compileModule(): Promise<WebAssembly.Module> {
const response = await fetch(wasmUrl)
if (!response.ok) throw new Error(`core module HTTP ${response.status}`)
try {
// Needs `Content-Type: application/wasm`; any other type falls back to bytes.
return await WebAssembly.compileStreaming(response.clone())
} catch {
return WebAssembly.compile(await response.arrayBuffer())
}
}
const browserSource: CoreSource = {
compile: compileModule,
// A distinct query per instance gives a new glue module record (R0 finding).
importGlue: (instance) =>
import(/* @vite-ignore */ `${glueUrl}?instance=${instance}`) as Promise<GlueModule>,
}
let runtime: CoreRuntime | null = null
/** The page's runtime, created on first use. */
export function coreRuntime(): CoreRuntime {
runtime ??= new CoreRuntime(browserSource)
return runtime
}
/** Test seam: run the application on another runtime (the Node source in vitest). */
export function setCoreRuntime(next: CoreRuntime): void {
runtime = next
}
+95
View File
@@ -0,0 +1,95 @@
import type { CoreOutcome, SheetSession } from '@faunosteo/core-wasm'
import type {
ArchiveImport,
DraftRestore,
LateralSide,
Observation,
Report,
SheetSnapshot,
Vertebra,
ZoneClick,
} from '@faunosteo/core-wasm/types'
/**
* The sheet-bound rules of the editor, run by the core on the session's sheet (R2): one
* call per user action or load, never in a render. Each returns the core's outcome; the
* caller stops on `ok: false` (nothing is written, the action shows `core.action_failed`).
*/
export interface FindingToggle {
observation: Observation
findingZoneId: string
path: readonly string[]
/** Position of the clicked leaf among the siblings the picker rendered. */
leafPosition: number
side?: LateralSide
}
export class SheetOps {
constructor(private readonly session: SheetSession) {}
private run<T>(op: string, input: unknown): CoreOutcome<T> {
return this.session.call<T>(op, JSON.stringify(input))
}
/** What a click on an annotation does to the active observation (the whole click chain). */
zoneClick(
observation: Observation | null,
annotationId: string,
): CoreOutcome<ZoneClick> {
return this.run('zone.click', { observation, annotationId })
}
addSides(
observation: Observation,
annotationId: string,
sides: readonly LateralSide[],
): CoreOutcome<Observation> {
return this.run('zone.add_sides', { observation, annotationId, sides })
}
removeZoneAt(observation: Observation, index: number): CoreOutcome<Observation> {
return this.run('zone.remove_at', { observation, index })
}
/** The vertebra picker's initial selection, the zone at `index` first. */
vertebraInitial(
observation: Observation,
index: number,
): CoreOutcome<Vertebra[]> {
return this.run('vertebra.initial', { observation, index })
}
syncVertebrae(
observation: Observation,
selection: readonly Vertebra[],
viewId: string,
): CoreOutcome<Observation> {
return this.run('vertebra.sync', { observation, selection, viewId })
}
toggleFinding(toggle: FindingToggle): CoreOutcome<Observation> {
return this.run('finding.toggle', toggle)
}
/** Reads the tab's draft (and the archive kept beside it) and rebases it on this sheet. */
restoreDraft(
draft: string,
archive: string | null,
): CoreOutcome<DraftRestore> {
return this.run('draft.restore', { draft, archive })
}
/**
* Reads a PDF attachment (any supported shape, as the byte scan parsed it) and rebases
* it on this sheet. Failures: `incompatible_version` (with `found`), `no_attachment`.
*/
importArchive(attachment: unknown): CoreOutcome<ArchiveImport> {
return this.run('archive.import', attachment)
}
/** The snapshot of this sheet an archive of `report` carries. */
snapshot(report: Report): CoreOutcome<SheetSnapshot> {
return this.run('archive.snapshot', { report })
}
}