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;