// A model vendor's turn and a request's fault, as the World's scenario places them (architecture, "Scenarios"). The // placed turns are matched with the pack's own matchers (its `semantics/scenario.ts` adapter, through the kernel's // grammar in scenario.ts), in the order they were placed; what answered, the labelled default or the fault that landed is recorded // in the twin's log (scenario-consume.ts). import { parseScenarioDocument, ScenarioEngine, scenarioFaultResult, type PackScenarioAdapter, type ScenarioFault, type ScenarioHandler, type ScenarioStatusFault } from './scenario.ts'; import { recordFault, recordTurn, requestFaults, turnDecisions, type ScenarioLedger } from './scenario-consume.ts'; import type { PlacedFault } from './placements.ts'; import type { ScenarioServed } from './sockets.ts'; /** A request fault as the kernel's fault grammar answers it: `rejected` at the edge (503 unless named), `failed` and * `lost` as the vendor's server failure (500 unless named). */ export function kernelFault(fault: PlacedFault): ScenarioFault { if (fault.kind === 'slow') return { kind: 'slow', ms: fault.ms ?? 0 }; if (fault.kind === 'drop') return { kind: 'drop', ...(fault.ms !== undefined ? { holdMs: fault.ms } : {}) }; if (fault.kind === 'generation') return { kind: 'generation', status: fault.status!, message: fault.message!, errorType: fault.errorType!, ...(fault.metadata ? { metadata: fault.metadata } : {}), ...(fault.partialText !== undefined ? { partialText: fault.partialText } : {}), ...(fault.event !== undefined ? { event: fault.event } : {}) }; const status = fault.status ?? (fault.kind === 'rejected' ? 503 : 500); return { kind: 'status', status, ...(fault.retryAfterSeconds !== undefined ? { retryAfterSeconds: fault.retryAfterSeconds } : {}), message: fault.message ?? `${fault.kind}: the World's scenario placed this fault (entry ${fault.index})`, ...(fault.errorType ? { errorType: fault.errorType } : {}), ...(fault.metadata ? { metadata: fault.metadata } : {}) }; } /** A model vendor's turn: a placed fault whose `on` the request satisfies lands first (a `slow` one holds the turn, then * it is decided); then the first placed turn decision whose `on` it satisfies and whose answer it admits, else the * miss, the pack's labelled stub. What landed and what decided are recorded either way. */ export async function serveTurn(asked: Req, ledger: ScenarioLedger, adapter: PackScenarioAdapter, signal?: AbortSignal): Promise { const engineOf = (handlers: ScenarioHandler[]) => new ScenarioEngine(adapter, parseScenarioDocument({ handlers }, adapter)); const faults = requestFaults(ledger).filter((f) => f.on !== undefined); if (faults.length) { const hit = engineOf(faults.map((f) => ({ id: `entry-${f.index}`, on: f.on!, fault: kernelFault(f) }))).next(asked); if (hit.kind === 'handler') { const fault = faults.find((f) => `entry-${f.index}` === hit.ruleId)!; await recordFault(ledger, fault, { point: 'turn' }); // a generation failure is admitted, then fails: the pack's handler answers it in its SDK's error wire if (fault.kind === 'generation') return hit; const result = await scenarioFaultResult(hit.fault!, () => adapter.renderFault?.(hit.fault as ScenarioStatusFault, asked), signal); if (result !== null) return { kind: 'fault', result, ruleId: hit.ruleId }; } } const decisions = turnDecisions(ledger); const decided = engineOf(decisions.map((d) => ({ id: `entry-${d.index}`, on: d.on ?? {}, respond: d.outcome }))).next(asked); await recordTurn(ledger, decided.kind === 'handler' ? decisions.find((d) => `entry-${d.index}` === decided.ruleId) : undefined, decided.kind === 'handler' ? decided.respond : null); return decided; } /** What a request fault answers in place of the vendor's answer: the vendor's own envelope where the pack renders one. */ export async function faultAnswer(fault: PlacedFault, render: (() => { body: unknown; headers?: Record } | undefined) | undefined, signal?: AbortSignal): Promise { const result = await scenarioFaultResult(kernelFault(fault), render, signal); return result === null ? null : Response.json(result.body, { status: result.status, headers: result.headers }); }