@sema-agent/core
Version:
Stateless, task-oriented AI agent core
98 lines (97 loc) • 6.8 kB
TypeScript
import type { AssistantMessage } from "../../internal/llm.js";
import type { ResolvedReasoning } from "../../brain/reasoning.js";
import type { PushQueue } from "../push-queue.js";
import type { SessionStore } from "../session.js";
import { type CompactOutcome, type DelegationLifecycleEvent, type OwedUserInputAccount, type TaskEvent, type TaskResult, type TaskSpec } from "../types.js";
import { type Stats } from "./assemble-result.js";
import type { EndOfTaskCompaction, InstallingPhaseResult, ManualCompactRef, Prepared, ResumeRun, RunInternals, RunnerDepsSeat, RunState, SuspendReap, TaskIdRef } from "./contracts.js";
export interface RunSettleAndTeardownInput {
/** borrowed-readonly — the task spec: the hooks slot, the suggestion opt-in, the retain / forward flags, the task id. */
spec: TaskSpec;
/** borrowed-mutable — the run's event queue: the compaction / human-input / task_progress / done frames, then CLOSED here. */
queue: PushQueue<TaskEvent>;
/** borrowed-mutable — the leg's prepared seat: the settle-time seal and workspace write, the refs the result assembly reads, the env the teardown reaps. */
prepared: Prepared;
/** borrowed-readonly — the resume plan when this leg resumed a checkpoint (the reopen compensation and the unpin read it). */
resume: ResumeRun | undefined;
/** borrowed-readonly — the trusted run-scoped channel: the delegation task type, cycle seq, parent task id and the display-sink forward. */
internals: RunInternals | undefined;
/** borrowed-readonly — the run's mutable state, read for the telemetry / degrade / limits / counters the result and the frames report. */
rs: RunState;
/** borrowed-mutable — this leg's usage counters: the nested / cumulative folds, the cache-hit rate, the mechanism counters are written here. */
stats: Stats;
/** borrowed-readonly — the run's event-identity mint. */
ident: () => {
eventId: string;
parentToolCallId?: string;
sourceTaskId?: string;
};
/** borrowed-readonly — the spawning tool call's id when this task runs AS A SUB-AGENT (gates the terminal tick). */
parentToolCallId: string | undefined;
/** borrowed-readonly — the sub-agent display name for the terminal tick. */
subagentName: string | undefined;
/** borrowed-readonly — the loop-liveness latch: `userHalted` = the halt verb was ACCEPTED (the unconsumed-halt notice's input; it does not sign the seat). */
loopLatch: {
readonly userHalted: boolean;
};
/** borrowed-readonly — the hard walltime timer: `fired` and `latenessMs` ride the result and the `task.end` frame. */
timeout: {
readonly fired: boolean;
readonly latenessMs?: number;
};
/** borrowed-readonly — the run's own committed tail (the read-state seal). */
ownCommittedTailRef: {
readonly current: string | undefined;
};
/** borrowed-readonly — the delegation-lifecycle emission closure (the terminal station is here). */
emitDelegationLifecycle: (event: DelegationLifecycleEvent) => void;
/** borrowed-mutable — the stream layer's backstop carrier: the owed delegation terminal is cleared here. */
taskIdRef: TaskIdRef | undefined;
/** borrowed-mutable — the manual /compact seat: the parked waiters are mooted and the channel closed before `done`. */
manualCompactRef: ManualCompactRef;
/** borrowed-readonly — the boundary lane's drain of the manual-compact waiters (with the outcome every waiter receives). */
drainManualCompact: (outcome: CompactOutcome) => void;
/** borrowed-readonly — the leg lane's final assistant message (undefined on every no-prompt arm). */
final: AssistantMessage | undefined;
/** borrowed-readonly — the adopted terminal cause (the leg's throw, or what the terminal-adoption lane lifted over it). */
threw: unknown;
/** borrowed-readonly — the live-abort flag captured before the leg's release-abort. */
abortedLive: boolean;
/** borrowed-readonly — the leg's snapshot of `loopLatch.userInterrupted`: names the origin of a stop that landed after the answer settled. */
userInterruptedLive: boolean;
/** borrowed-readonly — the terminal-adoption lane's ONE evaluation of `answerSettled` (a clean `"stop"` final, no continuation
* lost). Read by the arbitration (`ResultFlags.answerSettled`), the teardown's stop-source attribution and the notice. */
answerSettled: boolean;
/** borrowed-readonly — the answered-but-never-collected deliveries the leg's finally settled. */
strandedHumanAnswers: ReadonlyArray<{
deliveryId: string;
toolCallId: string;
}>;
/** borrowed-readonly — the reasoning seat's resolution, as the read face on its Result (the result's `effectiveReasoning`). */
reasoningResolution: {
readonly current: ResolvedReasoning | undefined;
};
/** borrowed-readonly — the end-of-task compaction's outcome (the terminal-adoption lane's `finish` call). */
comp: EndOfTaskCompaction;
/** borrowed-readonly — the run's compaction breaker: the ONE source of `TaskResult.compaction.breakerOpen`. */ compactionBreaker: {
readonly failures: number;
};
/** borrowed-readonly — the adoption lane's unsettled undrained-input account, closed below (after the reopen). */
owedUserInput: OwedUserInputAccount;
/** borrowed-readonly — the stream's result setter: the assembled TaskResult is published through it exactly once. */
setResult: (r: TaskResult) => void;
/** borrowed-readonly — the stream's suggestions seat: the fire-and-forget suggestion pass's promise is handed to it. */
onSuggestions: (p: Promise<string[]>) => void;
/** borrowed-readonly — the stream's reap-stash for a committed pause (what `destroy()` reaps when the run SUSPENDED). */
onSuspend: (reap: SuspendReap) => void;
/** borrowed-readonly — the Runner's deployment deps, read LIVE (`onError`, `hooks`, `checkpointStore`, `executionEnv`, `onNotice`; the
* suggestion pass reads `brain` / `pricing` / `roles` and the owned-env teardown `onError` through it too); the one home of why it
* is a seat and not a captured object is {@link RunnerDepsSeat}. */
runner: RunnerDepsSeat;
/** borrowed-readonly — the session store: the resumed-terminal leg unpins the checkpoint's session through it. */
sessions: SessionStore;
}
/** Nothing comes back: the result is published through `setResult`, the queue is closed, the teardown legs ran. */
export interface RunSettleAndTeardownResult extends InstallingPhaseResult {
}
export declare function runSettleAndTeardown(input: RunSettleAndTeardownInput): Promise<RunSettleAndTeardownResult>;