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 strict subscription tool schemas

rcourtmana7849c9ef04a
diff --git a/docs/release-control/v6/internal/subsystems/ai-runtime.md b/docs/release-control/v6/internal/subsystems/ai-runtime.md
index 73e2eae131..d77d617bc2 100644
--- a/docs/release-control/v6/internal/subsystems/ai-runtime.md
+++ b/docs/release-control/v6/internal/subsystems/ai-runtime.md
@@ -307,10 +307,18 @@ coding/plan-mode persona. Neither route receives Pulse MCP configuration. The
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
+requires at least one when tool choice is `required`. That output schema is a
+deep-copied transport projection rather than an alias of the native Pulse tool
+schema: every projected object is closed, every projected property is required,
+and fields that remain optional in the native schema are nullable only at the
+strict transport boundary. Before normal Pulse validation or execution, the
+adapter recursively removes null placeholders for those originally optional
+fields while preserving required nulls for the native validator to decide.
+No-tool turns use the same strict empty-object input contract, and neither
+projection nor cleanup may mutate the registry-owned native schema. 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
resource, action, and verification contracts remain the only infrastructure
authority. Calls are serialized per local subscription agent and bounded by
the configured request timeout and prompt/output size limits, with a two-minute
diff --git a/internal/ai/patrol_model_readiness.go b/internal/ai/patrol_model_readiness.go
index cbe2c54f1d..c41212e8b2 100644
--- a/internal/ai/patrol_model_readiness.go
+++ b/internal/ai/patrol_model_readiness.go
@@ -1006,9 +1006,10 @@ func patrolReadinessTools() []providers.Tool {
Name: patrolReadinessChangeTool,
Description: "Synthetic change tool. Never call this during a readiness evaluation.",
InputSchema: map[string]interface{}{
- "type": "object",
- "properties": map[string]interface{}{"nonce": map[string]interface{}{"type": "string"}},
- "required": []string{"nonce"},
+ "type": "object",
+ "properties": map[string]interface{}{"nonce": map[string]interface{}{"type": "string"}},
+ "required": []string{"nonce"},
+ "additionalProperties": false,
},
},
}
diff --git a/internal/ai/patrol_model_readiness_test.go b/internal/ai/patrol_model_readiness_test.go
index da6e6e8d83..4a1dabe9b4 100644
--- a/internal/ai/patrol_model_readiness_test.go
+++ b/internal/ai/patrol_model_readiness_test.go
@@ -167,6 +167,17 @@ func TestPatrolReadinessContextFixturesDoNotPutEvidenceAtTheRecencyEdge(t *testi
}
}
+func TestPatrolReadinessToolsUseClosedObjectSchemas(t *testing.T) {
+ for _, tool := range patrolReadinessTools() {
+ if tool.InputSchema["type"] != "object" {
+ t.Fatalf("%s input schema type = %#v", tool.Name, tool.InputSchema["type"])
+ }
+ if tool.InputSchema["additionalProperties"] != false {
+ t.Fatalf("%s input schema is not closed: %#v", tool.Name, tool.InputSchema)
+ }
+ }
+}
+
func TestRunPatrolModelReadinessWithProvider_SeparatesProtocolFromContextQuality(t *testing.T) {
provider := &scriptedReadinessProvider{contextWindow: 32768, wrongContext: true}
result := runPatrolModelReadinessWithProvider(
diff --git a/internal/ai/providers/subscription_agent.go b/internal/ai/providers/subscription_agent.go
index ac1620886a..2e248c4126 100644
--- a/internal/ai/providers/subscription_agent.go
+++ b/internal/ai/providers/subscription_agent.go
@@ -10,6 +10,7 @@ import (
"os"
"os/exec"
"path/filepath"
+ "sort"
"strings"
"time"
@@ -432,6 +433,12 @@ func subscriptionAgentPrompt(req ChatRequest) ([]byte, error) {
}
func subscriptionAgentOutputSchema(req ChatRequest) map[string]interface{} {
+ strictEmptyInput := map[string]interface{}{
+ "type": "object",
+ "additionalProperties": false,
+ "properties": map[string]interface{}{},
+ "required": []string{},
+ }
toolCalls := map[string]interface{}{
"type": "array",
"items": map[string]interface{}{
@@ -441,7 +448,7 @@ func subscriptionAgentOutputSchema(req ChatRequest) map[string]interface{} {
"properties": map[string]interface{}{
"id": map[string]interface{}{"type": "string"},
"name": map[string]interface{}{"type": "string"},
- "input": map[string]interface{}{"type": "object"},
+ "input": strictEmptyInput,
},
},
}
@@ -451,13 +458,9 @@ func subscriptionAgentOutputSchema(req ChatRequest) map[string]interface{} {
} else {
variants := make([]interface{}, 0, len(req.Tools))
for _, tool := range req.Tools {
- inputSchema := tool.InputSchema
+ inputSchema := normalizeSubscriptionAgentToolSchema(tool.InputSchema)
if len(inputSchema) == 0 {
- inputSchema = map[string]interface{}{
- "type": "object",
- "additionalProperties": false,
- "properties": map[string]interface{}{},
- }
+ inputSchema = cloneSubscriptionAgentSchemaMap(strictEmptyInput)
}
variants = append(variants, map[string]interface{}{
"type": "object",
@@ -483,6 +486,193 @@ func subscriptionAgentOutputSchema(req ChatRequest) map[string]interface{} {
}}
}
+// normalizeSubscriptionAgentToolSchema projects a provider-neutral Pulse tool
+// schema into the strict structured-output dialect used by local subscription
+// agents. The projection is deliberately transport-local: native schemas keep
+// their optional-field semantics for Pulse validation and every source map is
+// deep-copied before the strict requirements are added.
+func normalizeSubscriptionAgentToolSchema(input map[string]interface{}) map[string]interface{} {
+ if len(input) == 0 {
+ return nil
+ }
+ return normalizeSubscriptionAgentSchemaNode(cloneSubscriptionAgentSchemaMap(input))
+}
+
+func cloneSubscriptionAgentSchemaMap(input map[string]interface{}) map[string]interface{} {
+ if input == nil {
+ return nil
+ }
+ cloned := make(map[string]interface{}, len(input))
+ for key, value := range input {
+ cloned[key] = cloneSubscriptionAgentSchemaValue(value)
+ }
+ return cloned
+}
+
+func cloneSubscriptionAgentSchemaValue(value interface{}) interface{} {
+ switch typed := value.(type) {
+ case map[string]interface{}:
+ return cloneSubscriptionAgentSchemaMap(typed)
+ case []interface{}:
+ cloned := make([]interface{}, len(typed))
+ for i := range typed {
+ cloned[i] = cloneSubscriptionAgentSchemaValue(typed[i])
+ }
+ return cloned
+ case []string:
+ return append([]string(nil), typed...)
+ case []map[string]interface{}:
+ cloned := make([]map[string]interface{}, len(typed))
+ for i := range typed {
+ cloned[i] = cloneSubscriptionAgentSchemaMap(typed[i])
+ }
+ return cloned
+ default:
+ return value
+ }
+}
+
+func normalizeSubscriptionAgentSchemaNode(schema map[string]interface{}) map[string]interface{} {
+ for _, keyword := range []string{"anyOf", "oneOf", "allOf"} {
+ normalizeSubscriptionAgentSchemaList(schema, keyword)
+ }
+ for _, keyword := range []string{"$defs", "definitions", "dependentSchemas"} {
+ children, ok := schema[keyword].(map[string]interface{})
+ if !ok {
+ continue
+ }
+ for name, child := range children {
+ if childSchema, ok := child.(map[string]interface{}); ok {
+ children[name] = normalizeSubscriptionAgentSchemaNode(childSchema)
+ }
+ }
+ }
+ if items, ok := schema["items"].(map[string]interface{}); ok {
+ schema["items"] = normalizeSubscriptionAgentSchemaNode(items)
+ } else if items, ok := schema["items"].([]interface{}); ok {
+ for i := range items {
+ if itemSchema, ok := items[i].(map[string]interface{}); ok {
+ items[i] = normalizeSubscriptionAgentSchemaNode(itemSchema)
+ }
+ }
+ }
+
+ properties, hasProperties := schema["properties"].(map[string]interface{})
+ if hasProperties {
+ originalRequired := subscriptionAgentRequiredSet(schema["required"])
+ names := make([]string, 0, len(properties))
+ for name, property := range properties {
+ names = append(names, name)
+ propertySchema, ok := property.(map[string]interface{})
+ if !ok {
+ continue
+ }
+ propertySchema = normalizeSubscriptionAgentSchemaNode(propertySchema)
+ if _, required := originalRequired[name]; !required {
+ propertySchema = nullableSubscriptionAgentSchema(propertySchema)
+ }
+ properties[name] = propertySchema
+ }
+ sort.Strings(names)
+ schema["required"] = names
+ }
+ if hasProperties || subscriptionAgentSchemaHasType(schema, "object") {
+ schema["additionalProperties"] = false
+ if !hasProperties {
+ schema["properties"] = map[string]interface{}{}
+ schema["required"] = []string{}
+ }
+ }
+ return schema
+}
+
+func normalizeSubscriptionAgentSchemaList(schema map[string]interface{}, keyword string) {
+ switch variants := schema[keyword].(type) {
+ case []interface{}:
+ for i := range variants {
+ if variant, ok := variants[i].(map[string]interface{}); ok {
+ variants[i] = normalizeSubscriptionAgentSchemaNode(variant)
+ }
+ }
+ case []map[string]interface{}:
+ for i := range variants {
+ variants[i] = normalizeSubscriptionAgentSchemaNode(variants[i])
+ }
+ }
+}
+
+func nullableSubscriptionAgentSchema(schema map[string]interface{}) map[string]interface{} {
+ if subscriptionAgentSchemaAllowsNull(schema) {
+ return schema
+ }
+ return map[string]interface{}{
+ "anyOf": []interface{}{
+ schema,
+ map[string]interface{}{"type": "null"},
+ },
+ }
+}
+
+func subscriptionAgentSchemaAllowsNull(schema map[string]interface{}) bool {
+ if subscriptionAgentSchemaHasType(schema, "null") {
+ return true
+ }
+ for _, keyword := range []string{"anyOf", "oneOf"} {
+ switch variants := schema[keyword].(type) {
+ case []interface{}:
+ for _, candidate := range variants {
+ if variant, ok := candidate.(map[string]interface{}); ok && subscriptionAgentSchemaAllowsNull(variant) {
+ return true
+ }
+ }
+ case []map[string]interface{}:
+ for _, variant := range variants {
+ if subscriptionAgentSchemaAllowsNull(variant) {
+ return true
+ }
+ }
+ }
+ }
+ return false
+}
+
+func subscriptionAgentSchemaHasType(schema map[string]interface{}, wanted string) bool {
+ switch schemaType := schema["type"].(type) {
+ case string:
+ return schemaType == wanted
+ case []string:
+ for _, candidate := range schemaType {
+ if candidate == wanted {
+ return true
+ }
+ }
+ case []interface{}:
+ for _, candidate := range schemaType {
+ if candidate == wanted {
+ return true
+ }
+ }
+ }
+ return false
+}
+
+func subscriptionAgentRequiredSet(raw interface{}) map[string]struct{} {
+ required := map[string]struct{}{}
+ switch values := raw.(type) {
+ case []string:
+ for _, value := range values {
+ required[value] = struct{}{}
+ }
+ case []interface{}:
+ for _, value := range values {
+ if name, ok := value.(string); ok {
+ required[name] = struct{}{}
+ }
+ }
+ }
+ return required
+}
+
func decodeSubscriptionAgentTurn(agent SubscriptionAgent, raw []byte) (subscriptionAgentTurn, error) {
var turn subscriptionAgentTurn
if agent == SubscriptionAgentClaude {
@@ -727,8 +917,10 @@ func rejectCodexAgentToolActivity(events []byte) error {
func validateSubscriptionAgentTurn(req ChatRequest, turn *subscriptionAgentTurn) error {
allowed := make(map[string]struct{}, len(req.Tools))
+ toolSchemas := make(map[string]map[string]interface{}, len(req.Tools))
for _, tool := range req.Tools {
allowed[tool.Name] = struct{}{}
+ toolSchemas[tool.Name] = tool.InputSchema
}
seen := make(map[string]struct{}, len(turn.RawToolCalls))
turn.ProviderToolCalls = make([]ToolCall, 0, len(turn.RawToolCalls))
@@ -745,6 +937,8 @@ func validateSubscriptionAgentTurn(req ChatRequest, turn *subscriptionAgentTurn)
return fmt.Errorf("subscription agent returned duplicate tool call id %q", call.ID)
}
seen[call.ID] = struct{}{}
+ stripSubscriptionAgentOptionalNulls(toolSchemas[call.Name], rawCall.Input)
+ call.Input = rawCall.Input
turn.ProviderToolCalls = append(turn.ProviderToolCalls, call)
}
if req.ToolChoice != nil {
@@ -767,6 +961,62 @@ func validateSubscriptionAgentTurn(req ChatRequest, turn *subscriptionAgentTurn)
return nil
}
+// stripSubscriptionAgentOptionalNulls removes only the null placeholders that
+// the strict transport requires for fields which remain optional in Pulse's
+// native tool contract. Required nulls are preserved for normal validation to
+// accept or reject according to the original schema.
+func stripSubscriptionAgentOptionalNulls(schema map[string]interface{}, input map[string]interface{}) {
+ if len(schema) == 0 || input == nil {
+ return
+ }
+ properties, _ := schema["properties"].(map[string]interface{})
+ required := subscriptionAgentRequiredSet(schema["required"])
+ for name, value := range input {
+ property, declared := properties[name]
+ propertySchema, schemaOK := property.(map[string]interface{})
+ if value == nil {
+ if declared {
+ if _, isRequired := required[name]; !isRequired {
+ delete(input, name)
+ }
+ }
+ continue
+ }
+ if declared && schemaOK {
+ stripSubscriptionAgentOptionalNullsFromValue(propertySchema, value)
+ }
+ }
+ for _, keyword := range []string{"anyOf", "oneOf", "allOf"} {
+ switch variants := schema[keyword].(type) {
+ case []interface{}:
+ for _, candidate := range variants {
+ if variant, ok := candidate.(map[string]interface{}); ok {
+ stripSubscriptionAgentOptionalNulls(variant, input)
+ }
+ }
+ case []map[string]interface{}:
+ for _, variant := range variants {
+ stripSubscriptionAgentOptionalNulls(variant, input)
+ }
+ }
+ }
+}
+
+func stripSubscriptionAgentOptionalNullsFromValue(schema map[string]interface{}, value interface{}) {
+ switch typed := value.(type) {
+ case map[string]interface{}:
+ stripSubscriptionAgentOptionalNulls(schema, typed)
+ case []interface{}:
+ items, ok := schema["items"].(map[string]interface{})
+ if !ok {
+ return
+ }
+ for _, item := range typed {
+ stripSubscriptionAgentOptionalNullsFromValue(items, item)
+ }
+ }
+}
+
// NormalizeCollectionsPtr keeps the Provider implementation concise while
// preserving the same non-nil collection contract as native clients.
func (r ChatResponse) NormalizeCollectionsPtr() *ChatResponse {
diff --git a/internal/ai/providers/subscription_agent_test.go b/internal/ai/providers/subscription_agent_test.go
index 8c5f4fae53..b80e65ceec 100644
--- a/internal/ai/providers/subscription_agent_test.go
+++ b/internal/ai/providers/subscription_agent_test.go
@@ -6,6 +6,7 @@ import (
"errors"
"os"
"path/filepath"
+ "reflect"
"runtime"
"strings"
"testing"
@@ -143,6 +144,125 @@ func TestSubscriptionAgentOutputSchemaUsesDeclaredNativeToolInputs(t *testing.T)
if noTools["maxItems"] != 0 {
t.Fatalf("no-tool schema maxItems = %#v", noTools["maxItems"])
}
+ fallbackProperties := noTools["items"].(map[string]interface{})["properties"].(map[string]interface{})
+ fallbackInput := fallbackProperties["input"].(map[string]interface{})
+ if fallbackInput["additionalProperties"] != false || len(fallbackInput["properties"].(map[string]interface{})) != 0 || !reflect.DeepEqual(fallbackInput["required"], []string{}) {
+ t.Fatalf("no-tool fallback input is not a strict empty object: %#v", fallbackInput)
+ }
+}
+
+func TestSubscriptionAgentOutputSchemaNormalizesNestedOptionalFieldsWithoutMutation(t *testing.T) {
+ native := map[string]interface{}{
+ "type": "object",
+ "additionalProperties": true,
+ "required": []string{"node"},
+ "properties": map[string]interface{}{
+ "node": map[string]interface{}{"type": "string"},
+ "cluster_name": map[string]interface{}{"type": "string"},
+ "filters": map[string]interface{}{
+ "type": "object",
+ "required": []string{"severity"},
+ "properties": map[string]interface{}{
+ "severity": map[string]interface{}{"type": "string", "enum": []string{"warning", "critical"}},
+ "limit": map[string]interface{}{"type": "integer"},
+ },
+ },
+ },
+ }
+ before, err := json.Marshal(native)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ schema := subscriptionAgentOutputSchema(ChatRequest{Tools: []Tool{{Name: "get_node_status", InputSchema: native}}})
+ toolCalls := schema["properties"].(map[string]interface{})["tool_calls"].(map[string]interface{})
+ variant := toolCalls["items"].(map[string]interface{})["anyOf"].([]interface{})[0].(map[string]interface{})
+ input := variant["properties"].(map[string]interface{})["input"].(map[string]interface{})
+ if input["additionalProperties"] != false || !reflect.DeepEqual(input["required"], []string{"cluster_name", "filters", "node"}) {
+ t.Fatalf("strict root input = %#v", input)
+ }
+ inputProperties := input["properties"].(map[string]interface{})
+ clusterNullable := inputProperties["cluster_name"].(map[string]interface{})["anyOf"].([]interface{})
+ if len(clusterNullable) != 2 || clusterNullable[1].(map[string]interface{})["type"] != "null" {
+ t.Fatalf("optional cluster_name was not nullable: %#v", inputProperties["cluster_name"])
+ }
+ filtersNullable := inputProperties["filters"].(map[string]interface{})["anyOf"].([]interface{})
+ filters := filtersNullable[0].(map[string]interface{})
+ if filters["additionalProperties"] != false || !reflect.DeepEqual(filters["required"], []string{"limit", "severity"}) {
+ t.Fatalf("strict nested filters = %#v", filters)
+ }
+ limitNullable := filters["properties"].(map[string]interface{})["limit"].(map[string]interface{})["anyOf"].([]interface{})
+ if len(limitNullable) != 2 || limitNullable[1].(map[string]interface{})["type"] != "null" {
+ t.Fatalf("optional nested limit was not nullable: %#v", limitNullable)
+ }
+
+ after, err := json.Marshal(native)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if string(after) != string(before) {
+ t.Fatalf("native tool schema mutated:\n before=%s\n after=%s", before, after)
+ }
+}
+
+func TestValidateSubscriptionAgentTurnStripsOnlyOptionalNullPlaceholders(t *testing.T) {
+ req := ChatRequest{Tools: []Tool{{Name: "get_node_status", InputSchema: map[string]interface{}{
+ "type": "object",
+ "required": []string{"node", "required_nullable"},
+ "properties": map[string]interface{}{
+ "node": map[string]interface{}{"type": "string"},
+ "required_nullable": map[string]interface{}{"type": []string{"string", "null"}},
+ "cluster_name": map[string]interface{}{"type": "string"},
+ "filters": map[string]interface{}{
+ "type": "object",
+ "required": []string{"mode"},
+ "properties": map[string]interface{}{
+ "mode": map[string]interface{}{"type": "string"},
+ "label": map[string]interface{}{"type": "string"},
+ },
+ },
+ "targets": map[string]interface{}{
+ "type": "array",
+ "items": map[string]interface{}{
+ "type": "object",
+ "properties": map[string]interface{}{"id": map[string]interface{}{"type": "string"}, "note": map[string]interface{}{"type": "string"}},
+ "required": []string{"id"},
+ },
+ },
+ },
+ }}}}
+ turn := subscriptionAgentTurn{RawToolCalls: []subscriptionAgentToolCall{{
+ ID: "call-1",
+ Name: "get_node_status",
+ Input: map[string]interface{}{
+ "node": "tower",
+ "required_nullable": nil,
+ "cluster_name": nil,
+ "filters": map[string]interface{}{"mode": "active", "label": nil},
+ "targets": []interface{}{map[string]interface{}{"id": "node-1", "note": nil}},
+ },
+ }}}
+ if err := validateSubscriptionAgentTurn(req, &turn); err != nil {
+ t.Fatal(err)
+ }
+ input := turn.ProviderToolCalls[0].Input
+ if _, ok := input["cluster_name"]; ok {
+ t.Fatalf("optional root null placeholder survived: %#v", input)
+ }
+ if value, ok := input["required_nullable"]; !ok || value != nil {
+ t.Fatalf("required null was not preserved for native validation: %#v", input)
+ }
+ filters := input["filters"].(map[string]interface{})
+ if _, ok := filters["label"]; ok || filters["mode"] != "active" {
+ t.Fatalf("nested optional null cleanup = %#v", filters)
+ }
+ target := input["targets"].([]interface{})[0].(map[string]interface{})
+ if _, ok := target["note"]; ok || target["id"] != "node-1" {
+ t.Fatalf("array item optional null cleanup = %#v", target)
+ }
+ if _, ok := turn.RawToolCalls[0].Input["cluster_name"]; ok {
+ t.Fatalf("raw transport call retained optional placeholder: %#v", turn.RawToolCalls[0].Input)
+ }
}
func TestSubscriptionAgentPromptSeparatesTrustedControlFromInfrastructureData(t *testing.T) {