Files
magpie/internal/gateway/codex_title_incomplete_test.go
T
yetone a21a2b54f1 gateway: a Codex title the model was cut short on before any text goes back as response.incomplete with its reason and usage, not as a completed reply with nothing in it (#1466, Edward4226)
codexTitle read the title model's reply with compactReply, which kept
the output and usage of response.completed and response.incomplete alike
and dropped how it ended, and writeTitleReply always wrote status
"completed". A title cut short at max_output_tokens (all of it spent
reasoning) reached Codex as a finished reply with output [].

What changed:
- compactResult keeps the response's status and incomplete_details
  (from the terminal event's response, or its type).
- codexTitle: when no title can be read and the reply was incomplete,
  writeTitleEnd streams response.created then one response.incomplete,
  status "incomplete", the vendor's incomplete_details and usage. A
  title read from the reply still completes with it; Titles off still
  completes with nothing (writeTitleReply is writeTitleEnd "completed").
- Compaction (codexCompact) is left as it was: a summary cut short with
  text in it is still used.

Codex: codex-rs/codex-api/src/sse/responses.rs takes response.incomplete
with a reason other than interrupted/content_filter as a stream error
("Incomplete response returned, reason: max_output_tokens"), which
core/src/responses_retry.rs retries up to the provider's
stream_max_retries; then the hidden title turn fails and
tui/src/temporary_structured_request.rs sets no title, as it did for
the empty completed reply. This is what Codex does when its own backend
answers so.

Run:
- TestCodexTitleIncompleteStaysIncomplete (Responses upstream, #1466's
  two-field schema) and TestCodexTitleIncompleteFromChat (Chat upstream,
  finish_reason length) fail on the old code (one response.completed,
  status completed) and pass now; the same test's whole title under the
  same schema completes. Existing TestCodexTitle*/TestTitle* pass.
- gofmt -l clean; go vet -tags nogui ./internal/gateway on darwin,
  GOOS=linux, GOOS=windows.
- go test -tags nogui -count=1 ./... under a temp HOME: all ok (with
  ./internal/gateway and ./ rerun after this change).
- Real use: the built binary's gateway in a sandbox HOME with only the
  owner's DeepSeek provider, the issue's title request on
  deepseek-flash. max_output_tokens 16: old response.completed,
  output [], status completed; new one response.incomplete, reason
  max_output_tokens, usage 16 reasoning tokens. 2048: a title and
  description, response.completed, before and after.
2026-10-10 14:14:47 +08:00

99 lines
5.2 KiB
Go

package gateway
import (
"net/http/httptest"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// codexTitleTwoFields is #1466's title request: Codex's schema with a
// description beside the title, on /v1 with x-openai-subagent.
func codexTitleTwoFields(t *testing.T, s *Server) *httptest.ResponseRecorder {
t.Helper()
body := `{"model":"fake/m1","stream":true,"max_output_tokens":256,"reasoning":{"effort":"low"},"input":[{"role":"user","content":[{"type":"input_text","text":"Generate a concise title and description for: Research methods for selecting the appearance of a virtual clothing presenter. Return the required JSON object."}]}],` +
`"text":{"format":{"type":"json_schema","name":"codex_output_schema","strict":true,"schema":{"type":"object","properties":{"title":{"type":"string","maxLength":36},"description":{"type":"string","minLength":1}},"required":["title","description"],"additionalProperties":false}}}}`
rec := httptest.NewRecorder()
req := httptest.NewRequest("POST", "/v1/responses", strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("x-openai-subagent", "thread_title")
s.Handler().ServeHTTP(rec, req)
return rec
}
// terminals is a Responses stream's terminal events.
func terminals(body string) []map[string]any {
var out []map[string]any
for _, ev := range events(body) {
switch ev["type"] {
case "response.completed", "response.incomplete", "response.failed":
out = append(out, ev)
}
}
return out
}
// A title the vendor cut short before any text (max_output_tokens, all of
// it spent reasoning) goes back to Codex as the response.incomplete it was,
// its reason and usage kept, the one terminal event in the stream — not as
// a completed reply with nothing in it (#1466). A title that came whole
// under the same schema still completes with it.
func TestCodexTitleIncompleteStaysIncomplete(t *testing.T) {
cut := sse(
`data: {"type":"response.created","response":{"id":"resp_cut","object":"response","status":"in_progress","output":[]}}`,
`data: {"type":"response.output_item.done","output_index":0,"item":{"type":"reasoning","id":"rs_1","summary":[]}}`,
`data: {"type":"response.incomplete","response":{"id":"resp_cut","object":"response","status":"incomplete","incomplete_details":{"reason":"max_output_tokens"},"output":[{"type":"reasoning","id":"rs_1","summary":[]}],"usage":{"input_tokens":60,"output_tokens":256,"output_tokens_details":{"reasoning_tokens":256},"total_tokens":316}}}`)
whole := sse(
`data: {"type":"response.created","response":{"id":"resp_ok","object":"response","status":"in_progress","output":[]}}`,
`data: {"type":"response.output_item.done","output_index":0,"item":{"type":"message","id":"msg_1","role":"assistant","status":"completed","content":[{"type":"output_text","text":"{\"title\":\"Virtual presenter looks\",\"description\":\"Ways to choose a virtual clothing presenter's appearance\"}"}]}}`,
`data: {"type":"response.completed","response":{"id":"resp_ok","object":"response","status":"completed","output":[],"usage":{"input_tokens":60,"output_tokens":40,"total_tokens":100}}}`)
f := &fake{t: t, ctype: "text/event-stream"}
setup(t, provider.Responses, f)
s := New()
f.reply = cut
rec := codexTitleTwoFields(t, s)
ends := terminals(rec.Body.String())
if rec.Code != 200 || len(ends) != 1 || ends[0]["type"] != "response.incomplete" {
t.Fatalf("cut short: %d, terminal events %v: %s", rec.Code, ends, rec.Body.String())
}
resp, _ := ends[0]["response"].(map[string]any)
details, _ := resp["incomplete_details"].(map[string]any)
used, _ := resp["usage"].(map[string]any)
out, _ := resp["output"].([]any)
if resp["status"] != "incomplete" || details["reason"] != "max_output_tokens" || used["output_tokens"] != float64(256) || len(out) != 0 {
t.Errorf("cut short: %v", resp)
}
f.reply = whole
rec = codexTitleTwoFields(t, s)
ends = terminals(rec.Body.String())
if text, _ := outputText(t, rec.Body.String()); rec.Code != 200 || len(ends) != 1 ||
text != `{"description":"Ways to choose a virtual clothing presenter's appearance","title":"Virtual presenter looks"}` {
t.Fatalf("whole: %d %q", rec.Code, rec.Body.String())
}
if resp, _ := ends[0]["response"].(map[string]any); resp["status"] != "completed" || resp["incomplete_details"] != nil {
t.Errorf("whole: %v", resp)
}
}
// A Chat model's title cut short (finish_reason "length", no text) goes
// back incomplete too, for max_output_tokens.
func TestCodexTitleIncompleteFromChat(t *testing.T) {
f := &fake{t: t, reply: sse(
`data: {"id":"c1","choices":[{"index":0,"delta":{"reasoning_content":"Let me think"}}]}`,
`data: {"id":"c1","choices":[{"index":0,"delta":{},"finish_reason":"length"}],"usage":{"prompt_tokens":60,"completion_tokens":256}}`,
`data: [DONE]`)}
setup(t, provider.Chat, f)
rec := codexTitleTwoFields(t, New())
ends := terminals(rec.Body.String())
if rec.Code != 200 || len(ends) != 1 || ends[0]["type"] != "response.incomplete" {
t.Fatalf("%d, terminal events %v: %s", rec.Code, ends, rec.Body.String())
}
resp, _ := ends[0]["response"].(map[string]any)
if details, _ := resp["incomplete_details"].(map[string]any); details["reason"] != "max_output_tokens" || resp["usage"] == nil {
t.Errorf("%v", resp)
}
}