UNPKG

@code-yeongyu/senpi

Version:

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

85 lines • 4.91 kB
/** * Zero-session trim: when a host that held sessions drops to zero of them, collect once and say what * that returned (`host_trimmed`, footprint before and after). * * Every session's state is per session - its runtime, its extension module graph (#1948) - so once * the registry is empty nothing live references it, yet the process keeps the pages until a full * collection runs; an idle task shard otherwise sits at its peak footprint for its whole idle window. * At most one trim per minute; idle exit is untouched. The single full collection is the accepted * exception to the no-sync rule, and the record is what logs it. * * A busy shard (a parent's task DAG) may never reach zero while it runs: its footprint stayed at * 40-60 GB while about 30 children came and went, almost all of it freed heap pages, because a host * whose other sessions keep allocating never goes idle long enough to return them (senpi#3218). So a * drop in the session count also trims, but only once the footprint is at least * {@link DECLINE_TRIM_FLOOR_BYTES}: a small host never pays the collection pause. With sessions still * running, the pause blocks them, so each collection is timed and the next busy-host trim waits at * least {@link DECLINE_TRIM_PAUSE_FACTOR} times that pause, which keeps collection near 1% of a busy * host's loop in steady state. A drop that lands inside that wait is kept and answered when it ends. * The zero-session trim keeps the plain one-minute spacing: no session is left to block. The first busy-host trim has no earlier pause to wait on, and one * collection on a multi-GB live heap can itself pass the stall threshold (measured: 63 ms at 0.5 GB live * and 149 ms at 1.5 GB on one machine, 5.2 s at 3 GB live on a slower one); that costs a `host_stalled` * record and ~120 s of refused ensures, never a session, and beats holding tens of GB of freed pages. */ import { type ProcessFootprint } from "../../core/process-footprint.ts"; import type { RpcHostTrimmedEvent } from "./rpc-host-lifecycle-types.ts"; export declare const ZERO_SESSION_TRIM_TICK_MS = 1000; export declare const ZERO_SESSION_TRIM_MIN_INTERVAL_MS = 60000; /** Below this footprint a session-count drop with sessions still open does not collect. */ export declare const DECLINE_TRIM_FLOOR_BYTES: number; /** A busy-host trim waits at least this many times the previous collection's pause. */ export declare const DECLINE_TRIM_PAUSE_FACTOR = 100; export interface ZeroSessionTrimmerOptions { readonly emit: (record: RpcHostTrimmedEvent) => void; /** Live sessions, including ones opening or closing. */ readonly sessions: () => number; readonly now?: () => number; /** Monotonic milliseconds for timing a collection; a wall-clock jump must not stretch the next wait. */ readonly monotonicNow?: () => number; readonly readFootprint?: () => ProcessFootprint; /** Runs one full collection; `false` when the runtime exposes none. */ readonly collect?: () => boolean; } export declare class ZeroSessionTrimmer { private readonly options; private readonly now; private heldSessions; private lastSessionCount; /** * A drop not yet answered: kept across ticks until the busy interval has elapsed and the footprint * was checked, so children closing inside the cooldown still get their trim once it ends. A backfill * in the same second must not hide it either. */ private declined; /** A drop to zero that landed inside the spacing: answered when it ends, unless sessions came back first. */ private zeroPending; private lastTrimAt; /** A collection that ran on the previous tick; its record waits for the allocator to return the pages. */ private pending; private lastPauseMs; private timer; private started; constructor(options: ZeroSessionTrimmerOptions); /** * Ticks only while a trim is owed (senpi#3747): the registry reports every open and close through * `observe`, so a host whose session count holds still has nothing to check and no timer runs. */ start(): void; stop(): void; /** The registry's own signal: any session that existed, however briefly, makes the next zero a transition. */ observe(size: number): void; check(): void; /** A collection waiting to be reported, a drop waiting for its spacing, or a zero not yet answered. */ private owesTick; private arm; private evaluate; private intervalElapsed; private busyIntervalElapsed; private trim; /** One tick after the collection: freed pages are decommitted lazily, so an immediate read shows none of it. */ private report; private readFootprint; } /** `Bun.gc(true)` on Bun, `gc()` on Node started with `--expose-gc`, otherwise nothing. */ export declare function collectFully(): boolean; //# sourceMappingURL=host-zero-session-trim.d.ts.map