/** * agent-manager.ts — Tracks agents, background execution, resume support. * * Background agents are subject to a configurable concurrency limit (default: 4). * Excess agents are queued and auto-started as running agents complete. * Foreground agents bypass the queue (they block the parent anyway). */ import type { Model } from "@mariozechner/pi-ai"; import type { AgentSession, ExtensionAPI, ExtensionContext } from "@mariozechner/pi-coding-agent"; import { type ToolActivity } from "./agent-runner.js"; import type { AgentRecord, IsolationMode, SubagentType, ThinkingLevel } from "./types.js"; export type OnAgentComplete = (record: AgentRecord) => void; export type OnAgentStart = (record: AgentRecord) => void; export type OnAgentCompact = (record: AgentRecord, info: CompactionInfo) => void; export type CompactionInfo = { reason: "manual" | "threshold" | "overflow"; tokensBefore: number; }; interface SpawnOptions { description: string; model?: Model; maxTurns?: number; isolated?: boolean; inheritContext?: boolean; thinkingLevel?: ThinkingLevel; isBackground?: boolean; bypassQueue?: boolean; isolation?: IsolationMode; signal?: AbortSignal; onToolActivity?: (activity: ToolActivity) => void; onTextDelta?: (delta: string, fullText: string) => void; onSessionCreated?: (session: AgentSession) => void; onTurnEnd?: (turnCount: number) => void; onAssistantUsage?: (usage: { input: number; output: number; cacheWrite: number; }) => void; onCompaction?: (info: CompactionInfo) => void; forkMessages?: any[]; parentSystemPrompt?: string; } export declare class AgentManager { private agents; private cleanupInterval; private onComplete?; private onStart?; private onCompact?; private maxConcurrent; /** Queue of background agents waiting to start. */ private queue; /** Number of currently running background agents. */ private runningBackground; constructor(onComplete?: OnAgentComplete, maxConcurrent?: number, onStart?: OnAgentStart, onCompact?: OnAgentCompact); /** Update the max concurrent background agents limit. */ setMaxConcurrent(n: number): void; getMaxConcurrent(): number; /** * Spawn an agent and return its ID immediately (for background use). * If the concurrency limit is reached, the agent is queued. */ spawn(pi: ExtensionAPI, ctx: ExtensionContext, type: SubagentType, prompt: string, options: SpawnOptions): string; /** Actually start an agent (called immediately or from queue drain). */ private startAgent; /** Start queued agents up to the concurrency limit. */ private drainQueue; /** * Spawn an agent and wait for completion (foreground use). * Foreground agents bypass the concurrency queue. */ spawnAndWait(pi: ExtensionAPI, ctx: ExtensionContext, type: SubagentType, prompt: string, options: Omit): Promise; /** * Resume an existing agent session with a new prompt. */ resume(id: string, prompt: string, signal?: AbortSignal): Promise; getRecord(id: string): AgentRecord | undefined; listAgents(): AgentRecord[]; abort(id: string): boolean; /** Dispose a record's session and remove it from the map. */ private removeRecord; private cleanup; /** * Remove all completed/stopped/errored records immediately. * Called on session start/switch so tasks from a prior session don't persist. */ clearCompleted(): void; /** Whether any agents are still running or queued. */ hasRunning(): boolean; /** Abort all running and queued agents immediately. */ abortAll(): number; /** Wait for all running and queued agents to complete (including queued ones). */ waitForAll(): Promise; dispose(): void; } export {};