UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

97 lines (96 loc) • 9.52 kB
import { type SessionStore } from "../session.js"; import { type ContractAssertionRunner } from "./contract-harness.js"; import { type ContractGrants } from "./grant-fixture.js"; /** * The cross-backend contract for {@link SessionStore} as the interface states it TODAY — what a store must * do so that the engine, a deployment's authorizer and a shell read the same answers off it whether the * store is the bundled `TtlSessionStore` (over an in-memory or file repo) or a backend that implements the * interface directly (a SQL store). vitest-free (see `contract-harness.ts`); every case builds a FRESH store * through `make()` and disposes it. * * Each case pins one sentence of the interface (`core/session.ts`) or of the session-tree storage it hands * out (`SessionStorage`, whose path / leaf / write-CAS rules every store's sessions carry). Optional faces * are judged only when present; a group whose face is absent passes WITHOUT asserting and says so through * `onOptionalMember` — a case name cannot carry "absent" (the default runner drops names, and a green * absent run is byte-identical to a green verified one), so a consumer that relies on a face asserts the * report, never the green. * * What the kit cannot see from outside, it does not claim: idle eviction and `pin` (the interface has no * clock seam; a binding that gives one — `ageIdle` / `idleClockOf` — has the `[readRow]` clause judge what a read does to it), what `release` does to an ordinary session's live set (`size` is best-effort and a durable * `list` may still show the history), a durable backend's crash recovery, and a delete that fails half-way. * * Semantics since @sema-agent/core 7.56.0: the list reads the store's rows (a reopened store lists them), pages by the * `(lastActiveAt, sessionId)` keyset, filters by the row's owner, and refuses a bad query through `readSessionListQuery`. * Since 7.57.0: the write faces — `logTransfer` (one write law: a commit lands only on the committer's row, keeps an existing row's * occupant, refuses a placed row, passes every entry through the import gate, writes nothing on any refusal) and the owner-guarded * `delete` — and a cached view is served only while it holds the row's leaf as well as its occupant. * Since 7.70.0: a `logTransfer` commit naming the row its committer judged (`expect`, a row expectation: the head its gate read, `null` * for none, and the log's mark when it binds) lands only on that row — a miss refuses it `conflict` with nothing written, before `decide` * — and a commit naming none lands as before; what is a bad `expect` is what the root's strict reading of it (`readRowExpectationMember`) * refuses, and a row written before occupant tokens (the `tokenlessRow` hook, owed by a durable store) is named by its head as it came. * Also since 7.70.0: a stored prompt-epoch record that does not read is kept as written and changes nothing about the pin a reopened * session answers — a store that wakes a bounded window answers `getEpochAnchor` with the nearest record below its floor that reads * (`walkPromptEpochCarriers`). Whether a bounded store answers below its floor at all is not judged here. * Since 7.71.0: `recordOwner` (reported as its own member, judged on a store with the ownership pair) — the claim of a row that records * no owner records the claimant's name in one step only on the occupant it names, leaves a row that records a name as it is, gives one * owner to two concurrent claims, refuses a name that is not one, and leaves every session handed out before it writing to the row. * Since 7.73.0: `readRow` (reported as its own member) — the read of a row writes nothing, sweeps nothing and is no activity (the last two * judged through the binding's idle clock, reported `idleClock`), and an id with no row resolves `undefined` while a row that cannot be * read rejects `corrupt_session` (the `unreadableRow` hook, reported as its own member). */ export interface SessionStoreContractHooks { /** Delete `sessionId`'s session ROW through the backend's own deletion path (history and all), so that a later * `acquire(sessionId)` creates a new session under the same id. */ deleteSession: (store: SessionStore, sessionId: string) => Promise<void>; /** A SECOND, independent store instance over the same rows as `store` — what a restarted process * constructs. After calling it the kit no longer uses `store`, so a backend that holds an exclusive lock * may release `store`'s first (the kit still disposes `store` at the end of the case: that dispose must * then be harmless). The kit disposes the returned instance. */ reopen: (store: SessionStore) => SessionStore | Promise<SessionStore>; /** A second LIVE instance over the same rows, used CONCURRENTLY with `store` — what another replica * constructs. Absent ⇒ the replica case is reported absent (a backend that is single-process by * contract has no replica). The kit disposes it. */ replica?: (store: SessionStore) => SessionStore; /** Write a row under `sessionId` (an id with no row) as this store's rows were written before occupant tokens were minted: a row whose * occupant cell is empty (on a SQL store, its column NULL), any owner, any log — the row a deployment's stock still holds from then. * A store declaring `durability: "durable"` gives it or says why it holds no such row (`noTokenlessRows`): neither ⇒ the cases that * need such a row fail naming both. On another store, absent ⇒ those cases are reported absent. */ tokenlessRow?: (store: SessionStore, sessionId: string) => Promise<void>; /** Write a row under `sessionId` (an id with no row) owned by `owner` whose head reads and whose log the store's open refuses * `corrupt_session` — a leaf that names an entry the log does not hold, or an entry the store cannot read — the row a damaged log * leaves. Absent ⇒ the case that deletes such a row is reported absent. */ openRefusedRow?: (store: SessionStore, sessionId: string, owner: string) => Promise<void>; /** Write a row under `sessionId` (an id with no row) whose header the store cannot read — the row's first record is not its header — * so its row read ({@link SessionStore.readRow}) must reject. Absent ⇒ the failed-read case is reported absent (`unreadableRow`). */ unreadableRow?: (store: SessionStore, sessionId: string) => Promise<void>; /** The binding's seam onto an idle-evicting store's clock, given with {@link idleClockOf}: put `sessionId` past the store's idle window, * so the store's next sweep evicts it (and, under a deleting policy, deletes its row). Both absent ⇒ the `[readRow]` cases that judge * sweeping and activity are reported absent (`idleClock`); one without the other is refused. */ ageIdle?: (store: SessionStore, sessionId: string) => void | Promise<void>; /** The idle clock the store holds for `sessionId` (any number that a read of the session would move), or `undefined` when the store * holds the session in no idle set (never acquired here, evicted, deleted). Given with {@link ageIdle}. */ idleClockOf?: (store: SessionStore, sessionId: string) => number | undefined | Promise<number | undefined>; /** Instead of `tokenlessRow`: why this store holds no row written before occupant tokens (a non-empty string — its occupant cell was * never empty, say how); the cases that need such a row are reported absent. Given beside `tokenlessRow`, refused. */ noTokenlessRows?: string; /** * The holder binding (the S-H cells), given together with {@link grants}: a FRESH store AS THE DEPLOYMENT COMPOSES IT — every wrapper * included (a cache, a routing layer) — built with the judge `grants` mints its holders against. Bind the composition itself: a cell * run on an inner store cannot see a wrapper, and a wrapper is what silently drops `SessionWriteOptions.holder` (or answers a row's * occupant before the holder). {@link reopen} must accept the stores it returns. Required (both) for a binding whose `make()` store * declares `fence`; absent with no `fence` ⇒ the holder cells are reported absent (`holder`). */ composed?: () => SessionStore; /** The grant fixture of {@link composed} ({@link ContractGrants}: current / replaced / ended / unknownStatus / malformed). */ grants?: ContractGrants; runAssertion?: ContractAssertionRunner; /** Optional faces (plus the replica and tokenlessRow hooks, and `requireExisting` — absent = the store rejects the option, its other * legal mode) a run verified or found absent; reported per judging case, after its assertions held. */ onOptionalMember?: (report: { member: "requireExisting" | "forget" | "fork" | "list" | "noteTaskRun" | "listPlaced" | "placements" | "headOf" | "ownership" | "replica" | "tokenlessRow" | "openRefusedRow" | "logTransfer" | "delete" | "deleteUnreadable" | "holder" | "recordOwner" | "readRow" | "idleClock" | "unreadableRow"; status: "verified" | "absent"; }) => void; } /** A hooks record the kit cannot run with is refused SYNCHRONOUSLY, before any case is registered, so a * `void sessionStoreContract(…)` registration fails at collection instead of as an unhandled rejection. */ export declare function sessionStoreContract(make: () => SessionStore, hooks: SessionStoreContractHooks): Promise<void>;