@sema-agent/core
Version:
Stateless, task-oriented AI agent core
74 lines (73 loc) • 6.27 kB
TypeScript
/**
* Whose is it: the identity of a run — the person and the scope of their work — as it is recorded beside what the run makes, and
* the one judgment of whether a record names the same identity as a run that asks for it. Every face that answers "is this yours"
* by a run's identity reads these two functions: a run's offloaded tool results (`LegToolResultView`, `tool-result-store.ts`) and a
* host's own read face over them; the record a face keeps (an owner record, a row's owner cells) is that face's own, the identity in
* it is this one.
*/
import { type IsolationScopeHolder } from "./isolation-scope.js";
/**
* The identity of a run: the person (`TaskSpec.principal`; `null` when the run names none — absent or the empty string — which is
* the anonymous identity, one identity of its own and not a name) and, when the run works in a separate scope of that person's
* work, that scope (`TaskSpec.isolationScope`, present only when it differs from the principal). The same pair a parked row
* records ({@link isolationScopeRecordOf}) and the MCP connection pool keys on; a delegated child, a fork, a workflow member, a
* background agent and a resumed leg carry their spawner's pair verbatim.
*/
export interface OwnerIdentity {
readonly principal: string | null;
readonly isolationScope?: string;
}
/**
* The identity of the run `holder` describes — a task's spec, or a caller's `{ principal, isolationScope }` exactly as a host would
* put them on a task: the principal (`null` when it names none), and the isolation scope only when it is not the principal. It is the
* canonical projection of the leg's one identity (`legIdentityOf`, which the tool context carries), so a run's view and a host's
* read face that hand it the same members get the same identity.
*/
export declare function ownerIdentityOf(holder: IsolationScopeHolder): OwnerIdentity;
/**
* The one-column encoding of a recorded principal for a store that keeps it in a nullable text column (the reference SQL
* adapters): the anonymous identity (`null`) is `''` — the engine never names a principal `''`, the empty string is absence — and
* no identity recorded (an absent member) is SQL `NULL`. {@link decodePrincipalColumn} reads it back; the pair round-trips all three.
*/
export declare function encodePrincipalColumn(principal: string | null | undefined): string | null;
/** The recorded principal a column written by {@link encodePrincipalColumn} holds: `undefined` (no identity recorded) for a
* non-string, `null` (the anonymous identity) for `''`, the name otherwise. */
export declare function decodePrincipalColumn(column: unknown): string | null | undefined;
/**
* What {@link identityReadVerdict} answers: `"readable"`, or why not — `"no_owner"` (no record: nothing recorded under the key, or a
* record stored without an owner), `"no_identity"` (a record that names no identity: written before identities were recorded, or by
* a store that does not keep them), `"owner_unreadable"` (an answer that is not a record, or whose identity members are not of their
* shape), `"other_identity"` (an identity that is not the reader's).
*/
export type IdentityReadVerdict = "readable" | "no_owner" | "no_identity" | "owner_unreadable" | "other_identity";
/**
* THE judge of "is this record yours": may a run of identity `identity` read what `record` records as its owner? Only when the record
* names an identity — `principal` a name or `null` (the anonymous identity), `isolationScope` absent or a name — and that identity is
* the reader's: the same principal and the same isolation scope (each side's scope is its member, else its principal — the
* `isolationScopeOf` reading). The anonymous identity is one identity: an anonymous run reads what anonymous runs made, nothing a
* named run made, and a named run nothing an anonymous run made; a principal named `default` (or any placeholder) is another person.
* A record with NO `principal` member is not the anonymous identity — it recorded none, and nobody reads it. The READER is judged as
* at a door, by the whole name rule: a `principal` that is neither `null` nor a name, or an `isolationScope` that is there and is not a
* name, is no identity and reads nothing (`other_identity`) — so a record whose owner is a name an earlier release stored and today's
* rule refuses is read by nobody, never by a caller who asks under that same string.
*
* Only the identity members are read; the rest of a record (a session, a task) is the face's own and is not judged here. A member
* that throws when read is not an absent one.
*
* @contract owner_identity.readable_is_same_person_and_partition — for two identities that each come from a mint (`ownerIdentityOf`,
* or `recordedOwnerIdentityOf` reading an owner record), the reader's principal `null` or a name and its `isolationScope` absent or a
* name, the verdict is `readable` if and only if their principals are equal and
* their partitions are equal, a partition being the identity's `isolationScope` when it has one and its principal otherwise (the
* `isolationScopeOf` reading); no mint produces the empty string, neither as a principal (an empty principal is the anonymous
* identity, `null`) nor as an `isolationScope` (it is absent when empty or when it equals the principal), so that reading and the
* `??` this judge compares partitions by agree on every pair of identities a mint can produce.
*/
export declare function identityReadVerdict(record: unknown, identity: OwnerIdentity): IdentityReadVerdict;
/**
* The identity an owner record names, read the one way every face reads it — the read judge above and an identity move's
* rewrite of the record: `principal` a name as a stored name is read (`recordedPrincipalNameOf`: a name an earlier release admitted
* still reads, so its record can be moved) or `null` (the anonymous identity), `isolationScope` absent or a
* non-empty string. `"none"` = neither member (the record names no identity); `"unreadable"` = a member that is not of its shape (or
* that throws when read). Only the two identity members are read.
*/
export declare function recordedOwnerIdentityOf(record: object): OwnerIdentity | "none" | "unreadable";