UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

73 lines • 4.24 kB
/** * Reaps the exited DIRECT children of the host process that no thread is going * to wait on. * * Why the host needs this at all (measured, see the matrix in the tracker): a * `worker_threads` Worker owns the exit watchers of every child IT spawned, and * `worker.terminate()` destroys those watchers with the thread - the session * worker quarantine (`session-worker-client.ts`) is exactly that call. Every * spawn API leaks the same way (node `child_process.spawn`, `Bun.spawn`, * `Bun.$`), under bun from source, under the compiled binary, and under Node, * and the zombies survive for the life of the host. Children spawned from a * LIVE thread are reaped by their own runtime and never reach this code. * * Why it is safe: a zombie carries no hint about which thread meant to wait on * it, so the only protection against STEALING a child from a live waiter is * time. A thread blocked in a synchronous call cannot reap its own exited child * until it unblocks, and stealing that child breaks the owner's contract - the * measurement shows `child_process` and `Bun.spawn` then reject with ECHILD and * `Bun.$` never settles at all. The reaper therefore waits for a pid to stay * waitable across two ticks at least `minWaitableMs` apart, defaulting far above * the longest synchronous block we measured, and it always peeks with * `waitid(..., WNOWAIT)` before it consumes anything. */ import { type ChildReaperSyscalls } from "./child-reaper-syscalls.ts"; /** Set to `0` to turn the reaper off; anything else leaves it on. */ export declare const CHILD_REAPER_ENV = "SENPI_RPC_HOST_REAPER"; /** Overrides how long a child must stay waitable before it is considered abandoned. */ export declare const CHILD_REAPER_MIN_WAITABLE_MS_ENV = "SENPI_RPC_HOST_REAPER_MIN_WAITABLE_MS"; /** Hard floor: a pid waitable for less than this is never touched. */ export declare const MIN_WAITABLE_FLOOR_MS = 5000; /** * The tick while the host has no children at all (senpi#3747): nothing can be waitable, and a child that * appears is only reaped after `minWaitableMs` (30 s by default) anyway, so a 5 s look costs nothing. */ export declare const CHILD_REAPER_IDLE_TICK_MS = 5000; export interface ChildReaperConfig { readonly enabled: boolean; readonly tickMs: number; readonly minWaitableMs: number; } export interface ChildReaperOptions { readonly syscalls: ChildReaperSyscalls; readonly now?: () => number; readonly minWaitableMs?: number; readonly log?: (message: string) => void; } export interface ChildReaper { /** One pass over the direct children: observe, then reap what aged out. */ tick(): void; /** Children seen exited and still unclaimed, oldest observation first. */ readonly waitingPids: readonly number[]; /** Direct children the last pass saw, live or exited. */ readonly childCount: number; } export declare function resolveChildReaperConfig(env: Readonly<Record<string, string | undefined>>): ChildReaperConfig; export declare function createChildReaper(options: ChildReaperOptions): ChildReaper; /** * Arms the reaper on the host loop. The tick is unref'd, so it never keeps an * otherwise idle host alive, and the whole thing is one `setInterval` the caller * stops at shutdown. */ export declare function startHostChildReaper(log: (message: string) => void): Promise<() => void>; /** Ticks `reaper` every `tickMs` while it has children, every `CHILD_REAPER_IDLE_TICK_MS` while it has none. */ export declare function scheduleChildReaper(reaper: ChildReaper, tickMs: number): () => void; /** * Collects the exit status of the exact exited children in `pids` right away (#1962). Only for pids whose * owning thread is gone, such as the children of a retired eval worker: no live thread will ever wait on them, * so the reaper's two-tick window protects nothing and would only leave them as zombies until it elapses. * A pid that is not an exited child of this process is left alone; `waitpid(-1)` is never called. Resolves to * the pids it collected; under Node or on Windows it collects nothing. */ export declare function collectOrphanedChildren(pids: readonly number[]): Promise<number[]>; //# sourceMappingURL=child-reaper.d.ts.map