@sema-agent/core
Version:
Stateless, task-oriented AI agent core
888 lines (887 loc) • 44.4 kB
JavaScript
import { types } from "node:util";
import { Type } from "typebox";
import { toolNameIndex } from "../internal/llm.js";
import { getToolContract, toolContractOfFace } from "../prompt-assembly/tool-catalog.js";
import { protocolOf } from "@sema-agent/primitives";
import { describeToolCatalog } from "./tool-registry.js";
import { TOOL_CATALOG_ENTRIES } from "./tool-catalog-entries.js";
import { ToolMountLedger } from "./tool-mount-plan.js";
import { RENDER_HINT_ACTIVITY_MAX_CHARS, RENDER_HINT_MAX_CHARS, RENDER_HINT_MAX_LIST, RESERVED_APPROVAL_CARDS, isToolApprovalCard, TOOL_APPROVAL_CARDS, TOOL_CARD_ID_MAX_CHARS, TOOL_CONTRACT_MAX_CHARS, TOOL_FAMILIES, TOOL_KEYS_MAX, TOOL_KEY_MAX_CHARS, TOOL_MOUNT_TAGS, TOOL_WIRE_NAME_MAX_CHARS, TOOL_PATH_ABSENCES, TOOL_PATH_BASES, } from "./tool-face.js";
import { ASK_USER_QUESTION_TOOL_NAME } from "./ask-question.js";
import { PRESENT_PLAN_TOOL_NAME } from "./present-plan-tool.js";
import { SANDBOX_NETWORK_TOOL_NAME } from "./command-sandbox.js";
import { wireOf } from "./tool-name-readings.js";
import { BOUNDARY_LEGS, rosterBoundaryLegs } from "./cross-boundary-taint.js";
import { isPlainRecord, receive } from "./mods/kernel/receive.js";
import { boundInputHashOf, canonicalJsonStringify, sha16 } from "./canonical-json.js";
function lit(values) {
return Type.Union(values.map((v) => Type.Literal(v)));
}
const Name = Type.String({ minLength: 1, maxLength: TOOL_WIRE_NAME_MAX_CHARS });
const Short = Type.String({ maxLength: TOOL_CONTRACT_MAX_CHARS });
const Key = Type.String({ maxLength: TOOL_KEY_MAX_CHARS });
const Digest16 = Type.String({ minLength: 16, maxLength: 16 });
export const TOOL_ROSTER_MAX_ENTRIES = 2048;
function rosterRowCount(tools) {
const names = new Set();
for (const t of tools)
names.add(t.name);
return names.size;
}
function rosterEntryCountProblem(rows) {
if (rows <= TOOL_ROSTER_MAX_ENTRIES)
return undefined;
return `the tool roster would carry ${rows} rows; a roster carries at most ${TOOL_ROSTER_MAX_ENTRIES} (one per distinct tool name, deferred tools included) — narrow the leg's tool face (the selector / excludeTools) or split the MCP servers across legs`;
}
export const ROSTER_SOURCES = Object.freeze(["builtin", "caller", "host", "mcp", "a2a", "external", "synthetic"]);
export const ToolRosterRenderHints = Type.Object({
userFacingName: Type.Optional(Type.String({ maxLength: RENDER_HINT_MAX_CHARS })),
activity: Type.Optional(Type.String({ maxLength: RENDER_HINT_ACTIVITY_MAX_CHARS })),
summaryParams: Type.Optional(Type.Array(Key, { maxItems: RENDER_HINT_MAX_LIST })),
resultCards: Type.Optional(Type.Array(Key, { maxItems: RENDER_HINT_MAX_LIST })),
approvalCard: Type.Optional(lit(TOOL_APPROVAL_CARDS)),
}, { additionalProperties: false });
export const ToolRosterEntry = Type.Object({
name: Name,
aliases: Type.Array(Name, { uniqueItems: true }),
source: lit(ROSTER_SOURCES),
origin: Type.Optional(Type.Object({ peer: Name, declaredBy: Type.Optional(lit(["center", "file", "request"])) }, { additionalProperties: false })),
remoteName: Type.Optional(Name),
contract: Type.Object({ contractId: Short, implementationRevision: Short }, { additionalProperties: false }),
shapeDigest: Digest16,
wireSchemaDigest: Digest16,
cardId: Type.String({ maxLength: TOOL_CARD_ID_MAX_CHARS }),
capabilityId: Digest16,
inputKeys: Type.Array(Type.Object({ name: Key, type: lit(["string", "number", "integer", "boolean", "object", "array", "null", "mixed"]) }, { additionalProperties: false }), { maxItems: TOOL_KEYS_MAX }),
effect: lit(["read", "write", "idempotent"]),
effectProvenance: lit(["declared", "caller", "synthetic", "default"]),
offload: Type.Boolean(),
offloadThresholdChars: Type.Optional(Type.Integer({ minimum: 0 })),
compactable: Type.Boolean(),
budgetExempt: Type.Boolean(),
egress: Type.Boolean(),
egressProvenance: lit(["declared", "caller", "default"]),
irreversibility: Type.Optional(lit(["never", "maybe", "always"])),
reversibilityProbe: Type.Boolean(),
contentOrigin: lit(["local", "execution", "external"]),
contentOriginProvenance: lit(["declared", "server", "exempted", "default"]),
family: lit(TOOL_FAMILIES),
boundaryLegs: Type.Union([Type.Literal("per_call"), Type.Array(lit(BOUNDARY_LEGS), { uniqueItems: true })]),
pathTarget: Type.Optional(Type.Object({
param: Key,
aliases: Type.Array(Key),
access: lit(["read", "create", "edit"]),
skillScopeEligible: Type.Boolean(),
base: Type.Optional(lit(TOOL_PATH_BASES)),
absent: Type.Optional(lit(TOOL_PATH_ABSENCES)),
patternParam: Type.Optional(Key),
each: Type.Optional(Type.Literal(true)),
}, { additionalProperties: false })),
hostTarget: Type.Optional(Type.Object({ param: Key }, { additionalProperties: false })),
ruleFace: Type.Object({ primaryParams: Type.Array(Key), params: Type.Array(Key), nameValues: Type.Optional(Type.Array(Type.String())) }, { additionalProperties: false }),
approvalPreview: Type.Boolean(),
validateInput: Type.Boolean(),
deferred: Type.Boolean(),
alwaysLoad: Type.Boolean(),
modelGate: Type.Optional(Short),
renderHints: Type.Optional(ToolRosterRenderHints),
startedShell: Type.Optional(lit(["zsh"])),
mountedBy: lit(TOOL_MOUNT_TAGS),
}, { additionalProperties: false });
export const ToolRoster = Type.Object({
schemaVersion: Type.Literal(1),
count: Type.Integer({ minimum: 0 }),
entries: Type.Array(ToolRosterEntry, { maxItems: TOOL_ROSTER_MAX_ENTRIES }),
digest: Digest16,
}, { additionalProperties: false });
export const ToolRosterDelta = Type.Object({
fromDigest: Digest16,
roster: ToolRoster,
summary: Type.Object({ added: Type.Array(Name), removed: Type.Array(Name), changed: Type.Array(Name) }, { additionalProperties: false }),
mcp: Type.Optional(Type.Array(Type.Unsafe(Type.Object({ name: Type.String(), status: Type.Union([Type.Literal("connected"), Type.Literal("failed")]), toolCount: Type.Integer({ minimum: 0 }) }, { additionalProperties: true })))),
}, { additionalProperties: false });
export function rosterEntryNamed(roster, toolName) {
return roster.entries.find((r) => r.name === toolName) ?? roster.entries.find((r) => r.aliases.includes(toolName));
}
export function rosterToolName(roster, name) {
return (roster === undefined ? undefined : rosterEntryNamed(roster, name)?.name) ?? name;
}
function namesResolvingTo(roster, e) {
return [e.name, ...e.aliases.filter((a) => a !== e.name && rosterToolName(roster, a) === e.name)];
}
export { ruleNamesOf } from "./tool-name-readings.js";
const RESERVED_CARD_OWNER = {
question: () => ASK_USER_QUESTION_TOOL_NAME,
plan: () => PRESENT_PLAN_TOOL_NAME,
network: () => SANDBOX_NETWORK_TOOL_NAME,
};
export function engineCardOf(call) {
const wire = wireOf("card.reserved", call);
return RESERVED_APPROVAL_CARDS.find((card) => RESERVED_CARD_OWNER[card]() === wire);
}
export function approvalCardOf(call, declared) {
return engineCardOf(call) ?? (declared !== undefined && !RESERVED_APPROVAL_CARDS.includes(declared) ? declared : "generic");
}
export function approvalCardMemberOf(call) {
const card = call.face?.approvalCard;
return card !== undefined ? { approvalCard: card } : {};
}
export function toolCallFaceOf(roster, toolName) {
if (roster === undefined)
return undefined;
const e = rosterEntryNamed(roster, toolName);
if (e === undefined)
return undefined;
const resolving = namesResolvingTo(roster, e);
return {
names: resolving.includes(toolName) ? [toolName, ...resolving.filter((n) => n !== toolName)] : resolving,
family: e.family,
...(e.pathTarget !== undefined ? { pathTarget: e.pathTarget } : {}),
...(e.hostTarget !== undefined ? { hostTarget: e.hostTarget } : {}),
...(e.ruleFace?.nameValues !== undefined ? { nameValues: Object.freeze([...e.ruleFace.nameValues]) } : {}),
capabilityId: e.capabilityId,
contractId: e.contract.contractId,
shapeDigest: e.shapeDigest,
implementationRevision: e.contract.implementationRevision,
approvalCard: approvalCardOf({ toolName: e.name }, e.renderHints?.approvalCard),
...(engineMountedRow(e) ? { engineMounted: true } : {}),
...(e.startedShell !== undefined ? { startedShell: e.startedShell } : {}),
};
}
export function parkedToolIdentity(roster, toolName) {
if (roster === undefined)
return {};
const e = rosterEntryNamed(roster, toolName);
return e === undefined ? {} : toolIdentityOf(e);
}
export function parkedToolFace(roster, req) {
if (roster === undefined)
return {};
const e = rosterEntryNamed(roster, req.toolName);
if (e === undefined)
return {};
const judged = req.face?.names;
return { family: e.family, ...(e.pathTarget !== undefined ? { pathTarget: e.pathTarget } : {}), ...(e.hostTarget !== undefined ? { hostTarget: e.hostTarget } : {}), names: judged !== undefined && judged.includes(req.toolName) ? [...judged] : namesResolvingTo(roster, e), declarationDigest: toolDeclarationDigestOf(e), ...(engineMountedRow(e) ? { engineMounted: true } : {}), ...(e.startedShell !== undefined ? { startedShell: e.startedShell } : {}) };
}
export function parkedRowLiveFace(roster, row) {
const spelled = row.names?.[0];
const under = spelled !== undefined && roster !== undefined && rosterToolName(roster, spelled) === row.toolName ? spelled : row.toolName;
return toolCallFaceOf(roster, under);
}
export function parkedCallFaceOf(pending) {
if (pending.family === undefined || pending.capabilityId === undefined || pending.contractId === undefined || pending.shapeDigest === undefined || pending.implementationRevision === undefined)
return undefined;
return {
...(pending.names !== undefined ? { names: pending.names } : {}),
family: pending.family,
...(pending.pathTarget !== undefined ? { pathTarget: pending.pathTarget } : {}),
...(pending.hostTarget !== undefined ? { hostTarget: pending.hostTarget } : {}),
capabilityId: pending.capabilityId,
contractId: pending.contractId,
shapeDigest: pending.shapeDigest,
implementationRevision: pending.implementationRevision,
...(pending.engineMounted === true ? { engineMounted: true } : {}),
...(pending.startedShell !== undefined ? { startedShell: pending.startedShell } : {}),
};
}
export function callFaceSeat(face) {
return face !== undefined ? { face } : {};
}
export function toolIdentityOf(entry) {
return { capabilityId: entry.capabilityId, contractId: entry.contract.contractId, shapeDigest: entry.shapeDigest, implementationRevision: entry.contract.implementationRevision };
}
export const ROSTER_APPROVAL_BINDING = Object.freeze({
name: { identity: ["capabilityId"] },
aliases: { unbound: "the names a call answers to are the call's own (the row's `names`); a resumed belt judges under the live names" },
source: { identity: ["capabilityId"] },
origin: { unbound: "the peer is the identity's (capabilityId); who declared the server is provenance no decision reads" },
remoteName: { identity: ["capabilityId"] },
contract: { identity: ["contractId", "implementationRevision"] },
shapeDigest: { identity: ["shapeDigest"] },
wireSchemaDigest: { unbound: "the schema with its description prose for the model; the shape a call is validated against is the identity's (shapeDigest)" },
cardId: { unbound: "derived from the source, peer, name and contract, all identity" },
capabilityId: { identity: ["capabilityId"] },
inputKeys: { unbound: "derived from the schema, whose shape is the identity's (shapeDigest)" },
effect: "declaration",
effectProvenance: "declaration",
offload: { unbound: "where the engine keeps the call's result (the deployment's own store), not what the call does or reaches" },
offloadThresholdChars: { unbound: "where the engine keeps the call's result (the deployment's own store), not what the call does or reaches" },
compactable: { unbound: "how the transcript keeps the result, not what the call does" },
budgetExempt: { unbound: "how the result counts against the context budget, not what the call does" },
egress: "declaration",
egressProvenance: { unbound: "where the egress value came from; the value is the declaration's (egress), no decision reads its provenance" },
irreversibility: "declaration",
reversibilityProbe: "declaration",
contentOrigin: "declaration",
contentOriginProvenance: { unbound: "where the content-origin value came from; the value is the declaration's, no decision reads its provenance" },
family: "face",
boundaryLegs: "declaration",
pathTarget: "face",
hostTarget: "face",
ruleFace: "declaration",
approvalPreview: { unbound: "whether the card had a preview, shown when it was asked" },
validateInput: { unbound: "the live tool validates the bytes it runs, whatever the row says" },
deferred: { unbound: "when the schema is loaded into the model's context, not what the call does" },
alwaysLoad: { unbound: "when the schema is loaded into the model's context, not what the call does" },
modelGate: "declaration",
renderHints: { unbound: "display, shown when the card was asked" },
startedShell: "face",
mountedBy: { identity: ["engineMounted"] },
});
const DECLARATION_MEMBERS = Object.keys(ROSTER_APPROVAL_BINDING).filter((k) => ROSTER_APPROVAL_BINDING[k] === "declaration");
export function toolDeclarationDigestOf(entry) {
const pairs = DECLARATION_MEMBERS.flatMap((k) => (entry[k] === undefined ? [] : [[k, entry[k]]]));
if (entry.source === "mcp" || entry.source === "a2a") {
try {
const tool = rowMember(entry);
if (tool !== undefined && typeof tool.peerRegistration === "function") {
const reg = tool.peerRegistration();
if (typeof reg === "string")
pairs.push(["registration", reg]);
}
}
catch {
}
}
return boundInputHashOf(Object.fromEntries(pairs));
}
export function mintCapabilityId(t) {
return sha16(canonicalJsonStringify({ source: t.source, peer: t.peer, name: t.name, contractId: t.contractId, ...(t.remote !== undefined ? { remote: t.remote } : {}) }));
}
export function rosterSourceNamesPeer(source) {
return source === "mcp" || source === "a2a" || source === "external";
}
export function identityHoldsNoServerName(pending) {
const name = Reflect.get(pending, "toolName");
const contractId = Reflect.get(pending, "contractId");
const id = Reflect.get(pending, "capabilityId");
if (typeof name !== "string" || typeof contractId !== "string" || typeof id !== "string")
return false;
return ROSTER_SOURCES.some((source) => !rosterSourceNamesPeer(source) && mintCapabilityId({ source, peer: null, name, contractId }) === id);
}
export function rosterDigestOf(entries) {
return sha16(canonicalJsonStringify(entries));
}
const SCALAR_TYPES = new Set(["string", "number", "integer", "boolean"]);
function schemaTypeOf(node) {
if (node === null || typeof node !== "object")
return { type: "mixed", scalar: false };
const s = node;
const branches = s["anyOf"] ?? s["oneOf"];
if (Array.isArray(branches) && branches.length > 0) {
const parts = branches.map(schemaTypeOf);
const scalar = parts.every((p) => p.scalar);
const first = parts[0].type;
return parts.every((p) => p.type === first) ? { type: first, scalar } : { type: "mixed", scalar };
}
if (s["const"] !== undefined) {
const t = typeof s["const"];
return t === "string" || t === "number" || t === "boolean" ? { type: t, scalar: true } : { type: "mixed", scalar: false };
}
const en = s["enum"];
if (Array.isArray(en) && en.length > 0) {
const kinds = new Set(en.map((v) => typeof v));
if (kinds.size === 1) {
const t = [...kinds][0];
if (t === "string" || t === "number" || t === "boolean")
return { type: t, scalar: true };
}
return { type: "mixed", scalar: [...kinds].every((k) => k === "string" || k === "number" || k === "boolean") };
}
const t = s["type"];
if (typeof t === "string") {
if (SCALAR_TYPES.has(t))
return { type: t, scalar: true };
if (t === "object" || t === "array" || t === "null")
return { type: t, scalar: false };
}
if (Array.isArray(t))
return { type: "mixed", scalar: t.every((x) => typeof x === "string" && SCALAR_TYPES.has(x)) };
if (s["properties"] !== undefined)
return { type: "object", scalar: false };
if (s["items"] !== undefined)
return { type: "array", scalar: false };
return { type: "mixed", scalar: false };
}
export function inputKeysOf(schema) {
const keys = [];
const scalar = new Set();
const props = schema?.properties;
if (props !== null && typeof props === "object" && !Array.isArray(props)) {
for (const [name, sub] of Object.entries(props)) {
const t = schemaTypeOf(sub);
keys.push({ name, type: t.type });
if (t.scalar)
scalar.add(name);
}
}
return { keys, scalar };
}
export function schemaKeyBoundProblem(schema) {
const props = schema?.properties;
if (props === null || typeof props !== "object" || Array.isArray(props))
return undefined;
const names = Object.keys(props);
if (names.length > TOOL_KEYS_MAX)
return `the schema declares ${names.length} top-level properties; a roster row carries at most ${TOOL_KEYS_MAX}`;
const long = names.find((n) => n.length > TOOL_KEY_MAX_CHARS);
if (long !== undefined)
return `the schema property ${JSON.stringify(long.slice(0, 40) + "…")} is ${long.length} characters; a roster key is at most ${TOOL_KEY_MAX_CHARS}`;
return undefined;
}
const NOT_IN_A_SERVER_TOOL_NAME = /[\p{Cc}\p{Cf}\p{Zl}\p{Zp}]|(?! )\p{Zs}/u;
export function rosterMemberBoundProblem(m) {
if (typeof m.name !== "string")
return { code: "config.tool_roster_bound", message: `tool has no string name (got ${m.name === null ? "null" : typeof m.name}); every roster row is keyed by a string \`name\`` };
const over = (label, value, bound) => `${label} ${JSON.stringify(value.slice(0, 40) + "…")} is ${value.length} characters; the roster's bound is ${bound}`;
const of = (text) => `tool ${JSON.stringify(m.name.slice(0, 40))}: ${text}`;
if (m.name.length > TOOL_WIRE_NAME_MAX_CHARS)
return { code: "config.tool_name_too_long", message: over("tool name", m.name, TOOL_WIRE_NAME_MAX_CHARS) };
const alias = m.aliases?.find((a) => a.length > TOOL_WIRE_NAME_MAX_CHARS);
if (alias !== undefined)
return { code: "config.tool_name_too_long", message: of(over("alias", alias, TOOL_WIRE_NAME_MAX_CHARS)) };
if (m.peer !== undefined && m.peer.length > TOOL_WIRE_NAME_MAX_CHARS)
return { code: "config.tool_name_too_long", message: of(over("peer", m.peer, TOOL_WIRE_NAME_MAX_CHARS)) };
if (m.remoteName !== undefined && m.remoteName.length > TOOL_WIRE_NAME_MAX_CHARS)
return { code: "config.tool_name_too_long", message: of(over("server tool name", m.remoteName, TOOL_WIRE_NAME_MAX_CHARS)) };
if (m.remoteName === "")
return { code: "config.tool_roster_bound", message: of("the server lists the tool under the empty name; a roster row carries a server tool name of at least one character") };
if (m.remoteName !== undefined && NOT_IN_A_SERVER_TOOL_NAME.test(m.remoteName))
return { code: "config.tool_roster_bound", message: of("the server lists the tool under a name with a control, separator, direction or zero-width character; a roster row carries a server tool name that prints as it is spelled") };
for (const [label, v] of [["contractId", m.contractId], ["implementationRevision", m.implementationRevision]]) {
if (v !== undefined && v.length > TOOL_CONTRACT_MAX_CHARS)
return { code: "config.tool_contract_too_long", message: of(over(`contract ${label}`, v, TOOL_CONTRACT_MAX_CHARS)) };
}
if (m.modelGate !== undefined && m.modelGate.length > TOOL_CONTRACT_MAX_CHARS)
return { code: "config.tool_roster_bound", message: of(over("modelGate", m.modelGate, TOOL_CONTRACT_MAX_CHARS)) };
if (m.cardId !== undefined && m.cardId.length > TOOL_CARD_ID_MAX_CHARS)
return { code: "config.tool_roster_bound", message: of(over("cardId", m.cardId, TOOL_CARD_ID_MAX_CHARS)) };
return undefined;
}
export function engineMountedRow(e) {
return e.mountedBy !== "scenario" && (e.source === "builtin" || e.source === "synthetic");
}
export function ruleFaceOfInstance(schema, declared, pathTarget) {
const { scalar } = inputKeysOf(schema);
const derived = new Set(scalar);
if (pathTarget !== undefined) {
derived.add(pathTarget.param);
for (const a of pathTarget.aliases ?? [])
derived.add(a);
}
const primary = declared?.primaryParams !== undefined ? [...declared.primaryParams] : pathTarget !== undefined ? [pathTarget.param, ...(pathTarget.aliases ?? [])] : [];
for (const p of primary)
derived.add(p);
const params = declared?.params !== undefined ? declared.params.filter((p) => derived.has(p)) : [...derived];
const ordered = [...new Set([...params, ...primary.filter((p) => params.includes(p) || declared?.params === undefined)])];
return { primaryParams: primary, params: ordered };
}
function capRenderHints(h) {
const out = {};
if (typeof h.userFacingName === "string")
out.userFacingName = h.userFacingName;
if (typeof h.activity === "string")
out.activity = h.activity;
if (Array.isArray(h.summaryParams))
out.summaryParams = h.summaryParams.filter((s) => typeof s === "string");
if (Array.isArray(h.resultCards))
out.resultCards = h.resultCards.filter((s) => typeof s === "string");
if (isToolApprovalCard(h.approvalCard))
out.approvalCard = h.approvalCard;
return Object.keys(out).length > 0 ? out : undefined;
}
const ROW_MEMBERS = new WeakMap();
export function rowMember(row) {
return ROW_MEMBERS.get(row);
}
export function survivorsByName(mounted) {
const winner = new Map();
for (const t of mounted)
winner.set(t.name, t);
return winner;
}
function segmentOf(meta) {
if (meta?.perTurn === true)
return 2;
return meta !== undefined && (rosterSourceNamesPeer(meta.source) || meta.followsDial === true) ? 1 : 0;
}
const byName = (a, b) => (a < b ? -1 : a > b ? 1 : 0);
export const faceOf = (t) => t;
const protocolFolded = (source) => source === "mcp" || source === "a2a";
function catalogDefinitionOf(tool) {
const contract = toolContractOfFace(tool, "core").contractId;
return TOOL_CATALOG_ENTRIES.find((e) => e.name === tool.name && e.face.contract?.contractId === contract);
}
export function planIdentityOf(tool, source) {
const id = catalogDefinitionOf(tool)?.id;
return { ...(id !== undefined ? { id } : {}), source: planSourceOf(source) };
}
function planSourceOf(source) {
switch (source) {
case "builtin":
case "synthetic":
case "mcp":
case "a2a":
return source;
default:
return "scenario";
}
}
function configError(code, message) {
const e = new Error(message);
e.code = code;
return e;
}
export class RosterBuilder {
#tools = [];
#engineLane = [];
#meta = new WeakMap();
#effects;
constructor(effects) {
this.#effects = effects;
}
mounts = new ToolMountLedger((t) => getToolContract(t, "core").contractId);
settleMountPlan(executionEnv) {
const all = [...this.#tools, ...this.#engineLane];
for (const t of all) {
if (!this.#perTurn(t))
continue;
for (const n of [t.name, ...(t.aliases ?? [])]) {
const other = all.find((o) => o !== t && (o.name === n || (o.aliases ?? []).includes(n)));
if (other !== undefined) {
throw configError("config.tool_face_invalid", `tool ${JSON.stringify(t.name)} declares \`perTurn\`, and ${n === t.name ? "its name" : `its alias ${JSON.stringify(n)}`} is also answered by the mounted tool ${JSON.stringify(other.name)} — a per-turn tool must own every name it answers to: rename it, or drop \`perTurn\``);
}
}
}
const members = [...survivorsByName(all).values()].map((t) => {
const meta = this.#meta.get(t);
return { name: t.name, ...planIdentityOf(t, meta?.source), mountedBy: meta?.mountedBy ?? "scenario", state: "mounted" };
});
return this.mounts.settle(all, members, executionEnv);
}
get tools() {
return this.#tools;
}
get perTurnStart() {
const i = this.#tools.findIndex((t) => this.#perTurn(t));
return i < 0 ? this.#tools.length : i;
}
#perTurn(tool) {
return this.#meta.get(tool)?.perTurn === true;
}
get length() {
return this.#tools.length;
}
metaOf(tool) {
return this.#meta.get(tool);
}
engineDefinitionOf(tool) {
const meta = this.#meta.get(tool);
return meta !== undefined && engineMountedRow(meta) ? catalogDefinitionOf(tool) : undefined;
}
mount(tool, meta) {
this.#settleEffects(this.#mountOne(tool, meta, this.#tools));
}
#mountOne(tool, meta, lane) {
const displaced = this.#survivorNamed(tool.name);
this.#admit(tool, meta);
if (lane === this.#tools)
lane.splice(this.#placeOf(tool, meta), 0, tool);
else
lane.push(tool);
return [tool.name, ...(displaced?.aliases ?? []), ...(tool.aliases ?? [])];
}
#placeOf(tool, meta) {
const segment = segmentOf(meta);
let at = 0;
for (; at < this.#tools.length; at++) {
const other = this.#tools[at];
const its = segmentOf(this.#meta.get(other));
if (its > segment || (its === segment && byName(other.name, tool.name) > 0))
break;
}
return at;
}
mountEngineLane(tool, meta) {
this.#settleEffects(this.#mountOne(tool, meta, this.#engineLane));
}
#admit(tool, meta) {
if (rosterSourceNamesPeer(meta.source) !== (meta.origin !== undefined)) {
throw configError("config.tool_roster_origin", `tool ${JSON.stringify(tool.name)}: a ${meta.source} mount ${meta.origin === undefined ? "requires" : "must not carry"} an origin`);
}
if (!TOOL_MOUNT_TAGS.includes(meta.mountedBy))
throw configError("config.tool_roster_mount_tag", `tool ${JSON.stringify(tool.name)}: mount tag ${JSON.stringify(meta.mountedBy)} is outside the closed set`);
this.#meta.set(tool, meta);
}
#settleEffects(names) {
const answering = toolNameIndex([...survivorsByName([...this.#tools, ...this.#engineLane]).values()]);
for (const name of new Set(names)) {
const owner = answering(name);
const declared = owner === undefined ? undefined : faceOf(owner).effect;
if (declared !== undefined)
this.#effects.set(name, declared);
else if (owner === undefined || !protocolFolded(this.#meta.get(owner)?.source))
this.#effects.delete(name);
}
}
#namesAfterRemoval(removed) {
return [...this.#tools, ...this.#engineLane, ...removed].flatMap((t) => [t.name, ...(t.aliases ?? [])]);
}
#survivorNamed(name) {
return survivorsByName([...this.#tools, ...this.#engineLane]).get(name);
}
mountAll(tools, meta) {
this.#settleEffects(tools.flatMap((t) => this.#mountOne(t, meta, this.#tools)));
}
mountLive(tools, meta) {
const over = rosterEntryCountProblem(rosterRowCount([...this.#tools, ...tools, ...this.#engineLane]));
if (over !== undefined)
throw configError("config.tool_roster_bound", over);
this.#settleEffects(tools.flatMap((t) => this.#mountOne(t, meta(t), this.#tools)));
}
unmountWhere(pred) {
const removed = [];
for (let i = this.#tools.length - 1; i >= 0; i--) {
const t = this.#tools[i];
if (pred(t, i)) {
this.#tools.splice(i, 1);
removed.unshift(t);
}
}
this.#settleEffects(this.#namesAfterRemoval(removed));
return removed;
}
replaceAt(index, tool) {
const prior = this.#tools[index];
if (prior === undefined)
throw new Error(`roster: no tool at index ${index}`);
const meta = this.#meta.get(prior);
this.#tools[index] = tool;
if (meta !== undefined)
this.#meta.set(tool, meta);
}
replaceDomain(prefix, fresh, meta) {
const over = rosterEntryCountProblem(rosterRowCount([...this.#tools.filter((t) => !t.name.startsWith(prefix)), ...fresh, ...this.#engineLane]));
if (over !== undefined)
throw configError("config.tool_roster_bound", over);
const removed = [];
for (let i = this.#tools.length - 1; i >= 0; i--)
if (this.#tools[i].name.startsWith(prefix))
removed.push(...this.#tools.splice(i, 1));
for (const t of fresh) {
this.#meta.set(t, meta);
this.#tools.splice(this.#placeOf(t, meta), 0, t);
}
this.#settleEffects(this.#namesAfterRemoval(removed));
}
snapshot(facts) {
const cut = this.perTurnStart;
const all = [...this.#tools.slice(0, cut), ...this.#engineLane, ...this.#tools.slice(cut)];
const over = rosterEntryCountProblem(rosterRowCount(all));
if (over !== undefined)
throw configError("config.tool_roster_bound", over);
const winner = survivorsByName(all);
const entries = [];
const emitted = new Set();
for (const t of all) {
if (emitted.has(t.name))
continue;
emitted.add(t.name);
const survivor = winner.get(t.name);
const meta = this.#meta.get(survivor) ?? { source: "caller", mountedBy: "scenario" };
const row = mintRosterEntry(survivor, meta, facts);
ROW_MEMBERS.set(row, survivor);
entries.push(row);
}
const roster = { schemaVersion: 1, count: entries.length, entries, digest: rosterDigestOf(entries) };
const worn = rosterEngineNamesProblem(roster);
if (worn !== undefined)
throw configError("config.tool_face_invalid", worn);
return roster;
}
}
let catalogOwners;
function catalogOwnerOf(name) {
if (catalogOwners === undefined) {
const owners = new Map();
for (const e of describeToolCatalog()) {
owners.set(e.name, e.name);
for (const a of e.face.aliases ?? [])
if (!owners.has(a))
owners.set(a, e.name);
}
catalogOwners = owners;
}
return catalogOwners.get(name);
}
export function rosterEngineNamesProblem(roster) {
for (const e of roster.entries) {
const resolving = namesResolvingTo(roster, e);
const worn = new Map();
for (const n of resolving) {
const owner = catalogOwnerOf(n);
if (owner !== undefined && !worn.has(owner))
worn.set(owner, n);
}
if (worn.size >= 2) {
return `tool ${JSON.stringify(e.name)} answers to the names of ${worn.size} engine tools (${[...worn.values()].map((n) => JSON.stringify(n)).join(", ")}): a tool may answer to at most one engine tool's names — keep one of those aliases`;
}
}
return undefined;
}
export function callerMountSource(tool) {
return toolContractOfFace(tool, "caller").origin === "core" ? "builtin" : "caller";
}
export function rosterCardId(source, toolName, peer, protocolTool, contractId) {
return source === "builtin" || source === "synthetic"
? `builtin:${toolName}`
: source === "mcp" || source === "a2a"
? `${source}:${peer}/${protocolTool}`
: `${source === "external" ? "external" : "caller"}:${contractId}`;
}
export function callerCardIdOf(tool) {
const t = tool;
return rosterCardId(callerMountSource(t), t.name, null, t.name, getToolContract(t, "caller").contractId);
}
export function protocolToolOf(toolName) {
return protocolOf(toolName)?.parse(toolName)?.tool ?? toolName;
}
export function rosterRemoteNameOf(toolName, remote) {
return remote !== undefined && remote !== protocolToolOf(toolName) ? remote : undefined;
}
export function boundaryLegsRefusal(toolName) {
const named = typeof toolName === "string" ? JSON.stringify(toolName.slice(0, 128)) : `(a ${typeof toolName} name)`;
return configError("config.tool_face_invalid", `tool ${named}: its boundaryLegs declaration is neither "per_call" nor a list of ${BOUNDARY_LEGS.join(" | ")}`);
}
const ROSTER_LEGS_RECEIPT = Object.freeze({ accessors: "refuse", depth: 3, others: "as-is", contain: true, isProxy: (v) => types.isProxy(v) });
const SLOT = /^(?:0|[1-9]\d*)$/;
export function assertToolBoundaryLegs(tools) {
const got = receive(tools, ROSTER_LEGS_RECEIPT);
if (!got.ok || !Array.isArray(got.value))
return;
const unread = new Set();
for (const f of got.faults ?? []) {
const [at, member, slot] = f.path;
if (at !== undefined && (member === "name" || (member === "boundaryLegs" && slot !== undefined && SLOT.test(slot))))
unread.add(at);
}
got.value.forEach((t, i) => {
if (!isPlainRecord(t) || unread.has(String(i)))
return;
const legs = t["boundaryLegs"];
if (Array.isArray(legs) && Object.getPrototypeOf(legs) !== Array.prototype)
return;
if (rosterBoundaryLegs("caller", legs, "write") === undefined)
throw boundaryLegsRefusal(t["name"]);
});
}
export function entryDelegates(entry) {
return typeof entry.withAgents === "function";
}
export function rosterMountsDelegation(tools) {
if (tools === undefined)
return false;
const got = receive(tools, ROSTER_LEGS_RECEIPT);
const list = got.ok ? got.value : undefined;
if (!got.ok || !Array.isArray(list))
return undefined;
if ((got.faults ?? []).some((f) => f.path.length <= 1 || f.path[1] === "withAgents"))
return undefined;
let mounted = false;
for (const t of list) {
if (!isPlainRecord(t))
return undefined;
if (entryDelegates(t))
mounted = true;
}
return mounted;
}
export function mintRosterEntry(tool, meta, facts) {
const face = faceOf(tool);
const fallbackOrigin = meta.source === "builtin" ? "core" : meta.source === "synthetic" ? "synthetic" : meta.source === "mcp" || meta.source === "a2a" ? meta.source : "caller";
const d = getToolContract(tool, fallbackOrigin);
const peer = meta.origin?.peer ?? null;
const protocolTool = peer !== null ? protocolToolOf(tool.name) : tool.name;
const remoteName = rosterRemoteNameOf(tool.name, typeof tool.mcpRemoteName === "string" ? tool.mcpRemoteName : undefined);
const cardId = rosterCardId(meta.source, tool.name, peer, protocolTool, d.contractId);
const { keys: inputKeys } = inputKeysOf(tool.parameters);
const declaredEffect = face.effect;
const foldedEffect = facts.effects.get(tool.name);
const effect = declaredEffect ?? foldedEffect ?? "write";
const effectProvenance = declaredEffect !== undefined ? (meta.source === "synthetic" ? "synthetic" : "declared") : foldedEffect !== undefined && protocolFolded(meta.source) ? "caller" : "default";
const boundaryLegs = rosterBoundaryLegs(meta.source, face.boundaryLegs, effect);
if (boundaryLegs === undefined)
throw boundaryLegsRefusal(tool.name);
const egress = face.egress === true || facts.egress.has(tool.name);
const egressProvenance = face.egress === true ? "declared" : egress ? "caller" : "default";
const irreversibility = face.irreversibility ?? facts.irreversibilityTier.get(tool.name);
const reversibilityProbe = face.reversibilityProbe !== undefined || facts.reversibilityProbes.has(tool.name);
const contentOrigin = face.contentOrigin ?? (facts.trustedTools.has(tool.name) ? "local" : "external");
const contentOriginProvenance = face.contentOrigin !== undefined ? (meta.source === "mcp" || meta.source === "a2a" ? "server" : "declared") : facts.trustedTools.has(tool.name) ? "exempted" : "default";
const pathTarget = face.pathTarget;
const rf = ruleFaceOfInstance(tool.parameters, face.ruleFace, pathTarget);
const declaredValues = face.ruleFace?.nameValues;
const isStringList = (v) => Array.isArray(v) && Array.from(v).every((s) => typeof s === "string");
if (declaredValues !== undefined && !isStringList(declaredValues))
throw configError("config.tool_roster_bound", `tool ${JSON.stringify(tool.name)}: ruleFace.nameValues must be a list of strings`);
const nameValues = declaredValues;
const hints = face.renderHints !== undefined ? capRenderHints(face.renderHints) : undefined;
if (face.startedShell !== undefined && face.startedShell !== "zsh")
throw configError("config.tool_roster_bound", `tool ${JSON.stringify(tool.name)}: startedShell must be "zsh" or absent (bash)`);
const startedShell = face.startedShell;
const bound = rosterMemberBoundProblem({
name: tool.name,
aliases: tool.aliases,
...(peer !== null ? { peer } : {}),
...(remoteName !== undefined ? { remoteName } : {}),
contractId: d.contractId,
implementationRevision: d.implementationRevision,
...(typeof face.modelGate === "string" ? { modelGate: face.modelGate } : {}),
cardId,
});
if (bound !== undefined)
throw configError(bound.code, bound.message);
return {
name: tool.name,
aliases: [...new Set(tool.aliases ?? [])],
source: meta.source,
...(meta.origin !== undefined ? { origin: { peer: meta.origin.peer, ...(meta.origin.declaredBy !== undefined ? { declaredBy: meta.origin.declaredBy } : {}) } } : {}),
...(remoteName !== undefined ? { remoteName } : {}),
contract: { contractId: d.contractId, implementationRevision: d.implementationRevision },
shapeDigest: d.shapeDigest,
wireSchemaDigest: d.wireSchemaDigest,
cardId,
capabilityId: mintCapabilityId({ source: meta.source, peer, name: tool.name, contractId: d.contractId, remote: remoteName }),
inputKeys,
effect,
effectProvenance,
offload: face.offload !== false,
...(typeof face.offloadThresholdChars === "number" && Number.isFinite(face.offloadThresholdChars) && face.offloadThresholdChars >= 0 ? { offloadThresholdChars: Math.floor(face.offloadThresholdChars) } : {}),
compactable: face.compactable === true,
budgetExempt: face.budgetExempt === true,
egress,
egressProvenance,
...(irreversibility !== undefined ? { irreversibility } : {}),
reversibilityProbe,
contentOrigin,
contentOriginProvenance,
family: face.family !== undefined && TOOL_FAMILIES.includes(face.family) ? face.family : "other",
boundaryLegs: boundaryLegs === "per_call" ? boundaryLegs : [...boundaryLegs],
...(pathTarget !== undefined
? {
pathTarget: {
param: pathTarget.param,
aliases: [...(pathTarget.aliases ?? [])],
access: pathTarget.access,
skillScopeEligible: pathTarget.skillScopeEligible === true,
...(pathTarget.base !== undefined ? { base: pathTarget.base } : {}),
...(pathTarget.absent !== undefined ? { absent: pathTarget.absent } : {}),
...(pathTarget.patternParam !== undefined ? { patternParam: pathTarget.patternParam } : {}),
...(pathTarget.each === true ? { each: true } : {}),
},
}
: {}),
...(face.hostTarget !== undefined ? { hostTarget: { param: face.hostTarget.param } } : {}),
ruleFace: { ...rf, ...(nameValues !== undefined ? { nameValues: [...nameValues] } : {}) },
approvalPreview: typeof tool.approvalPreview === "function",
validateInput: typeof tool.validateInput === "function",
deferred: facts.deferred.has(tool.name),
alwaysLoad: face.alwaysLoad === true || face.mcpAlwaysLoad === true,
...(typeof face.modelGate === "string" ? { modelGate: face.modelGate } : {}),
...(hints !== undefined ? { renderHints: hints } : {}),
...(startedShell !== undefined ? { startedShell } : {}),
mountedBy: meta.mountedBy,
};
}
export class ToolRosterDeltaSeat {
#current;
#snapshot;
#pending = [];
#sink;
arm(current, snapshot) {
this.#current = current;
this.#snapshot = snapshot;
}
get current() {
return this.#current;
}
republish(mcp) {
if (this.#current === undefined || this.#snapshot === undefined)
return undefined;
const next = this.#snapshot();
const delta = { ...diffToolRoster(this.#current, next), ...(mcp !== undefined && mcp.length > 0 ? { mcp: [...mcp] } : {}) };
this.#current = next;
if (this.#sink !== undefined)
this.#sink(delta);
else {
this.#pending.push(delta);
if (this.#pending.length > 64)
this.#pending.shift();
}
return delta;
}
subscribe(sink) {
this.#sink = sink;
const pending = this.#pending;
this.#pending = [];
for (const d of pending)
sink(d);
}
unsubscribe() {
this.#sink = undefined;
}
drain() {
const pending = this.#pending;
this.#pending = [];
return pending;
}
}
export function ruleFacesFromRoster(roster) {
const byName = new Map(roster.entries.map((e) => [e.name, e]));
for (const e of roster.entries)
for (const a of e.aliases)
if (!byName.has(a))
byName.set(a, e);
return (toolName) => {
const e = byName.get(toolName);
if (e === undefined)
return undefined;
const params = new Set(e.ruleFace.params);
return {
primaryParams: e.ruleFace.primaryParams,
params: e.ruleFace.params,
nonScalar: e.inputKeys.filter((k) => !params.has(k.name) && (k.type === "object" || k.type === "array")).map((k) => k.name),
...(e.ruleFace.nameValues !== undefined ? { nameValues: Object.freeze([...e.ruleFace.nameValues]) } : {}),
};
};
}
export function diffToolRoster(prev, next) {
const before = new Map(prev.entries.map((e) => [e.name, canonicalJsonStringify(e)]));
const after = new Map(next.entries.map((e) => [e.name, canonicalJsonStringify(e)]));
const added = [];
const removed = [];
const changed = [];
for (const [name, row] of after) {
const old = before.get(name);
if (old === undefined)
added.push(name);
else if (old !== row)
changed.push(name);
}
for (const name of before.keys())
if (!after.has(name))
removed.push(name);
return { fromDigest: prev.digest, roster: next, summary: { added, removed, changed } };
}
export function offendingResultCard(hints, cardId, engineCards) {
for (const c of hints?.resultCards ?? []) {
if (typeof c !== "string")
continue;
if (c.startsWith(`${cardId}/`))
continue;
if (c.startsWith("builtin:") || engineCards.has(c) || !c.includes("/"))
return c;
return c;
}
return undefined;
}