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.

Accept safe plain Claude completion turns

rcourtman1439495e70c0
diff --git a/docs/release-control/v6/internal/subsystems/ai-runtime.md b/docs/release-control/v6/internal/subsystems/ai-runtime.md
index aa1d5b574f..c07927afda 100644
--- a/docs/release-control/v6/internal/subsystems/ai-runtime.md
+++ b/docs/release-control/v6/internal/subsystems/ai-runtime.md
@@ -300,7 +300,11 @@ 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. Pulse does not use Claude Code's hidden `--json-schema`
+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`
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 65b65666bc..a87711fb7b 100644
--- a/internal/ai/providers/subscription_agent.go
+++ b/internal/ai/providers/subscription_agent.go
@@ -13,6 +13,7 @@ import (
"strings"
"time"
+ "github.com/rcourtman/pulse-go-rewrite/internal/agentcapabilities"
"github.com/rcourtman/pulse-go-rewrite/internal/config"
)
@@ -234,6 +235,9 @@ func (c *SubscriptionAgentClient) Chat(ctx context.Context, req ChatRequest) (*C
}
turn, err := decodeSubscriptionAgentTurn(c.agent, raw)
+ if err != nil && c.agent == SubscriptionAgentClaude {
+ turn, err = decodeClaudePlainCompletion(req, raw)
+ }
if err != nil {
return nil, err
}
@@ -445,6 +449,38 @@ func decodeSubscriptionAgentTurn(agent SubscriptionAgent, raw []byte) (subscript
return turn, nil
}
+func decodeClaudePlainCompletion(req ChatRequest, raw []byte) (subscriptionAgentTurn, error) {
+ var turn subscriptionAgentTurn
+ var wrapper claudePrintResponse
+ if err := json.Unmarshal(raw, &wrapper); err != nil {
+ return turn, fmt.Errorf("decode Claude subscription response: %w", err)
+ }
+ if len(wrapper.PermissionDenials) > 0 {
+ return turn, errors.New("Claude subscription agent attempted a denied built-in tool")
+ }
+ if len(wrapper.StructuredOutput) > 0 && string(wrapper.StructuredOutput) != "null" {
+ return turn, errors.New("Claude subscription response contained invalid structured output")
+ }
+ content := strings.TrimSpace(wrapper.Result)
+ if content == "" {
+ return turn, errors.New("Claude subscription response did not contain structured output or a plain completion")
+ }
+ toolNames := make([]string, 0, len(req.Tools))
+ for _, tool := range req.Tools {
+ toolNames = append(toolNames, tool.Name)
+ }
+ catalog := agentcapabilities.NewProviderToolNameCatalog(toolNames)
+ if agentcapabilities.ProviderToolCallArtifactIndex(content, catalog) >= 0 {
+ return turn, errors.New("Claude subscription response leaked a tool call through plain content")
+ }
+ turn.Content = content
+ turn.StopReason = "end_turn"
+ turn.RawToolCalls = []subscriptionAgentToolCall{}
+ turn.InputTokens = wrapper.Usage.InputTokens
+ turn.OutputTokens = wrapper.Usage.OutputTokens
+ return turn, nil
+}
+
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 67959d6dd9..d5d11e28f8 100644
--- a/internal/ai/providers/subscription_agent_test.go
+++ b/internal/ai/providers/subscription_agent_test.go
@@ -349,6 +349,23 @@ func TestDecodeSubscriptionAgentTurnRejectsUnknownStructuredFields(t *testing.T)
}
}
+func TestDecodeClaudePlainCompletionRejectsToolCallArtifacts(t *testing.T) {
+ req := ChatRequest{Tools: []Tool{{Name: "pulse_query"}}}
+ raw := []byte(`{"result":"Patrol completed without another tool call.","usage":{"input_tokens":4,"output_tokens":7}}`)
+ turn, err := decodeClaudePlainCompletion(req, raw)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if turn.Content == "" || turn.StopReason != "end_turn" || turn.InputTokens != 4 || turn.OutputTokens != 7 {
+ t.Fatalf("plain completion = %#v", turn)
+ }
+
+ leaked := []byte(`{"result":"pulse_query({\"action\":\"list\"})"}`)
+ if _, err := decodeClaudePlainCompletion(req, leaked); err == nil || !strings.Contains(err.Error(), "leaked a tool call") {
+ t.Fatalf("plain tool-call leak error = %v", err)
+ }
+}
+
func writeExecutable(t *testing.T, path, body string) {
t.Helper()
if err := os.WriteFile(path, []byte(body), 0700); err != nil {