fix: also apply adjacency guard inside compressContext

compressContext calls fixToolPairs but was missing fixToolAdjacency and
stripTrailingAssistantOrphanToolUse, leaving orphan tool_use blocks when
context pruning splits tool_use/tool_result pairs.

Co-Authored-By: OpenClaude (mimo-v2.5-pro) <openclaude@gitlawb.com>
This commit is contained in:
oyi77
2026-05-19 04:12:33 +07:00
committed by diegosouzapw
parent 44d9abac96
commit 62652b1fc1

View File

@@ -266,6 +266,8 @@ function purifyHistory(messages: Record<string, unknown>[], 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<string, unknown>[], 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) {