/**
 * Lane Queue System
 *
 * OpenClaw-inspired concurrency control: "Default Serial, Explicit Parallel"
 *
 * Each session has its own "lane" for task execution:
 * - Tasks execute serially by default to prevent state corruption
 * - Only explicitly marked safe/idempotent tasks run in parallel
 * - Prevents race conditions in multi-channel/multi-session scenarios
 *
 * Usage:
 * ```typescript
 * const queue = getLaneQueue();
 *
 * // Serial execution (default)
 * await queue.enqueue('session-1', async () => {
 *   await doSomething();
 * });
 *
 * // Parallel execution (explicit)
 * await queue.enqueue('session-1', async () => {
 *   await readOnlyOperation();
 * }, { parallel: true });
 * ```
 */
import { EventEmitter } from 'events';
export interface Task<T = unknown> {
    /** Unique task ID */
    id: string;
    /** Lane/session this task belongs to */
    laneId: string;
    /** Task function to execute */
    fn: () => Promise<T>;
    /** Task options */
    options: TaskOptions;
    /** Task status */
    status: TaskStatus;
    /** When task was enqueued */
    enqueuedAt: Date;
    /** When task started */
    startedAt?: Date;
    /** When task completed */
    completedAt?: Date;
    /** Task result (if completed) */
    result?: T;
    /** Task error (if failed) */
    error?: Error;
    /** Promise resolver */
    resolve: (value: T) => void;
    /** Promise rejector */
    reject: (error: Error) => void;
}
export type TaskStatus = 'pending' | 'running' | 'completed' | 'failed' | 'cancelled';
export interface TaskOptions {
    /** Allow parallel execution with other parallel tasks */
    parallel?: boolean;
    /** Task priority (higher = executed first) */
    priority?: number;
    /** Task timeout in ms */
    timeout?: number;
    /** Task category for metrics */
    category?: string;
    /** Whether task is idempotent (safe to retry) */
    idempotent?: boolean;
    /** Retry count on failure */
    retries?: number;
    /** Delay between retries in ms */
    retryDelay?: number;
}
export interface Lane {
    /** Lane ID (usually session ID) */
    id: string;
    /** Pending tasks */
    pending: Task[];
    /** Currently running tasks */
    running: Task[];
    /** Is lane paused */
    paused: boolean;
    /** Is lane processing */
    processing: boolean;
    /** Lane statistics */
    stats: LaneStats;
}
export interface LaneStats {
    totalTasks: number;
    completedTasks: number;
    failedTasks: number;
    totalDuration: number;
    averageDuration: number;
}
export interface LaneQueueConfig {
    /** Maximum concurrent parallel tasks per lane */
    maxParallel: number;
    /** Default task timeout in ms */
    defaultTimeout: number;
    /** Enable task metrics */
    metricsEnabled: boolean;
    /** Maximum pending tasks per lane */
    maxPending: number;
    /** Default retry count */
    defaultRetries: number;
    /** Default retry delay in ms */
    defaultRetryDelay: number;
}
export declare const DEFAULT_LANE_QUEUE_CONFIG: LaneQueueConfig;
export interface LaneQueueEvents {
    /** Task enqueued */
    'task:enqueued': (task: Task) => void;
    /** Task started */
    'task:started': (task: Task) => void;
    /** Task completed */
    'task:completed': (task: Task) => void;
    /** Task failed */
    'task:failed': (task: Task, error: Error) => void;
    /** Task cancelled */
    'task:cancelled': (task: Task) => void;
    /** Lane created */
    'lane:created': (laneId: string) => void;
    /** Lane paused */
    'lane:paused': (laneId: string) => void;
    /** Lane resumed */
    'lane:resumed': (laneId: string) => void;
    /** Lane drained */
    'lane:drained': (laneId: string) => void;
}
export declare class LaneQueue extends EventEmitter {
    private config;
    private lanes;
    private taskCounter;
    constructor(config?: Partial<LaneQueueConfig>);
    /**
     * Enqueue a task for execution
     */
    enqueue<T>(laneId: string, fn: () => Promise<T>, options?: TaskOptions): Promise<T>;
    /**
     * Insert task into array sorted by priority (descending)
     */
    private insertByPriority;
    /**
     * Process tasks in a lane
     */
    private processLane;
    /**
     * Get next tasks to execute
     */
    private getNextTasks;
    /**
     * Execute a single task
     */
    private executeTask;
    /**
     * Execute function with timeout
     */
    private executeWithTimeout;
    /**
     * Delay helper
     */
    private delay;
    /**
     * Get or create a lane
     */
    private getOrCreateLane;
    /**
     * Pause a lane
     */
    pause(laneId: string): boolean;
    /**
     * Resume a lane
     */
    resume(laneId: string): boolean;
    /**
     * Cancel all pending tasks in a lane
     */
    cancelPending(laneId: string): number;
    /**
     * Get lane info
     */
    getLane(laneId: string): Lane | undefined;
    /**
     * List all lanes
     */
    listLanes(): Lane[];
    /**
     * Get lane statistics
     */
    getStats(laneId: string): LaneStats | undefined;
    /**
     * Get global statistics
     */
    getGlobalStats(): LaneStats;
    /**
     * Remove a lane
     */
    removeLane(laneId: string): boolean;
    /**
     * Clear all lanes
     */
    clear(): void;
    /**
     * Format queue status
     */
    formatStatus(): string;
}
export declare function getLaneQueue(config?: Partial<LaneQueueConfig>): LaneQueue;
export declare function resetLaneQueue(): void;
export default LaneQueue;
