UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

72 lines (71 loc) • 4.51 kB
import { retiredKeyOf, retiredKeyRefusal } from "../retired-keys.js"; import { boundedThrownName } from "../thrown-value.js"; import { captureProjectSeat } from "../project-seat.js"; export const DRAINED_LEG_FRAME_TYPES = Object.freeze(["status", "wiring_manifest", "message_committed", "workspace_changed"]); export const LEG_LABEL_KINDS = Object.freeze(["impl", "verifier", "fix", "rung"]); const drained = new WeakSet(); export function markDrainedLeg(internals) { if (internals !== undefined) drained.add(internals); } export function forgetDrainedLeg(internals) { if (internals !== undefined) drained.delete(internals); } const RETIRED_RUN_INTERNALS_KEYS = Object.freeze({ onStatusEvent: 'a drained leg\'s frames reach one seat now — `RunInternals.onLegFrame(frame, at)`; read `frame.type === "status"` for what this seat was handed (the same frame object)', onWiringManifest: 'a drained leg\'s frames reach one seat now — `RunInternals.onLegFrame(frame, at)`; read `frame.type === "wiring_manifest"` and `frame.manifest` for what this seat was handed, `at.leg` for the composition\'s label it took as its second argument', }); function seatOutsideCopy(bag, name) { if (Reflect.getOwnPropertyDescriptor(bag, name)?.enumerable === true) return false; return Reflect.get(bag, name) !== undefined; } function untouched(entry) { return `Nothing was read or started${entry === "resumeStream" ? " (the checkpoint was not read or claimed: it is still pending)" : ""}`; } function fenced(read, field, what, entry) { try { return read(); } catch (cause) { const thrown = boundedThrownName(cause); throw Object.assign(new Error(`${what} could not be read: the read threw ${thrown} — a bag whose read throws is refused rather than ignored or let out as the host's own error. ${untouched(entry)} — hand in a plain bag whose members read without throwing.`), { code: "config.invalid_argument", detail: { field, entry, thrownName: thrown } }); } } export function readLegInternals(internals, entry) { return fenced(() => ({ ...internals }), "RunInternals", "RunInternals", entry); } export function refuseHiddenLegFrameSeat(bag, entry) { if (!fenced(() => seatOutsideCopy(bag, "onLegFrame"), "onLegFrame", "RunInternals.onLegFrame", entry)) return; throw Object.assign(new Error(`RunInternals.onLegFrame: seat must be an own enumerable member of the bag you hand in — this bag answers one from its prototype or as a non-enumerable property, and the leg reads its own enumerable members only, so a seat it cannot read would hear nothing; refused rather than ignored. ${untouched(entry)} — write the seat as a plain member of the bag.`), { code: "config.invalid_argument", detail: { field: "onLegFrame", entry } }); } export function admitLegFrameSeat(internals, entry) { if (internals === undefined) return undefined; const isDrained = drained.delete(internals); for (const [key, replacement] of Object.entries(RETIRED_RUN_INTERNALS_KEYS)) { const hit = fenced(() => retiredKeyOf(internals, { [key]: replacement }), key, `RunInternals.${key}`, entry); if (hit !== undefined) throw retiredKeyRefusal(hit, "RunInternals"); } const read = readLegInternals(internals, entry); if (read.parentProject !== undefined) read.parentProject = captureProjectSeat(read.parentProject, "RunInternals.parentProject"); const seat = read.onLegFrame; if (seat === undefined) { refuseHiddenLegFrameSeat(internals, entry); return read; } if (typeof seat !== "function") { throw Object.assign(new Error(`RunInternals.onLegFrame must be a function (got ${seat === null ? "null" : typeof seat}) — a seat the engine cannot call is refused rather than ignored`), { code: "config.invalid_argument", detail: { field: "onLegFrame", entry }, }); } if (!isDrained) { throw Object.assign(new Error(`RunInternals.onLegFrame is refused on Runner.${entry}: the seat carries the frames of a leg the engine drains itself (Runner.runTask / Runner.resume), and this entry hands you the leg's stream, where the same frames ride. ${untouched(entry)} — drop the seat here and read the stream, or enter through ${entry === "runTaskStream" ? "Runner.runTask" : "Runner.resume"}.`), { code: "config.invalid_argument", detail: { field: "onLegFrame", entry } }); } return read; }