Files
yetone 1aaac93b52 provider, gui: Compact at — where Codex and Claude Code compact a long conversation, set per provider or per model in its editor and to any size in Settings → Long conversations (#876, hisiling)
Until now the only choice was 272K for every model or Full window for every model. The 272K default stays as it is (e950eb08: OpenAI runs its own 1M models at 272K in Codex, and Chen saw 70–90s to a first token at 550K).

What changes:

- A provider's editor has Compact at beside Context window and Max output, in the same form: "500k" for all of its models, "deepseek-v4-flash=272k" for one, and empty to follow Settings. A size at or above a model's window means the whole window. The values are kept in settings.ModelCompacts.
- Settings → Long conversations takes a size of its own beside Compact at / Full window (settings.CompactAt).
- `magpie model compact` takes a size such as 500k.

Order: the model's own value, then its provider's "*", then Settings. Full window turns off only the Settings value. A group uses the smallest value among its members.

Where it applies:

- Codex's model_catalog is told the threshold as context_window, with the whole window as max_context_window.
- Claude Code's CLAUDE_CODE_AUTO_COMPACT_WINDOW follows the main model. A Claude model now takes its provider's value when one is set.
- The Codex list tag changes when the Settings size or Full window changes.

Tests:

- New: TestModelCompactsSetFromTheEditor, TestSetCompactAt, TestCodexCatalogCompactAt and TestClaudeCompactAt.
- TestSettingsSaveKeepsWhatItDoesNotSend now covers the two new fields.
- compact-at.test.cjs (WebKit, en/zh) checks the editor field (shown, refused, chips, saved) and the Settings size (posted, hidden under Full window, no scroll).
- go vet ./..., go test -tags nogui of the root package, provider, codexcat, agent, gui, gateway, settings and catalog under a temp HOME, and GOOS=linux/windows builds all pass.
2026-10-05 15:20:05 +08:00

68 lines
2.1 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 {
out[e["slug"].(string)] = [2]any{e["context_window"], e["max_context_window"]}
}
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(500000), float64(1000000)},
"d/pro": {float64(1000000), nil},
"x/big": {float64(settings.WorkingWindow), float64(1000000)},
"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(500000), float64(1000000)},
"x/big": {float64(400000), float64(1000000)},
"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(500000), float64(1000000)},
"x/big": {float64(1000000), nil},
})
}