/** * The process supervisor: the one module that knows how a command is started, * how its output is captured, and how it and everything it started are stopped. * Every platform-specific detail lives here and nowhere else (ADR-0004): * nothing else in the project may spawn or signal a process. * * @author mystery4f <40482524+mystery4f@users.noreply.github.com> */ import { type ShellChoice, type ShellPolicy } from "./shell.js"; /** How one command is handed to this platform's shell lives with the policy (ADR-0008). */ export type { ShellInvocation } from "./shell.js"; /** How a process ended. */ export interface ProcessExit { exitCode: number | null; signal: NodeJS.Signals | null; } /** How a stop request ended. */ export type StopOutcome = /** The tree is gone; the polite signal or the forced kill took it down. */ "stopped" /** Nothing was left to signal: the command had already ended. */ | "already-exited" /** The forced kill was sent but the tree is still there. */ | "tree-survived"; export interface StopResult { readonly outcome: StopOutcome; /** Milliseconds from the stop request to this answer. */ readonly tookMs: number; /** * How long the polite signal was allowed to wait, and `0` when nothing was * signalled. Worth carrying: the same command stops in a second or in ten * depending on this number, so a result that names it needs no inference. */ readonly graceMs: number; /** What the system said, when the tree could not be confirmed gone. */ readonly detail?: string; } export interface StopProcessOptions { /** How long the polite signal gets before the forced kill. Defaults to 3s. */ graceMs?: number; /** How long the forced kill gets to clear the tree. Defaults to 5s. */ confirmMs?: number; } /** A command that is already running, with its output going to one file. */ export interface RunningProcess { readonly pid: number | undefined; /** Resolves once the process is gone and every byte it wrote is on disk. */ readonly exit: Promise; /** Bytes flushed to the output file so far. */ bytesWritten(): number; /** * Stop the command and everything it started: a polite signal, a grace * period, a forced kill, then polling until the whole tree is gone. A second * call returns the first answer instead of signalling twice. */ stop(options?: StopProcessOptions): Promise; /** True once a stop signal was actually delivered to the tree. */ signalled(): boolean; /** * True once the process is gone, including when a stop ended it. A caller that * adopted the process has to know the tree is still there before it hands it to * a task; an exit that already happened belongs in front of the reader instead. */ hasExited(): boolean; } export interface StartProcessOptions { command: string; /** Directory the command runs in. */ cwd: string; /** Output file; created or appended to. */ outputPath: string; /** * Bytes the output file may hold. Chunks that would cross the cap are kept up * to the cap and then dropped, so the file stops on the exact byte while the * stop travels to the tree. */ maxOutputBytes?: number; /** Called once, when a chunk is dropped for crossing `maxOutputBytes`. */ onOutputCap?: () => void; platform?: NodeJS.Platform; env?: NodeJS.ProcessEnv; /** * Variables for this child only, layered over `env`. Empty strings are values * and blank the variable; the base environment still decides the shell and * carries the stop commands. */ envOverrides?: Record; /** Shell resolved at load time; resolved from `env` when it is not given. */ shellPolicy?: ShellPolicy; /** Shell for this one command; the policy's shell unless this names another. */ shell?: ShellChoice; } /** * Start one command and stream stdout and stderr into one output file. * * Both pipes write into the same append stream, so the file holds the two * streams in the order the writes arrive instead of two files the reader would * have to interleave by hand. The exit promise resolves only after the stream * has flushed, so "the task finished" always means "its output is on disk". */ export declare function startProcess(options: StartProcessOptions): RunningProcess; /** * How long a stop lets the polite signal work before forcing. Exported because * it is the documented default a caller overrides per stop, not an internal * constant: a test that skips the wait still has to be able to say what it skipped. */ export declare const KILL_GRACE_MS = 3000; /** How long a stop waits for a forced kill to clear the tree. */ export declare const KILL_CONFIRM_MS = 5000;