UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

88 lines (87 loc) • 6.18 kB
import { type LedgerDamagedLine } from "./shared-ledger.js"; /** * The three snapshot+ledger families of a file data root, named by the directory each lives in under it — the names the read-only check * reports by and the operator verb takes. A damaged pair refuses ITS store only ({@link import("./shared-ledger.js").FileLedgerDamagedError}). */ export type FileLedgerName = "checkpoints" | "workflow-runs" | "background-agents"; /** One ledger as {@link inspectFileLedgers} reads it. */ export type FileLedgerHealth = /** Every line is a record (a missing ledger is clean: it holds no rows). Its store opens. */ { readonly state: "clean"; } /** Clean, and its last segment is an append that never returned (a crash's — or, beside a live store, one in flight): read as not having * happened; the store opens and its append log cuts the bytes. */ | { readonly state: "uncommitted-tail"; readonly line: number; readonly bytes: number; } /** Its store refuses to open (`store.ledger_damaged`) until {@link setAsideLedgerDamage} runs. `setAside` = the rows the verb would * set aside (by non-secret name), `kept` = the rows it would keep. */ | ({ readonly state: "damaged"; } & DamageReport) /** The pair kept being compacted under three reads (a live store beside the check): no verdict — check again. */ | { readonly state: "unsettled"; }; /** What a damaged pair's fold says, for the check and the verb alike. */ interface DamageReport { readonly damagedLines: readonly LedgerDamagedLine[]; /** The rows set aside (or that would be) — present in the fold but possibly stale — by their non-secret names. */ readonly setAside: readonly string[]; /** Snapshot rows lost outright (one per damaged snapshot line); a damaged ledger line may also have held a row's only state. */ readonly snapshotRowsLost: number; /** Rows the rewritten pair keeps (or would): the ones the damage cannot have moved. */ readonly kept: number; } /** * Read every snapshot+ledger pair of a data root and say which of them would refuse to open — WITHOUT opening any: no writer fence is * taken or consulted, nothing is created, cut or written, so it runs beside a live store (an older engine still serving the root before * an upgrade) and before a host decides which stores to start. The judgement is the stores' own (`judgeJsonlFile` through the one fold), * bracketed against a compaction. A root whose adoption is in flight is refused as the stores refuse it; a ledger event kind this binary * does not know throws as the checkpoint store's replay does. It judges LINES, not rows: a row that folds from records but does not read * as a row of its store (a checkpoint that reads pending yet records its execution) is clean here, and the store's own reads answer it * (a listing names it damaged, a resume refuses it). Its `root` is the data root the stores were given: a store built over another * directory is checked by passing that directory. */ export declare function inspectFileLedgers(root: string): Readonly<Record<FileLedgerName, FileLedgerHealth>>; /** What {@link setAsideLedgerDamage} did. */ export type LedgerDamageSetAside = /** The pair holds no damage: nothing was copied, written or created — a second run after a set-aside answers this. */ { readonly ledger: FileLedgerName; readonly damaged: false; } | ({ readonly ledger: FileLedgerName; readonly damaged: true; /** Where the verbatim copy of both files now is: `<root>/<ledger>/set-aside-<stamp>` ({@link setAsideLedgerDamage}). */ readonly setAsideDir: string; } & DamageReport); /** * Set a damaged ledger's damage aside, so its store opens again. Run it with that store CLOSED — in this process and every other (a store * open here is refused `store.dir_in_use`; another process holding the directory refuses through its writer fence, which this verb takes). * * In order: copies both files of the pair VERBATIM into a NEW directory `<root>/<ledger>/set-aside-<stamp>/` (directory 0700, files * 0600) and makes the copies durable — each file synced, then both directories (a failure to open or sync any of them throws before the * pair is touched; on Windows, which has no directory sync, the copies' contents are synced only) — then rewrites the pair from the rows the * damage cannot have moved — each row some event settled whole * AFTER the last damaged ledger line — and empties the ledger, as a compaction does. Every other row is set aside: it stays only in the * copy. Nothing is guessed back: a row whose newest state may have been on a damaged line is not served at its previous state, and for the * checkpoint ledger that is the closed direction — a set-aside card is gone, so a resume of it answers `checkpoint.not_found` and a person * starts over; it is never decided a second time. `<stamp>` is the wall clock in UTC to the millisecond (ISO 8601 with `:` and `.` as `-`) * and six random hex digits — `set-aside-2026-11-14T01-02-03-456Z-a1b2c3` — so two runs in one millisecond still name two directories, * and a name that exists already is refused (never merged into). * * Idempotent: on a pair with no damage it answers `damaged: false` and creates, copies and writes nothing; interrupted between the * snapshot and the ledger rewrite, a second run keeps the same rows. A ledger event kind this binary does not know (a newer worker's * ledger) refuses as the store's replay refuses it, and nothing is written. * * While an adoption of the root is in flight, it runs on a ledger one of the adoption's identity legs still has to read (the leg has * not written its done bit) and is refused, as every store is, on any other: the leg replays the repaired pair and judges every row * again at its next step, and the adoption then completes. It moves what the repaired pair holds — a row whose arrival was set aside * or lost while its source was kept stays under the old owner — and its report keeps the count the leg's first bit recorded. */ export declare function setAsideLedgerDamage(root: string, ledger: FileLedgerName): LedgerDamageSetAside; export {};