UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

135 lines (134 loc) • 8.4 kB
import { applyClampSeats, clampFieldSeatsOf } from "./child-clamp-seats.js"; import { assertNoRetiredKeys } from "./retired-keys.js"; import { mergeChildSandbox, receiveTaskSandbox } from "./command-sandbox.js"; import { codedRefusal } from "./thrown-value.js"; import { isolationScopeOf } from "./isolation-scope.js"; import { screenProjectSeat } from "./project-seat.js"; function frozenDataCopy(v) { if (Array.isArray(v)) return Object.freeze(Array.from(v, (x) => frozenDataCopy(x))); if (v === null || typeof v !== "object" || ![Object.prototype, null].includes(Object.getPrototypeOf(v))) return v; return Object.freeze(Object.fromEntries(Object.entries(v).map(([k, x]) => [k, frozenDataCopy(x)]))); } const RETIRED_PARENT_SPEC_SEAT_KEYS = Object.freeze({ memoryOff: "the request-level memory-off fact is the clamp table's `memory` row now — carry `memory: { enabled: false }`", }); export function assertParentSpecSeatCurrent(seat) { if (seat !== undefined) assertNoRetiredKeys(seat, RETIRED_PARENT_SPEC_SEAT_KEYS, "ParentSpecSeat"); } const SEAT_FAST_MODE_LEG = new WeakMap(); const SEAT_PROJECT = new WeakMap(); export function bindParentSpecSeatFastMode(seat, leg) { const cell = SEAT_FAST_MODE_LEG.get(seat); if (cell === undefined || cell.leg !== undefined) throw new Error("parentSpecSeat: a seat's fast mode is bound once, by the leg that minted it"); cell.leg = leg; } export function bindParentSpecSeatProject(seat, project) { const cell = SEAT_PROJECT.get(seat); if (cell === undefined || cell.bound) throw new Error("parentSpecSeat: a seat's project is bound once, by the leg that minted it"); cell.bound = true; cell.seat = project; } export function parentSpecSeatOf(spec, frozenOnQuestion) { const word = spec.fastMode; const cell = {}; const minted = screenProjectSeat(spec.project, "ParentSpecSeatFields.project"); if (!minted.ok) throw codedRefusal(minted.code, minted.message); const projectWord = minted.seat; const projectCell = { bound: false }; const seat = Object.freeze({ ...(spec.principal !== undefined ? { principal: spec.principal } : {}), ...(spec.isolationScope !== undefined ? { isolationScope: spec.isolationScope } : {}), ...(spec.oneShot === true ? { oneShot: true } : {}), ...(spec.clientContext !== undefined ? { clientContext: spec.clientContext } : {}), ...(frozenOnQuestion !== undefined ? { onQuestion: frozenOnQuestion } : {}), ...(spec.mods !== undefined ? { mods: spec.mods } : {}), ...(spec.additionalDirectories !== undefined ? { additionalDirectories: Object.freeze([...spec.additionalDirectories]) } : {}), ...(spec.additionalReadDirectories !== undefined ? { additionalReadDirectories: Object.freeze([...spec.additionalReadDirectories]) } : {}), ...(spec.envFacts !== undefined ? { envFacts: Object.freeze({ ...spec.envFacts }) } : {}), ...(spec.sandbox !== undefined ? { sandbox: receiveTaskSandbox(spec.sandbox) } : {}), ...(spec.memoryPersistenceCapable !== undefined ? { memoryPersistenceCapable: spec.memoryPersistenceCapable } : {}), ...(spec.classifyAllShell === true ? { classifyAllShell: true } : {}), ...(spec.compaction !== undefined ? { compaction: Object.freeze({ ...spec.compaction }) } : {}), ...(spec.skillListingBudgetFraction !== undefined ? { skillListingBudgetFraction: spec.skillListingBudgetFraction } : {}), ...(spec.skillListingMaxDescChars !== undefined ? { skillListingMaxDescChars: spec.skillListingMaxDescChars } : {}), ...(spec.fileHistory !== undefined ? { fileHistory: spec.fileHistory } : {}), ...(spec.projectMemory !== undefined ? { projectMemory: spec.projectMemory } : {}), ...(spec.promptForm !== undefined ? { promptForm: spec.promptForm } : {}), ...(spec.promptBinding !== undefined ? { promptBinding: spec.promptBinding } : {}), ...(spec.promptLocale !== undefined ? { promptLocale: spec.promptLocale } : {}), ...(spec.advisorModel !== undefined ? { advisorModel: spec.advisorModel } : {}), ...(spec.attachments !== undefined ? { attachments: Object.freeze({ ...spec.attachments }) } : {}), ...(spec.language !== undefined ? { language: spec.language } : {}), ...(spec.instructionFamily !== undefined ? { instructionFamily: frozenDataCopy(spec.instructionFamily) } : {}), get fastMode() { const asked = cell.leg?.requested; return asked === undefined || (!asked && word === undefined) ? word : asked; }, get project() { return projectCell.bound ? projectCell.seat : projectWord; }, ...clampFieldSeatsOf(spec), }); SEAT_FAST_MODE_LEG.set(seat, cell); SEAT_PROJECT.set(seat, projectCell); return seat; } export function applyParentSpecSeat(child, seat) { if (seat === undefined) return child; assertParentSpecSeatCurrent(seat); const fill = (key, value) => child[key] === undefined && value !== undefined ? { [key]: value } : {}; const landed = { ...child, ...fill("principal", seat.principal), ...fill("isolationScope", seat.isolationScope), ...fill("clientContext", seat.clientContext), ...fill("onQuestion", seat.onQuestion), ...fill("mods", seat.mods), ...fill("additionalDirectories", (seat.additionalDirectories !== undefined ? [...seat.additionalDirectories] : undefined)), ...fill("additionalReadDirectories", (seat.additionalReadDirectories !== undefined ? [...seat.additionalReadDirectories] : undefined)), ...fill("envFacts", (seat.envFacts !== undefined ? { ...seat.envFacts } : undefined)), ...fill("compaction", (seat.compaction !== undefined ? { ...seat.compaction } : undefined)), ...fill("skillListingBudgetFraction", seat.skillListingBudgetFraction), ...fill("skillListingMaxDescChars", seat.skillListingMaxDescChars), ...fill("fileHistory", seat.fileHistory), ...fill("projectMemory", seat.projectMemory), ...fill("promptForm", seat.promptForm), ...fill("promptBinding", seat.promptBinding), ...fill("promptLocale", seat.promptLocale), ...fill("advisorModel", seat.advisorModel), ...fill("attachments", (seat.attachments !== undefined ? { ...seat.attachments } : undefined)), ...fill("language", seat.language), ...(seat.sandbox !== undefined ? { sandbox: mergeChildSandbox(seat.sandbox, child.sandbox) } : {}), ...fill("instructionFamily", (seat.instructionFamily !== undefined ? { ...seat.instructionFamily } : undefined)), ...fill("fastMode", seat.fastMode), ...(seat.oneShot === true ? { oneShot: true } : {}), ...(seat.classifyAllShell === true ? { classifyAllShell: true } : {}), ...(seat.memoryPersistenceCapable === false ? { memoryPersistenceCapable: false } : fill("memoryPersistenceCapable", seat.memoryPersistenceCapable)), }; return applyClampSeats(landed, clampFieldSeatsOf(seat)); } const SEAT_FACTS_ON_CTX = ["principal", "isolationScope", "oneShot", "onQuestion", "memoryPersistenceCapable"]; export function parentSpecSeatOfCtx(ctx) { const seat = ctx.parentSpecSeat; assertParentSpecSeatCurrent(seat); const facts = SEAT_FACTS_ON_CTX.filter((k) => ((v) => v !== undefined && !(k === "isolationScope" && v === ""))(ctx[k])); if (seat === undefined) { if (facts.length === 0) return undefined; throw seatRefusal(`the tool context carries ${facts.join(" / ")} but no \`parentSpecSeat\` — a delegating caller hands its children the seat it was minted (a Runner-filled ctx always carries it); spawning without it would silently drop the identity`); } const disagree = facts.filter((k) => k === "oneShot" ? (ctx.oneShot === true) !== (seat.oneShot === true) : k === "isolationScope" ? (ctx.isolationScope || undefined) !== isolationScopeOf(seat) : ctx[k] !== seat[k]); if (disagree.length > 0) throw seatRefusal(`the tool context's ${disagree.join(" / ")} disagree with its \`parentSpecSeat\` — one mint, one value`); return seat; } function seatRefusal(message) { return codedRefusal("config.parent_spec_seat_missing", `parentSpecSeat: ${message}`); }