@sema-agent/core
Version:
Stateless, task-oriented AI agent core
54 lines (53 loc) • 4.64 kB
TypeScript
/**
* A checkpoint's non-secret identity (`Checkpoint.checkpointId`) as it leaves the store: ONE judge and one closed set of words for
* why an id is not handed out. The faces that read it through {@link readGateIdentity}: the park card a background agent's state
* carries (the live and durable task poll, ListAgents, AgentTranscript — and the engine card on `tool_end.structured` that carries
* their `agentState`), the three engine sentences that name a row (the unreadable summary, the projected summary's descriptor
* damage, the listing selectors), and the unrecorded-execution notice. Not yet through it: the resumed leg's `tool_start` /
* `tool_end` frames hand the row's id out as `resumesCheckpoint` as stored, and a workflow resume's not-pending refusal and its two
* sentences about a parked ordinal name it as stored — those faces move onto the judge in a later slice.
*
* Why a judge: the value is read back from persistence (a store's decoded row, a third-party store's answer), so anything can be
* written there — a row copied wrong, a migration that put the resume token in the id cell, a wrapper that prefixed it. Two
* criteria, both failing closed (a value the judge cannot clear is treated as not there):
* 1. the id never CONTAINS the row's token, compared case-insensitively — containment, not equality (`"cp_" + token` hands the
* whole capability out just the same), and case-folded (the upper-cased form of a lowercase hex token is as reversible);
* 2. the id is bounded ({@link MAX_CHECKPOINT_ID_CHARS}): the engine mints `cp_` + a UUID (39 characters), so an unbounded value
* would let the persisted bytes decide how large every face that echoes it is.
* What it guards against is an accident (a copy, a naive wrap, a case change), not an adversary who can write any value into the row:
* such a writer already holds the row and its token. There is deliberately no format pattern — a token grouped like a UUID passes
* one, and a pattern would only add a silent loss of the id the day the engine changes how it mints one.
*/
/** The upper bound on a trusted `checkpointId`, in UTF-16 code units (`String.prototype.length`). */
export declare const MAX_CHECKPOINT_ID_CHARS = 128;
/** Why a gate's identity is not handed out — `absent`: the row carries no id (a row parked before the member existed);
* `untrusted`: it carries one the judge refused (not a string, empty, over the bound, or containing the row's token). */
export declare const CHECKPOINT_ID_MISSING: readonly ["absent", "untrusted"];
export type CheckpointIdMissing = (typeof CHECKPOINT_ID_MISSING)[number];
/** Whether `word` is a member of {@link CHECKPOINT_ID_MISSING} (for a reader of a stored word): a non-string is not. */
export declare function isCheckpointIdMissing(word: unknown): word is CheckpointIdMissing;
/**
* THE judge: `raw` (a checkpoint id read back from persistence) as it may leave, or `undefined`. It passes exactly when `raw` is a
* non-empty string of at most {@link MAX_CHECKPOINT_ID_CHARS} code units that does not contain `token` once both are folded with
* `String.prototype.toLowerCase()`. `token` is the same row's resume token: `undefined` / `null` read as no token, anything else as
* `String(token)`; an empty token tests nothing, and a token `String()` cannot convert (its conversion throws) fails the id
* closed — the judge never throws. Callers hand both values as read, without normalizing them.
*/
export declare function trustedCheckpointId(raw: unknown, token: unknown): string | undefined;
/** One gate's identity reading: the trusted id, or why there is none — exactly one of the two keys, spread as it is into a face. */
export type GateIdentity = {
readonly checkpointId: string;
readonly checkpointIdMissing?: never;
} | {
readonly checkpointIdMissing: CheckpointIdMissing;
readonly checkpointId?: never;
};
/**
* A gate's identity from the id cell read off its row and that row's token: `{ checkpointId }` when {@link trustedCheckpointId} passes
* it; otherwise `{ checkpointIdMissing }` — `absent` when the cell read as `undefined`, `untrusted` when it held anything else
* (`null` included: a reader that could not read the cell hands `null`, because the cell was there).
*/
export declare function readGateIdentity(raw: unknown, token: unknown): GateIdentity;
/** How an engine sentence names a checkpoint row: `checkpoint "<id>"` for a trusted id, else the reason the id is not shown —
* never a value the judge refused, never the token. */
export declare function checkpointRowLabel(identity: GateIdentity): string;