@sema-agent/core
Version:
Stateless, task-oriented AI agent core
158 lines (157 loc) • 13.9 kB
TypeScript
import { MemoryEngine, type SessionMemoryStatus } from "./engine.js";
import { type MemoryPartitionIncidentSink, type SessionCaptureRecordStore, type SessionIdentityRead } from "./layout.js";
import { type EraseMemoryEntriesInput, type MemoryErasureAttestation } from "./file-backend.js";
import type { MemoryBackend } from "./types.js";
/** The directory (under the engine root) whose transaction lock serializes a deployment's erasures — the
* judgment across stores and the executions it admits run as one critical section. */
export declare const MEMORY_DEPLOYMENT_ERASURE_LOCK_DIR = ".memory-erasure-judge";
export interface MemoryDeploymentInput {
/** The deployment's backend — the instance given as `RunnerDeps.memoryBackend`. It must publish its own control
* plane (`controlPlaneRoot`): the scope registry and the capture records a session's memory status reads live
* there, and a backend without one keeps them in per-project control planes no deployment-wide reading can name —
* refused (`config.memory_scope_key`). This is the deployment's own question, not consolidation's
* (`memoryGovernanceDurability`). A backend that carries only `governance` stays refused here: its session accounts
* and capture records ride its store (#3484), its scope registry does not. */
backend: MemoryBackend;
/** The value given as `RunnerDeps.memoryEngineDir` — a required key, judged as `resolveMemoryScopeStore`
* judges it (`undefined` written out reads the Runner's same fallback). */
memoryEngineDir: string | undefined;
/** The scope contract the deployment's sessions are minted under (`TaskSpec.memory.scopeContract`) — the
* reading of which plane a request's scope keys belong to. A required key, judged as `resolveMemoryScopeStore`
* judges it. */
scopeContract: "v2" | undefined;
/** Advisory incident seat for every engine and store this face opens (`MemoryEngineOptions.onIncident`). */
onIncident?: MemoryPartitionIncidentSink;
/** The host's capture record carrier (`RunnerDeps.memoryCaptureRecordStore`), instantiated per store and
* keyed by that store's control plane — the seat sessions read. Absent ⇒ each engine's file default. */
memoryCaptureRecordStore?: (plane: {
controlDir: string;
}) => SessionCaptureRecordStore;
}
/** One store of the deployment and the engine this face drives it with. */
export interface MemoryDeploymentStore {
readonly plane: "project" | "personal";
readonly backend: MemoryBackend;
readonly engine: MemoryEngine;
}
/** The keys of {@link SessionMemoryStatus} that are facts of the SESSION and fold across stores. */
type FoldedStatusKey = "captureOptedOut" | "committedCount" | "foldedCount" | "optOutSource" | "lastCaptureAt";
/**
* A session's memory status across every store of the deployment ({@link MemoryDeployment.sessionMemoryStatus}):
* the session keys folded by {@link foldSessionMemoryStatuses}, the session's write home, plus each store's own answer.
*/
export interface DeploymentSessionMemoryStatus extends Pick<SessionMemoryStatus, FoldedStatusKey> {
/** The directory of the session's write home: the `writeHome` of the one `planes[]` row {@link chooseSessionWriteHome}
* names, handed back unchanged — its contract states when the key is absent. The path is a directory on the engine's
* disk: a host decides whether it is the caller's to see. */
writeHome?: string;
/** Always present: one row per store of the deployment, in store order (project plane first) — each store's
* answer exactly as its engine gave it. The folded keys are read from these rows; a store whose answer made a
* folded key absent is the row whose key is absent; a store-bound key (`writeHome`, the directory of the
* session's write home IN that store) is answered here as that store's own value, and the session-level
* `writeHome` above is one of them. A store that could not be opened answers
* `{ optOutSource: "fault" }` (every source unreadable). With the host's capture record carrier
* (`memoryCaptureRecordStore`) the personal plane of a directory-pinned deployment also has a row while its
* store is not in the set, whenever its engine answers a capture fact for the session — a standing record or a
* fault (`optOutSource` present): that carrier keeps the record off the mount, where the set cannot see it. */
planes: ReadonlyArray<{
readonly plane: "project" | "personal";
readonly status: SessionMemoryStatus;
}>;
}
export interface MemoryDeployment {
/**
* The deployment's stores, read from the disk at each call: the project plane (the deployment's backend), then —
* on a directory-pinned deployment — the personal plane's store when a write left a record on its mount (the
* store opened on first use and kept as an engine instance, never as a reason to stay in the set).
*
* @contract memory.deployment.store_set — on a directory-pinned deployment the personal plane's store is in the
* set exactly when its mount holds a record: a file under `<engine root>/personal/.engine` or
* `<engine root>/personal/memory` other than the named artifacts an open or a read leaves, matched at that place
* and by that name only (`*` = any rest of the name): control plane top level, files `root-anchor.json` ·
* `scopes.json` · `scopes.json.journal` · `store-identity.json` · `.tmp-*` · `.link-probe-*`; control plane top level, directories as whole trees
* `root-anchor.json.lock` · `scopes.json.lock` · `txn.lock` · `root-anchor.json.lock.stale-*` ·
* `scopes.json.lock.stale-*` · `txn.lock.stale-*`; memory directory top level, files `.casefold-probe-*`. A
* directory that cannot be read, for any reason but absence (a link that leads nowhere, at it or above it, is no
* absence), counts as holding one, and so does an anchor, a scope registry, a registry journal or a store identity that
* stands there damaged (a link, no regular file, unreadable, not JSON or out of shape). It stays in the set, too, while this
* process saw it hold a record and its control plane no longer holds its root anchor or its account (moved away, unmounted:
* `memory.store_absent`). Opening, reading (an export that
* mints the store's identity included) and a refused request add no store — so does the session status read with a host
* capture carrier, which opens the personal store and leaves its anchor and empty directories, unless the deployment's own
* store is expected and absent (then nothing is opened or made) — and every deployment
* object over one engine root answers the same. A store in the set that cannot be
* opened — the damaged anchor, scope registry, registry journal or store identity that put it there is one such cause, its absence
* another: the census and the open read the same fault — is not a store of the list: the call throws (`ControlPlaneCorruptError`; a
* bare system error is carried by one that names its errno, the original as its `cause`; an absent one, `memory.store_absent`), and
* the session status answers that plane `optOutSource: "fault"`.
*/
stores(): readonly MemoryDeploymentStore[];
/**
* Erase across the deployment — the input and the receipt of `MemoryEngine.eraseMemoryEntries`, judged as one
* request in the whole deployment. The deployment's erasures run one at a time (a transaction lock under the
* engine root, renewed while the stores execute; a lock found lost before a store executes or after the last
* one throws `ControlPlaneCorruptError`, facet `lock-ownership`, instead of a receipt; a deployment's own File store that is
* expected and absent refuses `memory.store_absent` before that lock is taken — nothing erased, nothing made — and so does the
* personal store when the request reaches it, or when the engine root itself is gone; a personal store the request does not reach,
* under a root that stands, is not asked); under it the stores
* are read once, and the reach, the judgment and the executions read that one census. The stores a request
* reaches:
* - a `{ scope }` selector ⇒ the store carrying that scope (opened if it does not exist yet);
* - with `expectedScopes` ⇒ the store carrying the granted scopes (the project store for an empty grant); a
* grant naming scopes kept in two stores is refused (`config.memory_erasure_scope`) before anything is
* deleted or recorded — one request is judged in one store's lock;
* - an `{ ids }` or `{ sessionId }` selector without a grant ⇒ every store of the deployment (each resolves
* its own share).
* Before any store executes, every store is asked for an anchor of the same requestId (all but a personal store this process saw that
* is gone and the request does not reach: it cannot be opened, and the request erases nothing there): one recorded under a
* different authorization set refuses (`config.memory_erasure_scope`); one with a different selector, or in a
* store this request does not reach, refuses `memory.erasure_selector_mismatch` — nothing is deleted or
* recorded. A store holding this request's own anchor replays it. The reached stores then execute in store
* order, and their receipts merge into one (`mergeErasureAttestations`: `complete` only when every store's
* receipt is; one store ⇒ its receipt as given). A
* failure in a later store throws after an earlier store executed: that store keeps its anchor and a replay
* of the same requestId converges every store. The thrown error is the refusing store's own (class, code and cause
* unchanged); its message opens with the sentence that says which stores had executed and how many entries they had
* erased, and what follows is that store's own sentence about itself — so "nothing was deleted" in it is never read
* as the request's. A refusal in the first store executed nothing and says only its own sentence.
*
* @contract memory.erasure.request_per_deployment — an erasure requestId names one request across the stores of the
* deployment that can be opened: its record is the anchor of the store that executed it, every store that can be opened is read
* for that anchor before any store executes, and a requestId first executed with another selector or in a store the request does
* not reach is refused `memory.erasure_selector_mismatch` with nothing deleted. A personal store this process saw hold content
* that is gone cannot be read: a request that reaches it is refused `memory.store_absent`; one that does not is judged by the
* stores that can be opened, so while it is gone a requestId it executed can execute again in another store under another
* selector — and once it is back, a replay of that requestId with either selector is refused `memory.erasure_selector_mismatch`
* (its two records do not converge; a new requestId runs). An erasure that bypasses this face (a store's own engine) is judged
* by that store alone.
*/
eraseMemoryEntries(input: EraseMemoryEntriesInput): Promise<MemoryErasureAttestation>;
/** A session's memory status across every store of the deployment ({@link DeploymentSessionMemoryStatus}).
* Never throws — an unreadable source is an absent key, as on one store. `identity` names the occupant of the id the status
* is read for, as a capture record carrier's `read` takes it, and every store reads under it (`MemoryEngine.sessionMemoryStatus`);
* absent ⇒ every record binds by id — a recycled id then answers the earlier occupant's records too. */
sessionMemoryStatus(sessionId: string, identity?: SessionIdentityRead): Promise<DeploymentSessionMemoryStatus>;
}
/**
* Fold the stores' answers for one session into the session's status — the deployment-wide reading's ONE law
* (one store ⇒ its five keys unchanged). Each store's answer comes from its engine; nothing is coined:
* - `committedCount` / `foldedCount`: the stores' counts added; any store's count absent (its ledger
* unreadable) ⇒ absent;
* - `lastCaptureAt`: the latest of the stores'; absent when the folded `committedCount` is absent (a store's
* ledger unreadable may hide a later capture) or no store has a capture;
* - `captureOptedOut`: `true` (with `optOutSource: "record"`) when any store holds a standing record — a
* one-way record anywhere is the session's record; `false` only when every store answers `false`; otherwise
* (a store's record read faulted, no store holds a record) absent, with `optOutSource: "fault"`.
*
* @contract memory.deployment_status.fold — a session's memory status across a deployment's stores is folded
* by this function only: counts add (any store's count absent ⇒ absent), `lastCaptureAt` is the latest
* (absent when a count is absent or no store has a capture), `captureOptedOut` is `true` when any store holds a
* record and `false` only when every store answers `false`, else absent with `optOutSource: "fault"`; a host
* that shows the deployment's answer projects it and folds nothing itself.
*/
export declare function foldSessionMemoryStatuses(statuses: readonly SessionMemoryStatus[]): Pick<SessionMemoryStatus, FoldedStatusKey>;
/** The deployment-wide memory face over `input` (see {@link MemoryDeployment}). Input keys are judged as
* `resolveMemoryScopeStore` judges them; a backend whose control-plane state is replica-local is refused. */
export declare function createMemoryDeployment(input: MemoryDeploymentInput): MemoryDeployment;
export {};