@sema-agent/core
Version:
Stateless, task-oriented AI agent core
81 lines (80 loc) • 7.11 kB
TypeScript
import type { CheckpointStore } from "../checkpoint-store.js";
import type { AcquiredSession, SessionRowExpectation, SessionStore } from "../session.js";
/** What the Runner lends an out-of-run verb for the session's lock. */
export interface SessionLockSeats {
/** Whether a leg of this Runner holds or awaits `sessionId`'s lock, or a write one issued to it has not settled. */
sessionBusy(sessionId: string): boolean;
/** Take `sessionId`'s lock; called synchronously after `sessionBusy` answered false, so nothing slips in between. */
takeSessionLock(sessionId: string): Promise<() => void>;
}
/**
* The request door: the request must be a plain data object — not a Proxy (its traps are the caller's code, and could
* answer the caller's read of a member differently from this one), its prototype `Object.prototype` or none (so no
* member is inherited) — judged before any member is read; its members are then taken from ONE snapshot of its own
* data properties, so no code of the request itself (a trap, a getter) runs while it is read and nothing read can
* change what is read next (an accessor member is a bad request); own keys inside the verb's closed set. `bad`
* throws the verb's own refusal; the member types are the verb's to judge on the returned snapshot.
*/
export declare function readPlainRequest(request: unknown, keys: ReadonlySet<string>, bad: (what: string) => never): Record<string, unknown>;
/** A value as a refusal names it: a string clipped, a primitive spelled, anything else by its type. */
export declare function shownValue(v: unknown): string;
/**
* The idle claim: refused with `busy()` when a leg of this Runner is running or queued on the session (its state is
* not moved under it); otherwise the session's lock is claimed in this same synchronous step and held by the verb
* until it releases it, so a leg that arrives meanwhile waits for the verb.
*/
export declare function claimIdleSession(seats: SessionLockSeats, sessionId: string, busy: () => Error): Promise<() => void>;
/** What an out-of-run verb may expect of the session it names: the occupant a read of it answered — a
* {@link SessionRowExpectation} whose `row` is the row (the session exists) and that binds no log. */
export type OccupantExpectation = SessionRowExpectation & {
readonly row: NonNullable<SessionRowExpectation["row"]>;
readonly log?: undefined;
};
/** The forms of a row expectation a door does not take, each with the sentence that refuses it: `noRow` for `row: null` (the door's
* subject must exist), `log` for a log mark (the door replaces no log). A form left out is taken. */
export interface RowExpectationRefusals {
readonly noRow?: string;
readonly log?: string;
}
/**
* The strict reading of a caller's `expect` member ({@link SessionRowExpectation}) — what the bundled commit and the out-of-run verbs
* take: read like the request it sits in (one snapshot of a plain data object, own keys a closed set — the expectation, its row and
* its log mark each; a class instance, a member a prototype supplies, an accessor, a Proxy or a key the type has not is no
* expectation), then by the shape reading of a row expectation (`readRowExpectation`); and `row: null` (no row) goes with no log
* but the empty one's mark — a row that is not there holds no entries, so a mark of any is a contradiction, not a precondition to
* drop. A form the door does not take (`refuse`; absent ⇒ every form is taken) or a value of no shape is the door's bad request:
* `bad(what)` throws the door's own refusal, `what` naming the member from `expect` down; a `bad` that returns instead is answered
* by a `TypeError` saying so — a refusal is never read as "no precondition". Absent ⇒ `undefined`. A store of its own that judges a
* commit's `expect` reads it through this before `sessionRowMatches`, so what it refuses as a bad argument is what the bundled
* commit refuses (the judge called directly still reads members, not data).
*/
export declare function readRowExpectationMember(raw: unknown, bad: (what: string) => never, refuse?: RowExpectationRefusals): SessionRowExpectation | undefined;
/** An out-of-run verb's `expect` member: {@link readRowExpectationMember} taking only the row — `row: null` names a
* session that is not there (this verb's session must exist) and `log` binds a log this verb does not replace. */
export declare function readOccupantExpectation(raw: unknown, bad: (what: string) => never): OccupantExpectation | undefined;
/** The session must exist — the same door and code a run with `requireExistingSession` meets. `verb` prefixes the
* message, `nothingDone` says what the verb did not do. With `expect`, the session the acquire answered must also be
* the row the caller read (`sessionRowMatches` on its owner and occupant cell); another row under the id is not the
* session the caller meant, so it is refused with the same code — nothing is done to it. */
export declare function acquireExistingSession(sessions: SessionStore, sessionId: string, verb: string, nothingDone: string, expect?: OccupantExpectation): Promise<AcquiredSession>;
/** A parked-session refusal's words: the verb that refuses, its code, and what a parked session's resume would undo of the verb's write
* (the sentence's second half, ending in what nothing was done and the way out). */
export interface ParkedSessionWords {
readonly verb: string;
readonly code: string;
readonly undone: string;
}
/**
* A session durably parked at a checkpoint is not written by an out-of-run verb whose write its resume would undo: the resume puts the
* branch's leaf back at the checkpoint's leaf (a summary written now lands off the branch the conversation continues on; a rewind written
* now is taken back with the cut turns). "Parked" is a pending row of the session in the checkpoint store the request's seat resolved to —
* the store the request names, else the deployment's, the one reading the Runner's resume makes of its config's seat — so a session whose
* run parked in its own store is asked there; no store (none wired, or the seat says `"disabled"`), no durable park. A store that cannot
* answer the question — it carries no `listBySession`, that read fails, or the answer is no list of rows (or its first row is no row with a
* token) — counts as one that answered "parked": the verb does not report a write it cannot promise the next run reads. `whose` is how the
* refusal's sentence names the store (the request's, or the deployment's). The refusal names the pending row by its non-secret
* `checkpointId` (the store's text, quoted as untrusted) and never by its token: the token is the resume capability and goes in no
* sentence. A row that carries no id (an earlier engine's, or one the store cannot read) is named "a checkpoint"; a host finds it by the
* session's id, which the request holds.
*/
export declare function refuseParkedSession(store: CheckpointStore | undefined, sessionId: string, whose: string, words: ParkedSessionWords): Promise<void>;