Files
magpie/internal/gateway/codex_titles_test.go
T
yetoneandchiperman 00d44d6928 routing: Codex's composer predictions story says their model is picked in Codex's row on the Agents page (#3425, chiperman)
Routing's Kind description for composer_predictions still sent users to
Settings › General › Codex composer predictions. That setting moved to
Codex's row on the Agents page. chiperman's commit (5e372e168) fixed the
Routing text in five languages. It is ported here with their zh, zh-TW
and ja; the de wording is mine.

Grepping for other texts that still name the old Settings place for a
setting that moved into an agent's row found four more, now fixed:
- app.js compactField, the provider editor's "Compact at": empty follows
  Long conversations, now "in their settings on the Agents page (else in
  Settings)". The field's text is one string now, and the header comment
  no longer repeats it, so app.js stays inside its budget (unchanged).
- gateway sealedWayOut: "Codex subagents on any model" is in Codex's
  settings on magpie's Agents page, not "magpie's Settings".
- gateway codex_titles: "Codex composer predictions are off" and "Codex
  titles are off" answers name the same place.
- cli `magpie model` help text.
Each gateway message has a test that fails on the old wording, and the
GUI files assert the new place and the absence of the old one.

Run on origin/main 46c418832 with this commit and #3429's after it (the
pushed pair; the Go and GUI runs below were at the pair's head):
- gofmt -l: nothing. go vet (darwin) of gui/gateway/cli,
  GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...: clean.
- go test -count=1 -timeout 45m ./internal/gui/ ./internal/cli/
  ./internal/gateway/: ok (gui 133s, cli 39s, gateway 405s)
- GUI, both engines: compact-at, routing-kind, mobile-web, sidebar,
  refresh-page, gateway-port: 64/76; the 12 reds are sidebar's page list
  missing Insights › Masking, red on origin/main too (#3498). With that
  list given "masking" for the run only, sidebar is 18/18.
- Broken by hand: the old app.js/i18n text makes compact-at fail on
  "says where Long conversations is"; the old Routing text makes
  routing-kind fail.
- Whole GUI suite (both engines, -concurrency 4) at the pre-rebase pair
  c28576084: 12,519 tests, 12,350 pass, 169 fail in 31 files. Each red
  file was run alone at that head and at origin/main 641b1217f. All are
  red on main too or pass alone, and each has an issue: the 30 files of
  #3527's list (#3497, #3498, #3488, #3523, #2648, #3479, #3099, #3402,
  #3404, #3500, #3408-#3415, #3094, #2174, #3091, #3104, #3406),
  gateway-caller-keys (#2572, #1671), plugin-github (red alone on main,
  #3218), mobile-web (passes against the commit's base; after the rebase
  it passes at this head), codex-resets-used (#3535),
  routing-busy-frames (#3557) and zhipu-team (#3558). signin-port-blocked's
  11 were ENOSPC from a full disk mid-run; 40/40 alone.
- Not tried: a real Codex session hitting the sealed-task or titles
  message; the text was read in the tests only.

Co-authored-by: chiperman <34889778+chiperman@users.noreply.github.com>
2026-10-25 12:06:13 +08:00

420 lines
17 KiB
Go

package gateway
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/settings"
"github.com/yetone/magpie/internal/usage"
)
// codexTitleRequest is a title request as Codex's TUI sends one on its own
// model, signed in to ChatGPT: a hidden thread_title turn, its schema in
// text.format; with subagent, as the app names one in x-openai-subagent.
func codexTitleRequest(t *testing.T, s *Server, subagent string) *httptest.ResponseRecorder {
t.Helper()
body := `{"model":"gpt-5.6-luna","stream":true,"input":[{"type":"message","role":"user","content":[{"type":"input_text","text":"Generate a concise title. User prompt:\nfix the login bug"}]}],` +
`"text":{"format":{"type":"json_schema","name":"codex_output_schema","strict":true,"schema":{"type":"object","properties":{"title":{"type":"string"}},"required":["title"],"additionalProperties":false}}}}`
rec := httptest.NewRecorder()
req := httptest.NewRequest("POST", CodexPath+"/responses", strings.NewReader(body))
req.Header.Set("Authorization", "Bearer chatgpt-token")
req.Header.Set("session_id", "title-thread")
if subagent != "" {
req.Header.Set("x-openai-subagent", subagent)
} else {
req.Header.Set("x-codex-turn-metadata", `{"thread_source":"thread_title","parent_thread_id":"main-chat"}`)
}
s.Handler().ServeHTTP(rec, req)
return rec
}
// outputText is the text of the message items in a Responses stream's
// response.completed.
func outputText(t *testing.T, body string) (string, int) {
t.Helper()
for _, ev := range events(body) {
if ev["type"] != "response.completed" {
continue
}
resp, _ := ev["response"].(map[string]any)
out, _ := resp["output"].([]any)
var text strings.Builder
for _, it := range out {
m, _ := it.(map[string]any)
cs, _ := m["content"].([]any)
for _, c := range cs {
cm, _ := c.(map[string]any)
s, _ := cm["text"].(string)
text.WriteString(s)
}
}
return text.String(), len(out)
}
t.Fatalf("no response.completed in %q", body)
return "", 0
}
// Codex's title requests go where Settings says (#705): as they came to
// Codex's ChatGPT sign-in by default; turned off, answered here with no
// title and sent nowhere; or to a model of magpie's, its plain answer
// handed back as the {"title": …} Codex reads. Each stays a title request
// in the Usage and Routing views.
func TestCodexTitlesSetting(t *testing.T) {
f := &fake{t: t, reply: sse(
`data: {"id":"c1","choices":[{"index":0,"delta":{"content":"Fix login bug"}}]}`,
`data: {"id":"c1","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":30,"completion_tokens":4}}`,
`data: [DONE]`)}
setup(t, provider.Chat, f)
chatgptCalls := 0
chatgpt(t, func(w http.ResponseWriter, r *http.Request) {
io.ReadAll(r.Body)
chatgptCalls++
w.Header().Set("Content-Type", "text/event-stream")
io.WriteString(w, sse(`data: {"type":"response.completed","response":{"id":"r1","output":[{"type":"message","role":"assistant","content":[{"type":"output_text","text":"{\"title\":\"From ChatGPT\"}"}]}],"usage":{"input_tokens":9,"output_tokens":2}}}`))
})
save := func(v string) {
t.Helper()
st := settings.Load()
st.CodexTitles = v
if err := settings.Save(st); err != nil {
t.Fatal(err)
}
}
// default: as before, to Codex's own sign-in
s := New()
rec := codexTitleRequest(t, s, "")
if text, _ := outputText(t, rec.Body.String()); chatgptCalls != 1 || f.calls != 0 || text != `{"title":"From ChatGPT"}` {
t.Fatalf("default: chatgpt %d, magpie model %d, %q", chatgptCalls, f.calls, text)
}
// off: nothing goes out, a finished reply with no title in it
save("off")
for _, sub := range []string{"", "thread_title", "thread_title_reconsideration"} {
rec = codexTitleRequest(t, s, sub)
text, n := outputText(t, rec.Body.String())
if rec.Code != 200 || n != 0 || text != "" || chatgptCalls != 1 || f.calls != 0 {
t.Fatalf("off (%q): %d %q, %d items, chatgpt %d, magpie model %d", sub, rec.Code, rec.Body.String(), n, chatgptCalls, f.calls)
}
}
// a model of magpie's: it writes the title, in the shape Codex reads
save("fake/m1")
rec = codexTitleRequest(t, s, "thread_title")
if text, _ := outputText(t, rec.Body.String()); rec.Code != 200 || text != `{"title":"Fix login bug"}` || f.calls != 1 || chatgptCalls != 1 {
t.Fatalf("routed: %d %q, magpie model %d, chatgpt %d", rec.Code, rec.Body.String(), f.calls, chatgptCalls)
}
var sent struct {
Model string `json:"model"`
}
json.Unmarshal(f.got, &sent)
if sent.Model != "m1" {
t.Errorf("the magpie model was asked for %q", sent.Model)
}
// a turn of the conversation is not a title: it goes as before
rec = httptest.NewRecorder()
req := httptest.NewRequest("POST", CodexPath+"/responses", strings.NewReader(`{"model":"gpt-5.5","stream":true,"input":"hi"}`))
req.Header.Set("Authorization", "Bearer chatgpt-token")
s.Handler().ServeHTTP(rec, req)
if chatgptCalls != 2 || f.calls != 1 {
t.Errorf("a plain turn: chatgpt %d, magpie model %d", chatgptCalls, f.calls)
}
// every title request is in the ledger as one: ChatGPT's, the three
// answered here, the magpie model's
var kinds []string
for _, u := range usage.Load(time.Time{}) {
kinds = append(kinds, u.Provider+":"+u.Kind)
if u.Provider == "magpie" && (!u.IsRejected() || u.Status != 200) {
t.Errorf("an answered-here title %+v", u)
}
// and says where the setting is: Codex's row on the Agents page
// since f1e4516d, not Settings (#3425)
if u.Provider == "magpie" && !strings.Contains(u.Error, "in Codex's settings on magpie's Agents page") {
t.Errorf("an answered-here title says %q", u.Error)
}
}
want := "openai:thread_title magpie:thread_title magpie:thread_title magpie:thread_title_reconsideration fake:thread_title openai:"
if got := strings.Join(kinds, " "); got != want {
t.Errorf("ledger %s\nwant %s", got, want)
}
st := s.Trace(t.Context(), 0, 0)
titles := 0
for _, r := range st.Routes {
if isTitleKind(r.Kind) {
titles++
}
}
if titles != 5 {
t.Errorf("Routing view titles %d, want 5: %+v", titles, st.Routes)
}
}
// codexProviderTitle is a title request as Codex's TUI sends one with
// magpie as its model provider (model_provider = "magpie", #743): to
// magpie's own /v1/responses, on the conversation's model, a magpie id —
// or, with backend, to the ChatGPT backend path on that magpie model.
func codexProviderTitle(t *testing.T, s *Server, backend bool) *httptest.ResponseRecorder {
t.Helper()
body := `{"model":"fake/m1","stream":true,"reasoning":{"effort":"low"},"input":[{"type":"message","role":"user","content":[{"type":"input_text","text":"Generate a concise title. User prompt:\nfix the login bug"}]}],` +
`"text":{"format":{"type":"json_schema","name":"codex_output_schema","strict":true,"schema":{"type":"object","properties":{"title":{"type":"string","minLength":1,"maxLength":36}},"required":["title"],"additionalProperties":false}}}}`
path := "/v1/responses"
if backend {
path = CodexPath + "/responses"
}
rec := httptest.NewRecorder()
req := httptest.NewRequest("POST", path, strings.NewReader(body))
req.Header.Set("session_id", "title-thread")
req.Header.Set("x-codex-turn-metadata", `{"thread_source":"thread_title","parent_thread_id":"main-chat"}`)
s.Handler().ServeHTTP(rec, req)
return rec
}
// A title Codex asks one of magpie's models for is one it can read
// (#743): with magpie as Codex's provider the request comes to /v1, where
// the Codex titles setting is heeded too, and with no model set there the
// conversation's model's plain answer — a Chat model is never shown the
// schema — is handed back as {"title": …}, as it is on the ChatGPT path.
// An answer with no title in it is in the Routing and Usage views as the
// failure it is to Codex.
func TestCodexTitleOnMagpieProvider(t *testing.T) {
answer := "Fix login bug"
f := &fake{t: t}
setup(t, provider.Chat, f)
reply := func() string {
return sse(
`data: {"id":"c1","choices":[{"index":0,"delta":{"content":`+strconvQuote(answer)+`}}]}`,
`data: {"id":"c1","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":30,"completion_tokens":4}}`,
`data: [DONE]`)
}
save := func(v string) {
t.Helper()
st := settings.Load()
st.CodexTitles = v
if err := settings.Save(st); err != nil {
t.Fatal(err)
}
}
s := New()
for _, backend := range []bool{false, true} {
f.reply = reply()
rec := codexProviderTitle(t, s, backend)
if text, _ := outputText(t, rec.Body.String()); rec.Code != 200 || text != `{"title":"Fix login bug"}` {
t.Fatalf("backend %v: %d %q", backend, rec.Code, rec.Body.String())
}
}
calls := f.calls
// the setting holds on /v1 as well
save("off")
rec := codexProviderTitle(t, s, false)
if text, n := outputText(t, rec.Body.String()); rec.Code != 200 || n != 0 || text != "" || f.calls != calls {
t.Fatalf("off on /v1: %d %q, upstream calls %d", rec.Code, rec.Body.String(), f.calls-calls)
}
save("")
// a turn of the conversation on /v1 is left as it was
f.reply = reply()
if code, body := post(t, "/v1/responses", `{"model":"fake/m1","stream":true,"input":"hi"}`); code != 200 || strings.Contains(body, `{\"title\"`) {
t.Fatalf("a plain turn: %d %q", code, body)
}
// no title in the answer: none for Codex, and the call says why
answer = ""
f.reply = reply()
rec = codexProviderTitle(t, s, false)
if text, n := outputText(t, rec.Body.String()); rec.Code != 200 || n != 0 || text != "" {
t.Fatalf("no title: %d %q", rec.Code, rec.Body.String())
}
var last usage.Record
for _, u := range usage.Load(time.Time{}) {
last = u
}
if last.Kind != "thread_title" || !strings.HasPrefix(last.Error, "title: ") || !last.Failed() {
t.Errorf("the ledger's no-title call %+v", last)
}
st := s.Trace(t.Context(), 0, 0)
var r Route
for _, c := range st.Routes {
if c.Seq > r.Seq {
r = c
}
}
if !isTitleKind(r.Kind) || !strings.HasPrefix(r.Error, "title: ") {
t.Errorf("the Routing view's no-title call %+v", r)
}
}
func strconvQuote(s string) string { b, _ := json.Marshal(s); return string(b) }
func TestTitleJSON(t *testing.T) {
for in, want := range map[string]string{
`{"title":"Fix login bug"}`: `{"title":"Fix login bug"}`,
"```json\n{\"title\": \"Fix login\"}\n```": `{"title":"Fix login"}`,
`"Fix login bug"`: `{"title":"Fix login bug"}`,
"Title: Fix login\n\nmore words": `{"title":"Fix login"}`,
"**修复登录问题**": `{"title":"修复登录问题"}`,
"<think>\nthe user wants\n</think>\n\nFix login": `{"title":"Fix login"}`,
"": "",
`{"name":"x"}`: "",
} {
if got := titleJSON(in, titleShape{}); got != want {
t.Errorf("titleJSON(%q) = %q, want %q", in, got, want)
}
}
}
// #743: a Codex whose title schema requires a description beside the
// title, no other field allowed, is given both, each in its bounds; the
// model's own description when its JSON has one.
func TestTitleJSONFillsTheSchema(t *testing.T) {
body := `{"text":{"format":{"type":"json_schema","strict":true,"schema":{"type":"object","properties":{"title":{"type":"string","minLength":1,"maxLength":36},"description":{"type":"string","minLength":1}},"required":["title","description"],"additionalProperties":false}}}}`
shape := titleShapeOf([]byte(body))
for in, want := range map[string]string{
"Fix login bug": `{"description":"Fix login bug","title":"Fix login bug"}`,
`{"title":"Fix login","description":"The login form rejects valid passwords"}`: `{"description":"The login form rejects valid passwords","title":"Fix login"}`,
"Fix the login bug that rejects every valid password on mobile": `{"description":"Fix the login bug that rejects every valid password on mobile","title":"Fix the login bug that rejects every"}`,
"": "",
} {
if got := titleJSON(in, shape); got != want {
t.Errorf("titleJSON(%q) = %s, want %s", in, got, want)
}
}
if got := titleJSON("Fix login", titleShapeOf([]byte(`{}`))); got != `{"title":"Fix login"}` {
t.Errorf("no schema: %s", got)
}
}
func codexDescriptionRequest(t *testing.T, s *Server) *httptest.ResponseRecorder {
t.Helper()
// ChatGPT.app's thread_description schema: description is required and has
// a minimum length, but no maximum.
body := `{"model":"gpt-6-luna","stream":true,"input":[{"type":"message","role":"user","content":[{"type":"input_text","text":"Describe the thread"}]}],` +
`"text":{"format":{"type":"json_schema","strict":true,"schema":{"type":"object","properties":{"description":{"type":"string","minLength":1}},"required":["description"],"additionalProperties":false}}}}`
rec := httptest.NewRecorder()
req := httptest.NewRequest("POST", CodexPath+"/responses", strings.NewReader(body))
req.Header.Set("Authorization", "Bearer chatgpt-token")
req.Header.Set("session_id", "description-thread")
req.Header.Set("x-codex-turn-metadata", `{"thread_source":"thread_description"}`)
s.Handler().ServeHTTP(rec, req)
return rec
}
func TestCodexDescriptionsSetting(t *testing.T) {
f := &fake{t: t, reply: sse(
`data: {"id":"c1","choices":[{"index":0,"delta":{"content":"Reviewed the routing change and verified it"}}]}`,
`data: {"id":"c1","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":30,"completion_tokens":8}}`,
`data: [DONE]`)}
setup(t, provider.Chat, f)
chatgptCalls := 0
chatgpt(t, func(w http.ResponseWriter, r *http.Request) {
io.ReadAll(r.Body)
chatgptCalls++
w.Header().Set("Content-Type", "text/event-stream")
io.WriteString(w, sse(`data: {"type":"response.completed","response":{"id":"r1","output":[{"type":"message","role":"assistant","content":[{"type":"output_text","text":"{\"description\":\"From ChatGPT\"}"}]}],"usage":{"input_tokens":9,"output_tokens":2}}}`))
})
save := func(v string) {
t.Helper()
st := settings.Load()
st.CodexDescriptions = v
if err := settings.Save(st); err != nil {
t.Fatal(err)
}
}
s := New()
rec := codexDescriptionRequest(t, s)
if text, _ := outputText(t, rec.Body.String()); chatgptCalls != 1 || f.calls != 0 || text != `{"description":"From ChatGPT"}` {
t.Fatalf("default: chatgpt %d, magpie model %d, %q", chatgptCalls, f.calls, text)
}
save("fake/m1")
rec = codexDescriptionRequest(t, s)
if text, _ := outputText(t, rec.Body.String()); rec.Code != 200 || text != `{"description":"Reviewed the routing change and verified it"}` || f.calls != 1 || chatgptCalls != 1 {
t.Fatalf("routed: %d %q, magpie model %d, chatgpt %d", rec.Code, rec.Body.String(), f.calls, chatgptCalls)
}
var sent struct {
Model string `json:"model"`
}
json.Unmarshal(f.got, &sent)
if sent.Model != "m1" {
t.Errorf("the magpie model was asked for %q", sent.Model)
}
var kinds []string
for _, u := range usage.Load(time.Time{}) {
kinds = append(kinds, u.Provider+":"+u.Kind)
}
want := "openai:thread_description fake:thread_description"
if got := strings.Join(kinds, " "); got != want {
t.Errorf("ledger %s\nwant %s", got, want)
}
}
func TestCodexDescriptionFallbackOnMagpie(t *testing.T) {
for _, path := range []string{"/v1/responses", "/backend-api/codex/responses"} {
t.Run(path, func(t *testing.T) {
f := &fake{t: t, reply: sse(
`data: {"id":"c1","choices":[{"index":0,"delta":{"content":"Plain description"}}]}`,
`data: {"id":"c1","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":10,"completion_tokens":2}}`,
`data: [DONE]`)}
setup(t, provider.Chat, f)
s := New()
body := `{"model":"fake/m1","stream":true,"input":"Describe the thread",` +
`"text":{"format":{"type":"json_schema","schema":{"type":"object","properties":{"description":{"type":"string","minLength":1}},"required":["description"]}}}}`
rec := httptest.NewRecorder()
req := httptest.NewRequest("POST", path, strings.NewReader(body))
req.Header.Set("x-codex-turn-metadata", `{"thread_source":"thread_description"}`)
s.Handler().ServeHTTP(rec, req)
if text, _ := outputText(t, rec.Body.String()); rec.Code != 200 || text != `{"description":"Plain description"}` || f.calls != 1 {
t.Fatalf("fallback: %d %q, calls %d", rec.Code, rec.Body.String(), f.calls)
}
})
}
}
func TestNoDescription(t *testing.T) {
f := &fake{t: t, reply: sse(
`data: {"id":"c1","choices":[{"index":0,"delta":{"content":" "}}]}`,
`data: {"id":"c1","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":10,"completion_tokens":1}}`,
`data: [DONE]`)}
setup(t, provider.Chat, f)
st := settings.Load()
st.CodexDescriptions = "fake/m1"
if err := settings.Save(st); err != nil {
t.Fatal(err)
}
s := New()
rec := codexDescriptionRequest(t, s)
text, n := outputText(t, rec.Body.String())
if rec.Code != 200 || n != 0 || text != "" {
t.Fatalf("empty reply: %d %q, %d items", rec.Code, rec.Body.String(), n)
}
var last usage.Record
for _, u := range usage.Load(time.Time{}) {
last = u
}
if last.Kind != "thread_description" || !strings.HasPrefix(last.Error, "description: ") || !last.Failed() {
t.Fatalf("unusable reply was not flagged: %+v", last)
}
}
func TestDescriptionJSON(t *testing.T) {
for in, want := range map[string]string{
"A short note": `{"description":"A short note"}`,
`{"description":"From JSON"}`: `{"description":"From JSON"}`,
"```json\n{\"description\":\"Fenced\"}\n```": `{"description":"Fenced"}`,
"": "",
} {
if got := descriptionJSON(in); got != want {
t.Errorf("descriptionJSON(%q) = %s, want %s", in, got, want)
}
}
}