UNPKG

@code-yeongyu/senpi

Version:

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

87 lines • 4.14 kB
/** * A session that runs on in the background (`run_in_background`, or a foreground command that auto-detached) is * announced to the host and, only when opted into (senpi#6201), stopped at a configured age. The reap runs here, where * the session's process group lives (this process, or the tool child), through `manager.stop`: the same TERM-then-KILL * stop `kill_bash` uses. Its one timer is unref'd and cleared when the session exits; with no opt-in none is armed. * A reap counts only when that stop resolved and the session really exited; a stop that rejects or leaves the session * running is reported once (`onBackgroundReapFailed`) and never retried, and the session keeps running. */ import { getAgentDir } from "../../../../../config.js"; import { createSessionLogger } from "../../../../session-log.js"; const MINUTE_MS = 60_000; const MAX_TIMER_MS = 2_147_483_647; /** The call's `auto_reap_minutes` wins over the setting; a whole number of minutes >= 1 reaps, 0 or less turns it off. */ function resolveAutoReapMinutes(requested, configured) { const value = requested ?? configured; if (value === undefined || !Number.isFinite(value)) return undefined; const minutes = Math.trunc(value); return minutes >= 1 ? minutes : undefined; } export function backgroundStart(input, configuredMinutes, startedAtMs) { return { description: input.description ?? input.command, command: input.command, startedAtMs, autoReapMinutes: resolveAutoReapMinutes(input.auto_reap_minutes, configuredMinutes), }; } function reapedView(runtime, minutes) { return { exitResult: runtime.exitResult, autoReapedAfterMinutes: minutes, fullOutput: () => runtime.fullOutput(), }; } export function trackBackgroundSession(ctx, id, runtime, start) { ctx.onBackgroundStart?.(id, start.description, start.startedAtMs, start.command, start.autoReapMinutes); const minutes = start.autoReapMinutes; let reapedAfterMinutes; /** The one reap attempt; the session's exit report waits for it, since the stop's own exit fires before it resolves. */ let reaping; let timer; const reap = async (age) => { const failure = await ctx.manager.stop(id).then((stopped) => { if (!stopped) return `the terminal manager has no session ${id} to stop`; return runtime.exited ? undefined : "the session was still running after the stop (TERM, then KILL)"; }, (error) => (error instanceof Error ? error.message : String(error))); if (failure === undefined) { reapedAfterMinutes = age; return; } const logger = ctx.sessionLogger ?? createSessionLogger(getAgentDir()); logger.warn("bash_auto_reap_failed", { bash_id: id, autoReapMinutes: age, error: failure }); if (!runtime.exited) ctx.onBackgroundReapFailed?.(id, age, failure); }; const arm = (deadlineMs) => { timer = setTimeout(() => { timer = undefined; if (runtime.exited || minutes === undefined) return; if (Date.now() < deadlineMs) { arm(deadlineMs); return; } // Recorded before the stop starts: the exit it causes can fire from inside `stop` itself. reaping = Promise.resolve().then(() => reap(minutes)); }, Math.min(Math.max(0, deadlineMs - Date.now()), MAX_TIMER_MS)); timer.unref?.(); }; if (minutes !== undefined && !runtime.exited) arm(start.startedAtMs + minutes * MINUTE_MS); if (minutes === undefined && !ctx.onBackgroundExit) return; runtime.session.onExit(() => { if (timer !== undefined) clearTimeout(timer); timer = undefined; const report = () => ctx.onBackgroundExit?.(id, reapedAfterMinutes === undefined ? runtime : reapedView(runtime, reapedAfterMinutes)); if (reaping === undefined) report(); else void reaping.then(report); }); } //# sourceMappingURL=background-auto-reap.js.map