UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

67 lines (66 loc) • 4.05 kB
import { types } from "node:util"; import { scopeKeyOf } from "../isolation-scope.js"; import { describeFault, isPlainRecord, receive } from "../mods/kernel/receive.js"; import { principalNameFault, principalNameOf } from "../principal-name.js"; export function checkpointScopeOf(spec, seat) { return seat || spec.durableApproval?.scope || scopeKeyOf(spec); } const DECLARED_SCOPE_DATA = Object.freeze({ accessors: "refuse", depth: 1, others: "refuse", isProxy: (v) => types.isProxy(v) }); export function readDeclaredCheckpointScope(declared) { if (typeof declared !== "object" || declared === null) return { kind: "none", fault: declared === null ? "null, not an object" : `a ${typeof declared}, not an object` }; const data = receive(declared, DECLARED_SCOPE_DATA); if (!data.ok) { const f = data.fault.path[0]; return f === undefined || f === "scope" ? { kind: "none", ...(f === "scope" ? { at: "scope" } : {}), fault: `not plain data — ${describeFault(data.fault)}` } : { kind: "none", at: f === "ttlMs" ? "ttlMs" : "undeclared", fault: describeFault({ kind: data.fault.kind, path: [] }) }; } const record = data.value; if (!isPlainRecord(record)) return { kind: "none", fault: "a list, not a record" }; if (!Object.hasOwn(record, "scope")) return { kind: "none", fault: "no `scope` member" }; const scope = record.scope; if (typeof scope !== "string") return { kind: "none", fault: `a \`scope\` that is ${scope === null ? "null" : typeof scope}` }; const name = principalNameOf(scope); return name !== undefined ? { kind: "scope", scope: name } : { kind: "none", fault: `a \`scope\` that is ${principalNameFault(scope)}` }; } export function declaredCheckpointScope(declared) { const reading = readDeclaredCheckpointScope(declared); return reading.kind === "scope" ? reading.scope : undefined; } export function placementValueOrAbsent(value) { return value || undefined; } const COMPOSITION_ENTRIES = { cascade: true, verify: true, repair: true }; export function compositionLegRecord(leg) { if (leg === undefined) return undefined; const entry = leg.entry; if (typeof entry !== "string" || !Object.hasOwn(COMPOSITION_ENTRIES, entry)) { const e = new Error(`compositionLeg.entry must name a composition entry (${Object.keys(COMPOSITION_ENTRIES).join(" / ")}; got ${typeof entry === "string" ? "another word" : typeof entry}) — an identity no entry minted is refused, never recorded on a card.`); e.code = "config.invalid_argument"; e.detail = { field: "compositionLeg.entry" }; throw e; } return { entry: entry, leg: leg.leg, hostSessionId: leg.hostSessionId }; } export function workSessionOfLeg(leg) { return leg === undefined || leg.hostSessionId === "" ? undefined : leg.hostSessionId; } export function sameCompositionLeg(a, b) { return a.entry === b.entry && a.leg === b.leg && a.hostSessionId === b.hostSessionId; } export function restoreRowIdentity(internals, state) { const row = state?.legIdentity; const restored = { ...(internals?.rowTaskId === undefined && state?.rowTaskId !== undefined ? { rowTaskId: state.rowTaskId } : {}), ...(internals?.parentTaskId === undefined && row?.parentTaskId !== undefined ? { parentTaskId: row.parentTaskId } : {}), ...(internals?.rootSessionId === undefined && row?.rootSessionId !== undefined && row.rootSessionScope !== undefined ? { rootSessionId: row.rootSessionId, rootSessionScope: row.rootSessionScope, ...(row.rootSessionIncarnation !== undefined ? { rootSessionIncarnation: row.rootSessionIncarnation } : {}) } : {}), ...(internals?.explicitAgentName === undefined && row?.spawnName !== undefined ? { explicitAgentName: row.spawnName } : {}), }; return Object.keys(restored).length === 0 ? internals : { ...(internals ?? {}), ...restored }; } export function resumedDeclaredTaskId(config, state) { return config.taskId ?? state.legIdentity?.declaredTaskId; }