UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

88 lines (87 loc) • 7.1 kB
/** * The DRAINED-LEG FRAME SEAT (`RunInternals.onLegFrame`): the one seat a host reads a leg's own frames through when the * engine drains that leg's stream itself, the closed set of frames it carries, the address each frame is handed with, and * the door both stream entries pass it through. * * WHICH LEGS. A leg is DRAINED when it was entered through `Runner.runTask` or `Runner.resume`: the engine iterates the * leg's stream for the caller, so the caller holds no `TaskStream` and the leg's frames reach it only through this seat * (the composition entries — the verify gate, the cascade — dispatch every leg that way). On `Runner.runTaskStream` / * `Runner.resumeStream` the caller holds the stream and the same frames ride it, so the seat is REFUSED there * (`config.invalid_argument`) rather than delivering every frame twice. The fact "this leg is drained" is the engine's * own: the two draining entries mark the exact internals object they hand on, the door takes the mark (once — it is * gone after the door reads it), and nothing else in the engine decides it. A copy of the object carries no mark; a * bag the engine builds for a child or a workflow leg never carries the seat. * * WHAT. One frame object per offer, the very object the leg's queue received, shallow-frozen before the push; offered * only when the queue accepted it (a closed stream refuses a late frame, and so does the seat). `at` names the leg the * frame came from: its task id, the session its rows land in and its run id (engine facts), and the composition's label when * the dispatcher bound one (`leg`, absent = nobody labelled this leg; the engine never mints one). */ import type { TaskEvent } from "../types.js"; /** The frames a drained leg offers to `RunInternals.onLegFrame` — closed: a frame type outside it never reaches the seat. */ export declare const DRAINED_LEG_FRAME_TYPES: readonly ["status", "wiring_manifest", "message_committed", "workspace_changed"]; /** One frame the seat receives: a `TaskEvent` of one of {@link DRAINED_LEG_FRAME_TYPES}, the object the leg's stream carries. */ export type DrainedLegFrame = Readonly<Extract<TaskEvent, { type: (typeof DRAINED_LEG_FRAME_TYPES)[number]; }>>; /** The kinds of leg a composition labels: the verify gate's implementation / verifier / fix legs and a cascade's rungs. */ export declare const LEG_LABEL_KINDS: readonly ["impl", "verifier", "fix", "rung"]; /** * A composition's label for the leg it dispatched. `round` is the gate's verification round for `impl` (0, the round * before the first), `verifier` (the round it verifies) and `fix` (the round whose findings it answers); for `rung` it is * the rung's place on the ladder, from 0. Minted by the dispatcher, frozen, never by the engine. */ export interface LegLabel { readonly kind: (typeof LEG_LABEL_KINDS)[number]; readonly round: number; } /** Where a frame handed to `RunInternals.onLegFrame` came from. */ export interface LegAt { /** The leg's task id (`TaskResult.taskId` of that leg). */ readonly taskId: string; /** The session the leg's rows land in. */ readonly sessionId: string; /** The leg's run (`TaskResult.runId` of that leg; the engine's mint, one per leg). Two legs of one session — a verify * gate's implementation leg and its fix legs, one task's successive messages — share `taskId` and `sessionId` and differ * here, so a frame joins the result of the leg that offered it by this id. The engine sets it on every frame it offers; * optional only so an address a host builds itself without it still type-checks. A leg whose prepare did not complete * offers no frame at all (its frames start after prepare): a result whose `runId` no frame carries is such a leg. */ readonly runId?: string; /** The composition's label for the leg; absent = nobody labelled it. */ readonly leg?: LegLabel; } /** The run's one push of a frame of {@link DRAINED_LEG_FRAME_TYPES}: freeze, push, and offer it to the seat when the queue * accepted it. Answers whether the queue accepted it. */ export type LegFrameOffer = (frame: DrainedLegFrame) => boolean; /** A draining entry marks the internals object it is about to hand to the stream entry, immediately before the call. */ export declare function markDrainedLeg(internals: object | undefined): void; /** …and clears it when that call returns or throws, so a mark the door did not take never outlives the call. */ export declare function forgetDrainedLeg(internals: object | undefined): void; /** The stream entry a draining entry hands its leg to: the door's `entry` word. */ export type LegFrameEntry = "runTaskStream" | "resumeStream"; /** * The ONE read of a caller's `RunInternals` bag: a shallow copy of its own enumerable members (an inherited or non-enumerable member is * not read; an accessor is read here, once), fenced ({@link fenced}) — a bag whose read throws is refused by name, `config.invalid_argument`. * The door reads the bag through it at both stream entries, and the resume's config judge reads a host's question through it, so the bag a * question is judged over is the bag the door hands the leg. Module-internal: not a root export. */ export declare function readLegInternals<T extends object>(internals: T, entry: LegFrameEntry): T; /** * The seat's refusal when the caller's object answers one the door's copy cannot hold ({@link seatOutsideCopy}): the ONE judgment of a * hidden seat, read by the door at both stream entries and by every composition that dispatches its legs through a copy of the caller's * bag (the verify gate, the cascade — `withLegIdentity`), so a seat refused by `Runner.runTask` is refused by a composition alike, and * the lookup that finds it is fenced once, here. */ export declare function refuseHiddenLegFrameSeat(bag: object, entry: LegFrameEntry): void; /** * The door every leg passes at its stream entry (`Runner.runTaskStream` before prepare, `Runner.resumeStream` at the * admission's head, before anything is read or claimed). Takes the drained mark off the caller's object — once, whatever * follows — and refuses a retired seat by name (`config.deps_retired_key`, read off the caller's object as every * retired-key door reads, row by row so a lookup that throws is refused by that row's name). Then it reads the bag ONCE — a * shallow copy of its own enumerable members, the copy the leg goes on with — and judges the seat on that copy: a seat that is * not a function, a seat the copy cannot hold (a prototype's or a non-enumerable one, {@link refuseHiddenLegFrameSeat}), and * the seat on a leg the engine does not drain, are refused (`config.invalid_argument`). Every read of the caller's bag here is * fenced ({@link fenced}). Answers the copy, so the seat the door judged is the seat the leg offers its frames to: a member that * answers the door one value and the leg another cannot pass unjudged. */ export declare function admitLegFrameSeat<T extends object>(internals: T | undefined, entry: LegFrameEntry): T | undefined;