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: keep main-loop leaked tool call from ending an investigation

rcourtmanb56568b85165
diff --git a/docs/release-control/v6/internal/subsystems/ai-runtime.md b/docs/release-control/v6/internal/subsystems/ai-runtime.md
index 1ced8c9a36..4ad4955225 100644
--- a/docs/release-control/v6/internal/subsystems/ai-runtime.md
+++ b/docs/release-control/v6/internal/subsystems/ai-runtime.md
@@ -8095,6 +8095,12 @@ so the persisted message matches the stream, and falls back only when that
prefix is empty. Proof: `TestInteractiveFinalSummaryKeepsCleanPrefixBeforeLeakedToolCall`
and `TestInvestigationFinalSummaryWithLeakedToolCallIsNotAConclusion` in
`internal/ai/chat/agentic_final_failure_test.go`.
+The same rule holds for a main-loop turn with no structured tool calls whose
+content leaked a textual tool call: a Patrol investigation drops that prefix and
+takes the bounded summary turn and failure receipt, while interactive chat keeps
+it. Proof: `TestInvestigationMainLoopLeakedToolCallIsNotAConclusion` and
+`TestInteractiveMainLoopKeepsCleanPrefixBeforeLeakedToolCall` in
+`internal/ai/chat/agentic_main_loop_leak_test.go`.
The private orchestrator clears the previous finding outcome when starting a
new session, after retaining its prior context. Failed runs preserve evidence,
diff --git a/internal/ai/chat/agentic.go b/internal/ai/chat/agentic.go
index 961e9e2537..792ed4976d 100644
--- a/internal/ai/chat/agentic.go
+++ b/internal/ai/chat/agentic.go
@@ -1463,6 +1463,22 @@ func (a *AgenticLoop) executeWithTools(ctx context.Context, sessionID string, me
continue
}
+ if suppressLeakedToolContent && isPatrolInvestigationExecution(a.currentExecutionProfile()) {
+ // The model serialized a tool call as prose instead of concluding,
+ // so the clean prefix is intent ("next I will check..."), not a
+ // conclusion. The orchestrator would read that unmarked prose as
+ // NEEDS_ATTENTION, which is terminal. Drop it and take the same
+ // bounded summary turn and failure receipt as an empty final turn.
+ // Interactive chat keeps the prefix it already streamed.
+ log.Warn().Str("session_id", sessionID).Msg("[AgenticLoop] Investigation turn leaked a tool call instead of concluding")
+ if len(resultMessages) > 0 {
+ resultMessages[len(resultMessages)-1].Content = ""
+ }
+ if len(providerMessages) > 0 {
+ providerMessages[len(providerMessages)-1].Content = ""
+ }
+ }
+
log.Debug().Msg("agentic loop complete - no tool calls")
return a.ensureFinalTextResponse(ctx, sessionID, resultMessages, providerMessages, callback)
}
diff --git a/internal/ai/chat/agentic_main_loop_leak_test.go b/internal/ai/chat/agentic_main_loop_leak_test.go
new file mode 100644
index 0000000000..44b02afbc4
--- /dev/null
+++ b/internal/ai/chat/agentic_main_loop_leak_test.go
@@ -0,0 +1,98 @@
+package chat
+
+import (
+ "context"
+ "strings"
+ "testing"
+
+ "github.com/rcourtman/pulse-go-rewrite/internal/ai/providers"
+ "github.com/rcourtman/pulse-go-rewrite/internal/ai/tools"
+)
+
+const mainLoopLeakedToolCall = `pulse_read(target_host="current_resource", command="df -h")`
+
+// mainLoopLeakProvider makes the first (tool-enabled) turn stream a clean
+// prefix followed by a leaked textual tool call, with no structured tool
+// calls. Any later turn streams summary, if set.
+func mainLoopLeakProvider(prefix, summary string, calls *int) *stubStreamingProvider {
+ return &stubStreamingProvider{chatStream: func(ctx context.Context, req providers.ChatRequest, cb providers.StreamCallback) error {
+ *calls++
+ if *calls == 1 {
+ cb(providers.StreamEvent{Type: "content", Data: providers.ContentEvent{Text: prefix}})
+ cb(providers.StreamEvent{Type: "content", Data: providers.ContentEvent{Text: mainLoopLeakedToolCall}})
+ } else if summary != "" {
+ cb(providers.StreamEvent{Type: "content", Data: providers.ContentEvent{Text: summary}})
+ }
+ cb(providers.StreamEvent{Type: "done", Data: providers.DoneEvent{}})
+ return nil
+ }}
+}
+
+func TestInteractiveMainLoopKeepsCleanPrefixBeforeLeakedToolCall(t *testing.T) {
+ prefix := "The root filesystem on pve1 is 92% full, driven by /var/log. "
+ calls := 0
+ loop := NewAgenticLoop(mainLoopLeakProvider(prefix, "unexpected summary", &calls), tools.NewPulseToolExecutor(tools.ExecutorConfig{}), "Answer the user")
+ var events []StreamEvent
+ messages, err := loop.ExecuteWithTools(context.Background(), "main-leak-chat", []Message{{Role: "user", Content: "Why is pve1 alerting?"}}, nil, func(event StreamEvent) {
+ events = append(events, event)
+ })
+ if err != nil {
+ t.Fatalf("interactive chat failed on a usable narrative: %v", err)
+ }
+ if calls != 1 {
+ t.Fatalf("provider calls=%d, want the streamed answer without a summary turn", calls)
+ }
+ if len(messages) == 0 {
+ t.Fatal("no messages returned")
+ }
+ final := messages[len(messages)-1].Content
+ if strings.TrimSpace(final) != strings.TrimSpace(prefix) {
+ t.Fatalf("persisted final message = %q, want the clean streamed prefix %q", final, prefix)
+ }
+ if streamed := streamedContent(t, events); streamed != final {
+ t.Fatalf("streamed content %q differs from persisted message %q", streamed, final)
+ }
+}
+
+func TestInvestigationMainLoopLeakedToolCallIsNotAConclusion(t *testing.T) {
+ // The orchestrator classifies unmarked prose as NEEDS_ATTENTION, which is
+ // terminal, so a "next I will check" prefix cut off by a leaked tool call
+ // must not end the investigation. It takes the same bounded summary turn
+ // as an empty final turn, and the failure receipt if that yields nothing.
+ prefix := "I will check disk usage on pve1 next. "
+ calls := 0
+ loop := NewAgenticLoop(mainLoopLeakProvider(prefix, "", &calls), tools.NewPulseToolExecutor(tools.ExecutorConfig{}), "Investigate")
+ loop.SetExecutionProfile(tools.ProfilePatrolInvestigation)
+ messages, err := loop.ExecuteWithTools(context.Background(), "main-leak-investigation", []Message{{Role: "user", Content: "Check"}}, nil, func(StreamEvent) {})
+ if err == nil {
+ t.Fatal("leaked tool-call turn became a completed investigation")
+ }
+ if calls != 2 {
+ t.Fatalf("provider calls=%d, want original plus bounded summary", calls)
+ }
+ if len(messages) == 0 || !strings.Contains(messages[len(messages)-1].Content, "final narrative") {
+ t.Fatalf("missing retained fallback receipt: %#v", messages)
+ }
+ for _, msg := range messages {
+ if strings.Contains(msg.Content, strings.TrimSpace(prefix)) {
+ t.Fatalf("leaked turn prefix persisted as investigation output: %#v", messages)
+ }
+ }
+}
+
+func TestInvestigationMainLoopLeakedToolCallRecoversWithRealConclusion(t *testing.T) {
+ conclusion := "### Conclusion\nNEEDS_ATTENTION: /var/log on pve1 needs rotation by the owner"
+ calls := 0
+ loop := NewAgenticLoop(mainLoopLeakProvider("I will check disk usage on pve1 next. ", conclusion, &calls), tools.NewPulseToolExecutor(tools.ExecutorConfig{}), "Investigate")
+ loop.SetExecutionProfile(tools.ProfilePatrolInvestigation)
+ messages, err := loop.ExecuteWithTools(context.Background(), "main-leak-recovered", []Message{{Role: "user", Content: "Check"}}, nil, func(StreamEvent) {})
+ if err != nil {
+ t.Fatalf("real summary conclusion was rejected: %v", err)
+ }
+ if calls != 2 {
+ t.Fatalf("provider calls=%d, want original plus bounded summary", calls)
+ }
+ if got := messages[len(messages)-1].Content; got != conclusion {
+ t.Fatalf("final message = %q, want the summary conclusion", got)
+ }
+}