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,2CAA2C;IAC3C,QAAQ,EAAE,kBAAkB,CAAC;CAC7B;AAED,qGAAqG;AACrG,wBAAgB,iBAAiB,CAChC,WAAW,EAAE,KAAK,EAAE,EACpB,QAAQ,EAAE,kBAAkB,GAC1B,MAAM,CAAC,qBAAqB,GAAG,SAAS,EAAE,eAAe,CAAC,CAsE5D;AAiBD,OAAO,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAEnD,sEAAsE;AACtE,wBAAgB,OAAO,CACtB,WAAW,EAAE,qBAAqB,EAClC,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,EACjB,kBAAkB,EAAE,MAAM,GAAG,SAAS,EACtC,aAAa,EAAE,aAAa,GAAG,SAAS,EACxC,KAAK,EAAE,WAAW,GAAG,SAAS,EAC9B,SAAS,EAAE,cAAc,GAAG,SAAS,EACrC,OAAO,EAAE,OAAO,GACd,OAAO,CAAC,MAAM,CAAC,aAAa,EAAE,eAAe,CAAC,CAAC,CAQjD;AAED,MAAM,WAAW,wBAAwB;IACxC,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC;IAClB,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,aAAa,CAAC,EAAE,aAAa,CAAC;CAC9B;AAED,0FAA0F;AAC1F,wBAAsB,kBAAkB,CACvC,WAAW,EAAE,qBAAqB,EAClC,OAAO,EAAE,wBAAwB,EACjC,OAAO,EAAE,cAAc,EACvB,OAAO,EAAE,OAAO,GACd,OAAO,CAAC,MAAM,CAAC,aAAa,EAAE,eAAe,CAAC,CAAC,CA0DjD","sourcesContent":["import {\n\ttype Context as AiContext,\n\ttype Api,\n\ttype AssistantMessage,\n\tcontentText,\n\ttype Model,\n\ttype Models,\n\ttype RetryCallbacks,\n\ttype RetryPolicy,\n\tretryAssistantCall,\n\ttype SimpleStreamOptions,\n\ttype Usage,\n\tuuidv7,\n} from \"@earendil-works/pi-ai\";\nimport type { AgentMessage, ThinkingLevel } from \"../../types.ts\";\nimport { type Context, getTelemetryContext } from \"../context.ts\";\nimport { convertToLlm, createBranchSummaryMessage, createCompactionSummaryMessage } from \"../messages.ts\";\nimport { buildContextEntries, sessionEntryToContextMessages } from \"../session/context.ts\";\nimport type { CompactionEntry, Entry, JsonValue } from \"../session/types.ts\";\nimport { CompactionError, err, ok, type Result } from \"../types.ts\";\nimport { addUsage } from \"../utils/usage.ts\";\nimport {\n\tcomputeFileLists,\n\tcreateFileOps,\n\textractFileOpsFromMessage,\n\ttype FileOperations,\n\tformatFileOperations,\n\tserializeConversation,\n} from \"./utils.ts\";\n\n/** File-operation details stored on generated compaction entries. */\nexport interface CompactionDetails extends Record<string, JsonValue> {\n\t/** Files read in the compacted history. */\n\treadFiles: string[];\n\t/** Files modified in the compacted history. */\n\tmodifiedFiles: string[];\n}\nfunction safeJsonStringify(value: unknown): string {\n\ttry {\n\t\treturn JSON.stringify(value) ?? \"undefined\";\n\t} catch {\n\t\treturn \"[unserializable]\";\n\t}\n}\n\nfunction extractFileOperations(\n\tmessages: AgentMessage[],\n\tentries: Entry[],\n\tprevCompactionIndex: number,\n): FileOperations {\n\tconst fileOps = createFileOps();\n\tif (prevCompactionIndex >= 0) {\n\t\tconst prevCompaction = entries[prevCompactionIndex] as CompactionEntry;\n\t\tif (\n\t\t\ttypeof prevCompaction.details === \"object\" &&\n\t\t\tprevCompaction.details !== null &&\n\t\t\t!Array.isArray(prevCompaction.details)\n\t\t) {\n\t\t\tif (Array.isArray(prevCompaction.details.readFiles)) {\n\t\t\t\tfor (const path of prevCompaction.details.readFiles) {\n\t\t\t\t\tif (typeof path === \"string\") fileOps.read.add(path);\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (Array.isArray(prevCompaction.details.modifiedFiles)) {\n\t\t\t\tfor (const path of prevCompaction.details.modifiedFiles) {\n\t\t\t\t\tif (typeof path === \"string\") fileOps.edited.add(path);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tfor (const msg of messages) {\n\t\textractFileOpsFromMessage(msg, fileOps);\n\t}\n\n\treturn fileOps;\n}\nfunction getMessageFromEntry(entry: Entry): AgentMessage | undefined {\n\tif (entry.type === \"message\") {\n\t\treturn entry.message as AgentMessage;\n\t}\n\tif (entry.type === \"branch_summary\") {\n\t\treturn createBranchSummaryMessage(entry.summary, entry.fromId, entry.timestamp);\n\t}\n\tif (entry.type === \"compaction\") {\n\t\treturn createCompactionSummaryMessage(entry.summary, entry.tokensBefore, entry.timestamp);\n\t}\n\treturn undefined;\n}\n\nfunction getMessageFromEntryForCompaction(entry: Entry): AgentMessage | undefined {\n\tif (entry.type === \"compaction\") {\n\t\treturn undefined;\n\t}\n\treturn getMessageFromEntry(entry);\n}\n\n/** Generated compaction data ready to be persisted as a compaction entry. */\nexport interface CompactResult<T = JsonValue> {\n\t/** Summary text that replaces compacted history in future context. */\n\tsummary: string;\n\t/** Estimated context tokens before compaction. */\n\ttokensBefore: number;\n\t/** Usage from the LLM call(s) that generated this summary, if available. */\n\tusage?: Usage;\n\t/** Retained recent messages stored directly on the compaction entry. */\n\tretainedTail: AgentMessage[];\n\t/** Optional implementation-specific details stored with the compaction entry. */\n\tdetails?: T;\n}\n\nexport type SummaryRequest = (\n\taiContext: AiContext,\n\toptions: SimpleStreamOptions,\n\tcontext: Context,\n) => Promise<AssistantMessage>;\n\nexport function createSummaryRequestOptions(options: SimpleStreamOptions, context: Context): SimpleStreamOptions {\n\treturn {\n\t\t...options,\n\t\tsignal: context.abortSignal,\n\t\ttelemetryContext: getTelemetryContext(context),\n\t\tcacheRetention: \"none\",\n\t\tsessionId: options.sessionId ?? uuidv7(),\n\t};\n}\n\nexport async function completeSimpleWithRetries(\n\tmodels: Models,\n\tmodel: Model<Api>,\n\taiContext: AiContext,\n\toptions: SimpleStreamOptions,\n\tretry: RetryPolicy | undefined,\n\tcallbacks: RetryCallbacks | undefined,\n\tcontext: Context,\n): Promise<AssistantMessage> {\n\t// Summaries are standalone requests, so isolate routing and avoid cache writes that cannot be reused.\n\tconst requestOptions = createSummaryRequestOptions(options, context);\n\treturn retryAssistantCall(\n\t\t() => models.completeSimple(model, aiContext, requestOptions),\n\t\tretry,\n\t\trequestOptions.signal,\n\t\tcallbacks,\n\t);\n}\n\n/** Compaction thresholds and retention settings. */\nexport interface CompactionSettings {\n\t/** Enable automatic compaction decisions. */\n\tenabled: boolean;\n\t/** Tokens reserved for summary prompt and output. */\n\treserveTokens: number;\n\t/** Approximate recent-context tokens to keep after compaction. */\n\tkeepRecentTokens: number;\n}\n\n/** Default compaction settings used by the harness. */\nexport const DEFAULT_COMPACTION_SETTINGS: CompactionSettings = {\n\tenabled: true,\n\treserveTokens: 16384,\n\tkeepRecentTokens: 20000,\n};\n\n/** Calculate total context tokens from provider usage. */\nexport function calculateContextTokens(usage: Usage): number {\n\treturn usage.totalTokens || usage.input + usage.output + usage.cacheRead + usage.cacheWrite;\n}\nfunction getAssistantUsage(msg: AgentMessage): Usage | undefined {\n\tif (msg.role === \"assistant\" && \"usage\" in msg) {\n\t\tconst assistantMsg = msg as AssistantMessage;\n\t\tif (\n\t\t\tassistantMsg.stopReason !== \"aborted\" &&\n\t\t\tassistantMsg.stopReason !== \"error\" &&\n\t\t\tassistantMsg.usage &&\n\t\t\tcalculateContextTokens(assistantMsg.usage) > 0\n\t\t) {\n\t\t\treturn assistantMsg.usage;\n\t\t}\n\t}\n\treturn undefined;\n}\n\n/** Return usage from the last valid assistant message in session entries. */\nexport function getLastAssistantUsage(entries: Entry[]): Usage | undefined {\n\tfor (let i = entries.length - 1; i >= 0; i--) {\n\t\tconst entry = entries[i];\n\t\tif (entry.type === \"message\") {\n\t\t\tconst usage = getAssistantUsage(entry.message as AgentMessage);\n\t\t\tif (usage) return usage;\n\t\t}\n\t}\n\treturn undefined;\n}\n\n/** Estimated context-token usage for a message list. */\nexport interface ContextUsageEstimate {\n\t/** Estimated total context tokens. */\n\ttokens: number;\n\t/** Tokens reported by the most recent assistant usage block. */\n\tusageTokens: number;\n\t/** Estimated tokens after the most recent assistant usage block. */\n\ttrailingTokens: number;\n\t/** Index of the message that provided usage, or null when none exists. */\n\tlastUsageIndex: number | null;\n}\n\nfunction getLastAssistantUsageInfo(messages: AgentMessage[]): { usage: Usage; index: number } | undefined {\n\tfor (let i = messages.length - 1; i >= 0; i--) {\n\t\tconst usage = getAssistantUsage(messages[i]);\n\t\tif (usage) return { usage, index: i };\n\t}\n\treturn undefined;\n}\n\n/** Estimate context tokens for messages using provider usage when available. */\nexport function estimateContextTokens(messages: AgentMessage[]): ContextUsageEstimate {\n\tconst usageInfo = getLastAssistantUsageInfo(messages);\n\n\tif (!usageInfo) {\n\t\tlet estimated = 0;\n\t\tfor (const message of messages) {\n\t\t\testimated += estimateTokens(message);\n\t\t}\n\t\treturn {\n\t\t\ttokens: estimated,\n\t\t\tusageTokens: 0,\n\t\t\ttrailingTokens: estimated,\n\t\t\tlastUsageIndex: null,\n\t\t};\n\t}\n\n\tconst usageTokens = calculateContextTokens(usageInfo.usage);\n\tlet trailingTokens = 0;\n\tfor (let i = usageInfo.index + 1; i < messages.length; i++) {\n\t\ttrailingTokens += estimateTokens(messages[i]);\n\t}\n\n\treturn {\n\t\ttokens: usageTokens + trailingTokens,\n\t\tusageTokens,\n\t\ttrailingTokens,\n\t\tlastUsageIndex: usageInfo.index,\n\t};\n}\n\n/** Return whether context usage exceeds the configured compaction threshold. */\nexport function shouldCompact(contextTokens: number, contextWindow: number, settings: CompactionSettings): boolean {\n\tif (!settings.enabled) return false;\n\treturn contextTokens > contextWindow - settings.reserveTokens;\n}\n\nconst ESTIMATED_IMAGE_CHARS = 4800;\n\nfunction estimateTextAndImageContentChars(content: string | Array<{ type: string; text?: string }>): number {\n\tif (typeof content === \"string\") {\n\t\treturn content.length;\n\t}\n\n\tlet chars = 0;\n\tfor (const block of content) {\n\t\tif (block.type === \"text\" && block.text) {\n\t\t\tchars += block.text.length;\n\t\t} else if (block.type === \"image\") {\n\t\t\tchars += ESTIMATED_IMAGE_CHARS;\n\t\t}\n\t}\n\treturn chars;\n}\n\n/** Estimate token count for one message using a conservative character heuristic. */\nexport function estimateTokens(message: AgentMessage): number {\n\tlet chars = 0;\n\n\tswitch (message.role) {\n\t\tcase \"user\": {\n\t\t\tchars = estimateTextAndImageContentChars(\n\t\t\t\t(message as { content: string | Array<{ type: string; text?: string }> }).content,\n\t\t\t);\n\t\t\treturn Math.ceil(chars / 4);\n\t\t}\n\t\tcase \"assistant\": {\n\t\t\tconst assistant = message as AssistantMessage;\n\t\t\tfor (const block of assistant.content) {\n\t\t\t\tif (block.type === \"text\") {\n\t\t\t\t\tchars += block.text.length;\n\t\t\t\t} else if (block.type === \"thinking\") {\n\t\t\t\t\tchars += block.thinking.length;\n\t\t\t\t} else if (block.type === \"toolCall\") {\n\t\t\t\t\tchars += block.name.length + safeJsonStringify(block.arguments).length;\n\t\t\t\t}\n\t\t\t}\n\t\t\treturn Math.ceil(chars / 4);\n\t\t}\n\t\tcase \"custom\":\n\t\tcase \"toolResult\": {\n\t\t\tchars = estimateTextAndImageContentChars(message.content);\n\t\t\treturn Math.ceil(chars / 4);\n\t\t}\n\t\tcase \"bashExecution\": {\n\t\t\tchars = message.command.length + message.output.length;\n\t\t\treturn Math.ceil(chars / 4);\n\t\t}\n\t\tcase \"branchSummary\":\n\t\tcase \"compactionSummary\": {\n\t\t\tchars = message.summary.length;\n\t\t\treturn Math.ceil(chars / 4);\n\t\t}\n\t}\n\n\treturn 0;\n}\nfunction findValidCutPoints(entries: Entry[], startIndex: number, endIndex: number): number[] {\n\tconst cutPoints: number[] = [];\n\tfor (let i = startIndex; i < endIndex; i++) {\n\t\tconst entry = entries[i];\n\t\tswitch (entry.type) {\n\t\t\tcase \"message\": {\n\t\t\t\tconst role = entry.message.role;\n\t\t\t\tswitch (role) {\n\t\t\t\t\tcase \"bashExecution\":\n\t\t\t\t\tcase \"custom\":\n\t\t\t\t\tcase \"branchSummary\":\n\t\t\t\t\tcase \"compactionSummary\":\n\t\t\t\t\tcase \"user\":\n\t\t\t\t\tcase \"assistant\":\n\t\t\t\t\t\tcutPoints.push(i);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\tcase \"toolResult\":\n\t\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase \"compaction\":\n\t\t\tcase \"branch_summary\":\n\t\t\tcase \"custom\":\n\t\t\t\tbreak;\n\t\t}\n\t\tif (entry.type === \"branch_summary\") cutPoints.push(i);\n\t}\n\treturn cutPoints;\n}\n\n/** Find the user-visible message that starts the turn containing an entry. */\nexport function findTurnStartIndex(entries: Entry[], entryIndex: number, startIndex: number): number {\n\tfor (let i = entryIndex; i >= startIndex; i--) {\n\t\tconst entry = entries[i];\n\t\tif (entry.type === \"branch_summary\") {\n\t\t\treturn i;\n\t\t}\n\t\tif (entry.type === \"message\") {\n\t\t\tconst role = entry.message.role;\n\t\t\tif (role === \"user\" || role === \"bashExecution\") {\n\t\t\t\treturn i;\n\t\t\t}\n\t\t}\n\t}\n\treturn -1;\n}\n\n/** Cut point selected for compaction. */\nexport interface CutPointResult {\n\t/** Index of the first entry retained after compaction. */\n\tfirstKeptEntryIndex: number;\n\t/** Index of the turn-start entry when the cut splits a turn, otherwise -1. */\n\tturnStartIndex: number;\n\t/** Whether the selected cut point splits an in-progress turn. */\n\tisSplitTurn: boolean;\n}\n\n/** Find the compaction cut point that keeps approximately the requested recent-token budget. */\nexport function findCutPoint(\n\tentries: Entry[],\n\tstartIndex: number,\n\tendIndex: number,\n\tkeepRecentTokens: number,\n): CutPointResult {\n\tconst cutPoints = findValidCutPoints(entries, startIndex, endIndex);\n\n\tif (cutPoints.length === 0) {\n\t\treturn { firstKeptEntryIndex: startIndex, turnStartIndex: -1, isSplitTurn: false };\n\t}\n\tlet accumulatedTokens = 0;\n\tlet cutIndex = cutPoints[0];\n\n\tfor (let i = endIndex - 1; i >= startIndex; i--) {\n\t\tconst entry = entries[i];\n\t\tif (entry.type !== \"message\") continue;\n\t\tconst messageTokens = estimateTokens(entry.message as AgentMessage);\n\t\taccumulatedTokens += messageTokens;\n\t\tif (accumulatedTokens >= keepRecentTokens) {\n\t\t\tfor (let c = 0; c < cutPoints.length; c++) {\n\t\t\t\tif (cutPoints[c] >= i) {\n\t\t\t\t\tcutIndex = cutPoints[c];\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\tbreak;\n\t\t}\n\t}\n\twhile (cutIndex > startIndex) {\n\t\tconst prevEntry = entries[cutIndex - 1];\n\t\tif (prevEntry.type === \"compaction\") {\n\t\t\tbreak;\n\t\t}\n\t\tif (prevEntry.type === \"message\") {\n\t\t\tbreak;\n\t\t}\n\t\tcutIndex--;\n\t}\n\tconst cutEntry = entries[cutIndex];\n\tconst isUserMessage = cutEntry.type === \"message\" && cutEntry.message.role === \"user\";\n\tconst turnStartIndex = isUserMessage ? -1 : findTurnStartIndex(entries, cutIndex, startIndex);\n\n\treturn {\n\t\tfirstKeptEntryIndex: cutIndex,\n\t\tturnStartIndex,\n\t\tisSplitTurn: !isUserMessage && turnStartIndex !== -1,\n\t};\n}\n\nexport const SUMMARIZATION_SYSTEM_PROMPT = `You are a context summarization assistant. Your task is to read a conversation between a user and an AI 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.`;\n\nconst SUMMARIZATION_PROMPT = `The messages above are a conversation to summarize. Create a structured context checkpoint summary that another LLM will use to continue the work.\n\nUse this EXACT format:\n\n## Goal\n[What is the user trying to accomplish? Can be multiple items if the session covers different tasks.]\n\n## Constraints & Preferences\n- [Any constraints, preferences, or requirements mentioned by user]\n- [Or \"(none)\" if none were mentioned]\n\n## Progress\n### Done\n- [x] [Completed tasks/changes]\n\n### In Progress\n- [ ] [Current work]\n\n### Blocked\n- [Issues preventing progress, if any]\n\n## Key Decisions\n- **[Decision]**: [Brief rationale]\n\n## Next Steps\n1. [Ordered list of what should happen next]\n\n## Critical Context\n- [Any data, examples, or references needed to continue]\n- [Or \"(none)\" if not applicable]\n\nKeep each section concise. Preserve exact file paths, function names, and error messages.`;\n\nconst UPDATE_SUMMARIZATION_PROMPT = `The messages above are NEW conversation messages to incorporate into the existing summary provided in <previous-summary> tags.\n\nUpdate the existing structured summary with new information. RULES:\n- PRESERVE all existing information from the previous summary\n- ADD new progress, decisions, and context from the new messages\n- UPDATE the Progress section: move items from \"In Progress\" to \"Done\" when completed\n- UPDATE \"Next Steps\" based on what was accomplished\n- PRESERVE exact file paths, function names, and error messages\n- If something is no longer relevant, you may remove it\n\nUse this EXACT format:\n\n## Goal\n[Preserve existing goals, add new ones if the task expanded]\n\n## Constraints & Preferences\n- [Preserve existing, add new ones discovered]\n\n## Progress\n### Done\n- [x] [Include previously done items AND newly completed items]\n\n### In Progress\n- [ ] [Current work - update based on progress]\n\n### Blocked\n- [Current blockers - remove if resolved]\n\n## Key Decisions\n- **[Decision]**: [Brief rationale] (preserve all previous, add new)\n\n## Next Steps\n1. [Update based on current state]\n\n## Critical Context\n- [Preserve important context, add new if needed]\n\nKeep each section concise. Preserve exact file paths, function names, and error messages.`;\n\n/** Generate or update a conversation summary for compaction. */\nexport async function generateSummary(\n\tcurrentMessages: AgentMessage[],\n\tmodels: Models,\n\tmodel: Model<Api>,\n\treserveTokens: number,\n\tcustomInstructions: string | undefined,\n\tpreviousSummary: string | undefined,\n\tthinkingLevel: ThinkingLevel | undefined,\n\tretry: RetryPolicy | undefined,\n\tcallbacks: RetryCallbacks | undefined,\n\tcontext: Context,\n): Promise<Result<string, CompactionError>> {\n\tconst result = await generateSummaryWithUsage(\n\t\tcurrentMessages,\n\t\tmodels,\n\t\tmodel,\n\t\treserveTokens,\n\t\tcustomInstructions,\n\t\tpreviousSummary,\n\t\tthinkingLevel,\n\t\tretry,\n\t\tcallbacks,\n\t\tcontext,\n\t);\n\treturn result.ok ? ok(result.value.text) : err(result.error);\n}\n\n/** Generate or update a conversation summary and return its provider usage. */\nexport function generateSummaryWithUsage(\n\tcurrentMessages: AgentMessage[],\n\tmodels: Models,\n\tmodel: Model<Api>,\n\treserveTokens: number,\n\tcustomInstructions: string | undefined,\n\tpreviousSummary: string | undefined,\n\tthinkingLevel: ThinkingLevel | undefined,\n\tretry: RetryPolicy | undefined,\n\tcallbacks: RetryCallbacks | undefined,\n\tcontext: Context,\n): Promise<Result<{ text: string; usage: Usage }, CompactionError>> {\n\treturn generateSummaryWithRequest(\n\t\tcurrentMessages,\n\t\t{ model, reserveTokens, customInstructions, previousSummary, thinkingLevel },\n\t\t(aiContext, options, requestContext) =>\n\t\t\tcompleteSimpleWithRetries(models, model, aiContext, options, retry, callbacks, requestContext),\n\t\tcontext,\n\t);\n}\n\nexport interface SummaryGenerationOptions {\n\tmodel: Model<Api>;\n\treserveTokens: number;\n\tcustomInstructions?: string;\n\tpreviousSummary?: string;\n\tthinkingLevel?: ThinkingLevel;\n}\n\n/** Generate one summary through a caller-owned one-request boundary. */\nexport async function generateSummaryWithRequest(\n\tcurrentMessages: AgentMessage[],\n\toptions: SummaryGenerationOptions,\n\trequest: SummaryRequest,\n\tcontext: Context,\n): Promise<Result<{ text: string; usage: Usage }, CompactionError>> {\n\tconst { model, reserveTokens, customInstructions, previousSummary, thinkingLevel } = options;\n\tconst maxTokens = Math.min(\n\t\tMath.floor(0.8 * reserveTokens),\n\t\tmodel.maxTokens > 0 ? model.maxTokens : Number.POSITIVE_INFINITY,\n\t);\n\tlet basePrompt = previousSummary ? UPDATE_SUMMARIZATION_PROMPT : SUMMARIZATION_PROMPT;\n\tif (customInstructions) {\n\t\tbasePrompt = `${basePrompt}\\n\\nAdditional focus: ${customInstructions}`;\n\t}\n\tconst llmMessages = convertToLlm(currentMessages);\n\tconst conversationText = serializeConversation(llmMessages);\n\tlet promptText = `<conversation>\\n${conversationText}\\n</conversation>\\n\\n`;\n\tif (previousSummary) {\n\t\tpromptText += `<previous-summary>\\n${previousSummary}\\n</previous-summary>\\n\\n`;\n\t}\n\tpromptText += basePrompt;\n\n\tconst summarizationMessages = [\n\t\t{\n\t\t\trole: \"user\" as const,\n\t\t\tcontent: [{ type: \"text\" as const, text: promptText }],\n\t\t\ttimestamp: Date.now(),\n\t\t},\n\t];\n\n\tconst completionOptions =\n\t\tmodel.reasoning && thinkingLevel && thinkingLevel !== \"off\"\n\t\t\t? { maxTokens, reasoning: thinkingLevel }\n\t\t\t: { maxTokens };\n\n\tconst response = await request(\n\t\t{ systemPrompt: SUMMARIZATION_SYSTEM_PROMPT, messages: summarizationMessages },\n\t\tcreateSummaryRequestOptions(completionOptions, context),\n\t\tcontext,\n\t);\n\tif (response.stopReason === \"aborted\") {\n\t\treturn err(new CompactionError(\"aborted\", response.errorMessage || \"Summarization aborted\"));\n\t}\n\tif (response.stopReason === \"error\") {\n\t\treturn err(\n\t\t\tnew CompactionError(\n\t\t\t\t\"summarization_failed\",\n\t\t\t\t`Summarization failed: ${response.errorMessage || \"Unknown error\"}`,\n\t\t\t),\n\t\t);\n\t}\n\n\tconst textContent = contentText(response.content);\n\n\treturn ok({ text: textContent, usage: response.usage });\n}\n\n/** Prepared inputs for a compaction run. */\nexport interface CompactionPreparation {\n\t/** Messages summarized into the history summary. */\n\tmessagesToSummarize: AgentMessage[];\n\t/** Prefix messages summarized separately when compaction splits a turn. */\n\tturnPrefixMessages: AgentMessage[];\n\t/** Recent messages retained after compaction and stored on the compaction entry. */\n\tretainedTail: AgentMessage[];\n\t/** Whether compaction splits a turn. */\n\tisSplitTurn: boolean;\n\t/** Estimated context tokens before compaction. */\n\ttokensBefore: number;\n\t/** Previous compaction summary used for iterative updates. */\n\tpreviousSummary?: string;\n\t/** File operations extracted from summarized history. */\n\tfileOps: FileOperations;\n\t/** Settings used to prepare compaction. */\n\tsettings: CompactionSettings;\n}\n\n/** Prepare session entries for compaction, or return undefined when compaction is not applicable. */\nexport function prepareCompaction(\n\tpathEntries: Entry[],\n\tsettings: CompactionSettings,\n): Result<CompactionPreparation | undefined, CompactionError> {\n\tif (pathEntries.length === 0 || pathEntries[pathEntries.length - 1].type === \"compaction\") {\n\t\treturn ok(undefined);\n\t}\n\n\tlet prevCompactionIndex = -1;\n\tfor (let i = pathEntries.length - 1; i >= 0; i--) {\n\t\tif (pathEntries[i].type === \"compaction\") {\n\t\t\tprevCompactionIndex = i;\n\t\t\tbreak;\n\t\t}\n\t}\n\n\tlet previousSummary: string | undefined;\n\tlet compactableEntries = pathEntries;\n\tif (prevCompactionIndex >= 0) {\n\t\tconst prevCompaction = pathEntries[prevCompactionIndex] as CompactionEntry;\n\t\tpreviousSummary = prevCompaction.summary;\n\t\tconst virtualRetainedEntries: Entry[] = prevCompaction.retainedTail.map((message, index) => ({\n\t\t\ttype: \"message\",\n\t\t\tid: `${prevCompaction.id}:retained:${index}`,\n\t\t\tparentId: index === 0 ? prevCompaction.id : `${prevCompaction.id}:retained:${index - 1}`,\n\t\t\tseq: prevCompaction.seq,\n\t\t\ttimestamp: message.timestamp,\n\t\t\tmessage,\n\t\t}));\n\t\tcompactableEntries = [...virtualRetainedEntries, ...pathEntries.slice(prevCompactionIndex + 1)];\n\t}\n\tconst boundaryEnd = compactableEntries.length;\n\n\tconst tokensBefore = estimateContextTokens(\n\t\tbuildContextEntries(pathEntries).flatMap(sessionEntryToContextMessages),\n\t).tokens;\n\n\tconst cutPoint = findCutPoint(compactableEntries, 0, boundaryEnd, settings.keepRecentTokens);\n\tconst historyEnd = cutPoint.isSplitTurn ? cutPoint.turnStartIndex : cutPoint.firstKeptEntryIndex;\n\tconst messagesToSummarize: AgentMessage[] = [];\n\tfor (let i = 0; i < historyEnd; i++) {\n\t\tconst msg = getMessageFromEntryForCompaction(compactableEntries[i]);\n\t\tif (msg) messagesToSummarize.push(msg);\n\t}\n\tconst turnPrefixMessages: AgentMessage[] = [];\n\tif (cutPoint.isSplitTurn) {\n\t\tfor (let i = cutPoint.turnStartIndex; i < cutPoint.firstKeptEntryIndex; i++) {\n\t\t\tconst msg = getMessageFromEntryForCompaction(compactableEntries[i]);\n\t\t\tif (msg) turnPrefixMessages.push(msg);\n\t\t}\n\t}\n\tconst retainedTail: AgentMessage[] = [];\n\tfor (let i = cutPoint.firstKeptEntryIndex; i < boundaryEnd; i++) {\n\t\tconst msg = getMessageFromEntryForCompaction(compactableEntries[i]);\n\t\tif (msg) retainedTail.push(msg);\n\t}\n\tconst fileOps = extractFileOperations(messagesToSummarize, pathEntries, prevCompactionIndex);\n\tif (cutPoint.isSplitTurn) {\n\t\tfor (const msg of turnPrefixMessages) {\n\t\t\textractFileOpsFromMessage(msg, fileOps);\n\t\t}\n\t}\n\n\treturn ok({\n\t\tmessagesToSummarize,\n\t\tturnPrefixMessages,\n\t\tretainedTail,\n\t\tisSplitTurn: cutPoint.isSplitTurn,\n\t\ttokensBefore,\n\t\tpreviousSummary,\n\t\tfileOps,\n\t\tsettings,\n\t});\n}\n\nconst TURN_PREFIX_SUMMARIZATION_PROMPT = `This is the PREFIX of a turn that was too large to keep. The SUFFIX (recent work) is retained.\n\nSummarize the prefix to provide context for the retained suffix:\n\n## Original Request\n[What did the user ask for in this turn?]\n\n## Early Progress\n- [Key decisions and work done in the prefix]\n\n## Context for Suffix\n- [Information needed to understand the retained recent work]\n\nBe concise. Focus on what's needed to understand the kept suffix.`;\n\nexport { serializeConversation } from \"./utils.ts\";\n\n/** Generate compaction summary data from prepared session history. */\nexport function compact(\n\tpreparation: CompactionPreparation,\n\tmodels: Models,\n\tmodel: Model<Api>,\n\tcustomInstructions: string | undefined,\n\tthinkingLevel: ThinkingLevel | undefined,\n\tretry: RetryPolicy | undefined,\n\tcallbacks: RetryCallbacks | undefined,\n\tcontext: Context,\n): Promise<Result<CompactResult, CompactionError>> {\n\treturn compactWithRequest(\n\t\tpreparation,\n\t\t{ model, customInstructions, thinkingLevel },\n\t\t(aiContext, options, requestContext) =>\n\t\t\tcompleteSimpleWithRetries(models, model, aiContext, options, retry, callbacks, requestContext),\n\t\tcontext,\n\t);\n}\n\nexport interface CompactGenerationOptions {\n\tmodel: Model<Api>;\n\tcustomInstructions?: string;\n\tthinkingLevel?: ThinkingLevel;\n}\n\n/** Generate compaction data through a caller-owned boundary for each provider request. */\nexport async function compactWithRequest(\n\tpreparation: CompactionPreparation,\n\toptions: CompactGenerationOptions,\n\trequest: SummaryRequest,\n\tcontext: Context,\n): Promise<Result<CompactResult, CompactionError>> {\n\tconst { model, customInstructions, thinkingLevel } = options;\n\tconst {\n\t\tmessagesToSummarize,\n\t\tturnPrefixMessages,\n\t\tretainedTail,\n\t\tisSplitTurn,\n\t\ttokensBefore,\n\t\tpreviousSummary,\n\t\tfileOps,\n\t\tsettings,\n\t} = preparation;\n\n\tlet summary: string;\n\tlet summaryUsage: Usage;\n\n\tif (isSplitTurn && turnPrefixMessages.length > 0) {\n\t\tlet historyText = \"No prior history.\";\n\t\tlet historyUsage: Usage | undefined;\n\t\tif (messagesToSummarize.length > 0) {\n\t\t\tconst historyResult = await generateSummaryWithRequest(\n\t\t\t\tmessagesToSummarize,\n\t\t\t\t{ model, reserveTokens: settings.reserveTokens, customInstructions, previousSummary, thinkingLevel },\n\t\t\t\trequest,\n\t\t\t\tcontext,\n\t\t\t);\n\t\t\tif (!historyResult.ok) return err(historyResult.error);\n\t\t\thistoryText = historyResult.value.text;\n\t\t\thistoryUsage = historyResult.value.usage;\n\t\t}\n\t\tconst turnPrefixResult = await generateTurnPrefixSummary(\n\t\t\tturnPrefixMessages,\n\t\t\tmodel,\n\t\t\tsettings.reserveTokens,\n\t\t\tthinkingLevel,\n\t\t\trequest,\n\t\t\tcontext,\n\t\t);\n\t\tif (!turnPrefixResult.ok) return err(turnPrefixResult.error);\n\t\tsummary = `${historyText}\\n\\n---\\n\\n**Turn Context (split turn):**\\n\\n${turnPrefixResult.value.text}`;\n\t\tsummaryUsage = historyUsage ? addUsage(historyUsage, turnPrefixResult.value.usage) : turnPrefixResult.value.usage;\n\t} else {\n\t\tconst summaryResult = await generateSummaryWithRequest(\n\t\t\tmessagesToSummarize,\n\t\t\t{ model, reserveTokens: settings.reserveTokens, customInstructions, previousSummary, thinkingLevel },\n\t\t\trequest,\n\t\t\tcontext,\n\t\t);\n\t\tif (!summaryResult.ok) return err(summaryResult.error);\n\t\tsummary = summaryResult.value.text;\n\t\tsummaryUsage = summaryResult.value.usage;\n\t}\n\n\tconst { readFiles, modifiedFiles } = computeFileLists(fileOps);\n\tsummary += formatFileOperations(readFiles, modifiedFiles);\n\tconst details: CompactionDetails = { readFiles, modifiedFiles };\n\n\treturn ok({ summary, tokensBefore, usage: summaryUsage, retainedTail, details });\n}\nasync function generateTurnPrefixSummary(\n\tmessages: AgentMessage[],\n\tmodel: Model<Api>,\n\treserveTokens: number,\n\tthinkingLevel: ThinkingLevel | undefined,\n\trequest: SummaryRequest,\n\tcontext: Context,\n): Promise<Result<{ text: string; usage: Usage }, CompactionError>> {\n\tconst maxTokens = Math.min(\n\t\tMath.floor(0.5 * reserveTokens),\n\t\tmodel.maxTokens > 0 ? model.maxTokens : Number.POSITIVE_INFINITY,\n\t);\n\tconst llmMessages = convertToLlm(messages);\n\tconst conversationText = serializeConversation(llmMessages);\n\tconst promptText = `<conversation>\\n${conversationText}\\n</conversation>\\n\\n${TURN_PREFIX_SUMMARIZATION_PROMPT}`;\n\tconst summarizationMessages = [\n\t\t{\n\t\t\trole: \"user\" as const,\n\t\t\tcontent: [{ type: \"text\" as const, text: promptText }],\n\t\t\ttimestamp: Date.now(),\n\t\t},\n\t];\n\n\tconst completionOptions =\n\t\tmodel.reasoning && thinkingLevel && thinkingLevel !== \"off\"\n\t\t\t? { maxTokens, reasoning: thinkingLevel }\n\t\t\t: { maxTokens };\n\tconst response = await request(\n\t\t{ systemPrompt: SUMMARIZATION_SYSTEM_PROMPT, messages: summarizationMessages },\n\t\tcreateSummaryRequestOptions(completionOptions, context),\n\t\tcontext,\n\t);\n\tif (response.stopReason === \"aborted\") {\n\t\treturn err(new CompactionError(\"aborted\", response.errorMessage || \"Turn prefix summarization aborted\"));\n\t}\n\tif (response.stopReason === \"error\") {\n\t\treturn err(\n\t\t\tnew CompactionError(\n\t\t\t\t\"summarization_failed\",\n\t\t\t\t`Turn prefix summarization failed: ${response.errorMessage || \"Unknown error\"}`,\n\t\t\t),\n\t\t);\n\t}\n\n\treturn ok({\n\t\ttext: contentText(response.content),\n\t\tusage: response.usage,\n\t});\n}\n"]}