@sema-agent/core
Version:
Stateless, task-oriented AI agent core
150 lines (149 loc) • 10 kB
TypeScript
/**
* The shape reading of a persisted row, written once per row family: a MEMBER-SHAPE TABLE beside the family's row type, and
* one factory ({@link rowShapeReader}) that turns it into the family's only reader. Every face of a store that reaches into a
* stored row's members — the replay's row judge, a listing, a `get`, a transition that reads a member before it writes — asks
* that reader, so no face answers "this row reads" for a row another face cannot read.
*
* NAMED SEGMENTS, IN ORDER. A table is a list of named segments. The `rowKey` segment is the family's row predicate as the
* replay reads it (what files a row under its key); the others are the members the readers reach for. A face judges exactly
* the segments it judged before this table existed, named at the call — the replay judges `rowKey` alone, so a row the replay
* kept is still kept; a listing or a `get` judges every segment. Event envelopes (the tag, the event's own key, a delete arm)
* and container predicates (a document's own shape) are not rows and stay with their store.
*
* EXITS, PER MEMBER, FROM A CLOSED SET ({@link ROW_EXITS}). A table names the exit of a row that does not read; an entry may name
* another for its member; a row's exit is the exit of the FIRST member that does not read. `list-damaged`: the row stays where
* it is, a listing answers it by its identity and a damaged verdict, a `get` and every verb that reads its members refuse in the
* family's code, deletion by key goes on. `block`: an ordered log's exit — the readable prefix is served and nothing is served,
* leased, acknowledged or counted past the first record that does not read. `omit` and `refuse-container` are the exits of a
* rule family's row (one row left out with a disclosure on every read; the whole container refused); no table here uses them yet.
*
* NOTHING IS ERASED FOR NOT READING: a reader answers; it never removes a row, and no face removes a row because it does not read.
*
* WORDS: the corrupt forms are the store contract's ({@link CorruptBodyForm}) — a member that is absent where the table requires
* it is `missing-required`; one of another type, outside its closed set, or contradicting the row's key is `wrong-type`; a row
* that is no object is `not-an-object`. An answer's `why` names the member by its path and its shape, never by its value.
*/
import type { CorruptBodyForm } from "./store-contracts/read-back-shape-contract.js";
/** The exits of a row that does not read (closed; see the module note). */
export declare const ROW_EXITS: readonly ["list-damaged", "block", "omit", "refuse-container"];
export type RowExit = (typeof ROW_EXITS)[number];
/** What a store knows about a row besides its body: the key it files the row under, and the members its medium holds as the
* authority over the body (a SQL row's primary key, partition scope and OCC revision columns — never a lossy display column). */
export interface RowContext {
readonly key: string;
readonly authoritative?: Readonly<Record<string, unknown>>;
}
/** A row that does not read: the corrupt form, the segment and member that failed first, the exit it takes, and why. */
export interface RowCorrupt {
readonly corrupt: CorruptBodyForm;
readonly segment: string;
readonly member: string;
readonly exit: RowExit;
readonly why: string;
}
/**
* A reader's answer: `row` — the row as its readers read it (the stored object itself, or a shallow copy carrying the readings of
* members that were absent, that a member's own reader normalizes, or that the medium holds authoritatively) — or a corrupt row.
*/
export type RowAnswer<T> = {
readonly row: T;
} | RowCorrupt;
/** A listed row's verdict: `ok`, or `damaged` with the family's code, the member that failed first, its corrupt form and the
* family's fixed engine sentence (the sentence a refusal of the same row starts with). Closed words and engine text only. */
export type RowVerdict = {
readonly kind: "ok";
} | DamagedRowVerdict;
/** The verdict of a row that does not read. A listing's damaged entry carries it as `rowVerdict` — the member a readable entry never
* has, so `"rowVerdict" in entry` is the narrowing. */
export interface DamagedRowVerdict {
readonly kind: "damaged";
readonly code: string;
readonly member: string;
readonly form: CorruptBodyForm;
readonly sentence: string;
}
/** One member's reading: it reads as stored, it reads AS a value (an absent member's reading, a reader's normalization), or not. */
export type MemberReading = {
readonly reads: true;
readonly as?: unknown;
readonly replaced?: true;
} | {
readonly form: CorruptBodyForm;
readonly why: string;
};
/** One entry of a table: the member's name (its path, for an answer) and its judgment over the record and the row's context. */
export interface MemberEntry {
readonly member: string;
readonly exit?: RowExit;
readonly judge: (row: Readonly<Record<string, unknown>>, ctx: RowContext) => MemberReading;
}
/** A named segment of a table (see the module note). */
export interface RowSegment {
readonly name: string;
readonly entries: readonly MemberEntry[];
}
/** A family's member-shape table. `admitOnly` entries are invariants only the write gate holds a candidate to. */
export interface RowShapeTable {
readonly family: string;
/** The family's refusal code — one code for the write gate and every read face. */
readonly code: string;
/** The fixed engine sentence of a refusal and of a damaged verdict — never built from the row. */
readonly sentence: string;
readonly segments: readonly RowSegment[];
readonly exit: RowExit;
readonly admitOnly?: readonly MemberEntry[];
}
/** A family's reader ({@link rowShapeReader}). */
export interface RowShapeReader<T> {
readonly table: RowShapeTable;
/** Judge `raw` over the named segments (default: every segment, in table order). Total over JSON values. */
read(raw: unknown, ctx: RowContext, segments?: readonly string[]): RowAnswer<T>;
/** Judge a write's candidate — the row as the store is about to keep it, its key and revision already placed by the store's own
* rule — over the named segments, then the `admitOnly` entries. The answered row is what the store keeps. */
admit(candidate: unknown, ctx: RowContext, segments?: readonly string[]): RowAnswer<T>;
/** The family's row predicate (the `rowKey` segment alone): what the replay files under its key. */
rowKey(raw: unknown): boolean;
/** The verdict a listing carries for an answer. */
verdictOf(answer: RowAnswer<T>): RowVerdict;
/** The damaged verdict of a corrupt row (what a listing carries for it). */
damagedVerdict(corrupt: RowCorrupt): DamagedRowVerdict;
/** The text of a refusal of a corrupt row: the fixed sentence, then the member's diagnostic. */
refusalText(corrupt: RowCorrupt): string;
}
/** Is an answer a corrupt row? */
export declare function isRowCorrupt<T>(answer: RowAnswer<T>): answer is RowCorrupt;
/** A face's check that a corrupt row takes the exit the face implements: the exit is the table's declaration, so a member whose
* declared exit this face does not implement is an engine defect, refused here rather than answered by another exit's rule. */
export declare function takesExit(corrupt: RowCorrupt, exit: RowExit, face: string): void;
/**
* The family's reader over its table: the only mechanism; the table carries the family's facts.
*/
export declare function rowShapeReader<T>(table: RowShapeTable): RowShapeReader<T>;
/** A value shape: `undefined` = the value reads; otherwise the phrase that follows "its <member>" (a path step, or " is not …"). */
export type ValueShape = (value: unknown) => string | undefined;
export declare const isString: ValueShape;
export declare const isNonBlankString: ValueShape;
export declare const isBoolean: ValueShape;
export declare const isFiniteNumber: ValueShape;
/** A non-negative safe integer (a revision, a generation). */
export declare const isCount: ValueShape;
export declare const isRecord: ValueShape;
/** A member of a closed word set. */
export declare function isWord(words: readonly string[]): ValueShape;
/** A list whose every element reads as `element`. */
export declare function isListOf(element: ValueShape): ValueShape;
/** A record whose named members read as their shapes (a member absent from `optional` is required). */
export declare function isRecordOf(members: Readonly<Record<string, ValueShape>>, optional?: readonly string[]): ValueShape;
/** A member the row must carry, of `shape`. */
export declare function required(member: string, shape: ValueShape, exit?: RowExit): MemberEntry;
/** A member the row may omit; present, it is of `shape`. */
export declare function optional(member: string, shape: ValueShape, exit?: RowExit): MemberEntry;
/** A member whose ABSENCE reads as a value (what the absence means, in a read and a write alike); present, it is of `shape`.
* `fill` may throw (the absence has no reading for this row): the row then does not read, `missing-required`. */
export declare function absentReads(member: string, fill: (row: Readonly<Record<string, unknown>>) => unknown, shape: ValueShape): MemberEntry;
/** A member a reader of its own reads (`reader` answers its reading or throws its refusal): a throw is a member that does not
* read; a reading other than the stored value (the reader's registered normalization) is what the row reads as. */
export declare function via(member: string, reader: (value: unknown) => unknown): MemberEntry;
/** A judgment over the whole row and its context (a member against the key, a member against another): `check` answers
* `undefined` or the sentence that says what does not read. */
export declare function pair(member: string, check: (row: Readonly<Record<string, unknown>>, ctx: RowContext) => string | undefined, form?: CorruptBodyForm): MemberEntry;