UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

235 lines (234 loc) • 17.1 kB
/** * The device lane's CROSS-BOUNDARY vocabulary: the run tree's two taint bits and its posture fact, the legs one call * travels, the one table those answer a call's obligation from, and the session record that carries a tree's state from * one leg of a session to the next. Pure data and pure functions — no I/O, no clock, no engine state. * * THE RULE. A call's cross-boundary obligation is decided by three things and nothing else: the legs THIS call travels * (declared by the code that dispatches it — a constant on most definitions, given per call by the two that route by * their arguments), whether the environment a leg reaches declares itself the target device right now, and the tree's * three bits. Two outcomes, independent of each other: `mandate` — a person confirms this call; `record` — once this * call is allowed, the tree's off-target-read bit is set. {@link crossBoundaryObligation} is the one function that * answers it; every place that asks calls it and none restates a cell. */ import type { AskClass, AssertAllKeysHandled } from "./ask-origin.js"; import type { EngineNotice } from "./engine-notice.js"; import type { ExecutionEnv } from "../internal/harness-types.js"; import { CROSS_BOUNDARY_POSTURES, type CrossBoundaryPosture } from "@sema-agent/primitives"; /** The run tree's two TAINT bits: the tree executes on a target device (`targetExecutionArmed`), and an off-target read * was allowed into it since (`offTargetReadObserved`). Presence-coded on every carrier: `true` or absent, never `false`. */ export declare const CROSS_BOUNDARY_TAINT_BITS: readonly ["targetExecutionArmed", "offTargetReadObserved"]; export type CrossBoundaryTaintBit = (typeof CROSS_BOUNDARY_TAINT_BITS)[number]; /** * The tree's state: the two taint bits and `governed` — the tree has run under the `multi_tenant` posture since it was * first armed. `governed` is a posture fact, not a taint bit; it rides the same carriers and joins the same way. Each * member is `true` or absent. The state only grows ({@link joinCrossBoundaryTree}). */ export interface CrossBoundaryTree { readonly targetExecutionArmed?: true; readonly offTargetReadObserved?: true; readonly governed?: true; } /** The deployment's posture words (`RunnerDeps.crossBoundaryPosture`), declared in `@sema-agent/primitives` (their one home: the * plain-http admission list's reading there reads a posture's default by them) and handed on here. */ export { CROSS_BOUNDARY_POSTURES, type CrossBoundaryPosture }; /** * The legs one call can travel: `env` — into or out of an execution environment (the call's arguments reach it, or its * content comes back from it); `outside_read` — content fetched from beyond the boundary; `outside_write` — an effect * produced beyond the boundary. An empty set is a call whose effect stays inside the engine (its process or its session * store). */ export declare const BOUNDARY_LEGS: readonly ["env", "outside_read", "outside_write"]; export type BoundaryLeg = (typeof BOUNDARY_LEGS)[number]; /** A definition's declaration of its legs: the legs every call travels, or `"per_call"` — the code that dispatches the call * gives them per call (the destination is chosen by the call's own arguments). */ export type BoundaryLegsDeclaration = BoundaryLegList | "per_call"; /** A list of legs, in any order, each at most once on a roster row. */ export type BoundaryLegList = readonly BoundaryLeg[]; /** What one call owes: a person's confirmation (`mandate`), the off-target-read bit once allowed (`record`), both, or neither. */ export interface CrossBoundaryObligation { readonly mandate: boolean; readonly record: boolean; /** Why the mandate holds, when it holds only because content from beyond the device was let into the tree: the same call on the * same tree without the off-target-read bit would owe no confirmation. `by` — the tool whose allowed call set that bit, when a call * of this tree set it in this process (a bit read from the session's record names no call). Said by the leg's seat * ({@link CrossBoundaryCheck.obligation}), which holds the tree's history; the table alone ({@link crossBoundaryObligation}) answers * what is owed under one state and never says it. Display only: no judgment reads it. */ readonly afterOffTargetRead?: { readonly by?: string; }; } /** What a `"per_call"` definition's dispatch gives for one call: the legs it travels and, when its `env` leg reaches an * environment other than the calling leg's own (a background shell's), that environment — its declaration * (`capabilities.externalContentTarget`) is read as the call is judged. */ export interface BoundaryLegsGiven { readonly legs: readonly BoundaryLeg[]; readonly env?: Pick<ExecutionEnv, "capabilities">; } /** What a leg's session is still owed when the leg ends: the legs each unfinished background task its access (the registry task access) delivers * along, read as the poll the session's next leg — another tree — would make of it (`TaskRegistry.owedDeliveries`). */ export type OwedDeliveries = () => readonly BoundaryLegsGiven[]; declare const CROSS_BOUNDARY_TREE_TOKEN: unique symbol; /** A run tree's cross-boundary IDENTITY — opaque and frozen, minted by the engine alone: a tool is handed it so a delegation or * workflow lane can join its children to the same tree and a registry handle can name the tree that spawned it, compared by * identity. Nothing reads or writes the tree's state through it; the state stays the engine's. */ export interface CrossBoundaryTreeToken { readonly [CROSS_BOUNDARY_TREE_TOKEN]: true; } /** A `"per_call"` definition's resolver, written beside the code that dispatches the call: handed the call's arguments as the * tool reads them and the calling tree's {@link CrossBoundaryTreeToken} (a handle that recorded one compares it by identity). No I/O. */ export type BoundaryLegsFor = (args: unknown, at: { readonly tree: CrossBoundaryTreeToken; }) => BoundaryLegsGiven; /** Why an exit refused a call its decision allowed: the call needs a person's confirmation under the tree's state NOW and no person * answered a question that asked for it (`unconfirmed` — a configuration's or a policy's answer, or a person's answer to a question * that did not carry the cross-boundary class), or the session record the call owes could not be written before it ran * (`unrecorded`). `message` is the one sentence the refusal carries to the model and the wire. */ export interface CrossBoundaryExitRefusal { readonly kind: "unconfirmed" | "unrecorded"; readonly message: string; } /** One call as the cross-boundary seat reads it: its wire name, its arguments as they would run, and its id (the trace names it). */ export interface CrossBoundaryCall { readonly toolName: string; readonly args: unknown; readonly toolCallId?: string; } /** The gate's cross-boundary seat — ENGINE-BUILT per leg (`runner/cross-boundary-lane.ts`), never a deployment callback. */ export interface CrossBoundaryCheck { /** This call's obligation under the tree's state at this moment (the station reads it on the final arguments), with * `afterOffTargetRead` when only the content let in since makes it a person's ({@link CrossBoundaryObligation.afterOffTargetRead}). */ obligation(call: CrossBoundaryCall): CrossBoundaryObligation; /** The allow exit's rule, on the bytes that run and the classes of the question a PERSON answered for this call (`undefined` when * no person answered — a policy's or a configuration's allow, a relayed grant, a row or record without classes): re-reads the * obligation; a mandate stands only when `answered` is a class list that carries `cross_boundary` — anything else refuses * (`unconfirmed`); otherwise the session record carrying the off-target-read bit the call owes is written, and the bit joins the * tree, before it runs — a call the exit refuses joins nothing. */ exit(call: CrossBoundaryCall, answered: readonly AskClass[] | undefined): Promise<CrossBoundaryExitRefusal | undefined>; } /** The gate input's seat for {@link CrossBoundaryCheck}: present on a leg whose station is ENGAGED (a posture is declared, the leg runs * on a target, or its tree is governed); absent ⇒ the decision path is byte-identical. */ export interface CrossBoundaryGateSeat { crossBoundaryCheck?: CrossBoundaryCheck; } /** The table's rows. An `env` leg is judged by where it lands: a target environment is its own row, any other environment * reads as both outside rows ({@link ENV_LEG_ROWS}). */ type LegRow = "env_to_target" | "outside_read" | "outside_write"; /** THE table. The host-lane column is empty, so a tree that never armed decides exactly as it would without this table. */ declare const OBLIGATION_TABLE: { readonly env_to_target: { readonly host_lane: "none"; readonly armed: "none"; readonly armed_observed: "mandate_if_governed"; }; readonly outside_read: { readonly host_lane: "none"; readonly armed: "record"; readonly armed_observed: "none"; }; readonly outside_write: { readonly host_lane: "none"; readonly armed: "mandate_if_governed"; readonly armed_observed: "mandate_if_governed"; }; }; export type ObligationTableCoversEveryRow = AssertAllKeysHandled<Exclude<LegRow, keyof typeof OBLIGATION_TABLE>>; /** The obligation of one call — the union of its legs' cells under the tree's current state. `legs` are the legs this call * travels (a definition's constant, or what its dispatch gave for this call); `reachedEnvIsTarget` — the environment its * `env` leg reaches declares itself the target right now (read off that environment, never off the tree's history; unread * when the call has no `env` leg). A leg outside {@link BOUNDARY_LEGS}, a non-boolean target fact or a malformed tree throws. */ export declare function crossBoundaryObligation(legs: readonly BoundaryLeg[], reachedEnvIsTarget: boolean, tree: CrossBoundaryTree): CrossBoundaryObligation; /** The cross-boundary station's own question (the gate's demotion of an allow — another layer's ask keeps its words): a call that * reaches beyond the device of a run that executes on one; or — when the seat says only the content let in since makes it a * person's ({@link CrossBoundaryObligation.afterOffTargetRead}) — a call into the device after that, naming the tool whose call let * the content in when the seat knows it. */ export declare function crossBoundaryQuestion(toolName: string, afterOffTargetRead: CrossBoundaryObligation["afterOffTargetRead"]): string; /** The tree is GOVERNED — what the table's `mandate_if_governed` cells read, and so what makes the session record a carrier some * judgment reads (a record that cannot be written refuses its call on a governed tree only). */ export declare function crossBoundaryTreeGoverned(tree: CrossBoundaryTree): boolean; /** The join of two tree states: every bit either holds. Monotone — it never clears a bit — and returns a fresh frozen * state, never one of its operands. */ export declare function joinCrossBoundaryTree(a: CrossBoundaryTree, b: CrossBoundaryTree): CrossBoundaryTree; /** Whether `covering` holds every bit `tree` holds — a record that covers a tree's state need not be written again. */ export declare function crossBoundaryTreeCovers(covering: CrossBoundaryTree, tree: CrossBoundaryTree): boolean; /** The strictest state: what a record that cannot be read stands for. */ export declare const STRICTEST_CROSS_BOUNDARY_TREE: CrossBoundaryTree; /** The `customType` of the session entry that records a tree's state: a custom entry, persisted with the transcript and * never replayed into model context — the state lives where the content it describes lives. */ export declare const CROSS_BOUNDARY_RECORD_TYPE = "engine:cross_boundary"; /** The record's `data`: written only for an armed tree, so `targetExecutionArmed` is always present. */ export interface CrossBoundaryRecordData { readonly targetExecutionArmed: true; readonly offTargetReadObserved?: true; readonly governed?: true; } /** The one spelling of a record's `data` for a tree state. A tree that is not armed has no record: refused. */ export declare function crossBoundaryRecordData(tree: CrossBoundaryTree): CrossBoundaryRecordData; /** What a branch says about its tree: no record on it; the join of its records (`entryId` = the newest one's); or a record — or an * entry — that cannot be read, which reads as the strictest state, with the sentence a disclosure names. A record covers the content * before it: `uncoveredContent` — conversation content (the session layer's `hasConversationContent`: a message, a custom message, a * compaction) follows the newest record, or with no record is on the branch at all — is content produced under a state no record says. */ export type CrossBoundaryRecordRead = { readonly kind: "absent"; readonly uncoveredContent: boolean; } | { readonly kind: "recorded"; readonly entryId?: string; readonly tree: CrossBoundaryTree; readonly uncoveredContent: boolean; } | { readonly kind: "malformed"; readonly entryId?: string; readonly tree: CrossBoundaryTree; readonly why: string; }; /** The members a session entry is read by. */ export interface CrossBoundaryRecordEntry { readonly type: string; readonly id?: string; readonly customType?: unknown; readonly data?: unknown; } /** * Read a branch's tree state: the JOIN of every cross-boundary record on `branch` (entries in path order) — the state only grows, so * a later record holding fewer bits never lowers it. Any record whose shape is not a record's, and any entry that cannot be read * (one of its members throws), makes the read {@link STRICTEST_CROSS_BOUNDARY_TREE}, never a looser state. No record reads `absent`. */ export declare function readCrossBoundaryRecord(branch: readonly CrossBoundaryRecordEntry[]): CrossBoundaryRecordRead; /** The one mint of `config.cross_boundary_posture_undeclared`: an armed tree on a deployment that configured no posture, so * the permissive `single_user` reading is in force. `detail.taint` lists the tree's taint bits. */ export declare function crossBoundaryPostureUndeclaredNotice(at: { readonly sessionId: string; readonly runId: string; readonly tree: CrossBoundaryTree; }): EngineNotice; /** Where a session record's write was owed: a leg's prepare, an exit before its call runs, a suspend before its row, a settle. */ export type CrossBoundaryRecordPhase = "prepare" | "exit" | "suspend" | "settle"; /** The one mint of `config.cross_boundary_record_unwritten`: a session record that could not be written and refused nothing — on a tree * that is not governed (no judgment reads the record), or at a governed leg's prepare / settle (no call to refuse). On a governed tree * the record stays owed: a prepare's by the leg's next write, and once none lands — a settle's failure is the leg's last — the next leg * reads the content after the newest record as off-target. A governed exit refuses its call and a governed suspend its row instead. * Minted once per state a leg could not write. */ export declare function crossBoundaryRecordUnwrittenNotice(at: { readonly sessionId: string; readonly runId: string; readonly phase: CrossBoundaryRecordPhase; readonly governed: boolean; readonly cause: string; }): EngineNotice; /** The face an MCP tool carries for its JUDGED `toolAxes` entry (`readToolAxes`' copy): the empty leg set when the entry declares * `boundaryFlow: "engine"`, nothing otherwise (its roster row then states the legs its effect implies). The one translation of that * declaration onto a mounted instance; an A2A skill has none (its reader refuses `boundaryFlow`). */ export declare function boundaryFlowFace(entry: { readonly boundaryFlow?: "engine"; } | undefined): { boundaryLegs?: readonly BoundaryLeg[]; }; /** * The legs a ROSTER ROW states, from where the instance came from, what it declares and its resolved effect. The engine's * own definitions (`builtin` / `synthetic` rows) and the protocol tools the engine materialized (`mcp` / `a2a`) are read by * their declaration; any other row — and a row of those sources that declares nothing — by its effect: `read` reaches beyond * the boundary to read, anything else to write. A declaration that is neither `"per_call"` nor a list of legs is refused on EVERY * row, whoever declared it (`undefined` back, the caller refuses the row loudly): a row that does not read a declaration still does * not let a bad one pass silently. The returned list is a fresh array in {@link BOUNDARY_LEGS} order. */ export declare function rosterBoundaryLegs(source: string, declared: unknown, effect: "read" | "write" | "idempotent"): BoundaryLegsDeclaration | undefined;