/** One turn's cancellation handles. `abort` is absent while the turn is only reserved (queued). */ export interface ActiveTurnEntry { /** * Primary stop mechanism — aborting the SDK query kills its child process. * Undefined while the turn is still queued (reserved but `query()` not yet * called); set by `adopt()` once the turn begins executing. */ abort?: AbortController; /** * Best-effort secondary stop, and a forward-looking hook. The SDK's * control-channel `interrupt()` is only available in streaming-input mode; * BrainBot sends a plain string prompt for text turns and a single-yield async * generator for multimodal, so `interrupt()` almost always rejects today and * is `.catch()`-swallowed — `abort` is the always-available primary. Kept so a * future streaming-input mode gets graceful interrupt for free. */ interrupt?: () => Promise; /** True once a `#stop` has fired for this turn (works in the reserved phase too). */ stoppedByUser: boolean; /** When the entry was first created (for diagnostics). */ startedAt: number; /** * Unique identity of the caller that currently owns this entry. Returned by * `reserve()`; carried through `adopt()`. Cleanup only removes an entry whose * token it still owns, so a completing turn can never delete a DIFFERENT * same-key turn's fresh reservation (the two-concurrent-invocations race). */ token: number; } export declare class ActiveTurns { private _turns; /** * Reserve a placeholder for a turn that is about to enter the concurrency * queue, so `#stop` can reach it before it starts executing. Returns a unique * ownership token on success, or null if an entry already exists for this key * (we must never overwrite a live turn's AbortController with a bare * placeholder). Pass the token to `adopt()` and `releaseReserved()`. * * KNOWN gap (accepted): only ONE entry exists per key at a time. If turn A is * live and message B queues behind it, B cannot reserve (returns null), so a * `#stop` in the narrow window after A's entry is cleared but before B adopts * can still return a false "nothing running". Covering that needs multi-entry * or queue-aware tracking; out of scope for this Esc-equivalent. */ reserve(key: string): number | null; /** * Attach the real cancellation handles once the turn starts executing. If a * placeholder exists (from `reserve`), its stop flag and token are preserved; * otherwise a fresh entry is created (the scheduler path, which never * reserves). Returns true iff a `#stop` already arrived while the turn was * queued — the caller should then abort immediately. */ adopt(key: string, handles: Pick & Partial): boolean; get(key: string): ActiveTurnEntry | undefined; /** True iff the entry for `key` is a bare reservation (no AbortController adopted yet). */ isReserved(key: string): boolean; /** * Remove a still-bare reservation IFF it is the one identified by `token` AND * it was never adopted (no abort). No-op if the entry was adopted (a live turn * owns its own cleanup) or if a DIFFERENT caller's token now holds the key (a * concurrent same-key turn reserved after ours was already gone). This is the * ownership guard that prevents a completing turn from deleting the next * turn's fresh reservation. */ releaseReserved(key: string, token: number): void; unregister(key: string): void; /** * Mark the turn stopped-by-user and, if it is already executing, fire the * AbortController (primary) plus a best-effort control-channel interrupt * (secondary). If the turn is only RESERVED (queued), just set the flag — * `adopt()` will honour it the moment the turn starts. Returns true iff an * entry (reserved or running) existed for `key`. */ stop(key: string): boolean; /** True iff a `#stop` has been requested for the turn under `key` (reserved or running). */ isStopRequested(key: string): boolean; } /** Module-level singleton */ export declare const activeTurns: ActiveTurns; export declare function reserveTurn(key: string): number | null; export declare function adoptTurn(key: string, handles: Pick & Partial): boolean; export declare function releaseReservedTurn(key: string, token: number): void; export declare function unregisterTurn(key: string): void; export declare function stopTurn(key: string): boolean; export declare function isStopRequested(key: string): boolean;