@sema-agent/core
Version:
Stateless, task-oriented AI agent core
67 lines (66 loc) • 4.87 kB
JavaScript
import { retiredKeyRefusal } from "../core/retired-keys.js";
import { strayMembersOf } from "../core/own-property-receipt.js";
import { readMember, readMemberOrUnreadable, UNREADABLE } from "../core/thrown-value.js";
import { describeRejectedValue } from "../core/tool-search-setting.js";
import { isPermissionMode, PERMISSION_MODES } from "../core/ask-origin.js";
import { takeDeniedChainRefusalToken } from "../core/runner/chain-refusal-throw.js";
const PARKED_RESUME_GATE_MEMBERS = Object.freeze(["permissionMode", "shellGate"]);
const gateRefusal = (message) => Object.assign(new Error(message), { code: "config.deps_retired_key", detail: { field: "inheritedGate" } });
export function parkedResumeGate(seat) {
if (seat === undefined)
return { seat: undefined };
if (typeof seat !== "object" || seat === null || Array.isArray(seat)) {
return { refusal: gateRefusal(`parkedResume.inheritedGate is ${seat === null ? "null" : Array.isArray(seat) ? "an array" : typeof seat}, not a record — hand the root's \`permissionMode\` and \`shellGate\` in a plain object, or leave the seat out`) };
}
const stray = strayMembersOf(seat, { subject: "parkedResume.inheritedGate", unit: "member", moment: "at the drive" }, PARKED_RESUME_GATE_MEMBERS)?.names[0];
if (stray !== undefined) {
const member = String(stray);
const replacement = member === "parentConstraints"
? "a host hands the root task's facts — its seat (`parentSpecSeat`), its current mode, and its own policy as `parkedResume.rootPolicy` — and the engine rebuilds the parked agent's chain from the row's record"
: "a host drive's gate seat reads the root's `permissionMode` and `shellGate` only; the rest of a parked agent's inherited gate is the row's own record";
return { refusal: Object.assign(retiredKeyRefusal({ key: member, replacement }, "parkedResume.inheritedGate"), { detail: { field: "inheritedGate" } }) };
}
const mode = readMemberOrUnreadable(seat, "permissionMode");
const floor = readMemberOrUnreadable(seat, "shellGate");
if (mode === UNREADABLE || floor === UNREADABLE)
return { refusal: gateRefusal(`parkedResume.inheritedGate.${mode === UNREADABLE ? "permissionMode" : "shellGate"} could not be read — hand plain values`) };
if (mode !== undefined && !isPermissionMode(mode)) {
return { refusal: gateRefusal(`parkedResume.inheritedGate.permissionMode is ${describeRejectedValue(mode)}, not one of ${PERMISSION_MODES.map((w) => JSON.stringify(w)).join(", ")} — hand the root's current mode, or leave the member out`) };
}
if (floor !== undefined && floor !== "always") {
return { refusal: gateRefusal(`parkedResume.inheritedGate.shellGate is ${describeRejectedValue(floor)}, not the shell floor "always" — hand "always" when the root runs under the floor, or leave the member out`) };
}
return { seat: { ...(mode !== undefined ? { permissionMode: mode } : {}), ...(floor === "always" ? { shellGate: "always" } : {}) } };
}
export function parkedResumeDoor(pr) {
try {
return parkedResumeGate(pr.inheritedGate);
}
catch (e) {
return { refusal: e };
}
}
export function parkedResumeUnhanded(seat, gate) {
if (seat === undefined) {
return Object.assign(new Error("the parked agent was not resumed: the drive hands no `parentSpecSeat` — mint it with `parentSpecSeatOf` from the root task's stored spec, so the agent reads its instruction files, language and directories; the approval stays pending"), { code: "config.parent_spec_seat_missing" });
}
if (gate?.permissionMode === undefined) {
return Object.assign(new Error("the parked agent was not resumed: the drive hands no `parkedResume.inheritedGate.permissionMode` — hand the root task's current mode (the row's is the mode at the park); the approval stays pending"), { code: "config.invalid_argument", detail: { field: "inheritedGate" } });
}
return undefined;
}
export function unresumableChainRefusal(e, token) {
if (takeDeniedChainRefusalToken(e) !== token)
return undefined;
const code = readMember(e, "code");
const reason = readMember(readMember(e, "detail"), "reason");
const named = typeof code === "string" ? code : "resume.parent_constraint_mismatch";
return { code: named, cause: typeof reason === "string" ? `${named}, ${reason}` : named };
}
export function spawnDepthUnknownNotice(taskId) {
return {
code: "delegation.spawn_depth_unknown",
message: `the background agent ${taskId} was revived from a row that records no delegation depth (a row an earlier engine release wrote), so it runs without the delegation tool: the depth it was born at cannot be read, and the depth of the tool that woke it is not its own`,
detail: { taskId },
};
}