Files
magpie/internal/gui/tune_window_test.go
T
yetone 035c259a6d tune: Codex's compaction windows run to the model's own window or 1M, not the largest prompt seen (zhemu on Discord)
zhemu: Codex Desktop on a CLIProxyAPI provider's 1M GPT model, Settings ›
Long conversations at 750K; Usage › Context › "Other window…" stopped at
~412K. tuneOf took Codex's window as max(272K, the largest prompt seen),
so tune.Compact capped both the offered windows and the user's own value
at the largest prompt (~412K), whatever the model's window or the
user's setting.

Now codexWindow reads the window from magpie's catalog: a model with a
known window (typed or catalog) is offered up to it, and "now" is the
window magpie tells Codex (Settings' long-conversation value or the
model's own compaction, as codexcat writes it), 750K for zhemu. A model
whose window magpie doesn't know caps nothing short of 1M, and a mix
with one unknown is unknown. Codex's own models keep 272K or the
largest prompt. Claude Code is unchanged: only Claude models get the
advice, and other vendors' models use magpie's own window.

Run:
- TestTuneWindowsAreTheUsers (internal/gui), a custom provider with an
  unknown window, 1M and 400K, on Codex and Claude Code. Old code:
  "Codex on cpa/gpt-cpa-open: windows offered up to 410000; want
  1000000" (and for 1m). Broken by hand: told dropped → "now 0, want
  750000"; window=0 for unknown dropped → the mixed case fails.
- TestCompactUnknownWindowCapsNothing (internal/tune): old code "no advice".
- gofmt -l clean; go vet ./... and -tags nogui for GOOS=darwin, linux,
  windows clean.
- context-tune.test.cjs (Chromium + WebKit): 70/70 pass.
- go test ./internal/tune ./internal/gui ./internal/settings ./internal/davsync
  green at this head, rebased on origin/main 95e7c6bcc.

Full suite: SUITE_LINE
2026-10-15 23:08:00 +08:00

111 lines
4.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package gui
import (
"fmt"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/sessions"
)
// tuneCalls is an agent's conversations on one model that grow by 5K a
// call from 20K, n calls each, and are never compacted: at 80 the largest
// prompt magpie has seen is 415K, whatever the model's window.
func tuneCalls(agent, model string, n int) []sessions.Call {
var out []sessions.Call
t0 := time.Now().Add(-48 * time.Hour)
for s := range 2 {
for i := range n {
c := sessions.Call{Time: t0.Add(time.Duration(s)*time.Hour + time.Duration(i)*20*time.Second), Agent: agent,
Session: fmt.Sprint("s", s), Model: model, File: fmt.Sprintf("/sessions/%s-%d.jsonl", agent, s)}
c.Input, c.Output = 20000+5000*i, 800
out = append(out, c)
}
}
return out
}
// The windows Tuned for you offers Codex for a model (zhemu on Discord: a
// CLIProxyAPI provider's GPT model with 1M, Settings › Long conversations
// at 750K, offered no more than 412K, the largest prompt it had been sent).
// A model whose window magpie doesn't know is offered up to 1M, not capped
// under the user's 750K; one typed at 1M is offered to 1M and runs now at
// the 750K magpie tells Codex; one typed at 400K is capped at its window.
// Claude Code is offered no window for another vendor's model, whose
// window is magpie's own (Settings), and a Claude model keeps its 1M.
func TestTuneWindowsAreTheUsers(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
t.Setenv("XDG_CACHE_HOME", t.TempDir())
home := t.TempDir()
if err := provider.Save(provider.Provider{ID: "cpa", Name: "CPA", Key: "k", Chat: "http://127.0.0.1:1/v1",
Models: []string{"gpt-cpa-open", "gpt-cpa-1m", "gpt-cpa-400k"},
Contexts: map[string]int{"gpt-cpa-1m": 1000000, "gpt-cpa-400k": 400000}}); err != nil {
t.Fatal(err)
}
if err := provider.SetCompactAt(750000); err != nil {
t.Fatal(err)
}
for _, e := range provider.Catalog() {
if e.ID == "cpa/gpt-cpa-open" && e.Context != 0 {
t.Fatalf("cpa/gpt-cpa-open has a window of %d: the case needs one magpie doesn't know", e.Context)
}
}
codex := func(model string) tuneAdvice {
t.Helper()
n := 80
if model == "cpa/gpt-cpa-400k" {
n = 70 // to 365K, inside its window
}
st := tuneOf(tuneCalls("codex", model, n), 7, home)
if len(st.Agents) != 1 || len(st.Agents[0].Advice) != 1 || st.Agents[0].Advice[0].Compact == nil {
t.Fatalf("%s: no compaction advice for Codex: %+v", model, st)
}
return st.Agents[0].Advice[0]
}
top := func(a tuneAdvice) int { return a.Compact.Curve[len(a.Compact.Curve)-1].Value }
for _, c := range []struct {
model string
top, window, curr int
}{
{"cpa/gpt-cpa-open", 1000000, 0, 1000000},
{"cpa/gpt-cpa-1m", 1000000, 1000000, 750000},
{"cpa/gpt-cpa-400k", 400000, 400000, 400000},
} {
a := codex(c.model)
if got := top(a); got != c.top {
t.Errorf("Codex on %s: windows offered up to %d; want %d", c.model, got, c.top)
}
if a.Compact.Window != c.window || a.Compact.Current != c.curr {
t.Errorf("Codex on %s: window %d, now %d; want window %d, now %d", c.model, a.Compact.Window, a.Compact.Current, c.window, c.curr)
}
}
// calls on the 400K model and on the one with no known window: that
// one isn't capped by the other's 400K, and what Codex runs at now
// isn't known for both
both := tuneOf(append(tuneCalls("codex", "cpa/gpt-cpa-400k", 70), tuneCalls("codex", "cpa/gpt-cpa-open", 80)...), 7, home)
if a := both.Agents[0].Advice[0]; top(a) != 1000000 || a.Compact.Window != 0 {
t.Errorf("Codex on two models, one window unknown: offered up to %d, window %d; want 1M, unknown", top(a), a.Compact.Window)
}
for _, m := range []string{"cpa/gpt-cpa-open", "cpa/gpt-cpa-1m"} {
st := tuneOf(tuneCalls("claude", m, 80), 7, home)
for _, ag := range st.Agents {
for _, a := range ag.Advice {
if a.Compact != nil {
t.Errorf("Claude Code on %s is offered windows %+v; its window is magpie's own", m, a.Compact.Curve)
}
}
}
}
st := tuneOf(tuneCalls("claude", "claude-opus-4-6", 80), 7, home)
if len(st.Agents) != 1 || len(st.Agents[0].Advice) == 0 || st.Agents[0].Advice[0].Compact == nil {
t.Fatalf("no compaction advice for Claude Code on claude-opus-4-6: %+v", st)
}
if a := st.Agents[0].Advice[0]; top(a) != 1000000 || a.Compact.Window != 1000000 {
t.Errorf("Claude Code on claude-opus-4-6, past 200K: offered up to %d of %d; want 1M", top(a), a.Compact.Window)
}
}