From 14dd72eb21be8b11f0c22e5145d11366afca7c08 Mon Sep 17 00:00:00 2001 From: "continue[bot]" Date: Mon, 1 Dec 2025 23:03:38 +0000 Subject: [PATCH] Fix compaction and token counting issues from Cubic review Address code review feedback: 1. Add fallback to use compacted history directly when ChatHistoryService is unavailable (fixes P1 issue in handlePostToolValidation and handleNormalAutoCompaction) 2. Prevent double-counting of system message tokens by checking if system message is already in history before subtracting tokens 3. Remove tool output name from token count since it's not sent to model This ensures compaction works correctly in headless contexts and provides more accurate token estimation. Co-authored-by: nate --- extensions/cli/src/compaction.ts | 12 +++++++-- .../streamChatResponse.compactionHelpers.ts | 25 +++++++++++++------ extensions/cli/src/util/tokenizer.ts | 4 +-- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/extensions/cli/src/compaction.ts b/extensions/cli/src/compaction.ts index 62324b712..c52ba0147 100644 --- a/extensions/cli/src/compaction.ts +++ b/extensions/cli/src/compaction.ts @@ -62,10 +62,18 @@ export async function compactChatHistory( const reservedForOutput = maxTokens === undefined ? Math.ceil(contextLimit * 0.35) : maxTokens; - // Account for system message AND safety buffer + // Check if system message is already in the history to avoid double-counting + const hasSystemMessageInHistory = chatHistory.some( + (item) => item.message.role === "system", + ); + + // Account for system message (if not already in history) AND safety buffer const SAFETY_BUFFER = 100; + const systemMessageReservation = hasSystemMessageInHistory + ? 0 + : systemMessageTokens; const availableForInput = - contextLimit - reservedForOutput - systemMessageTokens - SAFETY_BUFFER; + contextLimit - reservedForOutput - systemMessageReservation - SAFETY_BUFFER; // Check if we need to prune to fit within context while ( diff --git a/extensions/cli/src/stream/streamChatResponse.compactionHelpers.ts b/extensions/cli/src/stream/streamChatResponse.compactionHelpers.ts index f7802b74a..722e384a3 100644 --- a/extensions/cli/src/stream/streamChatResponse.compactionHelpers.ts +++ b/extensions/cli/src/stream/streamChatResponse.compactionHelpers.ts @@ -116,13 +116,19 @@ export async function handlePostToolValidation( systemMessage, }); - if (wasCompacted && chatHistorySvc) { - chatHistorySvc.setHistory(compactedHistory); - chatHistory = chatHistorySvc.getHistory(); + if (wasCompacted) { + // Use the service to update history if available, otherwise use local copy + if (chatHistorySvc && typeof chatHistorySvc.setHistory === "function") { + chatHistorySvc.setHistory(compactedHistory); + chatHistory = chatHistorySvc.getHistory(); + } else { + // Fallback: use the compacted history directly when service unavailable + chatHistory = compactedHistory; + } // Verify compaction brought us under the limit const postCompactionValidation = validateContextLength( - [postToolSystemItem, ...compactedHistory], + [postToolSystemItem, ...chatHistory], model, SAFETY_BUFFER, ); @@ -188,9 +194,14 @@ export async function handleNormalAutoCompaction( systemMessage, }); - if (wasCompacted && chatHistorySvc) { - chatHistorySvc.setHistory(updatedChatHistory); - return chatHistorySvc.getHistory(); + if (wasCompacted) { + // Use the service to update history if available, otherwise use local copy + if (chatHistorySvc && typeof chatHistorySvc.setHistory === "function") { + chatHistorySvc.setHistory(updatedChatHistory); + return chatHistorySvc.getHistory(); + } + // Fallback: return the compacted history directly when service unavailable + return updatedChatHistory; } return chatHistory; diff --git a/extensions/cli/src/util/tokenizer.ts b/extensions/cli/src/util/tokenizer.ts index 272d9b7e7..cccc60ad2 100644 --- a/extensions/cli/src/util/tokenizer.ts +++ b/extensions/cli/src/util/tokenizer.ts @@ -77,9 +77,7 @@ function countToolOutputTokens( if (item.content) { tokenCount += encode(item.content).length; } - if (item.name) { - tokenCount += encode(item.name).length; - } + // Note: item.name is not sent to the model, only used for internal tracking tokenCount += 5; // Output structure overhead } return tokenCount;