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

@@ -7,9 +7,9 @@ import type {
FindingZone,
LateralSide,
LocalizedOptionalText,
} from '@faunosteo/domain'
} from '@faunosteo/core-wasm/types'
import Button from '@/components/ui/Button'
import { pickLabel } from '@/services/sheetIndex'
import { pickLabel } from '@/i18n/pickLocaleLabel'
import { useT } from '@/i18n/useT'
import { useLocaleStore } from '@/state/localeStore'
import type { Locale } from '@/i18n/types'
@@ -25,7 +25,11 @@ export interface FindingsTreePickerProps {
zone: FindingZone
side?: LateralSide
selected: Finding[]
onChange: (next: Finding[]) => void
/**
* A leaf was ticked or unticked: its path and its position among the siblings shown.
* The parent has the core build or remove the finding (R2).
*/
onToggle: (leafPath: string[], leafPosition: number) => void
}
function isLeaf(node: TreeNode): boolean {
@@ -69,13 +73,14 @@ function descriptionOf(node: TreeNode, locale: Locale): string | undefined {
/**
* Drill-down picker over one finding zone of the sheet. A finding is keyed
* by `(findingZoneId, path, side)`; labels are snapshotted at selection.
* by `(findingZoneId, path, side)`; the core snapshots its labels when it
* builds it. The selection predicates below only decide what is shown.
*/
export default function FindingsTreePicker({
zone,
side,
selected,
onChange,
onToggle,
}: FindingsTreePickerProps) {
const t = useT()
const locale = useLocaleStore((s) => s.locale)
@@ -125,25 +130,6 @@ export default function FindingsTreePicker({
selected.filter((f) => matches(f) && pathStartsWith(f.path, nodePath))
.length
function toggleLeaf(leafPath: string[], leaf: TreeNode) {
if (isLeafSelected(leafPath)) {
onChange(
selected.filter((f) => !(matches(f) && samePath(f.path, leafPath))),
)
return
}
const next: Finding = {
findingZoneId: zone.id,
path: leafPath,
labelFrSnapshot: leaf.name.fr,
labelEnSnapshot: leaf.name.en,
}
if ('requires_vet_referral' in leaf && leaf.requires_vet_referral)
next.requiresVetReferral = true
if (side) next.side = side
onChange([...selected, next])
}
const focusedDescription = focusedNode && descriptionOf(focusedNode, locale)
return (
@@ -202,7 +188,7 @@ export default function FindingsTreePicker({
{t('findings.no_options')}
</li>
) : (
currentNodes.map((node) => {
currentNodes.map((node, position) => {
const nodePath = [...path, node.id]
const nodeLabel = pickLabel(node.name, locale)
const nodeDesc = descriptionOf(node, locale)
@@ -215,7 +201,7 @@ export default function FindingsTreePicker({
<input
type="checkbox"
checked={isLeafSelected(nodePath)}
onChange={() => toggleLeaf(nodePath, node)}
onChange={() => onToggle(nodePath, position)}
className="mt-1 accent-[var(--color-accent)]"
aria-label={nodeLabel}
/>
@@ -1,6 +1,6 @@
import Dialog from '@/components/ui/Dialog'
import Button from '@/components/ui/Button'
import type { LateralSide } from '@faunosteo/domain'
import type { LateralSide } from '@faunosteo/core-wasm/types'
import { useT } from '@/i18n/useT'
export interface LateralityPickerProps {
@@ -5,7 +5,7 @@ import type {
LateralSide,
Observation,
ReportZone,
} from '@faunosteo/domain'
} from '@faunosteo/core-wasm/types'
import { AnimatePresence, motion } from 'motion/react'
import Card from '@/components/ui/Card'
import Badge from '@/components/ui/Badge'
@@ -15,7 +15,8 @@ import Textarea from '@/components/ui/Textarea'
import FindingsTreePicker from './FindingsTreePicker'
import FreeTextFindingInput from './FreeTextFindingInput'
import SeveritySelector from './SeveritySelector'
import { isObservable, pickLabel, type SheetIndex } from '@/services/sheetIndex'
import { isObservable, type SheetIndex } from '@/services/sheetIndex'
import { pickLabel } from '@/i18n/pickLocaleLabel'
import { useT } from '@/i18n/useT'
import { useLocaleStore } from '@/state/localeStore'
import type { Locale } from '@/i18n/types'
@@ -96,7 +97,7 @@ function unresolvedFindings(
sheet: SheetIndex,
): Finding[] {
return observation.findings.filter(
(f) => !sheet.nodeAt(f.findingZoneId, f.path),
(f) => !sheet.hasNode(f.findingZoneId, f.path),
)
}
@@ -152,6 +153,23 @@ export default function ObservationCard({
event.stopPropagation()
fn()
}
/** The core builds or removes the finding of the clicked leaf (R2). */
function toggleFinding(
findingZoneId: string,
side: LateralSide | undefined,
path: string[],
leafPosition: number,
) {
const outcome = sheet.ops.toggleFinding({
observation,
findingZoneId,
path,
leafPosition,
...(side ? { side } : {}),
})
if (outcome.ok) onChange(outcome.value)
else window.alert(t('core.action_failed'))
}
const removeFinding = (target: Finding) =>
onChange({
...observation,
@@ -379,11 +397,8 @@ export default function ObservationCard({
zone={zone}
side={side}
selected={observation.findings}
onChange={(next) =>
onChange({
...observation,
findings: next,
})
onToggle={(path, leafPosition) =>
toggleFinding(zone.id, side, path, leafPosition)
}
/>
</div>
@@ -1,4 +1,4 @@
import type { Observation } from '@faunosteo/domain'
import type { Observation } from '@faunosteo/core-wasm/types'
import ObservationCard from './ObservationCard'
import Button from '@/components/ui/Button'
import type { SheetIndex } from '@/services/sheetIndex'
@@ -1,29 +1,39 @@
import type { LateralSide, Observation } from '@faunosteo/domain'
import type {
LateralSide,
Observation,
Vertebra,
} from '@faunosteo/core-wasm/types'
import VertebraPicker from '@/components/observation/VertebraPicker'
import LateralityPicker from '@/components/observation/LateralityPicker'
import { useReportStore } from '@/state/reportStore'
import { useLocaleStore } from '@/state/localeStore'
import { isObservable, pickLabel, type SheetIndex } from '@/services/sheetIndex'
import {
addSides,
collectVertebrae,
sidesPresent,
syncVertebrae,
type ZoneRef,
} from '@/services/zoneInteraction'
import { isObservable, type SheetIndex } from '@/services/sheetIndex'
import { pickLabel } from '@/i18n/pickLocaleLabel'
import { useT } from '@/i18n/useT'
/** The zone the vertebra picker edits, with its initial selection (computed on open). */
export interface VertebraTarget {
zoneIndex: number
initial: Vertebra[]
}
/** The bilateral annotation the laterality picker records sides for. */
export interface LateralTarget {
annotationId: string
sidesPresent: LateralSide[]
}
export interface ObservationPickersProps {
sheet: SheetIndex
activeIndex: number
activeObs: Observation | undefined
vertebraTarget: number | null
lateralTarget: ZoneRef | null
vertebraTarget: VertebraTarget | null
lateralTarget: LateralTarget | null
onCloseVertebra: () => void
onCloseLateral: () => void
}
/** Vertebra and laterality pickers of the active observation. */
/** Vertebra and laterality pickers of the active observation; the core applies the result. */
export default function ObservationPickers({
sheet,
activeIndex,
@@ -37,12 +47,25 @@ export default function ObservationPickers({
const locale = useLocaleStore((s) => s.locale)
const updateObservation = useReportStore((s) => s.updateObservation)
const vertebraZone =
vertebraTarget !== null ? activeObs?.zones[vertebraTarget] : undefined
vertebraTarget !== null
? activeObs?.zones[vertebraTarget.zoneIndex]
: undefined
const vertebraAnnotation =
vertebraZone && sheet.annotation(vertebraZone.annotationId)
const lateralAnnotation =
lateralTarget && sheet.annotation(lateralTarget.annotationId)
/** Applies a core-built observation to the fresh active one; alerts on failure. */
function apply(
run: (fresh: Observation) => ReturnType<SheetIndex['ops']['addSides']>,
) {
const fresh = useReportStore.getState().report?.observations[activeIndex]
if (!fresh) return
const outcome = run(fresh)
if (outcome.ok) updateObservation(activeIndex, outcome.value)
else window.alert(t('core.action_failed'))
}
return (
<>
{vertebraTarget !== null &&
@@ -53,17 +76,12 @@ export default function ObservationPickers({
open
onClose={onCloseVertebra}
onConfirm={(vertebrae) => {
const fresh =
useReportStore.getState().report?.observations[activeIndex]
if (fresh) {
updateObservation(
activeIndex,
syncVertebrae(fresh, vertebrae, sheet, vertebraZone.viewId),
)
}
apply((fresh) =>
sheet.ops.syncVertebrae(fresh, vertebrae, vertebraZone.viewId),
)
onCloseVertebra()
}}
initial={collectVertebrae(activeObs, sheet, vertebraTarget)}
initial={vertebraTarget.initial}
regions={sheet.regions()}
title={`${t('vertebra.title_prefix')}${pickLabel(vertebraAnnotation.name, locale)}`}
initialRegion={
@@ -77,15 +95,11 @@ export default function ObservationPickers({
open
onClose={onCloseLateral}
title={pickLabel(sheet.name(lateralAnnotation), locale)}
existingSides={sidesPresent(activeObs, lateralTarget)}
existingSides={lateralTarget.sidesPresent}
onSelect={(sides: LateralSide[]) => {
const fresh =
useReportStore.getState().report?.observations[activeIndex]
if (fresh)
updateObservation(
activeIndex,
addSides(fresh, lateralAnnotation, sides),
)
apply((fresh) =>
sheet.ops.addSides(fresh, lateralTarget.annotationId, sides),
)
onCloseLateral()
}}
/>
@@ -1,4 +1,4 @@
import type { Severity } from '@faunosteo/domain'
import type { Severity } from '@faunosteo/core-wasm/types'
import { useT } from '@/i18n/useT'
export interface SeveritySelectorProps {
@@ -1,9 +1,14 @@
import { useState } from 'react'
import type { Vertebra, VertebralRegion } from '@faunosteo/domain'
import type {
Vertebra,
VertebraAction,
VertebralRegion,
} from '@faunosteo/core-wasm/types'
import Dialog from '@/components/ui/Dialog'
import Button from '@/components/ui/Button'
import Chip from '@/components/ui/Chip'
import { pickLabel } from '@/services/sheetIndex'
import { pickLabel } from '@/i18n/pickLocaleLabel'
import { reportOps } from '@/core/reportOps'
import { useT } from '@/i18n/useT'
import { useLocaleStore } from '@/state/localeStore'
@@ -26,6 +31,8 @@ const sameVertebra = (a: Vertebra, r: string, i: number) =>
* Picks vertebrae across every region of the sheet. A region shows its
* usual count; when the sheet documents a wider range (D6), one control
* extends the grid to the documented maximum. Nothing horse-specific here.
* The selection rules (tap, check all, uncheck all) run in the core; the
* grid extent and the chip state are display.
*/
export default function VertebraPicker({
open,
@@ -57,19 +64,17 @@ export default function VertebraPicker({
const has = (r: string, i: number) =>
selected.some((v) => sameVertebra(v, r, i))
const toggle = (r: string, i: number) =>
setSelected((cur) =>
has(r, i)
? cur.filter((v) => !sameVertebra(v, r, i))
: [...cur, { region: r, index: i }],
)
const all = (r: string, n: number) =>
setSelected((cur) => [
...cur.filter((v) => v.region !== r),
...Array.from({ length: n }, (_, k) => ({ region: r, index: k + 1 })),
])
const none = (r: string) =>
setSelected((cur) => cur.filter((v) => v.region !== r))
/** One tap, applied by the core to the selection the picker shows (R2). */
const apply = (action: VertebraAction) => {
const outcome = reportOps.selectVertebrae(selected, action)
if (outcome.ok) setSelected(outcome.value)
else window.alert(t('core.action_failed'))
}
const toggle = (region: string, index: number) =>
apply({ kind: 'toggle', region, index })
const all = (region: string, count: number) =>
apply({ kind: 'all', region, count })
const none = (region: string) => apply({ kind: 'none', region })
return (
<Dialog open={open} onClose={onClose} title={title} size="lg">
@@ -1,8 +1,8 @@
import type { Animal, LocalizedText, Owner } from '@faunosteo/domain'
import type { Animal, LocalizedText, Owner } from '@faunosteo/core-wasm/types'
import Label from '@/components/ui/Label'
import Input from '@/components/ui/Input'
import Select from '@/components/ui/Select'
import { pickLabel } from '@/services/sheetIndex'
import { pickLabel } from '@/i18n/pickLocaleLabel'
import { useT } from '@/i18n/useT'
import { useLocaleStore } from '@/state/localeStore'
@@ -1,4 +1,4 @@
import type { Report } from '@faunosteo/domain'
import type { Report } from '@faunosteo/core-wasm/types'
import Label from '@/components/ui/Label'
import Textarea from '@/components/ui/Textarea'
import NextAppointmentPicker from './NextAppointmentPicker'
@@ -0,0 +1,21 @@
import Button from '@/components/ui/Button'
import Notice from '@/components/ui/Notice'
import { useT } from '@/i18n/useT'
export interface CoreUnavailableProps {
onRetry: () => void
}
/**
* Degraded state of the sheet pages when the core module cannot run (CSP, network, an old
* browser, a refused sheet): the session data stays where it is, the practitioner retries.
*/
export default function CoreUnavailable({ onRetry }: CoreUnavailableProps) {
const t = useT()
return (
<main className="p-6 space-y-3 max-w-xl">
<Notice tone="error">{t('sheet.core_unavailable')}</Notice>
<Button onClick={onRetry}>{t('sheet.core_retry')}</Button>
</main>
)
}
@@ -1,5 +1,5 @@
import type { ComponentProps } from 'react'
import type { Animal, Owner, Practitioner } from '@faunosteo/domain'
import type { Animal, Owner, Practitioner } from '@faunosteo/core-wasm/types'
import Button from '@/components/ui/Button'
import Dialog from '@/components/ui/Dialog'
import PractitionerForm from '@/components/report/PractitionerForm'
@@ -1,4 +1,4 @@
import type { Report } from '@faunosteo/domain'
import type { Report } from '@faunosteo/core-wasm/types'
import Input from '@/components/ui/Input'
import Label from '@/components/ui/Label'
import { useT } from '@/i18n/useT'
@@ -1,4 +1,4 @@
import type { Practitioner } from '@faunosteo/domain'
import type { Practitioner } from '@faunosteo/core-wasm/types'
import Label from '@/components/ui/Label'
import Input from '@/components/ui/Input'
import { useT } from '@/i18n/useT'
@@ -1,4 +1,4 @@
import type { Report } from '@faunosteo/domain'
import type { Report } from '@faunosteo/core-wasm/types'
import AppHeader from '@/components/brand/AppHeader'
import Button from '@/components/ui/Button'
import { formatDate } from '@/lib/dates'