@sema-agent/core
Version:
Stateless, task-oriented AI agent core
91 lines (90 loc) • 6.5 kB
TypeScript
/**
* The grant holder: the value a host hands a run as `RunInternals.holder` — its credential for the one claim grant the run acts under —
* and the two words a store answers about it. The engine carries the value verbatim and never judges, stores, compares or prints it;
* the codecs below are for hosts (the deployment's judge and the code that mints the value), so that every judge a deployment runs
* reads one encoding — the grant codec ({@link encodeGrantHolder}) and the composite one ({@link encodeCompositeHolder}: a grant half,
* an occupancy half of the session tree, or both). The engine's own paths never call either.
*
* Two words, one each:
* - `holder.superseded` — a write carrying a holder whose grant is no longer the run's current leg, or whose run has ended: the store
* wrote nothing, and the leg stops without retrying ({@link holderSupersededError}, {@link isHolderSuperseded});
* - `holder.malformed` — a value {@link decodeGrantHolder} cannot read: a judge that meets one throws it, and the write throws with it,
* nothing written.
*/
/** What a grant names: the run (`taskId`) and the leg of that run the grant was given to (`legId`). Both non-empty strings. */
export interface GrantHolderParts {
readonly taskId: string;
readonly legId: string;
}
/**
* Encode a grant as the holder a host hands a run (`RunInternals.holder`).
*
* THE FORM: `g1.<taskId>.<legId>`, each part the UTF-8 bytes of its string in base64url with no padding. The value is opaque to
* everything but {@link decodeGrantHolder}: the engine never reads it, and a host does not parse it by hand.
*
* STABLE ACROSS VERSIONS: the `g1.<taskId>.<legId>` form does not change from one release to the next, so a host may hand the value
* between replicas and may store it. A future form takes a new prefix, and its release lists that change as hard-breaking.
*
* THE ROUND-TRIP LAW (both directions hold):
* - `decodeGrantHolder(encodeGrantHolder(p))` has the same `taskId` and `legId` as `p`, for every `p` this function accepts;
* - `encodeGrantHolder(decodeGrantHolder(h)) === h` for every `h` {@link decodeGrantHolder} accepts — each grant has exactly one
* encoding, so two holders are the same grant exactly when they are the same string.
*
* Refuses (`config.invalid_argument`) a part that is not a non-empty, well-formed string (a lone surrogate would not survive the
* round trip), naming the part, never its value.
*/
export declare function encodeGrantHolder(parts: GrantHolderParts): string;
/**
* Decode a holder minted by {@link encodeGrantHolder} (the form and the round-trip law are written there). Anything else — not a
* string, another version, a part that is not the one base64url encoding of non-empty UTF-8 — throws `holder.malformed`, never
* answers a grant: a judge that meets it throws, and the write that carried it throws with nothing written.
*/
export declare function decodeGrantHolder(holder: string): GrantHolderParts;
/** What the occupancy half of a composite holder names: the session tree's root row (`rootSessionId`), its owner (`null` for a row that
* records none) and the occupant token of the row (`incarnation`). Strings non-empty when present. */
export interface OccupancyHolderParts {
readonly rootSessionId: string;
readonly owner: string | null;
readonly incarnation: string;
}
/** What a composite holder carries: a grant half, an occupancy half, or both. A half that is absent is a half with nothing to judge. */
export interface CompositeHolderParts {
readonly grant?: GrantHolderParts;
readonly occupancy?: OccupancyHolderParts;
}
/**
* Encode the holder a host hands a run (`RunInternals.holder` / `RunInternals.treeHolder`) when it carries more than a grant: a grant
* half (`{ taskId, legId }`), an occupancy half (`{ rootSessionId, owner, incarnation }` — the session tree's root row as the host read
* it), or both. The engine carries the value verbatim and never decodes it; the deployment's judge decodes it with
* {@link decodeCompositeHolder} and judges each half it carries.
*
* THE FORM: `h1.<taskId>.<legId>.<rootSessionId>.<owner>.<incarnation>`, each part the UTF-8 bytes of its string in base64url with no
* padding; the parts of an absent half are empty, and so is `owner` when it is `null`. STABLE ACROSS VERSIONS as `g1` is.
*
* THE ROUND-TRIP LAW (both directions, as for {@link encodeGrantHolder}): `decodeCompositeHolder(encodeCompositeHolder(p))` carries the
* same halves and parts as `p`; `encodeCompositeHolder(decodeCompositeHolder(h)) === h` for every `h` it accepts. A grant-only holder is
* spelled here as `h1.…` only: {@link decodeCompositeHolder} does not read `g1`, and {@link decodeGrantHolder} does not read `h1`.
*
* Refuses (`config.invalid_argument`) parts with no half, a present part that is not a non-empty, well-formed string — naming the part,
* never its value.
*/
export declare function encodeCompositeHolder(parts: CompositeHolderParts): string;
/**
* Decode a holder minted by {@link encodeCompositeHolder} — THE one decoder of the form (the form and the round-trip law are written
* there): the halves it carries, a half it does not carry absent from the answer (nothing to judge on that half). Anything else — not a
* string, another version (`g1` included), a half with only some of its parts, an `owner` with no root, both halves absent, a part that
* is not the one base64url encoding of non-empty UTF-8 — throws `holder.malformed`, never answers a holder.
*/
export declare function decodeCompositeHolder(holder: string): CompositeHolderParts;
/**
* THE refusal a store answers a write that carries a holder its judge does not hold current — the one constructor every store uses
* (an append, a leaf move, a card's `put` or steer: the writes with no row a caller could re-read). `what` names the write ("append",
* "setLeafId", "put", …), never the holder. The store wrote nothing. A CAS-shaped verb (`resolve`, `expire`) does not throw it: it
* loses, and its caller's loss reading names the word.
*/
export declare function holderSupersededError(what: string): Error & {
readonly code: "holder.superseded";
};
/** THE reading of {@link holderSupersededError}: any thrown value whose `code` is `holder.superseded` (read once, guarded — a thrown
* value's accessor that throws is not this word). */
export declare function isHolderSuperseded(err: unknown): boolean;