Compare commits

...
Author SHA1 Message Date
Cline Evaluation 1d70baff55 auto summarize compact 2025-06-06 00:36:49 -07:00
9 changed files with 179 additions and 49 deletions
+3 -2
View File
@@ -70,8 +70,9 @@ enum ClineSay {
DELETED_API_REQS = 21;
CLINEIGNORE_ERROR = 22;
CHECKPOINT_CREATED = 23;
LOAD_MCP_DOCUMENTATION = 24;
INFO = 25;
LOAD_MCP_DOCUMENTATION = 24;
INFO = 25;
AUTO_COMPACT_SUMMARY = 26;
}
// Enum for ClineSayTool tool types
@@ -1,4 +1,5 @@
import { getContextWindowInfo } from "./context-window-utils"
import { AUTO_SUMMARIZATION_PROMPT } from "@core/prompts/autoSummarizationPrompt"
import { formatResponse } from "@core/prompts/responses"
import { GlobalFileNames } from "@core/storage/disk"
import { fileExistsAtPath } from "@utils/fs"
@@ -115,62 +116,29 @@ export class ContextManager {
conversationHistoryDeletedRange: [number, number] | undefined,
previousApiReqIndex: number,
taskDirectory: string,
) {
): Promise<{
conversationHistoryDeletedRange: [number, number] | undefined
updatedConversationHistoryDeletedRange: boolean
truncatedConversationHistory: Anthropic.Messages.MessageParam[]
summary?: string
}> {
let updatedConversationHistoryDeletedRange = false
let summary: string | undefined
// If the previous API request's total token usage is close to the context window, truncate the conversation history to free up space for the new request
if (previousApiReqIndex >= 0) {
const previousRequest = clineMessages[previousApiReqIndex]
if (previousRequest && previousRequest.text) {
const timestamp = previousRequest.ts
if (previousRequest?.text) {
const { tokensIn, tokensOut, cacheWrites, cacheReads }: ClineApiReqInfo = JSON.parse(previousRequest.text)
const totalTokens = (tokensIn || 0) + (tokensOut || 0) + (cacheWrites || 0) + (cacheReads || 0)
const { maxAllowedSize } = getContextWindowInfo(api)
// This is the most reliable way to know when we're close to hitting the context window.
if (totalTokens >= maxAllowedSize) {
// Since the user may switch between models with different context windows, truncating half may not be enough (ie if switching from claude 200k to deepseek 64k, half truncation will only remove 100k tokens, but we need to remove much more)
// So if totalTokens/2 is greater than maxAllowedSize, we truncate 3/4 instead of 1/2
const keep = totalTokens / 2 > maxAllowedSize ? "quarter" : "half"
// we later check how many chars we trim to determine if we should still truncate history
let [anyContextUpdates, uniqueFileReadIndices] = this.applyContextOptimizations(
apiConversationHistory,
conversationHistoryDeletedRange ? conversationHistoryDeletedRange[1] + 1 : 2,
timestamp,
)
let needToTruncate = true
if (anyContextUpdates) {
// determine whether we've saved enough chars to not truncate
const charactersSavedPercentage = this.calculateContextOptimizationMetrics(
apiConversationHistory,
conversationHistoryDeletedRange,
uniqueFileReadIndices,
)
if (charactersSavedPercentage >= 0.3) {
needToTruncate = false
}
}
if (needToTruncate) {
// go ahead with truncation
anyContextUpdates = this.applyStandardContextTruncationNoticeChange(timestamp) || anyContextUpdates
// NOTE: it's okay that we overwriteConversationHistory in resume task since we're only ever removing the last user message and not anything in the middle which would affect this range
conversationHistoryDeletedRange = this.getNextTruncationRange(
apiConversationHistory,
conversationHistoryDeletedRange,
keep,
)
updatedConversationHistoryDeletedRange = true
}
// if we alter the context history, save the updated version to disk
if (anyContextUpdates) {
await this.saveContextHistory(taskDirectory)
}
const { newHistory, summaryText } = await this._summarizeAndReplaceHistory(apiConversationHistory, api)
apiConversationHistory = newHistory
summary = summaryText
updatedConversationHistoryDeletedRange = true // This will signal to the task to save the new history range
}
}
}
@@ -184,6 +152,7 @@ export class ContextManager {
conversationHistoryDeletedRange: conversationHistoryDeletedRange,
updatedConversationHistoryDeletedRange: updatedConversationHistoryDeletedRange,
truncatedConversationHistory: truncatedConversationHistory,
summary,
}
}
@@ -848,4 +817,39 @@ export class ContextManager {
return percentCharactersSaved
}
private async _summarizeAndReplaceHistory(
apiConversationHistory: Anthropic.Messages.MessageParam[],
api: ApiHandler,
): Promise<{ newHistory: Anthropic.Messages.MessageParam[]; summaryText: string }> {
const systemPrompt = "You are a summarization expert." // A simple system prompt for the summarization task.
const userMessage: Anthropic.MessageParam = {
role: "user",
content: AUTO_SUMMARIZATION_PROMPT,
}
const conversationToSummarize = [...apiConversationHistory, userMessage]
const stream = api.createMessage(systemPrompt, conversationToSummarize)
let fullResponse = ""
for await (const chunk of stream) {
if (chunk.type === "text") {
fullResponse += chunk.text
}
}
const summaryMatch = fullResponse.match(/<summary>([\s\S]*?)<\/summary>/)
const summaryText = summaryMatch ? summaryMatch[1].trim() : "Could not extract summary."
const firstMessage = apiConversationHistory[0]
const summaryAssistantMessage: Anthropic.MessageParam = {
role: "assistant",
content: `This session is being continued from a previous conversation that ran out of context. The conversation is summarized below:\n${summaryText}.\n\nPlease continue the conversation from where we left it off without asking the user any further questions. Continue with the last task that you were asked to work on.`,
}
const newHistory: Anthropic.Messages.MessageParam[] = [firstMessage, summaryAssistantMessage]
return { newHistory, summaryText }
}
}
@@ -25,7 +25,7 @@ export function getContextWindowInfo(api: ApiHandler) {
maxAllowedSize = contextWindow - 30_000
break
case 200_000: // claude models
maxAllowedSize = contextWindow - 40_000
maxAllowedSize = contextWindow - 170_000
break
default:
maxAllowedSize = Math.max(contextWindow - 40_000, contextWindow * 0.8) // for deepseek, 80% of 64k meant only ~10k buffer which was too small and resulted in users getting context window errors.
@@ -0,0 +1,78 @@
export const AUTO_SUMMARIZATION_PROMPT = `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 <analysis> 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, etc
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. Problem Solving: Document problems solved and any ongoing troubleshooting efforts.
5. Pending Tasks: Outline any pending tasks that you have explicitly been asked to work on.
6. 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.
7. 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 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 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:
<example>
<analysis>
[Your thought process, ensuring all points are covered thoroughly and accurately]
</analysis>
<summary>
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. Problem Solving:
[Description of solved problems and ongoing troubleshooting]
5. Pending Tasks:
- [Task 1]
- [Task 2]
- [...]
6. Current Work:
[Precise description of current work]
7. Optional Next Step:
[Optional Next step to take]
</summary>
</example>
Please provide your summary based on the conversation so far, following this structure and ensuring precision and thoroughness in your response.
There may be additional summarization instructions provided in the included context. If so, remember to follow these instructions when creating the above summary. Examples of instructions include:
<example>
## Compact Instructions
When summarizing the conversation focus on typescript code changes and also remember the mistakes you made and how you fixed them.
</example>
<example>
# Summary instructions
When you are using compact - please focus on test output and code changes. Include file reads verbatim.
</example>
`
+7
View File
@@ -1700,6 +1700,13 @@ export class Task {
await ensureTaskDirectoryExists(this.getContext(), this.taskId),
)
if (contextManagementMetadata.summary) {
await this.say("auto_compact_summary", contextManagementMetadata.summary)
await this.saveCheckpoint()
this.apiConversationHistory = contextManagementMetadata.truncatedConversationHistory
await this.overwriteApiConversationHistory(this.apiConversationHistory)
}
if (contextManagementMetadata.updatedConversationHistoryDeletedRange) {
this.conversationHistoryDeletedRange = contextManagementMetadata.conversationHistoryDeletedRange
await this.saveClineMessagesAndUpdateHistory() // saves task history item which we use to keep track of conversation history deleted range
+1
View File
@@ -188,6 +188,7 @@ export type ClineSay =
| "checkpoint_created"
| "load_mcp_documentation"
| "info" // Added for general informational messages like retry status
| "auto_compact_summary"
export interface ClineSayTool {
tool:
@@ -96,6 +96,7 @@ function convertClineSayToProtoEnum(say: AppClineSay | undefined): ClineSay | un
checkpoint_created: ClineSay.CHECKPOINT_CREATED,
load_mcp_documentation: ClineSay.LOAD_MCP_DOCUMENTATION,
info: ClineSay.INFO,
auto_compact_summary: ClineSay.AUTO_COMPACT_SUMMARY,
}
const result = mapping[say]
@@ -139,6 +140,7 @@ function convertProtoEnumToClineSay(say: ClineSay): AppClineSay | undefined {
[ClineSay.CHECKPOINT_CREATED]: "checkpoint_created",
[ClineSay.LOAD_MCP_DOCUMENTATION]: "load_mcp_documentation",
[ClineSay.INFO]: "info",
[ClineSay.AUTO_COMPACT_SUMMARY]: "auto_compact_summary",
}
return mapping[say]
@@ -0,0 +1,34 @@
import React from "react"
import MarkdownBlock from "../common/MarkdownBlock"
import { WithCopyButton } from "./ChatRow"
interface AutoCompactSummaryProps {
summary: string
}
const AutoCompactSummary: React.FC<AutoCompactSummaryProps> = ({ summary }) => {
return (
<div>
<div
style={{
display: "flex",
alignItems: "center",
gap: "10px",
marginBottom: "12px",
}}>
<span
className="codicon codicon-history"
style={{
color: "var(--vscode-foreground)",
marginBottom: "-1.5px",
}}></span>
<span style={{ color: "var(--vscode-foreground)", fontWeight: "bold" }}>Conversation Summary</span>
</div>
<WithCopyButton textToCopy={summary}>
<MarkdownBlock markdown={summary} />
</WithCopyButton>
</div>
)
}
export default AutoCompactSummary
+4 -1
View File
@@ -36,6 +36,7 @@ import NewTaskPreview from "./NewTaskPreview"
import QuoteButton from "./QuoteButton"
import ReportBugPreview from "./ReportBugPreview"
import UserMessage from "./UserMessage"
import AutoCompactSummary from "./AutoCompactSummary"
interface CopyButtonProps {
textToCopy: string | undefined
@@ -70,7 +71,7 @@ const StyledContainer = styled.div`
}
`
const WithCopyButton = React.forwardRef<HTMLDivElement, WithCopyButtonProps>(
export const WithCopyButton = React.forwardRef<HTMLDivElement, WithCopyButtonProps>(
({ children, textToCopy, style, onMouseUp, ...props }, ref) => {
const [copied, setCopied] = useState(false)
@@ -1334,6 +1335,8 @@ export const ChatRowContent = ({
)}
</>
)
case "auto_compact_summary":
return <AutoCompactSummary summary={message.text || ""} />
case "shell_integration_warning":
return (
<>