Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

fix(ai): skip orphaned tool calls when pruning messages

rcourtmana1fd2c4ddc7a
diff --git a/internal/ai/chat/agentic.go b/internal/ai/chat/agentic.go
index 2ff0263b63..c50546a796 100644
--- a/internal/ai/chat/agentic.go
+++ b/internal/ai/chat/agentic.go
@@ -1251,10 +1251,23 @@ func pruneMessagesForModel(messages []Message) []Message {
start := len(messages) - MaxContextMessagesLimit
pruned := messages[start:]
+
+ // Skip leading tool results (orphaned from pruned tool calls)
for len(pruned) > 0 && pruned[0].ToolResult != nil {
pruned = pruned[1:]
}
+ // If we start with an assistant message that has tool calls,
+ // skip it and its following tool results — we've pruned the
+ // user message that preceded it, so the sequence is broken.
+ for len(pruned) > 0 && pruned[0].Role == "assistant" && len(pruned[0].ToolCalls) > 0 {
+ pruned = pruned[1:]
+ // Also skip the tool results that followed
+ for len(pruned) > 0 && pruned[0].ToolResult != nil {
+ pruned = pruned[1:]
+ }
+ }
+
return pruned
}