多喝热水 (Discord #help, Windows, v0.1.1384, Codex desktop with ChatGPT
sign-in): Codex wouldn't open, "config.toml:154:1: invalid type: map,
expected a boolean". That line is the [features.async_compaction] table
1978eabe5 (#2490/#2493) writes.
Codex versions, from codex-rs source and the real binaries:
- up to 0.163.0-alpha.5 (0.160.0, 0.162.1 included) FeaturesToml is a
flattened BTreeMap<String, bool>: any table under [features] fails the
whole config; an unknown bool key is only a warning.
- from 0.163.0-alpha.8, async_compaction is FeatureToml<…>, bool or
table, and model_async_compact_threshold_percent is read only from the
table (the bool gives Codex's default 65).
- the provider table's capabilities = {...} is safe: 0.161 and older
ignore unknown provider keys (schemars-only deny_unknown_fields),
0.162 parses it. 0.160.0 and 0.162.1 load a config holding it.
- ChatGPT.app 26.1013.40934 bundles Codex 0.163.0-alpha.10.2
(Contents/Resources/codex-cli/codex-package.json); the reporter's
Windows app rejects the table, so it bundles one before alpha.8.
Fix: the table is written only when every Codex found here that reads
this config (the CLI on PATH, the desktop app's bundled codex on macOS,
the Store app on Windows, the editor extension, Codex in WSL) has a
version at or after 0.163.0-alpha.8. Otherwise, or when a version can't
be read (the Windows app, the extension), magpie writes the bare
[features] async_compaction = true with no share. A table magpie wrote
before is taken back through its stash (only the keys the stash names
as magpie's; an emptied table goes), so a config already broken opens
again on the next sync. A table or bool the user wrote stays. The stash
records a bool it wrote (`bool`), and the table comes back once every
reader is new enough. Settings' state carries codexAsyncHeld, and the
Codex row says which Codex holds the share back and that it needs
0.163. The note costs app.js 390 bytes; its budget and the core's are
raised by exactly that, the agent's own row in the core.
Run:
- real binaries, sandbox CODEX_HOME/HOME, `codex features list` on the
config magpie writes (TestCodexAsyncConfigLoadsInRealCodex,
MAGPIE_TEST_CODEX_BINS): 0.162.1, 0.163.0-alpha.8 and 0.164.0 load it;
alpha.8 and 0.164.0 report async_compaction true. By hand before the
fix: 0.160.0 and 0.162.1 exit 1 on the table ("invalid type: map,
expected a boolean"), load the bool and nothing; alpha.8 and 0.164.0
load all three.
- broken by hand (table always allowed): the real-binary test is red
for 0.162.1 with Codex's own error, OldCodexGetsTheBareKey red in all
four cases, TableTakenBackForOldCodex red ("magpie's table not
mended"). Green again with the fix.
- go test -tags nogui ./internal/agent ./internal/gui ./internal/edit
./internal/settings: ok (gui after the budget, re-run alone: ok).
- GUI (node --test, chromium + webkit): codex-async-compact,
agent-prefs, settings-general, segs-glide, i18n-keys: 100 pass, 0 fail.
- gofmt -l clean; go vet ./..., GOOS=linux go vet -tags nogui ./...,
GOOS=windows go vet ./...; go build for darwin, linux (nogui),
windows: ok.
Not tried: the Windows box (Store-app detection, reading its bundled
codex's version, a non-Store install), a WSL distro, a real Codex run
past the share, the whole GUI suite. The full go test ./... is queued
behind other sessions' suites (one per machine) and wasn't waited for.
353 lines
12 KiB
Go
353 lines
12 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), 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"})
|
|
}
|
|
}
|
|
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
|
|
}
|