Files
magpie/internal/gateway/codex_compact_lite_test.go
T
yetone f81913994a gateway: a Responses Lite compaction's summary request offers no tools, and a reply without a summary says what it held (WuSix on Discord)
WuSix (Discord) got "compaction: codex/gpt-6-astra failed to summarise
this conversation 3 times in a row (last: the model wrote no summary)".

codex/gpt-6-astra's catalog entry is Codex's own (codexcat.ownEntry), and
Codex's entry puts gpt-6 on Responses Lite: its tools go as an
additional_tools input item, none in tools. codexInputAs set "tools": []
for magpie's summary request but left that item in, so the model was
asked for the summary with every Codex tool still offered (tool_choice
auto, which codexBody sets anyway). A reply that is a function_call and
no message is "the model wrote no summary". codexInputAs now leaves the
additional_tools item out when it turns the trigger into the summary
prompt; calls in the history stay as they came.

The 502 and the held message now say what the reply held instead
(answeredWith: "(it answered with function_call shell)", "reasoning",
"nothing"), so the next report says which it was.

Hypothesis, not confirmed on a real reply (see below): the fake backend in
TestCodexLiteCompactionOffersNoTools answers a call whenever any tool is
offered, as an agentic model mid-task may. What the test holds is that
the request offers none.

Run at base 398803e3f (origin/main), in this worktree:
- TestCodexLiteCompactionOffersNoTools red before the fix (502
  "compaction: the model wrote no summary"), green after
- gofmt -l internal/: clean
- go vet ./internal/gateway/, GOOS=linux go vet -tags nogui, GOOS=windows
  go vet: ok
- go test -tags nogui -count=1 -timeout 45m ./internal/gateway/: ok
  (366.6s); only comments and docs changed after it started

Real ChatGPT backend, the owner's Codex sign-in (access token read
from ~/.codex/auth.json, not refreshed), 2026-10-23: the exact body
magpie sends upstream, captured from the gateway with a fake backend
(before and after this change), then POSTed to
chatgpt.com/backend-api/codex/responses with Codex's headers and
X-OpenAI-Internal-Codex-Responses-Lite: true. gpt-6-astra, effort low,
reasoning.context all_turns; a developer prompt, a user ask, a
function_call shell (namespace "functions") and its output, then the
compaction prompt. Three tries each:
  (a) old: additional_tools kept, tool_choice auto  200 x3, message
      (978/1010/1037 chars)
  (b) this change: additional_tools left out        200 x3, message
      (1176/1003/991 chars)
  (c) additional_tools kept, tool_choice none       200 x3, message
      (972/1009/1026 chars)
So the backend takes a Lite request whose history calls the "functions"
namespace with no additional_tools item: the fix doesn't 400. The old
request also summarised on this short history, so the cause of WuSix's
reports isn't reproduced on the real backend; with a long tool-heavy
conversation at a higher effort it may still be, and answeredWith will
say.

Not tried: a long conversation, efforts above low, and WuSix's own
conversation.
2026-10-23 09:20:23 +08:00

125 lines
5.0 KiB
Go

package gateway
import (
"encoding/base64"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// WuSix on Discord, codex/gpt-6-astra: "compaction: codex/gpt-6-astra
// failed to summarise this conversation 3 times in a row (last: the model
// wrote no summary)". codex/gpt-6-astra's catalog entry is Codex's own
// (codexcat.ownEntry), which puts gpt-6 on Responses Lite, so Codex sends
// its tools as an additional_tools input item and none in tools. magpie's
// summary request emptied tools and left that item in, so the model was
// still offered every tool Codex has, and an agentic model in the middle
// of its work called one rather than writing the summary: a function_call
// and no message, which is no summary.
//
// The backend here answers as such a model would: a call to the first tool
// it is offered, wherever it is offered, and the summary only when it is
// offered none.
func TestCodexLiteCompactionOffersNoTools(t *testing.T) {
codexSignedIn(t)
var mu sync.Mutex
var got [][]byte
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, _ := io.ReadAll(r.Body)
mu.Lock()
got = append(got, b)
mu.Unlock()
var q struct {
Input []struct {
Type string `json:"type"`
Tools []map[string]any `json:"tools"`
} `json:"input"`
Tools []map[string]any `json:"tools"`
}
json.Unmarshal(b, &q)
offered := len(q.Tools) > 0
for _, it := range q.Input {
offered = offered || it.Type == "additional_tools" && len(it.Tools) > 0
}
item := `{"type":"message","role":"assistant","phase":"final_answer","content":[{"type":"output_text","text":"SUMMARY of the work"}]}`
if offered {
item = `{"type":"function_call","call_id":"c2","name":"shell","namespace":"functions","arguments":"{\"cmd\":\"go test ./...\"}"}`
}
w.Header().Set("Content-Type", "text/event-stream")
io.WriteString(w, sse(
`data: {"type":"response.created","response":{"id":"resp_1","model":"gpt-6-astra"}}`,
`data: {"type":"response.output_item.done","output_index":0,"item":`+item+`}`,
`data: {"type":"response.completed","response":{"id":"resp_1","status":"completed","usage":{"input_tokens":9,"output_tokens":2}}}`))
}))
t.Cleanup(up.Close)
was := provider.CodexBase
provider.CodexBase = up.URL + "/backend-api/codex"
t.Cleanup(func() { provider.CodexBase = was })
// liteRequest's conversation, on codex/gpt-6-astra, with Codex's
// compaction_trigger after it
body := strings.Replace(liteRequest, `"model":"fake/m1"`, `"model":"codex/gpt-6-astra","reasoning":{"effort":"xhigh","context":"all_turns"}`, 1)
body = strings.Replace(body, "\n]}", `,{"type":"compaction_trigger"}]}`, 1)
if !strings.Contains(body, "compaction_trigger") || !strings.Contains(body, "codex/gpt-6-astra") {
t.Fatalf("fixture: %s", body)
}
rec := httptest.NewRecorder()
req := httptest.NewRequest("POST", CodexPath+"/responses", strings.NewReader(body))
req.Header.Set("Authorization", "Bearer chatgpt-token")
req.Header.Set("chatgpt-account-id", "acct-1")
req.Header.Set("X-OpenAI-Internal-Codex-Responses-Lite", "true")
New().Handler().ServeHTTP(rec, req)
if rec.Code != 200 {
t.Fatalf("status %d: %s", rec.Code, rec.Body.String())
}
mu.Lock()
defer mu.Unlock()
if len(got) != 1 {
t.Fatalf("the backend was asked %d times, not once", len(got))
}
sent := string(got[0])
if strings.Contains(sent, `"additional_tools"`) || strings.Contains(sent, `"apply_patch"`) || strings.Contains(sent, `"mcp__probe"`) {
t.Errorf("the summary request offers the conversation's tools: %s", sent)
}
if !strings.Contains(sent, "CONTEXT CHECKPOINT COMPACTION") || !strings.Contains(sent, `"call_id":"c1"`) || !strings.Contains(sent, "list the files") {
t.Errorf("the summary request lost the conversation or the prompt: %s", sent)
}
var item map[string]any
for _, e := range events(rec.Body.String()) {
if e["type"] == "response.output_item.done" {
item, _ = e["item"].(map[string]any)
}
}
enc, _ := item["encrypted_content"].(string)
sum, _ := base64.StdEncoding.DecodeString(strings.TrimPrefix(enc, magpieCompaction))
if item["type"] != "compaction" || string(sum) != "SUMMARY of the work" {
t.Errorf("compaction: %s", rec.Body.String())
}
}
// A summary reply without a summary says what it held instead, so the
// next report says whether the model called a tool, only reasoned, or
// answered nothing.
func TestCompactNoSummarySaysWhatCame(t *testing.T) {
for _, c := range []struct {
reply, want string
}{
{`{"id":"r","status":"completed","output":[{"type":"reasoning"},{"type":"function_call","name":"shell"},{"type":"function_call","name":"shell"}]}`, " (it answered with reasoning, function_call shell)"},
{`{"id":"r","status":"completed","output":[]}`, " (it answered with nothing)"},
} {
res, err := compactReply([]byte(c.reply))
if err != nil {
t.Fatal(err)
}
if got := answeredWith(res.Output); got != c.want {
t.Errorf("%s: %q, want %q", c.reply, got, c.want)
}
}
}