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.

Honor OpenRouter reasoning limits

rcourtman7fb39f65cf25
diff --git a/docs/release-control/v6/internal/subsystems/ai-runtime.md b/docs/release-control/v6/internal/subsystems/ai-runtime.md
index 7fb2668bf5..6d6684c34e 100644
--- a/docs/release-control/v6/internal/subsystems/ai-runtime.md
+++ b/docs/release-control/v6/internal/subsystems/ai-runtime.md
@@ -4238,6 +4238,15 @@ model-led diagnostic freedom. Non-interactive tool-turn prose remains available
but is not concatenated into the persisted Patrol run analysis; the bounded
terminal summary is the sole operator conclusion.
+OpenRouter is part of that provider-neutral reasoning-control boundary. When a
+runtime request declares low, medium, or high `ReasoningEffort`, both streaming
+and non-streaming OpenRouter transports must serialize the gateway-native
+`reasoning.effort` request object. An absent or invalid hint must omit the
+object, and non-OpenRouter OpenAI-compatible endpoints must not receive this
+OpenRouter-specific shape. This is required for bounded Patrol recovery turns:
+raising the output allowance without constraining reasoning can consume the
+entire allowance before a required observer or action proposal is emitted.
+
Patrol Pro investigation now separates three previously conflated quantities:
model responses, model-selected tool calls, and evidence-tool calls. The
operator-facing investigation budget limits evidence calls; core derives a
diff --git a/internal/ai/providers/openai.go b/internal/ai/providers/openai.go
index 11aa8555d7..fb1de453b9 100644
--- a/internal/ai/providers/openai.go
+++ b/internal/ai/providers/openai.go
@@ -243,13 +243,18 @@ func (c *OpenAIClient) Name() string {
// openaiRequest is the request body for the OpenAI API
type openaiRequest struct {
- Model string `json:"model"`
- Messages []openaiMessage `json:"messages"`
- MaxTokens int `json:"max_tokens,omitempty"` // Legacy parameter for older models
- MaxCompletionTokens int `json:"max_completion_tokens,omitempty"` // For o1/o3 models
- Temperature float64 `json:"temperature,omitempty"`
- Tools []openaiTool `json:"tools,omitempty"`
- ToolChoice interface{} `json:"tool_choice,omitempty"` // "none", "required", or provider-specific choices
+ Model string `json:"model"`
+ Messages []openaiMessage `json:"messages"`
+ MaxTokens int `json:"max_tokens,omitempty"` // Legacy parameter for older models
+ MaxCompletionTokens int `json:"max_completion_tokens,omitempty"` // For o1/o3 models
+ Reasoning *openAIReasoning `json:"reasoning,omitempty"` // OpenRouter native reasoning control
+ Temperature float64 `json:"temperature,omitempty"`
+ Tools []openaiTool `json:"tools,omitempty"`
+ ToolChoice interface{} `json:"tool_choice,omitempty"` // "none", "required", or provider-specific choices
+}
+
+type openAIReasoning struct {
+ Effort ReasoningEffort `json:"effort"`
}
// openaiTool represents a function tool in OpenAI format
@@ -410,6 +415,13 @@ func (c *OpenAIClient) isOpenRouter() bool {
return c.Name() == "openrouter" || strings.Contains(c.baseURL, "openrouter.ai")
}
+func (c *OpenAIClient) openRouterReasoning(effort ReasoningEffort) *openAIReasoning {
+ if !c.isOpenRouter() || effort == "" || !effort.Valid() {
+ return nil
+ }
+ return &openAIReasoning{Effort: effort}
+}
+
func (c *OpenAIClient) usesOfficialOpenAIEndpoint() bool {
if c.Name() != "openai" {
return false
@@ -567,8 +579,9 @@ func (c *OpenAIClient) Chat(ctx context.Context, req ChatRequest) (*ChatResponse
// Build request
openaiReq := openaiRequest{
- Model: model,
- Messages: messages,
+ Model: model,
+ Messages: messages,
+ Reasoning: c.openRouterReasoning(req.ReasoningEffort),
}
// max_completion_tokens is required by official OpenAI reasoning models.
@@ -822,15 +835,16 @@ func (c *OpenAIClient) SupportsThinking(model string) bool {
// openaiStreamRequest extends openaiRequest with streaming field
type openaiStreamRequest struct {
- Model string `json:"model"`
- Messages []openaiMessage `json:"messages"`
- MaxTokens int `json:"max_tokens,omitempty"`
- MaxCompletionTokens int `json:"max_completion_tokens,omitempty"`
- Temperature float64 `json:"temperature,omitempty"`
- Tools []openaiTool `json:"tools,omitempty"`
- ToolChoice interface{} `json:"tool_choice,omitempty"`
- Stream bool `json:"stream"`
- StreamOptions *streamOptions `json:"stream_options,omitempty"`
+ Model string `json:"model"`
+ Messages []openaiMessage `json:"messages"`
+ MaxTokens int `json:"max_tokens,omitempty"`
+ MaxCompletionTokens int `json:"max_completion_tokens,omitempty"`
+ Reasoning *openAIReasoning `json:"reasoning,omitempty"`
+ Temperature float64 `json:"temperature,omitempty"`
+ Tools []openaiTool `json:"tools,omitempty"`
+ ToolChoice interface{} `json:"tool_choice,omitempty"`
+ Stream bool `json:"stream"`
+ StreamOptions *streamOptions `json:"stream_options,omitempty"`
}
type streamOptions struct {
@@ -950,9 +964,10 @@ func (c *OpenAIClient) ChatStream(ctx context.Context, req ChatRequest, callback
}
openaiReq := openaiStreamRequest{
- Model: model,
- Messages: messages,
- Stream: true,
+ Model: model,
+ Messages: messages,
+ Reasoning: c.openRouterReasoning(req.ReasoningEffort),
+ Stream: true,
}
if c.supportsStreamOptions() {
openaiReq.StreamOptions = &streamOptions{IncludeUsage: true}
diff --git a/internal/ai/providers/openai_test.go b/internal/ai/providers/openai_test.go
index 7ec04bcb57..99c0a53b07 100644
--- a/internal/ai/providers/openai_test.go
+++ b/internal/ai/providers/openai_test.go
@@ -473,6 +473,35 @@ func TestOpenAIClient_ChatStream_OpenRouterDefaultsCompletionBudget(t *testing.T
assert.Zero(t, captured.MaxTokens)
}
+func TestOpenAIClient_ChatStream_OpenRouterSerializesReasoningEffort(t *testing.T) {
+ var captured openaiStreamRequest
+ client := NewOpenAICompatibleClient("openrouter", "sk-test", "openrouter:qwen/qwen3.5-plus", "https://openrouter.ai/api/v1", 0)
+ client.streamClient = &http.Client{
+ Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) {
+ if err := json.NewDecoder(r.Body).Decode(&captured); err != nil {
+ return nil, err
+ }
+ return &http.Response{
+ StatusCode: http.StatusOK,
+ Header: http.Header{"Content-Type": []string{"text/event-stream"}},
+ Body: io.NopCloser(strings.NewReader(
+ "data: {\"choices\":[{\"delta\":{},\"finish_reason\":\"stop\"}]}\n" +
+ "data: [DONE]\n",
+ )),
+ }, nil
+ }),
+ }
+
+ err := client.ChatStream(context.Background(), ChatRequest{
+ Messages: []Message{{Role: "user", Content: "Propose the observer."}},
+ ReasoningEffort: ReasoningEffortLow,
+ }, func(StreamEvent) {})
+
+ require.NoError(t, err)
+ require.NotNil(t, captured.Reasoning)
+ assert.Equal(t, ReasoningEffortLow, captured.Reasoning.Effort)
+}
+
func TestOpenAIClient_ChatStream_OpenAIDoesNotDefaultCompletionBudget(t *testing.T) {
var captured openaiStreamRequest
client := NewOpenAIClient("sk-test", "gpt-4", "https://api.openai.com/v1", 0)
@@ -530,6 +559,67 @@ func TestOpenAIClient_Chat_OpenRouterDefaultsCompletionBudget(t *testing.T) {
assert.Zero(t, captured.MaxTokens)
}
+func TestOpenAIClient_Chat_OpenRouterSerializesReasoningEffort(t *testing.T) {
+ var captured openaiRequest
+ client := NewOpenAICompatibleClient("openrouter", "sk-test", "openrouter:qwen/qwen3.5-plus", "https://openrouter.ai/api/v1", 0)
+ client.client = &http.Client{
+ Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) {
+ if err := json.NewDecoder(r.Body).Decode(&captured); err != nil {
+ return nil, err
+ }
+ return &http.Response{
+ StatusCode: http.StatusOK,
+ Header: http.Header{"Content-Type": []string{"application/json"}},
+ Body: io.NopCloser(strings.NewReader(
+ `{"id":"chatcmpl-1","model":"qwen/qwen3.5-plus","choices":[{"message":{"role":"assistant","content":"ok"},"finish_reason":"stop"}]}`,
+ )),
+ }, nil
+ }),
+ }
+
+ resp, err := client.Chat(context.Background(), ChatRequest{
+ Messages: []Message{{Role: "user", Content: "Propose the observer."}},
+ ReasoningEffort: ReasoningEffortLow,
+ })
+
+ require.NoError(t, err)
+ assert.Equal(t, "ok", resp.Content)
+ require.NotNil(t, captured.Reasoning)
+ assert.Equal(t, ReasoningEffortLow, captured.Reasoning.Effort)
+}
+
+func TestOpenAIClient_Chat_NonOpenRouterOmitsReasoningEffort(t *testing.T) {
+ var captured openaiRequest
+ client := NewOpenAIClient("sk-test", "gpt-4", "https://api.openai.com/v1", 0)
+ client.client = &http.Client{
+ Transport: roundTripFunc(func(r *http.Request) (*http.Response, error) {
+ if err := json.NewDecoder(r.Body).Decode(&captured); err != nil {
+ return nil, err
+ }
+ return &http.Response{
+ StatusCode: http.StatusOK,
+ Header: http.Header{"Content-Type": []string{"application/json"}},
+ Body: io.NopCloser(strings.NewReader(
+ `{"id":"chatcmpl-1","model":"gpt-4","choices":[{"message":{"role":"assistant","content":"ok"},"finish_reason":"stop"}]}`,
+ )),
+ }, nil
+ }),
+ }
+
+ _, err := client.Chat(context.Background(), ChatRequest{
+ Messages: []Message{{Role: "user", Content: "Hi"}},
+ ReasoningEffort: ReasoningEffortLow,
+ })
+
+ require.NoError(t, err)
+ assert.Nil(t, captured.Reasoning)
+}
+
+func TestOpenAIClient_OpenRouterOmitsInvalidReasoningEffort(t *testing.T) {
+ client := NewOpenAICompatibleClient("openrouter", "sk-test", "qwen/qwen3.5-plus", "https://openrouter.ai/api/v1", 0)
+ assert.Nil(t, client.openRouterReasoning(ReasoningEffort("unbounded")))
+}
+
func TestOpenAIClient_Chat_KeepsReasoningOutOfVisibleContent(t *testing.T) {
client := NewOpenAIClient("sk-test", "openrouter:deepseek/deepseek-v4-pro", "https://openrouter.ai/api/v1", 0)
client.client = &http.Client{