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:
1 parent
794dfd9d0b
commit
74fcb37564
65 files changed
+2227
-1678
No files matched your search
@@ -1,21 +1,17 @@
|
||||
import {
|
||||
AnatomicalSheetDocumentSchema,
|
||||
ReportMigrationMapSchema,
|
||||
type AnatomicalSheetDocument,
|
||||
type ReportMigrationMap,
|
||||
type SheetRef,
|
||||
} from '@faunosteo/domain'
|
||||
import type { LocalizedText, SheetRef } from '@faunosteo/core-wasm/types'
|
||||
|
||||
/**
|
||||
* Where the application gets the anatomical sheet it works on. Step 5 ships
|
||||
* one implementation, the sheet bundled at build time (virtual module, see
|
||||
* vite/sheet-bundle.ts); the Supabase implementation of Step 13 sits behind
|
||||
* the same interface. A loaded sheet is immutable for the session.
|
||||
* the same interface. The payload is handed to the core as it is: the core
|
||||
* parses the document and the migration map (R2). A loaded sheet is
|
||||
* immutable for the session.
|
||||
*/
|
||||
|
||||
export type SheetStatus = 'draft' | 'published'
|
||||
|
||||
/** Raw payload of the virtual module, validated here before use. */
|
||||
/** Raw payload of the virtual module, parsed by the core (`SheetHandle`). */
|
||||
export interface BundledSheetData {
|
||||
status: SheetStatus
|
||||
ref: {
|
||||
@@ -32,56 +28,53 @@ export interface BundledSheetData {
|
||||
assetHashes: Record<string, string>
|
||||
}
|
||||
|
||||
export interface LoadedSheet {
|
||||
status: SheetStatus
|
||||
export interface SheetProvider {
|
||||
/** The reference sheet payload of a species, or null when none is available. */
|
||||
getReferenceSheet(speciesId: string): Promise<BundledSheetData | null>
|
||||
}
|
||||
|
||||
/**
|
||||
* What the pages that only offer to start or import a report show, read from the payload
|
||||
* without the core: the reference a new report is stamped with and the species name.
|
||||
*/
|
||||
export interface SheetAvailability {
|
||||
ref: SheetRef
|
||||
speciesId: string
|
||||
/** Document schema version of the sheet (`ref.schema_version`). */
|
||||
schemaVersion: number
|
||||
document: AnatomicalSheetDocument
|
||||
/** SVG text by `svg_asset_id`. */
|
||||
assets: Readonly<Record<string, string>>
|
||||
/** sha256 by `svg_asset_id`; carried into a report archive as provenance. */
|
||||
assetHashes: Readonly<Record<string, string>>
|
||||
migrationMap: ReportMigrationMap
|
||||
name: LocalizedText
|
||||
}
|
||||
|
||||
export interface SheetProvider {
|
||||
/** The reference sheet of a species, or null when none is available. */
|
||||
getReferenceSheet(speciesId: string): Promise<LoadedSheet | null>
|
||||
}
|
||||
|
||||
export function parseBundledSheet(data: BundledSheetData): LoadedSheet {
|
||||
const document = AnatomicalSheetDocumentSchema.parse(data.document)
|
||||
const migrationMap = ReportMigrationMapSchema.parse(data.migrationMap)
|
||||
export function availabilityOf(data: BundledSheetData): SheetAvailability {
|
||||
const document = data.document as { name: LocalizedText }
|
||||
return {
|
||||
status: data.status,
|
||||
ref: {
|
||||
sheetId: data.ref.sheet_id,
|
||||
versionId: data.ref.version_id,
|
||||
documentSha256: data.ref.document_sha256,
|
||||
},
|
||||
speciesId: data.ref.species_id,
|
||||
schemaVersion: data.ref.schema_version,
|
||||
document,
|
||||
assets: data.assets,
|
||||
assetHashes: data.assetHashes,
|
||||
migrationMap,
|
||||
name: document.name,
|
||||
}
|
||||
}
|
||||
|
||||
let cached: Promise<LoadedSheet | null> | null = null
|
||||
let cached: Promise<BundledSheetData | null> | null = null
|
||||
|
||||
async function loadBundled(): Promise<LoadedSheet | null> {
|
||||
async function loadBundled(): Promise<BundledSheetData | null> {
|
||||
const mod = await import('virtual:faunosteo-sheet')
|
||||
return mod.bundledSheet ? parseBundledSheet(mod.bundledSheet) : null
|
||||
return mod.bundledSheet
|
||||
}
|
||||
|
||||
export const bundledSheetProvider: SheetProvider = {
|
||||
async getReferenceSheet(speciesId) {
|
||||
cached ??= loadBundled()
|
||||
const sheet = await cached
|
||||
return sheet && sheet.speciesId === speciesId ? sheet : null
|
||||
let sheet: BundledSheetData | null
|
||||
try {
|
||||
sheet = await cached
|
||||
} catch (error) {
|
||||
// A failed chunk load is not remembered: the next attempt fetches again.
|
||||
cached = null
|
||||
throw error
|
||||
}
|
||||
return sheet && sheet.ref.species_id === speciesId ? sheet : null
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
+105
-12
@@ -1,20 +1,40 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
import { buildSheetIndex, type SheetIndex } from '@/services/sheetIndex'
|
||||
import { bundledSheetProvider, type SheetProvider } from './provider'
|
||||
import type { SheetIndexData } from '@faunosteo/core-wasm/types'
|
||||
import { coreRuntime } from '@/core/runtime'
|
||||
import { SheetOps } from '@/core/sheetOps'
|
||||
import { SheetIndex } from '@/services/sheetIndex'
|
||||
import {
|
||||
availabilityOf,
|
||||
bundledSheetProvider,
|
||||
type SheetAvailability,
|
||||
type SheetProvider,
|
||||
} from './provider'
|
||||
|
||||
/**
|
||||
* The reference sheet of a species for the current session, loaded once
|
||||
* through the provider and indexed. `none` means the build carries no sheet
|
||||
* (production bundle without SHEET_BUNDLE=draft) or no sheet for the species.
|
||||
* The reference sheet of a species for the current session. `useSheet` loads it into the
|
||||
* core (the editor, the import): `none` means the build carries no sheet (production
|
||||
* bundle without SHEET_BUNDLE=draft) or none for the species; `core_unavailable` means the
|
||||
* module could not be loaded (CSP, network, old browser) and `invalid_sheet` that the
|
||||
* bundled payload was refused, both with a `retry`. `useSheetAvailability` reads the
|
||||
* payload alone, without the core, for the pages that only offer to start a report.
|
||||
*/
|
||||
|
||||
export type SheetLoad =
|
||||
| { status: 'none' }
|
||||
| { status: 'ready'; index: SheetIndex }
|
||||
| { status: 'core_unavailable' }
|
||||
| { status: 'invalid_sheet' }
|
||||
|
||||
export type SheetState =
|
||||
| { status: 'loading' }
|
||||
| { status: 'none' }
|
||||
| { status: 'ready'; index: SheetIndex }
|
||||
| { status: 'core_unavailable'; retry: () => void }
|
||||
| { status: 'invalid_sheet'; retry: () => void }
|
||||
|
||||
let provider: SheetProvider = bundledSheetProvider
|
||||
const cache = new Map<string, Promise<SheetIndex | null>>()
|
||||
/** Settled loads only (`ready`, `none`): a failure is never remembered. */
|
||||
const cache = new Map<string, Promise<SheetLoad>>()
|
||||
|
||||
/** Test seam: swap the provider and forget loaded sheets. */
|
||||
export function setSheetProvider(next: SheetProvider): void {
|
||||
@@ -22,19 +42,48 @@ export function setSheetProvider(next: SheetProvider): void {
|
||||
cache.clear()
|
||||
}
|
||||
|
||||
export function loadSheetIndex(speciesId: string): Promise<SheetIndex | null> {
|
||||
async function loadIntoCore(speciesId: string): Promise<SheetLoad> {
|
||||
const bundle = await provider.getReferenceSheet(speciesId)
|
||||
if (!bundle) return { status: 'none' }
|
||||
if (!(await coreRuntime().ready())) return { status: 'core_unavailable' }
|
||||
const session = coreRuntime().sheet(JSON.stringify(bundle))
|
||||
const data = session.call<SheetIndexData>('index_data', '')
|
||||
if (data.ok) {
|
||||
const index = new SheetIndex(data.value, bundle.assets, new SheetOps(session))
|
||||
return { status: 'ready', index }
|
||||
}
|
||||
return {
|
||||
status: data.kind === 'invalid_sheet' ? 'invalid_sheet' : 'core_unavailable',
|
||||
}
|
||||
}
|
||||
|
||||
/** The species' sheet in the core; failures are retried on the next call. */
|
||||
export function loadSheet(speciesId: string): Promise<SheetLoad> {
|
||||
let pending = cache.get(speciesId)
|
||||
if (!pending) {
|
||||
pending = provider
|
||||
.getReferenceSheet(speciesId)
|
||||
.then((sheet) => (sheet ? buildSheetIndex(sheet) : null))
|
||||
pending = loadIntoCore(speciesId).catch(
|
||||
(): SheetLoad => ({ status: 'core_unavailable' }),
|
||||
)
|
||||
cache.set(speciesId, pending)
|
||||
void pending.then((load) => {
|
||||
const settled = load.status === 'ready' || load.status === 'none'
|
||||
if (!settled) cache.delete(speciesId)
|
||||
})
|
||||
}
|
||||
return pending
|
||||
}
|
||||
|
||||
/** The ready index, or null (tests and callers that need the sheet or nothing). */
|
||||
export async function loadSheetIndex(
|
||||
speciesId: string,
|
||||
): Promise<SheetIndex | null> {
|
||||
const load = await loadSheet(speciesId)
|
||||
return load.status === 'ready' ? load.index : null
|
||||
}
|
||||
|
||||
export function useSheet(speciesId: string | undefined): SheetState {
|
||||
const [state, setState] = useState<SheetState>({ status: 'loading' })
|
||||
const [attempt, setAttempt] = useState(0)
|
||||
useEffect(() => {
|
||||
let cancelled = false
|
||||
if (!speciesId) {
|
||||
@@ -42,13 +91,57 @@ export function useSheet(speciesId: string | undefined): SheetState {
|
||||
return
|
||||
}
|
||||
setState({ status: 'loading' })
|
||||
loadSheetIndex(speciesId).then((index) => {
|
||||
const retry = () => {
|
||||
void coreRuntime()
|
||||
.retry()
|
||||
.finally(() => setAttempt((n) => n + 1))
|
||||
}
|
||||
void loadSheet(speciesId).then((load) => {
|
||||
if (cancelled) return
|
||||
setState(index ? { status: 'ready', index } : { status: 'none' })
|
||||
if (load.status === 'ready' || load.status === 'none') setState(load)
|
||||
else if (load.status === 'invalid_sheet') setState({ status: load.status, retry })
|
||||
else setState({ status: 'core_unavailable', retry })
|
||||
})
|
||||
return () => {
|
||||
cancelled = true
|
||||
}
|
||||
}, [speciesId, attempt])
|
||||
return state
|
||||
}
|
||||
|
||||
export type SheetAvailabilityState =
|
||||
| { status: 'loading' }
|
||||
| { status: 'none' }
|
||||
| { status: 'ready'; sheet: SheetAvailability }
|
||||
|
||||
/** Whether the build carries a sheet for the species, without loading the core. */
|
||||
export function useSheetAvailability(
|
||||
speciesId: string | undefined,
|
||||
): SheetAvailabilityState {
|
||||
const [state, setState] = useState<SheetAvailabilityState>({
|
||||
status: 'loading',
|
||||
})
|
||||
useEffect(() => {
|
||||
let cancelled = false
|
||||
if (!speciesId) {
|
||||
setState({ status: 'none' })
|
||||
return
|
||||
}
|
||||
void provider
|
||||
.getReferenceSheet(speciesId)
|
||||
.then((bundle) => {
|
||||
if (cancelled) return
|
||||
setState(
|
||||
bundle
|
||||
? { status: 'ready', sheet: availabilityOf(bundle) }
|
||||
: { status: 'none' },
|
||||
)
|
||||
})
|
||||
// A failed chunk load leaves the page loading, as before R2; a reload fetches again.
|
||||
.catch(() => undefined)
|
||||
return () => {
|
||||
cancelled = true
|
||||
}
|
||||
}, [speciesId])
|
||||
return state
|
||||
}
|
||||
Reference in new issue
Block a user