UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

280 lines (279 loc) • 18.5 kB
import type { ToolCallPathTarget, ToolCatalogEntry, ToolMountTag } from "./tool-face.js"; /** The closed set of facts a mount row may read. */ export declare const TOOL_MOUNT_FACTS: readonly ["runner.self", "leg.parentNotify", "leg.peerLane", "leg.taskNotification", "leg.session", "leg.backgroundAgents", "leg.startersWithheld", "env.hands", "env.performsIo", "env.modelShell", "env.backgroundShell", "env.scheduler", "spec.enableBlockedReport", "spec.planMode", "spec.interactiveTools.on", "spec.interactiveTools.off", "spec.planFile", "spec.selfOrchestration", "spec.outputSchema", "spec.handsReadOnly", "spec.runLedger", "spec.mcp", "spec.advisorModel", "spec.selector", "seat.checkpoint", "seat.workflows", "seat.offload", "seat.offloadBytes", "seat.lsp", "seat.artifact", "seat.question", "seat.sharedMemory", "seat.memoryEngine", "seat.peerTasks", "caps.allowWorkflows", "model.gate", "listing.skills", "listing.mcp", "listing.mcp.resources", "listing.a2a", "artifact.verdict", "advisor.route", "lsp.readFace", "names.declared", "names.occupied.sharedMemory", "names.occupied.memoryEngine", "names.deferred", "names.retracted"]; export type ToolMountFact = (typeof TOOL_MOUNT_FACTS)[number]; /** The name identity a mount judgment reads: a name and the spellings that dispatch to it. */ export interface ToolMountIdentity { readonly name: string; readonly aliases?: readonly string[]; } /** The atomically-mounted groups one late judgment may take back off the roster (the memory pairs). */ export type ToolMountRetractable = "sharedMemory" | "memoryEngine"; /** A row's condition. Each leaf names the fact it reads; the composites are Kleene's. */ export type ToolMountCond = { readonly fact: ToolMountFact; } /** The task's tool valve leaves this identity mounted (`spec.selector`). */ | { readonly valve: string; readonly aliases?: readonly string[]; } /** A caller tool declares this name (`names.declared`); `viaAlias` also counts a caller alias. */ | { readonly declared: string; readonly viaAlias?: true; } /** The model's class gate keeps this caller tool (`model.gate`). */ | { readonly kept: string; } /** The late judgment took this group back (`names.retracted`). */ | { readonly retracted: ToolMountRetractable; } /** Another row's verdict. */ | { readonly row: string; } | { readonly not: ToolMountCond; } | { readonly all: readonly ToolMountCond[]; } | { readonly any: readonly ToolMountCond[]; }; /** The phases of a prepare, in the order it reaches them. */ export declare const TOOL_MOUNT_SEGMENTS: readonly ["caller", "synthetic", "workflow", "output", "protocol", "hands", "delegation", "question", "scheduler", "lsp", "artifact", "context", "advisor", "engine"]; export type ToolMountSegment = (typeof TOOL_MOUNT_SEGMENTS)[number]; /** One catalog row: the definition `id`, the phase that mounts it, the tag its mount carries, and its condition. */ export interface ToolMountRule { readonly id: string; readonly segment: ToolMountSegment; readonly tag: ToolMountTag; /** The condition the mount site asks. */ readonly when: ToolMountCond; /** Mounted on the engine lane: the tool valve does not apply to it. */ readonly engineLane?: true; /** The group a late judgment may take this definition back with. */ readonly retractable?: ToolMountRetractable; /** The definition's calls run through the execution environment whatever its condition reads (its body reaches the * environment, or the environment executes it): the ledger's env-hand roster is this column over the rows a leg judged * mounted, and the mount plan counts the member as a consumer of the environment. */ readonly envHand?: true; } /** The member families no catalog row names. */ export declare const TOOL_MOUNT_FAMILIES: readonly ["caller", "mcp", "mcp-resources", "a2a"]; export type ToolMountFamily = (typeof TOOL_MOUNT_FAMILIES)[number]; /** The rows, in prepare order. */ export declare const TOOL_MOUNT_RULES: readonly ToolMountRule[]; /** * The tools the task's interactive-tool switch (`TaskSpec.interactiveTools`) governs: the names of the definitions whose mount row * reads it, read off {@link TOOL_MOUNT_RULES} (a row that reads another row that reads it counts), each name once, in the table's * order. `false` keeps every one of them off the roster; `true` mounts each where the rest of its row holds. A row added to the * table that reads the switch adds its tool's name here, with no edit. */ export declare const INTERACTIVE_TOOL_NAMES: readonly string[]; /** The names (aliases too) of the definitions whose row cannot hold while `fact` reads false, read off {@link TOOL_MOUNT_RULES}: the * tools that fact alone keeps off a leg. */ export declare function namesNeedingFact(fact: ToolMountFact): ReadonlySet<string>; /** The catalog definition behind a row (every row names one). */ export declare function mountRuleEntry(rule: ToolMountRule): ToolCatalogEntry; /** The names a retractable group's definitions answer to (the group whose names the leg reads as occupied before it mounts). */ export declare function mountGroupNames(group: ToolMountRetractable): readonly string[]; /** The row for a catalog id, or undefined when no row mounts that definition (a scenario definition a host mounts as its own). */ export declare function mountRuleOf(id: string): ToolMountRule | undefined; /** A three-valued verdict and the facts that decided it (the open ones, when unknown). */ export interface ToolMountVerdict { readonly value: boolean | "unknown"; readonly why: readonly ToolMountFact[]; } /** What a reader of the rows can read: each answer is the fact's value, or `undefined` when it cannot be read. */ export interface ToolMountFactSource { fact(f: ToolMountFact): boolean | undefined; valve(identity: ToolMountIdentity): boolean | undefined; declared(name: string, viaAlias: boolean): boolean | undefined; kept(name: string): boolean | undefined; retracted(group: ToolMountRetractable): boolean | undefined; } /** Kleene evaluation of one condition; `row` answers a cross-row reference. */ export declare function evaluateMountCond(cond: ToolMountCond, src: ToolMountFactSource, row: (id: string) => ToolMountVerdict): ToolMountVerdict; /** * Whether row `id`'s definition ends up on the roster (its condition, the valve, its group's retraction), over facts a * phase states before the roster exists. A fact the row reads and the phase never stated is a fault, never a guess. */ export declare function mountRowVerdict(id: string, facts: Partial<Record<ToolMountFact, boolean>>, leaves: (identity: ToolMountIdentity) => boolean): boolean; /** The member sources a plan names. */ export type ToolMountSource = "builtin" | "synthetic" | "scenario" | "mcp" | "a2a"; /** One tool a plan names. */ export interface ToolMountPlanMember { readonly name: string; /** The catalog definition, when one is behind the member. */ readonly id?: string; readonly source: ToolMountSource; readonly mountedBy: ToolMountTag; readonly state: "mounted" | "conditional"; /** For a conditional member: the facts that decide it. */ readonly when?: readonly ToolMountFact[]; } /** A listing whose members are only known live. */ export interface ToolMountPlanFamily { readonly source: "mcp" | "a2a"; readonly peer: string; readonly state: "conditional"; readonly when: readonly ToolMountFact[]; } /** A definition that does not mount, and the facts that keep it off (a caller's own tool carries no catalog id). */ export interface ToolMountPlanAbsent { readonly id?: string; readonly name: string; readonly because: readonly ToolMountFact[]; } /** * The tools a leg mounts, read off the mount table. `static` is the deployment's reading before any task (a fact it * cannot read leaves the member conditional); `effective` is a leg's own, after its prepare: every member mounted, and * its members are the leg's roster. `executionEnv` is whether the leg needs an execution environment * ({@link readExecutionEnv}): on the effective half, the reading the leg made before its environment was started. */ export interface ToolMountPlan { readonly half: "static" | "effective"; readonly members: readonly ToolMountPlanMember[]; readonly families: readonly ToolMountPlanFamily[]; readonly absent: readonly ToolMountPlanAbsent[]; readonly executionEnv: ToolMountPlanExecutionEnv; } /** A family member a reader knows by name before the leg runs. */ export interface ToolMountPlannedMember { readonly family: ToolMountFamily; readonly identity: ToolMountIdentity; /** The catalog definition behind it, when its contract is one. */ readonly id?: string; readonly source: ToolMountSource; readonly mountedBy: ToolMountTag; /** The member's own condition (the family's listing fact, its class gate). */ readonly when: ToolMountCond; /** The path target its face declares, when it declares one. */ readonly pathTarget?: ToolCallPathTarget; /** The member is the delegation tool (`entryDelegates`): its children are placed from the leg's seat. */ readonly delegates?: true; } /** * The plan the rows give over a reading's facts: each catalog row is a member (holds), a conditional member (open) or an * absent definition (fails); the reading's members add the family members a reader knows by name (each under its own * condition and the valve); its families the listings it only knows by peer; `executionEnv` its answer to whether the leg * needs an environment — read by the caller ({@link readExecutionEnv}), once, at the moment the leg reads it, over the facts the * leg holds then (a fact the leg states after it decides is not yet stated), and carried here. */ export declare function planToolMounts(reading: ExecutionEnvReading, executionEnv: ToolMountPlanExecutionEnv): ToolMountPlan; /** Whether a leg needs an execution environment: a consumer of it mounts or a need beside the tools holds (`needed`), one * of them is open (`conditional`), or neither (`unneeded`). */ export declare const EXECUTION_ENV_NEEDS: readonly ["needed", "conditional", "unneeded"]; export type ExecutionEnvNeed = (typeof EXECUTION_ENV_NEEDS)[number]; /** A leg's needs of an environment beside its tools: a declared workspace; a user attachment the engine reads by path; a * session plan file; a file restore; the session's checkout record; a resume's workspace or working directory; files a * compaction re-delivers (the reads the leg's read state carries in, re-read through the environment); a placement * its delegating caller asked for; a tool that delegates (its children are placed from the leg's seat); a registration * that calls a method whose bottom runs on the environment, subscribes to an event whose payload states what it holds, or * — on a leg that can call a tool — subscribes to a tool-call event that hands its handlers its face; an engine-memory * session (its partition is keyed by the task root, and the project marker read there, through the environment); an ask * that may park (a leg that can call a tool, where a park can be taken: the environment arms the park lane — its shell read * station — and decides whether its park can be taken). */ export declare const EXECUTION_ENV_ASKS: readonly ["workspace", "attachment_paths", "plan_file", "restore_files", "checkout", "restore", "compaction_restore", "caller_placement", "delegation", "mods", "memory", "park_lane"]; export type ExecutionEnvAsk = (typeof EXECUTION_ENV_ASKS)[number]; /** A plan's answer: the state, the consumers that decide it (in plan order; a listing known only by peer is named * `<source>:<peer>`), and the needs beside the tools that hold or are open, in the set's order. */ export interface ToolMountPlanExecutionEnv { readonly state: ExecutionEnvNeed; readonly tools: readonly string[]; readonly asks: readonly ExecutionEnvAsk[]; } /** The needs a half reads itself; `delegation`, `mods` and `park_lane` the plan answers from its own rows and members (with * the half's {@link ExecutionEnvReading.mods} for the second, its `seat.checkpoint` fact for the third). */ export type ExecutionEnvAskReading = Readonly<Record<Exclude<ExecutionEnvAsk, "delegation" | "mods" | "park_lane">, boolean | undefined>>; /** What a half hands the plan: the facts it can read (a reading of the environment's facts is replaced, never read), the * family members it knows by name, the listings it knows by peer (each with whether a tool it may list declares a * non-read path target), whether the leg installs a write-protection table, and its own needs. */ export interface ExecutionEnvReading { readonly src: ToolMountFactSource; readonly members: readonly ToolMountPlannedMember[]; readonly families: readonly { readonly family: ToolMountPlanFamily; readonly writesPath: boolean; readonly readsPath?: boolean; }[]; readonly writeProtected: boolean; readonly asks: ExecutionEnvAskReading; /** Whether the leg's registrations reach the environment on any leg, and on a leg that can call a tool. */ readonly mods: { readonly always: boolean | undefined; readonly onCall: boolean | undefined; }; } /** * Whether a leg needs an execution environment — ONE reading, both halves. The rows are read twice over the half's facts: * once with every environment fact true (an upper bound for any environment: no row reads one under a `not`), once with * none (no environment at all — a deployment's static one is not read: a member that needs an environment is counted even * where a static one stands, so a leg whose tools consume one always asks its factory; a leg that reads `unneeded` still * stands in a static env when one stands, which only enriches it — nothing that consumes the environment runs there). A * member CONSUMES the environment when its placement differs between the two readings, when its row carries * {@link ToolMountRule.envHand}, or when the leg installs a write-protection table and one of the member's names takes a * non-read path target ({@link writesProtectedPath} — the judge resolves that target in the environment). A consumer counts * at its placement with an environment; an open need counts as `conditional`. */ export declare function readExecutionEnv(r: ExecutionEnvReading): ToolMountPlanExecutionEnv; /** The internal fault the ledger raises when a mount and the table disagree. */ export declare const TOOL_MOUNT_PLAN_MISMATCH = "internal.tool_mount_plan_mismatch"; /** The leg's name domain, bound once its spec and valve are final. */ export interface ToolMountLegBinding { /** The task's tool valve: does it leave this identity mounted? */ readonly leaves: (identity: ToolMountIdentity) => boolean; /** The caller's own tools, as the class gate left them. */ readonly declared: readonly ToolMountIdentity[]; } /** What the ledger reads off a mounted instance beside its name. */ export type ToolMountContractOf<T extends ToolMountIdentity> = (tool: T) => string; /** * The ledger a leg's roster builder carries. Facts enter once (`know`; a second, different word for a fact is a fault); * a mount site asks `judge` / `judgeTool` / `judgeMember` and mounts on yes; `settle` checks, before the roster is * snapshotted, that the instances on it are exactly the ones the rows said — by name, and by contract for every catalog * definition — and that every row it was never asked about fails over the facts the leg stated. */ export declare class ToolMountLedger<T extends ToolMountIdentity> { #private; constructor(contractOf: ToolMountContractOf<T>); /** Bind the leg's name domain (once). */ bindLeg(leg: ToolMountLegBinding): void; /** State facts. A fact stated twice must keep its word. */ know(facts: Partial<Record<ToolMountFact, boolean>>): void; /** Has this fact been stated? */ knows(f: ToolMountFact): boolean; /** The word stated for this fact, or `undefined` when none was. */ factOf(f: ToolMountFact): boolean | undefined; /** Does row `id`'s condition hold — a READING (a fact another phase derives from the row), never a mount: nothing is * recorded. */ holds(id: string): boolean; /** Would row `id`'s condition hold were `assumed` stated as well — a reading for a phase that has to decide before the * fact it assumes is live (the memory trio's injection is taught before its session mounts); nothing is recorded. */ holdsIf(id: string, assumed: Partial<Record<ToolMountFact, boolean>>): boolean; /** The names of the rows `select` picks whose condition holds — a reading, as {@link holds}. */ holding(select: (entry: ToolCatalogEntry) => boolean): string[]; /** Does row `id` hold? A yes records the definition the roster must end up carrying. */ judge(id: string): boolean; /** The leg's env hands: the names of the rows judged mounted whose {@link ToolMountRule.envHand} column is set — a live * view, so a reader holding it sees every later judgment. */ get envHands(): ReadonlySet<string>; /** Does the row of the catalog definition this instance is hold? The instance names the row by name and contract. */ judgeTool(tool: T): boolean; /** Mount a family member: its family's listing holds by construction (the member is in hand), so the member is * recorded and the answer is yes — the valve decides it later, as it decides every other member. */ judgeMember(family: ToolMountFamily, tool: T): boolean; /** The late judgment's word on a group (taken back or kept). */ retract(group: ToolMountRetractable, taken: boolean): void; /** * Check the roster against the rows: the instances on it (later mount wins, as the roster dispatches) must be the * judged-yes definitions and members the valve and the retractions leave, one for one by name, each catalog * definition under its own contract; every row never asked must fail over the leg's facts. Returns the leg's * effective plan, carrying `executionEnv` — the reading the leg made before its environment was started, as it was made. */ settle(onRoster: readonly T[], members: readonly ToolMountPlanMember[], executionEnv: ToolMountPlanExecutionEnv): ToolMountPlan; /** The effective plan, once settled. */ get plan(): ToolMountPlan | undefined; }