@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
133 lines • 6.94 kB
TypeScript
/**
* 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 declare class StreamIdleTimeoutError extends Error {
readonly idleTimeoutMs: number;
constructor(idleTimeoutMs: number);
}
/**
* 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 declare class StreamDurationBudgetError extends Error {
readonly maxDurationMs: number;
constructor(maxDurationMs: number);
}
/** Matches the agent stream idle-timeout default (`httpIdleTimeoutMs`). */
export declare const DEFAULT_SUMMARIZATION_IDLE_TIMEOUT_MS = 300000;
/**
* 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 declare const DEFAULT_SUMMARIZATION_MAX_DURATION_MS = 120000;
/**
* 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 declare 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 declare const SUMMARIZATION_MAX_DURATION_CAP_MS = 1800000;
/**
* 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 declare const SUMMARIZATION_TOTAL_BUDGET_MS = 900000;
/**
* 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 declare function summarizationTotalBudgetMs(attemptOverrideMs?: number): number;
/**
* 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 declare class SummarizationTotalBudgetError extends Error {
readonly totalBudgetMs: number;
constructor(totalBudgetMs: number);
}
export interface SummarizationDeadline {
readonly totalBudgetMs: number;
/** Time left before the whole compaction is out of budget; never negative. */
remainingMs(): number;
/**
* Clamp one attempt's wall-clock budget to what the compaction has left, so a
* retry started near the deadline cannot re-arm a full attempt budget. Throws
* {@link SummarizationTotalBudgetError} once nothing is left.
*/
attemptBudgetMs(requestedMs: number): number;
}
export declare function createSummarizationDeadline(totalBudgetMs: number, now?: () => number): SummarizationDeadline;
/**
* 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 declare function summarizationMaxDurationMs(estimatedInputTokens: number, overrideMs?: number): number;
export interface ConsumeStreamWithIdleTimeoutOptions<T, R = void> {
/** Silence budget per read; the timer resets on every event. */
readonly idleTimeoutMs: number;
/** Total wall-clock budget for the whole stream; omit to leave it unbounded. */
readonly maxDurationMs?: number;
/** Tear down the underlying request (abort the request-local controller). */
readonly abort: () => void;
readonly onEvent?: (event: T) => void;
/** Caller cancellation; an abort here ends the wait without an idle error. */
readonly signal?: AbortSignal;
/**
* Final settlement of the stream (its `result()`), awaited under the SAME
* timers as iteration. A provider whose iterator ends without pushing a
* terminal `done`/`error` event leaves `result()` pending forever; settling it
* after the watchdog's timers were cleared parked compaction with no timer
* armed at all (#1741).
*/
readonly settle?: () => Promise<R>;
}
/**
* Drain an event stream, failing with {@link StreamIdleTimeoutError} when no
* event arrives within `idleTimeoutMs`. Caller aborts propagate as the
* stream's own abort outcome, never masked as an idle timeout.
*/
export declare function consumeStreamWithIdleTimeout<T>(stream: AsyncIterable<T> | PromiseLike<AsyncIterable<T>>, options: ConsumeStreamWithIdleTimeoutOptions<T, void> & {
settle?: undefined;
}): Promise<void>;
export declare function consumeStreamWithIdleTimeout<T, R>(stream: AsyncIterable<T> | PromiseLike<AsyncIterable<T>>, options: ConsumeStreamWithIdleTimeoutOptions<T, R> & {
settle: () => Promise<R>;
}): Promise<R>;
//# sourceMappingURL=stream-watchdog.d.ts.map