Files
magpie/internal/gateway/bare_reasoning_test.go
T
ggbdpq f822689ada gateway: a relay's "referenced reasoning item" refusal is read as an unreadable item (#3062)
What changed: unreadableItem, the matcher that reads a 400 as "this input
item can't be read", now also takes the wording a Responses passthrough
relay answers in — "Referenced reasoning item 'rs_…' was not found or has
expired" — beside OpenAI's own ("Item with id 'rs_…' not found",
"encrypted content for item"). The refusal is recognized wherever the
matcher is read: the passthrough turn is asked again without the bare
reasoning (and the provider's model isn't sent them again), the ChatGPT
backend drops the named item and asks the rest again, and a compaction
refused so is asked again as text.

Why: a Codex thread behind such a relay (opencode.ai's OpenCode Go)
collects reasoning ids the relay can't resolve, two ways — a turn failed
over to a provider magpie translates leaves Codex reasoning items magpie
minted (an rs_ id and a summary, nothing sealed), which never existed on
the relay's store; and the relay seals its own reasoning only briefly, so
after a resume gap ids from its earlier replies read as expired. Codex
replays the whole history every turn, and the relay's 400, worded by the
relay and naming no input param, passed refusesInput unread — so the
thread 400s on every later turn. The reporter's repro now strips the
named bare item, asks again, and answers 200.

Run: TestRelayRefusedReasoningItemReadAsUnreadable and a TestRefusesInput
row for the reporter's exact body are red before the change and green
after; the bare-reasoning, foreign-seal and item-not-found families stay
green (also with -race, and under an ablation and a mutation of the new
wording, where the old wordings' tests stay green); gofmt clean; go vet
on darwin, linux and windows; builds with -tags nogui on the three. Full
./internal/gateway/ is green on the third run (511s); a first run hit go
test's default 10m timeout and a second failed only
TestAsyncGatewayResponseDoesNotHoldStream — a fixed-10s-guard streaming
hook test unrelated to this change, which passes 3/3 alone.

Generated-by: GLM-5.3-Flash (ZCode)

Landed from ggbdpq's branch (patch posted on the issue; sha256/patch-id
checked against the issue) by yetone, rebased on origin/main 2026-11-03.
Run at the pushed tree, on mote (macOS): go test -tags nogui -count=1 of
internal/gateway with -run over this batch's tests and their families
(PlainSchema, Antigravity, AutoMode, WebP, ClaudeTUI, NativeRelay,
Namespace, Grok, Zed, Codex, ClinePass, rewrite) ok; -race -count=3 of
the new tests ok; GOOS=linux and GOOS=windows go vet of internal/gateway
and gofmt -l clean. The full gateway-and-importers run, started at an
earlier base, was still running at the push; its reds get issues.
Broken by hand: the regexp change reverted, test kept:
TestRelayRefusedReasoningItemReadAsUnreadable red; restored, green.
Not tried against the reporter's relay; the fixture is its literal body.
2026-11-03 02:27:57 +08:00

291 lines
12 KiB
Go

package gateway
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// relayUpstream plays a Responses relay in front of OpenAI as #1044's
// reporter captured it: a request whose input has reasoning with nothing
// sealed in it is turned away with the relay's own 400, word for word,
// naming only the input; without such reasoning the turn is answered. With
// expired set it turns the same reasoning away in an OpenCode Go relay's
// own words, naming the id it can't resolve (#3062). With refuse unset it
// takes the reasoning, as a relay for a DeepSeek model does (#388). With
// status set, every request gets that error instead.
type relayUpstream struct {
mu sync.Mutex
refuse bool
expired bool
status int
errMsg string
inputs [][]map[string]any
}
func (u *relayUpstream) ServeHTTP(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
var q struct {
Input []map[string]any `json:"input"`
}
json.Unmarshal(body, &q)
u.mu.Lock()
u.inputs = append(u.inputs, q.Input)
u.mu.Unlock()
if u.status != 0 {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(u.status)
io.WriteString(w, u.errMsg)
return
}
for _, it := range q.Input {
enc, _ := it["encrypted_content"].(string)
if u.refuse && it["type"] == "reasoning" && enc == "" {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{"error":{"message":"bad response status code 400 (request id: 20261007000605227481978n1OdHdSC)","type":"invalid_request_error","param":"input","code":null}}`)
return
}
if u.expired && it["type"] == "reasoning" && enc == "" {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{"error":{"message":"OpenCode Go: Referenced reasoning item '`+it["id"].(string)+`' was not found or has expired.","type":"invalid_request_error"}}`)
return
}
}
w.Header().Set("Content-Type", "text/event-stream")
io.WriteString(w, sse(
`data: {"type":"response.created","response":{"id":"resp_relay","status":"in_progress"}}`,
`data: {"type":"response.output_item.done","output_index":0,"item":{"type":"message","id":"m1","role":"assistant","content":[{"type":"output_text","text":"SUMMARY"}]}}`,
`data: {"type":"response.completed","response":{"id":"resp_relay","status":"completed","output":[],"usage":{"input_tokens":5,"output_tokens":2}}}`))
}
func (u *relayUpstream) asked() [][]map[string]any {
u.mu.Lock()
defer u.mu.Unlock()
return append([][]map[string]any(nil), u.inputs...)
}
// bareConversation is the shape of the reporter's old conversation: messages,
// function calls, Codex's apply_patch as custom tool calls, and reasoning
// items with nothing sealed in them (one with a summary, as magpie gives
// Codex in a translated reply; one with an id alone), beside one sealed.
const bareConversation = `{"type":"message","role":"developer","content":[{"type":"input_text","text":"<permissions instructions>"}]},
{"type":"message","role":"user","content":[{"type":"input_text","text":"fix the login bug"}]},
{"type":"reasoning","id":"rs_18dbe693b3dce4800000003c","summary":[{"type":"summary_text","text":"Looking at main.go"}]},
{"type":"function_call","id":"fc_1","call_id":"call_1","name":"shell","arguments":"{\"cmd\":\"ls\"}"},
{"type":"function_call_output","call_id":"call_1","output":"main.go"},
{"type":"reasoning","id":"rs_18dbe693b3dce4800000003d","summary":[]},
{"type":"custom_tool_call","id":"ctc_1","status":"completed","call_id":"call_2","name":"apply_patch","input":"*** Begin Patch\n*** Update File: main.go\n@@\n-old\n+new\n*** End Patch"},
{"type":"custom_tool_call_output","call_id":"call_2","output":"Success. Updated the following files:\nM main.go"},
{"type":"reasoning","id":"rs_sealed","summary":[],"encrypted_content":"gAAAA-relay-sealed"},
{"type":"message","role":"assistant","content":[{"type":"output_text","text":"I fixed main.go"}]}`
// bareRequests are a compaction (Codex's trigger, no tools) and a turn
// relayed as it is, each over bareConversation, for model.
func bareRequests(model string) []struct{ name, body string } {
return []struct{ name, body string }{
{"compaction", `{"model":"` + model + `","stream":true,"store":false,"include":["reasoning.encrypted_content"],"tools":[{"type":"function","name":"shell","parameters":{"type":"object"}}],
"input":[` + bareConversation + `,{"type":"compaction_trigger"}]}`},
{"a turn relayed as it is", `{"model":"` + model + `","stream":true,"store":false,"include":["reasoning.encrypted_content"],
"input":[` + bareConversation + `,{"type":"message","role":"user","content":[{"type":"input_text","text":"now add a test"}]}]}`},
}
}
func saveRelay(t *testing.T, u *relayUpstream) {
t.Helper()
srv := httptest.NewServer(u)
t.Cleanup(srv.Close)
if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "k", Models: []string{"gpt-5.4"}, Responses: srv.URL + "/v1"}); err != nil {
t.Fatal(err)
}
}
func codexPostTo(s *Server, body string) *httptest.ResponseRecorder {
rec := httptest.NewRecorder()
req := httptest.NewRequest("POST", CodexPath+"/responses", strings.NewReader(body))
req.Header.Set("Authorization", "Bearer chatgpt-token")
s.Handler().ServeHTTP(rec, req)
return rec
}
func reasoningIDs(input []map[string]any) []string {
var ids []string
for _, it := range input {
if it["type"] == "reasoning" {
id, _ := it["id"].(string)
ids = append(ids, id)
}
}
return ids
}
func kinds(input []map[string]any) map[string]int {
out := map[string]int{}
for _, it := range input {
k, _ := it["type"].(string)
out[k]++
}
return out
}
// #1044: Codex on a Responses relay in front of OpenAI, its old
// conversation carrying reasoning with nothing sealed in it. The relay
// turned the compaction (and any turn relayed as it is) away with a 400
// naming only the input, and the reporter saw the same request answered
// once those items were left out. It is asked again without them, every
// other item kept, and that provider's model isn't sent them again.
func TestBareReasoningLeftOutWhereRefused(t *testing.T) {
for _, tc := range bareRequests("relay/gpt-5.4") {
t.Run(tc.name, func(t *testing.T) {
fresh(t)
u := &relayUpstream{refuse: true}
saveRelay(t, u)
s := New()
rec := codexPostTo(s, tc.body)
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "SUMMARY") && !strings.Contains(rec.Body.String(), "U1VNTUFSWQ") {
t.Fatalf("%d %s", rec.Code, rec.Body)
}
got := u.asked()
if len(got) != 2 {
t.Fatalf("asked the relay %d times, want 2", len(got))
}
if ids := strings.Join(reasoningIDs(got[0]), ","); ids != "rs_18dbe693b3dce4800000003c,rs_18dbe693b3dce4800000003d,rs_sealed" {
t.Errorf("first ask's reasoning: %s", ids)
}
if ids := strings.Join(reasoningIDs(got[1]), ","); ids != "rs_sealed" {
t.Errorf("asked again with reasoning %s, want the sealed only", ids)
}
before, after := kinds(got[0]), kinds(got[1])
for _, k := range []string{"message", "function_call", "function_call_output", "custom_tool_call", "custom_tool_call_output"} {
if after[k] == 0 || after[k] != before[k] {
t.Errorf("%s: %d asked again, %d first", k, after[k], before[k])
}
}
// the next request goes without them at once
rec = codexPostTo(s, tc.body)
if got = u.asked(); rec.Code != 200 || len(got) != 3 || strings.Join(reasoningIDs(got[2]), ",") != "rs_sealed" {
t.Fatalf("again: %d, %d asks, reasoning %v", rec.Code, len(got), reasoningIDs(got[len(got)-1]))
}
})
}
}
// #3062: a Codex thread behind a Responses passthrough relay collects
// reasoning ids the relay can't resolve — ones magpie minted in a
// translated reply, or the relay's own whose seal lapsed after a resume
// gap — and Codex replays them on every turn. The relay answers 400 in
// its own words, naming no input ("OpenCode Go: Referenced reasoning item
// 'rs_…' was not found or has expired."), and the thread stayed broken for
// good. The refusal is read as OpenAI's own unreadable item: asked once
// more without the bare reasoning, every other item kept, and that
// provider's model isn't sent them again.
func TestRelayRefusedReasoningItemReadAsUnreadable(t *testing.T) {
for _, tc := range bareRequests("relay/gpt-5.4") {
t.Run(tc.name, func(t *testing.T) {
fresh(t)
u := &relayUpstream{expired: true}
saveRelay(t, u)
s := New()
rec := codexPostTo(s, tc.body)
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "SUMMARY") && !strings.Contains(rec.Body.String(), "U1VNTUFSWQ") {
t.Fatalf("%d %s", rec.Code, rec.Body)
}
got := u.asked()
if len(got) != 2 {
t.Fatalf("asked the relay %d times, want 2", len(got))
}
if ids := strings.Join(reasoningIDs(got[0]), ","); ids != "rs_18dbe693b3dce4800000003c,rs_18dbe693b3dce4800000003d,rs_sealed" {
t.Errorf("first ask's reasoning: %s", ids)
}
if ids := strings.Join(reasoningIDs(got[1]), ","); ids != "rs_sealed" {
t.Errorf("asked again with reasoning %s, want the sealed only", ids)
}
before, after := kinds(got[0]), kinds(got[1])
for _, k := range []string{"message", "function_call", "function_call_output", "custom_tool_call", "custom_tool_call_output"} {
if after[k] == 0 || after[k] != before[k] {
t.Errorf("%s: %d asked again, %d first", k, after[k], before[k])
}
}
// the next request goes without them at once
rec = codexPostTo(s, tc.body)
if got = u.asked(); rec.Code != 200 || len(got) != 3 || strings.Join(reasoningIDs(got[2]), ",") != "rs_sealed" {
t.Fatalf("again: %d, %d asks, reasoning %v", rec.Code, len(got), reasoningIDs(got[len(got)-1]))
}
})
}
}
// An upstream that takes reasoning with nothing sealed in it, as a relay
// for a DeepSeek model wants it back (#388), still gets it as Codex sent
// it, and is asked once.
func TestBareReasoningKeptWhereTaken(t *testing.T) {
for _, tc := range bareRequests("relay/gpt-5.4") {
t.Run(tc.name, func(t *testing.T) {
fresh(t)
u := &relayUpstream{}
saveRelay(t, u)
rec := codexPostTo(New(), tc.body)
if rec.Code != 200 {
t.Fatalf("%d %s", rec.Code, rec.Body)
}
got := u.asked()
if len(got) != 1 {
t.Fatalf("asked %d times", len(got))
}
if ids := strings.Join(reasoningIDs(got[0]), ","); ids != "rs_18dbe693b3dce4800000003c,rs_18dbe693b3dce4800000003d,rs_sealed" {
t.Errorf("reasoning sent: %s", ids)
}
b, _ := json.Marshal(got[0])
if !strings.Contains(string(b), "Looking at main.go") {
t.Errorf("the summary didn't go: %s", b)
}
})
}
}
// A 400 over something other than the input is the upstream's answer: the
// reasoning isn't given up for it, and the request isn't asked again.
func TestBareReasoningKeptOverAnotherRefusal(t *testing.T) {
fresh(t)
u := &relayUpstream{status: http.StatusBadRequest,
errMsg: `{"error":{"message":"Invalid value for 'reasoning.effort'","type":"invalid_request_error","param":"reasoning.effort","code":null}}`}
saveRelay(t, u)
rec := codexPostTo(New(), bareRequests("relay/gpt-5.4")[1].body)
if rec.Code != http.StatusBadRequest {
t.Fatalf("%d %s", rec.Code, rec.Body)
}
if got := u.asked(); len(got) != 1 || len(reasoningIDs(got[0])) != 3 {
t.Fatalf("asked %d times, reasoning %v", len(got), reasoningIDs(got[0]))
}
}
func TestRefusesInput(t *testing.T) {
for _, tc := range []struct {
status int
body string
want bool
}{
{400, `{"error":{"message":"bad response status code 400 (request id: x)","type":"invalid_request_error","param":"input","code":null}}`, true},
{400, `{"error":{"message":"Invalid 'input[3].id'","type":"invalid_request_error","param":"input[3].id"}}`, true},
{400, `{"error":{"code":null,"message":"Item with id 'rs_1' not found.","param":null,"type":"invalid_request_error"}}`, true},
{400, `{"error":{"message":"OpenCode Go: Referenced reasoning item 'rs_18e02568274e11f100000242' was not found or has expired.","type":"invalid_request_error"}}`, true},
{400, `{"error":{"message":"bad response status code 400","type":"invalid_request_error"}}`, false},
{400, `{"error":{"message":"Invalid value for 'reasoning.effort'","param":"reasoning.effort"}}`, false},
{400, `{"error":{"message":"bad","param":"inputs"}}`, false},
{429, `{"error":{"param":"input"}}`, false},
} {
if got := refusesInput(tc.status, []byte(tc.body)); got != tc.want {
t.Errorf("%d %s: %v", tc.status, tc.body, got)
}
}
}