/** * Outcome envelope of every core call (closed kinds, never an exception for content): * `{ ok: true, value }` or `{ ok: false, kind, detail?, found? }`. Two kinds come from the * runtime itself: `core_unavailable` (the module is not loaded, failed to load or is being * re-created after a trap) and `internal` (the call trapped; the instance was dropped). */ export type CoreOutcome = | { ok: true; value: T } | { ok: false; kind: string; detail?: string; found?: number } export type CoreFailure = Extract, { ok: false }> export const CORE_UNAVAILABLE: CoreFailure = { ok: false, kind: 'core_unavailable', } export function failure(kind: string, detail?: string): CoreFailure { return detail === undefined ? { ok: false, kind } : { ok: false, kind, detail } } /** Parses the JSON text a core call returned. */ export function parseOutcome(text: string): CoreOutcome { try { return JSON.parse(text) as CoreOutcome } catch { return failure('internal', 'unreadable outcome') } }