Files
magpie/internal/gateway/codex_access_programs_test.go
T
yetone dd25997a45 codex: a client's access_programs and the account's extraBody reach the ChatGPT backend, and its refusal comes back as it said it (magpie-releases#18)
NonMirror (0.1.1239, OpenCode, codex/gpt-daybreak-blue-latest on a ChatGPT
sign-in): a top-level Responses access_programs never reached upstream,
and the codex provider's extraBody wasn't applied.

Cause:
- A Codex account is streamed upstream, so a whole (stream:false)
  Responses request and every Chat or Messages request is translated
  (parseResponses/parseChat -> Request -> buildResponses). Request had no
  field for access_programs, so the rebuilt body dropped it. Only a
  streamed Responses request, relayed through codexBody, kept it.
- WithExtraBody skipped every signed-in account, and withPicks didn't
  copy the providers.json ExtraBody onto the account.
- writeError rewrapped every vendor error with code and param null, so
  even a forwarded refusal lost what it was about.
- The backend refuses a program the workspace hasn't got inside a 200
  stream (error event, invalid_request_error, access_program_not_enabled).
  The whole-translated path answered that 502 api_error, and translated
  streams wrote it as api_error, which holdWriter reads as 502: the same
  request went three times, then to the other accounts, and every one
  rested.

Fix:
- Request.AccessPrograms (Responses and Chat), built into the Responses
  body as it came; a null is kept.
- A ChatGPT (Codex) account takes extraBody; store, tool_choice and
  include stay magpie's (codexOwned). withPicks keeps ExtraBody.
- writeVendorError keeps the vendor's error.code and error.param on the
  relayed, whole-translated and streamed-translated status-error paths.
- requestRefused: a Responses stream error/failed whose error type is
  invalid_request_error carries status 400 and the vendor's error
  (Event.Said). The whole path answers that 400 with code and param; the
  Chat, Anthropic and Responses encoders write its type/code/param
  (saidError). Read by the type, never by words.
- Codex CLI sends access_programs as a top-level object on its Responses
  request (codex-rs ResponsesApiRequest.access_programs); OpenAI's Daybreak
  guide documents the same field.

Run:
- New: internal/gateway/codex_access_programs_test.go
  (TestCodexAccessProgramsReachBackend, TestCodexAccountTakesExtraBody,
  TestAccessProgramsNullKept), passes -count=3.
- Broken by hand, each went red: buildResponses not emitting the field
  (probe answered 200, the reporter's symptom); extraBodyTakes' Codex case;
  withPicks' line; codexOwned emptied; writeVendorError ignoring the body;
  the continuation site alone; retryable(400) true (asked twice, rested);
  requestRefused returning nothing (502, asked 4 times, both accounts
  rested); saidError a no-op (streamed: asked 4 times, rested); the whole
  site back to 502 (same).
- go test -timeout 30m ./...: all 62 packages ok (gateway 1559s at load
  ~26; an earlier run at the default 10m timed out in gateway under that
  load, not a failure). gofmt -l clean. GOOS=linux and GOOS=windows
  go vet -tags nogui ./... clean.

Real account (sandbox HOME, own port 3486, copied auth.json with a dead
refresh token; owner's ChatGPT Pro account, model codex/gpt-5.6-luna):
- "__daybreak_forwarding_probe__": 400 invalid_value, param
  access_programs.cyber ("Supported values are: 'standard',
  'daybreak_blue', and 'daybreak_red'") on Responses whole and streamed,
  Chat whole and streamed. The backend says invalid_value, not the
  guide's invalid_access_program.
- standard: 200. null: 400 invalid_type.
- daybreak_blue (not enabled for this workspace): 400
  access_program_not_enabled with param on Responses and Chat whole, the
  error event with that code on Responses and Chat streamed; ~1s each,
  one try (was 3 tries and a 502 before this change).
- The next plain request was served 200 by the same account: nothing
  rested.

Not verified: the Daybreak model itself (not listed for this account),
so no 200 with daybreak_blue; OpenCode's own wire; the Messages route
against the real backend (fake backend only).
2026-10-15 16:51:31 +08:00

268 lines
14 KiB
Go

package gateway
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"sync"
"testing"
"github.com/tidwall/gjson"
"github.com/yetone/magpie/internal/provider"
)
// daybreakBackend stands in for the ChatGPT backend as OpenAI's Daybreak
// guide has the Responses API answer access_programs: a cyber program it
// doesn't know is a 400 invalid_access_program, null is refused as well.
// daybreak_red stands for a program the workspace hasn't got, which the
// real backend refuses inside a 200 stream. It keeps every body it was sent.
type daybreakBackend struct {
mu sync.Mutex
bodies [][]byte
}
func (d *daybreakBackend) last(t *testing.T) []byte {
t.Helper()
d.mu.Lock()
defer d.mu.Unlock()
if len(d.bodies) == 0 {
t.Fatal("the ChatGPT backend was asked nothing")
}
return d.bodies[len(d.bodies)-1]
}
func (d *daybreakBackend) asked() int {
d.mu.Lock()
defer d.mu.Unlock()
return len(d.bodies)
}
func daybreak(t *testing.T) *daybreakBackend {
t.Helper()
d := &daybreakBackend{}
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, "/models") {
io.WriteString(w, `{"models":[{"slug":"gpt-daybreak-blue-latest","display_name":"Daybreak Blue","visibility":"list","supported_reasoning_levels":[{"effort":"medium"}],"context_window":272000}]}`)
return
}
b, _ := io.ReadAll(r.Body)
d.mu.Lock()
d.bodies = append(d.bodies, b)
d.mu.Unlock()
if ap := gjson.GetBytes(b, "access_programs"); ap.Exists() {
if c := ap.Get("cyber"); ap.Type == gjson.Null || c.Exists() && c.String() != "standard" && c.String() != "daybreak_blue" && c.String() != "daybreak_red" {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(400)
io.WriteString(w, `{"error":{"message":"Invalid access program for access_programs.cyber.","type":"invalid_request_error","param":"access_programs.cyber","code":"invalid_access_program"}}`)
return
}
}
w.Header().Set("Content-Type", "text/event-stream")
if gjson.GetBytes(b, "access_programs.cyber").String() == "daybreak_red" {
// what the real backend said for daybreak_blue to a workspace
// without it: a 200 stream whose error event names the program
io.WriteString(w, sse(
`event: error`+"\n"+`data: {"type":"error","error":{"message":"The requested Cyber access program is not authorized for this workspace.","type":"invalid_request_error","param":"access_programs.cyber","code":"access_program_not_enabled"}}`))
return
}
io.WriteString(w, sse(
`data: {"type":"response.created","response":{"id":"r1","model":"gpt-daybreak-blue-latest"}}`,
`data: {"type":"response.output_item.done","output_index":0,"item":{"type":"message","id":"msg_1","role":"assistant","content":[{"type":"output_text","text":"OK"}]}}`,
`data: {"type":"response.completed","response":{"id":"r1","status":"completed","output":[],"usage":{"input_tokens":5,"output_tokens":1}}}`))
}))
t.Cleanup(up.Close)
was := provider.CodexBase
provider.CodexBase = up.URL + "/backend-api/codex"
t.Cleanup(func() { provider.CodexBase = was })
return d
}
// magpie-releases#18 (NonMirror, OpenCode): a client's access_programs, the
// Daybreak program Codex CLI sends the ChatGPT backend on each turn it was
// picked for (codex-rs ResponsesApiRequest.access_programs), reaches the
// backend on a Codex account's model on every path that asks it: Responses
// relayed (streamed) and made again (one body, which the account is
// streamed for), and Chat made again as Responses. The value goes as it
// came, so the backend's 400 invalid_access_program for a value it doesn't
// take reaches the client, the reporter's probe among them.
func TestCodexAccessProgramsReachBackend(t *testing.T) {
codexSignedIn(t, "spare@example.com")
d := daybreak(t)
const blue = `{"cyber":"daybreak_blue"}`
for _, c := range []struct{ name, path, body string }{
{"responses streamed", "/v1/responses", `{"model":"codex/gpt-daybreak-blue-latest","stream":true,"input":"Respond with OK.","access_programs":` + blue + `}`},
{"responses whole", "/v1/responses", `{"model":"codex/gpt-daybreak-blue-latest","stream":false,"input":"Respond with OK.","access_programs":` + blue + `}`},
{"chat streamed", "/v1/chat/completions", `{"model":"codex/gpt-daybreak-blue-latest","stream":true,"messages":[{"role":"user","content":"Respond with OK."}],"access_programs":` + blue + `}`},
{"chat whole", "/v1/chat/completions", `{"model":"codex/gpt-daybreak-blue-latest","messages":[{"role":"user","content":"Respond with OK."}],"access_programs":` + blue + `}`},
} {
code, out := post(t, c.path, c.body)
if code != 200 {
t.Fatalf("%s: %d %s", c.name, code, out)
}
if got := gjson.GetBytes(d.last(t), "access_programs").Raw; got != blue {
t.Errorf("%s: the backend got access_programs %q in %s", c.name, got, d.last(t))
}
}
// the reporter's probe, and null, which the guide says is refused: the
// backend's own 400, its code and param as it said them, not a 200
// from a request it never saw them in. The request is at fault, not
// the account: the spare signed in beside it is never asked, and
// nobody rests.
for _, c := range []struct{ name, path, body string }{
{"probe, responses whole", "/v1/responses", `{"model":"codex/gpt-daybreak-blue-latest","input":"Respond with OK.","stream":false,"access_programs":{"cyber":"__daybreak_forwarding_probe__"}}`},
{"probe, responses streamed", "/v1/responses", `{"model":"codex/gpt-daybreak-blue-latest","input":"Respond with OK.","stream":true,"access_programs":{"cyber":"__daybreak_forwarding_probe__"}}`},
{"probe, chat", "/v1/chat/completions", `{"model":"codex/gpt-daybreak-blue-latest","messages":[{"role":"user","content":"Respond with OK."}],"access_programs":{"cyber":"__daybreak_forwarding_probe__"}}`},
{"probe, chat streamed", "/v1/chat/completions", `{"model":"codex/gpt-daybreak-blue-latest","stream":true,"messages":[{"role":"user","content":"Respond with OK."}],"access_programs":{"cyber":"__daybreak_forwarding_probe__"}}`},
{"null, responses whole", "/v1/responses", `{"model":"codex/gpt-daybreak-blue-latest","input":"Respond with OK.","access_programs":null}`},
} {
before := d.asked()
code, out := post(t, c.path, c.body)
if code != 400 || gjson.Get(out, "error.code").String() != "invalid_access_program" || gjson.Get(out, "error.param").String() != "access_programs.cyber" {
t.Errorf("%s: %d %s\nthe backend got %s", c.name, code, out, d.last(t))
}
if n := d.asked() - before; n != 1 {
t.Errorf("%s: the backend was asked %d times, want once", c.name, n)
}
if r := restingNow(t); len(r) > 0 {
t.Errorf("%s: resting after the request's own 400: %v", c.name, r)
}
}
// a program the workspace hasn't got is refused inside the stream: a
// client answered whole gets that refusal as a 400 with its code and
// param, not a 502 after the same request was sent three times; one
// answered as a stream gets it inside the stream. Asked once either
// way, and nobody rests.
for _, c := range []struct {
name, path, body string
whole bool
}{
{"not enabled, responses whole", "/v1/responses", `{"model":"codex/gpt-daybreak-blue-latest","input":"Respond with OK.","stream":false,"access_programs":{"cyber":"daybreak_red"}}`, true},
{"not enabled, chat whole", "/v1/chat/completions", `{"model":"codex/gpt-daybreak-blue-latest","messages":[{"role":"user","content":"Respond with OK."}],"access_programs":{"cyber":"daybreak_red"}}`, true},
{"not enabled, chat streamed", "/v1/chat/completions", `{"model":"codex/gpt-daybreak-blue-latest","stream":true,"messages":[{"role":"user","content":"Respond with OK."}],"access_programs":{"cyber":"daybreak_red"}}`, false},
{"not enabled, responses streamed", "/v1/responses", `{"model":"codex/gpt-daybreak-blue-latest","input":"Respond with OK.","stream":true,"access_programs":{"cyber":"daybreak_red"}}`, false},
} {
before := d.asked()
code, out := post(t, c.path, c.body)
if c.whole {
if code != 400 || gjson.Get(out, "error.code").String() != "access_program_not_enabled" || gjson.Get(out, "error.param").String() != "access_programs.cyber" {
t.Errorf("%s: %d %s", c.name, code, out)
}
} else if !strings.Contains(out, "not authorized for this workspace") {
t.Errorf("%s: %d %s", c.name, code, out)
}
if n := d.asked() - before; n != 1 {
t.Errorf("%s: the backend was asked %d times, want once", c.name, n)
}
if r := restingNow(t); len(r) > 0 {
t.Errorf("%s: resting after the request's own refusal: %v", c.name, r)
}
}
// none sent, none added
if code, out := post(t, "/v1/responses", `{"model":"codex/gpt-daybreak-blue-latest","input":"hi"}`); code != 200 {
t.Fatalf("without: %d %s", code, out)
}
if gjson.GetBytes(d.last(t), "access_programs").Exists() {
t.Errorf("access_programs added: %s", d.last(t))
}
}
// magpie-releases#18: the extraBody written for the built-in codex
// provider (by hand: its editor has no box for it) goes on its requests,
// as a key provider's does — filling in what the client left out, a
// "!name" replacing it — on every API the request came in on. What Codex
// CLI's request needs to be answered at all (store false, tool_choice
// auto, its sealed reasoning asked back) stays magpie's.
func TestCodexAccountTakesExtraBody(t *testing.T) {
codexSignedIn(t)
d := daybreak(t)
os.MkdirAll(filepath.Dir(provider.Path()), 0o755)
os.WriteFile(provider.Path(), mustJSON(map[string]any{"providers": []any{map[string]any{
"id": "codex",
"extraBody": map[string]any{
"access_programs": map[string]any{"cyber": "__daybreak_forwarding_probe__"},
"!store": true,
"!tool_choice": "none",
"!include": []string{},
"client_metadata": map[string]any{"session": "${sessionId}"},
}}}}), 0o600)
provider.ForgetAccounts()
// the reporter's: a benign request, the probe from extraBody alone
code, out := post(t, "/v1/responses", `{"model":"codex/gpt-daybreak-blue-latest","input":"Respond with OK.","stream":false}`)
if code != 400 || gjson.Get(out, "error.code").String() != "invalid_access_program" {
t.Fatalf("extraBody's probe: %d %s\nthe backend got %s", code, out, d.last(t))
}
if ap := gjson.GetBytes(d.last(t), "access_programs.cyber").String(); ap != "__daybreak_forwarding_probe__" {
t.Fatalf("extraBody's access_programs didn't reach the backend: %s", d.last(t))
}
got := d.last(t)
if gjson.GetBytes(got, "store").Bool() || gjson.GetBytes(got, "tool_choice").String() != "auto" ||
!strings.Contains(gjson.GetBytes(got, "include").Raw, "reasoning.encrypted_content") {
t.Errorf("extraBody set what Codex's request must carry: %s", got)
}
if !gjson.GetBytes(got, "client_metadata.session").Exists() || strings.Contains(string(got), "${sessionId}") {
t.Errorf("client_metadata: %s", got)
}
// the client's own value is kept over extraBody's unforced one, on
// Chat and on Anthropic's Messages too
for _, c := range []struct{ name, path, body string }{
{"responses", "/v1/responses", `{"model":"codex/gpt-daybreak-blue-latest","input":"hi","stream":true,"access_programs":{"cyber":"daybreak_blue"}}`},
{"chat", "/v1/chat/completions", `{"model":"codex/gpt-daybreak-blue-latest","messages":[{"role":"user","content":"hi"}],"access_programs":{"cyber":"daybreak_blue"}}`},
} {
if code, out := post(t, c.path, c.body); code != 200 {
t.Fatalf("%s: %d %s\nthe backend got %s", c.name, code, out, d.last(t))
}
}
code, out = post(t, "/v1/messages", `{"model":"codex/gpt-daybreak-blue-latest","max_tokens":64,"messages":[{"role":"user","content":"hi"}]}`)
// Anthropic's error shape has no code: the backend's words are said
if code != 400 || !strings.Contains(out, "Invalid access program for access_programs.cyber") ||
gjson.GetBytes(d.last(t), "access_programs.cyber").String() != "__daybreak_forwarding_probe__" {
t.Errorf("messages: %d %s\nthe backend got %s", code, out, d.last(t))
}
// a program the workspace hasn't got, which the backend refuses inside
// its stream, reaches a Messages client as the request's refusal,
// whole or streamed: asked once, nobody resting
os.WriteFile(provider.Path(), mustJSON(map[string]any{"providers": []any{map[string]any{
"id": "codex", "extraBody": map[string]any{"access_programs": map[string]any{"cyber": "daybreak_red"}}}}}), 0o600)
provider.ForgetAccounts()
for _, stream := range []bool{false, true} {
before := d.asked()
code, out := post(t, "/v1/messages", `{"model":"codex/gpt-daybreak-blue-latest","max_tokens":64,"stream":`+map[bool]string{false: "false", true: "true"}[stream]+`,"messages":[{"role":"user","content":"hi"}]}`)
if !strings.Contains(out, "invalid_request_error") || !strings.Contains(out, "not authorized for this workspace") || !stream && code != 400 {
t.Errorf("not enabled, messages (stream %v): %d %s", stream, code, out)
}
if n := d.asked() - before; n != 1 {
t.Errorf("not enabled, messages (stream %v): the backend was asked %d times, want once", stream, n)
}
if r := restingNow(t); len(r) > 0 {
t.Errorf("not enabled, messages (stream %v): resting after the request's own refusal: %v", stream, r)
}
}
}
// json.RawMessage keeps a null the client sent as "null" (the guide's
// refused value), which Request.AccessPrograms carries on.
func TestAccessProgramsNullKept(t *testing.T) {
r, err := parseResponses([]byte(`{"model":"m","input":"hi","access_programs":null}`))
if err != nil {
t.Fatal(err)
}
if string(r.AccessPrograms) != "null" {
t.Fatalf("access_programs %q", r.AccessPrograms)
}
var m map[string]json.RawMessage
json.Unmarshal(buildResponses(r, "m", "chatgpt.com", false), &m)
if string(m["access_programs"]) != "null" {
t.Fatalf("built %s", m["access_programs"])
}
}