codex: the Codex app's first 100 models hold the ones used, hidden ones last

@ZhenLuoTTAWDTT on X: with Codex on magpie the model list stops short,
and the models past it can't be chosen.

Cause, from the clients' own code: the Codex app (ChatGPT.app
26.1013.40934, app-initial-90eb42395b7d.js) and the VS Code extension
(openai.chatgpt 26.51021.22027, model-queries-667cf4bd657d.js) both ask
their app-server for model/list with includeHidden, cursor null and
limit 100 (e?.limit??100) and never ask for nextCursor. codex-rs's
list_models pages the list sorted by priority, hidden entries counted,
so anything past the 100th is in no picker there. The CLI's /model
(tui model_popups.rs) lists every preset in a scrolling popup and has no
cap. magpie truncated nothing; its models are numbered after the
account's own, so a user with many models lost everything past ~100 in
the app, while hidden entries (codex-auto-review and the like) took
slots at the top.

Fix: codexcat.Fit ranks the list last of all, on every route magpie
hands Codex a list (/backend-api/codex/models after the user's own
order, /v1/codex/models before its 1 MiB cut, and the
model_catalog_json file via CatalogUsed): hidden entries go after every
listed one, and with more than 100 listed, the models Codex used in the
last 30 days (usage.CodexUsed, most recent first, at most 100; on
/v1/codex/models only the asking key's, CodexUsedBy) are brought into
the first 100 in the order they stood. The rest keep their order. The
list's ETag and X-Models-Etag carry a tag of the used set
(codexcat.UsedTag), so Codex asks again when it uses a model it hadn't;
CodexUsed is read at most once a minute, or at once when Append writes a
Codex call to a model it doesn't hold. Documented in
docs/subsystems/gateway-routing.md.

Tests (each red on the old code, broken by hand: Fit a no-op, the gateway
calls and the used tag removed, Append's codexUsedNew removed):
TestFitKeepsUsedModelsWhereTheCodexAppReads, TestFitLeavesAShortListsOrder,
TestCatalogUsedFits, TestUsedTagIsTheSet (codexcat), TestCodexUsed
(usage), TestCodexModelListKeepsUsedModelsInTheAppsHundred and
TestCodexCatalogKeepsTheKeysUsedModelsFirst (gateway).

Real run: codex-cli 0.162.1's app-server in a sandbox CODEX_HOME (never
~/.codex) with a model_catalog_json of 3 hidden entries at priority 1-3
and 150 listed models, driven over stdio with the app's own request
(model/list includeHidden, limit 100). Before: page 1 held 100 of 153,
3 of them hidden, the last many/model-096, nextCursor "100". The same
catalog put through Fit with many/model-140 used: page 1 has no hidden
entry and ends with many/model-140. The TUI's /model scrolled to all
150 (tmux).

Run with this change on e4ca47b0f: gofmt -l clean; go vet
-tags nogui with GOOS=linux and GOOS=windows on codexcat, usage, gateway
and agent clean. go test -count=1 -timeout 45m: codexcat ok, usage ok;
-run 'Codex|Fit|Used|ModelList|Catalog' on gateway, usage and agent ok
(122s, 11s, 80s). The whole gateway and agent packages ran too, but the
machine's disk was full under other sessions' runs (209M to 2.5G free),
and 959 lines of that log are "no space left on device". Of the reds,
three are not that: TestFolderVarsAreListed is #3884 (CARGO_HOME, red on
main since e4552cca9); TestHistoryKeepsDaysAndDropsOld (gateway) and
TestSuffixModesReachAgentFiles (agent) failed beside half-written
settings files and pass alone 3 of 3 at this commit. Not run: the full
./... suite (no room on the disk); the Codex app and VS Code extension
themselves against a magpie with this change (their request was read
from their bundles and replayed against a real app-server).

Rebased on ca8a6f346 (agent rename and Codex app route, claude_runs in
gateway) and run again: GOOS=windows go vet on agent
and gateway clean; -run 'Codex|Fit|Used|ModelList|Catalog' ok on
codexcat, usage and gateway (75s), and with |Rename ok on agent (68s).
Then rebased on 94c2aaecb (usage rename) and usage run whole again: ok
(88s).
This commit is contained in:
yetone
2026-10-27 18:37:16 +08:00
parent 94c2aaecba
commit 28f3c4d7fd
10 changed files with 533 additions and 6 deletions
+1
View File
@@ -227,6 +227,7 @@ recorded as a prediction failure (`noPrediction`).
- A translated Chat stream's tool calls are read by `index`, not as one after another (`chatDecoder.toolCall` in [`chat.go`](../../internal/gateway/chat.go)). An upstream may send several calls' fragments by turns (index 0, 1, 0, 1…), the first of each with its id and name; read as a new call at each change of index, two calls reached Codex as six `function_call` items, the first two with `{}` and four with no name (mysterious_dove on Discord). The open call streams as it comes; a call that starts while the open one's arguments aren't yet whole JSON is held by its index and given whole, in index order, at `finish_reason` or `[DONE]`. Every translation from Chat (Responses, Messages, Gemini, a whole reply) reads the stream through it. `TestInterleavedToolCallsToResponses`, `TestInterleavedToolCallsToAnthropic`, `TestInterleavedToolCallsWhole`, `TestToolCallsNamedFirstThenArgued` in [`translate_stream_test.go`](../../internal/gateway/translate_stream_test.go).
- A relayed Chat reply whose choice made calls and ended `stop` ends `tool_calls`, streamed (`chatTidy`) and whole (`chatWhole`), as the translations' collector already ends it (`collector.finish`): told it stopped, a client that reads `finish_reason` ends its turn with the calls not run (喜欢一个人 on Discord, a relay in front of deepseek-v4.1-flash). Any other reason stays as it came. `TestChatCallsEndToolCalls` in [`chattidy_finish_test.go`](../../internal/gateway/chattidy_finish_test.go).
- 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`).
- The Codex app and its VS Code extension read only the first 100 of that list by priority, hidden entries counted: each asks its app-server for `model/list` with `limit: 100` and never for the next page (ChatGPT.app 26.1013, openai.chatgpt 26.51021; the CLI's `/model` lists every one). So `codexcat.Fit` ranks the list last of all: hidden entries after every listed one, and, with more than 100 listed (`codexcat.AppList`), the models Codex used in the last 30 days (`usage.CodexUsed`, most recent first, at most 100) brought into the first 100 in the order they stood, the rest kept in theirs. The user's own order (`codexcat.Order`, #855) goes first. The same holds for `/v1/codex/models` (with the models that key's Codex used alone, `usage.CodexUsedBy`, before the 1 MiB cut) and the `model_catalog_json` magpie writes (`codexcat.CatalogUsed`). `codexListTag` carries a tag of the used set (`codexcat.UsedTag`), so the ETag and `X-Models-Etag` change when Codex uses a model it hadn't; `usage.CodexUsed` is read again a minute on, or at once when `Append` writes a Codex call to a model it doesn't hold (`codexUsedNew`). `TestCodexModelListKeepsUsedModelsInTheAppsHundred`, `TestCodexCatalogKeepsTheKeysUsedModelsFirst`, `TestFitKeepsUsedModelsWhereTheCodexAppReads` and `TestCodexUsed` cover it (@ZhenLuoTTAWDTT on X).
- 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 Messages 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. The signature itself is kept in magpie's folder, `gemini-signatures/<key>`, named by its hash (`keepSignature` in [`gemini_sigstore.go`](../../internal/gateway/gemini_sigstore.go)), and the id carries only the key (`__tsk16__<key>`), so an id stays under 64 characters however long the step's signature (#2411: LobeHub's PostgreSQL index turned away a 3KB id). A key magpie no longer has, or whose file doesn't hash to it, reads as no signature; one not read for 90 days is removed. Where the folder can't be written, the id carries the signature as before. Across APIs, only Claude's reasoning is carried to a Responses client, below.
- Claude's reasoning goes to a Responses client (Codex) in an envelope and comes back as Claude wrote it (mysterious_dove_96933 on Discord). Anthropic wants a tool loop's next request to have the turn's thinking blocks, signed, and its `redacted_thinking` whole, or answers 400; Codex with `store: false` keeps only a reply's items, and of a reasoning item only `encrypted_content` is opaque. So the Anthropic decoders name a signature as Claude's (`KSig` with `sealAnthropic`, `Part.SignedBy`), and the Responses encoder and `renderResponses` give each thinking block a reasoning item whose `encrypted_content` is `magpie-claude1:` and the block's JSON in base64 (`claudeThinking`, [`internal/claudeseal`](../../internal/claudeseal/claudeseal.go)): its summary with it, an empty summary for a block signed with no text, and a `redacted_thinking` block as an item of its own. `parseResponses` reads such an item back into the block (`claudePart`), in order, so `anthropicBody` sends it as it was; an envelope that can't be read is its summary, as before. A Gemini thought's signature isn't named, and isn't sealed. The envelope goes to no Responses API: `passthrough` and `forwardOnce` take it out (`withoutClaudeSeals`: the item keeps its summary, one with none goes), `codexInputAs` leaves such an item out for Codex's own models as it does one with nothing sealed, and `codexUpstreamOn` takes it out before the ChatGPT backend; OpenAI's own `encrypted_content` is untouched. `redact.MaskJSON` keeps it whole (a reasoning item's seal), so the placeholders Claude was sent stay in what its signature covers. Claude's subscription names its signatures too, so Codex gets the same items, though its tool loop goes on inside Claude Code's own run, which keeps the thinking itself. `TestCodexToolLoopGivesClaudeItsThinkingBack`, `TestClaudeBlocksNeverGoToAResponsesAPI`, `TestGeminiSignatureIsNotSealedAsClaudes`, `TestMaskJSONKeepsClaudeBlocks`.
- 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. The summary request magpie makes offers none either: `codexInputAs` sends `tools: []` and leaves out the `additional_tools` input item, where Codex's Responses Lite (the entry of a ChatGPT account's gpt-6 model, copied from Codex's own) puts every tool; left in, the model may call one in place of the summary (the likely cause of WuSix's codex/gpt-6-astra "the model wrote no summary" on Discord, not confirmed on a real reply), and a reply with no summary says what it held instead (`answeredWith`). When a provider answers its summary request 404, 405 or 501 (`lacksEndpoint`), 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). Any other failure goes back with its status, said as the compaction's with who answered it (`compactFailed`: "compaction: <provider> (<model>, <base URL>) answered the summary request 502 Bad Gateway: …"), the vendor's code, param and Retry-After kept. A compaction relayed to OpenAI (`/responses/compact` on Codex's own model, as a `/responses` turn) that OpenAI answers with an error status and an empty body, which Codex shows as "unexpected status 502 Bad Gateway: Unknown error", or with a web page, is said in words naming the model, the status, and the account it went to (`codexSaidNothing`, outrageous_piglet on Discord); an error OpenAI says in JSON goes as it came. `TestCodexOwnCompactFailureIsSaid`, `TestCodexCompactFailureSaysCompaction`, `TestCodexCompactNoEndpointCompactsLocally`. 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 summary is wanted only whole (`askWhole`, `holdWriter.wantWhole`): its stream is held to its end, not let through at its first words, so a reply cut short (`errEndedShort`), failed halfway (a 429 "Individual quota reached" after some of it) or ended `incomplete` (`endedIncomplete`, `incompleteReply`) fails over to the next candidate like a failure before any of it, and rests the account as that failure would; one that reached its output limit rests nobody. With nobody left, `codexCompact` answers 502 and never hands Codex the words before the cut as the summary (#2274, huangzuomin: Antigravity cutting each summary short). Codex with context_management's experimental mode compacts in the background at every step while the conversation is over its threshold and drops a failed compaction without a word, so a conversation whose summaries keep failing is asked again every few seconds, each paying for the whole history (1169 compactions in 6 hours there). After `compactFailsMost` (3) failed summaries in a row of one conversation (`prompt_cache_key`) on one model, each within `compactHeldFor` (5 minutes) of the last, the next is answered at once with a 400 naming the last failure, which Codex doesn't ask again, and no model is asked until 5 minutes have passed (`compactFails` in [`codex_compact_guard.go`](../../internal/gateway/codex_compact_guard.go)); a summary that comes clears the count, and a sign-in's 401 or 403 isn't counted. `TestCodexCompactCutSummaryFailsOver`, `TestCodexCompactCutSummaryIsNoSummary`, `TestCodexCompactKeepsFailingIsHeld`.
+6 -2
View File
@@ -30,6 +30,7 @@ import (
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/sessions"
"github.com/yetone/magpie/internal/settings"
"github.com/yetone/magpie/internal/usage"
)
// Codex talks the OpenAI Responses API. Signed in to ChatGPT, it is
@@ -269,8 +270,11 @@ func codexIn(at place) *Agent {
// kept, not written over by the next model switch or sync (#1450).
// Each model's default effort is the one this config starts new
// sessions at, or the model's nearest (codexcat.ModelDefault), which
// Codex takes on a switch to it.
list := func() []byte { return codexcat.CatalogAt(magpieModels("codex"), get("model_reasoning_effort")) }
// Codex takes on a switch to it. The models Codex used are among the
// first 100, which is all the Codex app reads (codexcat.Fit).
list := func() []byte {
return codexcat.CatalogUsed(magpieModels("codex"), get("model_reasoning_effort"), usage.CodexUsed())
}
ownList := func() []byte {
cur, _ := edit.Read(catalogPath)
return codexcat.Keep(cur, list())
+120 -2
View File
@@ -96,10 +96,16 @@ func Catalog(ms []catalog.Model) []byte { return CatalogAt(ms, NewSessionEffort(
// CatalogAt renders models as a whole models.json for a Codex that starts
// new sessions at effort ("" for its models' own defaults): each model's
// default_reasoning_level is ModelDefault's.
func CatalogAt(ms []catalog.Model, effort string) []byte {
func CatalogAt(ms []catalog.Model, effort string) []byte { return CatalogUsed(ms, effort, nil) }
// CatalogUsed is CatalogAt with the models Codex used, most recent first,
// kept where the Codex app reads them (Fit).
func CatalogUsed(ms []catalog.Model, effort string, used []string) []byte {
entries := entriesAt(ms, 0, effort)
Fit(entries, used)
out := struct {
Models []any `json:"models"`
}{Models: entriesAt(ms, 0, effort)}
}{Models: entries}
if out.Models == nil {
out.Models = []any{}
}
@@ -539,6 +545,108 @@ func Order(entries []any, at map[string]int) {
}
}
// AppList is how much of Codex's model list the Codex app and its VS Code
// extension read: each asks its app-server for model/list with
// includeHidden, cursor null and limit 100, and never for the next page
// (ChatGPT.app 26.1013's and openai.chatgpt 26.51021's webview), so a
// model past the 100th by priority is in no picker of theirs and can't be
// chosen there. The CLI's /model lists every one.
const AppList = 100
// Fit ranks entries, Codex's own and magpie's, so the first AppList by
// priority are ones the Codex app can show and the user wants: a hidden
// entry, which no picker shows, goes after every listed one; and when more
// than AppList are listed, the ones used (slugs, most recent first, as
// usage.CodexUsed has them) are brought into the first AppList, in the
// order they stood, in place of the last of the others there. The order
// is otherwise kept, and the priorities numbered 1 to n in it. The slice
// is put in that order too.
func Fit(entries []any, used []string) {
type ranked struct {
e any
slug string
was int
hidden bool
promoted bool
}
rs := make([]ranked, len(entries))
for i, e := range entries {
r := ranked{e: e, was: i}
switch o := e.(type) {
case map[string]any:
r.slug, _ = o["slug"].(string)
v, _ := o["visibility"].(string)
r.hidden = v == "hide"
switch p := o["priority"].(type) {
case float64:
r.was = int(p)
case int:
r.was = p
}
case *model:
r.slug, r.was, r.hidden = o.Slug, o.Priority, o.Visibility == "hide"
}
rs[i] = r
}
slices.SortStableFunc(rs, func(a, b ranked) int {
if a.hidden != b.hidden {
if a.hidden {
return 1
}
return -1
}
return a.was - b.was
})
listed := 0
for _, r := range rs {
if !r.hidden {
listed++
}
}
if listed > AppList && len(used) > 0 {
at := map[string]int{}
for i, r := range rs[:listed] {
at[r.slug] = i
// a ChatGPT account's own model, which magpie's records name
// as codex/<slug>
if _, ok := at["codex/"+r.slug]; !ok {
at["codex/"+r.slug] = i
}
}
n := 0
for _, u := range used {
if i, ok := at[u]; ok && !rs[i].promoted && n < AppList {
rs[i].promoted = true
n++
}
}
// the first AppList: the used ones and as many others, from the top,
// as there is room for, in the order they stood
room := AppList - n
first, rest := make([]ranked, 0, AppList), make([]ranked, 0, listed)
for _, r := range rs[:listed] {
if r.promoted || room > 0 {
if !r.promoted {
room--
}
first = append(first, r)
continue
}
rest = append(rest, r)
}
copy(rs, append(first, rest...))
}
for n, r := range rs {
switch o := r.e.(type) {
case map[string]any:
o["priority"] = n + 1
case *model:
o.Priority = n + 1
}
entries[n] = r.e
}
}
// CacheEntries is Codex's own models, as models_cache.json describes them
// for the ChatGPT account it last asked with, by slug.
func CacheEntries() map[string]map[string]any {
@@ -844,6 +952,16 @@ func Tag(ms []catalog.Model) string {
return hex.EncodeToString(sum[:6])
}
// UsedTag names the set of models Codex used (Fit's used): the list Fit
// makes changes with the set, not with which of them came last.
func UsedTag(used []string) string {
set := slices.Clone(used)
slices.Sort(set)
b, _ := json.Marshal(set)
sum := sha256.Sum256(b)
return hex.EncodeToString(sum[:6])
}
// tagMark is where a tag starts in an ETag magpie made.
const tagMark = "+magpie-"
+109
View File
@@ -0,0 +1,109 @@
package codexcat
import (
"encoding/json"
"fmt"
"os"
"slices"
"testing"
"github.com/yetone/magpie/internal/catalog"
)
// The Codex app and its VS Code extension read the first AppList of
// model/list by priority and never the next page, hidden ones counted
// (@ZhenLuoTTAWDTT on X: "未被显示的模型也无法选择"). Fit puts a hidden
// entry after every listed one and, with more listed than AppList, brings
// the models Codex used into the first AppList, keeping the order
// otherwise.
func TestFitKeepsUsedModelsWhereTheCodexAppReads(t *testing.T) {
var entries []any
// Codex's own: two listed, three hidden near the top
for i, s := range []string{"gpt-6", "codex-auto-review", "gpt-6-mini", "gpt-oss", "gpt-6-luna"} {
v := "list"
if i == 1 || i == 3 || i == 4 {
v = "hide"
}
entries = append(entries, map[string]any{"slug": s, "priority": float64(i + 1), "visibility": v})
}
// magpie's 150, after them
for i := range 150 {
entries = append(entries, map[string]any{"slug": fmt.Sprintf("many/model-%03d", i), "priority": 106 + i, "visibility": "list"})
}
Fit(entries, []string{"many/model-140", "codex/gpt-6-luna", "many/model-003", "gone/model"})
var order []string
pri := map[string]int{}
for i, e := range entries {
o := e.(map[string]any)
slug := o["slug"].(string)
order = append(order, slug)
pri[slug] = o["priority"].(int)
if pri[slug] != i+1 {
t.Fatalf("%s is at %d with priority %d", slug, i, pri[slug])
}
}
first := order[:AppList]
for _, s := range []string{"codex-auto-review", "gpt-oss", "gpt-6-luna"} {
if slices.Contains(first, s) {
t.Errorf("hidden %s is in the first %d", s, AppList)
}
}
if !slices.Contains(first, "many/model-140") {
t.Errorf("the used many/model-140 is at %d, past the first %d the Codex app reads", pri["many/model-140"], AppList)
}
// 2 of Codex's own listed and 98 of magpie's: 97 from the top, then the used one
if want := append([]string{"gpt-6", "gpt-6-mini"}, order[2:99]...); !slices.Equal(first[:99], want) || order[98] != "many/model-096" || order[99] != "many/model-140" || order[100] != "many/model-097" {
t.Errorf("the first 101: %v", order[:101])
}
if last := order[len(order)-3:]; !slices.Equal(last, []string{"codex-auto-review", "gpt-oss", "gpt-6-luna"}) {
t.Errorf("the last three %v, want the hidden ones in their order", last)
}
}
// A list the Codex app reads whole is left in its order, used or not.
func TestFitLeavesAShortListsOrder(t *testing.T) {
var entries []any
for i := range 20 {
entries = append(entries, map[string]any{"slug": fmt.Sprintf("p/m%02d", i), "priority": i + 1, "visibility": "list"})
}
Fit(entries, []string{"p/m19", "p/m10"})
for i, e := range entries {
if s := e.(map[string]any)["slug"]; s != fmt.Sprintf("p/m%02d", i) {
t.Fatalf("%d is %v", i, s)
}
}
}
// The model_catalog_json magpie writes Fits the same way.
func TestCatalogUsedFits(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", os.Getenv("HOME"))
var ms []catalog.Model
for i := range 130 {
ms = append(ms, catalog.Model{ID: fmt.Sprintf("many/model-%03d", i)})
}
var got struct {
Models []map[string]any `json:"models"`
}
if err := json.Unmarshal(CatalogUsed(ms, "", []string{"many/model-120"}), &got); err != nil {
t.Fatal(err)
}
for _, m := range got.Models {
if m["slug"] == "many/model-120" {
if p := m["priority"].(float64); p > AppList {
t.Fatalf("the used model's priority is %v", p)
}
return
}
}
t.Fatal("many/model-120 is not in the catalog")
}
func TestUsedTagIsTheSet(t *testing.T) {
if UsedTag([]string{"a", "b"}) != UsedTag([]string{"b", "a"}) {
t.Error("the tag changed with the order alone")
}
if UsedTag([]string{"a"}) == UsedTag([]string{"a", "b"}) {
t.Error("the tag didn't change with the set")
}
}
+120
View File
@@ -0,0 +1,120 @@
package gateway
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"slices"
"testing"
"time"
"github.com/yetone/magpie/internal/codexcat"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/usage"
)
// The Codex app and its VS Code extension show the first 100 of Codex's
// model list by priority, hidden ones counted, and never ask for the rest
// (@ZhenLuoTTAWDTT on X: with magpie's models in it, the ones past that
// couldn't be shown or chosen). The list magpie hands Codex puts the
// account's hidden models last and brings the ones the user used into the
// first 100; its ETag changes when Codex uses another, so Codex asks again.
func TestCodexModelListKeepsUsedModelsInTheAppsHundred(t *testing.T) {
codexSignedIn(t)
if err := provider.Save(provider.Provider{ID: "codex", Models: []string{"gpt-5.5"}}); err != nil {
t.Fatal(err)
}
var ms []string
for i := range 130 {
ms = append(ms, fmt.Sprintf("model-%03d", i))
}
if err := provider.Save(provider.Provider{ID: "many", Name: "Many", Key: "k", Chat: "http://127.0.0.1:9/v1", Models: ms}); 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":"codex-auto-review","priority":0,"visibility":"hide"},{"slug":"gpt-5.5","priority":1,"visibility":"list"}]}`)
}))
defer up.Close()
was := provider.CodexBase
provider.CodexBase = up.URL + "/backend-api/codex"
defer func() { provider.CodexBase = was }()
list := func() ([]string, string) {
rec := httptest.NewRecorder()
req := httptest.NewRequest("GET", CodexPath+"/models", nil)
req.Header.Set("Authorization", "Bearer chatgpt-token")
New().Handler().ServeHTTP(rec, req)
var got struct {
Models []map[string]any `json:"models"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &got); err != nil {
t.Fatal(err)
}
slices.SortStableFunc(got.Models, func(a, b map[string]any) int {
return int(a["priority"].(float64) - b["priority"].(float64))
})
var out []string
for _, m := range got.Models {
out = append(out, m["slug"].(string))
}
return out, rec.Header().Get("ETag")
}
usage.Append(usage.Record{Time: time.Now().Add(-time.Hour), Agent: "codex", Provider: "many", Requested: "many/model-120", Status: 200})
got, tag := list()
if len(got) != 132 {
t.Fatalf("%d models", len(got))
}
first := got[:codexcat.AppList]
if slices.Contains(first, "codex-auto-review") || got[len(got)-1] != "codex-auto-review" {
t.Errorf("the hidden codex-auto-review is at %d of %d", slices.Index(got, "codex-auto-review")+1, len(got))
}
if !slices.Contains(first, "many/model-120") {
t.Errorf("the used many/model-120 is at %d, past the %d the Codex app reads", slices.Index(got, "many/model-120")+1, codexcat.AppList)
}
if first[0] != "gpt-5.5" || first[1] != "many/model-000" {
t.Errorf("the top is %v", first[:2])
}
usage.Append(usage.Record{Time: time.Now(), Agent: "codex", Provider: "many", Requested: "many/model-125", Status: 200})
got, tag2 := list()
if !slices.Contains(got[:codexcat.AppList], "many/model-125") {
t.Errorf("the newly used many/model-125 is at %d", slices.Index(got, "many/model-125")+1)
}
if tag2 == tag {
t.Errorf("the ETag %q didn't change with the models used", tag)
}
}
// The catalog a Codex on another computer reads puts the models that key's
// Codex used in the first 100, and no other key's.
func TestCodexCatalogKeepsTheKeysUsedModelsFirst(t *testing.T) {
fresh(t)
var ms []string
for i := range 130 {
ms = append(ms, fmt.Sprintf("model-%03d", i))
}
if err := provider.Save(provider.Provider{ID: "many", Name: "Many", Key: "k", Chat: "http://127.0.0.1:9/v1", Models: ms}); err != nil {
t.Fatal(err)
}
usage.Append(usage.Record{Time: time.Now().Add(-time.Hour), Agent: "codex", Provider: "many", Requested: "many/model-110", Status: 200})
usage.Append(usage.Record{Time: time.Now().Add(-time.Hour), Agent: "codex", Provider: "many", Requested: "many/model-111", CallerKeyID: "someone-else", Status: 200})
w := httptest.NewRecorder()
New().Handler().ServeHTTP(w, httptest.NewRequest("GET", CodexCatalogPath, nil))
var list struct {
Models []map[string]any `json:"models"`
}
if err := json.Unmarshal(w.Body.Bytes(), &list); err != nil {
t.Fatal(err)
}
at := map[string]float64{}
for _, m := range list.Models {
at[m["slug"].(string)] = m["priority"].(float64)
}
if p := at["many/model-110"]; p == 0 || p > codexcat.AppList {
t.Errorf("the used many/model-110's priority is %v", p)
}
if p := at["many/model-111"]; p <= codexcat.AppList {
t.Errorf("another key's many/model-111 was brought up, to %v", p)
}
}
+9 -1
View File
@@ -2021,6 +2021,9 @@ func (s *Server) codexModels(w http.ResponseWriter, r *http.Request) {
if at, ok := provider.CodexOrder(); ok {
codexcat.Order(all, at)
}
// the Codex app and its VS Code extension read the first 100 alone:
// hidden ones go last, and the ones the user uses are among the 100
codexcat.Fit(all, usage.CodexUsed())
writeJSON(w, 200, map[string]any{"models": all})
}
@@ -2046,7 +2049,12 @@ func codexListTag() string {
if codexOwnOnly() {
return provider.CodexOwnListTag()
}
return provider.CodexListTag()
// with the models Codex used, which codexcat.Fit brings to the top
tag := provider.CodexListTag()
if used := usage.CodexUsed(); len(used) > 0 {
tag += "." + codexcat.UsedTag(used)
}
return tag
}
// codexInput restores summaries magpie made. For a magpie model, it also
+7 -1
View File
@@ -7,8 +7,10 @@ import (
"net/http"
"strconv"
"github.com/yetone/magpie/internal/access"
"github.com/yetone/magpie/internal/codexcat"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/usage"
)
// CodexCatalogPath is Codex's model list in its own shape, for a Codex that
@@ -40,7 +42,11 @@ func (s *Server) codexCatalog(w http.ResponseWriter, r *http.Request) {
var b bytes.Buffer
b.Write(head)
n, out := 0, 0
for _, e := range codexcat.Entries(ms, 0) {
// the Codex app reads the first 100 alone (codexcat.AppList): the ones
// this key's Codex used are among them, and kept when the end is cut
entries := codexcat.Entries(ms, 0)
codexcat.Fit(entries, usage.CodexUsedBy(access.Caller(r.Context()).KeyID))
for _, e := range entries {
var one bytes.Buffer
enc := json.NewEncoder(&one)
enc.SetEscapeHTML(false)
+125
View File
@@ -0,0 +1,125 @@
package usage
import (
"slices"
"sync"
"time"
)
// codexUsedDays is how far back CodexUsed looks.
const codexUsedDays = 30
var codexUsedMemo struct {
sync.Mutex
path string
at time.Time
gen int // counted up by codexUsedNew: a read begun before it is not kept
all []string
byKey map[string][]string
}
// CodexUsed is the models Codex asked for in the last 30 days, as its
// records name them (a ChatGPT account's own as codex/<slug>), most
// recently used first, at most 100. Codex's model list puts these where the
// Codex app reads it (codexcat.Fit). It is read again at most once a
// minute, the list's ETag being on every reply Codex gets, or at once when
// Codex uses a model it hadn't (codexUsedNew). A call that failed counts:
// the user chose that model all the same.
func CodexUsed() []string {
all, _ := codexUsed()
return all
}
// CodexUsedBy is CodexUsed for the Codex calls made with one gateway key
// alone: a key's holder is told nothing of what others used.
func CodexUsedBy(keyID string) []string {
_, byKey := codexUsed()
return byKey[keyID]
}
func codexUsed() ([]string, map[string][]string) {
path, now := Path(), time.Now()
codexUsedMemo.Lock()
if codexUsedMemo.path == path && now.Sub(codexUsedMemo.at) < time.Minute && !codexUsedMemo.at.After(now) {
all, byKey := codexUsedMemo.all, codexUsedMemo.byKey
codexUsedMemo.Unlock()
return all, byKey
}
gen := codexUsedMemo.gen
codexUsedMemo.Unlock() // the read is done outside: a reply's ETag waits on the lock
last := map[string]time.Time{}
keyLast := map[string]map[string]time.Time{}
readLogSnapshot().visit(now.Add(-codexUsedDays*24*time.Hour), func(r Record) {
if !codexChoice(r) {
return
}
if r.Time.After(last[r.Requested]) {
last[r.Requested] = r.Time
}
k := keyLast[r.CallerKeyID]
if k == nil {
k = map[string]time.Time{}
keyLast[r.CallerKeyID] = k
}
if r.Time.After(k[r.Requested]) {
k[r.Requested] = r.Time
}
})
all := mostRecent(last)
byKey := make(map[string][]string, len(keyLast))
for id, k := range keyLast {
byKey[id] = mostRecent(k)
}
codexUsedMemo.Lock()
if codexUsedMemo.gen == gen {
codexUsedMemo.path, codexUsedMemo.at, codexUsedMemo.all, codexUsedMemo.byKey = path, now, all, byKey
}
codexUsedMemo.Unlock()
return all, byKey
}
// codexChoice reports whether r is a call to a model the user chose in
// Codex.
func codexChoice(r Record) bool {
return r.Agent == "codex" && r.Kind == "" && !r.IsReported() && r.Requested != ""
}
// codexUsedNew has CodexUsed read again on its next call when Append writes
// a Codex call to a model it doesn't hold, for the call's key or at all.
func codexUsedNew(r Record) {
if !codexChoice(r) {
return
}
codexUsedMemo.Lock()
defer codexUsedMemo.Unlock()
// a list of 100 is full: a model past it waits for the minute
isNew := func(l []string) bool { return len(l) < 100 && !slices.Contains(l, r.Requested) }
if isNew(codexUsedMemo.all) || isNew(codexUsedMemo.byKey[r.CallerKeyID]) {
codexUsedMemo.at = time.Time{}
codexUsedMemo.gen++
}
}
// mostRecent is the models in last, the latest first, at most 100.
func mostRecent(last map[string]time.Time) []string {
used := make([]string, 0, len(last))
for m := range last {
used = append(used, m)
}
slices.SortFunc(used, func(a, b string) int {
if c := last[b].Compare(last[a]); c != 0 {
return c
}
if a < b {
return -1
}
if a > b {
return 1
}
return 0
})
if len(used) > 100 {
used = used[:100]
}
return used
}
+35
View File
@@ -0,0 +1,35 @@
package usage
import (
"slices"
"testing"
"time"
)
// CodexUsed is the models Codex asked for, the latest first, its own calls
// alone; a model Codex uses for the first time is in it at once, though
// the list is otherwise read again a minute on (codexcat.Fit puts these in
// the first 100 the Codex app reads).
func TestCodexUsed(t *testing.T) {
pageHome(t)
now := time.Now()
Append(Record{Time: now.Add(-40 * 24 * time.Hour), Agent: "codex", Provider: "p", Requested: "p/too-old", Status: 200})
Append(Record{Time: now.Add(-3 * time.Hour), Agent: "codex", Provider: "p", Requested: "p/a", Status: 200})
Append(Record{Time: now.Add(-2 * time.Hour), Agent: "codex", Provider: "p", Requested: "p/b", Status: 500})
Append(Record{Time: now.Add(-90 * time.Minute), Agent: "claude", Provider: "p", Requested: "p/claude-only", Status: 200})
Append(Record{Time: now.Add(-80 * time.Minute), Agent: "codex", Provider: "p", Requested: "p/keyed", CallerKeyID: "k1", Status: 200})
Append(Record{Time: now.Add(-time.Hour), Agent: "codex", Provider: "p", Requested: "p/a", Status: 200})
if got, want := CodexUsed(), []string{"p/a", "p/keyed", "p/b"}; !slices.Equal(got, want) {
t.Fatalf("CodexUsed %v, want %v", got, want)
}
if got := CodexUsedBy("k1"); !slices.Equal(got, []string{"p/keyed"}) {
t.Errorf("CodexUsedBy(k1) %v", got)
}
if got := CodexUsedBy(""); !slices.Equal(got, []string{"p/a", "p/b"}) {
t.Errorf("CodexUsedBy(\"\") %v", got)
}
Append(Record{Time: now.Add(-time.Minute), Agent: "codex", Provider: "p", Requested: "p/new", Status: 200})
if got := CodexUsed(); !slices.Contains(got, "p/new") {
t.Errorf("a model used for the first time is not in %v", got)
}
}
+1
View File
@@ -245,6 +245,7 @@ func Append(r Record) {
if !r.IsProbe() { // magpie's own test is no call to export
observeLedger(r)
}
defer codexUsedNew(r)
b, err := json.Marshal(r)
if err != nil {
return