@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
251 lines • 11.1 kB
JavaScript
/**
* Idle watchdog for compaction summarization streams.
*
* A provider connection can stall — open but silent — for far longer than any
* user will wait, and compaction previously had no bound at all: the session
* sat on "Compacting…" until ESC aborted it. The agent loop's main-turn
* reader already has this shape of protection (`StreamIdleTimeoutError` in
* packages/agent); this brings the same guarantee to summarization requests.
*/
export class StreamIdleTimeoutError extends Error {
constructor(idleTimeoutMs) {
super(`Summarization stream stalled: no provider events for ${idleTimeoutMs}ms; treating the request as dead`);
this.name = "StreamIdleTimeoutError";
this.idleTimeoutMs = idleTimeoutMs;
}
}
/**
* A stream that keeps trickling events never trips the idle watchdog, yet the
* summarization it feeds is serialized on the session's agent-event queue: a
* slow-but-alive request holds tool results at the batch barrier and keeps typed
* input queued until the user aborts. The wall-clock budget bounds that class.
*/
export class StreamDurationBudgetError extends Error {
constructor(maxDurationMs) {
super(`Summarization stream exceeded its ${maxDurationMs}ms wall-clock budget; treating the request as too slow to keep the session waiting`);
this.name = "StreamDurationBudgetError";
this.maxDurationMs = maxDurationMs;
}
}
/** Matches the agent stream idle-timeout default (`httpIdleTimeoutMs`). */
export const DEFAULT_SUMMARIZATION_IDLE_TIMEOUT_MS = 300_000;
/**
* Total time one summarization attempt may hold the session. Well above healthy
* summarizations (tens of seconds) and below the idle budget, so a live-but-slow
* provider fails fast enough to keep the session interactive. Retries apply this
* budget per attempt.
*/
export const DEFAULT_SUMMARIZATION_MAX_DURATION_MS = 120_000;
/**
* How much the wall-clock budget grows per estimated input token.
*
* The 120s floor covers healthy summaries. Large sessions summarize hundreds of
* thousands of tokens: a 257k-token input already exceeds 120s on slower
* providers while still streaming (#1068), so the budget scales with the amount
* being summarized. 2ms/token assumes a worst-case sustained throughput of ~500
* tokens/second; faster providers simply finish well inside the budget.
*/
export const SUMMARIZATION_MAX_DURATION_PER_TOKEN_MS = 2;
/**
* Absolute ceiling for one summarization attempt. The wall-clock budget exists
* to stop a slow-but-alive stream from holding the session forever; it must stay
* a bound, so size-scaled budgets are clamped here instead of growing linearly
* without limit.
*/
export const SUMMARIZATION_MAX_DURATION_CAP_MS = 1_800_000;
/**
* Total wall clock ONE compaction may hold the session across every attempt,
* retry and overflow shrink.
*
* The per-attempt budget is deliberately proportional to the input, so a large
* session legally licenses a 690s attempt (345k tokens) or the full 30-minute
* ceiling (900k tokens), and each retry re-arms that budget from scratch: the
* user's wait grew with the very thing that made it slow, without bound (#1741).
* This cap is the session-health bound the per-attempt budget cannot be: it
* never scales with the input, and every attempt of one compaction shares it.
*/
export const SUMMARIZATION_TOTAL_BUDGET_MS = 900_000;
/**
* Total budget for one compaction. Size never raises it; only an explicit
* `compaction.summarizationMaxDurationMs` override does, because an operator who
* deliberately allows a longer single attempt must not have that attempt cut
* short by the total. Clamped to {@link SUMMARIZATION_MAX_DURATION_CAP_MS}.
*/
export function summarizationTotalBudgetMs(attemptOverrideMs) {
const override = attemptOverrideMs !== undefined && Number.isFinite(attemptOverrideMs) && attemptOverrideMs > 0
? Math.min(SUMMARIZATION_MAX_DURATION_CAP_MS, attemptOverrideMs)
: 0;
return Math.max(SUMMARIZATION_TOTAL_BUDGET_MS, override);
}
/**
* One compaction outlived {@link SUMMARIZATION_TOTAL_BUDGET_MS}. Distinct from
* {@link StreamDurationBudgetError}, which bounds a single attempt: this one says
* no further attempt may start, so recovery must come from the deterministic
* fallback rather than another provider request.
*/
export class SummarizationTotalBudgetError extends Error {
constructor(totalBudgetMs) {
super(`Compaction exceeded its ${totalBudgetMs}ms total wall-clock budget across every summarization attempt and retry`);
this.name = "SummarizationTotalBudgetError";
this.totalBudgetMs = totalBudgetMs;
}
}
export function createSummarizationDeadline(totalBudgetMs, now = Date.now) {
const startedMs = now();
const remainingMs = () => Math.max(0, totalBudgetMs - (now() - startedMs));
return {
totalBudgetMs,
remainingMs,
attemptBudgetMs: (requestedMs) => {
const remaining = remainingMs();
if (remaining <= 0)
throw new SummarizationTotalBudgetError(totalBudgetMs);
return Math.min(requestedMs, remaining);
},
};
}
/**
* Total time one summarization attempt may hold the session, sized to its input.
*
* The budget never shrinks below {@link DEFAULT_SUMMARIZATION_MAX_DURATION_MS};
* inputs small enough that `2ms/token` stays under that floor keep the exact
* 120s contract. Above ~60k estimated tokens the budget grows by
* {@link SUMMARIZATION_MAX_DURATION_PER_TOKEN_MS} per token and is clamped to
* {@link SUMMARIZATION_MAX_DURATION_CAP_MS}. An explicit positive `overrideMs`
* (e.g. the `compaction.summarizationMaxDurationMs` setting) replaces the
* computed budget; non-finite and non-positive values are ignored.
*/
export function summarizationMaxDurationMs(estimatedInputTokens, overrideMs) {
if (overrideMs !== undefined && Number.isFinite(overrideMs) && overrideMs > 0) {
return Math.min(SUMMARIZATION_MAX_DURATION_CAP_MS, overrideMs);
}
const scaled = estimatedInputTokens * SUMMARIZATION_MAX_DURATION_PER_TOKEN_MS;
return Math.min(SUMMARIZATION_MAX_DURATION_CAP_MS, Math.max(DEFAULT_SUMMARIZATION_MAX_DURATION_MS, scaled));
}
const IDLE_TRIP = "idle-trip";
const BUDGET_TRIP = "budget-trip";
const CALLER_ABORTED = "caller-aborted";
export async function consumeStreamWithIdleTimeout(stream, options) {
const { idleTimeoutMs, maxDurationMs, abort, onEvent, signal, settle } = options;
let iterator;
let removeAbortListener;
let callerAbortPromise;
// One absolute deadline for the whole stream, not a per-read budget.
let budgetPromise;
let budgetTimer;
let budgetMs = 0;
if (maxDurationMs !== undefined) {
budgetMs = maxDurationMs;
const { promise, resolve } = Promise.withResolvers();
budgetTimer = setTimeout(() => resolve(BUDGET_TRIP), budgetMs);
budgetTimer.unref?.();
budgetPromise = promise;
}
if (signal !== undefined && !signal.aborted) {
const { promise, resolve } = Promise.withResolvers();
const onAbort = () => resolve(CALLER_ABORTED);
signal.addEventListener("abort", onAbort, { once: true });
removeAbortListener = () => signal.removeEventListener("abort", onAbort);
callerAbortPromise = promise;
}
// Settle the stream under the timers this call already armed. Every exit that
// is not a thrown watchdog error goes through here, so `result()` can never be
// awaited with no deadline in force.
const settleUnderWatchdogs = async () => {
if (!settle)
return undefined;
const { promise: idlePromise, resolve: resolveIdle } = Promise.withResolvers();
const timer = setTimeout(() => resolveIdle(IDLE_TRIP), idleTimeoutMs);
timer.unref?.();
const contenders = [
settle().then((value) => ({ settled: value })),
idlePromise,
];
if (budgetPromise)
contenders.push(budgetPromise);
let outcome;
try {
outcome = await Promise.race(contenders);
}
finally {
clearTimeout(timer);
}
if (outcome === IDLE_TRIP) {
abort();
throw new StreamIdleTimeoutError(idleTimeoutMs);
}
if (outcome === BUDGET_TRIP) {
abort();
throw new StreamDurationBudgetError(budgetMs);
}
return outcome.settled;
};
try {
// A caller that already cancelled still settles its stream - the terminal
// aborted message is what callers return - but under the same watchdogs.
if (signal?.aborted)
return await settleUnderWatchdogs();
let resolvedStream;
if (Symbol.asyncIterator in stream) {
resolvedStream = stream;
}
else {
const streamContenders = [
Promise.resolve(stream),
];
if (callerAbortPromise)
streamContenders.push(callerAbortPromise);
if (budgetPromise)
streamContenders.push(budgetPromise);
const resolution = await Promise.race(streamContenders);
if (resolution === BUDGET_TRIP) {
abort();
throw new StreamDurationBudgetError(budgetMs);
}
if (resolution === CALLER_ABORTED)
return await settleUnderWatchdogs();
resolvedStream = resolution;
}
iterator = resolvedStream[Symbol.asyncIterator]();
while (true) {
const { promise: idlePromise, resolve: resolveIdle } = Promise.withResolvers();
const timer = setTimeout(() => resolveIdle(IDLE_TRIP), idleTimeoutMs);
timer.unref?.();
const contenders = [iterator.next(), idlePromise];
if (callerAbortPromise)
contenders.push(callerAbortPromise);
if (budgetPromise)
contenders.push(budgetPromise);
let result;
try {
result = await Promise.race(contenders);
}
finally {
clearTimeout(timer);
}
if (result === IDLE_TRIP) {
abort();
void iterator?.return?.();
throw new StreamIdleTimeoutError(idleTimeoutMs);
}
if (result === BUDGET_TRIP) {
abort();
void iterator?.return?.();
throw new StreamDurationBudgetError(budgetMs);
}
if (result === CALLER_ABORTED) {
void iterator?.return?.();
return await settleUnderWatchdogs();
}
if (result.done)
return await settleUnderWatchdogs();
onEvent?.(result.value);
}
}
finally {
removeAbortListener?.();
if (budgetTimer !== undefined)
clearTimeout(budgetTimer);
}
}
//# sourceMappingURL=stream-watchdog.js.map