/** * TaskQueue — Distributed task queue with dependency resolution and lock-based claiming. * * Tasks are enqueued by the orchestrator and claimed by external agent workers. * Supports role-based matching, dependency ordering, and timeout reclamation. */ import { EventEmitter } from "events"; import type { AgentRole } from "../plugin/shared/contracts.js"; export type TaskStatus = "pending" | "claimed" | "running" | "completed" | "failed" | "timeout"; export interface QueueTask { id: string; role: AgentRole; name: string; intent: string; payload: unknown; dependencies: string[]; status: TaskStatus; claimedBy: string | null; claimedAt: number | null; completedAt: number | null; result: unknown; error: string | null; createdAt: number; timeoutMs: number; } export interface TaskQueueStats { pending: number; claimed: number; running: number; completed: number; failed: number; total: number; } export declare class TaskQueue extends EventEmitter { private tasks; private taskCounter; private reclaimTimer; private persistPath; private persistTimer; constructor(persistPath?: string); /** Start the reclaim timer for timed-out tasks. Loads persisted tasks if persistPath is set. */ start(): Promise; /** Stop the reclaim timer. */ stop(): void; /** Enqueue a new task. Returns the task ID. */ enqueue(task: Omit): string; /** * Claim the next available task for a role. * Returns the task if one is available, or null. */ claim(agentId: string, role: AgentRole): QueueTask | null; /** Mark a task as running (agent started executing). */ markRunning(taskId: string, agentId: string): boolean; /** Complete a task with a result. */ complete(taskId: string, agentId: string, result: unknown): boolean; /** Fail a task with an error. */ fail(taskId: string, agentId: string, error: string): boolean; /** Get a task by ID. */ get(taskId: string): QueueTask | null; /** Get all tasks. */ getAll(): QueueTask[]; /** Get pending tasks for a role. */ getPendingForRole(role: AgentRole): QueueTask[]; /** Get queue stats. */ getStats(): TaskQueueStats; /** * Wait for a specific task to complete or fail. * Resolves with the task when done, rejects on timeout. */ waitForTask(taskId: string, timeoutMs?: number): Promise; /** Clear completed and failed tasks older than the given age. */ prune(maxAgeMs?: number): number; /** Write pending + claimed tasks to disk (debounced). */ private deferPersist; /** Persist active tasks (pending, claimed, running) to disk. */ persist(): Promise; /** Load persisted tasks from disk. Pending tasks are re-enqueued; claimed/running are marked as timeout. */ loadPersisted(): Promise; /** Reclaim tasks that were claimed but timed out. */ private reclaimTimedOut; }