@sema-agent/core
Version:
Stateless, task-oriented AI agent core
43 lines (42 loc) • 1.71 kB
JavaScript
export class BackgroundShellError extends Error {
code;
cause;
constructor(code, message, cause) {
super(message);
this.code = code;
this.cause = cause;
this.name = "BackgroundShellError";
}
}
export function retainsBackgroundProcesses(lanes) {
return (lanes.env === true || lanes.task === true || lanes.marked === true) && lanes.envDying !== true;
}
export function envDeclaresRetain(env) {
const caps = env.backgroundCapabilities;
return typeof caps === "object" && caps !== null && caps.retainBackgroundProcesses === true;
}
export function hasBackgroundShell(env) {
const e = env;
return (typeof e.spawnBackground === "function" &&
typeof e.pollBackground === "function" &&
typeof e.killBackground === "function" &&
typeof e.disposeBackgroundShells === "function" &&
typeof e.backgroundCapabilities === "object" &&
e.backgroundCapabilities !== null &&
e.backgroundCapabilities.supported === true);
}
export async function sweepBackgroundShells(env, attribution, opts) {
try {
attribution?.markStopSourceForEnvSweep(env, "env_sweep", opts?.except);
}
catch {
}
await env.disposeBackgroundShells(opts?.except !== undefined ? { except: opts.except } : undefined);
}
export async function sweepRunBoundaryBackgroundShells(env, registry, lanes) {
const envDying = lanes.envDying === true;
if (retainsBackgroundProcesses({ env: envDeclaresRetain(env), task: lanes.task, envDying }))
return;
const except = envDying ? [] : registry.runningShellIds(env);
await sweepBackgroundShells(env, registry, except.length > 0 ? { except } : undefined);
}