/** * Shell Process Registry * * Purpose: * - Provide canonical lifecycle tracking and control for shell child-process executions. * * Key Features: * - Stable execution IDs with explicit lifecycle states. * - Active-process handle tracking for cancellation and chat-scoped stop operations. * - Query APIs for single execution and filtered execution history. * - Safe delete semantics that block deletion of active executions. * - Subscription API for status-change notifications. * * Implementation Notes: * - Registry uses in-memory maps and bounded history ordering. * - Lifecycle transitions are validated through a transition table. * - Cancel operations are idempotent and return explicit outcomes. * * Recent Changes: * - 2026-02-13: Initial implementation for shell child process monitor/cancel/delete lifecycle control. */ import type { ChildProcess } from 'child_process'; export type ShellProcessStatus = 'queued' | 'starting' | 'running' | 'completed' | 'failed' | 'canceled' | 'timed_out'; export interface ShellProcessExecutionRecord { executionId: string; command: string; parameters: string[]; directory: string; worldId: string | null; chatId: string | null; status: ShellProcessStatus; cancelRequested: boolean; queuedAt: string; startedAt: string | null; finishedAt: string | null; exitCode: number | null; signal: string | null; stdoutLength: number; stderrLength: number; error: string | null; durationMs: number | null; } export interface ShellProcessStatusEvent { executionId: string; status: ShellProcessStatus; record: ShellProcessExecutionRecord; } export interface ListShellProcessExecutionsOptions { limit?: number; statuses?: ShellProcessStatus[]; worldId?: string; chatId?: string; activeOnly?: boolean; } export type CancelShellProcessOutcome = 'cancel_requested' | 'already_finished' | 'not_found' | 'not_cancellable'; export interface CancelShellProcessResult { executionId: string; outcome: CancelShellProcessOutcome; } export type DeleteShellProcessOutcome = 'deleted' | 'not_found' | 'active_process_conflict'; export interface DeleteShellProcessResult { executionId: string; outcome: DeleteShellProcessOutcome; } export declare function createShellProcessExecution(input: { command: string; parameters: string[]; directory: string; worldId?: string; chatId?: string; }): ShellProcessExecutionRecord; export declare function attachShellProcessHandle(executionId: string, process: ChildProcess): boolean; export declare function detachShellProcessHandle(executionId: string): boolean; export declare function transitionShellProcessExecution(executionId: string, status: ShellProcessStatus, patch?: Partial>): ShellProcessExecutionRecord | null; export declare function markShellProcessCancelRequested(executionId: string): ShellProcessExecutionRecord | null; export declare function getShellProcessExecution(executionId: string): ShellProcessExecutionRecord | null; export declare function listShellProcessExecutions(options?: ListShellProcessExecutionsOptions): ShellProcessExecutionRecord[]; export declare function cancelShellProcessExecution(executionId: string): CancelShellProcessResult; export declare function stopShellProcessesForChatScope(worldId: string, chatId: string): { killed: number; }; export declare function deleteShellProcessExecution(executionId: string): DeleteShellProcessResult; export declare function subscribeShellProcessStatus(listener: (event: ShellProcessStatusEvent) => void): () => void; export declare function clearShellProcessRegistryForTests(): { executionCount: number; activeCount: number; };