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 <nate@continue.dev>
This commit is contained in:
parent
e86bb5a3db
commit
14dd72eb21
3 changed files with 29 additions and 12 deletions
|
|
@ -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 (
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue