Files
magpie/internal/gateway/passthrough_refused_test.go
T
yetone bb50f4ad94 gateway: a Codex passthrough member OpenAI doesn't know is left out and asked again
xuanwo on X: every new conversation in the Codex app (two agents) ended
at once with the ChatGPT backend's 400 "[ObjectParam]
[input[55].internal_chat_message_metadata_passthrough.content_token_count]
[unknown_parameter]". The member is new in the Codex app's bundled
build; no open-source Codex sends it (main 67f0994, npm 0.166.0), and
openai/codex#52313 was the same refusal of content_item_metadata in
0.163.0-alpha.1. magpie sends the passthrough as Codex wrote it to
OpenAI hosts and ChatGPT accounts (forVendor), so the refusal reached
the app. The reporter's own look at magpie's routing log: the main
conversation failed at input[49] and input[55], and a conversation
with no subagent failed the same way, so subagents aren't the cause.

The member is Codex's own attribution, not the conversation. A turn
turned away over one passthrough member, or the passthrough as a whole,
is asked once more without it on every item, with the other members
and every other byte kept as written. The provider isn't sent that
member again (unfit, as for a tool's strict). The signed-in Codex
relay's retry (withoutUnreadable) takes the same refusal.

Run (mote, go1.26.8):
- TestPassthroughMemberLeftOutWhereRefused, TestPassthroughRefusalWordings,
  TestWithoutPassthrough: ok.
- Broken by hand: with the retry turned off, TestPassthroughMemberLeftOut
  WhereRefused gets the reporter's exact 400 back.
- At d714c4216 + this: gofmt clean; go vet ./internal/gateway on
  darwin, linux and windows ok; go test ./internal/gateway -run
  'Passthrough|ToolStrict|Unreadable|Optional|LiteSubagent' ok.
- The whole internal/gateway package was still running at push (the
  owner's ship-at-once rule); its result is not in this record.

Not tried: the reporter's Codex app build. Its content_token_count isn't
in any build that can be downloaded, so the fixture is the refusal text
from their screenshot.
2026-11-01 03:45:10 +08:00

134 lines
6.8 KiB
Go

package gateway
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"github.com/tidwall/gjson"
"github.com/yetone/magpie/internal/provider"
)
// passthroughRefusalBody is the ChatGPT backend's 400 in xuanwo's
// screenshot (on X), the message as it reached the Codex app after
// magpie's "Codex: ".
const passthroughRefusalBody = `{"error":{"code":null,"message":"[ObjectParam] [input[2].internal_chat_message_metadata_passthrough.content_token_count] [unknown_parameter] Unknown parameter: 'input[2].internal_chat_message_metadata_passthrough.content_token_count'.","param":null,"type":"invalid_request_error"}}`
// passthroughTurn is a Codex turn whose items carry the passthrough with
// the member the backend doesn't know, and one item without a passthrough.
const passthroughTurn = `{"model":"%s","stream":false,"input":[` +
`{"type":"message","role":"developer","content":[{"type":"input_text","text":"be brief"}],"internal_chat_message_metadata_passthrough":{"turn_id":"t1","content_token_count":[3]}},` +
`{"type":"message","role":"user","content":[{"type":"input_text","text":"hi"}]},` +
`{"type":"message","role":"user","content":[{"type":"input_text","text":"again"}],"internal_chat_message_metadata_passthrough":{"turn_id":"t2","content_item_kinds":["text"],"content_token_count":[1]}}]}`
func passthroughHas(q []byte, field string) bool {
for _, it := range gjson.GetBytes(q, "input").Array() {
if it.Get(passthroughKey + "." + field).Exists() {
return true
}
}
return false
}
// A newer Codex app's passthrough member the ChatGPT backend doesn't know
// turned every new conversation away (xuanwo on X). The turn is asked
// once more without that member, the rest of the passthrough kept, and
// the next turn goes without it at once.
func TestPassthroughMemberLeftOutWhereRefused(t *testing.T) {
fresh(t)
// an OpenAI host, which is sent the passthrough as Codex wrote it
if err := provider.Save(provider.Provider{ID: "chatgpt", Name: "Codex", Key: "k", Responses: "https://api.openai.com/v1", Models: []string{"m1"}}); err != nil {
t.Fatal(err)
}
var mu sync.Mutex
var sent [][]byte
s := New()
s.client = &http.Client{Transport: countTransport(func(r *http.Request) (*http.Response, error) {
b, _ := io.ReadAll(r.Body)
mu.Lock()
sent = append(sent, b)
mu.Unlock()
if passthroughHas(b, "content_token_count") {
return &http.Response{StatusCode: 400, Header: http.Header{"Content-Type": {"application/json"}}, Request: r,
Body: io.NopCloser(strings.NewReader(passthroughRefusalBody))}, nil
}
return &http.Response{StatusCode: 200, Header: http.Header{"Content-Type": {"application/json"}}, Request: r,
Body: io.NopCloser(strings.NewReader(`{"id":"r1","object":"response","status":"completed","output":[{"type":"message","id":"m1","role":"assistant","content":[{"type":"output_text","text":"ok"}]}],"usage":{"input_tokens":5,"output_tokens":2}}`))}, nil
})}
ask := func() *httptest.ResponseRecorder {
rec := httptest.NewRecorder()
body := strings.Replace(passthroughTurn, "%s", "chatgpt/m1", 1)
s.Handler().ServeHTTP(rec, httptest.NewRequest("POST", "/v1/responses", strings.NewReader(body)))
return rec
}
if rec := ask(); rec.Code != 200 || !strings.Contains(rec.Body.String(), "ok") {
t.Fatalf("first turn: %d %s (asked %d)", rec.Code, rec.Body, len(sent))
}
if len(sent) != 2 || !passthroughHas(sent[0], "content_token_count") || passthroughHas(sent[1], "content_token_count") {
t.Fatalf("asked %d times: %s", len(sent), sent)
}
again := sent[1]
if gjson.GetBytes(again, "input.0."+passthroughKey+".turn_id").String() != "t1" ||
gjson.GetBytes(again, "input.2."+passthroughKey+".content_item_kinds.0").String() != "text" ||
gjson.GetBytes(again, "input.#").Int() != 3 || gjson.GetBytes(again, "input.2.content.0.text").String() != "again" {
t.Fatalf("asked again with: %s", again)
}
if rec := ask(); rec.Code != 200 || len(sent) != 3 || passthroughHas(sent[2], "content_token_count") {
t.Fatalf("next turn: %d, asked %d times", rec.Code, len(sent))
}
}
// The refusal is known in each place it can name the passthrough; another
// unknown parameter, or an item's other field, is not taken for it.
func TestPassthroughRefusalWordings(t *testing.T) {
for body, want := range map[string]string{
passthroughRefusalBody: "content_token_count",
"Codex: [ObjectParam] [input[55].internal_chat_message_metadata_passthrough.content_token_count] [unknown_parameter] Unknown parameter: 'input[55].internal_chat_message_metadata_passthrough.content_token_count'.": "content_token_count",
`{"error":{"message":"Unknown parameter: 'input[0].internal_chat_message_metadata_passthrough.content_item_metadata'.","param":"input[0].internal_chat_message_metadata_passthrough.content_item_metadata","code":"unknown_parameter"}}`: "content_item_metadata",
`{"error":{"message":"Unknown parameter: 'input[4].internal_chat_message_metadata_passthrough'.","code":"unknown_parameter"}}`: "",
} {
got, ok := refusedPassthrough([]byte(body))
if !ok || got != want {
t.Errorf("%s: %q %v, want %q", body, got, ok, want)
}
}
for _, body := range []string{
`{"error":{"message":"Unknown parameter: 'input[0].foo'.","code":"unknown_parameter"}}`,
`{"error":{"message":"Invalid type for 'input[0].internal_chat_message_metadata_passthrough.turn_id': expected a string.","code":"invalid_type"}}`,
`{"error":{"message":"Unsupported parameter: 'store'."}}`,
} {
if f, ok := refusedPassthrough([]byte(body)); ok {
t.Errorf("taken for a passthrough refusal (%q): %s", f, body)
}
}
}
// Only the member goes, from every item that has it; every other byte
// stays as Codex sent it. For the passthrough as a whole, each item's goes.
func TestWithoutPassthrough(t *testing.T) {
body := []byte(strings.Replace(passthroughTurn, "%s", "gpt-6", 1))
b, ok := withoutPassthrough(body, "content_token_count")
if !ok || passthroughHas(b, "content_token_count") || !json.Valid(b) {
t.Fatalf("%v %s", ok, b)
}
want := strings.NewReplacer(`,"content_token_count":[3]`, "", `,"content_token_count":[1]`, "").Replace(string(body))
if string(b) != want {
t.Fatalf("got %s\nwant %s", b, want)
}
if _, ok := withoutPassthrough(b, "content_token_count"); ok {
t.Fatal("removed it twice")
}
all, ok := withoutPassthrough(body, "")
if !ok || strings.Contains(string(all), passthroughKey) || gjson.GetBytes(all, "input.#").Int() != 3 || gjson.GetBytes(all, "input.2.content.0.text").String() != "again" {
t.Fatalf("%v %s", ok, all)
}
// the signed-in Codex relay asks again the same way
if b2, ok := withoutUnreadable(body, []byte(passthroughRefusalBody)); !ok || string(b2) != want {
t.Fatalf("withoutUnreadable: %v %s", ok, b2)
}
}