/**
 * Parallel Task Executor Module
 *
 * Provides automatic parallel execution capabilities with smart detection,
 * task dependency management, and resource-aware concurrency control.
 *
 * Principal Investigator: Bo Shang
 * Framework: agi-cli
 */
export type TaskStatus = 'pending' | 'running' | 'completed' | 'failed' | 'cancelled';
export interface ParallelTask<T = unknown> {
    /** Unique identifier for the task */
    id: string;
    /** Human-readable label for the task */
    label?: string;
    /** The async function to execute */
    execute: () => Promise<T>;
    /** Task IDs that must complete before this task can run */
    dependencies?: string[];
    /** Priority (higher = runs first when resources available) */
    priority?: number;
    /** Whether this task can run in parallel with others */
    parallelizable?: boolean;
    /** Optional group for batch processing */
    group?: string;
    /** Timeout in milliseconds */
    timeout?: number;
    /** Retry configuration */
    retry?: {
        maxAttempts?: number;
        backoffMs?: number;
        shouldRetry?: (error: unknown) => boolean;
    };
}
export interface TaskResult<T = unknown> {
    taskId: string;
    status: TaskStatus;
    result?: T;
    error?: Error;
    durationMs: number;
    attempts: number;
    startedAt: number;
    completedAt: number;
}
export interface ParallelExecutorConfig {
    /** Maximum concurrent tasks (default: 5) */
    maxConcurrency?: number;
    /** Default timeout per task in ms (default: 30000) */
    defaultTimeout?: number;
    /** Whether to auto-detect parallelizable tasks (default: true) */
    autoDetectParallel?: boolean;
    /** Whether to continue on task failure (default: true) */
    continueOnFailure?: boolean;
    /** Rate limiting config (optional) */
    rateLimit?: {
        maxRequests: number;
        windowMs: number;
    };
    /** Callback for task events */
    onTaskEvent?: (event: TaskEvent) => void;
    /** Enable adaptive concurrency based on system resources (default: true) */
    adaptiveConcurrency?: boolean;
    /** Enable deadlock detection for competing agents (default: true) */
    deadlockDetection?: boolean;
    /** Maximum memory usage percentage before throttling (0-100, default: 80) */
    maxMemoryUsagePercent?: number;
    /** Minimum concurrency level (default: 1) */
    minConcurrency?: number;
    /** Performance telemetry collection (default: true) */
    collectTelemetry?: boolean;
}
export interface TaskEvent {
    type: 'task.start' | 'task.complete' | 'task.error' | 'task.retry' | 'batch.start' | 'batch.complete';
    taskId?: string;
    batchId?: string;
    timestamp: number;
    data?: Record<string, unknown>;
}
export interface ExecutionPlan {
    /** Tasks grouped by execution phase (tasks in same phase can run in parallel) */
    phases: ParallelTask[][];
    /** Tasks that must run sequentially */
    sequential: ParallelTask[];
    /** Total estimated parallelism factor */
    parallelismFactor: number;
}
export interface BatchResult<T = unknown> {
    batchId: string;
    results: TaskResult<T>[];
    totalDurationMs: number;
    successCount: number;
    failureCount: number;
    parallelismAchieved: number;
}
export declare class ParallelExecutor {
    private readonly config;
    private readonly pool;
    private readonly rateLimiter;
    private readonly results;
    private cancelled;
    constructor(config?: ParallelExecutorConfig);
    /**
     * Cancel all pending tasks
     */
    cancel(): void;
    /**
     * Reset the executor state
     */
    reset(): void;
    /**
     * Execute a single task with retry and timeout handling
     */
    private executeTask;
    private createResult;
    /**
     * Execute tasks with automatic parallelization based on dependencies
     */
    execute<T>(tasks: ParallelTask<T>[]): Promise<BatchResult<T>>;
    /**
     * Execute tasks in groups, where tasks within a group run in parallel
     */
    executeByGroup<T>(tasks: ParallelTask<T>[]): Promise<Map<string, BatchResult<T>>>;
    /**
     * Simple parallel map with automatic concurrency
     */
    map<T, R>(items: T[], fn: (item: T, index: number) => Promise<R>, concurrency?: number): Promise<R[]>;
    /**
     * Plan execution without running (useful for visualization)
     */
    plan<T>(tasks: ParallelTask<T>[]): ExecutionPlan;
    /**
     * Auto-detect if a task is parallelizable based on heuristics
     */
    private detectParallelizable;
    private emit;
    private sleep;
    /**
     * Get execution statistics
     */
    getStats(): {
        poolActive: number;
        poolPending: number;
        resultsCount: number;
    };
}
/**
 * Create a parallel executor with default configuration
 */
export declare function createParallelExecutor(config?: ParallelExecutorConfig): ParallelExecutor;
/**
 * Execute tasks in parallel with automatic dependency resolution
 */
export declare function executeParallel<T>(tasks: ParallelTask<T>[], config?: ParallelExecutorConfig): Promise<BatchResult<T>>;
/**
 * Parallel map with concurrency limit
 */
export declare function mapParallel<T, R>(items: T[], fn: (item: T, index: number) => Promise<R>, concurrency?: number): Promise<R[]>;
/**
 * Create a task definition helper
 */
export declare function createTask<T>(id: string, execute: () => Promise<T>, options?: Partial<Omit<ParallelTask<T>, 'id' | 'execute'>>): ParallelTask<T>;
export interface WorkerConfig {
    id: string;
    maxConcurrency?: number;
}
export interface WorkItem<T = unknown> {
    id: string;
    execute: () => Promise<T>;
    priority?: number;
}
export interface WorkerPoolConfig {
    workers: WorkerConfig[];
    balanceStrategy?: 'round-robin' | 'least-busy' | 'random';
}
export declare class WorkerPool {
    private readonly workers;
    private readonly workerIds;
    private readonly balanceStrategy;
    private roundRobinIndex;
    constructor(config: WorkerPoolConfig);
    /**
     * Submit work to the pool
     */
    submit<T>(work: WorkItem<T>): Promise<T>;
    /**
     * Submit multiple work items
     */
    submitAll<T>(items: WorkItem<T>[]): Promise<T[]>;
    private selectWorker;
    /**
     * Get pool statistics
     */
    getStats(): Map<string, {
        active: number;
        pending: number;
    }>;
    /**
     * Get total active work items across all workers
     */
    get totalActive(): number;
    /**
     * Get total pending work items across all workers
     */
    get totalPending(): number;
}
declare const _default: {
    ParallelExecutor: typeof ParallelExecutor;
    WorkerPool: typeof WorkerPool;
    createParallelExecutor: typeof createParallelExecutor;
    executeParallel: typeof executeParallel;
    mapParallel: typeof mapParallel;
    createTask: typeof createTask;
};
export default _default;
//# sourceMappingURL=parallelExecutor.d.ts.map