Files
magpie/internal/codexcat/compact_test.go
T
yetone deb0b8da72 codexcat: Codex shows a model's whole window and compacts at Long conversations' threshold (#3975, #3562; 01huadalang on Discord)
01huadalang on Discord: a Command Code model with a 1M window showed
258,400 in Codex. EasonSpirit (#3975) and greenfinchgreen-gif (#3562)
saw the same on OpenCode Go's 1M models.

The cause: magpie put Long conversations' threshold (272K by default, a
Compact at set on the model or provider, or a size typed in Settings)
into the Codex catalog entry's context_window, and the real window into
max_context_window. Codex shows context_window at 95% as the
conversation's context, so 272000 showed as 258400. The plugin was not
at fault: commandcode lists these models at 1M, from Command Code's
own list or its fallback table.

Now each catalog entry carries two values:
- context_window is the model's whole window.
- auto_compact_token_limit is the threshold, written only when it is
  below the window. Full window writes none.
This applies to entries magpie writes itself (entriesAt) and to a
relay's own Codex-shape entry (listedEntry). Codex 0.165 compacts at
the lesser of the threshold and 90% of the window. A
model_auto_compact_token_limit the user put in config.toml still comes
first.

Two other changes:
- max_context_window is never written now. It stays a field of
  codexcat's model, so Keep doesn't carry an old catalog's value
  forward as if the user had added it.
- Tag carries a format mark (catalogFormat), so a Codex holding a list
  in the old shape fetches it again.

Codex's own models (codex/) and Codex's base_instructions are
unchanged.

One behaviour moves. Codex used to compact at 90% of 272000 (244,800).
It now compacts at 272,000, the number the setting shows ("Compact at
272K").

Tests:
- TestCodexWindowShownCompactApart (new). Checks Command Code's
  claude-opus-5-5 at 1M, a relay entry, and a relay entry with its own
  Compact at, under the default, Compact at 400K, and Full window.
- TestCodexCatalogCompactAt, TestCodexCatalogToolSearch,
  TestCodexCatalogCompactAtReachesDisk and
  TestTypedWindowReachesCodexAndClaudeCode now expect the window in
  context_window and the threshold in auto_compact_token_limit.

Broken by hand: I put the old codexcat.go back. TestCodexCatalogToolSearch,
TestCodexCatalogCompactAt and TestCodexWindowShownCompactApart went red
("commandcode/claude-opus-5-5 told window/compact [272000 <nil>]; want
[1e+06 272000]"). With this change restored, all three are green.

Real Codex 0.165.0 in a sandbox CODEX_HOME, against a local fake
Responses server, using the catalog codexcat.Catalog now writes for
commandcode/claude-opus-5-5 at 1M:
- The rollout records model_context_window 950000. The old-shape entry
  (272000 with a max of 1000000) records 258400, the reporter's number.
- A turn reporting 260K tokens is not compacted. One reporting 280K is
  (CONTEXT CHECKPOINT COMPACTION). The old shape compacted at 260K.
- model_auto_compact_token_limit = 500000 in config.toml keeps the
  280K turn uncompacted.

Run (mote-run, at this change on origin/main 9b833205f; rebased onto 56bc3d515, which touches only GUI plugin files, and codexcat run again there: ok):
- gofmt -l: clean.
- go vet -tags nogui ./... for darwin, GOOS=linux and GOOS=windows.
- go test -tags nogui -count=1 -timeout 90m on codexcat, agent, gateway,
  provider, gui, cli and catalog: all 7 ok (codexcat 1.0s, agent 182s, gateway 510s,
  provider 119s, gui 70s, cli 43s, catalog 0.7s).

Not changed or not tried:
- Claude Code's CLAUDE_CODE_AUTO_COMPACT_WINDOW and Grok Build's
  context_window still carry the threshold, so they show it as the
  window. Filed as #4319.
- Not tried: a real Command Code account, and the Codex desktop app.
  The app reads the same catalog through the same app-server.
- #3553 asks for effective_context_window_percent 100. That isn't done:
  95% is Codex's own value for its models.
2026-10-30 03:47:58 +08:00

135 lines
5.0 KiB
Go

package codexcat
import (
"encoding/json"
"os"
"path/filepath"
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/settings"
)
// Codex is told the threshold the user set (#876, hisiling: 272K was the
// only one): a model's own or its provider's before the one for every
// model, which may be any size too; one at or above the window is the
// whole window, and Full window still runs the rest to theirs.
func TestCodexCatalogCompactAt(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", os.Getenv("HOME"))
t.Setenv("XDG_CONFIG_HOME", filepath.Join(os.Getenv("HOME"), ".config"))
windows := func() map[string][2]any {
var got struct {
Models []map[string]any `json:"models"`
}
json.Unmarshal(Catalog([]catalog.Model{
{ID: "d/flash", Name: "flash", Context: 1000000, Compact: 500000},
{ID: "d/pro", Name: "pro", Context: 1000000, Compact: 2000000},
{ID: "x/big", Name: "big", Context: 1000000},
{ID: "x/small", Name: "small", Context: 128000},
}), &got)
out := map[string][2]any{}
for _, e := range got.Models {
if e["max_context_window"] != nil {
t.Errorf("%s: max_context_window %v; the window is context_window", e["slug"], e["max_context_window"])
}
out[e["slug"].(string)] = [2]any{e["context_window"], e["auto_compact_token_limit"]}
}
return out
}
check := func(what string, want map[string][2]any) {
t.Helper()
got := windows()
for id, w := range want {
if got[id] != w {
t.Errorf("%s: %s told %v; want %v", what, id, got[id], w)
}
}
}
check("default", map[string][2]any{
"d/flash": {float64(1000000), float64(500000)},
"d/pro": {float64(1000000), nil},
"x/big": {float64(1000000), float64(settings.WorkingWindow)},
"x/small": {float64(128000), nil},
})
if err := settings.Save(settings.Settings{CompactAt: 400000}); err != nil {
t.Fatal(err)
}
check("compact at 400K", map[string][2]any{
"d/flash": {float64(1000000), float64(500000)},
"x/big": {float64(1000000), float64(400000)},
"x/small": {float64(128000), nil},
})
if err := settings.Save(settings.Settings{CompactAt: 400000, FullContext: true}); err != nil {
t.Fatal(err)
}
check("full window", map[string][2]any{
"d/flash": {float64(1000000), float64(500000)},
"x/big": {float64(1000000), nil},
})
}
// Codex shows a model's context_window (95% of it) as the conversation's
// context, so a 1M model of a plugin's told magpie's 272K threshold there
// showed 258K (01huadalang on Discord, Command Code's 1M models). The
// window is the model's own, and the threshold goes where Codex compacts
// (auto_compact_token_limit), for a model magpie describes and for a
// relay's own entry alike.
func TestCodexWindowShownCompactApart(t *testing.T) {
t.Setenv("HOME", t.TempDir())
t.Setenv("USERPROFILE", os.Getenv("HOME"))
t.Setenv("XDG_CONFIG_HOME", filepath.Join(os.Getenv("HOME"), ".config"))
relay := json.RawMessage(`{"slug":"big-listed","display_name":"Big","base_instructions":"x","context_window":1000000,"max_context_window":1000000,"auto_compact_token_limit":null,"supported_reasoning_levels":[]}`)
entries := func() map[string]map[string]any {
var got struct {
Models []map[string]any `json:"models"`
}
json.Unmarshal(Catalog([]catalog.Model{
{ID: "commandcode/claude-opus-5-5", Name: "Claude Opus 5.5", Context: 1000000},
{ID: "relay/big-listed", Name: "Big", Context: 1000000, Codex: relay},
{ID: "relay/big-own", Name: "Big own", Context: 1000000, Codex: relay, Compact: 200000},
}), &got)
out := map[string]map[string]any{}
for _, e := range got.Models {
out[e["slug"].(string)] = e
}
return out
}
check := func(what string, want map[string][2]any) {
t.Helper()
got := entries()
for id, w := range want {
e := got[id]
if e == nil {
t.Fatalf("%s: no entry for %s", what, id)
}
if have := [2]any{e["context_window"], e["auto_compact_token_limit"]}; have != w {
t.Errorf("%s: %s told window/compact %v; want %v", what, id, have, w)
}
}
}
// magpie's default: the plugin's 1M model shows 1M and compacts at
// 272K; a relay's entry is the vendor's (its null), unless the user
// set a threshold on it
check("default", map[string][2]any{
"commandcode/claude-opus-5-5": {float64(1000000), float64(settings.WorkingWindow)},
"relay/big-listed": {float64(1000000), nil},
"relay/big-own": {float64(1000000), float64(200000)},
})
if err := settings.Save(settings.Settings{CompactAt: 400000}); err != nil {
t.Fatal(err)
}
check("compact at 400K", map[string][2]any{
"commandcode/claude-opus-5-5": {float64(1000000), float64(400000)},
"relay/big-listed": {float64(1000000), float64(400000)},
"relay/big-own": {float64(1000000), float64(200000)},
})
if err := settings.Save(settings.Settings{FullContext: true}); err != nil {
t.Fatal(err)
}
check("full window", map[string][2]any{
"commandcode/claude-opus-5-5": {float64(1000000), nil},
"relay/big-listed": {float64(1000000), nil},
})
}