@sema-agent/core
Version:
Stateless, task-oriented AI agent core
67 lines (66 loc) • 3.51 kB
JavaScript
import { inlineUntrusted } from "../core/untrusted-text.js";
import { isOccupantToken } from "../engine/session/repo-utils.js";
export function rootSessionAddressOf(root) {
if (root.rootSessionId === undefined)
return undefined;
return { sessionId: root.rootSessionId, occupant: root.rootSessionIncarnation, ...(root.rootSessionScope !== undefined ? { scope: root.rootSessionScope } : {}) };
}
export function treeRootAddressOf(anchor, own, taskScope) {
return rootSessionAddressOf(anchor) ?? { ...own, scope: taskScope };
}
export function rootSessionAddressKeyOf(address) {
if (address.unread === true)
return undefined;
return JSON.stringify([address.scope ?? null, address.sessionId, isOccupantToken(address.occupant) ? address.occupant : null]);
}
const NO_ROOT = { code: "no_root" };
export function rootSessionLaneOf(runner, root, unkept) {
if (root === undefined)
return { ok: false, cause: NO_ROOT };
return runner.sessionCompletionLane?.(root, unkept) ?? { ok: false, cause: { code: "no_route" } };
}
export function rootSessionSink(runner, root, mount) {
return (n, o) => {
const lane = rootSessionLaneOf(runner, root, mount);
if (lane.ok)
lane.sink(n, o);
else
mount?.(n, o);
};
}
function laneCauseClause(cause) {
switch (cause.code) {
case "no_root":
return "its row names no root session";
case "no_root_scope":
return "its row was written before rows recorded where the root session's box lives";
case "no_mailbox":
return "this deployment wires no mailbox store";
case "mailbox_unsafe":
return cause.reason;
case "ungrammatical":
return "the root session id is outside the box address grammar";
case "floor_off":
return "this deployment does not declare durable engine frames (`sessionBoxEngineFrames`)";
case "no_route":
return "this runner seat offers no session route";
}
}
export function resumeLaneMissingNotice(taskId, rootSessionId, lane) {
const head = `the parked agent ${taskId} was resumed by a drive whose task registry holds no park of it (it parked through another registry: in another process, before this one restarted, or in another registry of this process)`;
const tail = "and a submission still waiting on it elsewhere is not settled by it";
const root = rootSessionId !== undefined ? { rootSessionId } : {};
if (lane.ok && rootSessionId !== undefined) {
return {
code: "delegation.resume_lane_missing",
message: `${head}: its completion is routed to its delegation tree's root session ${inlineUntrusted(rootSessionId, 120)} — to that session's live run when it runs on this runner, else to the session's durable box, which its next run reads on any runner and in any process (a box that refuses it is reported on its own; a session that no longer holds the occupant the tree was born under, or a tree that recorded no occupant token for its root, has its completion discarded and announced) — ${tail}`,
detail: { taskId, ...root, path: "box" },
};
}
const cause = lane.ok ? NO_ROOT : lane.cause;
return {
code: "delegation.resume_lane_missing",
message: `${head}: its completion goes to the deployment's background channel because ${laneCauseClause(cause)}, ${tail}`,
detail: { taskId, ...root },
};
}