Files
magpie/internal/gui/tune_apply_test.go
T
yetone c903c3a411 usage: Use on Codex's window lands over 7 and 30 days, a model's own Compact at named apart (hoobnn on Discord)
hoobnn on Discord (11-01): "观测-用量-上下文里,在7天或者30天的选项里,点击
应用codex的上下文,没生效。" Over 7 or 30 days the range held calls on a
model whose provider has a Compact at of its own (430K); today's didn't.
Tuned for you's "Now" was the largest compaction point over every model
in the range, so it came from that model. Use put the advice in Long
conversations, which did move Codex's other models (Codex's list had
them at the advice), but the card still read 430K and then turned held
("compacts at its own 430K, which comes first"), so nothing seemed to
happen. And with Long conversations at magpie's default, a range of
only such models offered a Use that could move nothing.

codexWindow (internal/gui/tune.go) now reads "Now" from the models Long
conversations moves, and lists each model with its own Compact at as
Apart (largest first). Only a range whose every model has its own is
held (Own), whatever Long conversations says. The card names each
model kept apart under the row: "{model} compacts at its own {value},
which this doesn't move", in en, zh, zh-TW, ja and de.
provider.CompactSetIn is CompactSet with the caller's settings and
group finder, as CompactFor takes them.

Siblings: Claude Code's knob is per model (no max over models), so it
has no such mix. 1, 7 and 30 days go through the same codexWindow;
the GUI test covers 7 and 30 and the Go test the 1-day sample.

Run on mote at d614f2172 plus this change, then at this head (on
7b6b7492a) again: TestCoreScriptBudget, TestTune*, context-tune and
i18n-keys, all ok.
- go test -tags nogui ./internal/provider ./internal/agent
  ./internal/codexcat: ok. ./internal/gui: ok but two tests red the same
  on a clean origin/main 9097687a3 (3 of 3 and 1 of 3), both filed:
  TestSettingsSaveKeepsWhatItDoesNotSend (BusyLoad, #4739) and
  TestColdTurnsTaken (#4510). TestTuneCodexUseLandsInEveryRange (new) puts
  two providers in a sandbox HOME, one with its own 430K, reads the
  advice for 1 model and for both, applies through agent.TuneApply as
  /api/tune/apply does, and checks the card and Codex's model list
  (codexcat): the other model at the advice, the own one at 430K.
- go vet (darwin), GOOS=linux go vet -tags nogui, GOOS=windows go vet
  for internal/gui and internal/provider; gofmt -l clean.
- node --test context-tune (178 pass, both engines, en/zh/zh-TW/ja/de,
  360 and 1100 wide), i18n-keys, gui-zh-tw, gui-de: pass. gui-ja: one
  red, "ja has every zh string", the #4532 string, filed as #4580 and
  fixed in its own commit next.
- TestCoreScriptBudget passes with no raise (two comments in
  context.js cut to fit: 1,533,023 of 1,533,105).

Broken by hand: with tune.go's Apart taken out, the new Go test is red
("only the model with its own 430K: own <nil>, now 430000" and "after
Use 120000: now 430000, own own/gpt-own-1m"); with the old context.js
the new GUI test is red, 42 of 42 (40 cases, both engines).

Not tried: hoobnn's own config on a real Codex; a profile-level
model_auto_compact_token_limit in config.toml (TuneRead reads the
top-level key as Locked, not a profile's); the whole Go suite and the
whole GUI suite (run after the push).
2026-11-01 15:19:32 +08:00

108 lines
4.3 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 (
"encoding/json"
"strconv"
"testing"
"github.com/yetone/magpie/internal/agent"
"github.com/yetone/magpie/internal/codexcat"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/sessions"
)
// hoobnn on Discord (11-01): under Usage › Context, over 7 or 30 days,
// Use on Codex's window did nothing. Those ranges held calls on a model
// whose provider has a Compact at of its own (430K), which Codex runs
// to whatever Long conversations says; today's calls didn't. "Now" was
// the largest window over every model, that one's 430K, so a Use put
// the advice in Long conversations, moved Codex's other models to it,
// and the card still read 430K, now held as that model's own. "Now" is
// where Long conversations has Codex's other models; a model with its
// own threshold is named apart. With no other model in the range, the
// card is held before the click as well, Long conversations at magpie's
// default or not, since Use couldn't move it.
func TestTuneCodexUseLandsInEveryRange(t *testing.T) {
sandboxHome(t)
home := t.TempDir()
for _, p := range []provider.Provider{
{ID: "cpa", Name: "CPA", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"gpt-cpa-1m"}, Contexts: map[string]int{"gpt-cpa-1m": 1000000}},
{ID: "own", Name: "Own", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"gpt-own-1m"}, Contexts: map[string]int{"gpt-own-1m": 1000000}},
} {
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
}
if err := provider.SetModelCompacts("own", map[string]int{"*": 430000}); err != nil {
t.Fatal(err)
}
advice := func(models ...string) tuneAdvice {
t.Helper()
var cs []sessions.Call
for _, m := range models {
cs = append(cs, tuneCalls("codex", m, 80)...)
}
st := tuneOf(cs, 7, home)
if len(st.Agents) != 1 || len(st.Agents[0].Advice) != 1 || st.Agents[0].Advice[0].Compact == nil {
t.Fatalf("%v: no compaction advice for Codex: %+v", models, st)
}
return st.Agents[0].Advice[0]
}
// where Codex's model list, as magpie gives it, has Codex compact
landed := func() map[string]int {
t.Helper()
var out struct {
Models []struct {
Slug string `json:"slug"`
At *int `json:"auto_compact_token_limit"`
} `json:"models"`
}
if err := json.Unmarshal(codexcat.Catalog(provider.CodexCatalog(provider.Catalog())), &out); err != nil {
t.Fatal(err)
}
at := map[string]int{}
for _, m := range out.Models {
if m.At != nil {
at[m.Slug] = *m.At
}
}
return at
}
// one model, with a threshold of its own, and Long conversations at
// magpie's default: Use couldn't move it, so there is none
if a := advice("own/gpt-own-1m"); a.Own == nil || a.Own.Model != "own/gpt-own-1m" || a.Own.Value != 430000 || a.Compact.Current != 430000 {
t.Errorf("only the model with its own 430K: own %+v, now %d; want it named, now 430000", a.Own, a.Compact.Current)
}
week := advice("cpa/gpt-cpa-1m", "own/gpt-own-1m")
if week.Own != nil {
t.Fatalf("7 days: held as %+v, though Long conversations moves cpa/gpt-cpa-1m", week.Own)
}
if len(week.Apart) != 1 || week.Apart[0] != (tuneOwn{Model: "own/gpt-own-1m", Value: 430000}) {
t.Errorf("7 days: apart %+v; want own/gpt-own-1m at 430000", week.Apart)
}
if today := advice("cpa/gpt-cpa-1m"); today.Compact.Current != week.Compact.Current || len(today.Apart) != 0 {
t.Errorf("Codex now: %d today, %d over 7 days (apart %+v); want the same", today.Compact.Current, week.Compact.Current, today.Apart)
}
best := week.Compact.Best
if best == week.Compact.Current {
t.Fatalf("the advice is %d, where Codex runs now: the case needs one that changes it", best)
}
// the click, as /api/tune/apply makes it
if err := agent.TuneApply(home, "codex", agent.TuneCompact, "", strconv.Itoa(best)); err != nil {
t.Fatal(err)
}
for _, ms := range [][]string{{"cpa/gpt-cpa-1m"}, {"cpa/gpt-cpa-1m", "own/gpt-own-1m"}} {
a := advice(ms...)
if a.Compact.Current != best || a.Own != nil || !a.Setting.Ours || a.Setting.Value != strconv.Itoa(best) {
t.Errorf("%v after Use %d: now %d, own %+v, setting %+v; want now %d, put in by magpie", ms, best, a.Compact.Current, a.Own, a.Setting, best)
}
}
at := landed()
if at["cpa/gpt-cpa-1m"] != best || at["own/gpt-own-1m"] != 430000 {
t.Errorf("Codex's list after Use %d: compacts at %v; want cpa/gpt-cpa-1m at %d, own/gpt-own-1m at its own 430000", best, at, best)
}
}