codex: Disconnect keeps an effort or model list the user set by hand after Connect

@yhwangl on X asked that putting an agent's config back never write
over what the user changed by hand after magpie took it over: only the
keys magpie changed go back, and a key magpie wrote that the user has
changed since keeps the user's value.

Codex's Disconnect put back the pre-connect model_reasoning_effort and
model_catalog_json whatever they were by then. An effort the user set
by hand to high after connecting went back to the low it was before,
and a model list of their own put in by hand was deleted.

Now:
- the effort goes back only while it is still one magpie set: the one
  settle moved it to (stashed as codex.effort_set), the one picked on
  the Agents page (the applied record), or none;
- dropProvider takes model_catalog_json out only when it is magpie's
  list (or empty), and unroute puts the old list back only where the
  key is empty;
- model_provider is put back only where the key is empty.
Other agents' fields already clear only a value still magpie's, and
put the pre-connect backup only into a field left empty.

Run, at this commit's tree (HEAD df8374921 plus this change), sandbox
HOME, private GOCACHE:
- gofmt -l internal/agent: clean; go vet ./internal/agent: ok
- go test -tags nogui -count=1 -run Codex ./internal/agent: ok (121.7s)
- broken by hand (the effort and catalog checks taken out of unroute):
  TestCodexDisconnectKeepsHandEdits went red, green again with them.
Not run: the full suite (runs with the uninstall change).
This commit is contained in:
yetone
2026-10-27 17:02:55 +08:00
parent 68bbcc01c5
commit e7f21dba5e
2 changed files with 100 additions and 7 deletions
+32 -7
View File
@@ -426,9 +426,12 @@ func codexIn(at place) *Agent {
ms := models()
model, effort := get("model"), get("model_reasoning_effort")
if e := catalog.Efforts(ms, model); len(e) > 0 && effort != "" && !contains(e, effort) {
if err := edit.SetTOMLTop(path, edit.KV{Path: "model_reasoning_effort", Value: codexcat.ModelDefault(e, effort)}); err != nil {
to := codexcat.ModelDefault(e, effort)
if err := edit.SetTOMLTop(path, edit.KV{Path: "model_reasoning_effort", Value: to}); err != nil {
return err
}
// magpie's own change, which Disconnect undoes while it stands
stash(map[string]string{at.key("codex.effort_set"): to})
if err := relist(); err != nil {
return err
}
@@ -524,7 +527,12 @@ func codexIn(at place) *Agent {
if !asProvider() {
return nil
}
if err := edit.DelTOMLTop(path, "model_provider", "model_catalog_json"); err != nil {
// a model list of the user's own, put in by hand since, stays
keys := []string{"model_provider"}
if c := get("model_catalog_json"); c == "" || ownCatalog(c) {
keys = append(keys, "model_catalog_json")
}
if err := edit.DelTOMLTop(path, keys...); err != nil {
return err
}
dropList()
@@ -1078,16 +1086,33 @@ func codexIn(at place) *Agent {
return "", err
}
was := unstash(at.key("codex.model"))
// what was there before magpie is put back only where magpie's
// change still stands: a key the user set by hand since keeps
// theirs (yhwangl on X)
var back []edit.KV
if p := unstash(at.key("codex.provider")); p != "" && p != magpieID {
if p := unstash(at.key("codex.provider")); p != "" && p != magpieID && get("model_provider") == "" {
back = append(back, edit.KV{Path: "model_provider", Value: p})
}
// the catalog of the user's own put back while its file is there:
// one removed since would keep Codex from loading its config
if c := unstash(at.key("codex.catalog")); c != "" && !ownCatalog(c) && !theirListGone(c) {
if c := unstash(at.key("codex.catalog")); c != "" && !ownCatalog(c) && !theirListGone(c) && get("model_catalog_json") == "" {
back = append(back, edit.KV{Path: "model_catalog_json", Value: c})
}
if e := unstash(at.key("codex.effort")); e != "" {
// the effort, where it is still one magpie set: picked on the
// Agents page, or moved to one its model has. One taken out
// reads the same however it was, and comes back.
id := at.id
if id == "" {
id = "codex"
}
ours := map[string]bool{"": true}
if set := unstash(at.key("codex.effort_set")); set != "" {
ours[set] = true
}
if r := appliedOf(id).Fields["effort"]; r != "" {
ours[r] = true
}
if e := unstash(at.key("codex.effort")); e != "" && ours[get("model_reasoning_effort")] {
back = append(back, edit.KV{Path: "model_reasoning_effort", Value: e})
}
if len(back) > 0 {
@@ -1116,7 +1141,7 @@ func codexIn(at place) *Agent {
return err
}
dropList()
forget(at.key("codex.model"), at.key("codex.effort"), at.key("codex.provider"), at.key("codex.catalog"), at.key("codex.out"), at.key("codex.joined"), at.key("codex.beside"))
forget(at.key("codex.model"), at.key("codex.effort"), at.key("codex.effort_set"), at.key("codex.provider"), at.key("codex.catalog"), at.key("codex.out"), at.key("codex.joined"), at.key("codex.beside"))
return nil
}
// magpie API: magpie stays Codex's provider whichever model is the
@@ -1255,7 +1280,7 @@ func codexIn(at place) *Agent {
return err
}
dropList()
forget(at.key("codex.model"), at.key("codex.effort"), at.key("codex.provider"), at.key("codex.catalog"))
forget(at.key("codex.model"), at.key("codex.effort"), at.key("codex.effort_set"), at.key("codex.provider"), at.key("codex.catalog"))
// its threads go on without magpie: the compactions magpie made
// in them written as Codex's own (a distro's Codex locks its
// files where this machine's locks can't reach)
+68
View File
@@ -0,0 +1,68 @@
package agent
import (
"os"
"path/filepath"
"strings"
"testing"
)
// Disconnect puts back only what magpie changed: a key the user edited by
// hand while Codex was on magpie keeps the user's value, though magpie had
// saved what was there before it connected (yhwangl on X).
func TestCodexDisconnectKeepsHandEdits(t *testing.T) {
home, read := codexHome(t, "", "model = \"gpt-5.5\"\nmodel_reasoning_effort = \"low\"\nmodel_catalog_json = \"/x/old-list.json\"\n")
theirs := filepath.Join(home, "my-list.json")
os.WriteFile(theirs, []byte(`{"models":[]}`), 0o644)
cx := codex(home)
if err := cx.Fields[0].Set("fake/m1"); err != nil {
t.Fatal(err)
}
cfg := read()
if !strings.Contains(cfg, "model_provider = \"magpie\"") {
t.Fatalf("not magpie's provider:\n%s", cfg)
}
// by hand, with magpie connected: an effort and a model list of their own
cfg = strings.Replace(cfg, "model_reasoning_effort = \"low\"", "model_reasoning_effort = \"high\"", 1)
lines := strings.Split(cfg, "\n")
for i, l := range lines {
if strings.HasPrefix(l, "model_catalog_json") {
lines[i] = "model_catalog_json = " + quoteTOML(theirs)
}
}
os.WriteFile(filepath.Join(home, ".codex", "config.toml"), []byte(strings.Join(lines, "\n")), 0o644)
if err := codex(home).Disconnect(); err != nil {
t.Fatal(err)
}
cfg = read()
for _, want := range []string{"model_reasoning_effort = \"high\"\n", "model_catalog_json = " + quoteTOML(theirs) + "\n", "model = \"gpt-5.5\"\n"} {
if !strings.Contains(cfg, want) {
t.Errorf("missing %q after Disconnect:\n%s", want, cfg)
}
}
if strings.Contains(cfg, "model_provider = \"magpie\"") {
t.Errorf("still on magpie:\n%s", cfg)
}
}
// What magpie changed and the user didn't is put back as it was.
func TestCodexDisconnectPutsBackWhatMagpieChanged(t *testing.T) {
home, read := codexHome(t, "", "model = \"gpt-5.5\"\nmodel_reasoning_effort = \"low\"\n")
if err := codex(home).Fields[0].Set("fake/m1"); err != nil {
t.Fatal(err)
}
if err := codex(home).Disconnect(); err != nil {
t.Fatal(err)
}
cfg := read()
for _, want := range []string{"model_reasoning_effort = \"low\"\n", "model = \"gpt-5.5\"\n"} {
if !strings.Contains(cfg, want) {
t.Errorf("missing %q after Disconnect:\n%s", want, cfg)
}
}
if strings.Contains(cfg, "model_catalog_json") || strings.Contains(cfg, "model_provider =") {
t.Errorf("magpie's keys left:\n%s", cfg)
}
}
func quoteTOML(s string) string { return "\"" + strings.ReplaceAll(s, `\`, `\\`) + "\"" }