gateway: a codex provider used only through routing groups keeps the ChatGPT account's own models out of Codex's list (#1602)
With Codex signed in to ChatGPT, Codex asks magpie's /backend-api/codex/models
for its list, and codexModels passes the backend's own models through,
narrowed only by the codex provider's picks (CodexNativePicked) and the
Agents page's hides (CodexNativeHidden, built from Catalog()). A provider
set to "Only through routing groups" (Provider.Unlisted) has none of its
models in Catalog(), so neither filter saw them: all of the account's
models stayed in Codex's picker, and the Agents page had no Codex group to
hide. Every other agent's list is Catalog-based and already left them out.
provider.CodexNativeUnlisted says which of the account's models are kept
out of agents' lists: every one when the codex provider is Unlisted, and by
slug those a group with Unlists names. codexModels drops them (hidden
entries such as codex-auto-review stay, as with picks; a Codex reached for
failover alone keeps them, as it is handed no groups), spawnChoices drops
the same ones from spawn_agent's list, and CodexListTag carries them so
Codex refetches. gateway-routing.md says so.
Test: TestCodexModelListUnlistedForGroups (a group unlisting gpt-5.5 takes
gpt-5.5 alone; the provider Unlisted takes gpt-6-sol and gpt-5.5 and keeps
codex-auto-review; ETag changes; spawn_agent offers neither; own-only keeps
them). With codex_backend.go and codex_spawn_models.go reverted it fails on
all three checks. Not tried with a real ChatGPT sign-in and Codex app.
Run (sandbox HOME, private GOCACHE, worktree at origin/main 97dfed0f3):
gofmt -l; go vet ./... (darwin), GOOS=linux -tags nogui, GOOS=windows;
go build for all three; go test -tags nogui ./internal/gateway
./internal/provider ./internal/catalog ./internal/codexcat: all ok.
This commit is contained in:
@@ -75,7 +75,7 @@ served by a plugin are in [Provider and plugin ownership](provider-plugins.md).
|
||||
- Codex offers its image tool (the `image_gen` namespace, #870) to every magpie provider. When a provider other than a ChatGPT sign-in or openai.com answers a bare 400, `attempt` asks it once more without the tool (`image_tool.go`). This happens both passed through and translated. If that works, the provider is marked unfit for the tool (`tool:image_gen`) and isn't offered it again. If it doesn't, the tool is restored and the 400 stands (#949).
|
||||
- A turn the agent stopped with its tool call still out is replayed by the client with the call and no result, and Google's APIs turn a history whose call turn goes unanswered away with 400, every later turn of the conversation with it. The Gemini build (`buildCodeAssistSent`, which serves a Google sign-in, Vertex AI and a custom provider's Gemini API alike) answers such a call with the synthetic result the chat build pairs an interrupted turn's call with (`pairToolMessages`, #1275), right after the calls (`pairFunctionTurns`, #1560): beside the answers the client sent there, or in a user turn of its own before what the user said, since a turn mixing a `functionResponse` with text is turned away too (#1533); a Claude model on Antigravity gets it in the one tool_result turn. A call with no id counts as answered by a response of its name.
|
||||
- A Responses request bound for anything but a ChatGPT sign-in has its tool exchanges mended in `attempt`, before it is relayed or translated, plugins included ([`responses.go`](../../internal/gateway/responses.go)). An item inside a run of calls and results (after one of the run's results and before the last, or before any result when the model didn't write it) moves to the end of that run (`joinToolRuns`, #1752, Edward4226): Codex writes its `<image_resize_notice>` right after each result whose image it resized, so a batch of two calls had it between their results, and a strict upstream answered 400 (OpenCode Go/DeepSeek "No tool output found for tool call …", Volcengine's "must be followed by tool messages", #1822); a Chat translation answered the second call with a synthetic result. An item carrying a `call_id` never moves, the model's own reasoning and text between its calls stay, and a call opens a run only when a result after it answers it. A result with no call id becomes a user message after its run (`orphanedToolOutputs`), and a call nothing answers gets a synthetic result at the end of its run (`pairToolItems`, #1341). `TestRelayedCodexImageBatchKeepsItsResultsTogether`, `TestJoinToolRunsOnlyMovesWhatBreaksARun`.
|
||||
- Codex's model list from the backend path (`codexModels`) is the ChatGPT backend's for its sign-in and then magpie's (`provider.CodexListed`), its ETag and every reply's `X-Models-Etag` tagged with `provider.CodexListTag` so a change has Codex ask again. A Codex there only for account failover, not connected (`CodexOwnOnly`, `agent.CodexOwnOnly`; #1385), gets the backend's alone, tagged with `provider.CodexOwnListTag`; neither tag contains the other, so connecting or disconnecting has Codex fetch the list again. The user's picks on the codex provider (`provider.CodexNativePicked`) narrow the backend's listed models. They keep its hidden ones (`visibility: "hide"`), which no picker shows. One of those is `codex-auto-review`: when the list lacks it and no reviewer is set, Codex's auto-review runs on the conversation's model at low effort (#1460, `TestCodexModelListPicksKeepHidden`).
|
||||
- Codex's model list from the backend path (`codexModels`) is the ChatGPT backend's for its sign-in and then magpie's (`provider.CodexListed`), its ETag and every reply's `X-Models-Etag` tagged with `provider.CodexListTag` so a change has Codex ask again. A Codex there only for account failover, not connected (`CodexOwnOnly`, `agent.CodexOwnOnly`; #1385), gets the backend's alone, tagged with `provider.CodexOwnListTag`; neither tag contains the other, so connecting or disconnecting has Codex fetch the list again. The user's picks on the codex provider (`provider.CodexNativePicked`) narrow the backend's listed models. They keep its hidden ones (`visibility: "hide"`), which no picker shows. One of those is `codex-auto-review`: when the list lacks it and no reviewer is set, Codex's auto-review runs on the conversation's model at low effort (#1460, `TestCodexModelListPicksKeepHidden`). The account's own models kept out of agents' lists for routing groups (`provider.CodexNativeUnlisted`) are dropped from it too, hidden ones kept: every one when the codex provider is "Only through routing groups" (`Provider.Unlisted`), and those a group that unlists its members names (`Group.Unlists`). A Codex there only for failover keeps them, as it is handed no groups; `spawnChoices` drops the same ones (#1602, `TestCodexModelListUnlistedForGroups`).
|
||||
- Reasoning state goes through magpie's translation as the vendor wrote it when the agent and the upstream speak the same API (#1445, XYenon): a non-streaming request to a stream-only account, web search on the agent's own API, a plugin, failover. The IR carries it sealed (`Part.Sealed`, `SealedBy`; events `KThinkStart`, `KSealed`, `KTextSig` in [`ir.go`](../../internal/gateway/ir.go)), so each thinking block or reasoning item stays apart, whole or streamed, and a seal is written only for its own API (`sealedBy`). Anthropic's `redacted_thinking` goes back raw (to DeepSeek as a thinking block, below), and a thinking block signed with no text keeps its signature (`aBlock` still writes `"thinking": ""`, #1447), on Claude's subscription too. A Responses reasoning item keeps its id, summary and `encrypted_content`, and goes back to a Responses upstream as it came when it has no reasoning text (`sealedReasoning`; text, another vendor's, is still left out for OpenAI, #1411). Gemini's `thoughtSignature` stays on the call it signed and on text; a later call of a signed step goes without one, and only an unsigned step or Claude on Antigravity gets `skip_thought_signature_validator`. Another API's agent gets the call's signature in its id, as on chat (#687), with its length in the mark (`__ts<n>__`), so an id a client shortened carries no signature and the call goes as unsigned (#1536); it gets no one else's seal. Carrying reasoning across APIs (an envelope) isn't done.
|
||||
- A translated Responses reply gives Codex the reasoning items a Responses upstream sent, as it sent them; any other upstream's reasoning becomes items magpie made: an `rs_` id and a summary, nothing sealed. Codex hands them back on every turn. A request relayed as is to OpenAI's API or Azure OpenAI goes without them (`withoutBareReasoning`) unless `store` is true, because both look the id up among stored items and answer 400 "Item with id 'rs_…' not found" (#1008). Any other Responses upstream gets them, because a DeepSeek model wants its reasoning back (#388). When one answers 400 naming the input (`refusesInput`: `param` is `input`, or the error names an item by id), as a relay in front of OpenAI does with a message of its own, `passthrough` asks once more without them and, once that works, leaves them out for that provider and model from then on (`bareReasoningRefused`, #1044). Codex's compaction offers no tools, so it is relayed even when its turns were translated. When a provider answers its summary request 404, or 400 for an item it didn't find (`itemNotFound`, a relay in front of either), `codexCompact` asks once more with the conversation as text (`plainCompact`), then compacts locally (#866, #1008). So does the ChatGPT backend's 502 "response protection is unavailable" (`protectionRefused`): a long history refused so was answered as text. Codex also compacts remotely, a `compaction_trigger` on `/responses` that wants exactly one compaction item back, on any provider table named "OpenAI", as CC Switch names its relay tables. magpie points those at `/v1` while a magpie model is on, so `handle` makes the summary there too (`codexInput`, `codexCompact`), and magpie's summaries go back to the model as text (#1301).
|
||||
- A Responses request Codex sends a Responses provider is fitted for a backend that isn't OpenAI's before it goes (`passthrough`): Responses Lite's `additional_tools` item lifted into `tools` (`liftAdditionalTools`, #350), the tool search as a function, OpenAI-only fields left out. A relay that checks a request is Codex's own turns that shape away (#1758: 400 "Codex 客户端请求特征无效", 10014). A provider set `codexAsSent` (`magpie provider set <id> codex=as-sent`) gets a request from Codex (`fromCodex`) as Codex sent it, its model name alone swapped, with Codex's own headers (`codexClientHeader`). Off is the default, and a subscription account is never sent so. `TestCodexAsSent`.
|
||||
|
||||
@@ -1221,6 +1221,22 @@ func (s *Server) codexModels(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
own = kept
|
||||
}
|
||||
// and the ones kept for routing groups, every one when the codex
|
||||
// provider serves only through them (#1602): a hidden one stays, as it
|
||||
// does through the picks. A Codex here for account failover alone is
|
||||
// handed no groups, so it keeps its own.
|
||||
if all, off := provider.CodexNativeUnlisted(); (all || len(off) > 0) && !codexOwnOnly() {
|
||||
kept := own[:0]
|
||||
for _, m := range own {
|
||||
o, _ := m.(map[string]any)
|
||||
slug, _ := o["slug"].(string)
|
||||
if v, _ := o["visibility"].(string); v != "hide" && (all || off[slug]) {
|
||||
continue
|
||||
}
|
||||
kept = append(kept, m)
|
||||
}
|
||||
own = kept
|
||||
}
|
||||
// multi-agent V1 when the user asked for it (#141): the version is all
|
||||
// that changes
|
||||
if codexcat.V1() {
|
||||
|
||||
@@ -7,6 +7,8 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -493,6 +495,96 @@ func TestCodexModelListHidesOnAgentsPage(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// The codex provider set to "Only through routing groups" keeps the
|
||||
// ChatGPT account's own models out of Codex's list, as it keeps them out of
|
||||
// every other agent's: Codex was handed all seven the backend listed, and
|
||||
// with none of them in magpie's lists, the Agents page had no Codex group
|
||||
// to hide (#1602). Its hidden ones (codex-auto-review) stay, and the ETag
|
||||
// changes so Codex asks again. A group that keeps its members out of the
|
||||
// lists (Group.Unlists) takes just those.
|
||||
func TestCodexModelListUnlistedForGroups(t *testing.T) {
|
||||
codexSignedIn(t)
|
||||
if err := provider.Save(provider.Provider{ID: "codex", Models: []string{"gpt-6-sol", "gpt-5.5"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("ETag", `"v1"`)
|
||||
io.WriteString(w, `{"models":[{"slug":"gpt-6-sol","priority":1,"visibility":"list"},{"slug":"gpt-5.5","priority":2,"visibility":"list"},{"slug":"codex-auto-review","priority":3,"visibility":"hide"}]}`)
|
||||
}))
|
||||
defer up.Close()
|
||||
was := provider.CodexBase
|
||||
provider.CodexBase = up.URL + "/backend-api/codex"
|
||||
defer func() { provider.CodexBase = was }()
|
||||
list := func() (map[string]bool, string) {
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest("GET", CodexPath+"/models", nil)
|
||||
req.Header.Set("Authorization", "Bearer chatgpt-token")
|
||||
New().Handler().ServeHTTP(rec, req)
|
||||
if rec.Code != 200 {
|
||||
t.Fatalf("%d %s", rec.Code, rec.Body)
|
||||
}
|
||||
var got struct {
|
||||
Models []map[string]any `json:"models"`
|
||||
}
|
||||
json.Unmarshal(rec.Body.Bytes(), &got)
|
||||
out := map[string]bool{}
|
||||
for _, m := range got.Models {
|
||||
slug, _ := m["slug"].(string)
|
||||
out[slug] = true
|
||||
}
|
||||
return out, rec.Header().Get("ETag")
|
||||
}
|
||||
got, before := list()
|
||||
if !got["gpt-6-sol"] || !got["gpt-5.5"] || !got["codex-auto-review"] {
|
||||
t.Fatalf("listed: %v", got)
|
||||
}
|
||||
var id string
|
||||
for _, e := range provider.Catalog() {
|
||||
if provider.CodexOwn(e) && e.Model == "gpt-5.5" {
|
||||
id = e.ID
|
||||
}
|
||||
}
|
||||
if id == "" {
|
||||
t.Fatal("no catalog entry for the account's gpt-5.5")
|
||||
}
|
||||
|
||||
// a group keeping its members out of the lists takes gpt-5.5 alone
|
||||
if err := provider.SaveGroup(provider.Group{ID: "mine", Name: "Mine", Members: []string{id}, Unlists: true}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, grouped := list()
|
||||
if !got["gpt-6-sol"] || got["gpt-5.5"] || !got["codex-auto-review"] || grouped == before {
|
||||
t.Errorf("group unlisting gpt-5.5: %v, ETag %q then %q", got, before, grouped)
|
||||
}
|
||||
|
||||
// the provider only through routing groups takes every one
|
||||
if err := provider.Save(provider.Provider{ID: "codex", Models: []string{"gpt-6-sol", "gpt-5.5"}, Unlisted: true}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, after := list()
|
||||
if got["gpt-6-sol"] || got["gpt-5.5"] || !got["codex-auto-review"] || after == grouped {
|
||||
t.Errorf("only through routing groups: %v (want codex-auto-review alone of the account's), ETag %q then %q", got, grouped, after)
|
||||
}
|
||||
|
||||
// and spawn_agent names none of them to a lead, as Codex's list
|
||||
// doesn't: from models_cache.json, which Codex wrote from a list before
|
||||
os.WriteFile(filepath.Join(os.Getenv("HOME"), ".codex", "models_cache.json"), []byte(`{"etag":"\"v1\"","models":[{"slug":"gpt-6-sol","visibility":"list","priority":1},{"slug":"gpt-5.5","visibility":"list","priority":2}]}`), 0o600)
|
||||
for _, c := range spawnChoices(httptest.NewRequest("POST", CodexPath+"/responses", nil), "group/mine", true) {
|
||||
if c.slug == "gpt-6-sol" || c.slug == "gpt-5.5" {
|
||||
t.Errorf("spawn_agent offers %s", c.slug)
|
||||
}
|
||||
}
|
||||
|
||||
// a Codex here for account failover alone is handed no groups, and
|
||||
// keeps its own
|
||||
was2 := CodexOwnOnly
|
||||
CodexOwnOnly = func() bool { return true }
|
||||
defer func() { CodexOwnOnly = was2 }()
|
||||
if got, _ := list(); !got["gpt-6-sol"] || !got["gpt-5.5"] {
|
||||
t.Errorf("own only: %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
// A reply's X-Models-Etag carries magpie's list too: Codex refetches its
|
||||
// model list on a new one, and only on the backend's it never would when a
|
||||
// provider was added.
|
||||
|
||||
@@ -201,13 +201,18 @@ func spawnChoices(r *http.Request, lead string, codexPath bool) []spawnChoice {
|
||||
// Codex's own, as its backend lists them to it, less the ones
|
||||
// taken out of its list on the Agents page
|
||||
hidden := provider.CodexNativeHidden()
|
||||
// and the ones kept for routing groups (#1602)
|
||||
all, unlisted := provider.CodexNativeUnlisted()
|
||||
if codexOwnOnly() {
|
||||
all, unlisted = false, nil
|
||||
}
|
||||
type own struct {
|
||||
spawnChoice
|
||||
at int
|
||||
}
|
||||
var owns []own
|
||||
for slug, e := range codexcat.CacheEntries() {
|
||||
if v, _ := e["visibility"].(string); v != "list" || hidden[slug] {
|
||||
if v, _ := e["visibility"].(string); v != "list" || hidden[slug] || all || unlisted[slug] {
|
||||
continue
|
||||
}
|
||||
name, _ := e["display_name"].(string)
|
||||
|
||||
@@ -429,6 +429,28 @@ func CodexNativeHidden() map[string]bool {
|
||||
return out
|
||||
}
|
||||
|
||||
// CodexNativeUnlisted is the ChatGPT account's own models kept out of the
|
||||
// lists agents see, which the backend lists to Codex all the same: every
|
||||
// one (all) when the codex provider serves only through routing groups
|
||||
// (Provider.Unlisted), and by slug those a routing group keeps out of the
|
||||
// lists (Group.Unlists). The gateway drops them from its /models answer as
|
||||
// it does the ones hidden; Codex then reaches them only through its groups
|
||||
// (#1602). A provider switched off keeps none out: it serves nothing, and
|
||||
// its setting is kept for when it is switched on again.
|
||||
func CodexNativeUnlisted() (all bool, slugs map[string]bool) {
|
||||
p, ok := find(All(), "codex")
|
||||
all = ok && !p.Off && p.Unlisted
|
||||
for _, e := range Unlisted() {
|
||||
if CodexOwn(e) {
|
||||
if slugs == nil {
|
||||
slugs = map[string]bool{}
|
||||
}
|
||||
slugs[e.Model] = true
|
||||
}
|
||||
}
|
||||
return all, slugs
|
||||
}
|
||||
|
||||
// CodexOrder is where each model of Codex's list goes when the user put
|
||||
// them in an order of their own on the Agents page (#855), by the slug
|
||||
// Codex knows it by — a ChatGPT account's own by its bare one, as the
|
||||
@@ -478,6 +500,14 @@ func codexListTag(kind string) string {
|
||||
for _, slug := range off {
|
||||
ms = append(ms, catalog.Model{ID: "-" + slug})
|
||||
}
|
||||
// and those kept out of the lists for routing groups (#1602)
|
||||
all, unlisted := CodexNativeUnlisted()
|
||||
if all {
|
||||
ms = append(ms, catalog.Model{ID: "~unlisted"})
|
||||
}
|
||||
for _, slug := range slices.Sorted(maps.Keys(unlisted)) {
|
||||
ms = append(ms, catalog.Model{ID: "~unlisted:" + slug})
|
||||
}
|
||||
// and the order the user put them in, the account's own among them
|
||||
for _, id := range ModelOrder("codex") {
|
||||
ms = append(ms, catalog.Model{ID: "^" + id})
|
||||
|
||||
Reference in New Issue
Block a user