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.

Support Patrol streaming on subscription routes

rcourtman99615c18e3b6
diff --git a/docs/release-control/v6/internal/subsystems/ai-runtime.md b/docs/release-control/v6/internal/subsystems/ai-runtime.md
index 1a3ba308b0..983c2d3a37 100644
--- a/docs/release-control/v6/internal/subsystems/ai-runtime.md
+++ b/docs/release-control/v6/internal/subsystems/ai-runtime.md
@@ -268,6 +268,12 @@ 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.
+Patrol consumes the provider streaming interface, so the adapter projects each
+fully validated CLI turn into canonical buffered `content`, `tool_start`, and
+`done` events. It must emit nothing before the complete CLI response passes the
+schema and tool-boundary checks. This is transport compatibility rather than a
+claim of token-by-token CLI streaming; Pulse still owns tool execution,
+`tool_end` progress, run-history persistence, and verification.
Codex JSONL event output is also a fail-closed audit channel: reported command,
file, MCP, web, computer, or image-tool activity invalidates the turn. This
detects a violated adapter instruction but cannot retroactively protect data a
diff --git a/internal/ai/providers/subscription_agent.go b/internal/ai/providers/subscription_agent.go
index 2f3a5aea72..33e93ae746 100644
--- a/internal/ai/providers/subscription_agent.go
+++ b/internal/ai/providers/subscription_agent.go
@@ -206,6 +206,43 @@ func (c *SubscriptionAgentClient) Chat(ctx context.Context, req ChatRequest) (*C
return response.NormalizeCollectionsPtr(), nil
}
+// ChatStream adapts the CLI's bounded, structured one-turn response to Pulse's
+// streaming provider contract. The local subscription CLIs do not expose the
+// same provider stream as an API transport, so no event is emitted until the
+// complete turn has passed the output schema and tool-boundary validation.
+// Pulse still receives canonical tool_start and done events and therefore owns
+// tool execution, progress, persistence, and verification as usual.
+func (c *SubscriptionAgentClient) ChatStream(ctx context.Context, req ChatRequest, callback StreamCallback) error {
+ response, err := c.Chat(ctx, req)
+ if err != nil {
+ return err
+ }
+ if callback == nil {
+ return nil
+ }
+ if response.Content != "" {
+ callback(StreamEvent{Type: "content", Data: ContentEvent{Text: response.Content}})
+ }
+ for _, call := range response.ToolCalls {
+ callback(StreamEvent{
+ Type: "tool_start",
+ Data: ToolStartEvent{ID: call.ID, Name: call.Name, Input: call.Input}.NormalizeCollections(),
+ })
+ }
+ callback(StreamEvent{
+ Type: "done",
+ Data: DoneEvent{
+ StopReason: response.StopReason,
+ ToolCalls: response.ToolCalls,
+ InputTokens: response.InputTokens,
+ OutputTokens: response.OutputTokens,
+ }.NormalizeCollections(),
+ })
+ return nil
+}
+
+func (c *SubscriptionAgentClient) SupportsThinking(string) bool { return false }
+
func (c *SubscriptionAgentClient) run(ctx context.Context, name string, args []string, stdin []byte, dir string) ([]byte, error) {
if c.timeout > 0 {
var cancel context.CancelFunc
diff --git a/internal/ai/providers/subscription_agent_live_test.go b/internal/ai/providers/subscription_agent_live_test.go
index ddfe4b928e..c1dc3fdc5d 100644
--- a/internal/ai/providers/subscription_agent_live_test.go
+++ b/internal/ai/providers/subscription_agent_live_test.go
@@ -31,19 +31,28 @@ func TestSubscriptionAgentLive(t *testing.T) {
if err := client.TestConnection(ctx); err != nil {
t.Fatalf("authentication readiness failed: %v", err)
}
- response, err := client.Chat(ctx, ChatRequest{
+ var toolStarts int
+ var done DoneEvent
+ err := client.ChatStream(ctx, ChatRequest{
System: "Select the supplied observation tool exactly once. Do not claim that it ran.",
Messages: []Message{{Role: "user", Content: "Inspect node tower using the supplied Pulse tool."}},
Tools: []Tool{{Name: "get_node_status", Description: "Read the current status of one node", 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},
+ }, func(event StreamEvent) {
+ switch event.Type {
+ case "tool_start":
+ toolStarts++
+ case "done":
+ done, _ = event.Data.(DoneEvent)
+ }
})
if err != nil {
- t.Fatalf("structured tool-selection turn failed: %v", err)
+ t.Fatalf("structured streaming tool-selection turn failed: %v", err)
}
- if len(response.ToolCalls) != 1 || response.ToolCalls[0].Name != "get_node_status" {
- t.Fatalf("unexpected structured response: %#v", response)
+ if toolStarts != 1 || len(done.ToolCalls) != 1 || done.ToolCalls[0].Name != "get_node_status" {
+ t.Fatalf("unexpected structured stream: starts=%d done=%#v", toolStarts, done)
}
})
}
diff --git a/internal/ai/providers/subscription_agent_test.go b/internal/ai/providers/subscription_agent_test.go
index 94799b4034..2f9dc8d5be 100644
--- a/internal/ai/providers/subscription_agent_test.go
+++ b/internal/ai/providers/subscription_agent_test.go
@@ -134,6 +134,23 @@ printf '%s' '{"structured_output":{"content":"healthy","stop_reason":"end_turn",
if len(response.ToolCalls) != 1 || response.ToolCalls[0].Name != "get_node_status" {
t.Fatalf("Codex tool calls = %#v", response.ToolCalls)
}
+ var streamEvents []StreamEvent
+ if err := codex.ChatStream(ctx, ChatRequest{Tools: []Tool{{Name: "get_node_status"}}, ToolChoice: &ToolChoice{Type: ToolChoiceRequired}}, func(event StreamEvent) {
+ streamEvents = append(streamEvents, event)
+ }); err != nil {
+ t.Fatalf("Codex buffered stream turn failed: %v", err)
+ }
+ if len(streamEvents) != 2 || streamEvents[0].Type != "tool_start" || streamEvents[1].Type != "done" {
+ t.Fatalf("Codex stream events = %#v", streamEvents)
+ }
+ start, ok := streamEvents[0].Data.(ToolStartEvent)
+ if !ok || start.ID != "c1" || start.Name != "get_node_status" || start.Input["node"] != "tower" {
+ t.Fatalf("Codex tool_start = %#v", streamEvents[0].Data)
+ }
+ done, ok := streamEvents[1].Data.(DoneEvent)
+ if !ok || done.StopReason != "tool_use" || len(done.ToolCalls) != 1 {
+ t.Fatalf("Codex done = %#v", streamEvents[1].Data)
+ }
claude := NewSubscriptionAgentClient(SubscriptionAgentClaude, "sonnet", 3*time.Second)
if err := claude.TestConnection(ctx); err != nil {
@@ -148,6 +165,39 @@ printf '%s' '{"structured_output":{"content":"healthy","stop_reason":"end_turn",
}
}
+func TestSubscriptionAgentBufferedStreamEmitsContentBeforeDone(t *testing.T) {
+ if runtime.GOOS == "windows" {
+ t.Skip("fake subscription CLIs use POSIX shell scripts")
+ }
+ binDir := t.TempDir()
+ writeExecutable(t, filepath.Join(binDir, "claude"), `#!/bin/sh
+printf '%s' '{"structured_output":{"content":"healthy","stop_reason":"end_turn","tool_calls":[]},"usage":{"input_tokens":12,"output_tokens":3}}'
+`)
+ t.Setenv("PATH", binDir+string(os.PathListSeparator)+os.Getenv("PATH"))
+
+ client := NewSubscriptionAgentClient(SubscriptionAgentClaude, "sonnet", 3*time.Second)
+ var events []StreamEvent
+ if err := client.ChatStream(context.Background(), ChatRequest{}, func(event StreamEvent) {
+ events = append(events, event)
+ }); err != nil {
+ t.Fatal(err)
+ }
+ if len(events) != 2 || events[0].Type != "content" || events[1].Type != "done" {
+ t.Fatalf("stream events = %#v", events)
+ }
+ content, ok := events[0].Data.(ContentEvent)
+ if !ok || content.Text != "healthy" {
+ t.Fatalf("content event = %#v", events[0].Data)
+ }
+ done, ok := events[1].Data.(DoneEvent)
+ if !ok || done.InputTokens != 12 || done.OutputTokens != 3 || done.ToolCalls == nil {
+ t.Fatalf("done event = %#v", events[1].Data)
+ }
+ if client.SupportsThinking("sonnet") {
+ t.Fatal("subscription-agent adapter must not expose private CLI reasoning")
+ }
+}
+
func TestDecodeClaudeSubscriptionAgentRejectsNonJSONResult(t *testing.T) {
raw, _ := json.Marshal(claudePrintResponse{Result: "not-json"})
if _, err := decodeSubscriptionAgentTurn(SubscriptionAgentClaude, raw); err == nil {