UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

167 lines (166 loc) • 12.3 kB
import type { MemoryEntryOrigin, MemoryGovernanceDocs, MemoryGovernanceObjects } from "./types.js"; /** The part of a governance store the account is kept through: its document and its custody objects. */ export type OriginClearanceStore = MemoryGovernanceDocs & Pick<MemoryGovernanceObjects, "putObject" | "getObject" | "deleteObject">; /** The custody object a clearance's cleared-entry text is kept as (kind `clearance-custody`): named by the row, so the * row alone finds it again. */ export declare function clearanceCustodyName(clearanceId: string): string; /** One clearance's terminal event (appended, never rewritten). */ export interface OriginClearanceEvent { eventId: string; at: number; to: "done" | "failed"; /** The resolver of THIS event (the opener on the normal path; a later resumer on a crash path). */ requestId: string; /** Mechanical detail (landing slug, refusal reason) — engine-composed, bounded by the writer. */ detail?: string; } export interface OriginClearanceRow { clearanceId: string; /** The entry whose marker was cleared. The re-record keeps the SAME id (§2.3-2's later-batch * arm), so this key joins the clearance to the entry across its whole life. */ entryId: string; scope: string; slug: string; /** The committed rev the clearance was judged against (the tombstone's CAS anchor). */ baseRev: string; /** The cleared marker, verbatim (audit: what the host vouched away). */ origin: MemoryEntryOrigin; /** WHO cleared — the mandatory audit attribution (never defaulted). */ requestId: string; /** WHY — the host's stated ground (never defaulted). */ reason: string; at: number; status: "pending" | "done" | "failed"; /** CUSTODY: the full serialized cleared-entry file text. A crash between the tombstone batch and * the re-record batch leaves this as the only copy — the pending row is the loud recovery seat * (a later clearEntryOrigin call for the same entry resumes from it). Kept as the row's custody * object ({@link clearanceCustodyName}), never inside the account: the stored row carries the * text's sha-256 (`custodyDigest`) and every read answers the text, checked against it. `""` on a row whose * custody was erased with its entry (`custodyErased`). */ entryText: string; /** Present (`true`) ⇔ an erasure of the entry erased this clearance's custody (#3463): the text is gone from the * store, `entryText` answers `""`, and the row no longer resumes (`memory.origin_clear_unknown`). The row itself — * who cleared, why, when, its events — stays. ADDITIVE: absent on every row whose custody stands. */ custodyErased?: true; /** Set the moment THIS clearance's tombstone commits ({@link markOriginClearanceTombstoned}) — * the resume arm's arbiter for an absent entry: with it, absence is the known crash window * (tombstone landed, re-record owed — replay from custody); without it, the absence is an * INDEPENDENT deletion this clearance never made, and replaying the add would silently undo a * deliberate delete. Absent on rows recorded before this member existed — such a row's resume * refuses conservatively (custody stays disclosed on the row; the refusal states it). */ tombstonedAt?: number; events: OriginClearanceEvent[]; } /** * #468② — the ACCOUNT SHADOW a `memory.origin_clear_not_marked` refusal carries when the engine already * held the answer: the entry's most recent COMPLETED clearance, as who asked for it and when it settled. * * It exists to discriminate the refusal's two very different causes without a second full read of the * account: an entry that was NEVER marked (no shadow — nothing to clear, and nothing ever cleared) versus a * RE-SEND of a clear that already succeeded (shadow present — the caller's own earlier call, or another * host's, already did this). Absence is honest absence: the engine never opens an extra read to fill it, so * a refusal raised on a path that did not need the ledger simply omits it. * * `requestId` is the OPENER's attribution (the `clearEntryOrigin` caller's own key — what a re-sending * caller compares against), while `settledAt` is the row's terminal `done` event, which on a crash-resumed * row is a LATER moment than the opening. The full row (reason, origin, custody, every event) stays one * {@link OriginClearanceRow} lookup away — this shadow is a discriminator, never a replacement for it. * * AS-OF SEMANTICS (codex r3): the account is read ONCE per `clearEntryOrigin` call, by the probe that looks * for a resumable pending row, and the committed entry is read after it. A clearance opened AND settled by * ANOTHER caller inside that window is therefore not in the snapshot this shadow is derived from, so its * absence means "no settled clearance as of this call's own read of the account", never "none exists" — and * a fresh call answers with the newer account. The caller's OWN earlier clear (the re-send case this exists * for) settled long before and is always in the snapshot. Deliberately not closed by a second read: the * shadow is a convenience over the authoritative `listOriginClearances`, and buying strictness with an extra * ledger read on a refusal path would cost every caller what only a concurrent third party could observe. */ export interface OriginClearanceShadow { requestId: string; settledAt: number; } /** Move every custody text the account still carries inline out into its custody object (each object before the * update that drops its text); writes nothing when there is none. */ export declare function moveOriginClearanceCustodyOut(store: OriginClearanceStore): Promise<void>; /** Strict read of the whole account (host audit face; corrupt = throw). Each row answers its custody text, wherever the * account keeps it (`""` once an erasure took it, `custodyErased`). */ export declare function readOriginClearances(store: Pick<MemoryGovernanceDocs, "readDoc"> & Pick<MemoryGovernanceObjects, "getObject">): Promise<OriginClearanceRow[]>; /** The account's erasure count — the store's one count of the erase door's runs — read BEFORE a reader reads what an * erasure takes: a clearance before it judges its entry (the count {@link openOriginClearance} appends under), a memory * mount before it lists any entry (`MemorySessionHandle.storeState`, the key a frozen copy of the index is found by). * A read writes nothing: an account never written reads 0, and every erasure writes it ({@link eraseOriginClearanceCustody}). */ export declare function readClearanceErasureCount(store: Pick<MemoryGovernanceDocs, "readDoc">): Promise<number>; /** * WRITE-AHEAD open: append the pending row (custody included) before any store mutation. Refuses a * second OPEN while a pending row for the same entry stands — the pending row is a resume seat, * not a queue (the caller resumes it instead). The custody text lands as its object before the update * that appends the row naming it; a refused open removes the object it wrote (no row names it). * * UNDER THE COUNT IT JUDGED UNDER (#3559). `judgedUnder` is the account's erasure count the caller read before it read the * entry ({@link readClearanceErasureCount}). Every erasure deletes its entries first and adds one to the count under the * lock it marks their rows under, so the update that appends this row sees one of two things: the count it judged under — * any erasure of the entry is still to come, reads this row and takes its custody — or a larger count, an erasure that may * have taken the entry after it was judged and read no row of it: the open is refused (`memory.origin_clear_conflict`, a * re-call re-judges) and the object it put goes with the refusal. */ export declare function openOriginClearance(store: OriginClearanceStore, row: Omit<OriginClearanceRow, "status" | "events" | "custodyErased">, judgedUnder: number): Promise<void>; /** * Record that THIS clearance's tombstone leg committed (called between the delete's success and * the re-record attempt). The record is what lets a later resume read an ABSENT entry as "the * known crash window — replay the add from custody" instead of guessing: absence alone is * un-attributable (an independent, deliberate deletion through the ordinary tombstone lane leaves * the identical committed state). Idempotent (first write wins); non-pending rows are a no-op * (a settled row's history never rewrites). */ export declare function markOriginClearanceTombstoned(store: OriginClearanceStore, input: { clearanceId: string; now: () => number; }): Promise<void>; /** * Terminal event append + status flip. Unknown clearanceId is a corrupt-caller refusal. * * SINGLE-WINNER terminal: a row already terminal ignores a late contradictory * settlement — two concurrent resumers race the store legs (the store's own CAS decides those), * and the loser's stale verdict must not overwrite the winner's audit state (a successfully * cleared entry reading `failed` with contradictory events). The loser's own thrown refusal is * its loud channel; a re-call then answers off the CURRENT committed state. * * `keepPending: true` appends the event WITHOUT flipping the status: the resume-seat form for a * refused re-record leg — the tombstone has committed and the custody row is the ONLY copy of the * bytes, so the row must stay resumable (a `failed` flip would strand them: the resume arm only * completes pending rows). */ export declare function settleOriginClearance(store: OriginClearanceStore, input: { clearanceId: string; to: "done" | "failed"; requestId: string; eventId: string; now: () => number; detail?: string; keepPending?: boolean; }): Promise<void>; /** Whether an erasure has taken this clearance's custody (a row that is not there reads as taken: nothing stands to * resume from). The re-record legs ask it right before they add the entry back from the custody they read earlier. */ export declare function clearanceCustodyErased(store: Pick<MemoryGovernanceDocs, "readDoc">, clearanceId: string): Promise<boolean>; /** What one erasure did to the clearances' custody (the attestation's `clearanceCustody` carrier class): `removed` — the * custody texts of the erased entries this call took out of the store: each row this call marked (an inline text dropped * from its row; a kept text whose object is gone after this call's delete — deleted by it, or already by an update of the * account that read the mark first, {@link clearanceUpdate}), and each object a replay found still standing under a row * marked before and deleted; `remaining` — the ones it could not delete. */ export interface ClearanceCustodyErasure { removed: number; remaining: number; } /** * #3463 — erase the custody of every clearance of an erased entry (`entryIds`). The rows go first: one update marks each * of them `custodyErased` at `at` (`custodyErasedAt`; an inline text and the digest are dropped with it), so from then on no read answers the text * and no resume acts on it; then each one's custody object is deleted. A row already marked is left as it is (a replay * converges: its object is deleted again when one still stands, and a row is never rewritten twice). The row itself — * who cleared, why, when, its events, its status — stays. A failed delete is counted in `remaining`, never swallowed; a * document that cannot be read or updated throws (the lane that called decides what its attestation says about that). * The same update adds one to the account's erasure count — the store's one count of the erase door's runs — a row of * these entries standing or not, and writes the account when it was never written (#3559, {@link openOriginClearance}): * every lane deletes the entries before it calls this, so a reader that read the count before an entry (a clearance * judging it, a memory mount listing it) and acts after this update sees the count moved. */ export declare function eraseOriginClearanceCustody(store: OriginClearanceStore, entryIds: ReadonlySet<string>, at: number): Promise<ClearanceCustodyErasure>;