Codex 0.163 can compact a long conversation in the background while a
turn goes on ([features.async_compaction]). A new row under Long
conversations turns it on, with the share of the window it starts at
(empty: Codex's own 65%). magpie writes it into each Codex config it
routes (codex_async.go):
- [features.async_compaction] enabled = true and
model_async_compact_threshold_percent. Codex reads the threshold only
in that table; one at the top level is ignored (tried).
- capabilities = { remote_compaction = "v2" } on [model_providers.magpie].
Codex starts a background compaction only on a provider with v2, which
it gives OpenAI and Azure alone. Tried with Codex 0.163 against a
sandbox gateway and a fake upstream reporting 70% use: without the
capability, no compaction in a whole turn; with it, compaction_trigger
requests ran beside the turn's steps and the next step's input held
the summary. The capability is written for a feature the user turned
on by hand too (#2493 asked whether magpie supports it: it didn't).
putProvider carries the table's capabilities over when a model switch
writes it again.
- What magpie wrote is in the stash (codex.async). A value the user set,
or changed since, is never written over and stays when the switch is
turned off; a bare async_compaction = true, an inline or a dotted
table is the user's as written. A config that doesn't parse is not
written to.
Not tried: Codex signed in with ChatGPT (openai_base_url to magpie on
the built-in provider, v2 by default), only the custom provider path.
Tests: TestCodexAsyncCompactionWritten, ...KeepsTheUsers and
...KeepsUsersCapability (internal/agent), each red with the matching
part removed by hand (no writing; no capability carry-over; no
ownership check on enabled). codex-async-compact.test.cjs: Chromium and
WebKit, en/zh/zh-TW/ja/de, red with the 1-100 check removed;
compact-at, i18n-keys, settings-general, settings-search pass.
TestSettingsSaveKeepsWhatItDoesNotSend holds the two new fields.
gofmt clean; go vet -tags nogui on darwin, linux, windows.
Full suite: go test -tags nogui -timeout 45m ./... (pipefail, suite-lock) at the four #2494/#2504/#2505/#2490 commits on origin/main a751f5282: 65 packages ok, among them internal/agent (2124s), internal/gateway (1845s) and internal/sessions (148s). internal/gui was red on TestSetAnswersWhileDevinAndProvidersHang alone (the state after a set took 11.5s at load ~250; #1375); internal/gui run again alone: ok (206s).
191 lines
6.0 KiB
Go
191 lines
6.0 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.
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
|
|
"github.com/pelletier/go-toml/v2"
|
|
|
|
"github.com/yetone/magpie/internal/edit"
|
|
)
|
|
|
|
const (
|
|
codexAsyncTable = "features.async_compaction"
|
|
codexAsyncPercent = "model_async_compact_threshold_percent"
|
|
)
|
|
|
|
// 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" }
|
|
}
|
|
|
|
// 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. 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) 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
|
|
if !want {
|
|
percent = 0
|
|
}
|
|
|
|
// [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 want && !hasEnabled && table["enabled"] == nil:
|
|
if err := edit.SetTOMLKey(path, codexAsyncTable, "enabled", true); err != nil {
|
|
return err
|
|
}
|
|
st.Enabled = true
|
|
case !want && st.Enabled:
|
|
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
|
|
}
|
|
|
|
// 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
|
|
}
|