/**
 * OrphanProcessReaper — Detect and clean up orphaned agentic-CLI
 * processes (Claude, Codex, future frameworks).
 *
 * Addresses the critical gap where framework CLI processes spawned
 * outside SessionManager (setup-wizard, login flow, corrupted state)
 * are invisible to the watchdog and accumulate indefinitely.
 *
 * Classification strategy:
 *   1. "tracked" — In a tmux session managed by SessionManager → leave alone
 *   2. "instar-orphan" — In a tmux session matching project naming but not tracked → auto-clean
 *   3. "external" — Not in a project-prefixed tmux session (user's own framework session, VS Code, etc.) → report only
 *
 * Safety: NEVER auto-kills framework sessions outside Instar.
 * External processes are only reported via Telegram for user decision.
 *
 * Provider-portability v1.0.0: was Claude-only. Now scans every
 * registered framework via `frameworkProcessSignals.ts`.
 */
import { EventEmitter } from 'node:events';
import type { SessionManager } from '../core/SessionManager.js';
import type { InstarConfig } from '../core/types.js';
import type { IntelligenceFramework } from '../core/intelligenceProviderFactory.js';
export interface FrameworkProcess {
    pid: number;
    ppid: number;
    rssKB: number;
    elapsedMs: number;
    command: string;
    tmuxSession: string | null;
    /** Which framework this process belongs to (claude-code, codex-cli, ...). */
    framework: IntelligenceFramework;
}
/**
 * @deprecated Use FrameworkProcess. ClaudeProcess remains as a type
 * alias for backwards-compat with v0.x consumers.
 */
export type ClaudeProcess = FrameworkProcess;
export type ProcessClassification = 'tracked' | 'instar-orphan' | 'external';
export interface ClassifiedProcess extends FrameworkProcess {
    classification: ProcessClassification;
    reason: string;
}
export interface ReaperReport {
    timestamp: string;
    tracked: ClassifiedProcess[];
    orphans: ClassifiedProcess[];
    external: ClassifiedProcess[];
    totalMemoryMB: number;
    orphanMemoryMB: number;
    externalMemoryMB: number;
    actionsPerformed: string[];
}
export interface OrphanReaperConfig {
    /** Poll interval in ms (default: 60000 — 1 minute) */
    pollIntervalMs?: number;
    /** Max age in ms before an orphan is auto-killed (default: 3600000 — 1 hour) */
    orphanMaxAgeMs?: number;
    /** Max age in ms before external processes are reported (default: 14400000 — 4 hours) */
    externalReportAgeMs?: number;
    /** Per-process RSS threshold in MB to flag as high-memory (default: 500) */
    highMemoryThresholdMB?: number;
    /** Whether to auto-kill instar orphans (default: true; false = report only) */
    autoKillOrphans?: boolean;
    /** Whether to report external (non-instar) Claude processes to the user (default: true) */
    reportExternalProcesses?: boolean;
    /** Callback to send alerts (e.g., Telegram) */
    alertCallback?: (message: string) => Promise<void>;
}
export declare class OrphanProcessReaper extends EventEmitter {
    private config;
    private reaperConfig;
    private sessionManager;
    private interval;
    private projectPrefix;
    private lastReport;
    private reportedExternalPids;
    private lastExternalAlertTime;
    private static EXTERNAL_ALERT_COOLDOWN_MS;
    constructor(config: InstarConfig, sessionManager: SessionManager, reaperConfig?: OrphanReaperConfig);
    start(): void;
    stop(): void;
    getLastReport(): ReaperReport | null;
    /**
     * Run a scan immediately and return the report.
     * Can be called on-demand via API endpoints.
     */
    scan(): Promise<ReaperReport>;
    private poll;
    /**
     * Find ALL framework-CLI processes (Claude, Codex, future frameworks)
     * owned by the current user.
     *
     * Builds a single `ps … | egrep …` pipeline with the disjunction of
     * every signal's psGrepNeedle, then parses each line and tags it with
     * the matched framework via `matchProcessSignal`. A single ps call
     * keeps the impact off the runtime hot-path.
     */
    private findAllFrameworkProcesses;
    /**
     * @deprecated v0.x compat shim — internal callers use the framework-
     * generic name. Retained so external code that referenced this method
     * via reflection or tests doesn't break. Will be removed in v2.
     */
    private findAllClaudeProcesses;
    /**
     * @deprecated v0.x compat shim — delegates to `matchProcessSignal`
     * with the Claude-code signal. New code should use the framework-
     * generic helpers from `frameworkProcessSignals.ts`.
     */
    private isClaudeCodeProcess;
    /**
     * List all tmux sessions and map pane PIDs to session names.
     */
    private listAllTmuxSessions;
    /**
     * Get tmux session names tracked by SessionManager.
     */
    private getTrackedSessionNames;
    /**
     * Classify each Claude process:
     *   - tracked: In a tmux session managed by SessionManager
     *   - instar-orphan: In a project-prefixed tmux session NOT tracked by SessionManager
     *   - external: Everything else (user sessions, VS Code, etc.)
     */
    private classifyProcesses;
    private getParentPid;
    private getProcessCommand;
    private killProcess;
    /**
     * §P0 #6 work-check: does the process have at least one active child? Used as
     * a 60-min-age-is-necessary-not-sufficient gate before a reap. A pgrep-empty
     * orphan is genuinely idle and safe to reap; an orphan with running children
     * is doing real work and must be kept.
     *
     * Errors → `false` (cannot inspect ⇒ allow the reap to proceed, matching the
     * pre-Phase-2 behavior; this never makes the reaper MORE aggressive — the
     * helper is a soft veto layered on top of the existing age gate).
     */
    private processHasActiveChildren;
    private killTmuxSession;
    /**
     * Manually kill an external process by PID.
     * Called via user command (e.g., "clean 12345").
     */
    killExternalProcess(pid: number): {
        success: boolean;
        message: string;
    };
    /**
     * Kill all external processes. User-initiated only.
     */
    killAllExternal(): {
        killed: number;
        freedMB: number;
        details: string[];
    };
    private parseElapsed;
    private formatDuration;
}
//# sourceMappingURL=OrphanProcessReaper.d.ts.map