UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

155 lines (154 loc) • 14 kB
/** * A checkpoint row's generation and the judges of a resolve's expectation over it (the generation, the claim holder). Every * backend, the resume's claim and a host that compares two reads of one row read the generation here, so "an absent `rev` * reads as 0" is written once. * A leaf: it types against `checkpoint-store.ts` (type only), so the store module can call it without a load-order cycle. */ import type { Checkpoint, ResolveExpectation } from "./checkpoint-store.js"; /** * THE generation of a checkpoint row: its monotonic {@link Checkpoint.rev}, where an absent `rev` (a row * minted before the field existed, never mutated) reads as `0`. This is the one place that rule is * written — the CAS predicate under it, the loss answer, both backends' resolve / reopen bump, and the * resume's expectation all read the generation through it, and so should a backend or a host that * compares two reads of one row (`checkpointRevOf(again) === checkpointRevOf(row)` ⇒ no resolve, reopen, identity re-key or queue append * landed between them). */ export declare function checkpointRevOf(cp: Pick<Checkpoint, "rev">): number; /** * REF-A4 — THE optimistic-concurrency half of `resolve`'s CAS predicate (design/80 D-1), shared by every * backend. The caller validated its reopen-by-reason guard against a `get()` snapshot; if a concurrent * resolve/reopen cycle, an identity re-key or a queue append moved the monotonic `rev` in the get→resolve window, that validation is stale ⇒ * lose the CAS (fail-closed), forcing a re-get + re-validate. Keyed on `rev`, NOT on `reopenReason` — a * 2-valued enum can ABA back to a prior value while the winner changed (round-2 BLOCKER fix). An absent * expectation opts out of the guard entirely. */ export declare function checkpointOccMatches(cp: Checkpoint, expect?: Pick<ResolveExpectation, "rev">): boolean; /** * The generation a WON resolve gives the row it consumed: one above the generation the resolver read ({@link checkpointRevOf}), because * every won resolve and every reopen bumps the counter by exactly one. It is the claimer's generation — the one value its compensation * (`CheckpointStore.reopen`, `expect.rev`) carries so that only the claim that consumed the row can give it back: a delayed or foreign * reopen finds a later generation (another resumer reopened and claimed again) and loses, instead of un-consuming a newer claim. */ export declare function checkpointClaimRevOf(read: Pick<Checkpoint, "rev">): number; /** * The deployment's judge of a grant holder, handed to an in-tree store (`holderIsCurrent`) — the ONE judge every write that carries a * holder is judged by (a session append or leaf move, a card's `put`, steer, `expire` and `resolve`). * * THE SENTENCE: `true` exactly when the grant `holder` names is the run's CURRENT leg AND that run has not ended (it is running or * parked); `false` otherwise. A run row whose status is a word outside the host's own live set (a status the judge does not know) is * NOT held: the judge answers `false` — fail-closed, never read as live. A value the judge cannot read as a grant is not an answer: * the judge throws (a host's judge over `decodeGrantHolder` throws `holder.malformed`), and the write throws with nothing written. * * Exactly `true` is current; any answer that is not a boolean — a promise from an async judge included — is refused loudly * ({@link holderJudgedCurrent}), never read by its truthiness and never as a lost grant. * * WHEN A HOST ENDS A RUN: a run that finishes normally is marked ended only once its settled result is in the host's hands — the * leg writes its last records after it reports its end (`task.end`), and a judge reading that report as "ended" refuses the leg's * own tail. Cancelling or superseding a run may withdraw its grant earlier; the writes refused then are the ones meant to be refused. * * SYNCHRONOUS — the store calls it inside its atomic step, right before the commit, so the judgement and the write are one step with * respect to code in THIS process. That closes the race only when the deployment's grant transfers happen in this process and * synchronously (an in-process grant table); a grant another process can transfer needs a store whose write and the grant table * share one transaction (a SQL backend: the write's condition and a locked read of the run row, in one transaction). * "One step" is with respect to OTHER writers and to the grant table's own transfers: the values the caller hands the write (the row, the * entry, the outcome, the options) are the caller's, read by the store as it writes them — a caller whose own objects run code when read * (an accessor that moves its own grant) is outside what the fence promises. */ export type HolderJudge = (holder: string) => boolean; /** THE reading of a store's `holderIsCurrent` option (`subject` names the option): absent, or a function — anything else is refused * when the store is built (`config.invalid_argument`), never read as "no judge" and never left to fail at the first write. */ export declare function readHolderJudge(subject: string, value: unknown): HolderJudge | undefined; /** * THE rule of a judge over a SHARED authority (one log, one ledger, one in-process view several store instances join): the judge * belongs to the authority, not to the instance that happened to build it — every instance that joins must bring the SAME judge (the * same function object, or none beside none), else the join is refused with the store kind's code, naming both sides. `authority` is * the shared object; `judges` the kind's table of the judge each authority was built with (weak: it dies with the authority). * A deployment whose judge is built after its stores hands every store one trampoline function and wires its body later. */ export declare function joinHolderJudge(judges: WeakMap<object, HolderJudge | null>, authority: object, judge: HolderJudge | undefined, kind: HolderStoreKind, what: string): void; /** The store kinds that judge a holder, each refused at its own seat with its own code. */ export type HolderStoreKind = "checkpoint" | "session"; /** * THE judgement of one write's holder, shared by every store that judges holders through a function (both session storages, both * checkpoint stores, on every verb that carries one): no holder ⇒ nothing to judge (`true` — the write is judged as before); a holder * and no judge ⇒ `false` (a store never ignores a precondition it was handed); otherwise the judge's answer, current only when it is * exactly `true`. An answer that is not a boolean throws the store's own seat code (`config.invalid_checkpoint_store` / * `config.invalid_session_store`) with nothing written: a wiring error is named as one, not passed as a grant (by truthiness) nor * reported as a superseded holder (by refusal). A judge that throws makes the write throw, nothing written. Called inside the store's * atomic step, BEFORE any other condition of the write (an expected leaf, a row's state) — a write that fails both is answered * `holder.superseded`. */ export declare function holderJudgedCurrent(holder: string | undefined, holderIsCurrent: HolderJudge | undefined, kind: HolderStoreKind): boolean; /** * The holder half of `resolve`'s CAS predicate ({@link ResolveExpectation.holder}): {@link holderJudgedCurrent} over `expect.holder` * for the checkpoint store. A miss is an ordinary CAS loss — the row is untouched. */ export declare function checkpointHolderMatches(expect: ResolveExpectation | undefined, holderIsCurrent: HolderJudge | undefined): boolean; /** * THE holder declaration of a store — one shape, one meaning, for the session store (`SessionStore.fence`, mirrored from * `SessionRepo.fence` by `TtlSessionStore`) and the checkpoint store (`CheckpointStore.fence`): `holder: "writes"` = this store judges * the holder of EVERY write it is handed that carries one, in that write's own atomic step, by the one sentence of {@link HolderJudge} * (a session store: append and leaf move; a checkpoint store: `put`, `setPendingSteer`, `expire`, `resolve` and each arm of * `claimTerminal`). `requireHolder: true` additionally tells the engine's run and verb doors to refuse a run or verb the HOST starts * without a holder (a leg the engine itself starts — a delegated child, a workflow member, an observer — is exempt); the store itself * never refuses a write for carrying none (a host's own maintenance writes carry none). A wrapper store forwards its inner store's * declaration member by member (a wrapper that drops it is refused at the door); a routing store declares only when every store it * routes to judges. */ export interface StoreHolderFence { readonly holder: "writes"; readonly requireHolder?: true; } /** * THE reading of a store's `fence` declaration ({@link StoreHolderFence}), for the store kind's seat: absent ⇒ `undefined` (the store * judges no holder); `{ holder: "writes" }` with an optional boolean `requireHolder` ⇒ the declaration, frozen (`requireHolder: false` * reads as absent). Every other form refuses with the seat's code, naming the seat: * - the retired `holder: true` (it promised `resolve` alone; a store that kept it judges no other write) — the sentence * names the law that replaced it; * - any other `holder`, a `requireHolder` that is not a boolean, a member outside the two, a declaration that is not an object or * cannot be read. */ export declare function readStoreFence(kind: HolderStoreKind, seat: string, store: unknown, member?: "fence" | "durableFence"): StoreHolderFence | undefined; /** Who started the run or verb a store seat's door judges: the host (a run it starts or resumes, or one of the out-of-run session * verbs), or an engine lane of a run (`engineLaneLegOf` — a delegated child, a workflow member, an observer sidecar). */ export type HolderSeatStarter = "host" | "engine"; /** * THE store-seat door of a grant holder — one function for both store kinds and for every entrance (a run's prepare, a resume's screen * before its claim, the out-of-run session verbs), each seat refused with its own code: * - the seat's `fence` is read ({@link readStoreFence} — a retired or malformed declaration refuses whether or not the run carries a * holder, since a declaration is a claim the engine reads at the seat); * - a run or verb that carries a holder over a store that does not declare `holder: "writes"` is refused — a holder the store would * ignore would fence nothing while the deployment believed it fenced; * - a store that declares `requireHolder: true` refuses a run or verb the HOST started without a holder (`startedBy: "host"`); a leg an * engine lane started (`"engine"`) carries no holder of its own and passes. * No store (a disarmed seat) has nothing to judge. Answers the declaration read. The holder's value is never named. */ export declare function assertStoreJudgesHolder(kind: HolderStoreKind, seat: string, store: unknown, holder: string | undefined, startedBy: HolderSeatStarter): StoreHolderFence | undefined; /** `RunInternals.holder` (and a tree holder, named by `seat`) when present: a non-empty string (the deployment's credential). A provided value that * is empty or not a string names no grant and is refused at the door (`config.invalid_argument`), never carried into a CAS where a * judge would read it as a grant that lost. The value itself is never printed — only its kind. */ export declare function assertHolderValue(holder: unknown, seat?: "RunInternals.holder" | "RunInternals.treeHolder" | "RunWorkflowOptions.treeHolder"): void; /** * THE session seat of a leg's write holder (`writeHolderOf`): {@link assertStoreJudgesHolder} over the store's `fence`, with one more * way through for a leg an ENGINE LANE started — a store that declares no `fence` but declares `durableFence` (the same one shape: * "every storage I hand out answers for itself whether it judges", a routing store's word) passes, and the storage it hands out is * asked at the acquire ({@link sessionStorageJudges}). A leg the host started is judged on `fence` alone. `durableFence` is read only * when the leg carries a write holder, the store declares no `fence` and an engine lane started the leg. */ export declare function assertSessionSeatJudgesHolder(store: unknown, holder: string | undefined, startedBy: HolderSeatStarter): StoreHolderFence | undefined; /** What a session storage says of itself (`SessionStorage.getResidence()`, optional, synchronous): `durability` "durable" (its writes * outlive the process) or "process-local"; `fence` exactly when every append and leaf move it is handed that carries a holder is judged * in that write's step. Asked only under a store's `durableFence`: a durable storage answers with a fence, or the leg is refused. The * engine's memory and file storages answer; `BaseSessionStorage` does not answer for a subclass. */ export interface SessionStorageResidence { readonly durability: import("./checkpoint-store.js").StoreDurability; readonly fence?: StoreHolderFence; } /** * Station 2 — does the storage a `durableFence` store handed out for an engine lane's leg judge the leg's write holder? `store`'s own * `fence` covers every storage it hands out (`true`: stamp, as the seat door already read). Otherwise the storage answers for itself * (`getResidence()`): a fence ⇒ `true`; `process-local` with none ⇒ `false` (the writes stay in this process, nothing to fence: not * stamped); anything else — no method, a method that throws, an answer that is not an object, a durability outside the two words, a * malformed fence, `durable` with none — refuses the leg loudly (`config.invalid_session_store`, the deployment's input), nothing written. */ export declare function sessionStorageJudges(store: unknown, storage: unknown): boolean;