Files
magpie/internal/gui/tune_async_test.go
T
yetone 5d955a4d4b tune: Codex can take the compaction advice in the background, its own limit kept (herman_hong)
herman_hong on Discord: Tuned for you advises Codex compact at ~120K.
A foreground compaction kept them waiting, so they had set 384K by hand.
Now that Codex compacts in the background, they want the advice there
and the 384K kept as the backstop.

Codex has this since 0.163.0 (checked in codex-rs at rust-v0.166.0, not
in 0.162.1): [features.async_compaction] enabled and
model_async_compact_threshold_percent, an integer share (1 to 100) of
the model's window, 65 by default. It is Stage::UnderDevelopment, runs
only with remote compaction v2 (which magpie already sets on its
provider), and starts none once the foreground limit is reached. magpie
already writes it from Settings' Background compaction (#2490), with
the version gate and the user's own keys kept. This drives that setting
from tune:

- agent: knob TuneAsync ("async_compact") on codex. TuneRead gives the
  share, TuneApply turns Background compaction on at it (stashing what
  Settings had the first time), and TuneUndo puts that back unless the
  user changed it since. The foreground limit (Long conversations, or
  the user's model_auto_compact_token_limit) is never touched. Async
  keys the user wrote in config.toml come first (codexAsyncOwn): the
  row says so and Apply errors. A table magpie wrote itself doesn't
  count as the user's.
- gui/tune.go: advice.async holds the advice as a share of the window,
  rounded down. With timed compactions it uses the token-cheapest
  window, since a background compaction keeps no one waiting. It is
  offered only when it would start before the foreground limit (the
  lesser of the limit now and 90% of the window). held lists Codexes
  too old for a share.
- context.js: the row gets a text button, "Use 120K in the background",
  whose tip gives the share and says the foreground limit stays. After
  Use a chip says so, with Undo background. A config.toml of the
  user's, or a Codex too old, gets a one-line caveat and no button.
  The too-old line is shared with Settings' row (codexHeld). No select,
  no layout change. zh, zh-TW, ja and de are in i18n.js.
- The core script budget goes up by 1,571 bytes for the row
  (core_budget_test.go says why). app.js is 278 bytes smaller.

Run (at this change on origin/main 6893cfbe3; rebased on d0a61b856,
whose new commits touch none of these files, and TestCoreScriptBudget,
TestTune*, TestCodexAsync* run again there: ok):
- gofmt -l: clean.
- mote-run go vet ./internal/agent ./internal/gui, plus GOOS=linux
  -tags nogui and GOOS=windows vet of both: ok.
- mote-run go test -count=1 -timeout 45m ./internal/agent
  ./internal/tune ./internal/provider: ok. ./internal/gui: the new
  tests and TestCoreScriptBudget pass. TestSettingsSaveKeepsWhatItDoes
  NotSend is red on clean origin/main too (#4514). Eleven AppKit window
  tests (TestMainWindowCorners, TestPanelCloseHidesIt, ...) were killed
  at their 30-40s on mote at load ~25; this change touches no window
  code.
- wsl-run node --test context-tune, codex-async-compact and i18n-keys:
  the new test passes in chromium and webkit, in all five languages,
  at 360 and 1100. It checks the label, the tip, the POST body
  {knob: async_compact, value: "30"}, the chip and Undo, the locked
  and held caveats, and no overflow. i18n-keys' "a plugin's words are
  its own" is red on main (#4524).

Broken by hand (each went red, then restored):
- codexAsyncOwn always "": TestTuneCodexAsyncOwn red in all 4 cases.
- asyncOurs ignoring the set share: TestTuneCodexAsync red (it would
  undo over the user's 50).
- codexAsyncAt keyed on WaitBest, and always ok: TestCodexAsyncAt red
  (timed cheapest, foreground, 90%).
- context.js posting without the knob: the GUI test red, knob
  "compact" where "async_compact" was wanted.

Not tried: a real Codex 0.163+ turn compacting in the background
through magpie, and a real account's /api/tune. When the models' windows
differ, the share comes from the advice's window, so the tokens are
approximate; the tip shows the share Codex gets.
2026-10-31 17:12:16 +08:00

37 lines
1.7 KiB
Go

package gui
import (
"testing"
"github.com/yetone/magpie/internal/tune"
)
// TestCodexAsyncAt: the advice as a share of the window Codex's background
// compaction starts at, rounded down; none where it would start no sooner
// than the foreground limit (the lesser of the limit now and 90% of the
// window) or the window isn't known. Timed compactions take the cheapest
// window, since one in the background keeps no one waiting.
func TestCodexAsyncAt(t *testing.T) {
for _, c := range []struct {
name string
r *tune.Compaction
pct, value int
ok bool
}{
{"herman_hong's", &tune.Compaction{Current: 384000, Window: 400000, Best: 120000}, 30, 120000, true},
{"rounded down", &tune.Compaction{Current: 384000, Window: 400000, Best: 123000}, 30, 120000, true},
{"timed takes the cheapest", &tune.Compaction{Current: 384000, Window: 400000, Best: 200000, Cheapest: 100000, Facts: tune.Facts{Took: 3}}, 25, 100000, true},
{"untimed keeps the advice", &tune.Compaction{Current: 384000, Window: 400000, Best: 200000, Cheapest: 100000}, 50, 200000, true},
{"not before the foreground", &tune.Compaction{Current: 120000, Window: 400000, Best: 120000}, 30, 120000, false},
{"not past 90%", &tune.Compaction{Window: 400000, Best: 380000}, 95, 380000, false},
{"at least 1%", &tune.Compaction{Current: 900000, Window: 1000000, Best: 5000}, 1, 10000, true},
{"no window", &tune.Compaction{Current: 384000, Best: 120000}, 0, 0, false},
{"none", nil, 0, 0, false},
} {
pct, v, ok := codexAsyncAt(c.r)
if ok != c.ok || (ok && (pct != c.pct || v != c.value)) || (!ok && c.pct == 0 && (pct != 0 || v != 0)) {
t.Errorf("%s: %d%% %d %v, want %d%% %d %v", c.name, pct, v, ok, c.pct, c.value, c.ok)
}
}
}