UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

283 lines (282 loc) • 25.2 kB
/** * THE judgment of every tool-name list one prepared task receives — the run's own selector lists (`TaskSpec.allowTools` / * `TaskSpec.excludeTools`; when the delegation lane spawned it, its spawning run judged them where they were mounted, and the evidence * it handed down spares them here), the definition a workflow agent was launched as (`RunInternals.definitionToolLists`: a layer of * its selector, judged here as a definition's lists are), the definitions it carries (`TaskSpec.agents`), and the definitions and tool-level lists a mounted * delegation tool carries (the `authored` faces — the engine's built-ins are not judged; only a tool a deployment mounted (depth 0) * publishes them). One rule for both sides: a value that is not a list, an entry that is not a string, a spelling that can name no tool * and a spelling that is another spelling of a tool the list's universe carries (`BASH` beside `Bash`) are REFUSED — but a spelling that can * name no tool in a DEFINITION's list (`TaskSpec.agents`, an authored face) is TOLD instead (one account per list, naming its first such entry, `config.agent.unknown_tool`, * `detail.reason: "grammar"`; the person who wrote a request's definition hears it too): a definition is written where the run's caller often * cannot reach it, so one entry of it does not take the run down — the delegation to that definition is refused where it is used * ({@link toolNameListFlaw}); an entry whose * name no tool of the universe answers to (a server this deployment does not declare, a misspelt name) is KEPT as written — the * selector reads every entry by name, so a denied one hides a tool of that name whenever one is mounted and an allowed one admits * it — and told to the host, one account per entry. And one contradiction, judged on every leg: a denylist (the run's own or one * of its ceiling) that reaches a tool the leg's kind relies on (the catalog's protocol column) is refused — neither a silently * ignored deny nor a silently crippled leg. * * Two kinds of list, two universes. A ROOT-facing list bounds the tools of the run that holds it: the run's own lists (also the * ceiling of every child), the policy name audit and the session-rules write gate ask the same question of it. Its universe is the * run's roster before the valve, the caller's own tools, every tool the catalog describes (`rootFacingToolNames`) and the exact * names of the pools its reachable delegation tools declare and can list — and another spelling of a carried name is always refused, * whatever a delegation tool injects per spawn. A CHILD-facing list bounds a child that does not exist yet: a definition's two * lists and a delegation tool's two lists. Its universe is the pool the delegation tool that hands the child declares (names, aliases, the * declared `extraToolNames`, and the tool's own name: the child is handed the tool again, rebuilt, while its depth allows) and the catalog — not the * spawning run's roster, which the child is not handed; every definition is judged against each reachable delegation tool's own pool; a delegation tool whose factory * injects tools per spawn with no declared roster lists none of its pool, so there another spelling is kept and told (it may be one * of that pool's own). Only the delegation tools the run's own selector leaves are read, in both: a tool it takes out hands nobody * anything. A layer a run inherits (its ceiling) is not asked again: the run that wrote it asked its own universe, and a layer a * checkpoint row restored is read for its spelling before the claim. The same goes for the lists a delegation tool hands its child * (the child's own): when the spawning run read them against a pool it could list, its evidence spares them in the child. When it * could not (the keep above), the child is the run whose roster can answer — it refuses another spelling of a tool of its own * universe (its roster, the catalog, the pools it can list, and the delegation tool that spawned it, under its exact mounted name: a name * of the child's lineage, mounted at its depth or not — an exact hit of that name is the tool's own, whatever else reads as a twin of it), * and the spawning run already told what it kept, so nothing is told a second time. * * Throws `config.tool_face_control` for the run's own lists and the protocol contradiction, `config.agent.unknown_tool` for a * definition's or a delegation tool's lists (`detail.reason`: `unknown_name` for a spelling no tool name can have in a delegation tool's * tool-level list, `near_miss` for another spelling of a tool of the universe; a spelling no tool name can have in a DEFINITION's list * is `grammar`, told here and refused at the delegation to it) — synchronous, like the phase that calls it. */ import type { AgentTool } from "../../internal/harness.js"; import { valveLeaves, type ToolNameListSide } from "../../agents/tool-filter.js"; import { type EngineNotice } from "../engine-notice.js"; import { type ProtocolId } from "@sema-agent/primitives"; import { type RetiredNameView } from "../tool-name-aliases.js"; import { POLICY_NAME_REFUSAL_CODE, type PolicyNameLayer } from "../retired-tool-name-account.js"; import { type DefinitionEntryReason } from "../definition-entry-grammar.js"; import type { ToolLegKind, ToolSelector, ToolSelectorLayer } from "../tool-face.js"; import { type ConstraintChainEntry } from "../tool-policy.js"; import type { TaskSpec } from "../types.js"; import type { CheckpointErrorReason } from "../checkpoint-store.js"; import type { RunInternals } from "./contracts.js"; import { type ConfigRefusalInput } from "../config-refusal-input.js"; import { type AnnounceOnceLedger } from "./announce-once-ledger.js"; /** What the delegation tools a run mounts hand their children (the pools of {@link delegationPoolReading}). */ interface DelegationPoolReading { readonly tools: NonNullable<TaskSpec["tools"]>; readonly names: Set<string>; readonly complete: boolean; } /** * THE judgment of the name a delegation tool is mounted under: the name it declares for itself (`createSubagentTool({ name })`, the default * name when none is given), which the factory binds to the `execute` that carries the delegation out ({@link declaredDelegationNameOf}) — * so a copy of the tool, a product of it, a stripped or shell copy all still say it. A delegation tool is ONE tool with ONE name: the lists * of its agent definitions, the copy of it a child is handed, the rebuilt copy a run's own definitions produce and the prompt text it speaks * all say the declared name, while the roster, the pool reading and the model's call say the mounted one. A mount whose `name` differs from * the declared one makes the tool answer to two names — a denied list that names one of them is read against the other — so it is REFUSED, * naming both names and the two ways to one name. This is the sentence of that refusal for the first such entry of the rosters given — or * `undefined`; it never throws (an entry whose members cannot be read is not judged here: the roster snapshot refuses it). A tool the * factory did not make, and a roster that is no list (the doors' to judge), are not read. */ export declare function renamedDelegationMountSentence(...rosters: unknown[]): string | undefined; /** * The refusal of a renamed delegation mount at prepare (`config.tool_face_control`, `detail.reason: "renamed_mount"`), asked of every roster * the run holds: the one the caller mounted and the one the per-task rebuild (a function the caller's tool supplies) handed back. A resume * asks the roster it is handed once more before the claim (this same function, through the resume screen's config-refusal channel), so the row stays pending. */ export declare function refuseRenamedDelegationMounts(...rosters: unknown[]): void; /** * THE reading of what the delegation tools a run mounts hand their children, read after the run's own tool valve: only a delegation * tool the valve leaves (`leaves`, the valve's one reading — `valveLeaves`) counts — one it takes out hands nobody anything, and neither * its pool names nor an unlistable pool of it belong to any universe. The tools themselves, every pool tool's name and alias (a declared * `extraToolNames` roster included), and whether every one of those pools can be listed: a delegation tool whose factory injects tools * per spawn with no declared roster has no listable pool (the static `tools` it was given included), so a name outside the listed ones * may be one of its own. This is the one place the pool side of both universes is defined; the list judgment below and the policy name * audit ask it. */ export declare function delegationPoolReading(callerTools: NonNullable<TaskSpec["tools"]>, leaves: ReturnType<typeof valveLeaves>): DelegationPoolReading; /** The arms of this module's `config.tool_face_control` refusal that a resume carries before the claim as `detail.reason` of a `checkpoint.config_refused` — the same words * ({@link CheckpointErrorReason}): a list the row recorded that is not a list / has a member that is not a string / has a spelling no tool name can have / is not in the * spelling a row records, a record that is not one, a renamed delegation mount, an occupied table of declared names. */ export type PersistedToolFaceReason = Extract<CheckpointErrorReason, "not_a_list" | "not_a_string" | "unknown_name" | "unsupported_pattern" | "invalid_record" | "renamed_mount" | "registry_occupied">; /** One entry the policy-name audit refuses, in the shape the entry problems of a rejection body are minted in (code · field · * entry · reason, the engine entry reading's words) plus the layer it sits in: `field` is the policy list (`deny` / `ask` / * `allow`), `reason` the name judgment's verdict word (`near_miss` for another spelling of a carried name). */ export interface PolicyNameProblem { readonly code: typeof POLICY_NAME_REFUSAL_CODE; readonly field: "deny" | "ask" | "allow"; readonly entry: string; readonly reason: "retired" | "unprefixed_namespace" | "unmintable_segment" | "near_miss"; readonly layer: PolicyNameLayer; } /** A refused entry with the sentence that names it. */ export interface PolicyNameFinding { readonly message: string; readonly problem: PolicyNameProblem; } /** The refusal of a policy layer whose name lists hold entries that reach no tool ({@link POLICY_NAME_REFUSAL_CODE}): the sentence is * the first entry's, `detail.problems` lists the refused entries of the layer in scan order (within the bounds above), `detail.total` * counts every one. */ export declare function policyNameRefusal(first: PolicyNameFinding, more: readonly PolicyNameFinding[]): Error; /** The arms of {@link toolNameListFlaw}: `detail.reason` of the refusal of a delegation to a definition whose list cannot be read. */ export type DefinitionListFlawReason = "not_a_list" | "not_a_string" | DefinitionEntryReason; /** * What stops a DEFINITION's list from being delegated to — the one reading the spawn-time check of a delegation tool (no prepare may * have run: a tool built by hand, a direct `execute`) and a workflow agent's launch share with the judgment before the run: the * sentence and the arm (`detail.reason`) of the first structural flaw (not a list, not a string), else of the first entry no tool name * can be (`grammar`: the definition cannot be delegated to) — or `undefined`. `withGrammar` is false for a list that bounds the run itself * (a delegation tool's tool-level list: its entry refuses the run at prepare and the child's own prepare reads it again), so only the * structural flaws are asked of it. */ export declare function toolNameListFlaw(where: string, side: ToolNameListSide, value: unknown, withGrammar: boolean): DefinitionListFlaw | undefined; /** The sentence and the arm of a refused delegation to a definition ({@link toolNameListFlaw}). */ export interface DefinitionListFlaw { readonly message: string; readonly reason: DefinitionListFlawReason; } /** Whose lists a workflow agent's definition layer is ({@link RunInternals.definitionToolLists}), in the words every sentence about them uses. */ export declare function workflowAgentTypeWhere(agentType: string): string; /** * The launch of a workflow agent as an agent type is the use of its definition, so it asks the definition's two lists what `Agent`'s spawn * asks them ({@link toolNameListFlaw}, allowed list first): the first one that cannot be delegated to — not a list, a member that is not a * string, an entry no tool name can be — refuses the launch, thrown as `config.agent.unknown_tool` with the arm as `detail.reason` (the * delegation lane answers the same flaw with a not-started result of the same code and arm). The lists' names are the leg's prepare's to * judge, once, with the leg's roster. */ export declare function refuseUnusableDefinition(agentType: string, lists: { readonly allowTools?: unknown; readonly denyTools?: unknown; }): void; /** * Whose lists a run's own layer are, in the words every sentence about them uses: the delegation tool (and agent definition) that wrote * them when the run records one (`RunInternals.ownToolLists`), else `undefined` — the run's own `TaskSpec` seats. */ export declare function ownListsWhereOf(ownBy: RunInternals["ownToolLists"]): string | undefined; /** * THE build of a leg's tool selector: `layers` are the ceiling it was spawned under, then its own lists (the last is its own), and * `legs` the kinds of leg it is. Every layer is read by the one layer reader ({@link readSelectorLayer}), once, whoever judged it and * whatever evidence the run carries — a live ceiling and the run's own lists were judged by the engine that wrote them, which may have * read what this one cannot (a list that is not one, a member that is not a string, an entry no rule syntax reads), so the leg does not * start without a layer that says something. The selector is compiled from what was read, never from the layers again: a getter that * answers differently the second time is not asked a second time, and the selector's layers (handed to the leg's children as their * ceiling) are the frozen product. Built before anything reads it, and before the session is acquired, so a refusal needs nothing given * back. `where` is whose lists the own layer is ({@link ownListsWhereOf}). (A ceiling a resumed row restored was read by the same reader * before the claim — `persistedToolNamesRefusal`.) `spec` carries the run's whole-face switch: the own layer is read as written first * (a malformed allowlist beside the switch refuses here as it does without it), and only what was read becomes the layer the switch * empties ({@link ownSelectorLayer}). `inputs`, when a leg's prepare hands them, are the input each layer came from (in the order of * `layers`): each layer is read inside an entrance with its own word, so a layer that cannot be read is about the input that wrote IT * (the leg's own lists are its request's even under a ceiling); a static plan or a library read hands none. */ export declare function readToolSelector(input: { readonly layers: readonly unknown[]; readonly inputs?: readonly ConfigRefusalInput[]; readonly where: string | undefined; readonly legs: readonly ToolLegKind[]; readonly spec?: { readonly excludeAllTools?: true; }; /** The leg's reading of a retired tool name (`retiredNameView`): the leg's own lists are read under it — a layer above them was read * under the reading of the leg that wrote it and is taken as read. Absent ⇒ every name as written. */ readonly retiredNames?: RetiredNameView; }): ToolSelector; /** The digest of the lists a record of the delegation tool that wrote a leg's own lists carries, or `undefined` when it records none or * is not one ({@link readOwnToolListsRecord}): the fingerprint the resume ladder compares a row's record and the live seat's by. */ export declare function ownToolListsDigestOf(value: unknown): string | undefined; /** * THE read-back of the tool names a checkpoint row carries, before the claim: the ceiling the resumed leg will run under when it is * the row's (`ceiling`: absent when a live seat hands one) — each layer read by {@link readSelectorLayer} as a recorded layer — and * the frozen policy chain (`inheritedGate.constraintChain`; each component's members {@link projectionToolNames} names), whose * replay matches a name as recorded, which is the rule syntax's reading only for a name in its canonical spelling. The refusal * sentence of the first entry neither reading may be applied to, naming it and where it sits, or `undefined`; a member that is not * a list, or an entry that is not a string, is named by its type only. And the delegation tool the row records as having written the * leg's own lists (`ownLists`, absent when a live seat hands one): it joins the universe those lists are asked of, so it must be a name. * The refusal is its sentence and the arm it is ({@link CheckpointErrorReason}); {@link refusePersistedToolNames} throws it as prepare's own refusal. */ export declare function persistedToolNamesRefusal(ceiling: unknown, chain: readonly ConstraintChainEntry[] | undefined, ownLists?: unknown): { readonly message: string; readonly reason: PersistedToolFaceReason; } | undefined; /** {@link persistedToolNamesRefusal} as prepare's own refusal (`config.tool_face_control`, the arm as `detail.reason`): what a resume's pre-claim screen throws through the resume's one refusal channel. */ export declare function refusePersistedToolNames(ceiling: unknown, chain: readonly ConstraintChainEntry[] | undefined, ownLists?: unknown): void; /** * The upstream judgment of a run's own lists, when it holds: the evidence digest the spawning run handed down (live) or the resumed row * recorded, returned only when it equals the digest of the lists this prepare read — the evidence is bound to the lists it was minted * for, so a resume that hands different lists is judged again. The value returned is what this leg's rows record. */ export declare function boundToolListsJudgment(evidence: string | undefined, own: { readonly allow: readonly string[] | undefined; readonly exclude: readonly string[] | undefined; }): string | undefined; /** * The record of the delegation tool that wrote a leg's own lists, held only for the lists it wrote: the live seat's (the delegation lane * mints it with the digest of exactly the lists it handed the child) or, with none, the resumed row's. A prepare whose own lists digest to * the record's `lists` keeps it — they are the lists that tool wrote, so their refusals name it and its name is a name of the leg's * lineage; any other lists (a resume whose host handed another table) are the host's own `TaskSpec` seats, and the leg asks them as such * with no lineage name cleared. A record that is not one (a host that set the live seat itself) refuses the leg, as a row's does before the * claim: `config.tool_face_control`, `detail.reason: "invalid_record"`. */ export declare function boundOwnToolLists(record: unknown, own: { readonly allow: readonly string[] | undefined; readonly exclude: readonly string[] | undefined; }): RunInternals["ownToolLists"]; export interface RunToolListsInput { /** borrowed-readonly — the REBOUND spec: `agents` (the task's definitions) and `tools` (the caller's tools and the mounted delegation tools; the selector's valve decides which of them are left — under the whole-face switch, none). */ spec: Pick<TaskSpec, "agents" | "tools">; /** borrowed-readonly — the assembled mount array BEFORE the selector's valve (names + aliases = the roster half of the universe). */ tools: ReadonlyArray<AgentTool>; /** borrowed-readonly — the peers the task declares, per protocol of the protocol table (its MCP servers, its A2A peers): a namespaced * spelling under one names a tool of it, whether or not the peer answered with any tool. */ declaredPeers: { readonly [P in ProtocolId]: ReadonlyArray<{ readonly name: string; }> | undefined; }; /** borrowed-readonly — the run's own lists (the selector's own layer), as the doors froze them. */ own: { readonly allow: readonly string[] | undefined; readonly exclude: readonly string[] | undefined; }; /** borrowed-readonly — the run's selector, as {@link readToolSelector} built it: every layer of it (the ceiling, then its own) already read, and judged here as read. */ selector: ToolSelector; /** The kinds of leg the run is (the ones the selector was built for). */ legs: readonly ToolLegKind[]; /** borrowed-readonly — the leg's one reading of a retired tool name in a rule (`TaskStartDoors.retiredNames`): the run's own lists are * judged under it; a list written for a child is judged under the view that child's start computes of its pool. */ retiredNames: RetiredNameView; /** True ⇔ the spawning run's prepare judged exactly these own lists where they were mounted, against a pool it could list (as the * child-facing lists of the delegation tool or definition that wrote them) — the evidence digest it handed down (live, or recorded * on the resumed row) equals the digest of the lists this prepare read ({@link boundToolListsJudgment}). They are then not judged * again: neither told nor refused. */ judgedUpstream: boolean; /** True ⇔ a delegation lane spawned this leg inside a prepared run, which read these own lists against a pool it could not list: it * kept what it could not judge and told it (no evidence digest was handed down). They are judged here, against this leg's roster, * and refused as any own list is — but nothing is told a second time. Absent ⇒ nobody told. */ toldUpstream?: boolean; /** The delegation tool (and the agent definition) whose lists the run's own are, when the run that spawned it wrote them * (`RunInternals.ownToolLists`); absent ⇒ the run's own `TaskSpec` seats. Its sentence is {@link delegationListWhere}; its tool name is * a name of the leg's lineage, so the run's own lists are asked of a universe that carries it. */ ownBy: RunInternals["ownToolLists"]; /** The agent type the workflow lane launched the leg as, when its definition writes tool lists (`RunInternals.definitionToolLists`): the * selector's layer just above the leg's own is that definition's, as {@link readToolSelector} read it. Absent ⇒ no such layer. */ definition?: { readonly agentType: string; } | undefined; /** borrowed-readonly — the once-per-session ledger whose operator sink carries the advisories and whose verb tells the retired-name notice once. */ onceLedger: Pick<AnnounceOnceLedger, "onError" | "oncePerSession">; /** borrowed-readonly — the deployment's notice sink (`RunnerDeps.onNotice`): the retired-name notice goes there. */ onNotice?: ((notice: EngineNotice) => void) | undefined; /** borrowed-readonly — the acquired session's id (operator-line context). */ sessionId: string; } export declare function judgeRunToolLists(input: RunToolListsInput): void; /** * THE judgment of a tool ceiling a HOST wrote, asked of the spec of the leg it bounds before that leg starts: one workflow agent's * (`RunWorkflowOptions.toolCeiling`, the default `at`), or a root run's (`RunInternals.toolCeiling` handed to `runTask` by a host — * the task-start doors pass that seat and the leg's own kinds). * A ceiling a prepared run hands down was judged where it was written and is not asked again; one a host wrote was judged nowhere, and * it bounds the tools of every leg that holds it — a root-facing list: it is read against the catalog, the leg's caller tools and the * listable pools of the delegation tools the leg's own selector leaves (the layers above the host's — a launching run's —, the host's * and the leg's own lists together: a tool they take out hands nobody anything), and another spelling of one of those is refused (`config.tool_face_control`, * `detail.reason: "near_miss"`) rather than kept as a deny that hides nothing. A name no tool answers to stays kept (a tool of that * name may be mounted later), unannounced: the host wrote no spec this leg could name the list of. */ /** A host-written ceiling is a list of layers: anything else (a string, a lone layer) refuses, naming the seat — the container only, * so no layer's list is read here. */ export declare function refuseNonListToolCeiling(ceiling: unknown, seat: string): asserts ceiling is readonly unknown[]; export declare function judgeHostToolCeiling(layers: readonly ToolSelectorLayer[], agent: Pick<TaskSpec, "tools" | "allowTools" | "excludeTools" | "excludeAllTools">, above?: readonly ToolSelectorLayer[], at?: { readonly seat: string; readonly legs: readonly ToolLegKind[]; }): void; export {};