UNPKG

@code-yeongyu/senpi

Version:

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

80 lines • 3.96 kB
/** Environment override for the cold-start policy: `transient` or `persistent`. */ export const HOST_COLD_START_ENV = "SENPI_RPC_HOST_COLD_START"; /** Environment override for the idle-exit window in milliseconds. */ export const HOST_IDLE_EXIT_MS_ENV = "SENPI_RPC_HOST_IDLE_EXIT_MS"; /** Default idle-exit window: 15 minutes of continuous no-connection, no-turn idle. */ export const DEFAULT_HOST_IDLE_EXIT_MS = 15 * 60_000; /** Soft handoff deadline: rescan and report, never interrupt turns or in-flight requests. */ export const HANDOFF_GRACE_MS_ENV = "SENPI_RPC_HANDOFF_GRACE_MS"; export const DEFAULT_HANDOFF_GRACE_MS = 10 * 60_000; export function parseColdStart(value) { return value === "transient" || value === "persistent" ? value : undefined; } export function parseIdleExitMs(value) { if (value === undefined || !/^\d+$/.test(value.trim())) return undefined; const parsed = Number(value.trim()); return Number.isFinite(parsed) && parsed > 0 ? parsed : undefined; } /** * Resolves the effective host policy. Precedence: environment overrides beat * settings.json, which beats the documented defaults (transient, 15 minutes). * Invalid values at either source fall through to the next source. */ export function resolveHostPolicy(settings, env) { const record = isRecord(settings) ? settings : {}; const coldStart = parseColdStart(env[HOST_COLD_START_ENV]) ?? parseColdStart(asOptionalString(record.coldStart)) ?? "transient"; const idleExitMs = parseIdleExitMs(env[HOST_IDLE_EXIT_MS_ENV]) ?? parseIdleExitMs(asOptionalString(record.idleExitMs)) ?? DEFAULT_HOST_IDLE_EXIT_MS; return { coldStart, idleExitMs }; } /** * Pure idle-window decision core. `update()` must be called with the CURRENT * activity state; the window only counts continuously idle time and any * activity resets it, so a busy host can never cross the threshold. * * `firstClientWithinMs` is the launcher's decision budget on a coordinated start (senpi#6001): until the * first client attaches, or that budget has passed, the window has not begun, so a host whose own startup * outlasts its idle window cannot exit before the ensure that started it has probed it. */ export class IdleExitDecider { constructor(idleExitMs, now = Date.now, firstClientWithinMs) { this.idleExitMs = idleExitMs; this.now = now; this.firstClientBy = firstClientWithinMs === undefined ? undefined : now() + firstClientWithinMs; } /** A client attached: the window starts over, even while it holds nothing yet (oh-my-openagent#14272). */ restart() { this.idleSince = undefined; this.firstClientBy = undefined; } update(activity) { // Only a client ends the wait for the first one: an unknown observer counts as an active turn at startup. if (activity.connections > 0) this.firstClientBy = undefined; if (this.firstClientBy !== undefined && this.now() < this.firstClientBy) return "active"; this.firstClientBy = undefined; // Any attachment or active turn both holds the host open and resets the // window, so only CONTINUOUS idle can ever cross the threshold. if (activity.connections > 0 || activity.activeTurns > 0) { this.idleSince = undefined; return "active"; } if (this.idleExitMs === Number.POSITIVE_INFINITY) return "idle"; if (this.idleSince === undefined) { this.idleSince = this.now(); return "idle"; } return this.now() - this.idleSince >= this.idleExitMs ? "exit" : "idle"; } } function isRecord(value) { return typeof value === "object" && value !== null && !Array.isArray(value); } function asOptionalString(value) { return typeof value === "string" ? value : typeof value === "number" ? String(value) : undefined; } //# sourceMappingURL=host-lifecycle-policy.js.map