0279953 chose the stopped message from CoreRuntime.stopped alone: it
promised the report was kept even when the tab's storage refused the
draft (export, the banner's advice, needs the core), four surfaces still
offered a retry that could not work, and a failed recovery below the cap
was a dead end since nothing called retry().
The runtime restarts a failed recovery on the next call (compiled module
reused, cap still counts traps) and notifies status listeners. One
CoreStoppedNotice on the editor, Home, New report and the full-page
notice: reload when saved, else core.stopped_unsaved with a core-free
copy download (also on the storage-full banner) and a beforeunload
guard. Export, import and new report say the stopped message; a core
failure reading an unread draft is FAILED, not held. ESLint census of
the failure keys; core.stopped says "plusieurs erreurs".
313 lines
11 KiB
TypeScript
313 lines
11 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
|
|
}
|
|
|
|
/**
|
|
* Counts compiles and imports; the numbered glue imports in `failing` are refused like a
|
|
* lost network (import 1 is the first instance, 2 its spare, 3 the recovery's fallback).
|
|
*/
|
|
function flakySource(failing: number[]) {
|
|
const inner = nodeCoreSource('pkg-proofs')
|
|
const source = {
|
|
compiles: 0,
|
|
imports: 0,
|
|
compile: () => {
|
|
source.compiles += 1
|
|
return inner.compile()
|
|
},
|
|
importGlue: async (n: number): Promise<GlueModule> => {
|
|
source.imports += 1
|
|
if (failing.includes(source.imports)) throw new TypeError('offline')
|
|
return inner.importGlue(n)
|
|
},
|
|
}
|
|
return source
|
|
}
|
|
|
|
/**
|
|
* A handle of a trapped instance is freed by the glue's finalizer inside the dead module,
|
|
* which throws out of the garbage collector at a random later time. Tests that trap with a
|
|
* live handle keep it reachable until the process ends.
|
|
*/
|
|
const retainedHandles: unknown[] = []
|
|
function retainHandle(sheet: object): void {
|
|
retainedHandles.push((sheet as { handle: unknown }).handle)
|
|
}
|
|
|
|
/** 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('index_data', '')).toMatchObject({
|
|
ok: true,
|
|
value: { ref: { sheetId: 'sheet', versionId: null } },
|
|
})
|
|
retainHandle(sheet)
|
|
trap(runtime)
|
|
expect(sheet.call('index_data', '')).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('index_data', '')).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)
|
|
expect(runtime.stopped).toBe(false)
|
|
trap(runtime)
|
|
expect(runtime.status).toBe('unavailable')
|
|
expect(runtime.stopped).toBe(true)
|
|
expect(await runtime.retry()).toBe(false)
|
|
expect(runtime.call('core_version', '')).toMatchObject({
|
|
kind: 'core_unavailable',
|
|
})
|
|
})
|
|
|
|
it('is not stopped by a load failure, which a retry can still fix', async () => {
|
|
const runtime = new CoreRuntime({
|
|
compile: () => Promise.reject(new WebAssembly.CompileError('blocked')),
|
|
importGlue: () => Promise.reject(new TypeError('offline')),
|
|
})
|
|
expect(await runtime.ready()).toBe(false)
|
|
expect(runtime.status).toBe('unavailable')
|
|
expect(runtime.stopped).toBe(false)
|
|
})
|
|
|
|
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 })
|
|
// Not JSON at all: a closed refusal too (invalid_json), not a poison.
|
|
expect(runtime.sheet('not json').open()).toMatchObject({ ok: false, kind: 'invalid_sheet' })
|
|
expect(runtime.status).toBe('ready')
|
|
})
|
|
|
|
it('poisons the instance when the handle constructor throws anything but a closed refusal', async () => {
|
|
const inner = nodeCoreSource('pkg')
|
|
const source: CoreSource = {
|
|
compile: () => inner.compile(),
|
|
importGlue: async (n) => {
|
|
const glue = await inner.importGlue(n)
|
|
// An engine stack overflow reaches JavaScript as a RangeError (V8).
|
|
class ThrowingHandle {
|
|
constructor() {
|
|
throw new RangeError('Maximum call stack size exceeded')
|
|
}
|
|
}
|
|
return n === 1 ? ({ ...glue, SheetHandle: ThrowingHandle } as unknown as GlueModule) : glue
|
|
},
|
|
}
|
|
const runtime = new CoreRuntime(source)
|
|
await runtime.ready()
|
|
expect(runtime.sheet(BUNDLE).open()).toMatchObject({ ok: false, kind: 'internal' })
|
|
expect(runtime.status).not.toBe('ready')
|
|
})
|
|
|
|
it('allows three recoveries by default, then stops on the fourth trap', async () => {
|
|
const runtime = new CoreRuntime(countingSource())
|
|
await runtime.ready()
|
|
for (let i = 0; i < 3; i += 1) {
|
|
trap(runtime)
|
|
expect(await runtime.ready()).toBe(true)
|
|
expect(runtime.stopped).toBe(false)
|
|
}
|
|
trap(runtime)
|
|
expect(runtime.stopped).toBe(true)
|
|
expect(await runtime.retry()).toBe(false)
|
|
})
|
|
|
|
it('restarts on the next call after a failed recovery below the cap', async () => {
|
|
const source = flakySource([2, 3])
|
|
const runtime = new CoreRuntime(source)
|
|
await runtime.ready()
|
|
trap(runtime)
|
|
expect(await runtime.ready()).toBe(false)
|
|
expect(runtime.stopped).toBe(false)
|
|
expect(runtime.ready()).toBeInstanceOf(Promise)
|
|
expect(await runtime.ready()).toBe(true)
|
|
expect(runtime.call('core_version', '')).toMatchObject({ ok: true })
|
|
// A restart is not a recovery, and it reuses the compiled module.
|
|
expect(source.compiles).toBe(1)
|
|
})
|
|
|
|
it('kicks the restart from a call and answers the next one', async () => {
|
|
const runtime = new CoreRuntime(flakySource([2, 3]))
|
|
await runtime.ready()
|
|
const sheet = runtime.sheet(BUNDLE)
|
|
trap(runtime)
|
|
expect(await runtime.ready()).toBe(false)
|
|
expect(runtime.call('core_version', '')).toMatchObject({ kind: 'core_unavailable' })
|
|
expect(runtime.status).toBe('loading')
|
|
expect(await runtime.ready()).toBe(true)
|
|
expect(sheet.call('index_data', '')).toMatchObject({ ok: true })
|
|
})
|
|
|
|
it('restarts from a sheet call as well', async () => {
|
|
const runtime = new CoreRuntime(flakySource([2, 3]))
|
|
await runtime.ready()
|
|
const sheet = runtime.sheet(BUNDLE)
|
|
expect(sheet.open()).toEqual({ ok: true, value: null })
|
|
retainHandle(sheet)
|
|
trap(runtime)
|
|
await runtime.ready()
|
|
expect(sheet.call('index_data', '')).toMatchObject({ kind: 'core_unavailable' })
|
|
expect(runtime.status).toBe('loading')
|
|
await runtime.ready()
|
|
expect(sheet.call('index_data', '')).toMatchObject({ ok: true })
|
|
})
|
|
|
|
it('does not restart once the cap is spent', async () => {
|
|
const source = flakySource([2, 3])
|
|
const runtime = new CoreRuntime(source, { maxRecoveries: 1 })
|
|
await runtime.ready()
|
|
trap(runtime)
|
|
expect(await runtime.ready()).toBe(false)
|
|
expect(runtime.stopped).toBe(true)
|
|
const imports = source.imports
|
|
expect(runtime.call('core_version', '')).toMatchObject({ kind: 'core_unavailable' })
|
|
expect(await runtime.ready()).toBe(false)
|
|
expect(runtime.status).toBe('unavailable')
|
|
expect(source.imports).toBe(imports)
|
|
})
|
|
|
|
it('does not restart a failed first load from a call', async () => {
|
|
const source = flakySource([1])
|
|
const runtime = new CoreRuntime(source)
|
|
expect(await runtime.ready()).toBe(false)
|
|
expect(runtime.call('core_version', '')).toMatchObject({ kind: 'core_unavailable' })
|
|
expect(await runtime.ready()).toBe(false)
|
|
expect(source.imports).toBe(1)
|
|
expect(await runtime.retry()).toBe(true)
|
|
})
|
|
|
|
it('notifies listeners after a microtask and survives a throwing one', async () => {
|
|
const runtime = new CoreRuntime(countingSource(), { maxRecoveries: 0 })
|
|
await runtime.ready()
|
|
const calls: boolean[] = []
|
|
runtime.subscribe(() => {
|
|
throw new Error('listener bug')
|
|
})
|
|
const unsubscribe = runtime.subscribe(() => calls.push(runtime.stopped))
|
|
trap(runtime)
|
|
expect(calls).toEqual([])
|
|
await Promise.resolve()
|
|
expect(calls).toEqual([true])
|
|
expect(runtime.stopped).toBe(true)
|
|
unsubscribe()
|
|
await runtime.retry()
|
|
await Promise.resolve()
|
|
expect(calls).toEqual([true])
|
|
})
|
|
})
|