Files
magpie/internal/agent/codex_async.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

410 lines
14 KiB
Go

package agent
// Codex's background compaction (0.163 on, #2490): with
// [features.async_compaction] enabled, Codex sends a compaction_trigger of
// its own while a turn goes on, once the conversation is past
// model_async_compact_threshold_percent of the model's window (in that
// table; one at the top level is not read), and takes the summary in at the
// turn's next step. It starts one only on a provider whose
// capabilities.remote_compaction is "v2", which Codex gives OpenAI's own and
// Azure alone: on magpie's [model_providers.magpie] it never ran until that
// was written there too. magpie's gateway answers the trigger as it does
// Codex's own compaction (codexCompact, or the provider's native one).
//
// What magpie writes is kept in the stash, so a value the user set there
// (by hand, or a value they changed since) is theirs: never written over,
// and left when the setting is turned off.
//
// The table form is read only from Codex 0.163.0-alpha.8 on. Before it,
// every [features] entry Codex doesn't know is a bool, and a table there
// stops Codex from loading its config at all ("invalid type: map, expected
// a boolean": 0.162.1's FeaturesToml, and the Codex app on Windows of
// 多喝热水 on Discord, which wouldn't open). The bare async_compaction =
// true every Codex reads: an older one ignores it, a newer one turns the
// feature on at its own 65%. So the table, which the threshold needs, is
// written only when every Codex that reads this config is known to be new
// enough (codexReaders), and the bare key otherwise; a table magpie wrote
// before is taken back out where it no longer is.
import (
"encoding/json"
"errors"
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
"sync"
"github.com/pelletier/go-toml/v2"
"github.com/yetone/magpie/internal/edit"
)
const (
codexAsyncTable = "features.async_compaction"
codexAsyncPercent = "model_async_compact_threshold_percent"
// codexAsyncTableFrom is the first Codex whose [features] takes
// async_compaction as a table (FeatureToml<AsyncCompactionConfigToml>
// in codex-rs/features, rust-v0.163.0-alpha.8; alpha.5 has none)
codexAsyncTableFrom = "0.163.0-alpha.8"
)
// CodexReader is a Codex that reads a config: what it is ("cli", "app",
// "ide", "wsl") and its version, "" when that isn't known.
type CodexReader struct {
Kind string `json:"kind"`
Version string `json:"version,omitempty"`
}
// codexReaders are the Codexes that read the config at at: the CLI on
// PATH, the desktop app's own (its bundled codex-cli on a Mac; the Store
// app's on Windows, whose version magpie can't read; on Linux the
// package's resources/codex, which says its version as the CLI does:
// codex-cli 0.166.0-alpha.5.1 in chatgpt 26.1027), the editor
// extension's, or a WSL distro's CLI. A var so tests can name them.
var codexReaders = func(at place) []CodexReader {
if at.spell != nil {
return []CodexReader{{Kind: "wsl", Version: at.version}}
}
var out []CodexReader
if bin, _ := (&Agent{ID: "codex", Bin: "codex"}).cliBin(); bin != "" {
real, _ := throughShim(bin)
out = append(out, CodexReader{Kind: "cli", Version: installedVersion(real)})
}
app := desktopApps["codex"]
switch runtime.GOOS {
case "darwin":
for _, bundle := range macBundles(app) {
if v, ok := bundledCodexVersion(bundle, app.id); ok {
out = append(out, CodexReader{Kind: "app", Version: v})
}
}
case "windows":
if storeAppVersion(windowsStorePackages(), app.store, app.publisher) != "" {
out = append(out, CodexReader{Kind: "app"})
}
case "linux":
if dir := app.linuxDir(); dir != "" {
out = append(out, CodexReader{Kind: "app", Version: installedVersion(filepath.Join(dir, "resources", "codex"))})
}
}
if extensionHere(at.home, "openai.chatgpt") {
out = append(out, CodexReader{Kind: "ide"})
}
return out
}
var codexPackageVersionRe = regexp.MustCompile(`"version"\s*:\s*"([^"]+)"`)
// bundledCodexVersion is the version of the codex a Codex app bundle
// ships (Contents/Resources/codex-cli/codex-package.json, "version":
// "0.163.0-alpha.10.2" in ChatGPT.app 26.1013); ok is false for a bundle
// that is no Codex app, "" for one whose version can't be read.
func bundledCodexVersion(bundle, id string) (v string, ok bool) {
if b, err := os.ReadFile(filepath.Join(bundle, "Contents", "Resources", "codex-cli", "codex-package.json")); err == nil {
if m := codexPackageVersionRe.FindSubmatch(b); m != nil {
return strings.TrimSpace(string(m[1])), true
}
return "", true
}
b, err := os.ReadFile(filepath.Join(bundle, "Contents", "Info.plist"))
if err != nil {
return "", false
}
m := plistIDRe.FindSubmatch(b)
return "", m != nil && strings.TrimSpace(string(m[1])) == id
}
// codexAsyncBlocked are the readers that keep the table out: one older
// than codexAsyncTableFrom, or of no known version. None at all is
// unknown as well: a Codex magpie can't see may read the config.
func codexAsyncBlocked(rs []CodexReader) []CodexReader {
if len(rs) == 0 {
return []CodexReader{{Kind: "cli"}}
}
var out []CodexReader
for _, r := range rs {
if r.Version == "" || Newer(codexAsyncTableFrom, r.Version) {
out = append(out, r)
}
}
return out
}
// codexAsyncHeld is what kept the table out of this machine's config at
// its last sync, for Settings' row to say; nil when nothing did.
var codexAsyncHeld struct {
sync.Mutex
rs []CodexReader
}
// CodexAsyncHeld is the Codexes here that kept the background
// compaction's table, and with it its threshold, out at the last sync:
// one too old for the feature, or one whose version isn't known.
func CodexAsyncHeld() []CodexReader {
codexAsyncHeld.Lock()
defer codexAsyncHeld.Unlock()
return codexAsyncHeld.rs
}
// codexAsyncTableOK says whether the table may be written at at.
func codexAsyncTableOK(at place) bool {
held := codexAsyncBlocked(codexReaders(at))
if at.spell == nil {
codexAsyncHeld.Lock()
codexAsyncHeld.rs = held
codexAsyncHeld.Unlock()
}
return len(held) == 0
}
// codexAsyncStash is what magpie wrote.
type codexAsyncStash struct {
Enabled bool `json:"enabled,omitempty"` // enabled = true
Percent int `json:"percent,omitempty"` // the threshold
Cap bool `json:"cap,omitempty"` // capabilities = { remote_compaction = "v2" }
Bool bool `json:"bool,omitempty"` // [features] async_compaction = true
}
// codexAsyncNow is how the config has them: the async_compaction value
// (nil, a bool, or a table) and magpie's provider table, nil when absent.
func codexAsyncNow(path string) (feature any, provider map[string]any, err error) {
raw, err := edit.Read(path)
if err != nil || raw == nil {
return nil, nil, err
}
var doc map[string]any
if err := toml.Unmarshal(raw, &doc); err != nil {
return nil, nil, err
}
features, _ := doc["features"].(map[string]any)
providers, _ := doc["model_providers"].(map[string]any)
p, _ := providers[magpieID].(map[string]any)
return features["async_compaction"], p, nil
}
// codexAsyncSync puts the setting into the Codex config at path, where
// Codex is routed through magpie or not: on is the setting, percent its
// threshold (0 leaves Codex's own). key is the stash's. tableOK: every
// Codex reading the config takes the table (codexAsyncTableOK); without
// it the bare key is written, and a table of magpie's taken out. The
// capability is written for a feature the user turned on in the config
// themselves too: on magpie's provider it would never run without it
// (#2493).
func codexAsyncSync(path, key string, routed, on bool, percent int, tableOK bool) error {
feature, provider, err := codexAsyncNow(path)
var bad *toml.DecodeError
if errors.As(err, &bad) {
return nil // one that doesn't parse is told by Check, and written to by nothing
}
if err != nil {
return err
}
var st codexAsyncStash
if raw := stashLoad()[key]; raw != "" {
json.Unmarshal([]byte(raw), &st)
}
was := st
want := routed && on
useTable := want && tableOK
if !useTable {
percent = 0
}
// the bare async_compaction = true magpie wrote, where the table is
// to go instead or nothing is: still as written, it goes; changed, it
// is the user's
if st.Bool && (!want || useTable) {
if feature == true {
if err := edit.DelTOMLKey(path, "features", "async_compaction"); err != nil {
return err
}
if feature, _, err = codexAsyncNow(path); err != nil {
return err
}
}
st.Bool = false
}
// [features.async_compaction]: written only as that table, which a
// config without the feature, or with the table, has. The bare
// async_compaction = true, or the table spelled inline or as dotted
// keys, is the user's, as written.
table, _ := feature.(map[string]any)
asTable := feature == nil
if table != nil {
t, err := edit.GetTOMLTable(path, codexAsyncTable)
if err != nil {
return err
}
asTable = t != nil
}
if asTable {
enabled, hasEnabled := table["enabled"].(bool)
if table["enabled"] != nil && !(hasEnabled && st.Enabled && enabled) {
st.Enabled = false // set, or changed since, by the user
}
switch {
case useTable && !hasEnabled && table["enabled"] == nil:
if err := edit.SetTOMLKey(path, codexAsyncTable, "enabled", true); err != nil {
return err
}
st.Enabled = true
case !useTable && st.Enabled:
// off, or a Codex here too old for the table: the table magpie
// wrote goes (a table left with nothing of the user's goes
// whole, so an old Codex loads its config again)
if err := edit.DelTOMLKey(path, codexAsyncTable, "enabled"); err != nil {
return err
}
st.Enabled = false
}
cur, hasPercent := table[codexAsyncPercent].(int64)
if table[codexAsyncPercent] != nil && !(hasPercent && int64(st.Percent) == cur) {
st.Percent = 0
}
switch {
case percent > 0 && (table[codexAsyncPercent] == nil || st.Percent != 0):
if st.Percent != percent {
if err := edit.SetTOMLKey(path, codexAsyncTable, codexAsyncPercent, percent); err != nil {
return err
}
}
st.Percent = percent
case percent == 0 && st.Percent != 0:
if err := edit.DelTOMLKey(path, codexAsyncTable, codexAsyncPercent); err != nil {
return err
}
st.Percent = 0
}
} else {
st.Enabled, st.Percent = false, 0
}
// the bare key, where a Codex here can't take the table: every Codex
// reads it, and only one new enough acts on it
if feature, _, err = codexAsyncNow(path); err != nil {
return err
}
switch {
case want && !useTable && feature == nil:
if err := edit.SetTOMLKey(path, "features", "async_compaction", true); err != nil {
return err
}
st.Bool = true
case st.Bool && feature != true:
st.Bool = false // changed since by the user
}
// capabilities on magpie's provider table, when there is one: one the
// user gave it, whatever it says, stays as it is
if feature, _, err = codexAsyncNow(path); err != nil {
return err
}
table, _ = feature.(map[string]any)
if feature == true || table["enabled"] == true {
want = routed
}
if provider == nil {
st.Cap = false
} else {
caps, has := provider["capabilities"].(map[string]any)
if has && !(st.Cap && len(caps) == 1 && caps["remote_compaction"] == "v2") {
st.Cap = false
}
switch {
case want && provider["capabilities"] == nil:
if err := edit.SetTOMLKey(path, "model_providers."+magpieID, "capabilities", edit.Inline{{Path: "remote_compaction", Value: "v2"}}); err != nil {
return err
}
st.Cap = true
case !want && st.Cap:
if err := edit.DelTOMLKey(path, "model_providers."+magpieID, "capabilities"); err != nil {
return err
}
st.Cap = false
}
}
if st != was {
if st == (codexAsyncStash{}) {
forget(key)
} else {
b, _ := json.Marshal(st)
stash(map[string]string{key: string(b)})
}
}
return nil
}
// codexProviderCaps is the capabilities magpie's provider table has, as an
// Inline to write it back with when the table is written again: one the
// user gave it, or magpie's for the background compaction, isn't lost.
func codexProviderCaps(path string) edit.Inline {
_, provider, err := codexAsyncNow(path)
if err != nil {
return nil
}
caps, _ := provider["capabilities"].(map[string]any)
var out edit.Inline
for _, k := range sortedKeys(caps) {
switch v := caps[k].(type) {
case string, bool, int64, float64:
out = append(out, edit.KV{Path: k, Value: v})
}
}
return out
}
// CodexAsyncHeldHere is the Codexes on this machine that would keep the
// background compaction's threshold out of its config (one before
// codexAsyncTableFrom, or of no known version), asked now rather than at
// the last sync, which asks only while the setting is on.
func CodexAsyncHeldHere(home string) []CodexReader {
return codexAsyncBlocked(codexReaders(here(tuneHome(home))))
}
// codexAsyncOwn names what the user has in Codex's config.toml that
// comes before magpie's background compaction threshold, as codexAsyncSync
// leaves it: the feature as a bare key of theirs (true at Codex's own 65%,
// or false), their table spelled inline or as dotted keys, enabled = false
// in it, or a threshold of their own. "" when magpie's threshold is what
// Codex would read.
func codexAsyncOwn(home string) string {
at := here(tuneHome(home))
path := filepath.Join(at.codexHome(), "config.toml")
feature, _, err := codexAsyncNow(path)
if err != nil || feature == nil {
return ""
}
var st codexAsyncStash
if raw := stashLoad()[at.key("codex.async")]; raw != "" {
json.Unmarshal([]byte(raw), &st)
}
bare := "features.async_compaction"
switch f := feature.(type) {
case bool:
if f && st.Bool {
return ""
}
return bare
case map[string]any:
if t, err := edit.GetTOMLTable(path, codexAsyncTable); err != nil || t == nil {
return bare
}
if e, has := f["enabled"]; has {
if on, _ := e.(bool); !on {
return codexAsyncTable + ".enabled"
}
}
if p, has := f[codexAsyncPercent]; has {
if n, _ := p.(int64); st.Percent == 0 || int64(st.Percent) != n {
return codexAsyncTable + "." + codexAsyncPercent
}
}
return ""
}
return bare
}