/**
 * Agent Process Manager
 *
 * Core lifecycle manager for agent processes on the bridge machine.
 * Provides comprehensive process management including creation, discovery,
 * monitoring, termination, and orphan reclamation capabilities.
 */
import { type ProcessRecord, type ProcessState, type HealthStatus } from './process-registry.js';
import { type ProcessInfo, type ProcessHealthStatus } from './process-detector.js';
/**
 * Process configuration for starting a new agent process
 */
export interface ProcessConfig {
    agentId: string;
    sessionId: string;
    pid: number;
    mode: 'sdk';
    workspacePath: string;
    command: string;
}
/**
 * Discovered process with management status
 */
export interface DiscoveredProcess extends ProcessInfo {
    managementStatus: 'managed' | 'orphan' | 'unknown';
    sessionId?: string;
}
/**
 * Termination result with escalation level details
 */
export interface TerminationResult {
    success: boolean;
    agentId: string;
    pid: number;
    level: number;
    terminated: number;
    failed: number;
    message: string;
}
/**
 * Reclamation result
 */
export interface ReclamationResult {
    success: boolean;
    pid: number;
    action: 'adopted' | 'terminated' | 'failed';
}
/**
 * Agent Process Manager
 *
 * Manages the complete lifecycle of agent processes including:
 * - Creation: Register and verify new processes
 * - Discovery: Scan for managed, orphan, and unknown processes
 * - Monitoring: Health checks and heartbeat tracking
 * - Termination: Graceful stop with escalation strategy
 * - Orphan Reclamation: Adopt or terminate orphan processes
 */
export declare class AgentProcessManager {
    private static instance;
    private readonly registry;
    /**
     * Private constructor - use getAgentProcessManager() for singleton access
     */
    private constructor();
    /**
     * Get the singleton instance
     */
    static getInstance(): AgentProcessManager;
    /**
     * Start a new process and register it in the process registry
     *
     * Flow:
     * 1. Register process with state='creating'
     * 2. Verify it's running
     * 3. Update state to 'running'
     *
     * @param config - Process configuration
     * @throws Error if process registration fails or process is not running
     */
    startProcess(config: ProcessConfig): Promise<void>;
    /**
     * Discover all agent processes on the system
     *
     * Scans OS for all agent processes, compares with registry, and returns
     * a list of discovered processes with their management status.
     *
     * @returns Array of discovered processes with management status
     */
    discoverAllProcesses(): Promise<DiscoveredProcess[]>;
    /**
     * Check health of all registered processes
     *
     * Updates heartbeat and health status in registry for each managed process.
     *
     * @returns Map of agentId to health status
     */
    healthCheckAll(): Promise<Map<string, ProcessHealthStatus>>;
    /**
     * Check health of a single process
     *
     * @param agentId - Agent ID to check
     * @returns Health status of the process
     */
    healthCheck(agentId: string): Promise<ProcessHealthStatus>;
    /**
     * Stop a process with graceful escalation strategy
     *
     * Escalation levels:
     * Level 1: SIGTERM → wait 3s
     * Level 2: taskkill /T /F (Win) or kill -9 (Unix) → wait 3s
     * Level 3: terminateProcessTree → wait 5s
     * Level 4: mark as "unstoppable", return failure
     *
     * @param agentId - Agent ID to stop
     * @returns Termination result with details
     */
    stopProcess(agentId: string): Promise<TerminationResult>;
    /**
     * Send SIGTERM to a process
     */
    private sendSigterm;
    /**
     * Send force kill (SIGKILL on Unix, taskkill /F on Windows)
     */
    private sendForceKill;
    /**
     * Force kill a process immediately using terminateProcessTree
     *
     * This bypasses the graceful escalation and uses the most aggressive
     * termination method immediately.
     *
     * @param agentId - Agent ID to force kill
     * @returns Termination result
     */
    forceKill(agentId: string): Promise<TerminationResult>;
    /**
     * Reclaim an orphan process by agentId
     *
     * Finds an orphan process by agentId, and either:
     * - Adopts it into the registry (if found and not managed)
     * - Terminates it (if adoption fails or process is problematic)
     *
     * @param agentId - Agent ID to find and reclaim
     * @returns Reclamation result
     */
    reclaimOrphan(agentId: string): Promise<ReclamationResult>;
    /**
     * Scan all orphans and attempt to reclaim each
     *
     * @returns Array of reclamation results
     */
    scanAndReclaimAll(): Promise<Array<{
        agentId: string;
        pid: number;
        action: string;
    }>>;
    /**
     * Mark a process as orphaned in the registry
     *
     * @param agentId - Agent ID to mark
     * @returns Whether the operation succeeded
     */
    markOrphaned(agentId: string): Promise<boolean>;
    /**
     * Check if a process is managed
     *
     * @param agentId - Agent ID to check
     * @returns Whether the process is managed
     */
    isManaged(agentId: string): boolean;
    /**
     * Get all managed processes
     *
     * @returns Array of all managed process records
     */
    getAllManagedProcesses(): ProcessRecord[];
    /**
     * Get managed processes by state
     *
     * @param state - Process state to filter by
     * @returns Array of process records matching the state
     */
    getManagedProcessesByState(state: ProcessState): ProcessRecord[];
    /**
     * Find processes running on the machine that are NOT in registry
     *
     * @returns Array of unmanaged process info
     */
    getUnmanagedProcesses(): Promise<ProcessInfo[]>;
    /**
     * Rebuild the process registry from persisted sessions (e.g. on startup)
     *
     * Scans all persisted sessions, checks which processes are still running,
     * and registers them with appropriate state.
     *
     * @param sessions - Array of persisted session data
     */
    rebuildRegistry(sessions: Array<{
        agentId: string;
        sessionId: string;
        pid?: number;
        workspacePath?: string;
        command?: string;
        mode?: 'sdk';
    }>): Promise<void>;
    /**
     * Get process statistics
     *
     * @returns Statistics for all managed processes
     */
    getStats(): Record<ProcessState | 'total', number>;
    /**
     * Remove a process from registry
     *
     * @param agentId - Agent ID to remove
     * @returns Whether the operation succeeded
     */
    removeProcess(agentId: string): Promise<boolean>;
    /**
     * Get a single process record
     *
     * @param agentId - Agent ID to get
     * @returns Process record or undefined
     */
    getProcess(agentId: string): ProcessRecord | undefined;
    /**
     * Get a process record by PID
     *
     * @param pid - Process ID to get
     * @returns Process record or undefined
     */
    getProcessByPid(pid: number): ProcessRecord | undefined;
}
/**
 * Get the AgentProcessManager singleton instance
 *
 * @returns AgentProcessManager instance
 */
export declare function getAgentProcessManager(): AgentProcessManager;
export type { ProcessRecord, ProcessState, HealthStatus, ProcessInfo, ProcessHealthStatus };
//# sourceMappingURL=agent-process-manager.d.ts.map