import { IWorkerAdapter, WorkerAdapterConfig, TaskOptions, TaskResult, TaskHandle, PoolStats } from '@stark-o/shared'; export { IWorkerAdapter, PoolStats, TaskCancelledError, TaskHandle, TaskOptions, TaskResult, TaskTimeoutError, WorkerAdapterConfig, WorkerNotInitializedError } from '@stark-o/shared'; /** * Extended configuration options for the Node.js worker adapter. */ interface NodeWorkerAdapterConfig extends WorkerAdapterConfig { /** * Maximum memory limit per worker in megabytes. * Sets --max-old-space-size for the sub-process. */ maxMemoryMB?: number; } /** * Message sent from the parent to the worker subprocess. */ interface WorkerRequest { type: 'execute'; taskId: string; bundleCode: string; context: unknown; args: unknown[]; } /** * Data-update message pushed to a running worker subprocess via IPC. */ interface WorkerDataUpdate { type: 'data-update'; message: { type: string; payload?: unknown; }; } /** * Message sent back from the worker subprocess to the parent. */ interface WorkerResponse { type: 'result' | 'error'; taskId: string; value?: unknown; error?: string; errorStack?: string; } /** * Worker adapter using Node.js child_process.fork() for sub-process management. * * Each task spawns a dedicated subprocess that runs the worker script. * The worker script imports all dependencies (like PodLogSink) directly * rather than receiving serialized functions. Only pack bundle code and * metadata state are passed as arguments via IPC. */ declare class WorkerAdapter implements IWorkerAdapter { private readonly config; private initialized; private readonly runningTasks; private taskIdCounter; /** Path to the built-in worker script (resolved at runtime) */ private workerScriptPath; constructor(config?: NodeWorkerAdapterConfig); /** * Initialize the worker adapter. * For subprocess-based execution, this is a lightweight operation. */ initialize(): Promise; /** * Check if the adapter has been initialized. */ isInitialized(): boolean; /** * Execute pack code in a subprocess. * * Forks a new child process running the worker script, passes the * bundle code + execution context as arguments via IPC message, * and waits for the result. * * @param bundleCode - The pack's JavaScript bundle code * @param context - The PackExecutionContext (serializable metadata/state) * @param args - Additional arguments for the pack entrypoint * @param options - Task execution options (timeout, etc.) * @returns Promise resolving to the task result */ execTask(bundleCode: string, context: unknown, args?: unknown[], options?: TaskOptions): Promise>; /** * Execute pack code in a subprocess with cancellation support. * * @param bundleCode - The pack's JavaScript bundle code * @param context - The PackExecutionContext (serializable metadata/state) * @param args - Additional arguments for the pack entrypoint * @param options - Task execution options * @returns Task handle with promise and cancel/forceTerminate functions */ execTaskCancellable(bundleCode: string, context: unknown, args?: unknown[], options?: TaskOptions): TaskHandle & { taskId: string; }; exec(_fn: (...args: Args) => T | Promise, _args?: Args, _options?: TaskOptions): Promise>; execCancellable(_fn: (...args: Args) => T | Promise, _args?: Args, _options?: TaskOptions): TaskHandle; execMethod(_method: string, _args?: unknown[], _options?: TaskOptions): Promise>; execMethodCancellable(_method: string, _args?: unknown[], _options?: TaskOptions): TaskHandle; /** * Get current pool statistics. * Reports running subprocess counts as pool stats. */ getStats(): PoolStats; /** * Push a data-update message to a running worker subprocess via IPC. * Used by the executor's sendMessage() to relay messages to packs. */ sendDataToWorker(taskId: string, message: { type: string; payload?: unknown; }): void; /** * Cancel all running tasks by killing their subprocesses. */ cancelAll(): void; /** * Terminate all workers and shut down. * * @param force - If true, use SIGKILL; otherwise SIGTERM * @param _timeout - Unused (kept for interface compatibility) */ terminate(force?: boolean, _timeout?: number): Promise; /** * Get the number of available CPU cores. */ static getCpuCount(): number; /** * Ensure the adapter is initialized before operations. */ private ensureInitialized; /** * Generate a unique task ID. */ private generateTaskId; /** * Handle and optionally report errors. */ private handleError; } /** * Default worker adapter instance. */ declare const defaultWorkerAdapter: WorkerAdapter; export { type NodeWorkerAdapterConfig, WorkerAdapter, type WorkerDataUpdate, type WorkerRequest, type WorkerResponse, defaultWorkerAdapter };