/** * Shell Manager - Track and manage background shell processes */ import { type ChildProcess } from 'node:child_process'; /** * Status of a background shell */ export type ShellStatus = 'running' | 'completed' | 'failed' | 'killed'; /** * Information about a background shell */ export interface BackgroundShell { /** * Unique identifier for this shell */ id: string; /** * The command being executed */ command: string; /** * Current status */ status: ShellStatus; /** * When the shell was started */ startTime: Date; /** * When the shell finished (if completed/failed/killed) */ endTime?: Date; /** * Exit code (if completed or failed) */ exitCode?: number; /** * Working directory */ cwd?: string; } /** * Output from a background shell */ export interface ShellOutput { /** * Shell ID */ id: string; /** * Current status */ status: ShellStatus; /** * Standard output (new since last read) */ stdout: string; /** * Standard error (new since last read) */ stderr: string; /** * Whether there is more output buffered */ hasMore: boolean; /** * Exit code (if completed) */ exitCode?: number; } /** * Options for ShellManager */ export interface ShellManagerOptions { /** * Maximum buffer size per stream in bytes */ maxBufferSize?: number; /** * Maximum number of concurrent shells */ maxShells?: number; /** * Shell to use (default: /bin/bash) */ shell?: string; /** * Auto-cleanup completed shells after this many milliseconds * Set to 0 to disable auto-cleanup */ autoCleanupMs?: number; } /** * Manages background shell processes */ export declare class ShellManager { private readonly shells; private readonly maxBufferSize; private readonly maxShells; private readonly shell; private readonly autoCleanupMs; private readonly cleanupTimers; constructor(options?: ShellManagerOptions); /** * Spawn a new background shell */ spawn(command: string, options?: { cwd?: string; env?: Record; }): string; /** * Get output from a shell (only new output since last read) */ getOutput(id: string, filter?: RegExp): ShellOutput | null; /** * Get all output from a shell (including previously read) */ getAllOutput(id: string): ShellOutput | null; /** * Kill a background shell */ kill(id: string): boolean; /** * Verify and sync shell status with actual process state. * Call this to ensure status reflects reality after potential race conditions. */ verifyStatus(id: string): ShellStatus | null; /** * Verify all shells and sync their status. * Returns number of shells whose status was corrected. */ verifyAllStatus(): number; /** * List all shells */ list(): BackgroundShell[]; /** * List running shells only */ listRunning(): BackgroundShell[]; /** * Get shell info */ get(id: string): BackgroundShell | null; /** * Remove a shell from tracking (only if not running) */ remove(id: string): boolean; /** * Clear all completed/failed/killed shells */ clearCompleted(): number; /** * Kill all running shells */ killAll(): number; /** * Adopt an existing running process into the shell manager. * Used when moving a foreground bash command to background (Ctrl+B). * * @param process - The ChildProcess to adopt * @param options - Options including command and initial buffers * @returns The shell ID for tracking */ adoptProcess(process: ChildProcess, options: { command: string; cwd?: string; initialStdout?: string; initialStderr?: string; }): string; /** * Cleanup - kill all shells and clear state */ dispose(): void; /** * Append data to buffer with size limit */ private appendToBuffer; /** * Schedule auto-cleanup for a completed shell */ private scheduleCleanup; } /** * Get or create the default shell manager */ export declare function getDefaultShellManager(): ShellManager; /** * Set a custom default shell manager */ export declare function setDefaultShellManager(manager: ShellManager): void;