UNPKG

@code-yeongyu/senpi

Version:

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

72 lines • 3.18 kB
/** * Deadline-bounded warnings for waits that have no deadline of their own (senpi#6189). * * A wait that never settles leaves no trace: no timer fires, no result is written, and the * session just idles. These helpers only observe such a wait. They log once when it outlives * its threshold and never resolve, reject, abort or delay anything themselves. */ /** A wait still pending after this long is logged once. A bash spawn's own shorter timeout lowers it. */ export const STALL_WARNING_MS = 30_000; /** The warning threshold for a bash spawn whose own `timeout` (in seconds) may be shorter than {@link STALL_WARNING_MS}. */ export function stallThresholdMs(timeoutSeconds) { if (typeof timeoutSeconds !== "number" || !Number.isFinite(timeoutSeconds) || timeoutSeconds <= 0) { return STALL_WARNING_MS; } return Math.max(1, Math.min(STALL_WARNING_MS, Math.trunc(timeoutSeconds * 1000))); } export function queueEntryLabel(event) { const toolCallId = event.toolCallId; return typeof toolCallId === "string" ? `${event.type}:${toolCallId}` : event.type; } /** * Call `onStall` once if `stop` has not run within `thresholdMs`. The caller keeps its own `await`, so the * watched wait's outcome and ordering are unchanged. The timer is unref'd: it never keeps the process alive. */ export function watchForStall(thresholdMs, onStall) { const startedAt = Date.now(); const timer = setTimeout(() => onStall(Date.now() - startedAt), thresholdMs); timer.unref?.(); return { stop: () => clearTimeout(timer) }; } /** The unsettled entries of a promise chain, oldest first, so a stalled wait on the chain can name what it waits behind. */ export class PendingQueueLedger { constructor() { this.nextId = 0; this.entries = new Map(); } /** * Record `run` as an entry from now until it settles, and return it wrapped for the chain. The wrapper returns * exactly what `run` returns or throws, and releases the entry from that result. It never attaches a handler to * the chain itself, so a rejection on the chain is exactly as visible (or unhandled) as before. */ track(label, run) { const id = this.nextId++; this.entries.set(id, { label, enqueuedAt: Date.now() }); const release = () => { this.entries.delete(id); }; return (...args) => { let result; try { result = run(...args); } catch (error) { release(); throw error; } // The chain adopts `result`, which marks it handled anyway; this handler changes nobody's view of it. if (result instanceof Promise) void result.then(release, release); else release(); return result; }; } snapshot() { const head = this.entries.values().next().value; if (head === undefined) return { queueDepth: 0 }; return { queueDepth: this.entries.size, queueHead: head.label, ageMs: Date.now() - head.enqueuedAt }; } } //# sourceMappingURL=stall-warning.js.map