From 40a66175a2bc02baa2e19a6619143667eded0f58 Mon Sep 17 00:00:00 2001 From: Lukas May Date: Fri, 30 Jan 2026 13:13:06 +0100 Subject: [PATCH] feat(01-03): create process types and registry - ProcessInfo interface for tracking process metadata - SpawnOptions interface for spawn configuration - ProcessRegistry class with Map-based storage - CRUD operations: register, unregister, get, getAll, getByPid, clear - Additional helpers: updateStatus, size getter --- src/process/registry.ts | 95 +++++++++++++++++++++++++++++++++++++++++ src/process/types.ts | 45 +++++++++++++++++++ 2 files changed, 140 insertions(+) create mode 100644 src/process/registry.ts create mode 100644 src/process/types.ts diff --git a/src/process/registry.ts b/src/process/registry.ts new file mode 100644 index 0000000..0f1aee6 --- /dev/null +++ b/src/process/registry.ts @@ -0,0 +1,95 @@ +/** + * Process Registry + * + * In-memory registry for tracking managed processes. + * Provides CRUD operations for process metadata. + * + * Note: This is in-memory for now. Phase 2 adds SQLite persistence. + */ + +import type { ProcessInfo } from './types.js'; + +/** + * Registry for tracking managed processes. + * Stores process information in memory using a Map. + */ +export class ProcessRegistry { + private processes: Map = new Map(); + + /** + * Register a new process in the registry. + * @param info - Process information to register + */ + register(info: ProcessInfo): void { + this.processes.set(info.id, info); + } + + /** + * Remove a process from the registry. + * @param id - Process ID to unregister + */ + unregister(id: string): void { + this.processes.delete(id); + } + + /** + * Get a process by its ID. + * @param id - Process ID to look up + * @returns Process info if found, undefined otherwise + */ + get(id: string): ProcessInfo | undefined { + return this.processes.get(id); + } + + /** + * Get all registered processes. + * @returns Array of all process info objects + */ + getAll(): ProcessInfo[] { + return Array.from(this.processes.values()); + } + + /** + * Find a process by its operating system PID. + * @param pid - OS process ID to search for + * @returns Process info if found, undefined otherwise + */ + getByPid(pid: number): ProcessInfo | undefined { + for (const process of this.processes.values()) { + if (process.pid === pid) { + return process; + } + } + return undefined; + } + + /** + * Clear all processes from the registry. + */ + clear(): void { + this.processes.clear(); + } + + /** + * Update a process's status in the registry. + * @param id - Process ID to update + * @param status - New status value + * @returns true if process was found and updated, false otherwise + */ + updateStatus(id: string, status: ProcessInfo['status']): boolean { + const process = this.processes.get(id); + if (process) { + process.status = status; + return true; + } + return false; + } + + /** + * Get the count of registered processes. + * @returns Number of processes in the registry + */ + get size(): number { + return this.processes.size; + } +} diff --git a/src/process/types.ts b/src/process/types.ts new file mode 100644 index 0000000..0088105 --- /dev/null +++ b/src/process/types.ts @@ -0,0 +1,45 @@ +/** + * Process Management Types + * + * Type definitions for process lifecycle management. + * Used by ProcessRegistry and ProcessManager. + */ + +/** + * Process status states + */ +export type ProcessStatus = 'running' | 'stopped' | 'crashed'; + +/** + * Information about a managed process + */ +export interface ProcessInfo { + /** Unique identifier for the process */ + id: string; + /** Operating system process ID */ + pid: number; + /** Command that was executed */ + command: string; + /** Arguments passed to the command */ + args: string[]; + /** When the process was started */ + startedAt: Date; + /** Current status of the process */ + status: ProcessStatus; +} + +/** + * Options for spawning a new process + */ +export interface SpawnOptions { + /** Unique identifier for tracking this process */ + id: string; + /** Command to execute */ + command: string; + /** Arguments to pass to the command */ + args?: string[]; + /** Working directory for the process */ + cwd?: string; + /** Environment variables for the process */ + env?: Record; +}