From c2e91aa9c9e0c6dede60ed9dd4fc26ee0c308ff4 Mon Sep 17 00:00:00 2001 From: Toshii <94262432+0xToshii@users.noreply.github.com> Date: Mon, 1 Dec 2025 12:23:58 -0800 Subject: [PATCH] running context rewriting prior to running auto compact (#7774) * running context rewriting prior to running auto compact * use sample timestamp as in getNewContextMessagesAndMetadata * clean up return var --- .../context-management/ContextManager.ts | 89 +++++++++++++++---- src/core/task/index.ts | 11 +++ 2 files changed, 84 insertions(+), 16 deletions(-) diff --git a/src/core/context/context-management/ContextManager.ts b/src/core/context/context-management/ContextManager.ts index ba05b51a..357598e8 100644 --- a/src/core/context/context-management/ContextManager.ts +++ b/src/core/context/context-management/ContextManager.ts @@ -206,26 +206,13 @@ export class ContextManager { // 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( + // Attempt file read optimization and check if we need to truncate + let { anyContextUpdates, needToTruncate } = this.attemptFileReadOptimizationCore( apiConversationHistory, - conversationHistoryDeletedRange ? conversationHistoryDeletedRange[1] + 1 : 2, + conversationHistoryDeletedRange, 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 @@ -588,6 +575,76 @@ export class ContextManager { return [contextHistoryUpdated, uniqueFileReadIndices] } + /** + * Private helper that attempts file read optimization and checks threshold. + */ + private attemptFileReadOptimizationCore( + apiConversationHistory: Anthropic.Messages.MessageParam[], + conversationHistoryDeletedRange: [number, number] | undefined, + timestamp: number, + ): { + anyContextUpdates: boolean + needToTruncate: boolean + } { + const startIndex = conversationHistoryDeletedRange ? conversationHistoryDeletedRange[1] + 1 : 2 + + const [anyContextUpdates, uniqueFileReadIndices] = this.applyContextOptimizations( + apiConversationHistory, + startIndex, + timestamp, + ) + + if (!anyContextUpdates) { + return { anyContextUpdates: false, needToTruncate: true } + } + + const percentSaved = this.calculateContextOptimizationMetrics( + apiConversationHistory, + conversationHistoryDeletedRange, + uniqueFileReadIndices, + ) + + return { + anyContextUpdates: true, + needToTruncate: percentSaved < 0.3, + } + } + + /** + * Public helper that attempts file read optimization and saves to disk. + */ + async attemptFileReadOptimization( + apiConversationHistory: Anthropic.Messages.MessageParam[], + conversationHistoryDeletedRange: [number, number] | undefined, + clineMessages: ClineMessage[], + previousApiReqIndex: number, + taskDirectory: string, + ): Promise { + // Extract timestamp using same logic as getNewContextMessagesAndMetadata + if (previousApiReqIndex < 0) { + return true + } + + const previousRequest = clineMessages[previousApiReqIndex] + if (!previousRequest || !previousRequest.text) { + return true + } + + const timestamp = previousRequest.ts + + const { anyContextUpdates, needToTruncate } = this.attemptFileReadOptimizationCore( + apiConversationHistory, + conversationHistoryDeletedRange, + timestamp, + ) + + if (anyContextUpdates) { + await this.saveContextHistory(taskDirectory) + } + + return needToTruncate + } + /** * Public function for triggering potentially setting the truncation message * If the truncation message already exists, does nothing, otherwise adds the message diff --git a/src/core/task/index.ts b/src/core/task/index.ts index 8d019f40..4292811e 100644 --- a/src/core/task/index.ts +++ b/src/core/task/index.ts @@ -2634,6 +2634,17 @@ export class Task { shouldCompact = false } } + + // Determine whether we can save enough tokens from context rewriting to skip auto-compact + if (shouldCompact) { + shouldCompact = await this.contextManager.attemptFileReadOptimization( + this.messageStateHandler.getApiConversationHistory(), + this.taskState.conversationHistoryDeletedRange, + this.messageStateHandler.getClineMessages(), + previousApiReqIndex, + await ensureTaskDirectoryExists(this.taskId), + ) + } } // when summarizing the context window, we do not want to inject updated to the context