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.
This commit is contained in:
bchanot committed 2026-10-14 00:12:04 +02:00
1 parent 22fea8736d
commit 343d6f4c1f
22 files changed
+783 -180

No files matched your search

+68
View File
@@ -0,0 +1,68 @@
/**
* Node source of the core module (vitest suites, scripts): the glue and the `.wasm` read
* from `pkg/` (the shipped build) or `pkg-proofs/` (with the R0 trap exports). Never
* imported by the application bundle.
*/
import { existsSync } from 'node:fs'
import { readFile } from 'node:fs/promises'
import { fileURLToPath } from 'node:url'
import { CoreRuntime, type CoreSource, type GlueModule } from './runtime'
export type CoreBuild = 'pkg' | 'pkg-proofs'
const glueUrl = (build: CoreBuild) =>
new URL(`../${build}/faunosteo_wasm.js`, import.meta.url)
const wasmUrl = (build: CoreBuild) =>
new URL(`../${build}/faunosteo_wasm_bg.wasm`, import.meta.url)
/** Path of a built module on disk (size measurements). */
export function coreWasmPath(build: CoreBuild = 'pkg'): string {
return fileURLToPath(wasmUrl(build))
}
/** True when `pnpm wasm:build` has produced the module. */
export function isCoreBuilt(build: CoreBuild = 'pkg'): boolean {
return existsSync(coreWasmPath(build))
}
/** Each import gets its own query string, hence its own module record and instance. */
let imports = 0
export function nodeCoreSource(build: CoreBuild = 'pkg'): CoreSource {
return {
compile: async () =>
WebAssembly.compile(await readFile(coreWasmPath(build))),
importGlue: async () => {
imports += 1
return (await import(
`${glueUrl(build).href}?instance=${imports}`
)) as GlueModule
},
}
}
/** A runtime over the Node source, loaded. */
export async function nodeCoreRuntime(
build: CoreBuild = 'pkg',
): Promise<CoreRuntime> {
const runtime = new CoreRuntime(nodeCoreSource(build))
if (!(await runtime.ready())) {
throw new Error(`core module ${build} failed to load`)
}
return runtime
}
/** The functions of the proofs build (R0 trap proofs). */
export interface ProofModule extends GlueModule {
panic_for_test(): void
recurse_for_test(depth: number): number
}
/** A fresh, initialised glue of the proofs build, outside any runtime (R0 proofs). */
export async function loadProofModule(): Promise<ProofModule> {
const source = nodeCoreSource('pkg-proofs')
const glue = (await source.importGlue(0)) as ProofModule
await glue.default({ module_or_path: await source.compile() })
return glue
}