import { a as CmdDef, T as TimerDeps, b as Cmd } from './core-sEWVgnpI.js'; import { S as SnapshotWriteCmd, a as SnapshotState, b as SnapshotSavedMsg } from './index-DUdoElz8.js'; import { D as DeadlineExceeded, a as DeadlineSub } from './index-CCs-QGGu.js'; import { Q as QueueItem } from './index-DmoyvP-O.js'; /** * The monitored-run knob. EVERY field is optional: * - omit `stages` → single-shot run (no pipeline; one `advance` finishes). * - omit `deadlineMs` → no no-progress watchdog (the run is never auto-failed). * - omit `snapshotEvery`→ no periodic checkpoint (no `snapshot_write` Cmds). * * An empty config `{}` is a valid single-shot, unwatched, uncheckpointed run. * * `Stage` is the consumer's stage identifier — any JSON-serializable value * (a string id, a `{ kind, ... }` record). It rides through the stage queue * unchanged so the consumer's reducer can branch on the current stage to emit * that stage's outstanding effect (the audit machine's `outstandingEffect`). */ interface MonitoredRunConfig { /** * The ordered stages of the pipeline. Position survives eviction (lives in * the Model's stage queue). Omit for a single-shot run. An empty array is * treated as single-shot too — there is nothing to pipeline through. */ readonly stages?: readonly Stage[]; /** * No-progress watchdog budget, in ms. The safety deadline is armed at * `lastProgressAt + deadlineMs` and re-armed on every progress event. Omit * for no watchdog. */ readonly deadlineMs?: number; /** * Checkpoint cadence — write a durable snapshot once this many progress * units have accumulated. Threaded straight into `../snapshot`'s `every`. * Omit for no checkpointing. */ readonly snapshotEvery?: number; /** * Store key the rolling checkpoint is written under. Forwarded to * `../snapshot`'s `key`; defaults to that module's `@@snapshot`. Ignored * when `snapshotEvery` is omitted. */ readonly snapshotKey?: string; } /** * Why a run terminated as `failed`. Discriminated so each reason carries only * its own data (pattern 11): * - `deadline` — the no-progress watchdog fired (`onDeadline`). * - `stage` — the consumer reported a stage failure via `advance(..., fail)`. * * `stale` is a SEPARATE phase (not a fail reason): a run is `stale` only by an * explicit consumer signal that progress has wedged WITHOUT the deadline having * fired — a soft, recoverable observation. The deadline firing is a hard * terminal (`failed { reason: "deadline" }`). * * The `stage` variant carries the FAILING STAGE VALUE (the `Stage` itself), not * a positional index: done stages leave the queue (`markDoneOp` filters them), * so a residual index drifts as the pipeline advances. The stage value is the * stable, meaningful identifier — `undefined` for a single-shot run (no stage). */ type RunFailure = { readonly reason: "deadline"; readonly at: number; } | { readonly reason: "stage"; readonly stage: Stage | undefined; readonly error: unknown; }; /** * The run bookkeeping every phase carries in common, split out so each arm of * the `MonitoredRunState` discriminated union names only the data it ADDS * (`runId` on started phases, `failure` on `failed`) — the same discipline the * sibling `PaginatorState` uses for its shared `seen` / `pages` counters. Plain * data end to end so the whole slice survives DO eviction / reload: * * - `stepStates` — the stage pipeline as a `work-queue` of stage items. * Empty for a single-shot run. The ONE `running` item is * the current stage; `done` items are retired stages * (they leave the queue via `markDoneOp`, so the queue * shrinks as the pipeline advances and is empty at * `done`). POSITION survives eviction because this list * is durable. * - `startedAt` — `at` of `start`. Observability; never compared in a verb. * - `lastProgressAt` — `at` of the most recent progress event; the watchdog * base. Re-armed by `progress` / `advance`. * - `progressSeq` — monotonic progress counter; keys the safety alarm Sub * id so every bump retires the old alarm and arms a new * one (the audit subs' `safety:{progressSeq}` discipline). * - `snapshot` — the `../snapshot` slice (cadence counter + durable * watermark). Always present (uniform shape); the gate * is simply never driven when `snapshotEvery` is omitted. */ interface RunCore { readonly stepStates: readonly QueueItem[]; readonly startedAt: number; readonly lastProgressAt: number; readonly progressSeq: number; readonly snapshot: SnapshotState; } /** * The slice — a discriminated union on `phase` so each phase carries ONLY its * own data and impossible combinations are unrepresentable (pattern 11), the * same idiom the sibling `CircuitState` / `PaginatorState` / `RunFailure` follow: * * - `idle` — created but never started. Carries no `runId` (identity is * host-minted at `start`) and no `failure`: "not started" is its * OWN phase, not a `running` run wearing an empty-string `runId` * sentinel. `start` is the only transition out, so no verb has to * guard "has this actually started?" — `phase === "idle"` says it. * - `running` — the run is live. Carries the host-minted `runId` and the * `RunCore` pipeline position + watchdog bookkeeping. * - `stale` — a soft "no progress observed" mark the consumer can set and * clear; still resumable (a `progress` clears it back to * `running`). Distinct from a fired deadline, which is terminal. * - `done` — every stage retired (or the single-shot `advance` landed). * - `failed` — terminal failure; the typed `failure` reason rides ONLY this * arm, so it is present iff `phase === "failed"` BY CONSTRUCTION — * there is no nullable side field to keep in sync. * - `cancelled` — the consumer stopped the run from outside (`cancel`). Its own * phase rather than a `RunFailure` reason, on the same argument * that keeps `stale` a phase: a caller who asked for the stop did * not suffer a failure, and every "did this fail?" reader would * otherwise have to except one reason. Terminal, so it re-enters * no verb and arms no watchdog. * * `runId` is the host-minted run identity, stamped at `start` (the machine never * mints — see the audit seed's "run identity is host-minted"). It rides every * started phase, but not `idle`. `cancelled` is the one phase reachable from BOTH * sides of that line, so its `runId` is `string | null`: `null` says the run was * cancelled before it ever started and there was no identity to mint, which is a * different fact from a cancelled live run and stays a different value. */ type MonitoredRunState = (RunCore & { readonly phase: "idle"; }) | (RunCore & { readonly phase: "running"; readonly runId: string; }) | (RunCore & { readonly phase: "stale"; readonly runId: string; }) | (RunCore & { readonly phase: "done"; readonly runId: string; }) | (RunCore & { readonly phase: "failed"; readonly runId: string; readonly failure: RunFailure; }) | (RunCore & { readonly phase: "cancelled"; readonly runId: string | null; readonly at: number; }); /** * The ENDED phases — a run that finished (`done`) or was stopped from outside * (`cancelled`). Every arm here was started or explicitly stopped, so `idle` is * out and `runId` is present on the whole union: a consumer holding one of these * reads the run identity without a `phase` guard the value can never fail. * * It is the resolved half of a drive: a promise that resolves only on an ended * run should say so in its type rather than resolve `MonitoredRunState` and * leave every reader to re-prove the `idle` arm impossible (#155). `cancelled` * keeps its `string | null` `runId` unchanged — a run cancelled before it ever * started genuinely has no identity, and that stays a different value here. */ type EndedRun = Extract, { readonly phase: "done" | "cancelled"; }>; /** * The outcome a consumer reports to `advance`: the current stage either * succeeded (retire it, claim the next) or failed (terminate the run). Plain * data — the consumer parses its stage result at its own boundary and hands a * settled outcome here. */ type StageResult = { readonly kind: "ok"; } | { readonly kind: "fail"; readonly error: unknown; }; /** The checkpoint-write Cmd, generic over the consumer's checkpoint value `V`. */ type MonitoredRunCmd = SnapshotWriteCmd; /** The deadline Msg the safety alarm dispatches when the watchdog fires. */ type MonitoredRunTimerMsg = DeadlineExceeded; /** * Build a monitored-run knob from `config`. `Stage` is the consumer's stage * identifier type; `V` is the consumer's checkpoint value type (what * `snapshotEvery` checkpoints). Returns the uniform L2 knob contract. * * No `rng` parameter: unlike `resilient-call`, nothing here jitters — the only * external inputs are time (an `at` / `atMs` parameter) and the consumer's * stage outcomes. */ declare function createMonitoredRun(config?: MonitoredRunConfig): { /** The `snapshot_write` Cmd def — list it in `cmds` when checkpointing. */ checkpoint: CmdDef<"snapshot_write", { readonly key: string; readonly seq: number; readonly at: number; readonly payload: V; }, undefined, { readonly [detail: string]: unknown; readonly _tag: "snapshot_write_failed"; }>; init: () => MonitoredRunState; start: (s: MonitoredRunState, runId: string, at: number) => readonly [MonitoredRunState, readonly MonitoredRunCmd[]]; progress: (s: MonitoredRunState, payload: V, at: number) => readonly [MonitoredRunState, readonly MonitoredRunCmd[]]; markStale: (s: MonitoredRunState) => readonly [MonitoredRunState, readonly MonitoredRunCmd[]]; cancel: (s: MonitoredRunState, at: number) => readonly [MonitoredRunState, readonly MonitoredRunCmd[]]; advance: (s: MonitoredRunState, payload: V, result: StageResult, at: number) => readonly [MonitoredRunState, readonly MonitoredRunCmd[]]; onDeadline: (s: MonitoredRunState, msg: MonitoredRunTimerMsg) => readonly [MonitoredRunState, readonly MonitoredRunCmd[]]; boot: (s: MonitoredRunState, at: number) => readonly [MonitoredRunState, readonly MonitoredRunCmd[]]; confirmSnapshot: (s: MonitoredRunState, msg: SnapshotSavedMsg) => MonitoredRunState; deadlines: (s: MonitoredRunState) => readonly DeadlineSub[]; timer: (s: MonitoredRunState) => TimerDeps | null; }; /** * Lift a knob result `[slice, cmds]` into a host `[State, cmds]` where the * slice lives at `state.run`. Convenience for the common single-slice host; * hosts with a differently-named field spread by hand. PURE — a thin record * rebuild, no clock / RNG. */ declare function liftRun; }, Stage, C extends Cmd>(state: S, [slice, cmds]: readonly [MonitoredRunState, readonly C[]]): readonly [S, readonly C[]]; export { type EndedRun as E, type MonitoredRunCmd as M, type RunFailure as R, type StageResult as S, type MonitoredRunConfig as a, type MonitoredRunState as b, type MonitoredRunTimerMsg as c, createMonitoredRun as d, liftRun as l };