@sema-agent/core
Version:
Stateless, task-oriented AI agent core
85 lines (84 loc) • 6.59 kB
TypeScript
import { type LeafEntry, type SessionLogMark, type SessionMetadata, type SessionStorage, type SessionTreeEntry, type SessionWriteOptions } from "../harness/types.js";
import { type ParentLinks, type ReplayLoss } from "./path-to-root.js";
import { type HolderJudge } from "../../core/checkpoint-generation.js";
/** How a session log's entries link to their parents — one reading for every walk over a storage's entries: an empty parent id reads as "no
* parent" here, as it always has (#2244 tracks reading it as a refusal). */
export declare function entryParentLinks(byId: {
get(id: string): SessionTreeEntry | undefined;
}): ParentLinks<SessionTreeEntry>;
/** Return the effective branch leaf after applying a session tree entry. */
export declare function leafIdAfterEntry(entry: SessionTreeEntry): string | null;
/** A log's {@link SessionLogMark}: its length and its last entry's id — the one reading of the mark, for the reader that marks a log it
* read and for the repo that judges the log it is about to write over. */
export declare function sessionLogMarkOf(entries: readonly SessionTreeEntry[]): SessionLogMark;
/** The leaf a log leaves its session on: the leaf after its last entry (`null` for an empty log) — what a storage replaying the log
* answers, and so what a repo answers as the head of a row whose log it has just written or read whole. */
export declare function resolveLeafId(entries: readonly SessionTreeEntry[]): string | null;
/** Was this storage built with a grant judge ({@link storageJudgeOf}: a judge ⇒ `true`, `null` ⇒ `false`, an unreadable slot throws)? The
* engine's own concrete storages answer their `getResidence` by it; the base class answers no residence itself (only the subclass knows
* where it lives). Not a root export. */
export declare function storageBuiltWithJudge(storage: object): boolean;
export declare abstract class BaseSessionStorage<TMetadata extends SessionMetadata = SessionMetadata> implements SessionStorage<TMetadata> {
private metadata;
private readonly entries;
private readonly byId;
private readonly labelsById;
private leafId;
private readonly floorId;
private readonly replayLoss;
/** The deployment's grant judge this storage's writes are judged by (`opts.holderIsCurrent`); `null` when built with none ⇒ a write carrying a
* holder is refused. Read only through {@link storageJudgeOf}. */
private readonly holderIsCurrent;
/**
* The storage of one loaded log. Its leaf (`leafId`, the leaf after the log's last entry unless the backend states its head) and its floor
* (`opts.floorEntryId`) must name entries the log holds: a load that does not is a stored log that does not hold together, refused
* `corrupt_session`. `opts.replayLoss` = what the replay that read the log dropped (absent = it dropped nothing): the refusals of this storage
* state it, and a walk that breaks at a parent the log does not hold carries it ({@link LineageBreak}). `opts.holderIsCurrent` = the
* deployment's grant judge every write carrying a holder is judged by ({@link assertExpectedLeaf}).
*/
protected constructor(metadata: TMetadata, entries: SessionTreeEntry[], leafId?: string | null, opts?: {
floorEntryId?: string | null;
replayLoss?: ReplayLoss;
holderIsCurrent?: HolderJudge;
});
/** ` (N of M records skipped as corrupt)` when the replay that read this log dropped records, else empty. */
private lostText;
/** The log's leaf must name an entry the log holds — at load, and after a leaf entry a write appended (the same stored fact either way). */
private assertLeafHeld;
/** The bounded-window root this storage was woken with, or null for a full-tree load (F3). */
getFloorId(): string | null;
/** What the replay that read this log dropped (`undefined` = it dropped nothing): the account a run that continues the session gives the person. */
getReplayLoss(): ReplayLoss | undefined;
getMetadata(): Promise<TMetadata>;
getLeafId(): Promise<string | null>;
/** The log's mark ({@link sessionLogMarkOf}) as it stands at this instant, read synchronously: for a repo judging a row and writing it in
* one step with no await between them (an await would let a write through this storage land between the judgement and the write). */
logMarkNow(): SessionLogMark;
/** The active leaf at this instant, read synchronously (the head a repo answers from the same step as the write it judged). */
leafNow(): string | null;
/** The repo's owner write of the row this storage holds ({@link SessionRepo.recordOwner}): the metadata every holder of this storage reads
* from now on, swapped in one synchronous step — the log, the leaf and every handle on this storage stay. Only the repo that holds the row
* calls it, inside the step that judged the row. */
protected adoptRecordedOwner(metadata: TMetadata): void;
protected createLeafEntry(leafId: string | null): LeafEntry;
createEntryId(): Promise<string>;
/**
* THE write's conditions, one synchronous judge point, in this order: the grant holder ({@link SessionWriteOptions.holder}, judged by
* this storage's `holderIsCurrent` — {@link holderJudgedCurrent}; not current ⇒ `holder.superseded`, whatever the leaf), then the
* optimistic lock (F2: `opts.expectedLeafId` provided and not the current in-memory leaf ⇒ `SessionError("conflict")`). Concrete
* `appendEntry`/`setLeafId` implementations call this before persisting, with no await between it and the write; durable backends
* should instead perform the same checks atomically against shared state (a conditional write in one transaction) so concurrent
* instances can't fork. `what` names the write in the refusal.
*/
protected assertExpectedLeaf(opts?: SessionWriteOptions, what?: "appendEntry" | "setLeafId"): void;
protected recordEntry(entry: SessionTreeEntry): void;
getEntry(id: string): Promise<SessionTreeEntry | undefined>;
findEntries<TType extends SessionTreeEntry["type"]>(type: TType): Promise<Array<Extract<SessionTreeEntry, {
type: TType;
}>>>;
getLabel(id: string): Promise<string | undefined>;
getPathToRoot(leafId: string | null): Promise<SessionTreeEntry[]>;
getEntries(): Promise<SessionTreeEntry[]>;
abstract setLeafId(leafId: string | null, opts?: SessionWriteOptions): Promise<void>;
abstract appendEntry(entry: SessionTreeEntry, opts?: SessionWriteOptions): Promise<void>;
}