/** * Layer 3: Emergency Truncation (Failsafe). * * Last-resort truncation when Layer 2 fails or context is still too large. * Drops the oldest conversation turns purely mechanically (no LLM call). * Preserves structural integrity: tool_use/tool_result pairs are dropped together. * * Triggers at 90% of context window (configurable), or reactively when * the API returns a context overflow error. * * This layer also serves as a mid-loop safety valve: it fires inside * transformContext during the agentic loop when estimated token count * exceeds 90%. Mid-loop truncation does NOT emit onBeforeCompaction * (no observational memory processing mid-loop). * * References: * - compaction-strategy.md (Layer 3: Emergency Truncation) * - phase-5-compaction.md (5.4) */ import type { AgentMessage } from '../context-manager.js'; import type { FailsafeConfig } from '../types.js'; export declare const FAILSAFE_DEFAULTS: FailsafeConfig; /** * Result of an emergency truncation operation. */ export interface FailsafeTruncationResult { /** The truncated conversation history. */ newHistory: AgentMessage[]; /** Number of turns removed. */ turnsRemoved: number; /** Estimated tokens after truncation. */ tokensAfter: number; } /** * Perform emergency truncation on conversation history. * * Drops the oldest turns (preserving structural pairs) until the * estimated token count drops below the threshold, or until only * the preserved tail remains. * * @param history - Conversation history (post-slot region) * @param contextWindow - Total context window size in tokens * @param slotTokens - Estimated tokens used by slots * @param threshold - Usage ratio threshold (default 0.90) * @returns Truncation result with new history and metrics */ export declare function emergencyTruncate(history: AgentMessage[], contextWindow: number, slotTokens: number, threshold?: number): FailsafeTruncationResult; /** * Check if emergency truncation should fire based on token count. */ export declare function shouldTruncate(currentTokens: number, contextWindow: number, threshold?: number): boolean; /** * Check if an error represents a context overflow. * Matches common API error patterns from various providers. */ export declare function isContextOverflow(error: Error): boolean; //# sourceMappingURL=failsafe.d.ts.map