/** * Context compaction for long sessions. * * Pure functions for compaction logic. The session manager handles I/O, * and after compaction the session is reloaded. */ import type { AgentMessage, StreamFn, ThinkingLevel } from "../../../agent-core/index.js"; import type { Model } from "../../../ai/index.js"; import { type SessionEntry } from "../session-manager.js"; import { type FileOperations } from "./utils.js"; import { type ConversationCompactionPolicy, type ManualCompactionPolicy } from "./policy.js"; import { type CompactionTokenSourceBucket } from "./tokens.js"; export { type CompactionTokenSourceBucket, calculateContextTokens, type ContextUsageEstimate, estimateCompactionTokenMetrics, estimateContextTokens, estimateTextTokens, estimateTokens, getLastAssistantUsage, shouldCompact, } from "./tokens.js"; export { type CutPointResult, type FindCutPointOptions, findCutPoint, findTurnStartIndex, } from "./cut-points.js"; export { generateSummary } from "./summarize.js"; export { deduplicateToolCalls } from "./dedupe.js"; /** Details stored in CompactionEntry.details for file tracking */ export interface CompactionDetails { readFiles: string[]; modifiedFiles: string[]; } export type CompactionSkipReason = "non_shrinking"; /** Result from compact() - SessionManager adds uuid/parentUuid when saving */ export interface CompactionResult { summary: string; firstKeptEntryId: string; tokensBefore: number; /** Estimated tokens in the raw span replaced by the compaction summary. */ tokensCompacted?: number; /** Estimated net tokens removed by replacing raw span tokens with summary tokens. */ tokensRemoved?: number; /** Estimated tokens added back as the compaction summary. */ summaryTokens?: number; /** Estimated percentage of compacted raw tokens removed by the summary. */ reductionPercent?: number; /** 1-based compaction ordinal within the current session branch. */ compactionNumber?: number; /** Largest estimated token buckets from the span being compacted. */ largestTokenSources?: CompactionTokenSourceBucket[]; /** True when the compaction was intentionally skipped and no history was pruned. */ skipped?: boolean; /** Machine-readable reason for a skipped compaction. */ skipReason?: CompactionSkipReason; /** Human-readable skipped/failure message for UI and persisted history. */ message?: string; /** Extension-specific data (e.g., ArtifactIndex, version markers for structured compaction) */ details?: T; } export type CompactionSettings = ConversationCompactionPolicy; export declare const DEFAULT_COMPACTION_SETTINGS: CompactionSettings; export declare const MANUAL_COMPACTION_KEEP_RECENT_TOKENS: number; /** * Manual/model-visible compaction is an explicit phase-boundary action. Keep a * smaller recent suffix than automatic threshold compaction so a ~20k-token * default session does not keep the whole eligible branch and compact nothing. */ export declare function getManualCompactionSettings(settings: CompactionSettings, manualPolicy?: ManualCompactionPolicy): CompactionSettings; export interface CompactionPreparation { /** UUID of first entry to keep */ firstKeptEntryId: string; /** Messages that will be summarized and discarded */ messagesToSummarize: AgentMessage[]; /** Messages that will be turned into turn prefix summary (if splitting) */ turnPrefixMessages: AgentMessage[]; /** Whether this is a split turn (cut point in middle of turn) */ isSplitTurn: boolean; tokensBefore: number; /** Summary from previous compaction, for iterative update */ previousSummary?: string; /** File operations extracted from messagesToSummarize */ fileOps: FileOperations; /** Compaction settions from settings.jsonl */ settings: CompactionSettings; /** 1-based compaction ordinal within the current session branch. */ compactionNumber: number; } export interface PrepareCompactionOptions { /** Prefer a caller-provided phase boundary over the usual recent-token suffix. */ phaseBoundary?: boolean; /** * Use a fixed entry id as the cut point. The caller owns the anchor and is * responsible for validating that it still belongs to the current branch. */ anchorEntryId?: string; /** * Protect the most recent user message from being folded into the compaction * summary. Useful for isolated agent loops (subagents) where the latest user * message carries the task/brief and must survive verbatim mid-run. */ pinLatestUserMessage?: boolean; } export declare function prepareCompaction(pathEntries: SessionEntry[], settings: CompactionSettings, options?: PrepareCompactionOptions): CompactionPreparation | undefined; /** * Generate summaries for compaction using prepared data. * Returns CompactionResult - SessionManager adds uuid/parentUuid when saving. * * @param preparation - Pre-calculated preparation from prepareCompaction() * @param customInstructions - Optional custom focus for the summary * @deprecated The active AgentSession compaction flow uses deterministic * DCP-lite (`compactDcpLite`) through `_buildDcpCompactionResult`. Keep this * exported legacy LLM fallback for SDK/harness compatibility only. */ export declare function compact(preparation: CompactionPreparation, model: Model, apiKey: string | undefined, headers?: Record, customInstructions?: string, signal?: AbortSignal, thinkingLevel?: ThinkingLevel, streamFn?: StreamFn): Promise; //# sourceMappingURL=compaction.d.ts.map