@sema-agent/core
Version:
Stateless, task-oriented AI agent core
102 lines (101 loc) • 8.25 kB
TypeScript
/**
* The device lane's cross-boundary WIRING for one leg: the tree's shared state ref, the session record that carries the state from
* one leg of a session to the next, and the gate seat whose ONE obligation reading the station and both exits share.
*
* WHERE THE STATE LIVES. The tree's state is one object, reached from the tree's opaque token (`RunInternals.crossBoundaryTree`,
* `ToolExecuteContext.crossBoundaryTreeForChildren`) through this module alone; each leg joins into it what it knows at prepare (its
* environment's declaration, its session's newest record, its posture) and every bit an exit sets lands there before the call
* runs — after the record that carries it, so a call the exit refuses sets nothing. Each leg's session holds a record
* (`CROSS_BOUNDARY_RECORD_TYPE`) of the state its content is read under — written only for an armed tree: at prepare and at an
* exit (before the call runs) when the state grew past what the session already says, and at a leg's end (before a suspend's row is
* put, when the leg settles) also when conversation content follows the newest record. A write that fails refuses its call (or its
* row) on a governed tree; a prepare's or a settle's, and any write on a tree that is not governed, mints
* `config.cross_boundary_record_unwritten`, once per state. A record says how the content before it is READ: a leg's end record also
* carries what the session is still owed — each unfinished background task the leg's task access reaches, judged as the poll the
* session's next leg (another tree) would make of it, so its delivery is read under the bit it brings. A leg that takes over a
* session whose content no record covers (none on the branch, or after the newest), on an armed tree, reads that content as
* off-target.
*
* THE OBLIGATION of a call is read off three things at the moment it is asked ({@link crossBoundaryObligation}): the legs the
* call travels (its roster row's declaration, or what the dispatching code gives per call), whether the environment the `env`
* leg reaches declares itself the target NOW, and the tree's state NOW.
*/
import type { AgentTool } from "../../internal/harness.js";
import type { ExecutionEnv, SessionTreeEntry } from "../../internal/harness-types.js";
import { type BoundaryLeg, type CrossBoundaryCall, type CrossBoundaryCheck, type CrossBoundaryPosture, type CrossBoundaryTreeToken, type OwedDeliveries } from "../cross-boundary-taint.js";
import type { NoticeRoute } from "../leg-identity.js";
import { type ToolRoster } from "../tool-roster.js";
import type { RunnerDeps } from "../types.js";
/** A new tree with no bit set, and the token that names it — the one mint of a token. */
export declare function mintCrossBoundaryTree(): CrossBoundaryTreeToken;
/** The tree a leg joins: the one its handed token names, or a new one (a root leg) — synchronous, so the phases that hand the token
* down never wait on the session. A token the engine did not mint names no tree it can read: reported, and the leg's tree reads as
* the strictest state. */
export declare function joinedCrossBoundaryTree(handed: CrossBoundaryTreeToken | undefined, report: (message: string) => void): CrossBoundaryTreeToken;
/** One leg's cross-boundary wiring (see the module header). */
export interface CrossBoundaryLane {
/** The tree's token — the one the leg's children are handed. */
readonly token: CrossBoundaryTreeToken;
/** The station and the exits are ENGAGED on this leg: a posture is declared, the leg runs on a target, or its tree is governed —
* each fixed at prepare, so no live consumer flips between two readings mid-run. */
readonly stationEngaged: boolean;
/** The gate seat, minted once the leg's roster and tool instances exist; undefined on a leg whose station is not engaged. */
seat(view: CrossBoundaryLegView): CrossBoundaryCheck | undefined;
/** The seat minted by {@link seat}, for the resumed call's exit. */
readonly check: CrossBoundaryCheck | undefined;
/** A suspend (a leg's end) writes the record its row's leaf owes: the new entry's id, or undefined when nothing was owed (or, on a
* tree that is not governed, the write failed and was reported); throws when it cannot be written on a governed tree. The record
* covers the content after the newest one and what the session is still `owed`, and is written only as the child of `atLeaf` (the
* row then points at it). */
owe(atLeaf: string, owed: OwedDeliveries): Promise<string | undefined>;
/** The tree is armed, so the session OWES the tree's record and a parked row says a reader must read it — on a tree that is not
* governed a failed write leaves the record missing (the notice says so), and the reader then reads the content after the newest
* record as off-target. */
recorded(): boolean;
/** The leg settles: the record that covers what it added and what the session is still `owed` is written, a failure reported
* loudly (the next leg then reads the content after the newest record as off-target); `boundMs` = what is left of a shared bound. */
settle(owed: OwedDeliveries, boundMs?: number): Promise<void>;
/** How many times this leg's record writes have waited on the session store — a station that judged before an exit reads again
* when the count moved across it (the exit's own form: judged again after a wait). */
storeWaits(): number;
/** The legs one call declares it travels, on its arguments, read off the live roster as the station reads them ({@link legsOfCall}:
* a call no row answers, or a `"per_call"` resolver that is missing, throws or answers outside its type, reads as every leg — the
* fault reported). Engaged or not: a confined Runner's mode point reads it for the one call it may release. */
legsOf(view: CrossBoundaryLegView, call: CrossBoundaryCall): readonly BoundaryLeg[];
}
/** What the seat reads per call: the live roster (the legs a row declares) and the mounted instances (a `"per_call"` row's resolver,
* read off the later mount under the row's name — the instance the row was minted from, but in a redial window: KL-2284-c). */
export interface CrossBoundaryLegView {
readonly tools: readonly AgentTool[];
readonly roster: {
readonly current: ToolRoster | undefined;
};
}
export interface CrossBoundaryLaneInput {
/** The tree this leg joins ({@link joinedCrossBoundaryTree}), or none — a new tree. */
token?: CrossBoundaryTreeToken;
/** The posture door's reading (`undefined` = undeclared). */
posture: CrossBoundaryPosture | undefined;
/** The run fold: this leg's environment declares the target, or the row it resumes was minted on one. */
externalContentTargetActive: boolean;
/** The leg's own environment — read for its CURRENT declaration (`targetIsExternal`) at each judgment. */
env: Pick<ExecutionEnv, "capabilities">;
session: {
getBranch(): Promise<ReadonlyArray<SessionTreeEntry>>;
getLeafId(): Promise<string | null>;
appendCustomEntry(customType: string, data?: unknown): Promise<string>;
};
resumeLeg: boolean;
sessionId: string;
runId: string;
hostTaskId: string;
/** Where this leg's user-face notices go (`noticeAddressOf`, minted once per leg): the leg's frames are filed under the session
* it names — the leg's own on a top-level leg, the delegation tree's root on a delegated one. */
noticeRoute: NoticeRoute;
deps: Pick<RunnerDeps, "onError" | "onNotice" | "tracer">;
}
/** A confined Runner's destination reading for one call: it lands on the leg's hands (`onHands` — the sandbox boundary's own judgment:
* a hand of the environment with no egress mark) and every boundary leg it declares ({@link CrossBoundaryLane.legsOf}) is the environment's. */
export declare function destinationOnHands(onHands: boolean, legs: readonly BoundaryLeg[]): boolean;
/** The leg's lane: the tree ref joined with what this leg knows, the session's record read and (when the state grew) written. */
export declare function prepareCrossBoundaryLane(input: CrossBoundaryLaneInput): Promise<CrossBoundaryLane>;