@sema-agent/core
Version:
Stateless, task-oriented AI agent core
58 lines (57 loc) • 3.93 kB
JavaScript
import { defaultTaskRegistry } from "../task-registry.js";
import { inlineUntrusted } from "../untrusted-text.js";
import { egressText } from "../untrusted-egress.js";
export const STOP_TAIL_DESCRIPTION_MAX_CHARS = 60;
const waitedDescription = (description) => inlineUntrusted(egressText("same-principal", description), STOP_TAIL_DESCRIPTION_MAX_CHARS);
export function stopTailWaitedFor(owed, access, registry = defaultTaskRegistry) {
if (owed === undefined)
return [];
return registry.runEndReading(owed.pending, access.read, access.owner).map(({ taskId, row, sweptWithOwner }) => row === undefined
? { taskId }
: { taskId, taskType: row.type, status: row.status, ...(row.description !== undefined ? { description: waitedDescription(row.description) } : {}), stopped: sweptWithOwner });
}
const TYPE_WORD = { background_agent: "agent", workflow: "workflow" };
function named(t) {
const what = `${t.taskType !== undefined ? (TYPE_WORD[t.taskType] ?? t.taskType) : "task"} ${t.taskId}`;
return t.description !== undefined ? `${what} "${t.description}"` : what;
}
function fatesSaid(waitedFor) {
const parts = [
["stopped with the run", waitedFor.filter((t) => t.stopped === true)],
["no longer waited for, left to their own lifetime", waitedFor.filter((t) => t.stopped === false)],
["not found at the run's end", waitedFor.filter((t) => t.stopped === undefined)],
];
return parts
.filter(([, ts]) => ts.length > 0)
.map(([what, ts]) => `${what}: ${ts.map(named).join(", ")}`)
.join("; ");
}
const counted = (n) => `${n} background task${n === 1 ? "" : "s"}`;
export function interruptUnconsumedNotice(waitedFor, origin, ids, crossedAxis) {
const tail = crossedAxis !== undefined
? `the answer stands as the result and the run ends completed; no interruption marker records the stop, and the result records the ${crossedAxis} limit it ended past.`
: "the answer stands as the result, the run ends completed, and no interruption marker or limit code records the stop.";
if (waitedFor.length === 0) {
return { code: "task.interrupt_unconsumed", message: `a stop arrived after the run had already given its answer: nothing was cut or stopped by it — ${tail}`, detail: { ...ids, origin, waitedFor: [] } };
}
return {
code: "task.interrupt_unconsumed",
message: `a stop arrived after the run had already given its answer, while it was still waiting for ${counted(waitedFor.length)} it started; ` +
`the stop ended that wait (${fatesSaid(waitedFor)}) — the result carries nothing from them, ${tail}`,
detail: { ...ids, origin, waitedFor: waitedFor.map((t) => ({ ...t })) },
};
}
export function ceilingTurnNote(waitedFor, ceilingMs) {
const left = waitedFor.length > 0 ? ` ${counted(waitedFor.length)} you started ${waitedFor.length === 1 ? "has" : "have"} not finished (${fatesSaid(waitedFor)}).` : "";
return (`[Engine note] The wait for the work you dispatched reached its ceiling (${ceilingMs} ms), so this run ends after this turn.${left} ` +
"Nothing from them reaches your result. Tell the person what is missing — call SendUserMessage with the gap and what it means for your conclusions — then end.");
}
export function waitCeilingReachedNotice(waitedFor, ceilingMs, ids) {
return {
code: "task.wait_ceiling_reached",
message: `the run gave its answer and waited ${ceilingMs} ms (its one-shot wait ceiling) for ${counted(waitedFor.length)} it started, then stopped waiting ` +
`(${fatesSaid(waitedFor)}) — the answer stands as the result and carries nothing from them. ` +
`The host can raise this wait's ceiling, or lift it so the wait lasts as long as the run's time limit allows.`,
detail: { ...ids, ceilingMs, waitedFor: waitedFor.map((t) => ({ ...t })) },
};
}