/**
 * Session Lanes for Tool Execution
 *
 * OpenClaw-inspired serialized execution system to prevent race conditions:
 * - Per-session lane serialization
 * - Global lane for cross-session operations
 * - Timeout handling with configurable limits
 * - Early termination via AbortSignal
 * - Queue management and metrics
 */
import { EventEmitter } from 'events';
/**
 * Lane execution task
 */
export interface LaneTask<T = unknown> {
    /** Task ID */
    id: string;
    /** Session ID (or 'global') */
    sessionId: string;
    /** Task name/description */
    name: string;
    /** Task function */
    execute: () => Promise<T>;
    /** Priority (higher = runs first in queue) */
    priority: number;
    /** Timeout in ms */
    timeoutMs: number;
    /** Abort signal */
    abortSignal?: AbortSignal;
    /** Created timestamp */
    createdAt: number;
    /** Started timestamp */
    startedAt?: number;
    /** Completed timestamp */
    completedAt?: number;
    /** Resolve function for promise */
    resolve: (value: T) => void;
    /** Reject function for promise */
    reject: (error: Error) => void;
}
/**
 * Lane status
 */
export type LaneStatus = 'idle' | 'executing' | 'draining';
/**
 * Lane info
 */
export interface LaneInfo {
    /** Session ID */
    sessionId: string;
    /** Current status */
    status: LaneStatus;
    /** Queue size */
    queueSize: number;
    /** Current task ID (if executing) */
    currentTaskId?: string;
    /** Total tasks executed */
    totalExecuted: number;
    /** Total errors */
    totalErrors: number;
    /** Created timestamp */
    createdAt: number;
    /** Last activity timestamp */
    lastActivityAt: number;
}
/**
 * Session lanes configuration
 */
export interface SessionLanesConfig {
    /** Default task timeout in ms */
    defaultTimeoutMs: number;
    /** Default wait timeout in ms */
    defaultWaitTimeoutMs: number;
    /** Maximum queue size per lane */
    maxQueueSize: number;
    /** Lane idle timeout in ms (auto-cleanup) */
    laneIdleTimeoutMs: number;
    /** Enable metrics collection */
    enableMetrics: boolean;
    /** Global lane concurrency (usually 1) */
    globalLaneConcurrency: number;
}
/**
 * Default configuration
 */
export declare const DEFAULT_SESSION_LANES_CONFIG: SessionLanesConfig;
/**
 * Lane execution result
 */
export interface LaneExecutionResult<T> {
    /** Task ID */
    taskId: string;
    /** Success status */
    success: boolean;
    /** Result value */
    result?: T;
    /** Error if failed */
    error?: Error;
    /** Execution time in ms */
    executionTimeMs: number;
    /** Was task cancelled */
    cancelled: boolean;
    /** Was task timed out */
    timedOut: boolean;
}
/**
 * Session lanes events
 */
export interface SessionLanesEvents {
    'lane:created': (sessionId: string) => void;
    'lane:destroyed': (sessionId: string) => void;
    'task:queued': (sessionId: string, taskId: string) => void;
    'task:started': (sessionId: string, taskId: string) => void;
    'task:completed': (sessionId: string, taskId: string, durationMs: number) => void;
    'task:failed': (sessionId: string, taskId: string, error: Error) => void;
    'task:timeout': (sessionId: string, taskId: string) => void;
    'task:cancelled': (sessionId: string, taskId: string) => void;
}
/**
 * Manages all session lanes
 */
export declare class SessionLanesManager extends EventEmitter {
    private config;
    private lanes;
    private globalLane;
    private cleanupInterval;
    constructor(config?: Partial<SessionLanesConfig>);
    /**
     * Execute a task in a session lane
     */
    execute<T>(sessionId: string, name: string, fn: () => Promise<T>, options?: {
        priority?: number;
        timeoutMs?: number;
        abortSignal?: AbortSignal;
        useGlobalLane?: boolean;
    }): Promise<LaneExecutionResult<T>>;
    /**
     * Execute in global lane (cross-session)
     */
    executeGlobal<T>(name: string, fn: () => Promise<T>, options?: {
        priority?: number;
        timeoutMs?: number;
        abortSignal?: AbortSignal;
    }): Promise<LaneExecutionResult<T>>;
    /**
     * Wait for a condition with timeout (like agent.wait in OpenClaw)
     */
    wait<T>(sessionId: string, condition: () => Promise<T | null>, options?: {
        timeoutMs?: number;
        pollIntervalMs?: number;
        abortSignal?: AbortSignal;
    }): Promise<T>;
    /**
     * Get or create a session lane
     */
    private getOrCreateLane;
    /**
     * Setup event forwarding from lane
     */
    private setupLaneEvents;
    /**
     * Destroy a session lane
     */
    destroyLane(sessionId: string): Promise<boolean>;
    /**
     * Cancel all tasks in a session
     */
    cancelSession(sessionId: string): number;
    /**
     * Get lane info
     */
    getLaneInfo(sessionId: string): LaneInfo | null;
    /**
     * Get all lanes info
     */
    getAllLanesInfo(): LaneInfo[];
    /**
     * Start cleanup interval
     */
    private startCleanupInterval;
    /**
     * Stop cleanup interval
     */
    private stopCleanupInterval;
    /**
     * Cleanup idle lanes
     */
    private cleanupIdleLanes;
    /**
     * Get statistics
     */
    getStats(): {
        totalLanes: number;
        activeLanes: number;
        idleLanes: number;
        totalQueued: number;
        globalLaneInfo: LaneInfo;
    };
    /**
     * Shutdown the manager
     */
    shutdown(): Promise<void>;
}
/**
 * Get session lanes manager instance
 */
export declare function getSessionLanesManager(config?: Partial<SessionLanesConfig>): SessionLanesManager;
/**
 * Reset session lanes manager
 */
export declare function resetSessionLanesManager(): Promise<void>;
export default SessionLanesManager;
