UNPKG

@code-yeongyu/senpi

Version:

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

107 lines • 5.45 kB
import type { RpcHostStalledEvent } from "./rpc-types.ts"; /** Environment override for the drift that logs a warning, in milliseconds. */ export declare const LOOP_LAG_WARN_MS_ENV = "SENPI_RPC_LOOP_LAG_WARN_MS"; export declare const DEFAULT_LOOP_LAG_WARN_MS = 500; export { DEFAULT_LOOP_LAG_ERROR_MS, LOOP_LAG_ERROR_MS_ENV, loopLagErrorMs } from "./loop-lag-threshold.ts"; /** Measurement interval: short enough to bound the blamed window, cheap enough to ignore. */ export declare const LOOP_LAG_TICK_MS = 200; /** * Measurement interval once no session has done any work for `LOOP_LAG_IDLE_AFTER_MS` (senpi#3738, omo-desktop-app#3359): * an idle host has nothing to blame, and a 200 ms tick was its largest source of idle wakeups. Drift is the * lateness of a tick against its scheduled time, so a stall is only seen when a tick falls due during it: on * this tick a stall can read up to 1 s short. Attributed work therefore restores the 200 ms tick the moment it * starts (`onSessionActivityStart`), before it can block the loop, and a stall it causes is measured to within * 200 ms as before. Never longer than the heartbeat interval, so the heartbeat stays as fresh as before. */ export declare const LOOP_LAG_IDLE_TICK_MS = 1000; /** How long the host must stay without session activity before the tick slows down. */ export declare const LOOP_LAG_IDLE_AFTER_MS = 2000; /** One warning per window, however many stalls it covers. */ export declare const LOOP_LAG_WARN_INTERVAL_MS = 10000; export interface LoopLagWatchdogOptions { /** Delivers one `host_stalled` lifecycle record to every connection. */ readonly emit: (record: RpcHostStalledEvent) => void; /** Defaults to one stderr line; tests capture it. */ readonly log?: (message: string) => void; readonly now?: () => number; readonly env?: Readonly<Record<string, string | undefined>>; /** Process CPU time in microseconds; defaults to `process.cpuUsage()`. */ readonly cpuUsage?: () => { readonly user: number; readonly system: number; }; /** Live JS heap in bytes; defaults to `process.memoryUsage().heapUsed`. */ readonly heapUsed?: () => number; /** Refreshes the generation's heartbeat file; defaults to `writeHeartbeat`. */ readonly writeHeartbeat?: (generationDir: string, at: string) => Promise<void>; } /** * Event-loop stall detector for the shared host. * * Every in-process session runs on the host's one event loop, so a session that blocks * it freezes every other session and the transport with it. A 200 ms timer measures how * late it is actually invoked: that lateness IS the time the loop spent unable to serve * anyone. Past the warning threshold the host says so once per 10 s, naming the session * and tool whose work held the loop; past the error threshold it also emits a * `host_stalled` lifecycle record so attached clients (and the desktop) can show it. * * Every stall also carries the process CPU time and the heap movement of the stalled * window, so it explains itself (senpi#2211): CPU near the drift means the host was busy * (a heap drop in the same window points at a collection); CPU near zero means the * process did not run - the machine starved it or it sat in a blocking wait. * * The watchdog only reports. It never aborts a turn, kills a session, or refuses work. */ export declare class LoopLagWatchdog { private readonly emit; private readonly log; private readonly now; private readonly warnMs; private readonly errorMs; private timer; private expectedTickAt; private intervalMs; private quietMs; private readonly idleIntervalMs; private unsubscribeActivity; private activityMark; private lastWarnAt; private readonly cpuUsage; private readonly heapUsed; private cpuMicrosAtTick; private heapBytesAtTick; /** This host's own generation directory when a supervisor launched it; evidence goes nowhere else. */ private readonly evidenceDir; private heartbeatInFlight; private lastHeartbeatAt; private readonly heartbeatIntervalMs; private readonly writeHeartbeat; /** * The parent this host was launched under. Once it is gone the host is orphaned and on its way out: * its generation directory belongs to nobody now (gc may be judging or removing it), so the host * stops writing evidence there instead of racing that with a heartbeat a dying process may leave torn. */ private readonly launchParentPid; private evidenceFailureLogged; constructor(options: LoopLagWatchdogOptions); start(): void; /** Back to the 200 ms tick, from now: attributed work just started and may block the loop next. */ private wake; private schedule; stop(): void; /** * One measurement. Public so tests drive it on an injected clock instead of * waiting for real drift. The first tick only establishes the baseline. */ tick(): void; /** * A healthy tick refreshes the heartbeat a reader takes as "this loop is running"; it stops the moment * the loop does, which is how a stall in progress becomes visible (senpi#2566). Async and best-effort: * the session loop never waits on it, and a write still in flight skips the next beat. */ private beat; private persistStall; private ownsEvidenceDir; private evidenceFailed; } //# sourceMappingURL=loop-lag-watchdog.d.ts.map