/** * 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 { /** Unique identifier for the task */ id: string; /** Human-readable label for the task */ label?: string; /** The async function to execute */ execute: () => Promise; /** 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 { 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; } 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 { batchId: string; results: TaskResult[]; 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(tasks: ParallelTask[]): Promise>; /** * Execute tasks in groups, where tasks within a group run in parallel */ executeByGroup(tasks: ParallelTask[]): Promise>>; /** * Simple parallel map with automatic concurrency */ map(items: T[], fn: (item: T, index: number) => Promise, concurrency?: number): Promise; /** * Plan execution without running (useful for visualization) */ plan(tasks: ParallelTask[]): 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(tasks: ParallelTask[], config?: ParallelExecutorConfig): Promise>; /** * Parallel map with concurrency limit */ export declare function mapParallel(items: T[], fn: (item: T, index: number) => Promise, concurrency?: number): Promise; /** * Create a task definition helper */ export declare function createTask(id: string, execute: () => Promise, options?: Partial, 'id' | 'execute'>>): ParallelTask; export interface WorkerConfig { id: string; maxConcurrency?: number; } export interface WorkItem { id: string; execute: () => Promise; 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(work: WorkItem): Promise; /** * Submit multiple work items */ submitAll(items: WorkItem[]): Promise; private selectWorker; /** * Get pool statistics */ getStats(): Map; /** * 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