UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

190 lines (189 loc) • 14 kB
import { type RuleBehavior, type RuleToolNameVerdict } from "@sema-agent/primitives"; import type { PolicyNameLayer } from "./retired-tool-name-account.js"; /** * THE near-miss reading of a tool name the judgment calls current: the spelling the roster carries when it carries no tool of this * exact name but one that differs from it only by case (`BASH` beside `Bash`), else `undefined`. Two kinds of name are never a near * miss, because the roster they would be read against is not the final one: an engine tool's (the engine mounts some of its tools * after a list is read) and a protocol tool's (a refresh mounts and unmounts them while the run goes on) — in both the exact * spelling can be the right one by the time it matters. Every station that asks "is this name a case twin of a carried tool" reads * this answer: an entry list and the persisted grammar's whole-tool rule through {@link toolNameFaultOf}, and a tool policy's name * lists through {@link judgePolicyEntryName} (the prepare-time policy-name audit, which reads two rosters and skips an inherited layer). */ export declare function nearMissSpellingOf(name: string, roster: ReadonlySet<string>): string | undefined; /** What {@link toolNameFaultOf} finds wrong with a rule's tool name: the judgment's stale words (`tool_name`, with the verdict and * its facts), or another spelling of a carried tool (`near_miss`, the spelling to write). */ export type ToolNameFault = { readonly reason: "tool_name"; readonly verdict: Exclude<RuleToolNameVerdict["kind"], "current">; readonly guidance?: string; readonly currentName?: string; readonly ambiguous?: true; } | { readonly reason: "near_miss"; readonly currentName: string; }; /** * THE ONE reading of a rule's tool name against a roster — the name judgment's stale words, then, for a name the judgment calls * current but the roster does not carry exactly, whether the roster carries it under another case ({@link nearMissSpellingOf}). * `undefined` = no fault: the name is carried, or nothing carried is another spelling of it. A name the roster simply does not * carry is silent: a tool of that name may be mounted later, and a list shared across runs is not wrong because one run lacks a * tool. A list entry (`readRuleEntry`) and a stored whole-tool rule (`parseRuleText`) read a name through this and nothing else; * a tool policy's lists at prepare read the same two steps over two rosters through {@link judgePolicyEntryName}. */ export declare function toolNameFaultOf(name: string, roster: ReadonlySet<string>, behavior: RuleBehavior | undefined): ToolNameFault | undefined; /** * The names a tool policy's lists are read against at prepare: `known` the tools this run can CALL (the judgment's roster), `rootFacing` * what another spelling of an entry is asked of — the run's own tools, the caller's, the pools its delegation tools hand their children * and the engine's catalog — and `carrierOf` where a carried spelling is found (read back into the refusal's sentence). */ export interface PolicyNameUniverse { readonly known: ReadonlySet<string>; readonly rootFacing: ReadonlySet<string>; readonly carrierOf: (spelling: string) => "run" | "pool" | "catalog"; /** The reading of a retired name the run's match reads its policies under ({@link retiredNameView}): it alone says which entries read as a * successor. Absent ⇒ the view of `known`. */ readonly retiredNames?: RetiredNameView; } /** What a policy-list entry's name is when it is not clear and is not read as another name: a word of the name judgment, or another * spelling of a carried tool. */ export type StaleEntryName = Exclude<RuleToolNameVerdict, { readonly kind: "current"; }> | { readonly kind: "case_twin"; readonly twin: string; readonly carrier: "run" | "pool" | "catalog"; }; /** * What the prepare-time audit does with one entry of a tool policy's `deny` / `ask` / `allow` list ({@link judgePolicyEntryName}): * - `clear` — the name is none of the stale words; whether it matches a tool THIS run is the audit's separate advisory; * - `read_as` — a name the engine retired with a successor that the run's view reads as it (`universe.retiredNames`): no tool of the run, * nor of a pool it hands its children, answers to the old name — the entry reads as the successor (the run's policy match reads the * successor's calls under the old name too) and the operator is told once; * - `refused` — a name that reaches no tool and whose being left alone would WIDEN the surface (a retired name with a successor that only a * pool the run hands its children carries, an unprefixed namespace, an unmintable segment, a case twin of a carried tool): the run is * refused, `stale` says what to write; * - `advised` — the same on an `allow` list, where matching nothing grants nothing and the policy is narrower than written: the entry stays * and the person is told (a retired name the run does not read as a successor, with its retirement line; a name that reads as more than * one server's); * - `left_out` — a name the engine retired with no successor on a restricting list: it names no tool in any roster, now or later, so the entry * is left out and told once as the retired-name notice (`field` is the list, `guidance` the retirement table's line). */ export type PolicyEntryNameReading = { readonly kind: "clear"; } | { readonly kind: "read_as"; readonly successor: string; } | { readonly kind: "refused"; readonly stale: StaleEntryName; } | { readonly kind: "advised"; readonly stale: Extract<RuleToolNameVerdict, { readonly kind: "retired" | "unmintable_segment"; }>; } | { readonly kind: "left_out"; readonly field: "deny" | "ask"; readonly guidance: string; }; /** * THE reading of one entry of a tool policy's name lists, the one question the prepare-time audit asks of every entry of the run's own * policy, of the rules its session stored and of the rules an ancestor's session handed it: the name judgment (`judgeRuleToolName`) over * `known`, then — for a name it calls current, or a retired one with no successor on a restricting list — whether the root-facing universe * carries another spelling of it (a case twin, {@link nearMissSpellingOf}); an inherited layer (`ancestor_session_rules`) was asked of the * roster of the ancestor that wrote it and is not asked of this leg's again. The answer is what the audit DOES with the entry, so no station * keeps a second criterion for it. * * Which station reads which: an entry list (`readRuleEntry` / `compileAllowList` — a task's tool-name lists, a skill's table, a definition's * denied tools) and a stored whole-tool rule (`parseRuleText`) read one roster through {@link toolNameFaultOf}; a tool policy's lists at * prepare read this, over two rosters (an entry list's single roster cannot say whether a case twin is the run's own tool, a delegation pool's * or the catalog's). All three answer from `judgeRuleToolName` and {@link nearMissSpellingOf}; only the disposition differs. */ export declare function judgePolicyEntryName(name: string, list: RuleBehavior, universe: PolicyNameUniverse, layer: PolicyNameLayer): PolicyEntryNameReading; /** The two kinds of tool-name miss — a closed set. `unknown`: the name is none the engine ever mounted (a typo, a * guess). `expected-absent`: the name is one the engine RETIRED (`RETIRED_TOOL_NAMES`), so its absence is no * surprise and the table says what replaced it (the reference spells the same two words for its render-time miss) — * or one the leg itself left out and says why (`AgentLoopConfig.absentToolReason`: a shell tool its start's reading left out). */ export declare const TOOL_MISS_KINDS: readonly ["unknown", "expected-absent"]; declare const UNKNOWN_MISS: "unknown", ABSENT_MISS: "expected-absent"; /** What {@link judgeToolNameMiss} answers: the kind (a {@link TOOL_MISS_KINDS} word) and, for `expected-absent`, the clause. */ export type ToolNameMiss = { readonly kind: typeof UNKNOWN_MISS; } | { readonly kind: typeof ABSENT_MISS; readonly detail: string; readonly successor?: string; }; /** * Fallback classifier for "a call named `name` matched nothing in `roster`" (the loop checks `config.absentToolReason` first, * falling back here when it returns `undefined`): `expected-absent` (with the model-facing `detail`) for a retired name, * `unknown` for everything else. It reads the table through `judgeRuleToolName`, so a rule's and a call's tool names share * one reading. The detail is the successor (`renamed to "<name>"`) when the table names one AND this run mounts it — a * successor the roster lacks (plan mode, no shell) is named as unavailable, never offered as the way forward, and is not * returned as `successor` — else the table's own line (`folded into …` / `removed — …`), the grammar the table states. */ export declare function judgeToolNameMiss(name: string, roster: ReadonlySet<string>): ToolNameMiss; /** * THE reading of a rule's retired tool name: the successor the judgment names (`judgeRuleToolName` → `retired` with a `currentName`) * when `roster` carries no tool of that name — a rule written with the old name reads as if the successor were written — else * `undefined` (the name reads as written: a roster that carries it wins, and a name with no successor stays a retired name). */ export declare function successorReadingOf(name: string, roster: ReadonlySet<string>): string | undefined; /** One leg's reading of every retired name with a successor ({@link retiredNameView}): the names each successor answers to in a rule. */ export interface RetiredNameView { /** The successor a written name reads as, or `undefined` (as written). */ readonly successorOf: (name: string) => string | undefined; /** The retired names a call of `successor` answers to in a rule's match (none for any other tool). */ readonly formerNamesOf: (successor: string) => readonly string[]; } /** The view of a roster that carries every retired name: nothing is read as another name. */ export declare const NO_RETIRED_READING: RetiredNameView; /** The view one leg reads its rules through — {@link successorReadingOf} asked once of every row of the retirement table against the * leg's roster (every name and alias its tools answer to). Computed once per leg; the readers of a rule's name take it as given. */ export declare function retiredNameView(roster: ReadonlySet<string>): RetiredNameView; /** A tool as a leg's view reads it: its names, and the pool a delegation tool hands its children (whose names the view reads too). */ type LegTool = { readonly name: string; readonly aliases?: readonly string[]; readonly agentToolPool?: ReadonlyArray<{ readonly name: string; readonly aliases?: readonly string[]; }>; }; /** The view where no leg's roster is in reach (a disclosure made before activation, a reading taken for the stricter answer): no tool * carries a retired name, so every one with a successor is read as it. */ export declare const ROSTERLESS_RETIRED_VIEW: RetiredNameView; /** THE view a leg reads its rules under — {@link retiredNameView} of every name its caller tools answer to and of every name a pool its * delegation tools hand its children answers to (only a caller's tool can answer to a retired name; the engine's and the protocol tools * never do). A run's start and a host's static plan of the same spec read through this one function. */ export declare function legRetiredNameView(tools: readonly LegTool[]): RetiredNameView; /** A rule entry as `view` reads it: an entry the rule syntax reads (`Tool`, `Tool()`, `Tool(content)`) whose tool name the view reads as * its successor is spelled with the successor (`Task(x)` ⇒ `Agent(x)`); every other entry, and a value that is not a string, as given. */ export declare function ruleEntryUnderView(entry: unknown, view: RetiredNameView): unknown; /** A list of rule entries as `view` reads it ({@link ruleEntryUnderView}); the same value when no entry is read otherwise, and a value * that is not a readable list as given (its judge refuses it in its own words). */ export declare function ruleListUnderView<T>(value: T, view: RetiredNameView): T; /** A selector layer's two lists under `view` ({@link ruleListUnderView}); the same layer when neither reads otherwise. */ export declare function selectorLayerUnderView<L extends { readonly allow?: unknown; readonly deny?: unknown; }>(layer: L, view: RetiredNameView): L; /** A call as a policy's name match reads it under `view`: the names it answers to and, after them, the retired names any of them * answers to in a rule (an alias of a successor included). The call's face and binding are not changed — the old names ride beside * it, read by rule-name matches only ({@link withFormerRuleNames}). */ export declare function callUnderView<C extends { readonly toolName: string; readonly face?: { readonly names?: readonly string[]; } | undefined; }>(call: C, view: RetiredNameView): C; /** A mod handler's matcher as `view` reads it: a `tool` field written with a retired name the view reads as its successor is that * successor (the reference reads a hook matcher's old name as the current one through `rd` (cli321.js:288377), `rd` (cli321.js:291968)); * every other field, and every other matcher, as written. */ export declare function matcherUnderView<M extends Readonly<Record<string, unknown>>>(matcher: M, view: RetiredNameView): M; /** The names a run mounts — every tool's primary name and its aliases: the roster `judgeRuleToolName` is given. */ export declare function mountedToolNames(tools: ReadonlyArray<{ readonly name: string; readonly aliases?: readonly string[]; }>): Set<string>; export {};