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.

Constrain plain Claude completion fallback

rcourtman6c8ecd228431
diff --git a/docs/release-control/v6/internal/subsystems/ai-runtime.md b/docs/release-control/v6/internal/subsystems/ai-runtime.md
index c07927afda..ced0ca7df1 100644
--- a/docs/release-control/v6/internal/subsystems/ai-runtime.md
+++ b/docs/release-control/v6/internal/subsystems/ai-runtime.md
@@ -300,11 +300,15 @@ provider prefixes rather than forwarding an invalid or cross-provider model
name to the local agent.
Claude receives that output schema through the trusted adapter system channel
and returns one JSON result that Pulse decodes with unknown fields and trailing
-values rejected whenever the model selects tools. A non-required turn may end
-with ordinary assistant prose instead; Pulse accepts that as a no-tool
-completion only after rejecting provider tool-call artifacts against the
-offered tool catalogue. Required-tool turns still fail when no structured call
-is returned. Pulse does not use Claude Code's hidden `--json-schema`
+values rejected whenever the model selects tools. After a successful
+`patrol_report_finding`, `patrol_assess_finding`, or `patrol_resolve_finding`
+result, the next non-required turn may end with ordinary assistant prose;
+Pulse accepts that as a no-tool completion only after rejecting provider
+tool-call artifacts against the offered tool catalogue. Plain content before a
+successful Patrol outcome remains an error, so prose wrapped around an
+unexecuted tool decision cannot silently turn a faulty run healthy.
+Required-tool turns still fail when no structured call is returned. Pulse does
+not use Claude Code's hidden `--json-schema`
retry loop: live qualification proved that wrapper could exhaust retries after
the model had already completed valid finding calls, incorrectly converting a
durable Patrol outcome into a provider failure. Codex may continue to use its
diff --git a/internal/ai/providers/subscription_agent.go b/internal/ai/providers/subscription_agent.go
index a87711fb7b..a6715c69dc 100644
--- a/internal/ai/providers/subscription_agent.go
+++ b/internal/ai/providers/subscription_agent.go
@@ -451,6 +451,9 @@ func decodeSubscriptionAgentTurn(agent SubscriptionAgent, raw []byte) (subscript
func decodeClaudePlainCompletion(req ChatRequest, raw []byte) (subscriptionAgentTurn, error) {
var turn subscriptionAgentTurn
+ if !followsSuccessfulPatrolOutcome(req) {
+ return turn, errors.New("Claude subscription response returned plain content before a successful Patrol outcome")
+ }
var wrapper claudePrintResponse
if err := json.Unmarshal(raw, &wrapper); err != nil {
return turn, fmt.Errorf("decode Claude subscription response: %w", err)
@@ -470,7 +473,7 @@ func decodeClaudePlainCompletion(req ChatRequest, raw []byte) (subscriptionAgent
toolNames = append(toolNames, tool.Name)
}
catalog := agentcapabilities.NewProviderToolNameCatalog(toolNames)
- if agentcapabilities.ProviderToolCallArtifactIndex(content, catalog) >= 0 {
+ if strings.Contains(content, `"tool_calls"`) || agentcapabilities.ProviderToolCallArtifactIndex(content, catalog) >= 0 {
return turn, errors.New("Claude subscription response leaked a tool call through plain content")
}
turn.Content = content
@@ -481,6 +484,30 @@ func decodeClaudePlainCompletion(req ChatRequest, raw []byte) (subscriptionAgent
return turn, nil
}
+func followsSuccessfulPatrolOutcome(req ChatRequest) bool {
+ if len(req.Messages) < 2 {
+ return false
+ }
+ last := req.Messages[len(req.Messages)-1]
+ if last.ToolResult == nil || last.ToolResult.IsError || strings.TrimSpace(last.ToolResult.ToolUseID) == "" {
+ return false
+ }
+ for i := len(req.Messages) - 2; i >= 0; i-- {
+ for _, call := range req.Messages[i].ToolCalls {
+ if call.ID != last.ToolResult.ToolUseID {
+ continue
+ }
+ switch call.Name {
+ case "patrol_report_finding", "patrol_assess_finding", "patrol_resolve_finding":
+ return true
+ default:
+ return false
+ }
+ }
+ }
+ return false
+}
+
func decodeStrictJSON(raw []byte, target interface{}) error {
decoder := json.NewDecoder(bytes.NewReader(raw))
decoder.DisallowUnknownFields()
diff --git a/internal/ai/providers/subscription_agent_test.go b/internal/ai/providers/subscription_agent_test.go
index d5d11e28f8..bbcbb5364f 100644
--- a/internal/ai/providers/subscription_agent_test.go
+++ b/internal/ai/providers/subscription_agent_test.go
@@ -350,7 +350,13 @@ func TestDecodeSubscriptionAgentTurnRejectsUnknownStructuredFields(t *testing.T)
}
func TestDecodeClaudePlainCompletionRejectsToolCallArtifacts(t *testing.T) {
- req := ChatRequest{Tools: []Tool{{Name: "pulse_query"}}}
+ req := ChatRequest{
+ Tools: []Tool{{Name: "pulse_query"}},
+ Messages: []Message{
+ {Role: "assistant", ToolCalls: []ToolCall{{ID: "finding-1", Name: "patrol_report_finding", Input: map[string]interface{}{}}}},
+ {Role: "user", ToolResult: &ToolResult{ToolUseID: "finding-1", Content: `{"ok":true}`}},
+ },
+ }
raw := []byte(`{"result":"Patrol completed without another tool call.","usage":{"input_tokens":4,"output_tokens":7}}`)
turn, err := decodeClaudePlainCompletion(req, raw)
if err != nil {
@@ -364,6 +370,15 @@ func TestDecodeClaudePlainCompletionRejectsToolCallArtifacts(t *testing.T) {
if _, err := decodeClaudePlainCompletion(req, leaked); err == nil || !strings.Contains(err.Error(), "leaked a tool call") {
t.Fatalf("plain tool-call leak error = %v", err)
}
+ embedded := []byte(`{"result":"Done. {\"content\":\"\",\"stop_reason\":\"tool_use\",\"tool_calls\":[{\"name\":\"pulse_query\"}]}"}`)
+ if _, err := decodeClaudePlainCompletion(req, embedded); err == nil || !strings.Contains(err.Error(), "leaked a tool call") {
+ t.Fatalf("embedded tool-call leak error = %v", err)
+ }
+
+ initial := ChatRequest{Tools: req.Tools}
+ if _, err := decodeClaudePlainCompletion(initial, raw); err == nil || !strings.Contains(err.Error(), "before a successful Patrol outcome") {
+ t.Fatalf("premature plain completion error = %v", err)
+ }
}
func writeExecutable(t *testing.T, path, body string) {