UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

398 lines (397 loc) • 21.9 kB
import { summarizeWorkflowRun } from "./workflow-run-store.js"; import { callKeyOrdinal } from "./workflow-journal-store.js"; import { ANONYMOUS_SCOPE } from "./isolation-scope.js"; import { delimitUntrusted } from "./untrusted-text.js"; import { boundedRedactedSummary } from "./untrusted-egress.js"; import { clipWithFilePointer, OUTPUT_BUDGET_MIN_CHARS } from "./tool-errors.js"; import { resolveEnvBudget } from "./env-budget.js"; import { isOccupantToken, sameOccupant } from "../engine/session/repo-utils.js"; export const SEMA_TASK_TYPES = Object.freeze(["background_bash", "workflow", "background_agent", "monitor", "peer_task"]); export const SEMA_TASK_HANDLE_PREFIXES = Object.freeze({ background_bash: "b", workflow: "w", background_agent: "a", monitor: "m", peer_task: "p", }); export const PEER_TASK_HANDLE_RE = new RegExp(`^${SEMA_TASK_HANDLE_PREFIXES.peer_task}[0-9a-f]{16}$`); export function monotonicNow() { return performance.now(); } export const ACTIVITY_INSTANT_SLACK_MS = 1_000; export function isActivityInstant(at, now = monotonicNow()) { return typeof at === "number" && Number.isFinite(at) && at >= 0 && at <= now + ACTIVITY_INSTANT_SLACK_MS; } export function activityStamp(wall = Date.now()) { return { updatedAt: wall, lastActivityMono: monotonicNow() }; } export function stampActivity(handle, wall = Date.now(), mono = monotonicNow()) { Object.assign(handle, { updatedAt: wall, lastActivityMono: Math.max(handle.lastActivityMono, mono) }); } export function taskReadHandleText(h) { const file = namedHandle(h.outputFile); if (file !== undefined) return `output: Read ${file}`; const ref = namedHandle(h.artifactRef); if (ref !== undefined) return `result: ReadToolResult (ref "${ref}")`; return undefined; } function namedHandle(value) { return value !== undefined && value.trim() !== "" ? value : undefined; } export function occupantStamp(input) { return input.rootSessionIncarnation !== undefined ? { rootSessionIncarnation: input.rootSessionIncarnation } : {}; } export function callOccupant(ctx, mount) { return occupantStamp({ rootSessionIncarnation: ctx?.rootSessionIncarnation ?? mount.occupant?.() }); } export function occupantAdmits(handle, access) { return !isOccupantToken(access.rootSessionIncarnation) || !isOccupantToken(handle.rootSessionIncarnation) || sameOccupant(handle.rootSessionIncarnation, access.rootSessionIncarnation); } export const defaultMonitorTimers = { setInterval: (fn, ms) => { const h = setInterval(() => void fn(), ms); h.unref?.(); return h; }, clearInterval: (h) => clearInterval(h), now: () => Date.now(), }; export function isLoopTask(handle) { return handle.type === "background_agent" || handle.type === "peer_task"; } export function holdsLane(handle) { return handle.status === "running" || (handle.type === "peer_task" && !handle.abort.signal.aborted); } const BLOCK_POLL_INTERVAL_MS = 400; export const sleepPollStep = (deadline, signal) => sleepMs(deadline === undefined ? BLOCK_POLL_INTERVAL_MS : Math.max(1, Math.min(BLOCK_POLL_INTERVAL_MS, deadline - Date.now())), signal); export const sleepMs = (ms, signal) => new Promise((resolve) => { const t = setTimeout(done, ms); function done() { clearTimeout(t); signal?.removeEventListener("abort", done); resolve(); } signal?.addEventListener("abort", done, { once: true }); }); export const MONITOR_BATCH_WINDOW_MS = 200; export const MONITOR_DEFAULT_TIMEOUT_MS = 300_000; export const MONITOR_MAX_TIMEOUT_MS = 3_600_000; export const MONITOR_MAX_BATCHES_PER_MINUTE = 50; export const MONITOR_STORM_BURST = 10; export const MONITOR_STORM_KILL_AFTER_MS = 30_000; const MONITOR_EVENT_RESULT_CAP = 2_000; const MONITOR_EVENT_LINE_CAP = 500; export const MONITOR_LINE_BUF_CAP = 1_048_576; export function clipMonitorEvent(s) { return s.length <= MONITOR_EVENT_RESULT_CAP ? s : `${s.slice(0, MONITOR_EVENT_RESULT_CAP)}\n…[+${s.length - MONITOR_EVENT_RESULT_CAP} chars truncated]`; } export function clipMonitorLine(s) { return s.length <= MONITOR_EVENT_LINE_CAP ? s : `${s.slice(0, MONITOR_EVENT_LINE_CAP)}...(truncated)`; } export function terminalTaskSummary(kind, label, status, exitCode) { if (kind === "monitor") { if (status === "completed") return `Monitor "${label}" stream ended`; if (status === "failed") return `Monitor "${label}" script failed${exitCode !== undefined ? ` (exit ${exitCode})` : ""}`; return `Monitor "${label}" stopped`; } if (status === "completed") return `Background command "${label}" completed${exitCode !== undefined ? ` (exit code ${exitCode})` : ""}`; if (status === "failed") return `Background command "${label}" failed${exitCode !== undefined ? ` with exit code ${exitCode}` : ""}`; return `Background command "${label}" was stopped`; } const TASK_OUTPUT_DEFAULT_CHARS = 32_000; export const TASK_OUTPUT_MAX_CHARS = 160_000; function taskMaxOutputChars() { return resolveEnvBudget(process.env.TASK_MAX_OUTPUT_LENGTH, { name: "TASK_MAX_OUTPUT_LENGTH", unit: "characters", default: TASK_OUTPUT_DEFAULT_CHARS, min: OUTPUT_BUDGET_MIN_CHARS, max: TASK_OUTPUT_MAX_CHARS }); } export function clipTaskOutput(s, fullOutputPath) { return clipWithFilePointer(s, taskMaxOutputChars(), fullOutputPath); } export function statusFromBackground(status, exitCode) { if (status === "running") return "running"; if (status === "killed") return "killed"; if (status === "failed") return "failed"; if (status === "exited") return exitCode === 0 ? "completed" : "failed"; return "failed"; } export function rollSpoolText(spool, s, cap) { if (s.length <= cap) return s; const half = Math.floor(cap / 2); spool.rolledChars += s.length - 2 * half; return s.slice(0, half) + s.slice(s.length - half); } export function accountDroppedBytes(spool, poll) { const d = poll.bytesDroppedBeforeCursor ?? 0; if (d > 0) spool.droppedBytes = (spool.droppedBytes ?? 0) + d; else if (poll.truncated === true) spool.dropUnknown = true; } export function dropFactsOf(acct) { return { ...((acct.droppedBytes ?? 0) > 0 ? { bytesDroppedBeforeCursor: acct.droppedBytes } : {}), ...(acct.dropUnknown === true ? { dropUnknown: true } : {}), }; } export function spoolDropNote(acct) { const facts = dropFactsOf(acct); const n = facts.bytesDroppedBeforeCursor; if (n !== undefined && facts.dropUnknown === true) return `[!] at least ${n} earlier byte(s) permanently dropped before this window, and an unknown further amount (8MB buffer evicted)\n`; if (n !== undefined) return `[!] ${n} earlier byte(s) permanently dropped before this window (8MB buffer evicted)\n`; if (facts.dropUnknown === true) return "[!] an unknown amount of earlier output was permanently dropped before this window (buffer evicted)\n"; return ""; } export const TASK_OUTPUT_REPLAY_KINDS = Object.freeze(["full", "cursor"]); export const REPLAY_FACE = { full: { stdout: "stdout (full, re-readable)", stdoutWhenGapped: "stdout (re-readable spool — INCOMPLETE, see note above)", stderr: "stderr" }, cursor: { stdout: "new stdout", stdoutWhenGapped: undefined, stderr: "new stderr" }, }; export const SPOOL_REPLAY = "full"; export const INCREMENT_REPLAY = "cursor"; export function stdoutHeadingOf(replay, gapDisclosed) { const face = REPLAY_FACE[replay]; return gapDisclosed ? (face.stdoutWhenGapped ?? face.stdout) : face.stdout; } export function renderSpoolBody(spool, opts) { const timedOutClause = spool.timedOut === true ? ` (timed out after ${spool.timeoutSec !== undefined ? `${spool.timeoutSec}s` : "its background time budget"})` : ""; const displayStatus = (spool.exitCode !== undefined ? `exited(code ${spool.exitCode})` : opts.running ? "running" : opts.handleStatus) + timedOutClause; const rolledNote = spool.rolledChars > 0 ? `[!] ${spool.rolledChars} earlier char(s) dropped from the spool (memory bound)\n` : ""; const droppedNote = spoolDropNote(spool); const stdoutHeading = stdoutHeadingOf(SPOOL_REPLAY, droppedNote !== "" || opts.drainNote !== ""); return (`status: ${displayStatus}\n${rolledNote}${droppedNote}${opts.drainNote}--- ${stdoutHeading} ---\n${opts.shape(spool.stdout) || "(none)"}\n` + `--- ${REPLAY_FACE[SPOOL_REPLAY].stderr} ---\n${opts.shape(spool.stderr) || "(none)"}`); } export function droppedGapNote(spool) { const facts = dropFactsOf(spool); const n = facts.bytesDroppedBeforeCursor; const tail = "(execution-environment tail buffer evicted) — the event stream and this spool are INCOMPLETE."; if (n !== undefined && facts.dropUnknown === true) return ` [!] At least ${n} earlier byte(s) of output were permanently dropped before this window, and an unknown further amount ${tail}`; if (n !== undefined) return ` [!] ${n} earlier byte(s) of output were permanently dropped before this window ${tail}`; if (facts.dropUnknown === true) return ` [!] An unknown amount of earlier output was permanently dropped before this window ${tail}`; return ""; } export function firstDropNote(spool) { if (spool.dropNotified === true) return ""; const note = droppedGapNote(spool); if (note !== "") spool.dropNotified = true; return note; } export function alreadyTerminalStopNote(id, status) { return `${id} already ${status} — nothing to stop; its completion notification carries its final state.`; } export function assertOwnership(input, site) { if (input.owner === undefined || input.scope === undefined) { throw new Error(`TaskRegistry.${site}: owner AND scope are both required (access is default-deny on both axes; use scope "${ANONYMOUS_SCOPE}" for a single-tenant deployment)`); } } export function canAccessWorkflowRun(run, access, held) { if (access.scope === undefined || run.scope !== access.scope) return false; if (held !== undefined && !occupantAdmits(held, access)) return false; if (access.owner !== undefined && run.sourceTaskId !== undefined && run.sourceTaskId === access.owner) return true; if (access.sessionId !== undefined && run.originatingSessionId !== undefined && run.originatingSessionId === access.sessionId) return true; return false; } const WORKFLOW_PARTIAL_RESULTS_MAX = 5; const WORKFLOW_AGENT_ROWS_MAX = 60; const WORKFLOW_POLL_BOUNDS = Object.freeze({ rows: WORKFLOW_AGENT_ROWS_MAX, partialResults: WORKFLOW_PARTIAL_RESULTS_MAX }); export const WORKFLOW_RECORD_WHOLE = Object.freeze({ rows: Number.POSITIVE_INFINITY, partialResults: Number.POSITIVE_INFINITY }); function workflowAgentRow(a, parkWaits) { const wait = a.status === "parked" && a.parkedCheckpointToken !== undefined ? parkWaits?.get(a.parkedCheckpointToken) : undefined; return { ordinal: callKeyOrdinal(a.callKey), label: a.label, ...(a.phase !== undefined ? { phase: a.phase } : {}), status: a.status, ...(wait !== undefined ? { park: wait.kind === "moved_on" ? { kind: wait.kind, runIds: [...wait.runIds] } : { ...wait } } : {}), ...(a.errorCode !== undefined ? { errorCode: a.errorCode } : {}), ...(a.errorMessage !== undefined ? { error: delimitUntrusted(`workflow agent ${a.label} error`, boundedRedactedSummary(a.errorMessage, 200)) } : {}), ...(a.startedAt !== undefined && a.endedAt !== undefined ? { elapsedMs: a.endedAt - a.startedAt } : {}), ...(a.replayed === true ? { replayed: true } : {}), }; } function budgetOvershootNote(overshoot) { if (overshoot === undefined) return ""; const unsettled = overshoot.unsettledTokens ?? 0; const total = overshoot.spentTokens + unsettled; return (` token budget OVERSHOT: this run spent ${total.toLocaleString()} total tokens against a ${overshoot.budgetTokens.toLocaleString()} ceiling ` + `(over by ${(total - overshoot.budgetTokens).toLocaleString()}` + `${unsettled > 0 ? `, of which ${unsettled.toLocaleString()} was observed on agents still in flight at the terminal and never settled` : ""}).`); } function timeoutInterruptionNote(interruption) { if (interruption === undefined) return ""; return (` INTERRUPTED by the workflow's total timeout (${interruption.timeoutMs.toLocaleString()}ms): when the deadline fired ` + `${interruption.agentsCompleted} agent(s) had completed, ${interruption.agentsFailed} had failed, and ` + `${interruption.agentsInFlight} were still in flight — the failed run status is the deadline's, not a verdict on those agents.` + (interruption.approvalWaitedMs !== undefined ? ` The longest single agent wait on a human approval reported for this run was ${interruption.approvalWaitedMs.toLocaleString()}ms` + (interruption.approvalWaitedMs > interruption.timeoutMs ? ` — MORE than the ${interruption.timeoutMs.toLocaleString()}ms window itself, so the deployment's approval clock is` + ` counting time this run did not (an earlier leg's wait carried forward, or a mis-scaled figure). Read it as` + ` "waiting dominated", not as a slice of this window.` : ` — that much of the ${interruption.timeoutMs.toLocaleString()}ms window was one agent BLOCKED on a person` + ` rather than working (the longest single wait, not a total).`) + ` Raising the timeout only helps if that approval gets answered.` : "")); } export function formatWorkflowRun(run, bounds = WORKFLOW_POLL_BOUNDS, parkWaits) { const summary = summarizeWorkflowRun(run); const done = run.agents.filter((a) => a.status === "completed" || a.status === "failed").length; const failed = run.agents.filter((a) => a.status === "failed").length; const allRows = run.agents.map((a, i) => ({ a, i })); const overCap = allRows.length > bounds.rows; const keptRows = overCap ? (() => { const urgent = allRows.filter((r) => r.a.status !== "completed"); const budget = Math.max(0, bounds.rows - urgent.length); const fill = allRows.filter((r) => r.a.status === "completed").slice(0, budget); return [...urgent, ...fill].sort((x, y) => x.i - y.i); })() : allRows; const agentRows = keptRows.map((r) => workflowAgentRow(r.a, parkWaits)); const endedUnfinished = run.status !== "running" && run.status !== "completed"; const finishedAgents = endedUnfinished ? run.agents.filter((a) => a.status === "completed" && a.output !== undefined && a.output !== "") : []; const stopper = run.status === "killed" ? run.stoppedBy : undefined; const endedWord = run.status === "killed" ? (stopper !== undefined ? "stopped (who stopped it is under `stoppedBy`)" : "stopped") : "failed"; return { content: JSON.stringify({ task_id: run.id, runId: run.id, type: "workflow", status: run.status, ...(summary.parked ? { parked: true } : {}), ...(summary.name ? { name: summary.name } : {}), ...(summary.currentPhase ? { currentPhase: summary.currentPhase } : {}), agents: { done, total: run.agents.length, failed }, agent_runs: agentRows, ...(overCap ? { agent_runs_note: `showing ${agentRows.length} of ${run.agents.length} agent rows (every non-completed row kept; completed rows trimmed).` } : {}), startedAt: run.startedAt, ...(run.endedAt !== undefined ? { endedAt: run.endedAt, elapsedMs: run.endedAt - run.startedAt } : {}), ...(run.resume !== undefined ? { resume: { from_run_id: run.resume.fromRunId, journal_entries: run.resume.journalEntries, replayed: run.resume.replayed, ...(run.resume.divergedAtOrdinal !== undefined ? { replay_stopped_at: run.resume.divergedAtOrdinal, replay_stopped_reason: run.resume.divergedReason } : {}), }, } : {}), ...(run.journalSkips !== undefined && run.journalSkips > 0 ? { journal_skipped: run.journalSkips } : {}), ...(endedUnfinished && run.error ? { error: delimitUntrusted("workflow error", boundedRedactedSummary(run.error, 500)) } : {}), ...(stopper !== undefined ? { stoppedBy: delimitUntrusted("workflow stopper", boundedRedactedSummary(stopper, 64)) } : {}), ...(run.errorCode !== undefined ? { errorCode: run.errorCode } : {}), ...(run.status === "completed" && run.result !== undefined ? { result: delimitUntrusted("workflow result", run.resultFull ?? run.result) } : {}), ...(finishedAgents.length > 0 ? { partial_results: finishedAgents.slice(0, bounds.partialResults).map((a) => ({ agent: a.label, output: delimitUntrusted(`workflow agent ${a.label} output`, a.output), })), ...(finishedAgents.length > bounds.partialResults ? { partial_results_note: `showing ${bounds.partialResults} of ${finishedAgents.length} completed agent outputs; the full set remains retrievable from the workflow run record (getWorkflowRun / the workflow journal).` } : {}), } : {}), note: (run.status === "running" ? "still running — poll again shortly." : run.status === "completed" ? run.result !== undefined ? "completed — `result` above is the workflow's return value." : "completed — this run predates result persistence; its result was delivered on the completion notification." : finishedAgents.length > 0 ? `${endedWord} — see error. ${finishedAgents.length} agent(s) had already completed before the run ended; their outputs are inlined under \`partial_results\`.` : `${endedWord} — see error.`) + timeoutInterruptionNote(summary.timeoutInterruption) + budgetOvershootNote(summary.budgetOvershoot), }), details: { task_id: run.id, type: "workflow", status: run.status, retrieval_status: "success", ...(run.completionId !== undefined ? { completionId: run.completionId } : {}), ...(run.errorCode !== undefined ? { errorCode: run.errorCode } : {}), details: summary, }, }; } export const DURABLE_AGENT_HEARTBEAT_MS = 60_000; export const DURABLE_AGENT_HANDLE_RE = /^a[0-9a-f]{6,32}$/; export function engineAgentName(handle) { if (!DURABLE_AGENT_HANDLE_RE.test(handle)) { throw new TypeError(`engineAgentName: ${JSON.stringify(handle)} is not an agent handle (expected "a" followed by 6-32 hex digits)`); } return `agent-${handle.slice(1, 13)}`; } export function chosenAgentName(handle, name) { if (name === undefined) return undefined; return DURABLE_AGENT_HANDLE_RE.test(handle) && name === engineAgentName(handle) ? undefined : name; } export const BG_AGENT_REAP_STOP_ERROR = "stopped (parent task ended or TaskStop)"; export function normalizeAgentName(s) { return s .normalize("NFKC") .replace(/[\p{Cc}\p{Cf}]/gu, (t) => (/\s/.test(t) ? t : "")) .trim() .toLowerCase() .replace(/\s+/g, "-"); } export function canAccess(handle, access) { if (handle.scope === undefined || access.scope === undefined || access.scope !== handle.scope) return false; if (handle.owner === undefined) return false; if (!occupantAdmits(handle, access)) return false; if (access.owner === handle.owner) return true; if (handle.sessionScoped && access.sessionId !== undefined && access.sessionId === handle.owner) return true; if (handle.type === "workflow" && access.sessionId !== undefined && handle.originatingSessionId === access.sessionId) return true; if (handle.type === "background_agent" && access.sessionId !== undefined && handle.rootSessionId === access.sessionId) return true; return false; } export function isLiveSessionResidentAgent(handle) { return handle.type === "background_agent" && handle.status === "running" && handle.sessionScoped === true; } export const PARKED_CLAIM_ROLLBACKS = Object.freeze(["rolled_back", "failed", "lost", "lost_unredeemable", "retry"]); export const PARKED_CLAIM_ROLLBACK_FACE = { rolled_back: { carriesTerminal: false, lead: "parked resume rolled back — the approval is still pending" }, failed: { carriesTerminal: true, lead: "parked resume failed — the approval is no longer redeemable" }, lost: { carriesTerminal: false, lead: "parked resume did not proceed — another transition owns this row and speaks for its cycle" }, lost_unredeemable: { carriesTerminal: false, lead: "parked resume did not proceed — the approval is no longer redeemable, and another transition owns this row and speaks for its cycle" }, retry: { carriesTerminal: false, lead: "parked resume did not proceed — its rollback could not complete against the stores (a reservation that still stands is cleared by the stale-claim sweep)" }, };