import { b as Cmd } from './core-sEWVgnpI.js'; import { B as BootingRuntime } from './runtime-types-f231H8Gq.js'; /** * A recorded run, sufficient to reproduce it via `../trace-replay`. * * The recorder OWNS this type; `../trace-replay` imports it. It is the wire * contract between the two halves of the record/replay feature. * * @typeParam S - the machine's State type. * @typeParam M - the machine's Msg union. */ interface Trace { /** * The post-init State captured by `onBoot`, used directly as the recorded * replay's starting State. No init call is repeated. `null` marks a missing * anchor in an older or incomplete trace; `replayTrace` refuses it. */ readonly loaded: S | null; /** The ordered, non-null msgs as observed. The replay input sequence. */ readonly msgs: readonly M[]; /** * The last observed state. The oracle: `replayTrace` deep-compares its * recomputed final state against this. Equal ⇒ the reducer still produces * the same result for this input. */ readonly finalState: S; /** * Per-transition `(msg, post-state)` pairs, in dispatch order, for human * inspection (e.g. "show me the state after step 3"). When present, there * is exactly one entry per recorded msg and `state` is the POST-transition * state for that msg (so `steps[i].msg === msgs[i]` and `steps.at(-1).state` * deep-equals `finalState`). NOT required for replay — replay only needs * `loaded` + `msgs` + `finalState`. * * Present iff the recorder was created with `captureSteps: true`, OR the * Trace was re-hydrated via {@link parseJSONL} from a JSONL whose every step * line carries a state (one written with `captureSteps: true`). A trace * dumped with `captureSteps: false` has `steps === undefined`, and so does * its JSONL once re-hydrated. * * `cmds`, when present, is the Cmds that msg's transition started, as the * engine reported them. `replayTrace` compares them step by step, so a * change that keeps every State but starts different effects is caught. */ readonly steps?: readonly TraceStep[]; } /** One recorded transition: the msg, the State after it, and its Cmds. */ interface TraceStep { readonly msg: M; readonly state: S; readonly cmds?: readonly Cmd[]; } /** Options for {@link recorder}. */ interface RecorderOptions { /** * Fraction of msgs to record, in `(0, 1]`. Default `1` (record everything). * * IMPORTANT — sampling breaks replay fidelity. A sampled trace DROPS msgs, * so its `msgs` array is no longer the complete input sequence and * `replayTrace` will NOT reproduce `finalState`. Sampling exists for * VOLUME STATISTICS (how much traffic, msg-type distribution), not for * faithful replay. Use the default `1` for any trace you intend to replay. * * The boot observe (`msg === null`) is ALWAYS recorded regardless of * `sampleRate` — `loaded` and `finalState` are anchors, not samples. */ readonly sampleRate?: number; /** * When `true`, retain a per-transition `(msg, post-state)` pair in * {@link Trace.steps} for EVERY recorded transition. Default `false`. * * Size vs. fidelity — the precise trade-off: * * - `captureSteps: false` (default) — the MINIMAL replay-faithful trace. * The recorder keeps `loaded` (boot state), the ordered `msgs`, and * `finalState`. That is exactly what `../trace-replay` needs to refold * the run and diff the result. Memory is O(msgs) — one msg ref each, no * per-step state snapshot. `dump().steps` is `undefined`; in JSONL, * interior step lines carry `state: null` (the post-states were never * retained) and only the LAST step line carries `finalState`. A trace * dumped this way REPLAYS correctly but cannot answer "what was the * state after step 3?". * * - `captureSteps: true` — the FULL-fidelity trace. The recorder also * snapshots the post-transition state for every msg, so `dump().steps` * holds `{ msg, state, cmds }` for every transition (post-state and the * Cmds it started) and * `toJSONL()` emits a non-null `state` on every step line. Memory is * O(msgs × |state|) — a state ref per transition (refs, not deep * copies; TEA states are conventionally immutable, so the runtime's * post-transition state object is retained by reference). This is the * mode for per-step inspection and for a JSONL file that round-trips * `steps` exactly via {@link parseJSONL}. * * Replay fidelity is identical in both modes — `../trace-replay` reads * `loaded` + `msgs` + `finalState` only and never touches `steps`. The * choice is purely "do I also want to inspect intermediate states?". */ readonly captureSteps?: boolean; } /** * A live recorder attached to a Runtime. Mirrors `historyTracker`'s * stop/snapshot semantics: `dump()` works at any time (including after * `stop()`), `stop()` is idempotent and detaches the observer. * * @typeParam S - the machine's State type. * @typeParam M - the machine's Msg union. */ interface Recorder { /** * Snapshot the recording so far as a {@link Trace}. Returns a fresh object * with copied arrays on every call (callers may slice/replay without * affecting the recorder's buffers). Entry values are references to the * originals (TEA states/msgs are conventionally immutable). * * Throws only if called before the boot observe has fired AND no msgs have * been recorded — i.e. there is no `finalState` yet. In practice callers * `await runtime.ready` before `dump()`, after which boot has fired. */ dump(): Trace; /** * Serialize the recording as JSONL (one JSON object per line) — a streamable * format that appends cleanly and survives truncation (a partial tail line * is the only loss). The first line is the boot header * `{ kind: "boot", state }`; each subsequent line is * `{ kind: "step", msg, state }` for one recorded transition. * * Re-hydrate with {@link parseJSONL}. * * Round-trip fidelity depends on `captureSteps`: * - `captureSteps: true` — every step line carries its non-null * post-state, so `parseJSONL(rec.toJSONL())` reconstructs `steps` * EXACTLY and deep-equals `rec.dump()` (which also has `steps`). Full * per-step fidelity round-trips. * - `captureSteps: false` (default) — interior step lines carry * `state: null` (those post-states were never retained); only the last * step line carries `finalState`. `parseJSONL` then populates `steps` * with just the entries that had a non-null state (the final one), so * the re-hydrated Trace is NOT deep-equal to `rec.dump()` (which has no * `steps`). It is still fully replay-faithful: `loaded`, `msgs`, and * `finalState` are intact. Record with `captureSteps: true` if you need * interior states to survive the round-trip. */ toJSONL(): string; /** * Detach the underlying observer. After `stop()`: * - no new transitions are recorded; * - `dump()` / `toJSONL()` keep working against the buffered contents; * - the recorder holds no observer reference into the runtime. * * Idempotent — subsequent calls are no-ops. */ stop(): void; } /** * Attach a recorder to a Runtime. Subscribes via `runtime.observe`, capturing * the boot state as `loaded` and every subsequent non-null msg in order. * * One-line creation, mirroring `historyTracker`: * * const rec = recorder(runtime); * // ... drive the runtime ... * await runtime.ready; // boot observe has fired * const trace = rec.dump(); // { loaded, msgs, finalState } * * @param runtime - any `BootingRuntime` (a full `Runtime` satisfies it). * The recorder uses only `observe`, which is total before boot * — so you attach it to the synchronous `run()` handle to * capture the boot transition as `loaded`. * @param opts - see {@link RecorderOptions}. Default records everything, * no step retention. */ declare function recorder(runtime: BootingRuntime, opts?: RecorderOptions): Recorder; /** * Re-hydrate a {@link Trace} from the JSONL produced by * {@link Recorder.toJSONL}. The inverse of `toJSONL`, so * `parseJSONL(rec.toJSONL())` deep-equals `rec.dump()`. * * Parsing rules: * - The first non-blank line MUST be the boot header `{ kind: "boot", state }`. * Its `state` becomes `Trace.loaded`. * - Each `{ kind: "step", msg, state }` line contributes `msg` to * `Trace.msgs` and `(msg, state)` to `Trace.steps`. * - `Trace.finalState` is the `state` of the LAST step line, or the boot * `state` when there are no steps (a boot-only trace). * * Blank lines are skipped (tolerant of trailing newlines / hand-edited files). * A malformed line, a missing/duplicate boot header, or a step with `null` * final state throws — the trace is unusable for replay and silent corruption * is worse than a loud parse error (errors are data; surface them). * * @typeParam S - the machine's State type. * @typeParam M - the machine's Msg union. */ declare function parseJSONL(jsonl: string): Trace; /** * A Sentry-shaped attachment carrying the full {@link Trace} as JSONL. The * shape matches Sentry's `Attachment` type so the consumer can hand it * straight to `addAttachment(...)` without any adaptation. * * - `filename` — `"tea-trace.jsonl"`. The name Sentry shows in the UI and * uses for download; `.jsonl` signals the streamable format. * - `data` — the trace serialized via {@link Recorder.toJSONL}'s format * (re-hydrate downstream with {@link parseJSONL}). * - `contentType` — `"application/x-ndjson"`, the standard MIME for * newline-delimited JSON. */ interface TraceAttachment { readonly filename: string; readonly data: string; readonly contentType: string; } /** * Serialize a {@link Trace} into a Sentry-shaped attachment so a crash event * carries the full, replayable run. Pure — no Sentry import, no I/O. The * returned object matches Sentry's `Attachment` shape exactly. * * The `data` field is JSONL in the same format {@link Recorder.toJSONL} * produces, so the home-side flow is `parseJSONL(attachment.data)` → * `replayTrace(machine, trace, ctx)`. * * Consumer wiring (no Sentry import here — the consumer owns the boundary): * * ```ts * import * as Sentry from "@sentry/browser"; // or "@sentry/node" * import { traceAttachment } from "./internal/persistence/recorder"; // internal module * Sentry.getCurrentScope().addAttachment(traceAttachment(rec.dump())); * ``` * * @typeParam S - the machine's State type. * @typeParam M - the machine's Msg union. */ declare function traceAttachment(trace: Trace): TraceAttachment; /** A Sentry-shaped breadcrumb. Matches the fields of Sentry's `Breadcrumb`. */ interface TraceBreadcrumb { readonly category: string; readonly type: string; readonly level: string; readonly message: string; readonly data?: Record; } /** Options for {@link breadcrumbsFromTrace}. */ interface BreadcrumbsOptions { /** * Max breadcrumbs to emit. Default `100` (Sentry's own default breadcrumb * cap). When the trace has more msgs than `limit`, the OLDEST are dropped — * the returned list is the newest-last tail, matching how a Sentry * breadcrumb ring buffer behaves (the crash context is the recent past). */ readonly limit?: number; /** * Customize each breadcrumb's `message`. Default uses `msg._tag`. Provide a * serializer to surface a richer one-liner (e.g. include an id). The msg is * ALSO always attached verbatim under `data.msg` regardless of this option, * so the structured payload is never lost to the string rendering. */ readonly serialize?: (msg: M) => string; } /** * Project a {@link Trace}'s recorded msgs into Sentry-shaped breadcrumbs — a * human-readable timeline of "what happened before the crash". Pure — no * Sentry import, no I/O. Each returned object matches Sentry's `Breadcrumb` * shape, so the consumer can loop and call `addBreadcrumb(...)`. * * One breadcrumb per recorded msg, in dispatch order (NEWEST LAST — Sentry * renders breadcrumbs oldest-first and the crash is the most recent event). * Each breadcrumb: * - `category` — `"tea"` (groups them in the Sentry breadcrumb panel). * - `type` — `"default"` (the generic Sentry breadcrumb type). * - `level` — `"info"` (msgs are normal flow; the crash is the event). * - `message` — `msg._tag`, or `serialize(msg)` when provided. * - `data.msg` — the full msg object (structured detail kept verbatim). * * Capped at `opts.limit` (default 100). When the trace has more msgs than the * cap, the OLDEST are dropped and the newest `limit` are kept — the recent * lead-up to the crash is the useful context. * * Consumer wiring (no Sentry import here — the consumer owns the boundary): * * ```ts * import * as Sentry from "@sentry/browser"; // or "@sentry/node" * import { breadcrumbsFromTrace } from "./internal/persistence/recorder"; // internal module * for (const b of breadcrumbsFromTrace(rec.dump())) Sentry.addBreadcrumb(b); * ``` * * @typeParam S - the machine's State type. * @typeParam M - the machine's Msg union. */ declare function breadcrumbsFromTrace(trace: Trace, opts?: BreadcrumbsOptions): TraceBreadcrumb[]; export { type BreadcrumbsOptions as B, type RecorderOptions as R, type Trace as T, type Recorder as a, type TraceAttachment as b, type TraceBreadcrumb as c, type TraceStep as d, breadcrumbsFromTrace as e, parseJSONL as p, recorder as r, traceAttachment as t };