import type { Agent } from "@caupulican/pi-agent-core/agent"; import { type CompactionResult, type CompactionSettings } from "@caupulican/pi-agent-core/compaction/compaction"; import { type SessionManager } from "@caupulican/pi-agent-core/session"; import type { AgentMessage, ThinkingLevel } from "@caupulican/pi-agent-core/types"; import type { Api, AssistantMessage, Model } from "@caupulican/pi-ai"; import type { ExtensionRunner } from "./extensions/index.ts"; import type { FailureCorpusRecorder } from "./failure-corpus.ts"; import type { SettingsManager } from "./settings-manager.ts"; export type AutoCompactionReason = "overflow" | "provider_recovery" | "threshold"; export interface ProviderRequestCompactionInput { requestTokens: number; nonCompactableTokens: number; attempt: number; } export type ProviderRequestCompactionDecision = { action: "send"; } | { action: "replan"; }; interface AutoCompactionRunOptions { initialTokens?: number; singlePass?: boolean; allowTrailingCompactionAsPrevious?: boolean; forceDeterministic?: boolean; recordThresholdFrontier?: boolean; } export declare class ProviderRequestEnvelopeOverflowError extends Error { constructor(message: string); } type CompactionControllerEvent = { type: "compaction_start"; reason: "manual" | AutoCompactionReason; } | { type: "compaction_end"; reason: "manual" | AutoCompactionReason; result: CompactionResult | undefined; aborted: boolean; willRetry: boolean; errorMessage?: string; skipReason?: string; } | { type: "warning"; message: string; }; export interface CompactionControllerDeps { agent: Agent; sessionManager: SessionManager; settingsManager: SettingsManager; getModel(): Model | undefined; getAdaptedSettings(): CompactionSettings; getRequestAuth(model: Model): Promise<{ apiKey?: string; headers?: Record; }>; resolveModelAndAuth(compactionModel: Model, sessionModel: Model): Promise<{ model: Model; apiKey?: string; headers?: Record; failure?: string; }>; resolveModel(sessionModel: Model): Model; getSelectionReason(): string | undefined; resolveThinkingLevel(compactionModel: Model, sessionModel: Model): ThinkingLevel | undefined; describeSummarizer(): string; getExtensionRunner(): ExtensionRunner; isRawStream(): boolean; disconnectAgent(): void; reconnectAgent(): void; abortForeground(): Promise; emit(event: CompactionControllerEvent): void; estimateCurrentContextTokens(messages: AgentMessage[]): number; buildPreDigest(): ((text: string, signal?: AbortSignal) => Promise) | undefined; getMemoryPreCompressInsight(): Promise; refreshAfterCompaction(): void; getFailureCorpus(): FailureCorpusRecorder; measureLiveContextTokens(): number; runAutoCompaction(reason: AutoCompactionReason, willRetry: boolean): Promise; compactWithRetry(run: () => Promise, signal: AbortSignal, provider?: string): Promise; onCompactionSettled?(): void; } export declare function runCompactionWithRetry(options: { run(): Promise; signal: AbortSignal; provider?: string; getRetrySettings(): { enabled: boolean; maxRetries: number; baseDelayMs: number; }; recordFailure(record: Parameters[0]): void; }): Promise; /** Owns compaction detection, execution, retry, persistence, notification, and cancellation. */ export declare class CompactionController { private manualAbortController; private autoAbortController; private autoRunPromise; private activeCompactionLifecycle; private overflowRecoveryAttempted; private providerRecoveryAttempted; private ineffectiveThresholdFrontier; private readonly deps; constructor(deps: CompactionControllerDeps); private buildCompactionInstructions; isRunning(): boolean; private buildExecutionOptions; /** * Open the durable compaction transaction only after deterministic preparation has produced its cut. * The caller must invoke this before extension or provider work begins. A failed append deliberately * leaves no active lifecycle, so the caller fails closed before starting summarization. */ private beginCompactionLifecycle; private recordAppliedCompaction; /** * Lifecycle records are durable bookkeeping and must not become compaction input or alter the * loop's trailing-compaction guard. They remain in the real branch so recovery can inspect them. * * A plain filter breaks ancestry because retained children still point at removed lifecycle * parents. Reconnect the already-linear active branch while preserving every real entry id. */ private getCompactionBranch; /** * Close the one transaction for this compaction retry ladder. The marker is attempted at most once; * an append failure is allowed to propagate because success must never be inferred from an unrecorded * terminal. A successful terminal always points at the canonical persisted compaction entry. */ private finishCompactionLifecycle; resetOverflowRecovery(): void; admitProviderRequest(input: ProviderRequestCompactionInput): Promise; abort(): void; checkContextWindowUsageWarning(): void; compact(customInstructions?: string): Promise; private runManualCompaction; check(assistantMessage: AssistantMessage, skipAbortedCheck?: boolean): Promise; measureLiveContextTokens(): number; /** Prefer authoritative branch order; timestamps are only a fallback for reconstructed messages. */ private isAssistantFromBeforeCompaction; get isCompacting(): boolean; runAuto(reason: AutoCompactionReason, willRetry: boolean, options?: AutoCompactionRunOptions): Promise; private runAutoOnce; private shouldDeferThresholdRetry; private recordThresholdFrontier; private emitIneffectiveThresholdSkip; compactWithRetry(run: () => Promise, signal: AbortSignal, provider?: string): Promise; private findLastAssistantMessage; private dropTrailingAssistantErrors; private appendProviderRecoveryContinuation; private getExtensionCompaction; private applyResult; } export {}; //# sourceMappingURL=compaction-controller.d.ts.map