UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

127 lines (126 loc) • 9.25 kB
import type { TSchema } from "typebox"; import type { AgentTool } from "../internal/harness-types.js"; import type { ProtocolId } from "@sema-agent/primitives"; import { type PromptTraitName, type ToolCardFactName } from "./catalog/schema.js"; /** The protocol slice comes from the protocol table — adding a protocol extends ProtocolId and this * union follows automatically (the origin projection is table-driven, prepare-task). */ export type ToolOrigin = "core" | "caller" | "synthetic" | ProtocolId; /** The origin a declared contract id names: a `core.` id is one of the engine's own contracts, any other id a caller's. The one * reading of the prefix — the definition factory labels a descriptor by it, and the caller-tool door refuses a caller entry (a * spec's `contract`, a product's descriptor) that claims one the engine did not mint. */ export declare function contractOriginOf(contractId: string): "core" | "caller"; export declare function isEngineDescriptor(value: unknown): boolean; export interface ToolContractDescriptor { /** Stable contract identity, e.g. `core.read@2`. Versioned independently of the wire name. */ contractId: string; /** Declared behavior revision (protocol §7.1: declarative — hashing a JS closure proves nothing). */ implementationRevision: string; wireName: string; /** Live deployment aliases (ToolSpec.aliases) — dispatchable alternate wire names. Retired-name * replay aliases are gone (RB-476-A, 5.0.0): a persisted old name misses loudly instead. */ aliases?: string[]; origin: ToolOrigin; /** Structure-only schema digest (descriptions/titles/examples stripped) — presentation-invariant. */ shapeDigest: string; /** Full schema digest (incl. descriptions) of the model-visible wire face — the schema as `projectToolSchema` shows it. */ wireSchemaDigest: string; /** The presentation card in effect ({@link cardIdOf}): `builtin:<wireName>` for a declared contract's card, the * undeclared-contract prefix for any other tool's, and `#<variant id>` after it when the mount chose one of the * description's variants (`descriptionVariants`) over its unconditional text. */ cardId: string; } /** * Manifest v2 row (protocol §5.4) — {@link projectToolManifest}'s per-tool projection of * {@link ToolContractDescriptor} onto the wire-visible manifest plane. REF-D25 (2026-08): * this is an intentional projection, not a subset alias — it drops `origin` and normalizes * `aliases` from optional to required (empty array when there are none), so it cannot be * expressed as `Pick<ToolContractDescriptor, ...>`. `core/trace.ts` (TraceEvent's * `prompt.assembled` manifest) and `core/runner/prepare-task.ts` (`Prepared.promptManifest.tools`) * both reference this same type — the value flows prepare-task.ts → runtask.ts → trace.ts * untouched, so a shape drift between those two sites would make a real call-shape change * invisible to replay/audit (the same shapeDigest-collision class documented above on * `stripPresentation`). */ export interface ToolManifestRow { wireName: string; aliases: string[]; contractId: string; implementationRevision: string; cardId: string; shapeDigest: string; wireSchemaDigest: string; } export declare function computeShapeDigest(schema: TSchema | undefined): string; export declare function computeWireSchemaDigest(schema: TSchema | undefined, description: string): string; /** The facts a card mount reads ({@link TOOL_CARD_FACT_NAMES}; a card naming another fact is refused rather than read against a * value the mount does not have). */ export type ToolCardFacts = Readonly<Record<ToolCardFactName, boolean>>; /** * The judge of a caller roster's card declarations where the roster enters, every entry the caller handed, mounted or not: the * config-door phase (over the snapshot's faces) and a resume's screen before its claim (one function, so a resume's row stays * pending). A description variant table the engine cannot read is refused (`config.tool_face_invalid`); an entry still carrying * the retired `descriptionClassic` as plain data is refused by name (`config.deps_retired_key`) — the door and the sentence the * snapshot gives the key on the LIVE entry under every reading of it (a prototype's getter, a non-enumerable member), so a face * never carries one and the resume's screen is where this line fires. What the receipt cannot take without running the caller's * code (an accessor, a Proxy, an entry of another prototype, a non-enumerable member) is not judged here: a variant table is * judged again at the mount ({@link mountDescriptionVariant}), a retired member by the snapshot — on a resume, after the claim. */ export declare function assertToolCardDeclarations(tools: unknown): void; /** * The face a run mounts for a tool under its prompt traits and its card facts: the first of the tool's description variants * whose trait condition holds over the run's traits and whose fact condition holds over `facts` replaces its description (a * shallow copy carrying the chosen variant's id, so the card id reports it); none holds, or the tool declares none, ⇒ the card's * unconditional text. A face some run already mounted is judged from its card again, never from the text that run chose. The * selection reads traits and facts through the catalog's own readings ({@link traitsHold}, {@link factsHold}). A variant the engine cannot read refuses the task by name * (`config.tool_face_invalid`) — never mounted as if the tool had one form. One function ({@link descriptionVariantsProblem}) * judges a table at both places: {@link assertToolCardDeclarations} over the entries the receipt can read as data, and here over * every tool the run keeps after the exclusion valve — so a table the door could not read is judged when it is mounted. */ export declare function mountDescriptionVariant(tool: AgentTool, traits: ReadonlySet<PromptTraitName>, facts: ToolCardFacts): AgentTool; export interface AttachContractInput { contractId: string; implementationRevision: string; origin: ToolOrigin; aliases?: string[]; } /** Attach a contract descriptor to a materialized tool (factory exit point). Digests are computed * from the tool's REAL mounted schema/description, so they always reflect the wire face. A descriptor attached here is the * caller's word, whatever its `origin`: it is never the engine's mint ({@link attachEngineToolContract}). */ export declare function attachToolContract(tool: AgentTool, input: AttachContractInput): void; /** The factory's attach for a contract the engine's catalog minted: the descriptor is itself recorded as the engine's mint, frozen * (its alias list included), so a roster door can tell a product the engine built from a definition from one a host dressed in a * `core.` id. Internal — not exported from the package root. */ export declare function attachEngineToolContract(tool: AgentTool, input: AttachContractInput): void; /** The descriptor `tool` carries, as attached — no shape judgment and no copy. Internal: the caller-roster door reads the claim here. */ export declare function attachedContractOf(tool: object): ToolContractDescriptor | undefined; /** * Resolve a tool's descriptor. Undeclared tools get a deterministic legacy descriptor derived from * their shape (`legacy:<shapeDigest>`), origin per the mount channel — an external tool NEVER * inherits a core contract by name (§7.2: don't guess contracts from names). */ export declare function getToolContract(tool: AgentTool, fallbackOrigin?: ToolOrigin): ToolContractDescriptor; /** What a contract is read off — a mounted instance, or a task's own entry before it mounts (plus the descriptor it carries). */ export type ToolContractFace = Pick<AgentTool, "name" | "aliases" | "parameters" | "description">; /** {@link getToolContract} over a {@link ToolContractFace}, for the static mount plan's reading; not exported from the root. */ export declare function toolContractOfFace(tool: ToolContractFace, fallbackOrigin?: ToolOrigin): ToolContractDescriptor; export declare function hasToolContract(tool: AgentTool): boolean; /** Manifest projection for the final mounted tool set (manifest v2 `tools`, protocol §5.4). */ export declare function projectToolManifest(tools: readonly AgentTool[], originOf?: (tool: AgentTool) => ToolOrigin): ToolManifestRow[]; /** * RB-15: wire-identity collision audit over the FINAL mounted tool set. * Rules (plan §6.2 tool row): * - an ALIAS colliding with another tool's canonical name or alias = FATAL (silent misdispatch * of durable replays would follow — never ship such a mount); * - two tools sharing a CANONICAL name = the historic deliberate yield (a caller tool may shadow * a built-in; the harness Map keeps the later mount) — NOT fatal, but returned as an explicit * exemption record so the mount site can surface it (observable, never silent). */ export declare function auditToolCollisions(tools: readonly AgentTool[]): { yields: Array<{ name: string; }>; aliasCollisions: Array<{ alias: string; owners: string[]; }>; };