UNPKG

@sema-agent/core

Version:

Stateless, task-oriented AI agent core

43 lines (42 loc) • 3.42 kB
/** * The run's end: one per run, and the one place a run takes back everything it handed to something that outlives it. * * A run reaches holders that can outlive it — the caller's `TaskSpec.signal` (a host may keep one controller for many runs, or * never abort it), the caller-stop gate composed from it, a parent leg's tool signal a child run is handed, and the process-level * seats of its session (the idle machine, the held queue of each box the leg drains), which live as long as the process. What the * run leaves on such a holder — a listener, a sink — stays reachable for as long as the holder does, and so does everything it * closes over (the run's prepared state, its conversation, its tool table, its images). A composed signal (`AbortSignal.any`) or a * timeout signal that carries an abort listener is held by the runtime itself until it aborts, so dropping the signal frees nothing * either. * * The rule: the run hands such a thing over only through its end — {@link RunEnd.listen} for a listener on a signal it does not * own, {@link RunEnd.lend} for anything else, naming how it is taken back. The stream leg mints the end when the run starts and * ends it once the run has settled — its result published by the run body, or by the failure backstop after a throw — so every * hand-over is taken back whether or not the run reached its loop, and the host owes nothing after the run. A hand-over whose job * ends sooner is taken back sooner (the release each verb returns); none outlives the run. Ending the run fires nothing and * answers nothing: the caller-stop gate still answers "did the caller ask this run to stop?". * * A listener's release is an explicit `removeEventListener`, not the `{ signal }` listener option: the runtime keeps the latest * listener hung with that option reachable through its target for as long as the target lives, so on a host's long-lived * signal it would keep the last run reachable. */ export interface RunEnd { /** * Hear `signal` abort, once, until the run ends. The return takes the listener off sooner (idempotent). Hangs nothing once * the run ended. A signal that already fired is the caller's to read: an abort is not replayed to a late listener. */ listen(signal: AbortSignal, onAbort: () => void): () => void; /** * Something was handed to a holder that outlives the run: `takeBack` takes it back, once — at the run's end, or sooner through * the return (idempotent). Lent after the run ended, it is taken back at once. `takeBack` must not throw; the end runs every * take-back before it reports one that did. */ lend(takeBack: () => void): () => void; /** The run ended: every hand-over not yet taken back is taken back; idempotent. Throws, once every take-back ran, when one threw. */ end(): void; } /** The stream leg's end of the run: `runEnd` ends once `settled` settles, either way. A take-back that threw (reported by * {@link RunEnd.end} once every other one ran) goes to `onError`, not to the process as an unhandled rejection; the run's result * stands. `onError` must not throw: the stream leg hands the deployment's error sink, which the Runner guards where it takes it. */ export declare function endOnSettle(runEnd: RunEnd, settled: Promise<unknown>, onError: (err: unknown) => void): void; export declare function mintRunEnd(): RunEnd;