UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

106 lines (105 loc) • 9.19 kB
/** * The caller roster's ONE snapshot (#1167). A run's tool roster is the array the run's one read of the caller's spec took for * `tools` (`readCallerSpec`, at the run's entrance), its entries as they stand when prepare starts — before its first await — and * nothing later, down to each entry's members: the driver rebinds onto a private copy of the spec whose `tools` is an own, fixed * data property — a frozen array of frozen entry FACES, each member read once (an omitted roster stays omitted, a value that is * no array stays as read). The safety scan, the mount, the listings and every other phase read those faces, * so no phase can read the caller's objects a second time after a caller microtask had the chance to change them — * not the array (a push, a reassignment of `spec.tools`) and not an entry (a name, an effect or egress mark, a schema, * the `execute` function swapped in place, a getter or a Proxy answering differently on a later read). * * The snapshot holds no live entry. Every object entry (a function included) becomes a face: its own enumerable members * (string and symbol keys — what the mount's spread carries) plus every ToolSpec member name read through an ordinary * property read (how the phases read a declaration — non-enumerable, inherited or Proxy-answered included), each read once; * every declaration value is deep-copied and frozen by the one copier. A primitive is immutable and stays as read. A * read that throws refuses the run at the snapshot, and so does an entry that carries a retired ToolSpec member under any * reading of the key ({@link refuseRetiredMember}: the entry is asked once, before any face is minted). Whether the ToolSpec arm can build the entry ({@link specArmCanConstruct}) * is asked of the entry once, there, and recorded on its face ({@link specArmCanBuild}); the mount refuses an entry it * cannot build from that record, so an `execute` that grows on the caller's object later is never read. The mount calls a * spec face's `execute` with the caller's own object as `this` ({@link callerEntryOrigin}). A `defineTool` product's face is * what every phase reads in its place, and the mount keeps the one authenticated rebind of the product object itself * ({@link rebindProductEntry}; the factory's seal refuses a product whose `execute` was replaced or which is not the object * it stamped): the factory rebuilds the product from its own members at the mount, and the run is refused when the rebuilt * product no longer answers as its face did. The tool a product mounts as is built from that face around the rebound * `execute`, never the rebuilt object itself (it shares list members and accessors with the caller's product): the * brand is asked once, here at the snapshot, and no object the caller can reach is ever a mounted tool. * * An array the caller changes after that instant is refused instead of run without the change: * {@link refuseLateRosterChanges}, called once at the end of prepare, re-reads the caller's `spec.tools` and refuses * the run with the declaration door's code (`config.tool_mount_denied`) naming every entry that is not, in order, the * object the snapshot read (a getter that hands back a fresh array of the same objects is not a change). A change to an * entry's members is not compared — the run never reads them again. A change after prepare returned is read by nothing. */ import type { AgentTool } from "../../internal/harness.js"; import type { TaskSpec } from "../types.js"; import { type CallerSpecRead } from "../task-spec-members.js"; import type { ToolSpec } from "../tool-spec.js"; import type { ToolDeclarationCode } from "../governance-codes.js"; import { type ToolCtxEnricher } from "../tools.js"; declare const ROSTER_FACE: unique symbol; /** A roster entry as the snapshot minted it. The brand is private to this module and {@link rosterFaceOf} is the only * function that returns one, so a phase that takes `RosterFace`s cannot be handed a live caller object: code that tries * to put one into the roster does not compile. */ export type RosterFace = ToolSpec & { readonly [ROSTER_FACE]: true; }; /** A task spec whose tool roster is faces — what the snapshot hands the config doors. */ export type CallerRosterSpec = Omit<TaskSpec, "tools"> & { tools?: RosterFace[]; }; /** The spec the doors rebind ({@link CallerRosterSpec}, its `durableApproval` the opt-in alone — the host's `false` is lifted off it into * the leg's one fact) — what the driver and every phase that reads the roster hold after the doors. */ export type RosterSpec = Omit<CallerRosterSpec, "durableApproval"> & { durableApproval?: import("../task-spec.js").DurableApprovalOptIn; }; /** What the refusal compares: the caller's own spec (re-read, never mutated here) and the objects the snapshot read * (`undefined` when the roster was not an array — an omitted roster snapshots as no entries, a non-array value is the * doors' to judge and is not compared). */ export interface CallerRosterSnapshot { readonly caller: TaskSpec; readonly handed: readonly unknown[] | undefined; } /** The object a snapshot face was read from (the entry itself when it was held as handed). */ export declare function callerEntryOrigin(entry: object): object; /** Can the mount's ToolSpec arm build this roster entry? Only the verdict recorded at the snapshot answers: an object that * is no face of this snapshot, a primitive, a product's face, is not built from. */ export declare function specArmCanBuild(entry: RosterFace): boolean; /** A roster entry claiming an engine contract its catalog did not mint for it — the id written by hand, on a spec or on a product — refused where the * declaration enters. */ export declare function callerEngineContractProblem(face: RosterFace): { code: ToolDeclarationCode; message: string; } | undefined; /** The retired-key door for one roster entry, asked of the LIVE entry once, before any member is copied: the key is present * iff ordinary property lookup answers a defined value ({@link retiredKeyOf} — an own enumerable member, a non-enumerable * one, a prototype's getter, a Proxy's trap alike), and a present one refuses the run by name with the sentence and code * the factory gives the same key on a `defineTool` spec (`config.deps_retired_key`). A lookup that throws is an entry whose * members cannot be read, refused as such. No face is ever minted from an entry that carries a retired member, so no later * phase has a key table of its own to consult for one. Every live object that is about to become a face meets it first: * {@link rosterFaceOf} asks it of each entry, and the per-task agents rebuild asks it of the object `withAgents` returned * before the stamp copy-back spreads that object (a spread drops a prototype's and a non-enumerable member). A value that * is no object holds no member. */ export declare function refuseRetiredMember(entry: unknown, index: number): void; /** One entry of the roster, and the ONLY place a {@link RosterFace} is minted. Every object entry (a function included) becomes a frozen face, so no entry the snapshot holds is a * live object: a primitive is immutable and stays as read; a `defineTool` product's face names the product for its sealed * rebind; an entry the ToolSpec arm cannot build gets a face too, and the mount refuses it from the verdict recorded here — * an `execute` or a mark that grows on the caller's object afterwards is never read. An entry carrying a retired member is * refused first ({@link refuseRetiredMember}). A read that throws refuses the run. */ export declare function rosterFaceOf(t: unknown, index: number): RosterFace; /** The mount's arm for a roster entry that is the face of a `defineTool` product: the exact product object is rebound to * the run's ctx through the factory's sealed seat, compared with its face ({@link refuseProductDrift}), and the tool the * run mounts is built from the face around the rebound `execute` ({@link mountedProduct}). `undefined` when the entry is * not a product's face (the mount's ToolSpec arm takes it). */ export declare function rebindProductEntry(entry: object, enrich: ToolCtxEnricher): AgentTool | undefined; /** Take the snapshot and rebind onto a private spec copy carrying it. The spec is taken through its one read * (`readCallerSpec` — the read the run took at its entrance, answered again for its snapshot): the read's snapshot is plain * own data, so the copy is that snapshot with `tools` replaced by its own frozen array of faces when the request carried one. * The caller's own object is kept for {@link refuseLateRosterChanges} only; the read's refusal is handed back for prepare's door. */ export declare function snapshotCallerRoster(handedSpec: TaskSpec): { spec: CallerRosterSpec; roster: CallerRosterSnapshot; refusal: CallerSpecRead["refusal"]; }; /** Refuse the run when the caller's `spec.tools` no longer holds exactly the objects the snapshot read, in order. */ export declare function refuseLateRosterChanges(roster: CallerRosterSnapshot): void; export {};