fix: prevent over-summarization and improve logging

This commit is contained in:
Bo26fhmC5M
2025-01-12 17:15:11 +09:00
parent 6e49970b76
commit f08c31612b

View File

@@ -337,18 +337,10 @@ export async function hypaMemoryV3(
if (shouldReserveEmptyMemoryTokens) { if (shouldReserveEmptyMemoryTokens) {
currentTokens += emptyMemoryTokens; currentTokens += emptyMemoryTokens;
console.log( console.log("[HypaV3] Reserved empty memory tokens:", emptyMemoryTokens);
"[HypaV3] Reserved empty memory tokens:",
"\nTokens:",
emptyMemoryTokens
);
} else { } else {
currentTokens += memoryTokens; currentTokens += memoryTokens;
console.log( console.log("[HypaV3] Reserved max memory tokens:", memoryTokens);
"[HypaV3] Reserved max memory tokens:",
"\nTokens:",
memoryTokens
);
} }
// If summarization is needed // If summarization is needed
@@ -357,21 +349,21 @@ export async function hypaMemoryV3(
maxContextTokens * (1 - db.hypaV3Settings.extraSummarizationRatio); maxContextTokens * (1 - db.hypaV3Settings.extraSummarizationRatio);
while (summarizationMode) { while (summarizationMode) {
if ( if (currentTokens <= targetTokens) {
currentTokens <= targetTokens ||
(currentTokens <= maxContextTokens &&
chats.length - startIdx <= minChatsForSimilarity)
) {
break; break;
} }
if (chats.length - startIdx <= minChatsForSimilarity) { if (chats.length - startIdx <= minChatsForSimilarity) {
return { if (currentTokens <= maxContextTokens) {
currentTokens, break;
chats, } else {
error: `[HypaV3] Cannot summarize further: input token count (${currentTokens}) exceeds max context size (${maxContextTokens}), but minimum ${minChatsForSimilarity} messages required.`, return {
memory: toSerializableHypaV3Data(data), currentTokens,
}; chats,
error: `[HypaV3] Cannot summarize further: input token count (${currentTokens}) exceeds max context size (${maxContextTokens}), but minimum ${minChatsForSimilarity} messages required.`,
memory: toSerializableHypaV3Data(data),
};
}
} }
const toSummarize: OpenAIChat[] = []; const toSummarize: OpenAIChat[] = [];
@@ -379,9 +371,10 @@ export async function hypaMemoryV3(
startIdx + db.hypaV3Settings.maxChatsPerSummary, startIdx + db.hypaV3Settings.maxChatsPerSummary,
chats.length - minChatsForSimilarity chats.length - minChatsForSimilarity
); );
let toSummarizeTokens = 0;
console.log( console.log(
"[HypaV3] Starting summarization iteration:", "[HypaV3] Evaluating summarization batch:",
"\nCurrent Tokens:", "\nCurrent Tokens:",
currentTokens, currentTokens,
"\nMax Context Tokens:", "\nMax Context Tokens:",
@@ -412,7 +405,7 @@ export async function hypaMemoryV3(
chatTokens chatTokens
); );
currentTokens -= chatTokens; toSummarizeTokens += chatTokens;
if (i === 0 || !chat.content.trim()) { if (i === 0 || !chat.content.trim()) {
console.log( console.log(
@@ -427,6 +420,17 @@ export async function hypaMemoryV3(
toSummarize.push(chat); toSummarize.push(chat);
} }
// Stop summarization if further reduction would go below target tokens (unless we're over max tokens)
if (
currentTokens <= maxContextTokens &&
currentTokens - toSummarizeTokens < targetTokens
) {
console.log(
`[HypaV3] Stopping summarization: would reduce below target tokens (${currentTokens} - ${toSummarizeTokens} < ${targetTokens})`
);
break;
}
// Attempt summarization // Attempt summarization
let summarizationFailures = 0; let summarizationFailures = 0;
const stringifiedChats = toSummarize const stringifiedChats = toSummarize
@@ -468,11 +472,14 @@ export async function hypaMemoryV3(
break; break;
} }
currentTokens -= toSummarizeTokens;
startIdx = endIdx; startIdx = endIdx;
} }
console.log( console.log(
"[HypaV3] Finishing summarization:", `[HypaV3] ${
summarizationMode ? "Completed" : "Skipped"
} summarization phase:`,
"\nCurrent Tokens:", "\nCurrent Tokens:",
currentTokens, currentTokens,
"\nMax Context Tokens:", "\nMax Context Tokens:",
@@ -745,6 +752,9 @@ export async function hypaMemoryV3(
summaryTokens + consumedSimilarMemoryTokens > summaryTokens + consumedSimilarMemoryTokens >
reservedSimilarMemoryTokens reservedSimilarMemoryTokens
) { ) {
console.log(
`[HypaV3] Stopping similar memory selection: would exceed reserved tokens (${consumedSimilarMemoryTokens} + ${summaryTokens} > ${reservedSimilarMemoryTokens})`
);
break; break;
} }
@@ -798,8 +808,8 @@ export async function hypaMemoryV3(
selectedSummaries, selectedSummaries,
"\nReal Memory Tokens:", "\nReal Memory Tokens:",
realMemoryTokens, realMemoryTokens,
"\nCurrent Tokens:", "\nAvailable Memory Tokens:",
currentTokens availableMemoryTokens
); );
if (currentTokens > maxContextTokens) { if (currentTokens > maxContextTokens) {