@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
61 lines • 3.38 kB
TypeScript
import type { AgentMessage } from "@earendil-works/pi-agent-core";
/**
* Overflow-retry policy for summarization requests (issue #650).
*
* When the provider verdicts a completed summarization response as context
* overflow, retrying with a slightly smaller input is correct — but the
* historical loop removed exactly one history item per FULL billed attempt,
* so a session whose summarization input exceeded the real window wedged on
* "Compacting..." for the length of its history (observed: ~48 minutes and
* ~13.5M billed tokens on gpt-5.6-sol). Every knob below exists to keep the
* worst case small and observable:
*
* - the summarization input is pre-sized before the first attempt;
* - each retry halves the estimated input instead of dropping one item;
* - retries stop at a hard attempt cap and a cumulative wall-clock budget;
* - exhaustion throws a typed error the deterministic fallback can classify.
*/
export declare const MAX_SUMMARIZATION_OVERFLOW_RETRIES = 3;
/**
* Cumulative wall-clock budget across all overflow retries of one compaction
* run. Twice the per-attempt stream budget: a slow provider may consume the
* per-attempt watchdog on each attempt, and the retry loop must still end
* well inside one user's patience.
*/
export declare const SUMMARIZATION_OVERFLOW_TOTAL_BUDGET_MS = 240000;
/** Fraction of the context window the summarization input may occupy. */
export declare const SUMMARIZATION_INPUT_BUDGET_RATIO = 0.6;
export declare class SummarizationOverflowExhaustedError extends Error {
readonly attempts: number;
readonly elapsedMs: number;
constructor(attempts: number, elapsedMs: number);
}
export interface PruneStep {
messages: AgentMessage[];
removedTokens: number;
}
export declare function estimateTotalTokens(messages: AgentMessage[]): number;
export declare function pruneOldMessagesToBudget(messages: AgentMessage[], targetTokens: number): AgentMessage[];
/** Estimated-token budget for the history portion of a summarization request. */
export declare function summarizationHistoryBudget(contextWindow: number, promptTokens: number): number;
/**
* Pre-size the summarization input before the first billed attempt: oldest
* history first, then the last turn's oldest tool steps; when the turn's
* request alone is still over the budget, its text is cut to fit. A fitting
* input passes through untouched.
*/
export declare function boundSummarizationInput(messages: AgentMessage[], contextWindow: number, promptTokens: number): AgentMessage[];
/**
* Shrink for the next overflow retry. Halving the estimated input converges
* in O(log n) retries instead of the historical one-item-per-attempt O(n),
* where each attempt was a full billed summarization stream.
*/
export declare function shrinkSummarizationInputForOverflowRetry(messages: AgentMessage[], contextWindow: number, promptTokens: number): AgentMessage[] | undefined;
/**
* Decide whether another billed overflow retry is allowed. The attempt cap and
* the cumulative budget are both checked BEFORE scheduling a new request: an
* attempt that consumed the whole per-attempt watchdog must not be followed by
* another equally expensive one once the total budget is spent.
*/
export declare function allowOverflowRetry(attempts: number, elapsedMs: number): boolean;
//# sourceMappingURL=overflow-retry.d.ts.map