import type { Model, StreamFn, Usage } from "../llm/index.js"; import { type AgentCoreCompletionRuntimeDeps } from "../loop/runtime-deps.js"; import type { AgentMessage, ThinkingLevel } from "../loop/types.js"; import { CompactionError, type Result, type SessionTreeEntry } from "../harness/types.js"; import { type FileOperations } from "./utils.js"; /** File-operation details stored on generated compaction entries. */ export interface CompactionDetails { /** Files read in the compacted history. */ readFiles: string[]; /** Files modified in the compacted history (sorted, for stable display). */ modifiedFiles: string[]; /** * The same modified set ordered most-recently-touched FIRST. Top-N consumers (working-file * attachments) pick from here so an alphabetical sort cannot push the file the model actually * needs out of the window (the LONGRUN-2b miss). Optional: absent on details persisted before * this field existed. */ modifiedFilesByRecency?: string[]; } /** Generated compaction data ready to be persisted as a compaction entry. */ export interface CompactionResult { /** Summary text that replaces compacted history in future context. */ summary: string; /** Entry id where retained history starts. */ firstKeptEntryId: string; /** Estimated context tokens before compaction. */ tokensBefore: number; /** Optional implementation-specific details stored with the compaction entry. */ details?: T; } /** Compaction thresholds and retention settings. */ export interface CompactionSettings { /** Enable automatic compaction decisions. */ enabled: boolean; /** Tokens reserved for summary prompt and output. */ reserveTokens: number; /** Approximate recent-context tokens to keep after compaction. */ keepRecentTokens: number; } /** Default compaction settings used by the harness. */ export declare const DEFAULT_COMPACTION_SETTINGS: CompactionSettings; /** * design/123 D2 — default structural coefficient (chars per token) for every structural token * estimate. 4 = the historical chars/4 heuristic (CC 198's legacy-family value); per-model override * via `Model.charsPerToken` (newer families → 3, CJK-heavy → 2–3). Threaded as an explicit parameter * through the estimate chain so caller-side coordinates never split (estimate vs cut-point vs defense). */ export declare const DEFAULT_CHARS_PER_TOKEN = 4; /** Calculate total context tokens from provider usage. */ export declare function calculateContextTokens(usage: Usage): number; /** * Return usage from the last successful assistant message in session entries. * * design/123 D1 stale-anchor note (fable-m2, same-family point): this scans RAW session entries, so * unlike `buildSessionContext` (which shallow-strips the usage of kept-tail assistants replayed * across a compaction boundary — the anchor "naturally disappears", CC 198 parity) it CAN still see * a pre-compaction anchor. Any consumer using this across a compaction boundary must treat such an * anchor as STALE: it reflects the pre-compaction request and overestimates the post-compaction * context. (Currently exported with no in-repo production consumer.) */ export declare function getLastAssistantUsage(entries: SessionTreeEntry[]): Usage | undefined; /** Estimated context-token usage for a message list. */ export interface ContextUsageEstimate { /** Estimated total context tokens. */ tokens: number; /** Tokens reported by the most recent assistant usage block. */ usageTokens: number; /** Estimated tokens after the most recent assistant usage block. */ trailingTokens: number; /** Index of the message that provided usage, or null when none exists. */ lastUsageIndex: number | null; } /** Estimate context tokens for messages using provider usage when available. * `charsPerToken` (design/123 D2): structural coefficient for the anchor-less fallback and the * trailing (post-anchor) increment — pass `Model.charsPerToken` so the estimate matches the model. */ export declare function estimateContextTokens(messages: AgentMessage[], charsPerToken?: number): ContextUsageEstimate; /** Return whether context usage exceeds the configured compaction threshold. */ export declare function shouldCompact(contextTokens: number, contextWindow: number, settings: CompactionSettings): boolean; /** Estimate token count for one message using a conservative character heuristic. * `charsPerToken` (design/123 D2): per-model structural coefficient (default 4 = byte-compatible * legacy heuristic). Images contribute a FIXED token weight independent of the coefficient. */ export declare function estimateTokens(message: AgentMessage, charsPerToken?: number): number; /** Find the user-visible message that starts the turn containing an entry. */ export declare function findTurnStartIndex(entries: SessionTreeEntry[], entryIndex: number, startIndex: number): number; /** Cut point selected for compaction. */ export interface CutPointResult { /** Index of the first entry retained after compaction. */ firstKeptEntryIndex: number; /** Index of the turn-start entry when the cut splits a turn, otherwise -1. */ turnStartIndex: number; /** Whether the selected cut point splits an in-progress turn. */ isSplitTurn: boolean; } /** Find the compaction cut point that keeps approximately the requested recent-token budget. * `charsPerToken` (design/123 D2): the keep-recent accumulation must live in the SAME structural * coordinate as the trigger estimate, or the kept tail is systematically over/under-sized. */ export declare function findCutPoint(entries: SessionTreeEntry[], startIndex: number, endIndex: number, keepRecentTokens: number, charsPerToken?: number): CutPointResult; export declare const SUMMARIZATION_SYSTEM_PROMPT = "You are a context summarization assistant. Your task is to read a conversation between a user and an AI coding assistant, then produce a structured summary following the exact format specified.\n\nDo NOT continue the conversation. Do NOT respond to any questions in the conversation. ONLY output the structured summary."; /** Fidelity-disclosure payload for a truncated summarization input (bridged by the caller to the * `compaction.input_truncated` trace frame). `keptChars` = surviving conversation chars (marker excluded). */ export interface SummarizationInputTruncation { label: "history" | "turn_prefix"; droppedChars: number; keptChars: number; } /** CC 198 jNl — verbatim marker inserted where the oldest conversation groups were dropped. */ export declare const COMPACTION_PTL_RETRY_MARKER = "[earlier conversation truncated for compaction retry]"; /** Generate or update a conversation summary for compaction. */ export declare function generateSummary(currentMessages: AgentMessage[], model: Model, reserveTokens: number, apiKey: string | undefined, headers?: Record, signal?: AbortSignal, customInstructions?: string, previousSummary?: string, thinkingLevel?: ThinkingLevel, streamFn?: StreamFn, runtime?: AgentCoreCompletionRuntimeDeps, charsPerToken?: number, onInputTruncated?: (info: SummarizationInputTruncation) => void): Promise>; /** Prepared inputs for a compaction run. */ export interface CompactionPreparation { /** Entry id where retained history starts. */ firstKeptEntryId: string; /** Messages summarized into the history summary. */ messagesToSummarize: AgentMessage[]; /** Prefix messages summarized separately when compaction splits a turn. */ turnPrefixMessages: AgentMessage[]; /** Whether compaction splits a turn. */ isSplitTurn: boolean; /** Estimated context tokens before compaction. */ tokensBefore: number; /** Previous compaction summary used for iterative updates. */ previousSummary?: string; /** File operations extracted from summarized history. */ fileOps: FileOperations; /** Settings used to prepare compaction. */ settings: CompactionSettings; } /** Prepare session entries for compaction, or return undefined when compaction is not applicable. * `charsPerToken` (design/123 D2, signature threading): keeps `tokensBefore` and the cut-point * accounting in the same per-model structural coordinate as the caller's trigger estimate. */ export declare function prepareCompaction(pathEntries: SessionTreeEntry[], settings: CompactionSettings, charsPerToken?: number): Result; export { computeFileLists, serializeConversation } from "./utils.js"; /** Generate compaction summary data from prepared session history. * `charsPerToken` (design/123 D2, signature threading): coefficient for the summarization-input * window guard. Defaults to the SUMMARY model's own `charsPerToken` (the estimated input is sent to * THAT model), then 4. */ export declare function compact(preparation: CompactionPreparation, model: Model, apiKey: string | undefined, headers?: Record, customInstructions?: string, signal?: AbortSignal, thinkingLevel?: ThinkingLevel, streamFn?: StreamFn, runtime?: AgentCoreCompletionRuntimeDeps, charsPerToken?: number, onInputTruncated?: (info: SummarizationInputTruncation) => void): Promise>; //# sourceMappingURL=compaction.d.ts.map