UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

146 lines (145 loc) • 9.39 kB
/** * `Runner.drain` — one call over what a Runner holds now, one receipt back. It composes the yield seats that already exist and * changes none of them: a foreground leg yields through the same turn-boundary attempt as `TaskSpec.preemptSignal` (the * Runner's own per-leg signal is the second source of that one test, answered once; after a `not_written` answer each later * call asks the leg again, at a boundary whose tool results go back to the model); background agents through the process registry's * `yieldBackground()`, asked only once every foreground leg has settled (a running parent decides its own background work); a * background shell is never suspended and is named; a park this Runner's legs minted is listed while its row is still pending. * Every class is read back after the call's window — from the legs' results, the registry's rows and each agent's own row writes, and * the checkpoint stores' rows — never from what was requested. */ import type { CheckpointStore, CheckpointToken } from "./checkpoint-store.js"; import { type GateKind, type ResumeGate } from "./pause-registry.js"; import type { TaskResult } from "./task-result.js"; import type { BackgroundUnyieldableReason, SemaTaskStatus } from "./task-registry-shared.js"; /** `Runner.drain`'s options. `waitMs` bounds the whole call (a finite, non-negative number of milliseconds): what has not * answered by then is read as it stands. */ export interface DrainOptions { readonly waitMs: number; } /** A durable park the receipt names: the leg, its checkpoint token, its gate and the resume word that continues it * (`Runner.resume(token, { gate: resume, ... })`; an approval also needs the host's decision). The token is a resume * capability: the receipt is the host's to read; a host that shows a park to anyone else hands out an opaque id of its own. */ export interface DrainParked { readonly taskId: string; readonly sessionId: string; readonly token: CheckpointToken; readonly gate: GateKind; readonly resume: ResumeGate; } /** A background shell row as the receipt names it. `sessionId`: the session a session-scoped `background_bash` or `monitor` row * belongs to; absent on a task-scoped one (its row records a task, not a session). */ interface DrainShell { readonly id: string; readonly type: "background_bash" | "monitor"; readonly retained?: true; readonly sessionId?: string; } /** A background agent as the receipt names it. `sessionId`: the session whose leg spawned it, as its row records it; absent * when the row records none. */ interface DrainAgent { readonly id: string; readonly sessionId?: string; } /** What one `Runner.drain` call came to, read back after the call's window. The host stops accepting work BEFORE it calls * `drain`; which process this was, when it stops and what replaces it are the host's drain to carry (the library knows no * instance). Workflow runs are not counted here: the registry's whole-registry request reaches them under its own rule. */ export interface DrainReceipt { /** The Runner's own legs running at the call. A delegated leg — a background agent's, a workflow run's member, a * synchronous child's — is not one: a synchronous child runs inside its parent's tool call, so the parent cannot reach a * clean boundary until the child returns (read `still_running` meanwhile) and the child itself is not asked to yield. * `suspend_lane_not_ready`: the leg cannot suspend durably (its manifest's `suspendLane.reasons` say why) and keeps * running; `still_running`: it had not answered by the end of the window. `startedDuringCall`: legs this Runner admitted * after the census, before the receipt returned — not asked to yield, counted nowhere else; non-empty ⇒ this receipt is * already stale, call again (once accepting has stopped). */ readonly foreground: { readonly parked: readonly DrainParked[]; readonly ended: readonly { readonly taskId: string; readonly kind: Exclude<TaskResult["terminal"]["kind"], "paused">; }[]; readonly notYielded: readonly { readonly taskId: string; readonly reason: "suspend_lane_not_ready" | "still_running"; }[]; readonly startedDuringCall: readonly { readonly taskId: string; }[]; }; /** The process registry's background agents running at the call. `requested`: whether the registry was asked to yield * (only once every foreground leg had settled). An agent is `parked` only once its own row's writes settled within the * window and its parked row landed; `unpersisted`: the store refused that row; `unconfirmed`: parked in the registry, its * own row's write not answered within the window. For `unpersisted` and `unconfirmed`: stopping the process now may lose * that agent's work — tell the user which ones; its row can be read again after the window to confirm. */ readonly background: { readonly requested: boolean; readonly parked: readonly (DrainAgent & { readonly token: string; })[]; readonly ended: readonly (DrainAgent & { readonly status: SemaTaskStatus | "gone"; })[]; readonly notYielded: readonly (DrainAgent & { readonly reason: BackgroundUnyieldableReason | "still_running" | "not_requested" | "unpersisted" | "unconfirmed"; })[]; }; /** The process registry's background shell rows running at the call, by their row's state at the end: a running process is * never suspended and the drain itself stops none (`TaskRegistry.killAllBackgroundShellsNow` does); a foreground leg that * parks tears down the shells it owns, as on a host's own `preemptSignal`, and those read `ended`. */ readonly shells: { readonly notYielded: readonly (DrainShell & { readonly reason: "not_suspendable"; })[]; readonly ended: readonly (DrainShell & { readonly status: SemaTaskStatus | "gone"; })[]; }; /** The parks this Runner's foreground legs minted in this process before the call whose rows are still pending, and every * pending park they minted is in the receipt (here, in `foreground.parked`, or in `parkedUnread`). The receipt is not the * store: a park an earlier process left pending, or one a background agent or another delegated leg parked before the call, * is not counted — list those with `CheckpointStore.listByScope(scope)` or `listBySession(query)`. */ readonly parked: readonly DrainParked[]; /** Parks of `parked`'s kind whose row could not be read within the window (the store threw, or did not answer in time): * neither shown pending nor shown gone. Not a park to count or to resume by its token; stopping now may lose it — tell the * user which ones; read the store again after the window to confirm. */ readonly parkedUnread: readonly DrainParked[]; } /** A foreground leg's yield as its turn boundary reads it: the leg's one signal (raised by the first drain call, and stays * raised), and how many drain calls have asked the leg so far. */ export interface LegYield { readonly signal: AbortSignal; readonly asks: () => number; } /** The Runner's two books (its running foreground legs, the parks they minted) and the drain over them. */ export declare class RunnerDrainSeats { private readonly claimed; private readonly legs; private readonly parks; /** One set per drain call in flight: the foreground legs this Runner admitted after that call's census. */ private readonly watchers; /** `claimed`: whether an actor of this Runner holds a settling claim on the token right now (a resume between its claim and * its duty decision) — such a park is not pending while it holds, and may come back pending: it stays in the book. */ constructor(claimed: (token: CheckpointToken) => boolean); /** Enter a foreground leg: its yield (the turn boundary's second yield source), its suspend-lane report (chained before the * caller's own), and its settle (with the result it settled on and the store its park is filed in). */ openLeg(taskId: () => string | undefined, callerReport: ((lane: { readonly ready: boolean; }) => void) | undefined): { readonly legYield: LegYield; readonly reportSuspendLane: (lane: { readonly ready: boolean; }) => void; readonly settle: (r: TaskResult | undefined, storeOf: () => CheckpointStore | undefined) => void; }; /** A resumed leg settled under another token and its consumed row is not coming back (the Runner's one judgment of that, * beside its other per-token records): the park leaves the book. */ forget(token: CheckpointToken): void; /** `Runner.drain`: `waitMs` is screened at the call (a bad value throws `RangeError`). */ drain(opts: DrainOptions): Promise<DrainReceipt>; private drainWithin; private receiptOf; /** A held park as the drain names it, read from its store: still `pending` ⇒ named; any other row ⇒ it left the book (it * was continued or expired elsewhere); a read that throws ⇒ named (the row was not shown to be gone). */ private stillPending; } export {};