Compare commits

...
8 changed files with 371 additions and 47 deletions
+1
View File
@@ -28,6 +28,7 @@ export const toolUseNames = [
"report_bug",
"new_rule",
"web_fetch",
"summarize_task",
] as const
// Converts array of tool call names into a union type ("execute_command" | "read_file" | ...)
@@ -8,6 +8,10 @@ import cloneDeep from "clone-deep"
import { ClineApiReqInfo, ClineMessage } from "@shared/ExtensionMessage"
import { ApiHandler } from "@api/index"
import { Anthropic } from "@anthropic-ai/sdk"
import { findLastIndex } from "@shared/array"
import { TaskState } from "../../task/TaskState"
import { summarizeTask } from "../../prompts/contextManagement"
enum EditType {
UNDEFINED = 0,
@@ -115,6 +119,7 @@ export class ContextManager {
conversationHistoryDeletedRange: [number, number] | undefined,
previousApiReqIndex: number,
taskDirectory: string,
taskState: TaskState,
) {
let updatedConversationHistoryDeletedRange = false
@@ -122,56 +127,11 @@ export class ContextManager {
if (previousApiReqIndex >= 0) {
const previousRequest = clineMessages[previousApiReqIndex]
if (previousRequest && previousRequest.text) {
const timestamp = previousRequest.ts
const { tokensIn, tokensOut, cacheWrites, cacheReads }: ClineApiReqInfo = JSON.parse(previousRequest.text)
const totalTokens = (tokensIn || 0) + (tokensOut || 0) + (cacheWrites || 0) + (cacheReads || 0)
const { maxAllowedSize } = getContextWindowInfo(api)
const { maxAllowedSize, contextWindow } = 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)
}
}
// Context window management logic can be added here if needed
}
}
@@ -848,4 +808,52 @@ export class ContextManager {
return percentCharactersSaved
}
/**
* Check if conversation should trigger automatic summarization based on token usage
*/
public shouldTriggerSummarization(
apiConversationHistory: Anthropic.Messages.MessageParam[],
clineMessages: ClineMessage[],
api: ApiHandler,
): {
totalTokens: number
maxAllowedSize: number
contextWindow: number
shouldSummarize: boolean
} {
let totalTokens = 0
let maxAllowedSize = 0
let contextWindow = 0
let shouldSummarize = false
const lastApiReqIndex = findLastIndex(clineMessages, (m) => m.say === "api_req_started")
if (lastApiReqIndex >= 0) {
const previousRequest = clineMessages[lastApiReqIndex]
if (previousRequest?.text) {
try {
const apiReqInfo: ClineApiReqInfo = JSON.parse(previousRequest.text)
const { tokensIn = 0, tokensOut = 0, cacheWrites = 0, cacheReads = 0 } = apiReqInfo
totalTokens = tokensIn + tokensOut + cacheWrites + cacheReads
const info = getContextWindowInfo(api)
maxAllowedSize = info.maxAllowedSize
contextWindow = info.contextWindow
shouldSummarize = totalTokens >= maxAllowedSize
} catch (error) {
console.error("Error parsing API request info for summarization threshold check:", error)
}
}
}
return {
totalTokens,
maxAllowedSize,
contextWindow,
shouldSummarize,
}
}
}
+70
View File
@@ -0,0 +1,70 @@
export const summarizeTask = (totalTokens: number, maxAllowedSize: number, contextWindow: number) =>
`<explicit_instructions type="summarize_task">
The current conversation is rapidly running out of context (${totalTokens}/${contextWindow} tokens used). Now, your urgent task is to create a detailed, comprehensive 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 <thinking> 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.
Usage:
<summarize_task>
<context>Your detailed summary</context>
</summarize_task>
Here's an example of how your output should be structured:
<example>
<thinking>
[Your thought process, ensuring all points are covered thoroughly and accurately]
</thinking>
<summarize_task>
<context>
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]
</context>
</summarize_task>
</example>
</explicit_instructions>\n
`
export const continuationPrompt = (summaryText: string) => `
This session is being continued from a previous conversation that ran out of context. The conversation is summarized below:
${summaryText}.
Please 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.
`
+1
View File
@@ -41,6 +41,7 @@ export class TaskState {
didRejectTool = false
didAlreadyUseTool = false
didEditFile: boolean = false
currentlySummarizing = false
// Consecutive request tracking
consecutiveAutoApprovedRequestsCount: number = 0
+113
View File
@@ -56,6 +56,9 @@ import { AutoApprove } from "./tools/autoApprove"
import { showNotificationForApprovalIfAutoApprovalEnabled } from "./utils"
import { ChatSettings } from "@/shared/ChatSettings"
import { summarizeTask, continuationPrompt } from "../prompts/contextManagement"
import { getContextWindowInfo } from "../context/context-management/context-window-utils"
export class ToolExecutor {
private autoApprover: AutoApprove
@@ -125,6 +128,7 @@ export class ToolExecutor {
private pushToolResult = (content: ToolResponse, block: ToolUse) => {
const isNextGenModel = isClaude4ModelFamily(this.api) || isGemini2dot5ModelFamily(this.api)
// 1. ADD NORMAL TOOL RESULT (existing logic)
if (typeof content === "string") {
const resultText = content || "(tool did not return anything)"
@@ -148,6 +152,7 @@ export class ToolExecutor {
} else {
this.taskState.userMessageContent.push(...content)
}
// once a tool result has been collected, ignore all other tool uses since we should only ever present one tool result per message
this.taskState.didAlreadyUseTool = true
}
@@ -188,6 +193,8 @@ export class ToolExecutor {
return `[${block.name} for creating a new task]`
case "condense":
return `[${block.name}]`
case "summarize_task":
return `[${block.name}]`
case "report_bug":
return `[${block.name}]`
case "new_rule":
@@ -1772,6 +1779,112 @@ export class ToolExecutor {
break
}
}
case "summarize_task": {
const context: string | undefined = block.params.context
try {
if (block.partial) {
// Show streaming summary generation in tool UI
const partialMessage = JSON.stringify({
tool: "summarizeTask",
content: this.removeClosingTag(block, "context", context),
} satisfies ClineSayTool)
await this.say("tool", partialMessage, undefined, undefined, block.partial)
break
} else {
if (!context) {
this.taskState.consecutiveMistakeCount++
this.pushToolResult(await this.sayAndCreateMissingParamError("summarize_task", "context"), block)
await this.saveCheckpoint()
break
}
this.taskState.consecutiveMistakeCount = 0
// Show completed summary in tool UI
const completeMessage = JSON.stringify({
tool: "summarizeTask",
content: context,
} satisfies ClineSayTool)
await this.say("tool", completeMessage, undefined, undefined, false)
// Auto-execute conversation replacement (no user approval needed)
// Clear the existing user message content that triggered the summary.
this.taskState.userMessageContent = []
this.pushToolResult(formatResponse.toolResult(continuationPrompt(context)), block)
// Replace conversation history (same logic as condense when approved)
const apiConversationHistory = this.messageStateHandler.getApiConversationHistory()
const keepStrategy = "none"
// Clear the context history at this point in time
this.taskState.conversationHistoryDeletedRange = this.contextManager.getNextTruncationRange(
apiConversationHistory,
this.taskState.conversationHistoryDeletedRange,
keepStrategy,
)
// Actually apply the truncation to the stored conversation history
const truncatedHistory = this.contextManager.getTruncatedMessages(
apiConversationHistory,
this.taskState.conversationHistoryDeletedRange,
)
await this.messageStateHandler.overwriteApiConversationHistory(truncatedHistory)
// Clear the deleted range now that it has been applied
this.taskState.conversationHistoryDeletedRange = undefined
await this.messageStateHandler.saveClineMessagesAndUpdateHistory()
console.log("TOOLEXECUTOR: ARE WE CURRENTLY SUMMARIZING?", this.taskState.currentlySummarizing)
// LOG: Debug what actually happens after summarization
console.log("=== SUMMARIZATION COMPLETE - DEBUG INFO ===")
console.log("Deleted Range:", this.taskState.conversationHistoryDeletedRange)
const currentApiHistory = this.messageStateHandler.getApiConversationHistory()
console.log("Total API messages after truncation:", currentApiHistory.length)
// Log the structure of what remains
currentApiHistory.forEach((msg, index) => {
const preview = Array.isArray(msg.content)
? msg.content
.map((block) => {
if (block.type === "text") {
return `text(${block.text.length} chars): "${block.text.substring(0, 100)}..."`
}
return `${block.type}`
})
.join(", ")
: typeof msg.content === "string"
? `string(${msg.content.length} chars): "${msg.content.substring(0, 100)}..."`
: "other"
console.log(`Message ${index} (${msg.role}): ${preview}`)
})
// Log what will be in the next API request
console.log("Current user message content that will be sent:")
this.taskState.userMessageContent.forEach((content, index) => {
if (content.type === "text") {
console.log(
`UserContent ${index}: text(${content.text.length} chars): "${content.text.substring(0, 200)}..."`,
)
} else {
console.log(`UserContent ${index}: ${content.type}`)
}
})
console.log("=== END SUMMARIZATION DEBUG ===")
}
await this.saveCheckpoint()
break
} catch (error) {
// Reset flag on error to prevent getting stuck
this.taskState.currentlySummarizing = false
await this.handleError("summarizing conversation", error, block)
await this.saveCheckpoint()
break
}
}
case "condense": {
const context: string | undefined = block.params.context
try {
+55
View File
@@ -82,6 +82,7 @@ import { isClaude4ModelFamily, isGemini2dot5ModelFamily } from "@utils/model-uti
import { isInTestMode } from "../../services/test/TestMode"
import { ensureLocalClineDirExists } from "../context/instructions/user-instructions/rule-helpers"
import { refreshWorkflowToggles } from "../context/instructions/user-instructions/workflows"
import { summarizeTask } from "../prompts/contextManagement"
import { MessageStateHandler } from "./message-state"
import { TaskState } from "./TaskState"
import { ToolExecutor } from "./ToolExecutor"
@@ -1744,6 +1745,7 @@ export class Task {
this.taskState.conversationHistoryDeletedRange,
previousApiReqIndex,
await ensureTaskDirectoryExists(this.getContext(), this.taskId),
this.taskState,
)
if (contextManagementMetadata.updatedConversationHistoryDeletedRange) {
@@ -1752,6 +1754,30 @@ export class Task {
// saves task history item which we use to keep track of conversation history deleted range
}
// LOG: Debug what messages are ACTUALLY sent to the API
console.log("=== ACTUAL API REQUEST MESSAGES ===")
console.log("System prompt length:", systemPrompt.length)
console.log("Total messages being sent to API:", contextManagementMetadata.truncatedConversationHistory.length)
console.log("Conversation history deleted range:", this.taskState.conversationHistoryDeletedRange)
contextManagementMetadata.truncatedConversationHistory.forEach((msg, index) => {
const preview = Array.isArray(msg.content)
? msg.content
.map((block) => {
if (block.type === "text") {
return `text(${block.text.length} chars): "${block.text.substring(0, 100)}..."`
}
return `${block.type}`
})
.join(", ")
: typeof msg.content === "string"
? `string(${msg.content.length} chars): "${msg.content.substring(0, 100)}..."`
: "other"
console.log(`API Message ${index} (${msg.role}): ${preview}`)
})
console.log("=== END API REQUEST MESSAGES ===")
let stream = this.api.createMessage(systemPrompt, contextManagementMetadata.truncatedConversationHistory)
const iterator = stream[Symbol.asyncIterator]()
@@ -2096,6 +2122,35 @@ export class Task {
// get previous api req's index to check token usage and determine if we need to truncate conversation history
const previousApiReqIndex = findLastIndex(this.messageStateHandler.getClineMessages(), (m) => m.say === "api_req_started")
console.log("ARE WE CURRENTLY SUMMARIZING?", this.taskState.currentlySummarizing)
// CHECK IF WE NEED SUMMARIZATION (only if not already summarizing)
if (this.taskState.currentlySummarizing) {
this.taskState.currentlySummarizing = false
} else {
const { totalTokens, maxAllowedSize, contextWindow, shouldSummarize } =
this.contextManager.shouldTriggerSummarization(
this.messageStateHandler.getApiConversationHistory(),
this.messageStateHandler.getClineMessages(),
this.api,
)
if (shouldSummarize) {
console.log("----------- WE SHOULD SUMMARIZE, SENDING SUMMARIZATION REQUEST -------------")
console.log(
`Total Tokens: ${totalTokens}, Max Allowed Size: ${maxAllowedSize}, Context Window: ${contextWindow}, Should Summarize: ${shouldSummarize}`,
)
console.log("----------- WE SHOULD SUMMARIZE, SENDING SUMMARIZATION REQUEST -------------")
// SET FLAG AND ADD SUMMARIZATION PROMPT
this.taskState.currentlySummarizing = true
userContent.push({
type: "text",
text: summarizeTask(totalTokens, maxAllowedSize, contextWindow),
})
}
}
// Save checkpoint if this is the first API request
const isFirstRequest = this.messageStateHandler.getClineMessages().filter((m) => m.say === "api_req_started").length === 0
+1
View File
@@ -136,6 +136,7 @@ export interface ClineSayTool {
| "listCodeDefinitionNames"
| "searchFiles"
| "webFetch"
| "summarizeTask"
path?: string
diff?: string
content?: string
@@ -584,6 +584,81 @@ export const ChatRowContent = memo(
/>
</>
)
case "summarizeTask":
return (
<>
<div style={headerStyle}>
{toolIcon("book")}
<span style={{ fontWeight: "bold" }}>Cline is condensing the conversation:</span>
</div>
<div
style={{
borderRadius: 3,
backgroundColor: CODE_BLOCK_BG_COLOR,
overflow: "hidden",
border: "1px solid var(--vscode-editorGroup-border)",
}}>
<div
style={{
color: "var(--vscode-descriptionForeground)",
padding: "9px 10px",
cursor: "pointer",
userSelect: "none",
WebkitUserSelect: "none",
MozUserSelect: "none",
msUserSelect: "none",
}}
onClick={handleToggle}>
{isExpanded ? (
<div>
<div style={{ display: "flex", alignItems: "center", marginBottom: "8px" }}>
<span style={{ fontWeight: "bold", marginRight: "4px" }}>Summary:</span>
<div style={{ flexGrow: 1 }}></div>
<span
className="codicon codicon-chevron-up"
style={{
fontSize: 13.5,
margin: "1px 0",
}}></span>
</div>
<span
className="ph-no-capture"
style={{
whiteSpace: "pre-wrap",
wordBreak: "break-word",
overflowWrap: "anywhere",
}}>
{tool.content}
</span>
</div>
) : (
<div style={{ display: "flex", alignItems: "center" }}>
<span
className="ph-no-capture"
style={{
whiteSpace: "nowrap",
overflow: "hidden",
textOverflow: "ellipsis",
marginRight: "8px",
direction: "rtl",
textAlign: "left",
flex: 1,
}}>
{tool.content + "\u200E"}
</span>
<span
className="codicon codicon-chevron-down"
style={{
fontSize: 13.5,
margin: "1px 0",
flexShrink: 0,
}}></span>
</div>
)}
</div>
</div>
</>
)
case "webFetch":
return (
<>