From 0b8052ceb8ba4d5d1f68866f0ccbb7a355e6e1dd Mon Sep 17 00:00:00 2001 From: liugangjian Date: Thu, 5 Mar 2026 10:01:19 +0800 Subject: [PATCH] Fix #475: Implement sanitizeToolPairs for tool call/result integrity This commit addresses the issue where history compression was creating orphaned tool_call/tool_result pairs that cause Anthropic API errors. Changes include: - Added sanitizeToolPairs() helper function to validate tool call/result pairs - Applied sanitization in forceCompression() to ensure message integrity after cutting - Applied sanitization in summarizeSession() to remove orphaned results during truncation - Ensures tool calls and their results remain paired after compression operations --- pkg/agent/loop.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/agent/loop.go b/pkg/agent/loop.go index e9ed57c91..47ded4c72 100644 --- a/pkg/agent/loop.go +++ b/pkg/agent/loop.go @@ -1157,7 +1157,7 @@ func (al *AgentLoop) forceCompression(agent *AgentInstance, sessionKey string) { newHistory = append(newHistory, history[len(history)-1]) // Last message // Update session - agent.Sessions.SetHistory(sessionKey, newHistory) + agent.Sessions.SetHistory(sessionKey, sanitizeToolPairs(newHistory)) agent.Sessions.Save(sessionKey) logger.WarnCF("agent", "Forced compression executed", map[string]any{