/** * Serializes handler work per room while allowing unrelated rooms to proceed. * Every item runs independently in arrival order; intent coalescing remains a * planner concern. Waiting transports may cancel before acquisition, while an * acquired lease remains owned until its complete durable outcome settles. */ import { ElizaError } from "../errors.js"; export declare class RoomHandlerQueueAbortedError extends Error { readonly roomId: string; constructor(roomId: string, options?: ErrorOptions); } export declare const DEFAULT_ROOM_HANDLER_QUEUE_MAX_PENDING_PER_ROOM = 32; export declare const DEFAULT_ROOM_HANDLER_QUEUE_MAX_PENDING_TOTAL = 512; export declare const DEFAULT_ROOM_HANDLER_QUEUE_MAX_ACTIVE_ROOMS = 256; export declare class RoomHandlerQueueClosedError extends ElizaError { constructor(reason: string); } export declare class RoomHandlerQueueGlobalSaturatedError extends ElizaError { readonly limitKind: "pending" | "rooms"; readonly limit: number; readonly current: number; constructor(limitKind: "pending" | "rooms", limit: number, current: number); } export declare class RoomHandlerQueueSaturatedError extends ElizaError { readonly name = "RoomHandlerQueueSaturatedError"; readonly roomId: string; readonly maxPendingPerRoom: number; readonly pendingCount: number; constructor(roomId: string, maxPendingPerRoom: number, pendingCount: number); } export interface RoomHandlerQueueOptions { /** Maximum admitted work per room, including the active owner. */ maxPendingPerRoom?: number; /** Maximum admitted work across every room, including active owners. */ maxPendingTotal?: number; /** Maximum distinct non-empty room queues retained at one time. */ maxActiveRooms?: number; /** Diagnostic boundary for telemetry-listener failures. */ onListenerError?: (error: unknown, event: RoomQueueEvent) => void; /** * Require explicit lease capabilities instead of inheriting async-local ownership. */ asyncContext?: "auto" | "explicit"; } export interface RoomHandlerLease { /** Release the room after the caller's complete durable outcome is settled. */ release(): Promise; } interface RoomHandlerOwnershipContext { lease: RoomHandlerLease; roomId: string; } interface RunWithOptions { /** Cancellation applies only while the work is queued, never after it starts. */ signal?: AbortSignal; } export interface WithRoomHandlerLeaseOptions { /** Cancellation applies only while ownership is queued. */ signal?: AbortSignal; /** Exact live capability to reuse when async-local context is unavailable. */ lease?: RoomHandlerLease; } export declare class RoomHandlerQueue { private rooms; private listeners; private activeLeases; readonly maxPendingPerRoom: number; readonly maxPendingTotal: number; readonly maxActiveRooms: number; private admissionClosedReason; private readonly onListenerError?; private readonly ownershipStorage; constructor(options?: RoomHandlerQueueOptions); /** * Run `fn` serialized against any other call for the same `roomId`. If a * prior handler for `roomId` is still running, `fn` waits in line until * the prior handler resolves (or rejects — failures don't block the queue). */ runWith(roomId: string, fn: () => Promise, options?: RunWithOptions): Promise; /** * Acquire exclusive ownership of a room until the returned lease releases. * A waiting transport may cancel without disturbing active work; after the * lease is granted, its owner alone decides when the durable turn is done. */ acquire(roomId: string, signal?: AbortSignal): Promise; /** * Own a room for the callback's complete persistence + processing boundary. * The lease is opaque and remains valid only until this callback settles. */ withLease(roomId: string, fn: (lease: RoomHandlerLease) => Promise, options?: WithRoomHandlerLeaseOptions): Promise; /** * Own multiple rooms in stable lexical order, running `fn` as the owner of * all of them. A nested call may reuse only rooms its turn already holds; * widening a nested transaction fails fast instead of risking an A→B/B→A * deadlock. */ withLeases(roomIds: Iterable, fn: (leases: ReadonlyMap) => Promise, options?: WithRoomHandlerLeaseOptions): Promise; /** * Serialize message-table writes performed by sibling async work sharing one * outer turn. The outer lease is still the public admission boundary; this * inner FIFO only makes its concurrent durable writes deterministic. */ withLeaseWrite(roomId: string, lease: RoomHandlerLease, fn: () => Promise): Promise; /** Serialize one atomic adapter write across every owned room. */ withLeaseWrites(leases: ReadonlyMap, fn: () => Promise): Promise; /** * Propagate a validated live capability through the work it owns. This is * the only ambient ownership signal used by nested runtime writers; callers * cannot manufacture or reuse it after release. */ runInLease(roomId: string, lease: RoomHandlerLease, fn: () => T): T; /** Return the exact live capability inherited by the current async work. */ currentLease(roomId: string): RoomHandlerLease | undefined; /** * Snapshot the exact live single-room ownership inherited by the current * async work; an owner of several rooms has no single ownership. */ currentOwnership(): RoomHandlerOwnershipContext | undefined; /** The inherited leases that are still live, by room. */ private liveAmbientLeases; /** Whether this exact live lease owns this queue and room. */ ownsLease(roomId: string, lease: RoomHandlerLease | null | undefined): lease is RoomHandlerLease; pendingFor(roomId: string): number; pendingTotal(): number; /** Freeze new admissions while allowing already-admitted work to settle. */ closeAdmissions(reason: string): void; isAcceptingAdmissions(): boolean; /** Whether callers must propagate the opaque lease without ambient context. */ requiresExplicitOwnership(): boolean; /** Wait for all queued + active work for a room to finish. */ quiesce(roomId: string): Promise; /** Wait for all queued + active work for every room to finish. */ quiesceAll(): Promise; onEvent(listener: (event: RoomQueueEvent) => void): () => void; private createQueue; private assertLeaseForRoom; private emit; } export type RoomQueueEvent = { type: "enqueued"; roomId: string; queueDepth: number; } | { type: "completed"; roomId: string; } | { type: "cancelled"; roomId: string; } | { type: "saturated"; roomId: string; queueDepth: number; maxPendingPerRoom: number; } | { type: "errored"; roomId: string; error: string; }; export {};