Files
magpie/internal/agent/codex_compact_held.go
T
yetone 5c637f17b2 gui: Long conversations says what makes Codex compact before it (zgjfjfl on Discord)
zgjfjfl set Long conversations to "Compact at 544K", and Codex still
compacted every conversation at about 128K. The model is gpt-6-A, with no
context size set on the provider, on v0.1.2281.

Codex 0.165 compacts at min(model_auto_compact_token_limit, 90% of the
window). magpie writes the model's window into the catalog and writes
auto_compact_token_limit only when Long conversations is below that
window. Two things put Codex under 544K, and the row said neither:

- ~/.codex/config.toml's model_context_window or
  model_auto_compact_token_limit, at the top or in a profile, which
  Codex reads before magpie's catalog. The row now names each key, with
  its profile and value, and "Take it out" removes those lines
  (agent.CodexCompactHeld, DropCodexCompactHolds,
  POST /api/settings/codex-compact-held/drop). Nothing is removed unless
  the user clicks.
- The model's own window. gpt-6-A isn't in models.dev, so its window
  comes from the provider's own /models list (context_length). A relay
  that lists it at 128K makes Codex compact at ~115K, and no Long
  conversations value can raise that. The row now names the model Codex
  runs (agent.CodexModel, from config.toml's model and the picked
  profile) and the window magpie tells Codex, and says a larger one is
  set in the provider's Context window (codexWindowUnder).

Which of the two the reporter has is not known: their config.toml and
their relay's list weren't seen.

Run (worktree at origin/main a6951cc63, through mote-run):
- Real Codex 0.165 against a fake Responses server in a sandbox HOME:
  config.toml model_context_window = 128000 made it compact at ~115K
  with Long conversations at 544K; with the key taken out it didn't.
- go test -tags nogui -run 'TestCodexCompactHeld|TestCodexWindowUnder|
  TestCoreScriptBudget|TestI18n' ./internal/agent ./internal/gui: ok.
- go vet -tags nogui for darwin, GOOS=linux and GOOS=windows on
  internal/agent and internal/gui: ok. gofmt -l: clean.
- node --test codex-compact-held.test.cjs on the Linux box (wsl-run):
  20 of 20 ok, Chromium and WebKit, en/zh/zh-TW/ja/de at 900 and 420px.
  On mote Chromium was 10 of 10 ok; mote's Playwright WebKit launch hung
  at 180s for every test there, beside another session's full GUI suite.
  The first Linux WebKit run was red at 420px in en, ja and de: the test
  took scrollTop before Playwright's click scrolled the button into view.
  The button is now wheeled into view first (reader.cjs reach), and the
  check allows only the browser holding scrollTop under a shorter page.
- i18n-keys.test.cjs: 4 of 4 ok.
- A first GUI run was red in every file: the new block declared `box`
  twice in one function (a SyntaxError), so no row drew. Fixed before
  this commit. A rerun of codex-async-compact, compact-at and
  compact-one-value at this tree was cut short when mote's browsers
  stopped launching; codex-async-compact's five Chromium cases had passed
  by then. That run was stopped, not finished.

Broken by hand:
- CodexCompactHeld without the profile lookup: its test went red.
- codexWindowUnder forced to nil (`if true ||`): red at line 46.
- CodexModel without the profile lookup: red at line 52.
- The window line turned off (`if (false) hb.append`): codex-compact-held
  red in all 10 Chromium cases at the own line's waitFor.

Not tried: a real relay listing gpt-6-A, the reporter's config.toml,
WSL Codex configs, `codex --profile` on the command line, the Codex app.

Core script budget: 1,537,356 -> 1,537,669 for the two lines in Codex's
row, with the comment cut and no room added.
2026-10-31 09:03:01 +08:00

112 lines
3.8 KiB
Go

package agent
// Codex reads a model_context_window or model_auto_compact_token_limit in
// its config.toml before the window and threshold magpie's catalog gives
// each model (codexcat: context_window, auto_compact_token_limit, from
// Long conversations): the first becomes every model's window, and Codex
// compacts at 90% of it; the second is where it compacts, whatever the
// catalog says (codex-rs models-manager with_config_overrides). A guide
// for a third-party model that puts model_context_window = 128000 there
// kept every Codex conversation compacting near 128K while Long
// conversations said 544K (zgjfjfl on Discord, 10-31). The value is the
// user's, so magpie never takes it out on its own: Long conversations
// names it, and takes it out when the user asks (DropCodexCompactHolds).
import (
"cmp"
"path/filepath"
"strconv"
"strings"
"github.com/yetone/magpie/internal/edit"
)
// codexCompactKeys are the config keys Codex reads before the catalog's
// window and threshold.
var codexCompactKeys = []string{"model_context_window", "model_auto_compact_token_limit"}
// CodexCompactHold is one such key in a Codex config: at the top level, or
// in the profile the config picks ([profiles.<Profile>]).
type CodexCompactHold struct {
Path string `json:"path"`
Key string `json:"key"`
Profile string `json:"profile,omitempty"`
Value string `json:"value"`
}
// CodexCompactHeld is the keys this machine's Codex config (home "" is the
// user's) has that come before Long conversations.
func CodexCompactHeld(home string) []CodexCompactHold {
path := filepath.Join(here(tuneHome(home)).codexHome(), "config.toml")
var out []CodexCompactHold
for _, k := range codexCompactKeys {
if v, ok := edit.GetTOMLTop(path, k); ok {
out = append(out, CodexCompactHold{Path: path, Key: k, Value: compactValue(v)})
}
}
if p, _ := edit.GetTOMLTop(path, "profile"); p != "" {
t, _ := edit.GetTOMLTable(path, "profiles."+tomlQuoted(p))
for _, k := range codexCompactKeys {
if v, ok := t[k]; ok {
out = append(out, CodexCompactHold{Path: path, Key: k, Profile: p, Value: compactValue(v)})
}
}
}
return out
}
// DropCodexCompactHolds takes those keys out of the config, so Codex
// compacts where Long conversations says.
func DropCodexCompactHolds(home string) error {
for _, h := range CodexCompactHeld(home) {
var err error
if h.Profile == "" {
err = edit.DelTOMLTop(h.Path, h.Key)
} else {
err = edit.DelTOMLKey(h.Path, "profiles."+tomlQuoted(h.Profile), h.Key)
}
if err != nil {
return err
}
}
return nil
}
// CodexModel is the magpie model this machine's Codex config runs (home ""
// is the user's): the profile's the config picks, else the top-level one,
// as Codex reads them; "" when Codex runs a model of its own provider.
// Long conversations can't compact past that model's own window: Codex
// compacts at 90% of it (zgjfjfl on Discord, 10-31: a relay's gpt-6-A).
func CodexModel(home string) string {
path := filepath.Join(here(tuneHome(home)).codexHome(), "config.toml")
m, _ := edit.GetTOMLTop(path, "model")
p, _ := edit.GetTOMLTop(path, "model_provider")
if name, _ := edit.GetTOMLTop(path, "profile"); name != "" {
t, _ := edit.GetTOMLTable(path, "profiles."+tomlQuoted(name))
m, p = cmp.Or(t["model"], m), cmp.Or(t["model_provider"], p)
}
if p != magpieID {
return ""
}
return m
}
// compactValue is a size as written (128_000 too), or the text as it is.
func compactValue(v string) string {
v = strings.TrimSpace(v)
if n, err := strconv.Atoi(strings.ReplaceAll(v, "_", "")); err == nil {
return strconv.Itoa(n)
}
return v
}
// tomlQuoted is a table name part, quoted when it isn't a bare key.
func tomlQuoted(s string) string {
for _, r := range s {
if !(r >= 'a' && r <= 'z' || r >= 'A' && r <= 'Z' || r >= '0' && r <= '9' || r == '_' || r == '-') {
return strconv.Quote(s)
}
}
return s
}