import { C as CmdOf, a as CmdDef, d as SettledOk, e as SettledErr, f as TaggedError, b as Cmd } from './core-sEWVgnpI.js'; /** * The snapshot knob. * * `every` is the cadence: a checkpoint is written once `every` units of * progress have accumulated since the last one (see `record`). It is the * `snapshotEvery` field `monitored-run` threads through to this module. * * `key` is the store key the checkpoint is written under. A single machine * keeps a single rolling checkpoint, so the key is fixed at config time rather * than passed per write — the latest checkpoint overwrites the previous one * (R2/KV are last-write-wins on a key). Defaults to `"@@snapshot"`, mirroring * `doStore`'s `@@state` default-key convention. */ interface SnapshotConfig { /** * Write a checkpoint once this many progress units have accumulated since the * last write. `every: 1` checkpoints on every `record`; a value `<= 0` is * treated as `1` (every progress unit triggers a write) — a non-positive * cadence has no other sensible meaning, and clamping keeps `record` from * never checkpointing on a misconfigured `0`. */ readonly every: number; /** Store key the rolling checkpoint is written under. Defaults to `"@@snapshot"`. */ readonly key?: string; } /** * The snapshot bookkeeping slice. Plain data — JSON-serializable so it rides * inside the consumer's `Store` blob and survives DO eviction / reload (the * "durable" non-negotiable). It carries NO payload: the thing being * checkpointed lives elsewhere in the machine's state; this slice only tracks * *when* to checkpoint and *what has been acknowledged durable*. * * - `sinceLast` — progress units accumulated since the last checkpoint * DECISION (reset to 0 the moment `record` decides to write, not when the * write lands). The cadence counter. * - `seq` — monotonic checkpoint sequence. Bumped each time a write is * emitted, so the write Cmd and its later acknowledgement can be matched * even if several are in flight. The "version" of the latest emitted * checkpoint. * - `lastSavedSeq` — the highest `seq` whose write the host has confirmed * durable (via `confirm`). `null` until the first confirmed write. The gap * `seq - lastSavedSeq` is the "writes emitted but not yet acknowledged" * depth — observable for backpressure / health. * - `lastSavedAt` — the `at` of the most recent confirmed write (epoch ms), * `null` until the first. Carried for observability, never compared inside * a verb (time is injected, never read here). */ interface SnapshotState { readonly sinceLast: number; readonly seq: number; readonly lastSavedSeq: number | null; readonly lastSavedAt: number | null; } /** * The checkpoint-write Cmd a `record` / `force` decision emits. Plain data * (invariant 1 — Cmds are data, not closures): it carries the `payload` to * write, the `key` to write it under, the `seq` it represents, and the `at` it * was decided. * * `payload` is `V` — the consumer's serializable checkpoint value (the run * state, the cursor, whatever the machine wants restartable). The verb takes it * as an argument so the snapshot slice never has to hold a copy of the * machine's domain state. * * `Cmd.define`d (ADR 0014, 0021), minted per `V` by a factory the way * `workflowActivityDef()` is, since the payload type is the knob's own * parameter. The handler you write puts the payload in your store and returns * `ok(undefined)`, or `err({ _tag: "snapshot_write_failed", … })`; the engine * mints `snapshot_write_ok` / `snapshot_write_err`. */ declare function snapshotWriteDef(): 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"; }>; /** * The Cmd a snapshot decision emits to write a checkpoint: `payload` under * `key`, at sequence `seq`. */ type SnapshotWriteCmd = CmdOf>>; /** * The checkpoint-READ Cmd a `requestLoad` decision emits — the recovery half of * the module. Plain data (invariant 1): it carries only the `key` to read. The * handler you write reads the store and returns `ok(payload)` — `null` when * the store has no checkpoint under `key` — or * `err({ _tag: "snapshot_load_failed", … })`. * * Distinct from `snapshot_write`: a write carries a payload OUT to the store; a * load asks the store to hand a payload BACK. A pure restored `init` may return * resume State and Cmds, including a load Cmd from `requestLoad`. A host `boot` * Msg after `run(...)` is an optional protocol; Subs are already open then. * Fold the recovered payload into the run slice, then continue. */ declare function snapshotLoadDef(): CmdDef<"snapshot_load", { readonly key: string; }, V | null, { readonly [detail: string]: unknown; readonly _tag: "snapshot_load_failed"; }>; /** The Cmd `requestLoad` emits to read the checkpoint saved under `key`. */ type SnapshotLoadCmd = CmdOf>>; /** * The Msg the engine mints when a write lands. Fold it through * `confirm(state, msg)` to advance `lastSavedSeq` / `lastSavedAt`. It carries * the write Cmd, so `msg.cmd.seq` matches the emitting decision and a late * acknowledgement of an older write can never regress a newer confirmed one. */ type SnapshotSavedMsg = SettledOk<"snapshot_write", SnapshotWriteCmd, undefined>; /** * The Msg the engine mints when a write fails. Errors are data (never * swallowed): the consumer's reducer decides whether a failed checkpoint is * fatal, retryable (compose `@demlik/tea/retry-backoff`), or merely logged. * `msg.cmd.seq` identifies which checkpoint failed. */ type SnapshotFailedMsg = SettledErr<"snapshot_write", SnapshotWriteCmd, TaggedError<"snapshot_write_failed">>; /** * The Msg the engine mints when a read resolves. `value` is the recovered * checkpoint, or `null` when the store has no checkpoint under `key` (fresh * boot — never checkpointed, or a wiped store). `null` is NOT an error — "no * checkpoint yet" is a normal first-run state, distinct from "the read failed" * (`snapshot_load_err`). */ type SnapshotLoadedMsg = SettledOk<"snapshot_load", SnapshotLoadCmd, V | null>; /** * The Msg the engine mints when a read fails. Errors are data: a failed * recovery read is a visible signal the consumer's reducer decides on * (fatal-abort, retry, or boot-fresh-and-log). Kept apart from a `null` load * so "the store has no checkpoint" can never masquerade as "the read failed". */ type SnapshotLoadFailedMsg = SettledErr<"snapshot_load", SnapshotLoadCmd, TaggedError<"snapshot_load_failed">>; /** * The bundle `createSnapshot` returns: plain functions plus the two Cmd defs to * list in `cmds`. There are no subs: a checkpoint is driven by progress (a * `record` call from the consumer's reducer), not by a timer. */ interface SnapshotKnob { /** The `snapshot_write` Cmd def — list it in the machine's `cmds`. */ readonly write: ReturnType>; /** The `snapshot_load` Cmd def — list it in the machine's `cmds`. */ readonly load: ReturnType>; /** The initial slice — zero progress, no checkpoint emitted or confirmed yet. */ init(): SnapshotState; /** * Account one unit of progress. PURE. Increments `sinceLast`; once the * accumulated count reaches the configured cadence, DECIDES to checkpoint: * resets `sinceLast` to 0, bumps `seq`, and emits a single `snapshot_write` * Cmd carrying `payload` + `key` + the new `seq` + `at`. Below the cadence it * returns the bumped slice and NO Cmd. * * `payload` is the checkpoint value to persist (the consumer's domain state); * `at` is the caller-stamped time the progress occurred (epoch ms) — injected, * never read from the clock here. */ record(state: SnapshotState, payload: V, at: number): readonly [SnapshotState, readonly SnapshotWriteCmd[]]; /** * Force a checkpoint regardless of the cadence counter. PURE. Always bumps * `seq`, resets `sinceLast`, and emits the write Cmd. Use on a phase boundary * the cadence would otherwise miss — e.g. "the run just finished, checkpoint * the terminal state now" — so the last durable checkpoint is the final state, * not whatever the modulo happened to land on. */ force(state: SnapshotState, payload: V, at: number): readonly [SnapshotState, readonly SnapshotWriteCmd[]]; /** * Record a confirmed durable write. PURE. Advances `lastSavedSeq` / * `lastSavedAt` to the acknowledged write's `seq` / `at`, but only forward: * an out-of-order acknowledgement of an OLDER `seq` (a slow write landing * after a newer one) is ignored, so the confirmed watermark is monotonic. * Fold the `snapshot_write_ok` Msg here. * * Returns the reducer-cell shape `readonly [SnapshotState, readonly Cmd[]]` * (with `Cmd.none` — an ack emits nothing) for uniformity with every sibling * knob, so it drops straight into a `snapshot_write_ok` cell. */ confirm(state: SnapshotState, msg: SnapshotSavedMsg): readonly [SnapshotState, readonly Cmd[]]; /** * Resume after a reload. PURE. Resets `sinceLast` to 0 because any * un-checkpointed progress accumulated before the crash is moot — recovery * resumes from the last DURABLE checkpoint, so the cadence counter starts * fresh from there. `seq` / `lastSavedSeq` / `lastSavedAt` are preserved * (they describe what is durable, which a reload does not change). A pure * restored `init` may use this to return resume State and Cmds. A host `boot` * Msg after `run(...)` is an optional protocol; Subs are already open then. * * Returns the reducer-cell shape `readonly [SnapshotState, readonly Cmd[]]` * (with `Cmd.none` — a resume emits nothing) so it drops straight into a * `snapshot_load_ok` cell. */ boot(state: SnapshotState): readonly [SnapshotState, readonly Cmd[]]; /** * Ask the store for the durable checkpoint. PURE. Emits a single * `snapshot_load` Cmd carrying the configured `key`; returns the slice * UNCHANGED (a read decides nothing about the cadence bookkeeping — only the * later `snapshot_load_ok` fold, via `boot`, touches the slice). This is the * entry point of the recovery loop: call from pure restored `init` to return * resume State and the load Cmd, or from an optional host `boot` Msg after * `run(...)`, when Subs are already open. */ requestLoad(state: SnapshotState): readonly [SnapshotState, readonly SnapshotLoadCmd[]]; } /** * Create a snapshot knob from a `SnapshotConfig`. The returned bundle is spliced * into a consumer machine: `init()` seeds the slice, `record` / `force` are * folded in reducer cells, `confirm` / `boot` handle the acknowledgement + * resume Msgs, and `write` / `load` are the Cmd defs whose handlers you write. */ declare function createSnapshot(config: SnapshotConfig): SnapshotKnob; export { type SnapshotWriteCmd as S, type SnapshotState as a, type SnapshotSavedMsg as b, type SnapshotConfig as c, type SnapshotFailedMsg as d, type SnapshotKnob as e, type SnapshotLoadCmd as f, type SnapshotLoadFailedMsg as g, type SnapshotLoadedMsg as h, createSnapshot as i, snapshotWriteDef as j, snapshotLoadDef as s };