diff --git a/open-sse/services/contextManager.ts b/open-sse/services/contextManager.ts index d4d48292ea..2834eaa26a 100644 --- a/open-sse/services/contextManager.ts +++ b/open-sse/services/contextManager.ts @@ -266,6 +266,8 @@ function purifyHistory(messages: Record[], targetTokens: number while (keep > 2) { let candidate = [...system, ...nonSystem.slice(-keep)]; candidate = fixToolPairs(candidate); + candidate = fixToolAdjacency(candidate); + candidate = stripTrailingAssistantOrphanToolUse(candidate); const tokens = estimateTokens(JSON.stringify(candidate)); if (tokens <= targetTokens) break; keep = Math.max(2, Math.floor(keep * 0.7)); // Drop 30% each iteration @@ -273,6 +275,8 @@ function purifyHistory(messages: Record[], targetTokens: number let result = [...system, ...nonSystem.slice(-keep)]; result = fixToolPairs(result); + result = fixToolAdjacency(result); + result = stripTrailingAssistantOrphanToolUse(result); // Add summary of dropped messages if (keep < nonSystem.length) {