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
This commit is contained in:
1 parent
68b93fcbea
commit
c2e91aa9c9
2 files changed
+84
-16
No files matched your search
@@ -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<boolean> {
|
||||
// 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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user