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.

Fail closed on incomplete OpenAI SSE streams

rcourtman01474a18b6f5
diff --git a/docs/release-control/v6/internal/subsystems/ai-runtime.md b/docs/release-control/v6/internal/subsystems/ai-runtime.md
index 0dfca4f290..af92ce9165 100644
--- a/docs/release-control/v6/internal/subsystems/ai-runtime.md
+++ b/docs/release-control/v6/internal/subsystems/ai-runtime.md
@@ -162,6 +162,11 @@ runtime cost control, and shared AI transport surfaces.
shape covered by the agent-exec auto-approval policy. It must not compose
shell loops, accept hostnames, interpolate unvalidated targets, or bypass
approval requirements for compound commands.
+15. Keep OpenAI-compatible streaming finalization fail-closed. `ChatStream` may
+ flush buffered final SSE lines when EOF arrives with unread bytes and may
+ accept providers that omit `[DONE]` only after a terminal `finish_reason`,
+ but it must not emit `done` or executable tool calls from partial tool-call
+ builders when the stream closes before that terminal provider state.
## Current State
diff --git a/internal/ai/providers/openai.go b/internal/ai/providers/openai.go
index a24d393c1c..0cdef2642b 100644
--- a/internal/ai/providers/openai.go
+++ b/internal/ai/providers/openai.go
@@ -920,6 +920,16 @@ func (c *OpenAIClient) ChatStream(ctx context.Context, req ChatRequest, callback
}
}
+ if finishReason == "" {
+ if len(toolCallBuilders) > 0 {
+ return fmt.Errorf("stream ended before tool call completion")
+ }
+ return fmt.Errorf("stream ended before completion marker")
+ }
+ if len(toolCallBuilders) > 0 && finishReason != "tool_calls" {
+ return fmt.Errorf("stream ended with finish_reason %q before completing tool calls", finishReason)
+ }
+
emitDone()
return nil
}
diff --git a/internal/ai/providers/openai_test.go b/internal/ai/providers/openai_test.go
index e452852b2a..fbf1d3a159 100644
--- a/internal/ai/providers/openai_test.go
+++ b/internal/ai/providers/openai_test.go
@@ -232,6 +232,76 @@ func TestOpenAIClient_ChatStream_EOFWithoutDoneFinalizesToolCalls(t *testing.T)
assert.Equal(t, map[string]interface{}{"host": "nas01"}, doneEvent.ToolCalls[0].Input)
}
+func TestOpenAIClient_ChatStream_EOFWithoutDoneRejectsIncompleteToolCall(t *testing.T) {
+ payload := strings.Join([]string{
+ `data: {"id":"chatcmpl-4","choices":[{"delta":{"tool_calls":[{"index":0,"id":"call_incomplete","type":"function","function":{"name":"lookup_host","arguments":"{\"host\""}}]}}]}`,
+ ``,
+ }, "\n")
+
+ client := NewOpenAIClient("sk-test", "gpt-4", "https://example.invalid/v1", 0)
+ client.client = &http.Client{
+ Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) {
+ return &http.Response{
+ StatusCode: http.StatusOK,
+ Header: http.Header{"Content-Type": []string{"text/event-stream"}},
+ Body: &readOnceEOFBody{payload: []byte(payload)},
+ }, nil
+ }),
+ }
+
+ var doneCalled bool
+
+ err := client.ChatStream(context.Background(), ChatRequest{Messages: []Message{{Role: "user", Content: "Hi"}}}, func(event StreamEvent) {
+ if event.Type == "done" {
+ doneCalled = true
+ }
+ })
+ require.Error(t, err)
+ assert.Contains(t, err.Error(), "stream ended before tool call completion")
+ assert.False(t, doneCalled)
+}
+
+func TestOpenAIClient_ChatStream_EOFWithoutDoneFinalizesStopResponse(t *testing.T) {
+ payload := strings.Join([]string{
+ `data: {"id":"chatcmpl-5","choices":[{"delta":{"content":"Done"}}]}`,
+ ``,
+ `data: {"id":"chatcmpl-5","choices":[{"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":7,"completion_tokens":3}}`,
+ ``,
+ }, "\n")
+
+ client := NewOpenAIClient("sk-test", "gpt-4", "https://example.invalid/v1", 0)
+ client.client = &http.Client{
+ Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) {
+ return &http.Response{
+ StatusCode: http.StatusOK,
+ Header: http.Header{"Content-Type": []string{"text/event-stream"}},
+ Body: &readOnceEOFBody{payload: []byte(payload)},
+ }, nil
+ }),
+ }
+
+ var content string
+ var doneEvent DoneEvent
+ var doneCalled bool
+
+ err := client.ChatStream(context.Background(), ChatRequest{Messages: []Message{{Role: "user", Content: "Hi"}}}, func(event StreamEvent) {
+ switch event.Type {
+ case "content":
+ content += event.Data.(ContentEvent).Text
+ case "done":
+ doneCalled = true
+ doneEvent = event.Data.(DoneEvent)
+ }
+ })
+ require.NoError(t, err)
+ require.True(t, doneCalled)
+ assert.Equal(t, "Done", content)
+ assert.Equal(t, "end_turn", doneEvent.StopReason)
+ assert.Equal(t, 7, doneEvent.InputTokens)
+ assert.Equal(t, 3, doneEvent.OutputTokens)
+ assert.Empty(t, doneEvent.ToolCalls)
+}
+
func TestOpenAIClient_Chat_ToolChoiceNone_DropsTools(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var got map[string]interface{}