Files
magpie/internal/agent/codex_hand_edit_test.go
T
yetone e7f21dba5e 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).
2026-10-27 17:02:55 +08:00

69 lines
2.4 KiB
Go

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, `\`, `\\`) + "\"" }