Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.
gateway: Codex's access_programs goes to OpenAI alone, so OpenCode Go's DeepSeek and GLM answer (#3172)
yetone46c41883277b
diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.mdindex 4f346b141..8c9f5b466 100644--- a/docs/subsystems/gateway-routing.md+++ b/docs/subsystems/gateway-routing.md@@ -176,6 +176,7 @@ recorded as a prediction failure (`noPrediction`).- A Chat upstream that wants a turn's reasoning back (`chatReplaysReasoning` in [`chat.go`](../../internal/gateway/chat.go): a host or a model, as magpie or the provider names it, with `deepseek` in it, such as OpenCode Go's `deepseek-v4.1-flash`, or Command Code's plugin) gets every assistant tool call with `reasoning_content`, relayed (`withReasoningOnToolTurns` in `passthrough`) or translated (`buildChat`). DeepSeek turns the whole request away ("The `reasoning_content` in the thinking mode must be passed back to the API") when a tool call made since the user's last message has none, which a client sends for a turn it saw no thinking on: another model's, a translated one (#1462). The turn's own thinking goes back as it came; one sent in `reasoning` or `reasoning_details` is copied in; otherwise it goes with a space (`noReasoning`: DeepSeek's Chat API takes an empty one, but a relay that leaves empty fields out would drop it). An assistant message right after a tool result, a reply with no call of its own, gets one too: DeepSeek refuses it the same way (checked 2026-10-12, #1822). A reply to the user, any other model and Mistral (#494) are sent as before.- An assistant turn the user cut off before it said anything (only its thinking, an empty text, nothing; an isolated Codex reasoning item; a Gemini thought alone) stays in the agent's history and is sent on every later turn. DeepSeek and Kimi turn the whole request away over an assistant message with neither content nor tool calls, `reasoning_content` alone included ("Invalid assistant message: content or tool_calls must be set", #2599). Every Chat upstream gets such a turn left out, translated (`buildChat`, before `pairToolMessages`) or relayed (`silentAssistantsOut` in `passthrough`), by one rule (`withoutSilentAssistants` in [`chat.go`](../../internal/gateway/chat.go)): the two user messages it stood between join into one, and after a tool result the user's message follows the tool message. A prefill to go on from (`prefix`, `partial`, a Resume), a legacy `function_call`, a refusal and an audio reply stay, and a relayed body without such a turn goes byte for byte. `TestChatEmptyAssistantLeftOutFromAnthropic`, `…FromResponses`, `…FromGemini`, `TestChatRelayLeavesOutEmptyAssistant`, `TestChatRelaySilentAssistantsOnly`.- Kimi's Chat API (`isKimi`: the Kimi platform's and a Kimi Code membership's hosts or presets) turns `reasoning_details` in a message away in any shape: an entry with a stream's `index` ("the reasoning_details at position 1 entry 0 must not contain streaming index") and any other entry ("… has an invalid type"), checked against api.kimi.com/coding on 2026-10-12. pi keeps another vendor's thinking there (MiMo's, through Command Code, with `index: 0`), so a conversation routed on to Kimi was refused on every turn (#1765). `passthrough` sends it to Kimi as `reasoning_content` (`reasoningDetailsAsContent`), which Kimi takes; a message with `reasoning_content` of its own keeps that. Another upstream that refuses a thinking field "at position N", as Kimi words it (a relay in front of it), is asked once more the same way and marked (`refusedInMessages`, `withoutMessageReasoning`).+- A Responses request's top-level `access_programs`, the Cyber program Codex asks the model under (`{"cyber":"standard"}` on a subagent it inherits, OpenAI's Daybreak programs, magpie-releases#18), goes only where Codex itself would send it: codex-rs sends it with a ChatGPT sign-in, or an API key on its provider named `openai`, and to no other provider (`cyber_access_program.rs`, `for_auth`, `for_provider`). In `forwardOnce`, relayed or built (from Chat too), it goes as it came to a ChatGPT (Codex) account, a host ending in `openai.com`, another magpie (`IsRemoteMagpie`, whose own upstream decides) and a relay set `codexAsSent` when Codex sent the request; every other Responses upstream is sent the request without it (`keepsAccessPrograms`, `withoutTopMember`, the rest byte for byte). OpenCode Go answered it, for DeepSeek and GLM, with 503 "Upstream request failed: Endpoint is unavailable." and 200 without it or with null; its Haiku took it (#3172, three reporters' real A/B runs). It is taken out before the user's extraBody is merged, so an `access_programs` written there still goes. Chat and Messages builds never carry it; a Chat request relayed as it came keeps whatever its client wrote. `TestAccessProgramsGoToOpenAIAlone`, `TestWithoutTopMember`, `TestCodexAccessProgramsReachBackend`.- Claude Code's deferred tools (ToolSearch, MCP tools that come and go) put blocks of their own in a message: a `tool_addition` (`tool` a `tool_reference` naming the tool, or a `tool_definition`), a `tool_removal`, and a `tool_reference`. Kimi's Anthropic endpoint refuses any of them with a whole-request 400 "Invalid request Error" that names nothing, so once a session had loaded a tool every turn failed (#2042); DeepSeek's takes a `tool_addition` naming a tool but refuses one defining it, a `tool_removal` and a message's `tool_reference` with 422 "unknown variant `tool_removal`" (both checked against the real endpoints on 2026-10-14). OpenCode Go also rejects DeepSeek inline definitions with a generic 422 "Upstream request failed: Endpoint is unavailable." (checked on 2026-10-15). Kimi and DeepSeek models on OpenCode are never sent these blocks (`forwardOnce`); a provider that refuses one in those words is asked again at once without them and not sent them again while magpie runs (`refuseShapes`, `shapeDeferred`; the retry loop reads a 422 as well as a 400). `withoutDeferredTools` drops the blocks and a message they leave empty; definitions present only in a `tool_addition.tool.definition` join the request's `tools` once by name (an existing top-level definition wins), and a `tool_reference` inside a `tool_result` (ToolSearch's answer, which both take) stays. Any other provider is sent them as Claude Code wrote them ([`anthropic_shapes.go`](../../internal/gateway/anthropic_shapes.go), `TestKimiIsSentNoDeferredToolBlocks`, `TestDeferredToolBlocksRefusedOnceLeftOut`, `TestOpenCodeDeepSeekInlineTools`, `TestDeferredInlineDefinitionPreserved`).- ToolSearch's answer is a `tool_result` whose content is `tool_reference` blocks alone. OpenDesign's Anthropic endpoint can't read one: 422 "Failed to deserialize the JSON body into the target type: messages[4]: missing field `content`" (#2124, the reporter's direct request without magpie fails the same way; the same result with a text block beside the references is taken). A provider that refuses in those words while the request holds such a result is asked again at once with a text block first in each, "Tool <name> is loaded and can be called now." (`toolLoadedText`, the words a translated route already gives a model in `stringOrText`), the references and the `tool_use`/`tool_result` pairing kept, and is sent it that way while magpie runs (`refuseShapes`, `shapeRefsOnly`, `withRefsSaid`). This is in `forward`, so `/v1/messages`, `count_tokens` and a group's member all get it; a request translated to another API already carries the references as that text. Any other provider is sent the bare references (`TestRefsOnlyToolResultRefusedGetsText`, `TestRefsSaidLeavesOtherResults`).- A tool Claude Code's ToolSearch loads, translated to a Responses API, used to join the top-level `tools` on the next request, which changed the prompt from its first bytes and missed the cache on every load (#2660). A deferred tool (`defer_loading`, `Tool.Deferred`) that a `tool_result`'s `tool_reference` names (`toolRefs`, `Part.Loads`) now goes, where `Request.LoadedInPlace` is set, in an `{"type":"additional_tools","role":"developer","tools":[…]}` item right after the run of tool outputs that loaded it, once, and not in `tools` (`buildResponses`): tools loaded by parallel results or one result naming several share one item, a tool named again isn't repeated, and the tool output's text is as before. OpenAI's tool search guide documents the item and that its tools load at the end of the context so the cache is kept. A compacted or edited history whose loading result is gone puts the tool back in `tools`, a new prefix, as is expected. `LoadedInPlace` is set (`loadsInPlace`, in `forwardTranslated`) for OpenAI's own API with a key (`api.openai.com`, `*.api.openai.com`) and for a provider set `additionalTools` (`magpie provider set <id> additional-tools=on`); nothing else, since xAI answers the item 422 (#350), the ChatGPT backend has been seen to take it only for Codex's Responses Lite, and no relay or plugin's vendor has been tried. An upstream that refuses a request holding the item with a 400 or 422 naming `additional_tools` is asked again with every tool in `tools`, logged ("turned away additional_tools … loaded tools go in tools from now on") and not sent the item again while magpie runs. Not handled: a tool a `tool_addition` block loads (an MCP server's late tool, the advisor) stays in `tools`. `TestLoadedToolsKeepThePrefix`, `TestLoadedToolsInOneItem`, `TestLoadedToolsWhereNotTaken`, `TestLoadsInPlaceScope`, `TestLoadedToolsThroughTheGateway`.diff --git a/docs/subsystems/providers-accounts.md b/docs/subsystems/providers-accounts.mdindex 59e760526..0209a585e 100644--- a/docs/subsystems/providers-accounts.md+++ b/docs/subsystems/providers-accounts.md@@ -21,7 +21,7 @@ keys and accounts per request is in [Gateway routing and fallback](gateway-routi| Anthropic key header | A provider of the user's own on a third-party Anthropic API is sent its key as both `x-api-key` and `Authorization: Bearer` (`AuthHeaders`); Anthropic's own API, Bedrock, Azure, Vertex and every subscription account have their own and never this choice (`BothAuth` is false). Some services refuse the two together with a 401 (Aloha on Discord: "鉴权 header, x-api-key 和 Authorization 不可以同时存在"), so `Provider.AuthHeader` (`authHeader`: empty for both, `x-api-key` or `bearer`; anything else is read as empty) sends one alone, picked in the editor's Key header row (shown while `bothAuth` is true, or a custom provider's Anthropic URL is a third party's). Left at both, a 401 whose body `catalog.BothAuthRefused` tells as that refusal (it names both headers and says they can't be together, in Chinese or English) is asked once more with x-api-key alone, then Bearer alone (`RetryOneAuth`, in the gateway's `forwardOnce` and the provider test's `probe`; `catalog.fetchOne` for the model list through `listAuth`); the first not answered 401 or 403 is kept with `LearnAuthHeader`, which never changes a pick the user made. A request carrying the user's own `Authorization` or `x-api-key` header is never retried. | [`authheader.go`](../../internal/provider/authheader.go), [`test.go`](../../internal/provider/test.go), [`catalog/authheader.go`](../../internal/catalog/authheader.go), [`catalog/live.go`](../../internal/catalog/live.go) || GLM Coding Plan key at pay as you go | Zhipu's and Z.ai's presets offer Pay as you go first (`/api/paas/v4`) and the Coding Plan (`/api/coding/paas/v4`, Responses at `/api/v1`); both share `/api/anthropic`, which bills the plan, so a plan's key left at Pay as you go worked in Claude Code and was refused 429 code 1113 "余额不足或无可用资源包" in Codex (@cleverpigo on X). Nothing in the key tells the two apart, so a Chat request to `open.bigmodel.cn` or `api.z.ai` at `/api/paas/v4` refused 1113 is asked once more at `/api/coding/paas/v4` (`RetryZhipuCoding`, in the gateway's `forwardOnce` and the provider test's `probe`). Served there, `LearnZhipuCoding` moves a saved provider with that one key, still at that URL, to the plan's OpenAI URL (and the preset's plan Responses URL when it has none); one with other keys stays and is asked again each time. Refused there too, the 1113 body keeps its code and gets both URLs and what to change in its message (`ZhipuNoBalanceHint`). A pay-as-you-go key with a balance is never answered 1113 and never sent to the plan's endpoint. | [`zhipu_coding.go`](../../internal/provider/zhipu_coding.go), [`test.go`](../../internal/provider/test.go) || Zhipu's 200 refusals | Zhipu's and Z.ai's `/api/v1` (the GLM Coding Plan's Responses API, from their Codex pages) and `/api/monitor` answer a refusal 200 in the platform's envelope, `{"code":1000,"msg":"身份验证失败。","success":false}`, where their OpenAI and Anthropic endpoints answer the same key 401 (@xanahopper on X: a refused key tested green on the Responses row only). `ZhipuEnveloped` turns a 2xx JSON body from `open.bigmodel.cn` or `api.z.ai` with `success: false` into `{"error":{"code","message"}}` at the status Zhipu's error-code table gives (1000–1005 → 401, 1113 and 1302–1399 → 429, 1220 → 403, 1210–1229, 1261 and 1301 → 400, anything else → 502), in the provider test's `probe` (endpoint tests, model tests, Detect APIs) and the gateway's `forwardOnce`. A stream, a compressed body, a body over 64 KB and every other host pass as they came. The plan's quota card for a key refused there (1000–1005, or a 401) says `<Zhipu or Z.ai> didn't take this key (<their words>): …` with the vendor's API-keys page (`zhipuRefused`), not asked again as a team's key, and the GUI shows it as that (`quotaError`) rather than "Allowance unavailable". | [`zhipu_envelope.go`](../../internal/provider/zhipu_envelope.go), [`planquota.go`](../../internal/provider/planquota.go), [`test.go`](../../internal/provider/test.go) |-| Extra body | `ExtraBody` (`extraBody` in providers.json, the editor's *Extra body*, #1604) is a JSON object whose fields `WithExtraBody` adds at the top level of every JSON body sent to the provider: Messages, Chat, Responses and Gemini, relayed as they came or translated, in the gateway's `forwardOnce` and in Test and Detect (`tested`). A field the body has already is kept; a name written `"!name"` replaces it. Values are put in as written, objects whole, not merged. `${sessionId}` in any string, at any depth, becomes the request's session: the agent's session header (`sessionOf`), else the id `conversationID` derives from the first user message (`magpie-` and its hash), so it stays the same through one conversation; Test and Detect send `magpie-test-` and 12 random letters, a new one each time. `model`, `stream` and the conversation (`messages`, `input`, `contents`, `system`, `systemInstruction`, `system_instruction`, `instructions`, `tools`, `ExtraBodyReserved`) are magpie's: Save refuses them with or without `!` (`ExtraBodyOK`), the editor says so as they are typed, and one written into providers.json by hand is dropped on read (`cleanExtraBody`). Not sent with it: `count_tokens`, embeddings, images, video and System One's decision model (other APIs), and a signed-in account's requests, except a ChatGPT (Codex) account's (magpie-releases#18): its requests are built as Codex CLI's (`codexBody`) and take the fields Codex CLI adds by choice, such as `access_programs` (OpenAI's Daybreak), while `store`, `tool_choice` and `include`, which the backend needs as `codexBody` sets them, can't be set (`codexOwned`). Its extraBody is written into providers.json by hand under the account's entry and kept onto the account by `withPicks`; the editor has no box for it. A client's own top-level `access_programs` on Responses or Chat is carried to a Responses build as it came (`Request.AccessPrograms`), so a request translated (a Codex account is streamed, so a whole Responses request is made again) keeps it as a relayed one does. One object goes to every API; fields for one API alone are not a thing yet. A `thinking` budget it puts in on an Anthropic endpoint is fitted to a Claude that thinks only adaptively after the merge, as an agent's is (`adaptiveThinking`, `TestExtraBodyBudgetFittedToAdaptiveModel`). | [`extrabody.go`](../../internal/provider/extrabody.go), [`gateway.go`](../../internal/gateway/gateway.go) |+| Extra body | `ExtraBody` (`extraBody` in providers.json, the editor's *Extra body*, #1604) is a JSON object whose fields `WithExtraBody` adds at the top level of every JSON body sent to the provider: Messages, Chat, Responses and Gemini, relayed as they came or translated, in the gateway's `forwardOnce` and in Test and Detect (`tested`). A field the body has already is kept; a name written `"!name"` replaces it. Values are put in as written, objects whole, not merged. `${sessionId}` in any string, at any depth, becomes the request's session: the agent's session header (`sessionOf`), else the id `conversationID` derives from the first user message (`magpie-` and its hash), so it stays the same through one conversation; Test and Detect send `magpie-test-` and 12 random letters, a new one each time. `model`, `stream` and the conversation (`messages`, `input`, `contents`, `system`, `systemInstruction`, `system_instruction`, `instructions`, `tools`, `ExtraBodyReserved`) are magpie's: Save refuses them with or without `!` (`ExtraBodyOK`), the editor says so as they are typed, and one written into providers.json by hand is dropped on read (`cleanExtraBody`). Not sent with it: `count_tokens`, embeddings, images, video and System One's decision model (other APIs), and a signed-in account's requests, except a ChatGPT (Codex) account's (magpie-releases#18): its requests are built as Codex CLI's (`codexBody`) and take the fields Codex CLI adds by choice, such as `access_programs` (OpenAI's Daybreak), while `store`, `tool_choice` and `include`, which the backend needs as `codexBody` sets them, can't be set (`codexOwned`). Its extraBody is written into providers.json by hand under the account's entry and kept onto the account by `withPicks`; the editor has no box for it. A client's own top-level `access_programs` on Responses or Chat is carried to a Responses build as it came (`Request.AccessPrograms`), so a request translated (a Codex account is streamed, so a whole Responses request is made again) keeps it as a relayed one does. It reaches only the upstreams Codex itself sends it to (`keepsAccessPrograms`, #3172, see [gateway routing](gateway-routing.md)); it is taken out before the extraBody merge, so an `access_programs` the user puts in a provider's extraBody still goes. One object goes to every API; fields for one API alone are not a thing yet. A `thinking` budget it puts in on an Anthropic endpoint is fitted to a Claude that thinks only adaptively after the merge, as an agent's is (`adaptiveThinking`, `TestExtraBodyBudgetFittedToAdaptiveModel`). | [`extrabody.go`](../../internal/provider/extrabody.go), [`gateway.go`](../../internal/gateway/gateway.go) || Native compaction | `NativeCompaction` (`nativeCompaction` in providers.json, the editor's *Compaction* row, `magpie provider set <id> compaction=native\|magpie`; #1099) has Codex's compaction asked of the provider as Codex sent it and its sealed item returned unchanged. Off by default. Only a provider whose wire carries it can be set so (`CarriesCompaction`: a Responses API, a ChatGPT account, GitHub Copilot; not another agent's sign-in or a plugin's), and `CompactsOn(model)` also needs the model asked on Responses. `SetNativeCompaction`, the CLI and the GUI's save refuse it elsewhere. An account keeps it through `Save`'s picks (`withPicks`), and `AddCopy` carries it. The gateway's half is in [gateway-routing](gateway-routing.md). | [`native_compaction.go`](../../internal/provider/native_compaction.go) || providers.json | `Path` is `providers.json` in magpie's config folder. A read that fails returns `ErrUnreadable` and is never treated as empty. `store` writes atomically through a rename and drops the cached catalog (`catalog.Touched`). | [`provider.go`](../../internal/provider/provider.go) || A provider renamed | `Rename` (the editor's id field, `magpie provider set <id> id=<new>`) keeps the old id in `Was`, so a model still picked by it reaches the provider. Usage is recorded under the id a call went out by and never rewritten; whatever reads it names the provider by its id now through `UsageRenames().Of(id, time)`: the summary, the Requests page (its blocks, the probe rows, other computers' days), the Sessions tab's vias, window holds, `NewPricer`/`NewCoster` and the gateway's `loggedAnswers` (cybercaius on Discord: a renamed provider was listed under both ids). `Visit` hands records out as recorded, so `Of` is taken once. An id another provider takes later (a rename onto it, or `Save` of a new provider under it) moves from the first one's `Was` to its `Gave` with the time (`giveID`): calls recorded under it before then stay the first one's, later ones are the new one's. An id held by a provider or a subscription now is never renamed (`Renamed`), and `freeID` doesn't hand out an id in any `Was`, so adding a vendor again gets `-2`. Ids reused before `Gave` existed have no time and go to whoever holds the id now. A providers.json that can't be read keeps the last renames read, not none. The summary and Requests caches key on `Renames.Key`, the vias cache too. | [`rename.go`](../../internal/provider/rename.go), [`block_price.go`](../../internal/usage/block_price.go) |diff --git a/internal/gateway/access_programs_vendor_test.go b/internal/gateway/access_programs_vendor_test.gonew file mode 100644index 000000000..994437e90--- /dev/null+++ b/internal/gateway/access_programs_vendor_test.go@@ -0,0 +1,187 @@+package gateway++import (+ "encoding/json"+ "io"+ "net/http"+ "net/http/httptest"+ "strings"+ "sync"+ "testing"++ "github.com/tidwall/gjson"+ "github.com/yetone/magpie/internal/provider"+)++// programsUpstream stands in for every upstream a test names, by the+// RoundTrip of the gateway's client: nothing reaches the network. It keeps+// what each was sent and answers in the API the path asks.+type programsUpstream struct {+ mu sync.Mutex+ urls []string+ sent [][]byte+}++func (u *programsUpstream) RoundTrip(r *http.Request) (*http.Response, error) {+ b, _ := io.ReadAll(r.Body)+ u.mu.Lock()+ u.urls = append(u.urls, r.URL.String())+ u.sent = append(u.sent, b)+ u.mu.Unlock()+ var reply string+ switch {+ case strings.HasSuffix(r.URL.Path, "/chat/completions"):+ reply = sse(`data: {"id":"c1","object":"chat.completion.chunk","model":"m","choices":[{"index":0,"delta":{"role":"assistant","content":"OK"}}]}`,+ `data: {"id":"c1","object":"chat.completion.chunk","model":"m","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":2,"completion_tokens":1}}`,+ `data: [DONE]`)+ case strings.HasSuffix(r.URL.Path, "/messages"):+ reply = sse(`event: message_start`+"\n"+`data: {"type":"message_start","message":{"id":"m1","type":"message","role":"assistant","model":"m","content":[],"usage":{"input_tokens":2,"output_tokens":0}}}`,+ `event: content_block_start`+"\n"+`data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}`,+ `event: content_block_delta`+"\n"+`data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"OK"}}`,+ `event: content_block_stop`+"\n"+`data: {"type":"content_block_stop","index":0}`,+ `event: message_delta`+"\n"+`data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":1}}`,+ `event: message_stop`+"\n"+`data: {"type":"message_stop"}`)+ default:+ reply = sse(`event: response.created`+"\n"+`data: {"type":"response.created","response":{"id":"r1","model":"m","status":"in_progress"}}`,+ `event: response.output_text.delta`+"\n"+`data: {"type":"response.output_text.delta","delta":"OK"}`,+ `event: response.completed`+"\n"+`data: {"type":"response.completed","response":{"id":"r1","model":"m","status":"completed","output":[],"usage":{"input_tokens":2,"output_tokens":1}}}`)+ }+ return &http.Response{StatusCode: 200, Status: "200 OK", Header: http.Header{"Content-Type": {"text/event-stream"}},+ Body: io.NopCloser(strings.NewReader(reply)), Request: r}, nil+}++func (u *programsUpstream) last(t *testing.T) (string, []byte) {+ t.Helper()+ u.mu.Lock()+ defer u.mu.Unlock()+ if len(u.sent) == 0 {+ t.Fatal("no upstream was asked")+ }+ return u.urls[len(u.urls)-1], u.sent[len(u.sent)-1]+}++// #3172 (csic21, HONGs23, TimWongUp): Codex's access_programs, its Cyber+// program ({"cyber":"standard"}, which a subagent inherits), went to every+// Responses upstream, and OpenCode Go answered it for DeepSeek and GLM+// with 503 "Endpoint is unavailable". Codex itself sends the field only to+// OpenAI (a ChatGPT sign-in, or an API key on its "openai" provider), so it+// now reaches the ChatGPT backend, OpenAI's API, another magpie and a relay+// set to take Codex's requests as sent, as it came, and no one else, on+// every path to a Responses upstream: relayed, and built from Chat.+func TestAccessProgramsGoToOpenAIAlone(t *testing.T) {+ t.Setenv("XDG_CONFIG_HOME", t.TempDir())+ t.Setenv("XDG_CACHE_HOME", t.TempDir())+ const programs = `{"cyber":"standard"}`+ save := func(p provider.Provider, api string) {+ t.Helper()+ if err := provider.Save(p); err != nil {+ t.Fatal(err)+ }+ for _, m := range p.Models {+ if err := provider.SetModelAPI(p.ID+"/"+m, api); err != nil {+ t.Fatal(err)+ }+ }+ }+ save(provider.Provider{ID: "ocgo", Name: "OpenCode Go", Key: "k",+ Chat: "https://opencode.ai/zen/go/v1", Responses: "https://opencode.ai/zen/go/v1",+ Models: []string{"deepseek-v4.1-flash", "glm-5.3-flash"}}, "responses")+ save(provider.Provider{ID: "ocgo-extra", Name: "OpenCode Go, the user's own program", Key: "k",+ Responses: "https://opencode.ai/zen/go/v1", Models: []string{"deepseek-v4.1-flash"},+ ExtraBody: provider.ExtraBody{"access_programs": json.RawMessage(`{"cyber":"users_own"}`)}}, "responses")+ save(provider.Provider{ID: "oczen", Name: "OpenCode Zen", Key: "k",+ Responses: "https://opencode.ai/zen/v1", Models: []string{"deepseek-v4.1-flash"}}, "responses")+ save(provider.Provider{ID: "relay", Name: "Relay", Key: "k",+ Responses: "https://relay.test/v1", Models: []string{"gpt-5.5"}}, "responses")+ save(provider.Provider{ID: "asis", Name: "Relay as sent", Key: "k", CodexAsSent: true,+ Responses: "https://asis.test/v1", Models: []string{"gpt-5.5"}}, "responses")+ save(provider.Provider{ID: "oai", Name: "OpenAI", Key: "sk-k",+ Responses: "https://api.openai.com/v1", Models: []string{"gpt-5.5"}}, "responses")+ save(provider.Provider{ID: "other-magpie", Name: "Another magpie", Key: "k", Preset: provider.RemoteMagpiePreset,+ Responses: "http://remote-magpie.test/v1", Models: []string{"codex/gpt-5.5"}}, "responses")+ save(provider.Provider{ID: "chatonly", Name: "Chat only", Key: "k",+ Chat: "https://chat.test/v1", Models: []string{"deepseek-chat"}}, "chat")+ save(provider.Provider{ID: "msgonly", Name: "Messages only", Key: "k",+ Anthropic: "https://messages.test", Models: []string{"glm-5.3"}}, "anthropic")++ up := &programsUpstream{}+ s := New()+ s.client = &http.Client{Transport: up}+ codex := http.Header{"User-Agent": {"codex_cli_rs/0.150.0 (Mac OS 26.2.0; arm64)"}, "Originator": {"codex_cli_rs"}}++ resp := func(model, ap string) string {+ return `{"model":"` + model + `","input":"Reply with OK.","stream":true,"reasoning":{"effort":"high"},"access_programs":` + ap + `}`+ }+ chat := func(model string) string {+ return `{"model":"` + model + `","messages":[{"role":"user","content":"Reply with OK."}],"stream":true,"access_programs":` + programs + `}`+ }+ for _, c := range []struct {+ name, path, body string+ h http.Header+ want string // the access_programs sent, "" for none+ }{+ // the reporters' requests: Codex's subagent, and their curl+ {"go deepseek, codex", "/v1/responses", resp("ocgo/deepseek-v4.1-flash", programs), codex, ""},+ {"go glm, codex", "/v1/responses", resp("ocgo/glm-5.3-flash", programs), codex, ""},+ {"go deepseek, curl", "/v1/responses", resp("ocgo/deepseek-v4.1-flash", programs), nil, ""},+ {"go, null", "/v1/responses", resp("ocgo/deepseek-v4.1-flash", "null"), codex, ""},+ {"go, chat built as responses", "/v1/chat/completions", chat("ocgo/deepseek-v4.1-flash"), nil, ""},+ // nobody else Codex would send it to+ {"zen", "/v1/responses", resp("oczen/deepseek-v4.1-flash", programs), codex, ""},+ {"relay", "/v1/responses", resp("relay/gpt-5.5", programs), codex, ""},+ {"relay as sent, not from codex", "/v1/responses", resp("asis/gpt-5.5", programs), nil, ""},+ // a value the user set for the provider goes, over the client's+ {"go, the user's extraBody", "/v1/responses", resp("ocgo-extra/deepseek-v4.1-flash", programs), codex, `{"cyber":"users_own"}`},+ // what Codex sends OpenAI goes to OpenAI as it came+ {"openai", "/v1/responses", resp("oai/gpt-5.5", `{"cyber":"daybreak_blue"}`), codex, `{"cyber":"daybreak_blue"}`},+ {"openai, null", "/v1/responses", resp("oai/gpt-5.5", "null"), codex, "null"},+ {"openai, chat built as responses", "/v1/chat/completions", chat("oai/gpt-5.5"), nil, programs},+ {"relay as sent, from codex", "/v1/responses", resp("asis/gpt-5.5", programs), codex, programs},+ {"another magpie", "/v1/responses", resp("other-magpie/codex/gpt-5.5", programs), codex, programs},+ // no other API has the field+ {"chat upstream", "/v1/responses", resp("chatonly/deepseek-chat", programs), codex, ""},+ {"messages upstream", "/v1/responses", resp("msgonly/glm-5.3", programs), codex, ""},+ } {+ rec := httptest.NewRecorder()+ req := httptest.NewRequest("POST", c.path, strings.NewReader(c.body))+ for k, vs := range c.h {+ req.Header[k] = vs+ }+ s.Handler().ServeHTTP(rec, req)+ if rec.Code != 200 {+ t.Errorf("%s: %d %s", c.name, rec.Code, rec.Body.String())+ continue+ }+ u, sent := up.last(t)+ ap := gjson.GetBytes(sent, "access_programs")+ if c.want == "" && ap.Exists() || c.want != "" && ap.Raw != c.want {+ t.Errorf("%s: %s was sent access_programs %q, want %q, in %s", c.name, u, ap.Raw, c.want, sent)+ }+ if strings.Count(string(sent), `"access_programs"`) > 1 {+ t.Errorf("%s: access_programs sent twice: %s", c.name, sent)+ }+ }+}++func TestWithoutTopMember(t *testing.T) {+ big := `{"input":"` + strings.Repeat("a", validMemoMin) + `", "access_programs":{"cyber":"standard"},"model":"m"}`+ for _, c := range []struct{ name, body, want string }{+ {"middle", `{"model":"m", "access_programs": {}, "input":"hi"}`, `{"model":"m", "input":"hi"}`},+ {"first", `{"access_programs":{"cyber":"standard"},"model":"m"}`, `{"model":"m"}`},+ {"last", `{"model":"m", "access_programs": null}`, `{"model":"m"}`},+ {"only", `{"access_programs":{"cyber":"standard"}}`, `{}`},+ {"twice", `{"access_programs":1,"x":2,"access_programs":3,"y":4}`, `{"x":2,"y":4}`},+ {"twice in a row, last", `{"x":2,"access_programs":1,"access_programs":3}`, `{"x":2}`},+ {"escaped key", `{"access_programs":1,"x":2}`, `{"x":2}`},+ {"nested kept", `{"x":{"access_programs":1},"access_programs":2}`, `{"x":{"access_programs":1}}`},+ {"spaces around", "\n {\"model\":\"m\", \"access_programs\":{}}\n", "\n {\"model\":\"m\"}\n"},+ {"long body", big, `{"input":"` + strings.Repeat("a", validMemoMin) + `", "model":"m"}`},+ {"absent", `{"model":"m"}`, `{"model":"m"}`},+ {"not an object", `["access_programs"]`, `["access_programs"]`},+ {"cut short", `{"model":"m", "access_programs":`, `{"model":"m", "access_programs":`},+ } {+ if got := string(withoutTopMember([]byte(c.body), "access_programs")); got != c.want {+ t.Errorf("%s: %q, want %q", c.name, got, c.want)+ }+ }+}diff --git a/internal/gateway/codex_backend.go b/internal/gateway/codex_backend.goindex a2408de5a..31e1bf45a 100644--- a/internal/gateway/codex_backend.go+++ b/internal/gateway/codex_backend.go@@ -2548,6 +2548,33 @@ func forVendor(p provider.Provider, body []byte) []byte {return nb}+// keepsAccessPrograms says a Responses request to p goes with its top-level+// access_programs, the Cyber program a request asks the model under+// (OpenAI's Daybreak, magpie-releases#18): to the ChatGPT backend, to+// OpenAI's API, to another magpie (whose own upstream decides), and to a+// relay that takes Codex's requests as Codex sent them (CodexAsSent). Codex+// itself sends the field to nobody else: only with a ChatGPT sign-in, or an+// API key on its provider named "openai" (codex-rs cyber_access_program.rs,+// for_auth and for_provider), which is how it comes to magpie. OpenCode Go+// answers it, for DeepSeek and GLM, with a 503 "Endpoint is unavailable"+// that says nothing of the field (#3172), and no other vendor has a program+// to read in it.+func keepsAccessPrograms(p provider.Provider, in http.Header) bool {+ return p.Account != nil && p.Account.Agent == "codex" ||+ strings.HasSuffix(p.Host(), "openai.com") ||+ p.IsRemoteMagpie() ||+ p.CodexAsSent && p.Account == nil && fromCodex(in)+}++// withoutAccessPrograms is body without its top-level access_programs, the+// rest byte for byte (withoutTopMember).+func withoutAccessPrograms(body []byte) []byte {+ if !bytes.Contains(body, []byte(`"access_programs"`)) {+ return body+ }+ return withoutTopMember(body, "access_programs")+}+// forVendorChanges reports whether forVendor has anything to leave out of// body, read as it reads it, without decoding the conversation: Codex// sends what the rewrite looks for by name in every request, and itsdiff --git a/internal/gateway/fastjson.go b/internal/gateway/fastjson.goindex 744aeb31d..14f56f991 100644--- a/internal/gateway/fastjson.go+++ b/internal/gateway/fastjson.go@@ -1116,6 +1116,69 @@ func topMember(body []byte, key string) (val []byte, found, ok bool) {return val, found, true}+// withoutTopMember is body without its top-level members named key (every+// one, as a body may name a key twice), every other byte as it came, the+// comma and spaces of a member taken out going with it. A body that isn't a+// valid JSON object, or has no such member, is returned as it is: a body+// that can't be read is not rewritten.+func withoutTopMember(body []byte, key string) []byte {+ var members []memberSpan+ if len(body) >= validMemoMin {+ var valid bool+ if valid, members = longBody(body); !valid {+ return body+ }+ } else if !jsonCheck(body) {+ return body+ }+ start := skipJSONSpace(body, 0)+ raw := trimJSON(body)+ if raw[0] != '{' {+ return body+ }+ if members == nil {+ eachMemberAt(raw, func(k0, k1, v0, v1 int) bool {+ members = append(members, memberSpan{k0, k1, v0, v1})+ return true+ })+ }+ // each run of members named key is cut from its first key to the next+ // member's key, or, when it ends the object, from the end of the member+ // before it (its comma with it) to the end of its last value+ var cuts [][2]int+ for i := 0; i < len(members); {+ if !keyIs(raw[members[i].k0:members[i].k1], key) {+ i+++ continue+ }+ j := i + 1+ for j < len(members) && keyIs(raw[members[j].k0:members[j].k1], key) {+ j+++ }+ switch {+ case j < len(members):+ cuts = append(cuts, [2]int{members[i].k0, members[j].k0})+ case i > 0:+ cuts = append(cuts, [2]int{members[i-1].v1, members[j-1].v1})+ default:+ cuts = append(cuts, [2]int{1, len(raw) - 1})+ }+ i = j+ }+ if len(cuts) == 0 {+ return body+ }+ out := make([]byte, 0, len(body))+ out = append(out, body[:start]...)+ last := 0+ for _, c := range cuts {+ out = append(out, raw[last:c[0]]...)+ last = c[1]+ }+ out = append(out, raw[last:]...)+ return append(out, body[start+len(raw):]...)+}+// memberOf is topMember of obj, a valid JSON object.func memberOf(obj []byte, key string) (val []byte, found bool) {eachMember(obj, func(k, v []byte) bool {diff --git a/internal/gateway/gateway.go b/internal/gateway/gateway.goindex cf34c99d8..07da5e70e 100644--- a/internal/gateway/gateway.go+++ b/internal/gateway/gateway.go@@ -4026,6 +4026,11 @@ func (s *Server) forwardOnce(ctx context.Context, p provider.Provider, to providbody = kimiLockedSampling(p, to, body)body = clinePin(p, to, body)body = codexImagesFit(p, to, body)+ if to == provider.Responses && !keepsAccessPrograms(p, in) {+ // Codex's Cyber program, relayed or built, goes to OpenAI alone+ // (#3172); before the user's own fields, which may name one+ body = withoutAccessPrograms(body)+ }if to == provider.Gemini && !p.FactoryGemini() {// the model is in a Gemini API's and Vertex AI's path// (upstreamPath), as Google's own SDK asks it; Factory's generatediff --git a/internal/gateway/ir.go b/internal/gateway/ir.goindex 3124d7bf7..47c798c1a 100644--- a/internal/gateway/ir.go+++ b/internal/gateway/ir.go@@ -222,7 +222,8 @@ type Request struct {// was sent, null and all, when the request is built again for a// Responses upstream, whose answer to a value it doesn't take// (invalid_access_program) is the client's to read; no other API takes- // it (magpie-releases#18).+ // it (magpie-releases#18). Only OpenAI's upstreams are sent it+ // (keepsAccessPrograms, #3172).AccessPrograms json.RawMessage// Metadata is an Anthropic client's metadata (Claude Code's user_id),// which goes on as it was sent when the request is built again for an