import { TurnAbortedError } from "./turn-aborted-error.js"; /** * Turn-scoped AbortController registry. * * Every inbound message handler invocation runs inside a turn controller. * The controller's signal threads through: * * - The Stage-1 response-handler LLM call * - Response-handler field evaluators * - The planner loop and per-step LLM calls * - Action handlers * - Sub-process / fetch / sub-agent spawns * * When the user (or a sibling field-evaluator like threadOps' abort op) wants * to abort the turn, they call `registry.abortTurn(roomId, reason)`. This * fires the controller, which propagates through every consumer that respects * the signal. * * Synchronous vs background: * * - Sync sub-tasks share the parent's signal directly. * - Background sub-agents (Claude Code / Codex / Pi spawned via plugin- * agent-orchestrator) get their own AbortController but register a * parent-signal listener that aborts the child when the parent fires. * This is set up at spawn time by the orchestrator, NOT here. * * Crash safety: * * - Controllers live in memory. A process crash loses them — that's fine * because there's no in-flight turn anymore. * - The registry never holds stale controllers. `runWith` always unregisters * on exit (success, error, or abort). */ export { TurnAbortedError }; export declare class TurnControllerRegistry { private readonly currentTurnStorage; private active; private listeners; private closedReason; /** * Run `fn` inside a turn-scoped AbortController. The signal is passed to * `fn` and registered under `roomId` for the duration. When `fn` exits * (normally, throwing, or aborted), the controller is removed from the * registry. * * Concurrent turns for the SAME `roomId` are all tracked. Use * `RoomHandlerQueue` to enforce one-at-a-time per room where needed. */ runWith(roomId: string, fn: (signal: AbortSignal) => Promise): Promise; /** * Abort the active turns for `roomId`. When called from inside a turn * (async context under `runWith`), that calling turn is excluded — an * abort evaluator kills the work the user wants stopped, not the turn * that is processing the stop request. Out-of-band callers (HTTP stop * route, lifecycle handlers) have no current turn and abort everything. * Returns true if at least one turn was aborted. */ abortTurn(roomId: string, reason: string): boolean; /** * Abort every active turn. Used by lifecycle handlers (APP_PAUSE on * mobile, container shutdown) that need to release all in-flight * inference at once. Returns the room ids that were actually aborted — * already-aborted turns are skipped. */ abortAllTurns(reason: string): string[]; /** * Abort every active turn and every turn that starts later. Runtime stop * uses it: a turn already holding or queued for its room enters runWith * after the stop and must not run unaborted. */ closeTurns(reason: string): string[]; hasActiveTurn(roomId: string): boolean; /** Whether abortTurn could stop work in this room, excluding its caller. */ hasAbortableTurn(roomId: string): boolean; /** * Snapshot of the currently-active turn room ids. Useful for diagnostic * endpoints that want to surface "what's running" without holding a * reference to the registry's internal map. */ activeRoomIds(): string[]; /** * Returns the AbortSignal for the active turn on `roomId`, or null. Used * by long-running tools that want to check abort status mid-execution. * Inside a turn this is the caller's own signal; out-of-band it is the * newest turn's signal. */ signalFor(roomId: string): AbortSignal | null; /** * Subscribe to turn lifecycle events. Useful for telemetry and the * InterruptBench harness. */ onEvent(listener: (event: TurnEvent) => void): () => void; private emit; } export type TurnEvent = { type: "started"; roomId: string; startedAt: number; } | { type: "completed"; roomId: string; durationMs: number; } | { type: "errored"; roomId: string; error: string; durationMs: number; } | { type: "aborted"; roomId: string; reason: string; } | { type: "aborted-cleanup"; roomId: string; reason: string; durationMs: number; };