Files
faunosteo/packages/core-wasm/tests/runtime.test.ts
T
bchanot 343d6f4c1f feat(workspace): R2.4 core runtime — lazy load, poison on trap, offline re-instantiation, per-generation sheet handles
packages/core-wasm: browser-safe CoreRuntime (synchronous calls once ready;
a WebAssembly.RuntimeError poisons the instance, the call answers internal,
a fresh instance comes from the kept compiled module and a glue record
imported in advance; at most 3 recoveries per page; retry after a load
failure), SheetSession (handle rebuilt per instance generation, never
called on a dropped one; invalid_sheet without poisoning), Node source.
crates/wasm: SheetHandle (sheet held in the module memory). Two builds:
pkg/ shipped without trap exports (checked), pkg-proofs/ for the trap
tests; BUILD_STAMP (hash of the Rust sources) refused when missing or
stale by the vitest global setup instead of skipping. Failure envelope
carries a numeric found version; compact JS-number output.
2026-10-14 00:12:04 +02:00

148 lines
5.1 KiB
TypeScript

/**
* The core runtime (R2 plan P9 rev 2) on the proofs build, whose trap exports make the
* failure paths real: lazy load, poison on a trap, re-instantiation from the kept compiled
* module and the pre-imported glue record (no network), handles rebuilt per instance and
* never called on a dropped one, the recovery cap, load failure and retry.
*/
import { readFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { CoreRuntime, type CoreSource, type GlueModule } from '../src/index'
import { nodeCoreSource, type ProofModule } from '../src/node'
const SEED = '../../../supabase/seed/anatomical-sheets/equine/draft/'
function seedJson(name: string): unknown {
const path = fileURLToPath(new URL(`${SEED}${name}`, import.meta.url))
return JSON.parse(readFileSync(path, 'utf8'))
}
const BUNDLE = JSON.stringify({
status: 'draft',
ref: {
sheet_id: 'sheet',
version_id: null,
document_sha256: '0'.repeat(64),
schema_version: 2,
species_id: 'equine',
},
document: seedJson('document.json'),
migrationMap: seedJson('report-migration-map.json'),
assets: {},
assetHashes: {},
})
/** Counts glue imports; imports after `offlineAfter` fail like a lost network. */
function countingSource(offlineAfter = Infinity) {
const inner = nodeCoreSource('pkg-proofs')
const source = {
imports: 0,
compile: () => inner.compile(),
importGlue: async (n: number): Promise<GlueModule> => {
source.imports += 1
if (source.imports > offlineAfter) throw new TypeError('offline')
return inner.importGlue(n)
},
}
return source
}
/** Traps the live instance through the proofs build (stack exhaustion). */
function trap(runtime: CoreRuntime) {
return runtime.invoke((glue) => {
;(glue as ProofModule).recurse_for_test(2_000_000)
return { ok: true, value: null }
})
}
describe('core runtime', () => {
it('loads on first need only', async () => {
const source = countingSource()
const runtime = new CoreRuntime(source)
expect(runtime.status).toBe('idle')
expect(runtime.call('core_version', '')).toMatchObject({
ok: false,
kind: 'core_unavailable',
})
expect(source.imports).toBe(0)
expect(await runtime.ready()).toBe(true)
expect(runtime.call('core_version', '')).toMatchObject({ ok: true })
})
it('poisons on a trap and recovers offline from the spare glue record', async () => {
const source = countingSource(2)
const runtime = new CoreRuntime(source)
await runtime.ready()
expect(trap(runtime)).toMatchObject({ ok: false, kind: 'internal' })
expect(runtime.status).toBe('recovering')
expect(runtime.call('core_version', '')).toMatchObject({
kind: 'core_unavailable',
})
expect(await runtime.ready()).toBe(true)
expect(runtime.generation).toBe(2)
expect(runtime.call('core_version', '')).toMatchObject({ ok: true })
})
it('rebuilds a sheet handle on the new instance and never calls the dropped one', async () => {
const runtime = new CoreRuntime(countingSource())
await runtime.ready()
const sheet = runtime.sheet(BUNDLE)
expect(sheet.call('sheet.ref', '')).toMatchObject({
ok: true,
value: { sheetId: 'sheet', versionId: null },
})
trap(runtime)
expect(sheet.call('sheet.ref', '')).toMatchObject({
kind: 'core_unavailable',
})
await runtime.ready()
// A call through the stack-trapped instance would fail in its allocator and poison
// again: one generation more and a ready runtime prove the handle was rebuilt.
expect(sheet.call('sheet.ref', '')).toMatchObject({ ok: true })
expect(runtime.generation).toBe(2)
expect(runtime.status).toBe('ready')
})
it('stops recovering after the cap', async () => {
const runtime = new CoreRuntime(countingSource(), { maxRecoveries: 1 })
await runtime.ready()
trap(runtime)
expect(await runtime.ready()).toBe(true)
trap(runtime)
expect(runtime.status).toBe('unavailable')
expect(await runtime.retry()).toBe(false)
expect(runtime.call('core_version', '')).toMatchObject({
kind: 'core_unavailable',
})
})
it('reports a load failure and loads again on retry', async () => {
const inner = nodeCoreSource('pkg')
let failNext = true
const source: CoreSource = {
compile: () => {
if (failNext) {
failNext = false
return Promise.reject(new WebAssembly.CompileError('blocked'))
}
return inner.compile()
},
importGlue: (n) => inner.importGlue(n),
}
const runtime = new CoreRuntime(source)
expect(await runtime.ready()).toBe(false)
expect(runtime.status).toBe('unavailable')
expect(await runtime.retry()).toBe(true)
expect(runtime.call('core_version', '')).toMatchObject({ ok: true })
})
it('refuses an invalid sheet payload without poisoning the instance', async () => {
const runtime = new CoreRuntime(nodeCoreSource('pkg'))
await runtime.ready()
const outcome = runtime.sheet('{"status":"draft"}').open()
expect(outcome).toMatchObject({ ok: false, kind: 'invalid_sheet' })
expect(runtime.status).toBe('ready')
expect(runtime.sheet(BUNDLE).open()).toEqual({ ok: true, value: null })
})
})