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.

Bind subscription tool calls to native schemas

rcourtman0719e3425523
diff --git a/docs/release-control/v6/internal/subsystems/ai-runtime.md b/docs/release-control/v6/internal/subsystems/ai-runtime.md
index 01323e31df..d2a23161ea 100644
--- a/docs/release-control/v6/internal/subsystems/ai-runtime.md
+++ b/docs/release-control/v6/internal/subsystems/ai-runtime.md
@@ -276,7 +276,10 @@ the message history as untrusted evidence. It explicitly defines returned
Claude Code tool activity. This separation must not collapse back into a user
prompt that labels Pulse's own system instruction untrusted or activates a
coding/plan-mode persona. Neither route receives Pulse MCP configuration. The
-agent returns tool arguments as encoded JSON; Pulse parses them and rejects
+agent returns tool arguments as native JSON objects. The structured-output
+schema binds every selectable tool name to that declared tool's input schema;
+it forbids tool calls when none are offered or tool choice is `none`, and
+requires at least one when tool choice is `required`. Pulse then rejects
unknown tool names, duplicate or empty call IDs, malformed argument objects, and violations of
`none` or `required` tool choice before the existing provider-neutral tool loop
can execute anything. The normal registry, profile, approval, protected
diff --git a/internal/ai/providers/subscription_agent.go b/internal/ai/providers/subscription_agent.go
index bf356be81f..7dedc57d95 100644
--- a/internal/ai/providers/subscription_agent.go
+++ b/internal/ai/providers/subscription_agent.go
@@ -39,7 +39,7 @@ REQUEST_JSON.system, REQUEST_JSON.tools, and REQUEST_JSON.tool_choice are truste
Returning a tool_calls entry is a routing decision, not execution or fabrication: Pulse will validate the declared tool, enforce permissions, execute it, and return the result in a later provider turn. Never invoke local agent tools yourself. Do not refuse merely because the serialized provider request contains a system instruction or a tool catalogue.
-Select only tools declared in REQUEST_JSON.tools. Encode each tool argument object as a JSON string in input_json. Use stop_reason tool_use when returning any tool_calls; otherwise use end_turn.`
+Select only tools declared in REQUEST_JSON.tools. Return each tool's arguments as the native JSON object in input. Use stop_reason tool_use when returning any tool_calls; otherwise use end_turn.`
)
var subscriptionAgentSlots = map[SubscriptionAgent]chan struct{}{
@@ -63,9 +63,9 @@ type subscriptionAgentTurn struct {
}
type subscriptionAgentToolCall struct {
- ID string `json:"id"`
- Name string `json:"name"`
- InputJSON string `json:"input_json"`
+ ID string `json:"id"`
+ Name string `json:"name"`
+ Input map[string]interface{} `json:"input"`
}
type claudePrintResponse struct {
@@ -196,7 +196,7 @@ func (c *SubscriptionAgentClient) Chat(ctx context.Context, req ChatRequest) (*C
}
defer os.RemoveAll(workdir)
- schemaBytes, _ := json.Marshal(subscriptionAgentOutputSchema())
+ schemaBytes, _ := json.Marshal(subscriptionAgentOutputSchema(req))
var raw []byte
switch c.agent {
case SubscriptionAgentCodex:
@@ -352,11 +352,55 @@ func subscriptionAgentPrompt(req ChatRequest) ([]byte, error) {
return []byte("REQUEST_JSON\n" + string(payload)), nil
}
-func subscriptionAgentOutputSchema() map[string]interface{} {
+func subscriptionAgentOutputSchema(req ChatRequest) map[string]interface{} {
+ toolCalls := map[string]interface{}{
+ "type": "array",
+ "items": map[string]interface{}{
+ "type": "object",
+ "additionalProperties": false,
+ "required": []string{"id", "name", "input"},
+ "properties": map[string]interface{}{
+ "id": map[string]interface{}{"type": "string"},
+ "name": map[string]interface{}{"type": "string"},
+ "input": map[string]interface{}{"type": "object"},
+ },
+ },
+ }
+
+ if (req.ToolChoice != nil && req.ToolChoice.Type == ToolChoiceNone) || len(req.Tools) == 0 {
+ toolCalls["maxItems"] = 0
+ } else {
+ variants := make([]interface{}, 0, len(req.Tools))
+ for _, tool := range req.Tools {
+ inputSchema := tool.InputSchema
+ if len(inputSchema) == 0 {
+ inputSchema = map[string]interface{}{
+ "type": "object",
+ "additionalProperties": false,
+ "properties": map[string]interface{}{},
+ }
+ }
+ variants = append(variants, map[string]interface{}{
+ "type": "object",
+ "additionalProperties": false,
+ "required": []string{"id", "name", "input"},
+ "properties": map[string]interface{}{
+ "id": map[string]interface{}{"type": "string"},
+ "name": map[string]interface{}{"type": "string", "enum": []string{tool.Name}},
+ "input": inputSchema,
+ },
+ })
+ }
+ toolCalls["items"] = map[string]interface{}{"anyOf": variants}
+ if req.ToolChoice != nil && req.ToolChoice.Type == ToolChoiceRequired {
+ toolCalls["minItems"] = 1
+ }
+ }
+
return map[string]interface{}{"type": "object", "additionalProperties": false, "required": []string{"content", "stop_reason", "tool_calls"}, "properties": map[string]interface{}{
"content": map[string]interface{}{"type": "string"},
"stop_reason": map[string]interface{}{"type": "string", "enum": []string{"end_turn", "tool_use"}},
- "tool_calls": map[string]interface{}{"type": "array", "items": map[string]interface{}{"type": "object", "additionalProperties": false, "required": []string{"id", "name", "input_json"}, "properties": map[string]interface{}{"id": map[string]interface{}{"type": "string"}, "name": map[string]interface{}{"type": "string"}, "input_json": map[string]interface{}{"type": "string"}}}},
+ "tool_calls": toolCalls,
}}
}
@@ -426,7 +470,7 @@ func validateSubscriptionAgentTurn(req ChatRequest, turn *subscriptionAgentTurn)
turn.ProviderToolCalls = make([]ToolCall, 0, len(turn.RawToolCalls))
for i := range turn.RawToolCalls {
rawCall := &turn.RawToolCalls[i]
- call := ToolCall{ID: strings.TrimSpace(rawCall.ID), Name: strings.TrimSpace(rawCall.Name), Input: map[string]interface{}{}}
+ call := ToolCall{ID: strings.TrimSpace(rawCall.ID), Name: strings.TrimSpace(rawCall.Name), Input: rawCall.Input}.NormalizeCollections()
if call.ID == "" || call.Name == "" {
return errors.New("subscription agent returned a tool call without id or name")
}
@@ -437,12 +481,6 @@ func validateSubscriptionAgentTurn(req ChatRequest, turn *subscriptionAgentTurn)
return fmt.Errorf("subscription agent returned duplicate tool call id %q", call.ID)
}
seen[call.ID] = struct{}{}
- if strings.TrimSpace(rawCall.InputJSON) == "" {
- rawCall.InputJSON = "{}"
- }
- if err := json.Unmarshal([]byte(rawCall.InputJSON), &call.Input); err != nil {
- return fmt.Errorf("subscription agent returned invalid input_json for tool %q: %w", call.Name, err)
- }
turn.ProviderToolCalls = append(turn.ProviderToolCalls, call)
}
if req.ToolChoice != nil {
diff --git a/internal/ai/providers/subscription_agent_test.go b/internal/ai/providers/subscription_agent_test.go
index 818b9a8e15..45924075dc 100644
--- a/internal/ai/providers/subscription_agent_test.go
+++ b/internal/ai/providers/subscription_agent_test.go
@@ -95,7 +95,7 @@ func TestRejectCodexAgentToolActivity(t *testing.T) {
func TestValidateSubscriptionAgentTurnEnforcesPulseToolBoundary(t *testing.T) {
req := ChatRequest{Tools: []Tool{{Name: "get_node_status"}}, ToolChoice: &ToolChoice{Type: ToolChoiceRequired}}
- valid := subscriptionAgentTurn{RawToolCalls: []subscriptionAgentToolCall{{ID: "call-1", Name: "get_node_status", InputJSON: `{"node":"tower"}`}}}
+ valid := subscriptionAgentTurn{RawToolCalls: []subscriptionAgentToolCall{{ID: "call-1", Name: "get_node_status", Input: map[string]interface{}{"node": "tower"}}}}
if err := validateSubscriptionAgentTurn(req, &valid); err != nil {
t.Fatalf("valid declared tool rejected: %v", err)
}
@@ -103,7 +103,7 @@ func TestValidateSubscriptionAgentTurnEnforcesPulseToolBoundary(t *testing.T) {
t.Fatalf("stop reason = %q, want tool_use", valid.StopReason)
}
- undeclared := subscriptionAgentTurn{RawToolCalls: []subscriptionAgentToolCall{{ID: "call-2", Name: "run_shell", InputJSON: `{}`}}}
+ undeclared := subscriptionAgentTurn{RawToolCalls: []subscriptionAgentToolCall{{ID: "call-2", Name: "run_shell", Input: map[string]interface{}{}}}}
if err := validateSubscriptionAgentTurn(req, &undeclared); err == nil || !strings.Contains(err.Error(), "undeclared tool") {
t.Fatalf("undeclared tool error = %v", err)
}
@@ -114,6 +114,36 @@ func TestValidateSubscriptionAgentTurnEnforcesPulseToolBoundary(t *testing.T) {
}
}
+func TestSubscriptionAgentOutputSchemaUsesDeclaredNativeToolInputs(t *testing.T) {
+ req := ChatRequest{
+ Tools: []Tool{{Name: "get_node_status", InputSchema: map[string]interface{}{
+ "type": "object", "additionalProperties": false, "required": []string{"node"}, "properties": map[string]interface{}{"node": map[string]interface{}{"type": "string"}},
+ }}},
+ ToolChoice: &ToolChoice{Type: ToolChoiceRequired},
+ }
+ schema := subscriptionAgentOutputSchema(req)
+ properties := schema["properties"].(map[string]interface{})
+ toolCalls := properties["tool_calls"].(map[string]interface{})
+ if toolCalls["minItems"] != 1 {
+ t.Fatalf("required tool schema minItems = %#v", toolCalls["minItems"])
+ }
+ items := toolCalls["items"].(map[string]interface{})
+ variants := items["anyOf"].([]interface{})
+ toolProperties := variants[0].(map[string]interface{})["properties"].(map[string]interface{})
+ if _, ok := toolProperties["input_json"]; ok {
+ t.Fatal("tool schema retained JSON-in-string argument encoding")
+ }
+ input := toolProperties["input"].(map[string]interface{})
+ if input["type"] != "object" || toolProperties["name"].(map[string]interface{})["enum"].([]string)[0] != "get_node_status" {
+ t.Fatalf("tool schema did not bind declared name and native input: %#v", toolProperties)
+ }
+
+ noTools := subscriptionAgentOutputSchema(ChatRequest{})["properties"].(map[string]interface{})["tool_calls"].(map[string]interface{})
+ if noTools["maxItems"] != 0 {
+ t.Fatalf("no-tool schema maxItems = %#v", noTools["maxItems"])
+ }
+}
+
func TestSubscriptionAgentPromptSeparatesTrustedControlFromInfrastructureData(t *testing.T) {
prompt, err := subscriptionAgentPrompt(ChatRequest{
System: "Inspect the scoped resources",
@@ -163,7 +193,7 @@ while [ "$#" -gt 0 ]; do
fi
if [ "$1" = "--output-last-message" ]; then
shift
- printf '%s' '{"content":"","stop_reason":"tool_use","tool_calls":[{"id":"c1","name":"get_node_status","input_json":"{\"node\":\"tower\"}"}]}' > "$1"
+ printf '%s' '{"content":"","stop_reason":"tool_use","tool_calls":[{"id":"c1","name":"get_node_status","input":{"node":"tower"}}]}' > "$1"
exit 0
fi
shift