Files
magpie/internal/agent/codex_home_env_test.go
T
yetone f201183047 tune: the compact window is Long conversations, one value for both agents
7eleven (Discord #help): the Usage page's "Tuned for you" compaction
value and the agents' sheet "Long conversations" window were two values
that could disagree. Codex's tune wrote model_auto_compact_token_limit
into config.toml beside the catalog's context_window that Long
conversations writes; Claude's read its own env.

One source of truth: settings.CompactAt / FullContext. Tune's compact
row for Codex and Claude Code now has source "long": its Current is
settings.Compact(), Use calls provider.SetCompactAt (Sync writes Codex's
catalog and Claude's CLAUDE_CODE_AUTO_COMPACT_WINDOW as before), Undo
puts back the CompactAt/Full it stashed under tune.long.compact. So a
value set on either page shows on the other and writes the same config.
writeCompact marks claude.compact_long when the env value came from
settings, so Claude's row reads it as ours, not as the user's.

Migration (Codex Sync, local place): an old tune.codex.compact stash
whose limit is still the one tune wrote moves into Long conversations.
At default or Full it takes tune's value; with a value the user set it
takes tune's only when smaller. config.toml gets the user's earlier
limit back or loses the key; the stash is forgotten. A limit the user
changed after tune wrote it, or wrote themselves, stays and reads
Locked. A provider's Compact at or the user's own Claude env stays
Locked too.

Run (mote, at this tree):
- go vet on agent, provider, gui, tune; GOOS=linux go vet -tags nogui
  ./...; GOOS=windows go vet ./...; go build ./... and -tags nogui: ok
- go test -tags nogui ./internal/agent ./internal/provider
  ./internal/tune ./internal/gui: ok (TestCoreScriptBudget within the
  budget, not raised; context.js is 33 bytes smaller)
- GUI: compact-one-value.test.cjs (new; Chromium and WebKit, en zh
  zh-TW ja de) and context-tune: 104/104 at the rebased head
Broken by hand: the Codex long branch, the Claude long env check and
the Sync migration call each turned red TestClaudeCompactAt,
TestCodexHomeEnvTakesThePick, TestTuneCodexCompact,
TestTuneCodexOwnLimit, TestTuneCodexMigrate (4 subtests),
TestTuneClaudeLong and TestTuneCodexMigrateOnSync. Old context.js and
i18n.js turn compact-one-value red ("You set 272K").
Not run: full suite, Go or GUI (shipped at once per owner; the GUI suite
is still running on mote, reds get their own issues).
Not tried: a real Codex or Claude Code session reading the written
config; the migration on Windows/WSL places (it runs for the local
place only).
2026-10-28 13:52:08 +08:00

62 lines
2.4 KiB
Go

package agent
import (
"os"
"path/filepath"
"strings"
"testing"
)
// With CODEX_HOME set, a model picked for Codex goes to
// $CODEX_HOME/config.toml, the file Codex reads, and its catalog beside it;
// nothing is written under ~/.codex, and what magpie shows as applied is
// read back from there (Wakkana on Discord, Codex 0.162.1: the pick went to
// ~/.codex/config.toml, Codex never saw it, and magpie showed it applied).
func TestCodexHomeEnvTakesThePick(t *testing.T) {
home, _ := codexHome(t, "", "")
if err := os.RemoveAll(filepath.Join(home, ".codex")); err != nil {
t.Fatal(err)
}
ch := filepath.Join(t.TempDir(), "codex-home")
os.MkdirAll(ch, 0o755)
os.WriteFile(filepath.Join(ch, "config.toml"), []byte("model = \"gpt-5.5\"\n"), 0o644)
t.Setenv("CODEX_HOME", ch)
cx := codex(home)
if cx.Path != filepath.Join(ch, "config.toml") || cx.Dir != ch {
t.Fatalf("Codex's files at %s (%s), want under %s", cx.Path, cx.Dir, ch)
}
if err := cx.Fields[0].Set("fake/m1"); err != nil {
t.Fatal(err)
}
b, _ := os.ReadFile(filepath.Join(ch, "config.toml"))
if cfg := string(b); !strings.Contains(cfg, `model = "fake/m1"`) || !strings.Contains(cfg, "magpie-models.json") {
t.Fatalf("$CODEX_HOME/config.toml doesn't have the pick:\n%s", cfg)
}
if _, err := os.Stat(filepath.Join(ch, "magpie-models.json")); err != nil {
t.Fatalf("the catalog isn't beside it: %v", err)
}
if got := codex(home).Fields[0].Get(); got != "fake/m1" {
t.Fatalf("magpie reads the pick back as %q", got)
}
if m := StandIn("codex", "gpt-5"); m != "fake/m1" {
t.Fatalf("the gateway's stand-in for Codex is %q, read from somewhere else", m)
}
// tune reads a compact limit of the user's from $CODEX_HOME's config,
// which comes before magpie's window, and writes none of its own
if err := os.WriteFile(filepath.Join(ch, "config.toml"), append([]byte("model_auto_compact_token_limit = 180000\n"), b...), 0o644); err != nil {
t.Fatal(err)
}
if s, _ := TuneRead(home, "codex", TuneCompact, ""); s.Locked != codexCompactKey || s.Value != "180000" {
t.Fatalf("tune didn't read $CODEX_HOME/config.toml: %+v", s)
}
if _, err := os.Stat(filepath.Join(home, ".codex")); !os.IsNotExist(err) {
var names []string
filepath.WalkDir(filepath.Join(home, ".codex"), func(p string, _ os.DirEntry, _ error) error {
names = append(names, p)
return nil
})
t.Fatalf("written under ~/.codex with CODEX_HOME set: %v", names)
}
}