From 481eee672e3f8e1fdb06f4e7081981d97d56e03b Mon Sep 17 00:00:00 2001 From: Diegox-17 Date: Thu, 12 Feb 2026 00:42:40 -0600 Subject: [PATCH] Fix LLM error by cleaning up CONSCIOUSLY message history Added logic to remove orphaned tool messages from history to prevent LLM errors. --- pkg/agent/context.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pkg/agent/context.go b/pkg/agent/context.go index e737fbd..e32e456 100644 --- a/pkg/agent/context.go +++ b/pkg/agent/context.go @@ -189,6 +189,17 @@ func (cb *ContextBuilder) BuildMessages(history []providers.Message, summary str systemPrompt += "\n\n## Summary of Previous Conversation\n\n" + summary } + //This fix prevents the session memory from LLM failure due to elimination of toolu_IDs required from LLM + // --- INICIO DEL FIX --- + //Diegox-17 + for len(history) > 0 && (history[0].Role == "tool") { + logger.DebugCF("agent", "Removing orphaned tool message from history to prevent LLM error", + map[string]interface{}{"role": history[0].Role}) + history = history[1:] + } + //Diegox-17 + // --- FIN DEL FIX --- + messages = append(messages, providers.Message{ Role: "system", Content: systemPrompt,