diff --git a/cmd/launch/codex_app_test.go b/cmd/launch/codex_app_test.go index 876bf6e5f..85c82fbfa 100644 --- a/cmd/launch/codex_app_test.go +++ b/cmd/launch/codex_app_test.go @@ -922,6 +922,11 @@ func TestCodexAppCountsOnlyOllamaRequestsInRegularProfile(t *testing.T) { if err != nil { t.Fatal(err) } + // Keep session files newer than the cutoff even with coarse file timestamps. + start = start.Add(-time.Minute) + if err := os.WriteFile(startPath, []byte(start.Format(time.RFC3339Nano)), 0o600); err != nil { + t.Fatal(err) + } configPath, err := codexConfigPath() if err != nil { t.Fatal(err) diff --git a/middleware/responses_web_search_test.go b/middleware/responses_web_search_test.go index accab325b..c58cccc5f 100644 --- a/middleware/responses_web_search_test.go +++ b/middleware/responses_web_search_test.go @@ -590,6 +590,83 @@ func TestWebSearchResponsesWriterStreamsFollowUpBeforeSecondSearch(t *testing.T) } } +func TestResponsesMiddlewareWebSearchReasoningBeforeClientTools(t *testing.T) { + gin.SetMode(gin.TestMode) + for _, tt := range []struct { + name string + afterSearch bool + }{ + {name: "search available but unused"}, + {name: "client tools after search", afterSearch: true}, + } { + t.Run(tt.name, func(t *testing.T) { + chunks := []api.ChatResponse{ + {Message: api.Message{Thinking: "Check both cities."}}, + {Message: api.Message{ToolCalls: []api.ToolCall{ + {ID: "call_paris", Function: api.ToolCallFunction{Name: "get_weather", Arguments: testArgs(map[string]any{"city": "Paris"})}}, + {ID: "call_london", Function: api.ToolCallFunction{Name: "get_weather", Arguments: testArgs(map[string]any{"city": "London"})}}, + }}}, + {Done: true}, + } + router := gin.New() + router.POST("/v1/responses", ResponsesMiddleware(), func(c *gin.Context) { + writer := c.Writer.(*WebSearchResponsesWriter) + writer.search = func(context.Context, string) (*api.WebSearchResponse, error) { + return &api.WebSearchResponse{}, nil + } + writer.followUpStream = func(_ context.Context, _ []api.Message, _ api.Tools, yield func(api.ChatResponse) error) error { + for _, chunk := range chunks { + if err := yield(chunk); err != nil { + return err + } + } + return nil + } + initial := chunks + if tt.afterSearch { + initial = []api.ChatResponse{{Done: true, Message: api.Message{ToolCalls: []api.ToolCall{ + {ID: "call_search", Function: api.ToolCallFunction{Name: "web_search", Arguments: testArgs(map[string]any{"query": "weather"})}}, + }}}} + } + for _, chunk := range initial { + if err := json.NewEncoder(c.Writer).Encode(chunk); err != nil { + t.Fatal(err) + } + } + }) + request := httptest.NewRequest(http.MethodPost, "/v1/responses", strings.NewReader(`{ + "model":"test-model", "stream":true, "input":"Check the weather in both cities.", + "tools":[{"type":"web_search"},{"type":"function","name":"get_weather","parameters":{"type":"object"}}] + }`)) + request.Header.Set("Content-Type", "application/json") + recorder := httptest.NewRecorder() + router.ServeHTTP(recorder, request) + if recorder.Code != http.StatusOK { + t.Fatalf("status = %d, body = %s", recorder.Code, recorder.Body.String()) + } + var gotTypes []string + for _, block := range strings.Split(recorder.Body.String(), "\n\n") { + if !strings.HasPrefix(block, "event: response.output_item.done\n") { + continue + } + _, data, _ := strings.Cut(block, "\ndata: ") + var event map[string]any + if err := json.Unmarshal([]byte(data), &event); err != nil { + t.Fatal(err) + } + gotTypes = append(gotTypes, event["item"].(map[string]any)["type"].(string)) + } + wantTypes := []string{"reasoning", "function_call", "function_call"} + if tt.afterSearch { + wantTypes = append([]string{"web_search_call"}, wantTypes...) + } + if !reflect.DeepEqual(gotTypes, wantTypes) { + t.Errorf("completed item order = %v, want %v", gotTypes, wantTypes) + } + }) + } +} + func TestWebSearchResponsesWriterStreamingMixedFollowUpDoesNotLatchText(t *testing.T) { gin.SetMode(gin.TestMode) recorder := httptest.NewRecorder() diff --git a/openai/responses.go b/openai/responses.go index 0a88f8b8d..7bd1033f0 100644 --- a/openai/responses.go +++ b/openai/responses.go @@ -1926,12 +1926,11 @@ func (c *ResponsesStreamConverter) FinishMessageItem() []ResponsesStreamEvent { } } -// EmitFunctionCallItems emits function_call events for client-provided tool -// calls that accompanied a web_search call (mixed responses). Unlike -// processToolCalls, this does not set toolCallsSent, so subsequent text -// content can still be processed. +// EmitFunctionCallItems emits client-provided tool calls from the web-search +// wrapper, closing any open reasoning item first. It does not set toolCallsSent, +// so subsequent text content can still be processed. func (c *ResponsesStreamConverter) EmitFunctionCallItems(toolCalls []api.ToolCall) []ResponsesStreamEvent { - return c.emitFunctionCallEvents(toolCalls) + return append(c.finishReasoning(), c.emitFunctionCallEvents(toolCalls)...) } func (c *ResponsesStreamConverter) processTextContent(content string) []ResponsesStreamEvent { diff --git a/openai/responses_test.go b/openai/responses_test.go index 1d69c4c66..8ae044f71 100644 --- a/openai/responses_test.go +++ b/openai/responses_test.go @@ -2017,6 +2017,52 @@ func TestResponsesStreamConverter_ToolCalls(t *testing.T) { } } +func TestResponsesStreamConverter_EmitFunctionCallItems(t *testing.T) { + converter := NewResponsesStreamConverter("resp_test", "msg_test", "test", ResponsesRequest{}) + events := converter.Process(api.ChatResponse{Message: api.Message{Thinking: "Check both cities."}}) + events = append(events, converter.EmitFunctionCallItems([]api.ToolCall{ + {ID: "call_paris", Function: api.ToolCallFunction{Name: "get_weather", Arguments: testArgs(map[string]any{"city": "Paris"})}}, + {ID: "call_london", Function: api.ToolCallFunction{Name: "get_weather", Arguments: testArgs(map[string]any{"city": "London"})}}, + })...) + events = append(events, converter.Process(api.ChatResponse{Message: api.Message{Content: "The weather is ready."}, Done: true})...) + + var addedIDs, completed, output []any + for _, event := range events { + data := event.Data.(map[string]any) + switch event.Event { + case "response.output_item.added": + if data["output_index"] != len(addedIDs) { + t.Errorf("added output_index = %v, want %d", data["output_index"], len(addedIDs)) + } + addedIDs = append(addedIDs, data["item"].(map[string]any)["id"]) + case "response.output_item.done": + item := data["item"].(map[string]any) + index := len(completed) + if data["output_index"] != index || index >= len(addedIDs) || item["id"] != addedIDs[index] { + t.Errorf("item completed out of order or at the wrong index: %#v", data) + } + completed = append(completed, item) + case "response.completed": + output = data["response"].(map[string]any)["output"].([]any) + } + } + wantTypes := []string{"reasoning", "function_call", "function_call", "message"} + if len(addedIDs) != len(wantTypes) || len(completed) != len(wantTypes) { + t.Fatalf("added %d and completed %d items, want %d", len(addedIDs), len(completed), len(wantTypes)) + } + for i, want := range wantTypes { + if got := completed[i].(map[string]any)["type"]; got != want { + t.Errorf("completed item %d type = %v, want %s", i, got, want) + } + } + if !reflect.DeepEqual(output, completed) { + t.Fatalf("final output = %#v, want completed items %#v", output, completed) + } + if got := output[3].(map[string]any)["content"].([]map[string]any)[0]["text"]; got != "The weather is ready." { + t.Errorf("text after client calls = %v", got) + } +} + func TestResponsesStreamConverter_ToolSearchCall(t *testing.T) { request := ResponsesRequest{Tools: []ResponsesTool{{Type: "tool_search", Execution: "client"}}} converter := NewResponsesStreamConverter("resp_123", "msg_456", "test", request) diff --git a/server/routes_test.go b/server/routes_test.go index 229e80b24..e46398d36 100644 --- a/server/routes_test.go +++ b/server/routes_test.go @@ -194,8 +194,9 @@ func TestLoadedModelLookupConcurrent(t *testing.T) { defer wg.Done() for range 20 { got, err := s.getExistingModelForRunner(name.String(), "") - if err != nil || got != cached { - t.Errorf("concurrent warm lookup = %p, %v; want %p", got, err, cached) + // Lock contention can fall back to loading an equivalent model. + if err != nil || got == nil || got.Name != cached.Name || got.Digest != cached.Digest { + t.Errorf("concurrent warm lookup = %+v, %v; want model %q with digest %q", got, err, cached.Name, cached.Digest) return } }