diff --git a/docs/configuration.md b/docs/configuration.md index 3acd5561..60921e18 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -28,7 +28,7 @@ Deep Code 使用 `settings.json` 设置文件进行持久化配置,支持两 | -------------------- | --------- | ------------------------------------------------------------------- | | `env` | object | 环境变量分组(见下方子字段表) | | `contextWindow` | number/string | 上下文窗口上限,可使用精确 token 数或 `128K`、`1M` 等格式 | -| `autoCompactWindow` | number/string | 自动压缩阈值,默认取最终上下文窗口的 50% | +| `autoCompactWindow` | number/string | 自动压缩阈值,默认取上下文窗口的 80% | | `model` | string | 模型名称。优先级高于 `env.MODEL` | | `thinkingEnabled` | boolean | 是否启用思考模式(DeepSeek V4 系列默认启用) | | `reasoningEffort` | string | 推理强度,可选 `"low"`、`"high"` 或 `"max"`(默认 `"max"`) | diff --git a/docs/configuration_en.md b/docs/configuration_en.md index be286bcc..ae2da888 100644 --- a/docs/configuration_en.md +++ b/docs/configuration_en.md @@ -28,7 +28,7 @@ The following are all the top-level fields supported in `settings.json`, along w | ------------------ | ------- | --------------------------------------------------------------------------- | | `env` | object | Group of environment variables (see sub-field table below) | | `contextWindow` | number/string | Context-window limit as an exact token count or `128K`/`1M` value | -| `autoCompactWindow` | number/string | Auto-compaction threshold; defaults to 50% of the final context window | +| `autoCompactWindow` | number/string | Auto-compaction threshold; defaults to 80% of the context window | | `model` | string | Model name. Takes precedence over `env.MODEL` | | `thinkingEnabled` | boolean | Whether to enable thinking mode (enabled by default for DeepSeek V4 series)| | `reasoningEffort` | string | Reasoning intensity: `"low"`, `"high"`, or `"max"` (default `"max"`) | diff --git a/packages/core/src/common/compaction.ts b/packages/core/src/common/compaction.ts new file mode 100644 index 00000000..d688ddb6 --- /dev/null +++ b/packages/core/src/common/compaction.ts @@ -0,0 +1,145 @@ +import type { ChatCompletionMessageParam } from "openai/resources/chat/completions"; +import type { SessionMessage } from "../session"; + +export type CompactBudget = { + output: number; + input: number; + threshold: number; + retain: number; +}; + +export function compactBudget(window: number, configuredThreshold = Math.floor(window * 0.8)): CompactBudget { + const output = Math.max(1, Math.min(65536, Math.floor(window / 8))); + const input = window - output; + const threshold = Math.min(configuredThreshold, input - output); + if (threshold <= 0) throw new Error("The context window leaves no room for conversation input."); + return { output, input, threshold, retain: Math.min(Math.floor(input * 0.16), Math.floor(threshold * 0.8)) }; +} + +/** An estimate, not a tokenizer. Never charge binary image encodings as text. */ +export function estimateRequestTokens(messages: readonly ChatCompletionMessageParam[], tools: unknown = []): number { + return messages.reduce((sum, message) => sum + 4 + estimateValue(message), estimateValue(tools)); +} + +function estimateValue(value: unknown): number { + if (typeof value === "string") { + let tokens = 0; + for (const char of value) tokens += /[\u3400-\u9fff\uf900-\ufaff]/u.test(char) ? 0.6 : 0.3; + return Math.ceil(tokens); + } + if (Array.isArray(value)) return value.reduce((sum, item) => sum + estimateValue(item), 0); + if (value && typeof value === "object") { + const record = value as Record; + if (record.type === "image_url" || record.type === "file") { + // Unknown model/image geometry: allow a conservative multi-tile visual budget. + // Files here are uploaded images; their short file IDs do not reflect their cost. + return (record.image_url as { detail?: string } | undefined)?.detail === "low" ? 85 : 4096; + } + return Object.entries(record).reduce((sum, [key, item]) => sum + estimateValue(key) + estimateValue(item), 0); + } + return value == null ? 0 : 1; +} + +export function isFixedContext(message: SessionMessage): boolean { + return Boolean(message.meta?.skillCatalog) || (message.role === "system" && !message.meta?.isSummary); +} + +/** Eligible prefixes, longest first, with tool calls/results kept on the same side. */ +export function compactPrefixes( + messages: SessionMessage[], + prices: number[], + retain: number +): { + history: SessionMessage[]; + ends: number[]; +} { + const history = messages.filter((message) => !isFixedContext(message)); + if (history.length < 2) return { history, ends: [] }; + const priceById = new Map(messages.map((message, index) => [message.id, prices[index] ?? 0])); + let keepFrom = history.length - 1; + let tail = priceById.get(history[keepFrom].id) ?? 0; + while (keepFrom > 0 && tail < retain) tail += priceById.get(history[--keepFrom].id) ?? 0; + + const calls = new Map(); + const edges: Array<[number, number]> = []; + const pending = new Set(); + const orphanResults = new Set(); + history.forEach((message, index) => { + // Once the conversation resumes, missing earlier results are replayed as interrupted. + if (message.role === "user" || message.role === "assistant") pending.clear(); + const params = message.messageParams as { tool_calls?: Array<{ id?: string }>; tool_call_id?: string } | null; + if (message.role === "assistant") { + for (const call of params?.tool_calls ?? []) { + if (!call.id) continue; + calls.set(call.id, [...(calls.get(call.id) ?? []), index]); + pending.add(index); + } + } + if (message.role === "tool") { + const candidates = calls.get(params?.tool_call_id ?? ""); + const from = candidates?.at(-1); + if (from === undefined) orphanResults.add(index); + else edges.push([from, index]); + } + }); + // Without a later conversation message, the trailing batch needs all of its results. + for (const index of pending) { + const params = history[index].messageParams as { tool_calls: Array<{ id?: string }> }; + if ( + params.tool_calls.every((call) => + history + .slice(index + 1) + .some( + (message) => + message.role === "tool" && + (message.messageParams as { tool_call_id?: string } | null)?.tool_call_id === call.id + ) + ) + ) + pending.delete(index); + } + const lastSummary = history.reduce((last, message, index) => (message.meta?.isSummary ? index : last), -1); + const ends: number[] = []; + for (let end = keepFrom; end > 0; end--) { + if (end <= lastSummary || edges.some(([from, to]) => from < end && to >= end)) continue; + if ([...pending, ...orphanResults].some((index) => index < end)) continue; + ends.push(end); + } + return { history, ends }; +} + +function errorRecord(error: unknown): Record { + return error && typeof error === "object" ? (error as Record) : {}; +} + +export function isContextOverflow(error: unknown): boolean { + const outer = errorRecord(error); + const inner = errorRecord(outer.error); + const code = String(inner.code ?? outer.code ?? ""); + const message = String(inner.message ?? outer.message ?? ""); + if (outer.status != null && outer.status !== 400 && outer.status !== 413 && outer.status !== 422) return false; + return ( + /^(context_length_exceeded|context_window_exceeded|max_context_length|CONTEXT_OVERFLOW)$/i.test(code) || + /(?:maximum context length|context (?:window|length).{0,40}(?:exceed|too (?:long|large))|(?:exceed|too many).{0,40}(?:context|input tokens))/i.test( + message + ) + ); +} + +/** Only explicit image capacity failures authorize lossy image projection. */ +export function imageOffloadCount(error: unknown): number { + const outer = errorRecord(error); + const inner = errorRecord(outer.error); + if (outer.status != null && outer.status !== 400 && outer.status !== 413 && outer.status !== 422) return 0; + const code = String(inner.code ?? outer.code ?? ""); + const message = String(inner.message ?? outer.message ?? ""); + if ( + !/^(IMAGE_OFFLOAD_REQUIRED|too_many_images|image_limit_exceeded|image_token_limit_exceeded)$/i.test(code) && + !/(?:too many images|images?.{0,80}(?:exceed.{0,40}(?:limit|budget)|(?:token|count|number).{0,30}(?:limit|exceed)))/i.test( + message + ) + ) + return 0; + const count = inner.offloadImages ?? outer.offloadImages; + return typeof count === "number" && Number.isInteger(count) && count > 0 ? count : 1; +} diff --git a/packages/core/src/prompt.ts b/packages/core/src/prompt.ts index af0b5e06..f7f147d3 100644 --- a/packages/core/src/prompt.ts +++ b/packages/core/src/prompt.ts @@ -5,91 +5,59 @@ import * as path from "path"; import ejs from "ejs"; import matter from "gray-matter"; import { fileURLToPath } from "url"; -import type { SessionMessage } from "./session"; import { findGitBashPath, resolveShellPath } from "./common/shell-utils"; import { supportsMultimodal, type MultimodalMode } from "./common/model-capabilities"; -const COMPACT_PROMPT_BASE = `Your task is to create a detailed summary of the conversation so far, paying close attention to the user's explicit requests and your previous actions. -This summary should be thorough in capturing technical details, code patterns, and architectural decisions that would be essential for continuing development work without losing context. - -Before providing your final summary, wrap your analysis in tags to organize your thoughts and ensure you've covered all necessary points. In your analysis process: - -1. Chronologically analyze each message and section of the conversation. For each section thoroughly identify: - - The user's explicit requests and intents - - Your approach to addressing the user's requests - - Key decisions, technical concepts and code patterns - - Specific details like: - - file names - - full code snippets - - function signatures - - file edits - - Errors that you ran into and how you fixed them - - Pay special attention to specific user feedback that you received, especially if the user told you to do something differently. -2. Double-check for technical accuracy and completeness, addressing each required element thoroughly. - -Your summary should include the following sections: - -1. Primary Request and Intent: Capture all of the user's explicit requests and intents in detail -2. Key Technical Concepts: List all important technical concepts, technologies, and frameworks discussed. -3. Files and Code Sections: Enumerate specific files and code sections examined, modified, or created. Pay special attention to the most recent messages and include full code snippets where applicable and include a summary of why this file read or edit is important. -4. Errors and fixes: List all errors that you ran into, and how you fixed them. Pay special attention to specific user feedback that you received, especially if the user told you to do something differently. -5. Problem Solving: Document problems solved and any ongoing troubleshooting efforts. -6. All user messages: List ALL user messages that are not tool results. These are critical for understanding the users' feedback and changing intent. -6. Pending Tasks: Outline any pending tasks that you have explicitly been asked to work on. -7. Current Work: Describe in detail precisely what was being worked on immediately before this summary request, paying special attention to the most recent messages from both user and assistant. Include file names and code snippets where applicable. -8. Optional Next Step: List the next step that you will take that is related to the most recent work you were doing. IMPORTANT: ensure that this step is DIRECTLY in line with the user's most recent explicit requests, and the task you were working on immediately before this summary request. If your last task was concluded, then only list next steps if they are explicitly in line with the users request. Do not start on tangential requests or really old requests that were already completed without confirming with the user first. - If there is a next step, include direct quotes from the most recent conversation showing exactly what task you were working on and where you left off. This should be verbatim to ensure there's no drift in task interpretation. - -Here's an example of how your output should be structured: - - - -[Your thought process, ensuring all points are covered thoroughly and accurately] - - - -1. Primary Request and Intent: - [Detailed description] - -2. Key Technical Concepts: - - [Concept 1] - - [Concept 2] - - [...] - -3. Files and Code Sections: - - [File Name 1] - - [Summary of why this file is important] - - [Summary of the changes made to this file, if any] - - [Important Code Snippet] - - [File Name 2] - - [Important Code Snippet] - - [...] - -4. Errors and fixes: - - [Detailed description of error 1]: - - [How you fixed the error] - - [User feedback on the error if any] - - [...] - -5. Problem Solving: - [Description of solved problems and ongoing troubleshooting] - -6. All user messages: - - [Detailed non tool use user message] - - [...] - -7. Pending Tasks: - - [Task 1] - - [Task 2] - - [...] - -8. Current Work: - [Precise description of current work] - -9. Optional Next Step: - [Optional Next step to take] - -`; +const SUMMARY_OPEN_TAG = ""; +const SUMMARY_CLOSE_TAG = ""; + +/** + * The summarization directive, delivered as the FINAL user message after the + * replayed conversation rather than as a distinct summarizer system prompt. + * Keeping the conversation's own system prompt, tools, and message prefix in + * front of it makes the auxiliary call a genuine prefix of the last routed + * request, so the provider's KV cache is reused instead of invalidated. + */ +export const COMPACTION_INSTRUCTION = [ + "You are now acting as a compaction engine for this AI coding assistant. Condense the conversation ABOVE into a structured checkpoint that lets another model resume the work with no loss of essential context.", + "", + 'Output EXACTLY the Markdown structure below: keep every section, in order. Use terse bullets, not prose paragraphs. Write "(none)" for an empty section — never drop a section.', + "", + "## Primary Request and Intent", + "- [the user's original and evolving goals; quote verbatim where the exact wording matters]", + "", + "## Key Technical Concepts", + "- [technologies, frameworks, patterns, and conventions in play]", + "", + "## Files and Code", + "- [exact path: why it matters, key changes or snippets]", + "", + "## Errors and Fixes", + "- [error: how it was resolved, plus any related user feedback]", + "", + "## Pending Jobs", + "- [explicitly requested work not yet completed]", + "", + "## Current Work", + "- [precisely what was in progress at this checkpoint]", + "", + "## Next Step", + '- [the single next action, directly in line with the most recent request, or "(none)"]', + "", + "## Critical Context", + "- [decisions and their rationale, constraints, user preferences, open questions, data needed to continue]", + "", + "Rules:", + "- Write concise English engineering prose. Preserve exact file paths, commands, error strings, identifiers, numeric values, function signatures, and syntax fragments.", + "- Capture user feedback and explicit instructions faithfully, especially corrections.", + "- Do NOT mention this summarization request or that the context was compacted.", + "- Output only the checkpoint text: do not call any tool or take any other action.", + `- If the conversation already contains a ${SUMMARY_OPEN_TAG} block, it is a PRIOR checkpoint. Do not copy it forward verbatim: preserve still-true facts, drop stale ones, and merge newer information into a single consolidated summary under the same structure.`, +].join("\n"); + +/** Framing that makes the replacement user message established context. */ +export const CHECKPOINT_PREAMBLE = + "This is an automatically generated checkpoint condensing an earlier span of the conversation to free up context. Treat the captured context as established background and build on it without restating it. Continue the task directly from the messages that follow, without acknowledging this checkpoint."; const SYSTEM_PROMPT_BASE = `You are a helpful software engineer assistant.`; @@ -293,20 +261,12 @@ export function getSystemPrompt(_projectRoot: string, options: PromptToolOptions return toolDocs ? `${SYSTEM_PROMPT_BASE}\n\n# Available Tools\n\n${toolDocs}` : SYSTEM_PROMPT_BASE; } -export function getCompactPrompt(sessionMessages: SessionMessage[]): string { - const jsonl = sessionMessages - .map((message) => - JSON.stringify({ - id: message.id, - role: message.role, - content: message.content, - contentParams: message.contentParams, - messageParams: message.messageParams, - createTime: message.createTime, - }) - ) - .join("\n"); - return `${COMPACT_PROMPT_BASE}\n\nconversation below:\n\n\`\`\`jsonl\n${jsonl}\n\`\`\``; +export function getCompactPrompt(): string { + return COMPACTION_INSTRUCTION; +} + +export function frameCompactSummary(summary: string): string { + return `${CHECKPOINT_PREAMBLE}\n\n${SUMMARY_OPEN_TAG}\n${summary}\n${SUMMARY_CLOSE_TAG}`; } export function getRuntimeContext(projectRoot: string, model?: string, addWorkingDirs: string[] = []): string { diff --git a/packages/core/src/session.ts b/packages/core/src/session.ts index df946158..2d315c38 100644 --- a/packages/core/src/session.ts +++ b/packages/core/src/session.ts @@ -14,6 +14,7 @@ import { buildSkillCatalogPrompt, buildSkillDocumentsPrompt, getCompactPrompt, + frameCompactSummary, getExtensionRoot, getPlanModePrompt, getRuntimeContext, @@ -41,6 +42,7 @@ import { DEFAULT_FILES_API_TIMEOUT_MS, DEFAULT_MAX_REQUEST_FILES_BYTES, getDefaultAutoCompactWindow, + getDefaultContextWindow, type McpServerConfig, type PermissionScope, type PermissionSettings, @@ -73,6 +75,14 @@ import { type DeepSeekFileReference, type DeepSeekFilesPolicy, } from "./common/deepseek-files"; +import { + compactBudget, + compactPrefixes, + estimateRequestTokens, + imageOffloadCount, + isContextOverflow, + isFixedContext, +} from "./common/compaction"; import { loadImageFile } from "./tools/image-file"; import { getLlmRetryDelayMs, @@ -310,6 +320,7 @@ export type MessageMeta = { asThinking?: boolean; isAnswers?: boolean; isSummary?: boolean; + omittedImages?: string[]; isModelChange?: boolean; skill?: SkillInfo; skillCatalog?: Array<{ name: string; description: string }>; @@ -517,8 +528,20 @@ export class SessionManager { this.sessionControllers.clear(); this.processTimeoutControls.clear(); this.mcpManager.disconnect(); + this.requestUsageAnchors.clear(); } + private readonly compactingSessions = new Set(); + private readonly requestUsageAnchors = new Map< + string, + { + route: string; + hashes: string[]; + estimate: number; + tokens: number; + } + >(); + private estimateStreamTokens(text: string): number { let tokens = 0; for (const char of text) { @@ -691,7 +714,7 @@ export class SessionManager { sessionId?: string, debug?: ChatCompletionDebugOptions ): Promise<{ - choices?: Array<{ message?: Record }>; + choices?: Array<{ message?: Record; finish_reason?: string | null }>; usage?: ModelUsage | null; }> { const requestId = crypto.randomUUID(); @@ -733,7 +756,7 @@ export class SessionManager { debug: ChatCompletionDebugOptions | undefined, requestId: string ): Promise<{ - choices?: Array<{ message?: Record }>; + choices?: Array<{ message?: Record; finish_reason?: string | null }>; usage?: ModelUsage | null; }> { const startedAt = new Date().toISOString(); @@ -838,7 +861,10 @@ export class SessionManager { request: streamRequest, response, }); - return response as { choices?: Array<{ message?: Record }>; usage?: ModelUsage | null }; + return response as { + choices?: Array<{ message?: Record; finish_reason?: string | null }>; + usage?: ModelUsage | null; + }; } let content = ""; @@ -846,6 +872,7 @@ export class SessionManager { let refusal: string | null = null; let usage: ModelUsage | null = null; let streamCompleted = false; + let finishReason: string | null = null; const responseChunks: unknown[] = []; const toolCallsByIndex = new Map< number, @@ -889,6 +916,7 @@ export class SessionManager { for (const choice of choices) { if (isUsageRecord(choice) && choice.finish_reason != null) { streamCompleted = true; + if (typeof choice.finish_reason === "string") finishReason = choice.finish_reason; } const delta = isUsageRecord(choice) && isUsageRecord(choice.delta) ? choice.delta : null; if (!delta) { @@ -992,7 +1020,7 @@ export class SessionManager { } const finalResponse = { - choices: [{ message }], + choices: [{ message, finish_reason: finishReason }], usage, }; this.logChatCompletionDebug(debug, { @@ -1672,20 +1700,7 @@ ${agentInstructions} permissionPrompt?: UserPromptContent ): Promise { const startedAt = Date.now(); - const { - client, - usingPlus, - configurationError, - apiKey, - model, - baseURL, - temperature, - thinkingEnabled, - reasoningEffort, - debugLogEnabled, - notify, - env, - } = this.createOpenAIClient(); + const { client, usingPlus, configurationError, model, notify, env } = this.createOpenAIClient(); const now = new Date().toISOString(); rebuildSessionStateFromHistory(sessionId, this.listSessionMessages(sessionId)); @@ -1769,87 +1784,7 @@ ${agentInstructions} } } - const compactPromptTokenThreshold = - this.getResolvedSettings().autoCompactWindow ?? getCompactPromptTokenThreshold(model); - if (session.activeTokens > compactPromptTokenThreshold) { - const message = this.buildAssistantMessage( - sessionId, - "The conversation is getting long, compacting...", - null - ); - message.meta = { asThinking: true }; - this.onAssistantMessage(message, false); - await this.compactSession(sessionId, sessionController.signal); - } - - const sessionMessages = await this.attachPromptImagesForRequest( - this.prepareSessionMessagesForRequest(this.listSessionMessages(sessionId)), - model, - this.getResolvedSettings().multimodal - ); - if (this.isInterrupted(sessionId)) { - return; - } - const filesSettings = this.getDeepSeekFilesSettings(); - if (filesSettings.enabled && !apiKey) { - throw new Error("Files API is enabled, but no API key is available for uploads."); - } - let prepared = filesSettings.enabled - ? await this.buildMessagesWithDeepSeekFiles( - sessionMessages, - thinkingEnabled, - model, - apiKey!, - sessionController.signal - ) - : { - messages: this.messageConverter.buildMessages( - sessionMessages, - thinkingEnabled, - model, - this.getResolvedSettings().multimodal - ), - references: [] as DeepSeekFileReference[], - }; - const thinkingOptions = buildThinkingRequestOptions(thinkingEnabled, baseURL, reasoningEffort); - const request = () => - this.createChatCompletionStream( - client, - { - model, - ...(temperature !== undefined ? { temperature } : {}), - messages: prepared.messages, - tools: getTools(this.getPromptToolOptions(), this.mcpToolDefinitions), - ...thinkingOptions, - }, - { signal: sessionController.signal }, - sessionId, - { - enabled: debugLogEnabled, - location: "SessionManager.activateSession", - baseURL, - params: { iteration, temperature, thinkingEnabled, reasoningEffort }, - } - ); - let response: Awaited>; - try { - response = await request(); - } catch (error) { - if (!filesSettings.enabled || prepared.references.length === 0 || !this.isRejectedDeepSeekFile(error)) { - throw error; - } - for (const reference of prepared.references) { - this.deepSeekFiles.invalidate(reference, apiKey!); - } - prepared = await this.buildMessagesWithDeepSeekFiles( - sessionMessages, - thinkingEnabled, - model, - apiKey!, - sessionController.signal - ); - response = await request(); - } + const response = await this.requestSessionCompletion(sessionId, sessionController.signal, iteration); const message = response.choices?.[0]?.message; const rawContent = message?.content; @@ -1977,93 +1912,378 @@ ${agentInstructions} } } - async compactSession(sessionId: string, signal?: AbortSignal): Promise { - this.throwIfAborted(signal); - const { client, model, baseURL, temperature, thinkingEnabled, reasoningEffort, debugLogEnabled } = + private getCompactionBudget(model: string) { + const settings = this.getResolvedSettings(); + return compactBudget(settings.contextWindow ?? getDefaultContextWindow(model), settings.autoCompactWindow); + } + + /** Derive exactly the content used by both normal and summary requests, before uploads. */ + private async prepareContext(messages: SessionMessage[], model: string, thinkingEnabled: boolean) { + const sources = await this.attachPromptImagesForRequest( + this.prepareSessionMessagesForRequest( + messages + .filter((message) => !message.compacted) + .map((message) => + message.meta?.isSummary && message.role === "system" + ? { + ...message, + role: "user" as const, + content: frameCompactSummary(message.content ?? ""), + } + : message + ) + ), + model, + this.getResolvedSettings().multimodal + ); + const converted = this.messageConverter.buildMessages( + sources, + thinkingEnabled, + model, + this.getDeepSeekFilesSettings().enabled ? "on" : this.getResolvedSettings().multimodal + ); + return { sources, messages: converted }; + } + + private measureRequest(sessionId: string, messages: ChatCompletionMessageParam[], model: string) { + const tools = getTools(this.getPromptToolOptions(), this.mcpToolDefinitions); + const client = this.createOpenAIClient(); + const route = JSON.stringify([model, client.baseURL, client.thinkingEnabled, client.reasoningEffort, tools]); + const hashes = messages.map((message) => crypto.createHash("sha256").update(JSON.stringify(message)).digest("hex")); + const estimate = estimateRequestTokens(messages, tools); + const anchor = this.requestUsageAnchors.get(sessionId); + const anchored = + anchor?.route === route && + anchor.hashes.length <= hashes.length && + anchor.hashes.every((hash, index) => hash === hashes[index]); + const tokens = anchored ? Math.max(estimate, anchor.tokens + estimate - anchor.estimate) : estimate; + return { route, hashes, estimate, tokens }; + } + + /** Shared file-reference recovery; it never repeats tool execution. */ + private async sendPreparedCompletion( + sources: SessionMessage[], + messages: ChatCompletionMessageParam[], + extra: Record, + sessionId: string, + signal: AbortSignal, + location: string + ) { + const { client, model, apiKey, baseURL, temperature, thinkingEnabled, reasoningEffort, debugLogEnabled } = this.createOpenAIClient(); - if (!client) { - return; - } - const sessionMessages = this.listSessionMessages(sessionId).filter((message) => !message.compacted); - if (sessionMessages.length === 0) { - return; - } - - const startIndex = sessionMessages.findIndex((message) => message.role !== "system"); - if (startIndex === -1) { - return; - } - - const searchStart = Math.floor(startIndex + ((sessionMessages.length - startIndex) * 2) / 3); - let endIndex = -1; - for (let i = Math.max(searchStart, startIndex); i < sessionMessages.length; i += 1) { - if (sessionMessages[i].role !== "tool") { - endIndex = i; - break; + if (!client) throw new Error("No model client is configured."); + const files = this.getDeepSeekFilesSettings(); + if (files.enabled && !apiKey) throw new Error("Files API is enabled, but no API key is available for uploads."); + // The final summary instruction is not a durable session source. + const suffix = extra.compaction === true ? [{ role: "user" as const, content: getCompactPrompt() }] : []; + const requestExtra = { ...extra }; + delete requestExtra.compaction; + const prepare = async () => + files.enabled + ? await this.buildMessagesWithDeepSeekFiles(sources, thinkingEnabled, model, apiKey!, signal) + : { messages, references: [] as DeepSeekFileReference[] }; + let prepared = await prepare(); + const request = () => + this.createChatCompletionStream( + client, + { + model, + ...(temperature !== undefined ? { temperature } : {}), + messages: [...prepared.messages, ...suffix], + tools: getTools(this.getPromptToolOptions(), this.mcpToolDefinitions), + ...buildThinkingRequestOptions(thinkingEnabled, baseURL, reasoningEffort), + ...requestExtra, + }, + { signal }, + sessionId, + { + enabled: debugLogEnabled, + location, + baseURL, + params: { temperature, thinkingEnabled, reasoningEffort }, + } + ); + try { + return await request(); + } catch (error) { + signal.throwIfAborted(); + if (!files.enabled || !prepared.references.length || !this.isRejectedDeepSeekFile(error)) throw error; + for (const reference of prepared.references) this.deepSeekFiles.invalidate(reference, apiKey!); + prepared = await prepare(); + return await request(); + } + } + + private async requestSessionCompletion(sessionId: string, signal: AbortSignal, iteration: number) { + const { model, thinkingEnabled } = this.createOpenAIClient(); + const budget = this.getCompactionBudget(model); + let context = await this.prepareContext(this.listSessionMessages(sessionId), model, thinkingEnabled); + let measurement = this.measureRequest(sessionId, context.messages, model); + for (let attempt = 0; measurement.tokens >= budget.threshold && attempt < 2; attempt++) { + if (attempt === 0) { + const notice = this.buildAssistantMessage(sessionId, "The conversation is getting long, compacting...", null); + notice.meta = { asThinking: true }; + this.onAssistantMessage(notice, false); + } + const changed = await this.compactSessionOnce(sessionId, signal, budget.retain); + if (!changed) break; + context = await this.prepareContext(this.listSessionMessages(sessionId), model, thinkingEnabled); + measurement = this.measureRequest(sessionId, context.messages, model); + } + if (measurement.tokens >= budget.threshold) { + throw new Error("The conversation is too large to compact safely. Shorten the input or start a new session."); + } + let overflowRecovered = false; + for (;;) { + signal.throwIfAborted(); + try { + const response = await this.sendPreparedCompletion( + context.sources, + context.messages, + {}, + sessionId, + signal, + `SessionManager.activateSession:${iteration}` + ); + const promptTokens = response.usage?.prompt_tokens; + this.requestUsageAnchors.set(sessionId, { + ...measurement, + tokens: Math.max( + measurement.estimate, + typeof promptTokens === "number" ? promptTokens : measurement.estimate + ), + }); + return response; + } catch (error) { + signal.throwIfAborted(); + const count = imageOffloadCount(error); + if ( + count > 0 && + this.offloadSessionImages( + sessionId, + context.sources.map((message) => message.id), + count + ) + ) { + this.requestUsageAnchors.delete(sessionId); + } else if (!overflowRecovered && isContextOverflow(error)) { + overflowRecovered = true; + if (!(await this.compactSessionOnce(sessionId, signal, 0))) throw error; + } else throw error; + context = await this.prepareContext(this.listSessionMessages(sessionId), model, thinkingEnabled); + measurement = this.measureRequest(sessionId, context.messages, model); + if (measurement.tokens > budget.input) + throw new Error("The conversation still exceeds the model context window."); } } - if (endIndex === -1 || endIndex <= startIndex) { - return; - } - - const compactPrompt = getCompactPrompt(sessionMessages.slice(startIndex, endIndex)); - const thinkingOptions = buildThinkingRequestOptions(thinkingEnabled, baseURL, reasoningEffort); - const response = await this.createChatCompletionStream( - client, - { - model, - ...(temperature !== undefined ? { temperature } : {}), - messages: [{ role: "user", content: compactPrompt }], - ...thinkingOptions, - }, - signal ? { signal } : undefined, - sessionId, - { - enabled: debugLogEnabled, - location: "SessionManager.compactSession", - baseURL, - params: { temperature, thinkingEnabled, reasoningEffort }, - } - ); - this.throwIfAborted(signal); - const rawLlmResponse = response.choices?.[0]?.message?.content; - const llmResponse = typeof rawLlmResponse === "string" ? rawLlmResponse : ""; - const compactedSummary = llmResponse.replace(/[\s\S]*?<\/analysis>/gi, "").trim(); + } - const now = new Date().toISOString(); - const responseUsage = response.usage ?? null; - this.updateSessionEntry(sessionId, (entry) => ({ - ...entry, - usage: accumulateUsage(entry.usage, responseUsage), - usagePerModel: accumulateUsagePerModel(entry.usagePerModel, model, responseUsage), - activeTokens: getTotalTokens(responseUsage), - updateTime: now, - })); + async compactSession(sessionId: string, signal?: AbortSignal): Promise { + await this.compactSessionOnce(sessionId, signal ?? new AbortController().signal, 0); + } - for (let i = startIndex; i < endIndex; i += 1) { - if (sessionMessages[i].meta?.skillCatalog) { - continue; + /** Commit only a complete, smaller checkpoint against an unchanged selected history. */ + private async compactSessionOnce(sessionId: string, signal: AbortSignal, retain: number): Promise { + signal.throwIfAborted(); + if (this.compactingSessions.has(sessionId)) throw new Error("This session is already being compacted."); + this.compactingSessions.add(sessionId); + try { + const { client, model, thinkingEnabled } = this.createOpenAIClient(); + if (!client) return false; + const budget = this.getCompactionBudget(model); + let active = this.listSessionMessages(sessionId).filter((message) => !message.compacted); + let context = await this.prepareContext(active, model, thinkingEnabled); + const prices = context.sources.map((message) => + estimateRequestTokens( + message.role === "tool" + ? [{ role: "tool", content: message.content ?? "", tool_call_id: "" }] + : this.messageConverter.buildMessages( + [message], + thinkingEnabled, + model, + this.getResolvedSettings().multimodal + ) + ) + ); + const prefixes = compactPrefixes(active, prices, retain); + let overflowRetried = false; + let maxPrefix = Infinity; + for (;;) { + signal.throwIfAborted(); + let selected: SessionMessage[] | undefined; + let replay: Awaited> | undefined; + for (const end of prefixes.ends) { + if (end >= maxPrefix) continue; + const prefix = prefixes.history.slice(0, end); + const ids = new Set(prefix.map((message) => message.id)); + const candidate = await this.prepareContext( + active.filter((message) => isFixedContext(message) || ids.has(message.id)), + model, + thinkingEnabled + ); + const cost = estimateRequestTokens( + [...candidate.messages, { role: "user", content: getCompactPrompt() }], + getTools(this.getPromptToolOptions(), this.mcpToolDefinitions) + ); + if (cost <= budget.input) { + selected = prefix; + replay = candidate; + break; + } + } + if (!selected || !replay) { + if (prefixes.ends.length === 0) return false; + throw new Error( + "The conversation is too large to summarize safely. Shorten the input or start a new session." + ); + } + const selectedIds = new Set(selected.map((message) => message.id)); + const firstSelectedId = selected[0].id; + const snapshot = JSON.stringify(active); + let response: Awaited>; + try { + response = await this.sendPreparedCompletion( + replay.sources, + replay.messages, + { compaction: true, max_tokens: budget.output, tool_choice: "none" }, + sessionId, + signal, + "SessionManager.compactSession" + ); + } catch (error) { + signal.throwIfAborted(); + if ( + JSON.stringify(this.listSessionMessages(sessionId).filter((message) => !message.compacted)) !== snapshot + ) { + throw new Error("The conversation changed during compaction. Please try again."); + } + const count = imageOffloadCount(error); + if ( + count > 0 && + this.offloadSessionImages( + sessionId, + replay.sources.map((message) => message.id), + count + ) + ) { + active = this.listSessionMessages(sessionId).filter((message) => !message.compacted); + context = await this.prepareContext(active, model, thinkingEnabled); + this.requestUsageAnchors.delete(sessionId); + continue; + } + if (!overflowRetried && isContextOverflow(error)) { + overflowRetried = true; + // Reduce substantially, rather than retrying an almost identical prefix. + maxPrefix = Math.max(2, Math.ceil(selected.length / 2) + 1); + maxPrefix = Math.min(maxPrefix, selected.length); + continue; + } + throw error; + } + signal.throwIfAborted(); + const now = new Date().toISOString(); + const usage = response.usage ?? null; + this.updateSessionEntry(sessionId, (entry) => ({ + ...entry, + usage: accumulateUsage(entry.usage, usage), + usagePerModel: accumulateUsagePerModel(entry.usagePerModel, model, usage), + updateTime: now, + })); + const choice = response.choices?.[0]; + const output = choice?.message; + const text = + typeof output?.content === "string" + ? output.content.replace(/[\s\S]*?<\/analysis>/gi, "").trim() + : ""; + if ( + !text || + output?.refusal || + (Array.isArray(output?.tool_calls) && output.tool_calls.length > 0) || + (choice?.finish_reason != null && choice.finish_reason !== "stop") + ) { + throw new Error("Compaction did not produce a complete text summary. The original conversation was kept."); + } + const summary: SessionMessage = { + id: crypto.randomUUID(), + sessionId, + role: "user", + content: frameCompactSummary(text), + contentParams: null, + messageParams: null, + compacted: false, + visible: false, + createTime: now, + updateTime: now, + meta: { isSummary: true }, + }; + const replacement = active.flatMap((message) => + message.id === firstSelectedId ? [summary] : selectedIds.has(message.id) ? [] : [message] + ); + const next = await this.prepareContext(replacement, model, thinkingEnabled); + if (estimateRequestTokens(next.messages) >= estimateRequestTokens(context.messages)) { + throw new Error("Compaction did not reduce the conversation size. The original conversation was kept."); + } + signal.throwIfAborted(); + const current = this.listSessionMessages(sessionId); + if (JSON.stringify(current.filter((message) => !message.compacted)) !== snapshot) { + throw new Error("The conversation changed during compaction. Please try again."); + } + const committed = current.flatMap((message) => { + const marked = selectedIds.has(message.id) ? { ...message, compacted: true, updateTime: now } : message; + return message.id === firstSelectedId ? [summary, marked] : [marked]; + }); + this.saveSessionMessages(sessionId, committed); + this.requestUsageAnchors.delete(sessionId); + const tokens = estimateRequestTokens( + next.messages, + getTools(this.getPromptToolOptions(), this.mcpToolDefinitions) + ); + this.updateSessionEntry(sessionId, (entry) => ({ ...entry, activeTokens: tokens, updateTime: now })); + return true; } - sessionMessages[i] = { ...sessionMessages[i], compacted: true, updateTime: now }; - } - - const summaryMessage: SessionMessage = { - id: crypto.randomUUID(), - sessionId, - role: "system", - content: `There are earlier parts of the conversation. Here is a summary: \n\n${compactedSummary}`, - contentParams: null, - messageParams: null, - compacted: false, - visible: false, - createTime: now, - updateTime: now, - meta: { - isSummary: true, - }, - }; - sessionMessages.splice(endIndex, 0, summaryMessage); - this.saveSessionMessages(sessionId, sessionMessages); + } finally { + this.compactingSessions.delete(sessionId); + } + } + + private offloadSessionImages(sessionId: string, sourceIds: string[], count: number): boolean { + const { model } = this.createOpenAIClient(); + const multimodal = supportsMultimodal(model, this.getResolvedSettings().multimodal); + if (!multimodal && !this.getDeepSeekFilesSettings().enabled) return false; + const sources = new Set(sourceIds); + let changed = false; + const messages = this.listSessionMessages(sessionId).map((message) => { + if (!sources.has(message.id) || message.compacted || (message.role !== "user" && message.role !== "system")) + return message; + const omitted = new Set(message.meta?.omittedImages ?? []); + const params = Array.isArray(message.contentParams) + ? message.contentParams + : message.contentParams + ? [message.contentParams] + : []; + const keys = params.flatMap((part, index) => (part?.type === "image_url" ? [`content:${index}`] : [])); + if (message.role === "user" && multimodal) { + (message.meta?.userPrompt?.imageUrls ?? []).forEach((url, index) => { + if (this.getLocalPromptImagePath(url) || /^https?:\/\//i.test(url)) keys.push(`prompt:${index}`); + }); + } + for (const key of keys) { + if (count <= 0) break; + if (omitted.has(key)) continue; + omitted.add(key); + count--; + changed = true; + } + return omitted.size === (message.meta?.omittedImages?.length ?? 0) + ? message + : { + ...message, + meta: { ...message.meta, omittedImages: [...omitted] }, + updateTime: new Date().toISOString(), + }; + }); + if (changed) this.saveSessionMessages(sessionId, messages); + return changed; } private getPromptToolOptions(): { @@ -2106,54 +2326,44 @@ ${agentInstructions} multimodal: MultimodalMode = "default" ): Promise { const includeImageContent = supportsMultimodal(model, multimodal); - const prepared = await Promise.all( + return await Promise.all( messages.map(async (message) => { - const imageUrls = message.role === "user" ? message.meta?.userPrompt?.imageUrls : undefined; - const promptImages: Array<{ source: "file" | "url"; value: string }> = []; - for (const imageUrl of imageUrls ?? []) { - const filePath = this.getLocalPromptImagePath(imageUrl); - if (filePath) { - promptImages.push({ source: "file", value: filePath }); - } else if (/^https?:\/\//i.test(imageUrl)) { - promptImages.push({ source: "url", value: imageUrl }); - } - } - if (promptImages.length === 0) { - return message; - } - - const contentParams = Array.isArray(message.contentParams) - ? [...message.contentParams] + const omitted = new Set(message.meta?.omittedImages ?? []); + const params = Array.isArray(message.contentParams) + ? message.contentParams : message.contentParams ? [message.contentParams] : []; - if (includeImageContent) { - const imageParts = await Promise.all( - promptImages.map(async ({ source, value }) => { - if (source === "url") { - return { type: "image_url", image_url: { url: value } }; - } - const { image } = await loadImageFile(value, this.loadSharp); - return { - type: "image_url", - image_url: { url: `data:${image.mediaType};base64,${image.data.toString("base64")}` }, - }; - }) - ); - for (const imagePart of imageParts) { - contentParams.push(imagePart); + const contentParams = params.map((part, index) => { + if (part?.type !== "image_url" || !omitted.has(`content:${index}`)) return part; + const url = typeof part.image_url?.url === "string" ? part.image_url.url : ""; + const reference = url.startsWith("data:") ? `session attachment ${message.id}/content:${index}` : url; + return { type: "text", text: `[Image omitted: ${reference}]` }; + }); + const imageUrls = message.role === "user" ? (message.meta?.userPrompt?.imageUrls ?? []) : []; + for (const [index, imageUrl] of imageUrls.entries()) { + const filePath = this.getLocalPromptImagePath(imageUrl); + if (!filePath && !/^https?:\/\//i.test(imageUrl)) continue; + if (omitted.has(`prompt:${index}`)) { + contentParams.push({ type: "text", text: `[Image omitted: ${filePath ?? imageUrl}]` }); + } else if (!includeImageContent) { + contentParams.push({ + type: "text", + text: `\n${JSON.stringify({ images: [filePath ?? imageUrl] }, null, 2)}\n`, + }); + } else if (filePath) { + const { image } = await loadImageFile(filePath, this.loadSharp); + contentParams.push({ + type: "image_url", + image_url: { url: `data:${image.mediaType};base64,${image.data.toString("base64")}` }, + }); + } else { + contentParams.push({ type: "image_url", image_url: { url: imageUrl } }); } } - if (!includeImageContent) { - contentParams.push({ - type: "text", - text: `\n${JSON.stringify({ images: promptImages.map(({ value }) => value) }, null, 2)}\n`, - }); - } - return { ...message, contentParams }; + return contentParams.length || params.length ? { ...message, contentParams } : message; }) ); - return prepared; } private getLocalPromptImagePath(imageUrl: string): string | null { @@ -2663,6 +2873,7 @@ ${agentInstructions} this.killTrackedProcess(processControlKey, pid); } + this.requestUsageAnchors.delete(sessionId); clearSessionState(sessionId); clearSessionWorkingDir(sessionId); const controller = this.sessionControllers.get(sessionId); @@ -2690,7 +2901,13 @@ ${agentInstructions} this.ensureProjectDir(); const messagePath = this.getSessionMessagesPath(sessionId); const payload = messages.map((message) => JSON.stringify(message)).join("\n"); - fs.writeFileSync(messagePath, payload ? `${payload}\n` : "", "utf8"); + const temporaryPath = `${messagePath}.${crypto.randomUUID()}.tmp`; + try { + fs.writeFileSync(temporaryPath, payload ? `${payload}\n` : "", "utf8"); + fs.renameSync(temporaryPath, messagePath); + } finally { + fs.rmSync(temporaryPath, { force: true }); + } } private updateSessionEntry(sessionId: string, updater: (entry: SessionEntry) => SessionEntry): SessionEntry | null { diff --git a/packages/core/src/settings.ts b/packages/core/src/settings.ts index c27b8970..4111814b 100644 --- a/packages/core/src/settings.ts +++ b/packages/core/src/settings.ts @@ -158,7 +158,7 @@ export function getDefaultContextWindow(model: string): number { } export function getDefaultAutoCompactWindow(model: string): number { - return getDefaultContextWindow(model) / 2; + return Math.floor(getDefaultContextWindow(model) * 0.8); } function parseTokenWindow(value: unknown): number | undefined { @@ -605,7 +605,7 @@ export function resolveSettingsSources( projectSettings?.autoCompactWindow, userSettings?.autoCompactWindow ); - const defaultAutoCompactWindow = Math.max(1, Math.floor(contextWindow / 2)); + const defaultAutoCompactWindow = Math.max(1, Math.floor(contextWindow * 0.8)); const autoCompactWindow = Math.min(configuredAutoCompactWindow ?? defaultAutoCompactWindow, contextWindow); const thinkingEnabled = diff --git a/packages/core/src/tests/compaction.test.ts b/packages/core/src/tests/compaction.test.ts new file mode 100644 index 00000000..f1f90845 --- /dev/null +++ b/packages/core/src/tests/compaction.test.ts @@ -0,0 +1,541 @@ +import { afterEach, test } from "node:test"; +import assert from "node:assert/strict"; +import * as fs from "node:fs"; +import * as os from "node:os"; +import * as path from "node:path"; +import { createHash } from "node:crypto"; +import { pathToFileURL } from "node:url"; +import { SessionManager, type SessionMessage } from "../session"; +import { OpenAIMessageConverter } from "../common/openai-message-converter"; +import { getCompactPrompt, frameCompactSummary } from "../prompt"; +import { + compactBudget, + compactPrefixes as selectPrefixes, + estimateRequestTokens, + imageOffloadCount, + isContextOverflow, +} from "../common/compaction"; + +const originalHome = process.env.HOME; +const originalUserProfile = process.env.USERPROFILE; +const directories: string[] = []; +afterEach(() => { + if (originalHome === undefined) delete process.env.HOME; + else process.env.HOME = originalHome; + if (originalUserProfile === undefined) delete process.env.USERPROFILE; + else process.env.USERPROFILE = originalUserProfile; + for (const directory of directories.splice(0)) fs.rmSync(directory, { recursive: true, force: true }); +}); + +function message(id: string, role: SessionMessage["role"] = "user", content = "history ".repeat(1000)): SessionMessage { + return { + id, + sessionId: "compact-test", + role, + content, + contentParams: null, + messageParams: null, + visible: true, + compacted: false, + createTime: "2026-01-01T00:00:00Z", + updateTime: "2026-01-01T00:00:00Z", + }; +} + +function answer(content = "## Current Work\n- Continue the requested implementation.", finish_reason = "stop") { + return { + choices: [{ message: { content }, finish_reason }], + usage: { prompt_tokens: 4000, completion_tokens: 20, total_tokens: 4020 }, + }; +} + +function fixture(history: SessionMessage[], respond: (request: any, call: number) => unknown = () => answer()) { + const root = fs.mkdtempSync(path.join(os.tmpdir(), "deepcode-compaction-")); + directories.push(root); + process.env.HOME = root; + process.env.USERPROFILE = root; + const requests: any[] = []; + const settings = { model: "test-vision", contextWindow: 256 * 1024, autoCompactWindow: 190000 }; + const manager = new SessionManager({ + projectRoot: root, + createOpenAIClient: () => ({ + model: settings.model, + thinkingEnabled: true, + client: { + chat: { + completions: { + create: async (request: any) => { + requests.push(request); + return await respond(request, requests.length); + }, + }, + }, + } as any, + }), + getResolvedSettings: () => settings, + renderMarkdown: (text) => text, + onAssistantMessage: () => {}, + }); + const internal = manager as any; + internal.saveSessionMessages("compact-test", history); + return { + manager, + internal, + requests, + settings, + root, + read: () => manager.listSessionMessages("compact-test"), + compact: (signal?: AbortSignal) => manager.compactSession("compact-test", signal), + run: () => internal.requestSessionCompletion("compact-test", new AbortController().signal, 0), + }; +} + +function compactPrefixes(history: SessionMessage[], prices: number[], retain: number) { + const selection = selectPrefixes(history, prices, retain); + return selection.ends.map((end) => selection.history.slice(0, end)); +} + +test("request estimates include reasoning and tools without charging image base64 as text", () => { + const request = (data: string) => [ + { + role: "user" as const, + content: [{ type: "image_url" as const, image_url: { url: `data:image/png;base64,${data}` } }], + }, + ]; + assert.equal(estimateRequestTokens(request("AA")), estimateRequestTokens(request("A".repeat(1000000)))); + assert.ok(estimateRequestTokens(request("AA")) > 1000); + assert.ok( + estimateRequestTokens([{ role: "assistant", content: "", reasoning_content: "x".repeat(10000) } as any]) > 3000 + ); + assert.ok(estimateRequestTokens([], [{ function: { description: "x".repeat(10000) } }]) > 3000); + assert.equal(compactBudget(262144).threshold, 196608); + assert.equal(compactBudget(1048576).threshold, Math.floor(1048576 * 0.8)); + assert.equal(compactBudget(262144, 50000).threshold, 50000); +}); + +test("prefixes preserve complete parallel tool groups, include old summaries and retain the newest unit", () => { + const summary = { ...message("summary", "system"), meta: { isSummary: true } }; + const call = { ...message("call", "assistant"), messageParams: { tool_calls: [{ id: "a" }, { id: "b" }] } }; + const results = ["a", "b"].map((id) => ({ ...message(id, "tool"), messageParams: { tool_call_id: id } })); + const history = [message("system", "system"), summary, message("older"), call, ...results]; + assert.deepEqual( + compactPrefixes( + history, + history.map(() => 10), + 0 + ).map((prefix) => prefix.map((m) => m.id)), + [["summary", "older"], ["summary"]] + ); + assert.deepEqual(compactPrefixes([message("huge")], [1000000], 0), []); + assert.deepEqual( + compactPrefixes([call, results[0], message("tail")], [10, 10, 10], 0).map((prefix) => prefix.map((m) => m.id)), + [["call", "a"]] + ); + assert.deepEqual(compactPrefixes([call, results[0]], [10, 10], 0), []); + const tail = [...history, message("tail")]; + assert.equal( + compactPrefixes( + tail, + tail.map(() => 10), + 0 + )[0].at(-1)?.id, + "b" + ); +}); + +test("historical interrupted calls can compact, but unfinished trailing tool batches stay intact", () => { + const call = { + ...message("call", "assistant"), + messageParams: { tool_calls: [{ id: "missing", type: "function", function: { name: "Bash", arguments: "{}" } }] }, + }; + const resumed = [call, message("resume", "user", "Continue"), message("work", "assistant"), message("tail")]; + const selected = compactPrefixes(resumed, [10, 10, 10, 10], 0)[0]; + assert.deepEqual( + selected?.map((m) => m.id), + ["call", "resume", "work"] + ); + const replay = new OpenAIMessageConverter().buildMessages(selected, false, "test-model"); + assert.equal(replay[1].role, "tool"); + assert.match(String(replay[1].content), /Previous tool call did not complete/); + assert.deepEqual(compactPrefixes([call], [10], 0), []); + + const parallel = { ...call, messageParams: { tool_calls: [{ id: "missing" }, { id: "done" }] } }; + const result = { ...message("result", "tool"), messageParams: { tool_call_id: "done" } }; + assert.deepEqual(compactPrefixes([parallel, result, message("system", "system")], [10, 10, 10], 0), []); +}); + +test("compaction follows the converter's preferred result when a real result replaces an interrupted placeholder", () => { + const call = { ...message("call", "assistant"), messageParams: { tool_calls: [{ id: "a" }] } }; + const placeholder = { + ...message("placeholder", "tool", JSON.stringify({ metadata: { interrupted: true } })), + messageParams: { tool_call_id: "a" }, + }; + const result = { ...message("result", "tool", "real result"), messageParams: { tool_call_id: "a" } }; + const history = [call, placeholder, message("resume"), result, message("tail")]; + assert.deepEqual(compactPrefixes(history, [10, 10, 10, 10, 10], 30), []); + const selected = compactPrefixes(history, [10, 10, 10, 10, 10], 0)[0]; + assert.deepEqual( + selected.map((m) => m.id), + ["call", "placeholder", "resume", "result"] + ); + const replay = new OpenAIMessageConverter().buildMessages(selected, false, "test-model"); + assert.equal(replay.find((m) => m.role === "tool")?.content, "real result"); +}); + +test("automatic compaction resumes after an interrupted parallel tool batch", async () => { + const call = { + ...message("interrupted", "assistant", "old work ".repeat(7000)), + messageParams: { + tool_calls: ["done", "missing"].map((id) => ({ + id, + type: "function", + function: { name: "Bash", arguments: "{}" }, + })), + }, + }; + const result = { ...message("result", "tool", "finished result"), messageParams: { tool_call_id: "done" } }; + const f = fixture([call, result, message("resume", "user", "continue ".repeat(6000))]); + f.settings.contextWindow = 100000; + const context = await f.internal.prepareContext(f.read(), f.settings.model, true); + f.settings.autoCompactWindow = + f.internal.measureRequest("compact-test", context.messages, f.settings.model).tokens - 10000; + await f.run(); + + assert.equal(f.requests.length, 2); + assert.equal(f.requests[0].tool_choice, "none"); + const toolResults = f.requests[0].messages.filter((m: any) => m.role === "tool"); + assert.equal(toolResults.find((m: any) => m.tool_call_id === "done")?.content, "finished result"); + assert.match( + toolResults.find((m: any) => m.tool_call_id === "missing")?.content, + /Previous tool call did not complete/ + ); + assert.equal(f.read().find((m) => m.id === "interrupted")?.compacted, true); + assert.equal(f.read().find((m) => m.id === "result")?.compacted, true); + assert.equal(f.read().find((m) => m.id === "resume")?.compacted, false); + assert.equal( + f.requests[1].messages.some((m: any) => m.role === "tool"), + false + ); +}); + +test("only explicit context and image capacity errors enable recovery", () => { + assert.equal(isContextOverflow({ status: 400, error: { code: "context_length_exceeded" } }), true); + assert.equal(isContextOverflow({ status: 400, message: "maximum context length is 10000 tokens" }), true); + assert.equal(isContextOverflow({ status: 401, message: "maximum context length" }), false); + assert.equal(imageOffloadCount({ code: "IMAGE_OFFLOAD_REQUIRED", offloadImages: 3 }), 3); + assert.equal(imageOffloadCount({ status: 400, message: "Too many images" }), 1); + assert.equal(imageOffloadCount({ status: 400, message: "Invalid image data" }), 0); + assert.equal(imageOffloadCount({ status: 413, message: "Request body too large" }), 0); +}); + +test("compaction instruction and checkpoint framing match the DSH source text", () => { + // Digest of COMPACTION_INSTRUCTION from the reference DSH summarizer.ts. + assert.equal( + createHash("sha256").update(getCompactPrompt()).digest("hex"), + "3b7aa71f525b9a82afc2f1747c4434ceb7b3bc1fadf8b64c37c4e97db70d074b" + ); + assert.match( + frameCompactSummary("checkpoint"), + /without acknowledging this checkpoint\.\n\n\ncheckpoint\n<\/compacted-summary>$/ + ); +}); + +test("native replay preserves multimodal observations and consolidates legacy system summaries", async () => { + const image = { + ...message("image", "user", "Slide 3: the title overlaps the chart. " + "details ".repeat(1000)), + contentParams: [{ type: "image_url", image_url: { url: "data:image/png;base64," + "A".repeat(100000) } }], + }; + const legacy = { ...message("legacy", "system"), meta: { isSummary: true } }; + const catalog = { ...message("catalog", "system", "Available skills"), meta: { skillCatalog: [] } }; + const archived = { ...message("archived"), compacted: true }; + const f = fixture([ + archived, + message("system", "system", "System instruction"), + legacy, + catalog, + image, + message("tail", "user", "continue"), + ]); + await f.compact(); + const request = f.requests[0]; + assert.equal(request.messages.at(-1).content, getCompactPrompt()); + assert.equal(request.tool_choice, "none"); + assert.equal(request.max_tokens, 32768); + assert.ok(request.tools.length > 0); + assert.ok( + request.messages.some( + (m: any) => m.role === "user" && typeof m.content === "string" && m.content.includes("") + ) + ); + const replayedImage = request.messages.find((m: any) => Array.isArray(m.content)); + assert.match(replayedImage.content[0].text, /title overlaps/); + assert.equal(replayedImage.content[1].type, "image_url"); + assert.ok(!request.messages.some((m: any) => typeof m.content === "string" && m.content.includes("AAAA"))); + const saved = f.read(); + assert.equal(saved.find((m) => m.id === "archived")?.content, archived.content); + assert.equal(saved.find((m) => m.id === "legacy")?.compacted, true); + assert.equal(saved.find((m) => m.id === "catalog")?.compacted, false); + assert.equal(saved.filter((m) => m.meta?.isSummary && !m.compacted).length, 1); + f.internal.appendSessionMessage("compact-test", message("next", "user", "Continue again")); + // Make this second region large enough for an honest shrink comparison. + f.internal.appendSessionMessage("compact-test", message("more")); + f.internal.appendSessionMessage("compact-test", message("last", "user", "next")); + await f.compact(); + assert.equal(f.read().filter((m) => m.meta?.isSummary && !m.compacted).length, 1); + assert.match(JSON.stringify(f.requests[1].messages), /compacted-summary/); +}); + +test("invalid summaries and cancellation leave original history intact", async () => { + const responses = [ + answer(""), + answer("partial", "length"), + answer("x".repeat(100000)), + { choices: [{ message: { content: "refused", refusal: "no" }, finish_reason: "stop" }] }, + { choices: [{ message: { content: "tool", tool_calls: [{ id: "a" }] }, finish_reason: "tool_calls" }] }, + ]; + for (const response of responses) { + const history = [message("old"), message("tail", "user", "continue")]; + const f = fixture(history, () => response); + await assert.rejects(f.compact(), /complete text summary|did not reduce/); + assert.deepEqual(f.read(), history); + assert.equal(f.requests.length, 1); + } + const history = [message("old"), message("tail")]; + const controller = new AbortController(); + const f = fixture(history, () => { + controller.abort(); + return answer(); + }); + await assert.rejects(f.compact(controller.signal)); + assert.deepEqual(f.read(), history); +}); + +test("streamed length finish is retained and rejects a truncated checkpoint", async () => { + const f = fixture([message("old"), message("tail")], async function* () { + yield { choices: [{ delta: { content: "partial checkpoint" }, finish_reason: "length" }] }; + }); + await assert.rejects(f.compact(), /complete text summary/); + assert.ok(f.read().every((m) => !m.compacted)); +}); + +test("summary overflow retries a smaller balanced prefix once", async () => { + const history = Array.from({ length: 7 }, (_, index) => message(String(index))); + const f = fixture(history, (_, call) => { + if (call === 1) throw { status: 400, code: "context_length_exceeded" }; + return answer(); + }); + await f.compact(); + assert.equal(f.requests.length, 2); + assert.ok(f.requests[1].messages.length < f.requests[0].messages.length); + const failing = fixture(history, () => { + throw { status: 400, code: "context_length_exceeded" }; + }); + await assert.rejects(failing.compact()); + assert.equal(failing.requests.length, 2); + assert.deepEqual(failing.read(), history); +}); + +test("over-budget summary input is reduced before sending and huge indivisible input fails closed", async () => { + const f = fixture([message("a"), message("b", "user", "x".repeat(1000000)), message("tail")]); + await f.compact(); + assert.equal(f.requests.length, 1); + assert.ok(!JSON.stringify(f.requests[0].messages).includes("x".repeat(100))); + const huge = fixture([message("huge", "user", "x".repeat(1000000)), message("tail")]); + await assert.rejects(huge.compact(), /too large/); + assert.equal(huge.requests.length, 0); + assert.ok(huge.read().every((m) => !m.compacted)); +}); + +test("image capacity recovery persists omission without deleting the original attachment", async () => { + const image = { + ...message("image"), + contentParams: [{ type: "image_url", image_url: { url: "data:image/png;base64,AAAA" } }], + }; + const f = fixture([image, message("tail")], (_, call) => { + if (call === 1) throw { status: 400, code: "too_many_images" }; + throw { status: 401, message: "unauthorized" }; + }); + await assert.rejects(f.compact()); + assert.equal(f.requests.length, 2); + assert.ok(JSON.stringify(f.requests[1].messages).includes("Image omitted:")); + assert.ok(!JSON.stringify(f.requests[1].messages).includes("base64")); + assert.deepEqual(f.read()[0].contentParams, image.contentParams); + assert.deepEqual(f.read()[0].meta?.omittedImages, ["content:0"]); + assert.ok(f.read().every((m) => !m.compacted)); + const replay = await f.internal.prepareContext(f.read(), f.settings.model, true); + assert.match(JSON.stringify(replay.messages), /Image omitted:/); +}); + +test("omitted prompt images are not reloaded from disk", async () => { + const f = fixture([]); + const input = { + ...message("prompt"), + meta: { + omittedImages: ["prompt:0"], + userPrompt: { imageUrls: [pathToFileURL(path.join(f.root, "missing.png")).href] }, + }, + }; + const replay = await f.internal.prepareContext([input], f.settings.model, true); + assert.match(JSON.stringify(replay.messages), /Image omitted:.*missing.png/); +}); + +test("normal request overflow forces one useful reduction, then retries without tool execution", async () => { + const f = fixture([message("old"), message("tail", "user", "continue")], (_, call) => { + if (call === 1) throw { status: 400, code: "context_length_exceeded" }; + return answer(); + }); + await f.run(); + assert.equal(f.requests.length, 3); + assert.equal(f.requests[1].messages.at(-1).content, getCompactPrompt()); + assert.ok(f.read().some((m) => m.compacted)); + const failing = fixture([message("old"), message("tail")], (request) => { + if (request.tool_choice === "none") return answer(); + throw { status: 400, code: "context_length_exceeded" }; + }); + await assert.rejects(failing.run()); + assert.equal(failing.requests.length, 3); +}); + +test("fresh large input triggers compaction before the next normal request", async () => { + const f = fixture([message("old", "user", "a".repeat(60000)), message("tail", "user", "b".repeat(50000))]); + f.settings.contextWindow = 100000; + const context = await f.internal.prepareContext(f.read(), f.settings.model, true); + const measured = f.internal.measureRequest("compact-test", context.messages, f.settings.model); + f.settings.autoCompactWindow = measured.tokens - 10000; + await f.run(); + assert.equal(f.requests[0].tool_choice, "none"); + assert.equal(f.requests.length, 2); +}); + +test("usage anchors include new input and invalidate when history or model changes", async () => { + const f = fixture([message("old")], () => ({ ...answer(), usage: { prompt_tokens: 100000 } })); + await f.run(); + const context = await f.internal.prepareContext(f.read(), f.settings.model, true); + const same = f.internal.measureRequest("compact-test", context.messages, f.settings.model); + assert.equal(same.tokens, 100000); + const added = [...context.messages, { role: "user", content: "x".repeat(10000) }]; + assert.ok(f.internal.measureRequest("compact-test", added, f.settings.model).tokens > 103000); + const changed = f.internal.measureRequest("compact-test", added, "different-model"); + assert.equal(changed.tokens, changed.estimate); + const rewritten = f.internal.measureRequest("compact-test", [{ role: "user", content: "changed" }], f.settings.model); + assert.equal(rewritten.tokens, rewritten.estimate); +}); + +test("concurrent compaction and changed histories cannot commit a stale summary", async () => { + let release!: (value: unknown) => void; + const f = fixture( + [message("old"), message("tail")], + () => + new Promise((resolve) => { + release = resolve; + }) + ); + const pending = f.compact(); + while (!release) await new Promise((resolve) => setImmediate(resolve)); + await assert.rejects(f.compact(), /already being compacted/); + f.internal.appendSessionMessage("compact-test", message("new")); + release(answer()); + await assert.rejects(pending, /changed during compaction/); + assert.ok(f.read().every((m) => !m.compacted)); + assert.equal(f.read().at(-1)?.id, "new"); +}); + +test("fixed context and an indivisible retained tail cannot cause repeated summary calls", async () => { + const fixed = fixture([message("system", "system", "x".repeat(1000000)), message("only")]); + await assert.rejects(fixed.run(), /too large/); + assert.equal(fixed.requests.length, 0); + assert.ok(fixed.read().every((m) => !m.compacted)); + + const tail = fixture([message("old"), message("tail", "user", "x".repeat(1000000))]); + await assert.rejects(tail.run(), /too large|did not reduce/); + assert.ok(tail.requests.length <= 2); + assert.ok(tail.requests.every((request) => request.tool_choice === "none")); + assert.equal(tail.read().find((m) => m.id === "tail")?.compacted, false); +}); + +test("new parallel tool results are measured before requesting another assistant step", async () => { + const call = { + ...message("call", "assistant", ""), + messageParams: { + tool_calls: [ + { id: "a", type: "function", function: { name: "Read", arguments: "{}" } }, + { id: "b", type: "function", function: { name: "Read", arguments: "{}" } }, + ], + }, + }; + const results = ["a", "b"].map((id) => ({ + ...message(id, "tool", "x".repeat(30000)), + messageParams: { tool_call_id: id }, + })); + const f = fixture([message("old", "user", "y".repeat(60000)), call, ...results]); + f.settings.contextWindow = 100000; + const context = await f.internal.prepareContext(f.read(), f.settings.model, true); + f.settings.autoCompactWindow = + f.internal.measureRequest("compact-test", context.messages, f.settings.model).tokens - 10000; + await f.run(); + assert.equal(f.requests.length, 2); + assert.equal(f.requests[0].tool_choice, "none"); + const normal = f.requests[1].messages; + assert.equal(normal.filter((m: any) => m.role === "tool").length, 2); + assert.equal(normal.find((m: any) => m.role === "assistant").tool_calls.length, 2); +}); + +test("image recovery terminates when no retained image remains", async () => { + const image = { + ...message("image"), + contentParams: [0, 1].map((index) => ({ + type: "image_url", + image_url: { url: `https://example.com/${index}.png` }, + })), + }; + const f = fixture([image, message("tail")], () => { + throw { status: 400, code: "too_many_images" }; + }); + await assert.rejects(f.run()); + assert.equal(f.requests.length, 3); + assert.deepEqual(f.read()[0].meta?.omittedImages, ["content:0", "content:1"]); + assert.ok(f.read().every((m) => !m.compacted)); +}); + +test("non-capacity errors do not compact or omit images", async () => { + const image = { + ...message("image"), + contentParams: [{ type: "image_url", image_url: { url: "https://example.com/image.png" } }], + }; + const history = [image, message("tail")]; + const f = fixture(history, () => { + throw { status: 401, message: "Unauthorized" }; + }); + await assert.rejects(f.run()); + assert.equal(f.requests.length, 1); + assert.deepEqual(f.read(), history); +}); + +test("summary requests share Files API upload and expired-reference recovery", async () => { + const image = { + ...message("image"), + contentParams: [{ type: "image_url", image_url: { url: "data:image/png;base64,AAAA" } }], + }; + const f = fixture([image, message("tail")], (_, call) => { + if (call === 1) throw Object.assign(new Error("file id expired"), { status: 400 }); + return answer(); + }); + const originalClient = f.internal.createOpenAIClient; + f.internal.createOpenAIClient = () => ({ ...originalClient(), apiKey: "test-key" }); + Object.assign(f.settings, { filesApiEnabled: true }); + let uploads = 0; + let invalidations = 0; + f.internal.deepSeekFiles = { + ensureUploaded: async () => ({ fileId: `file-${++uploads}`, imageHash: "a".repeat(64), bytes: 3 }), + invalidate: () => { + invalidations++; + }, + }; + await f.compact(); + assert.equal(uploads, 2); + assert.equal(invalidations, 1); + assert.equal(f.requests.length, 2); + assert.equal(f.requests[1].messages.at(-1).content, getCompactPrompt()); + assert.match(JSON.stringify(f.requests[1].messages), /file-2/); + assert.doesNotMatch(JSON.stringify(f.requests[1].messages), /base64/); +}); diff --git a/packages/core/src/tests/session.test.ts b/packages/core/src/tests/session.test.ts index c554f593..3cac4bc1 100644 --- a/packages/core/src/tests/session.test.ts +++ b/packages/core/src/tests/session.test.ts @@ -999,9 +999,9 @@ test("compactSession does not mark skill catalog messages compacted", async () = id: "assistant-1", sessionId, role: "assistant", - content: "first reply", + content: "first reply".repeat(1000), contentParams: null, - messageParams: null, + messageParams: { tool_calls: [{ id: "call-1", type: "function", function: { name: "Read", arguments: "{}" } }] }, compacted: false, visible: true, createTime: now, @@ -3515,9 +3515,9 @@ test("SessionManager resets active tokens to latest post-compaction response usa const responses = [ createChatResponse("large", { - prompt_tokens: 139_990, + prompt_tokens: 209_990, completion_tokens: 10, - total_tokens: 140_000, + total_tokens: 210_000, }), createChatResponse("summary", { prompt_tokens: 100, @@ -3532,21 +3532,21 @@ test("SessionManager resets active tokens to latest post-compaction response usa ]; const manager = createMockedClientSessionManager(workspace, responses); - const sessionId = await manager.createSession({ text: "" }); - assert.equal(manager.getSession(sessionId)?.activeTokens, 140_000); + const sessionId = await manager.createSession({ text: "history ".repeat(1000) }); + assert.equal(manager.getSession(sessionId)?.activeTokens, 210_000); - await manager.replySession(sessionId, { text: "" }); + await manager.replySession(sessionId, { text: "new input ".repeat(16000) }); const session = manager.getSession(sessionId); const usage = session?.usage as Record; const usagePerModel = session?.usagePerModel?.["test-model"] as Record; assert.equal(session?.activeTokens, 7); - assert.equal(usage.prompt_tokens, 140_095); + assert.equal(usage.prompt_tokens, 210_095); assert.equal(usage.completion_tokens, 35); - assert.equal(usage.total_tokens, 140_130); - assert.equal(usagePerModel.prompt_tokens, 140_095); + assert.equal(usage.total_tokens, 210_130); + assert.equal(usagePerModel.prompt_tokens, 210_095); assert.equal(usagePerModel.completion_tokens, 35); - assert.equal(usagePerModel.total_tokens, 140_130); + assert.equal(usagePerModel.total_tokens, 210_130); assert.equal(usagePerModel.total_reqs, 3); }); @@ -3556,14 +3556,14 @@ test("SessionManager uses the configured auto compact window", async () => { setHomeDir(home); const responses = [ - createChatResponse("large", { prompt_tokens: 990, completion_tokens: 10, total_tokens: 1000 }), + createChatResponse("large", { prompt_tokens: 99_990, completion_tokens: 10, total_tokens: 100_000 }), createChatResponse("summary", { prompt_tokens: 50, completion_tokens: 10, total_tokens: 60 }), createChatResponse("after compact", { prompt_tokens: 5, completion_tokens: 2, total_tokens: 7 }), ]; - const manager = createMockedClientSessionManager(workspace, responses, 500); + const manager = createMockedClientSessionManager(workspace, responses, 90_000); - const sessionId = await manager.createSession({ text: "" }); - await manager.replySession(sessionId, { text: "" }); + const sessionId = await manager.createSession({ text: "history ".repeat(1000) }); + await manager.replySession(sessionId, { text: "new input ".repeat(16000) }); assert.equal(manager.getSession(sessionId)?.activeTokens, 7); assert.equal(manager.getSession(sessionId)?.usagePerModel?.["test-model"]?.total_reqs, 3); @@ -4414,11 +4414,21 @@ test("Files API mode invalidates a rejected file ID and uploads it once more", a assert.deepEqual(retriedUserMessage.content, [{ type: "file", file_id: "file-image-2" }]); }); -test("Files API mode checks the aggregate request limit before uploading", async () => { +test("Files API mode omits over-budget images before uploading and preserves the attachment", async () => { const workspace = createTempDir("deepcode-files-limit-workspace-"); const home = createTempDir("deepcode-files-limit-home-"); setHomeDir(home); - const client = { chat: { completions: { create: async () => assert.fail("chat request must not start") } } }; + const requests: any[] = []; + const client = { + chat: { + completions: { + create: async (request: any) => { + requests.push(request); + return createChatResponse("done", { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 }); + }, + }, + }, + }; const manager = new SessionManager({ projectRoot: workspace, createOpenAIClient: () => ({ @@ -4444,8 +4454,14 @@ test("Files API mode checks the aggregate request limit before uploading", async const sessionId = await manager.createSession({ imageUrls: [await createOnePixelPngDataUrl()] }); - assert.equal(manager.getSession(sessionId)?.status, "failed"); - assert.match(manager.getSession(sessionId)?.failReason ?? "", /configured 4-byte/); + assert.equal(manager.getSession(sessionId)?.status, "completed"); + assert.equal(requests.length, 1); + assert.match(JSON.stringify(requests[0].messages), /Image omitted:/); + const imageMessage = manager + .listSessionMessages(sessionId) + .find((message) => message.meta?.userPrompt?.imageUrls?.length); + assert.deepEqual(imageMessage?.meta?.omittedImages, ["prompt:0"]); + assert.ok(imageMessage?.meta?.userPrompt?.imageUrls?.length); }); test("sessions reject unsupported pasted image data before creating a session", async () => { diff --git a/packages/core/src/tests/settings-and-notify.test.ts b/packages/core/src/tests/settings-and-notify.test.ts index eca53783..5193648b 100644 --- a/packages/core/src/tests/settings-and-notify.test.ts +++ b/packages/core/src/tests/settings-and-notify.test.ts @@ -246,9 +246,9 @@ test("resolveSettings derives model-specific context window defaults", () => { ); assert.equal(regular.contextWindow, 256 * 1024); - assert.equal(regular.autoCompactWindow, 128 * 1024); + assert.equal(regular.autoCompactWindow, Math.floor(256 * 1024 * 0.8)); assert.equal(deepseekV4.contextWindow, 1024 * 1024); - assert.equal(deepseekV4.autoCompactWindow, 512 * 1024); + assert.equal(deepseekV4.autoCompactWindow, Math.floor(1024 * 1024 * 0.8)); }); test("resolveSettings parses numeric and K/M context window settings", () => { @@ -273,7 +273,7 @@ test("resolveSettings derives auto compact window from the configured context wi ); assert.equal(resolved.contextWindow, 512 * 1024); - assert.equal(resolved.autoCompactWindow, 256 * 1024); + assert.equal(resolved.autoCompactWindow, Math.floor(512 * 1024 * 0.8)); }); test("resolveSettings ignores invalid windows and caps auto compact window at context window", () => { @@ -289,7 +289,7 @@ test("resolveSettings ignores invalid windows and caps auto compact window at co ); assert.equal(invalid.contextWindow, 256 * 1024); - assert.equal(invalid.autoCompactWindow, 128 * 1024); + assert.equal(invalid.autoCompactWindow, Math.floor(256 * 1024 * 0.8)); assert.equal(capped.contextWindow, 128 * 1024); assert.equal(capped.autoCompactWindow, 128 * 1024); }); @@ -318,7 +318,7 @@ test("resolveSettingsSources skips invalid higher-priority context window values ); assert.equal(resolved.contextWindow, 512 * 1024); - assert.equal(resolved.autoCompactWindow, 256 * 1024); + assert.equal(resolved.autoCompactWindow, Math.floor(512 * 1024 * 0.8)); }); test("resolveSettings reads TEMPERATURE, THINKING_ENABLED, REASONING_EFFORT, and DEBUG_LOG_ENABLED from env", () => { @@ -898,7 +898,7 @@ test("resolveSettings applies deepseek-flash capabilities and respects explicit assert.equal(resolved.model, "deepseek-flash"); assert.equal(resolved.thinkingEnabled, true); assert.equal(resolved.contextWindow, 1024 * 1024); - assert.equal(resolved.autoCompactWindow, 512 * 1024); + assert.equal(resolved.autoCompactWindow, Math.floor(1024 * 1024 * 0.8)); const overridden = resolveSettings( { model: "deepseek-flash", thinkingEnabled: false, contextWindow: "512K", autoCompactWindow: "128K" }, diff --git a/packages/core/templates/skills/bundled/deepcode-self-refer/references/configuration.md b/packages/core/templates/skills/bundled/deepcode-self-refer/references/configuration.md index 391303ff..ae656103 100644 --- a/packages/core/templates/skills/bundled/deepcode-self-refer/references/configuration.md +++ b/packages/core/templates/skills/bundled/deepcode-self-refer/references/configuration.md @@ -28,7 +28,7 @@ Deep Code 使用 `settings.json` 设置文件进行持久化配置,支持两 | -------------------------- | ------------- | ------------------------------------------------------------------------------------- | | `env` | object | 环境变量分组(见下方子字段表) | | `contextWindow` | number/string | 上下文窗口上限,可使用精确 token 数或 `128K`、`1M` 等格式 | -| `autoCompactWindow` | number/string | 自动压缩阈值,默认取最终上下文窗口的 50% | +| `autoCompactWindow` | number/string | 自动压缩阈值,默认取上下文窗口的 80%;预留回复空间时可能提前触发 | | `model` | string | 模型名称。默认 `deepseek-v4-flash`,优先级高于 `env.MODEL` | | `thinkingEnabled` | boolean | 是否启用思考模式(DeepSeek V4 系列默认启用) | | `reasoningEffort` | string | 推理强度,可选 `"low"`、`"high"` 或 `"max"`(默认 `"max"`) | diff --git a/packages/core/templates/skills/bundled/deepcode-self-refer/references/configuration_en.md b/packages/core/templates/skills/bundled/deepcode-self-refer/references/configuration_en.md index beab23d4..a5e72d6f 100644 --- a/packages/core/templates/skills/bundled/deepcode-self-refer/references/configuration_en.md +++ b/packages/core/templates/skills/bundled/deepcode-self-refer/references/configuration_en.md @@ -28,7 +28,7 @@ The following are all the top-level fields supported in `settings.json`, along w | -------------------------- | ------------- | -------------------------------------------------------------------------------------------------------------- | | `env` | object | Group of environment variables (see sub-field table below) | | `contextWindow` | number/string | Context-window limit as an exact token count or `128K`/`1M` value | -| `autoCompactWindow` | number/string | Auto-compaction threshold; defaults to 50% of the final context window | +| `autoCompactWindow` | number/string | Auto-compaction threshold; defaults to 80% of the context window and may trigger earlier to reserve room for replies | | `model` | string | Model name. Defaults to `deepseek-v4-flash` and takes precedence over `env.MODEL` | | `thinkingEnabled` | boolean | Whether to enable thinking mode (enabled by default for DeepSeek V4 series) | | `reasoningEffort` | string | Reasoning intensity: `"low"`, `"high"`, or `"max"` (default `"max"`) |