Files
magpie/internal/agent/codex_reconnect_joined_test.go
T
yetone fae438b4a5 agent: Codex switched on again is joined, and its own models say where a pick goes
NancyTom18 on X: Codex on a routing group, Disconnect and restore, switched
on again, then an OpenAI model picked: every OpenAI row said 直连. After a
routing group again they said 经 magpie.

Reproduced in a sandbox HOME (ChatGPT sign-in, a group): reconnect put the
group back by Apply alone, so Codex was beside its sign-in and not joined
(codex.joined went with the Disconnect). Picking one of its own models then
took magpie out (set → unroute, #940): config.toml had no openai_base_url
and no model_provider, so Codex went straight to OpenAI and the 直连 tags
were true. Back on the group, the rows said via magpie from routed(), while
a pick would again go straight to OpenAI, with no ask: that label was false.

- reconnect calls Join first where the agent has one, so a Codex switched
  on again comes back as the switch first connects it: joined, its own
  models staying through magpie.
- modelOptions: beside its sign-in, not joined (with ChatGPT), and failover
  off, Codex's own models say Direct "OpenAI", so the picker tags them
  直连 and asks before a pick takes Codex off magpie.

Tests: TestCodexReconnectStaysJoined (red on the old applied.go: "connected
again, not joined"), TestCodexBesideOwnModelsSayDirect (red on the old
codex.go: "gpt-5.5 says via"). Each fix reverted by hand, its test went red.

Run on mote at this tree: go test -timeout 45m ./... all ok except
internal/gui TestMainWindowCorners/macOS_26_SDK, mote's macOS 27 red filed
as #3949 (window chrome, untouched here). go vet ./..., GOOS=linux go vet
-tags nogui ./..., GOOS=windows go vet ./..., go build ./..., gofmt -l
clean. GUI files unchanged; agent-leave-magpie and agent-path-same pass in
Chromium and WebKit (4/4 each).

Not tried: a real Codex binary with a real ChatGPT sign-in sending a turn;
the route is read from config.toml as Codex's own provider rules read it.
A Codex already beside a group from before this release stays so: its
OpenAI rows now say 直连 and ask; switching it off and on joins it.
2026-10-29 07:32:05 +08:00

125 lines
4.2 KiB
Go

package agent
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// nancyCodex is NancyTom18's Codex (on X): signed in with ChatGPT, its own
// models listed, and a routing group named for one of them.
func nancyCodex(t *testing.T) (*Agent, func() string) {
t.Helper()
home, read := codexHome(t, chatGPTAuth, "model = \"gpt-5.5\"\n")
os.WriteFile(filepath.Join(home, ".codex", "models_cache.json"),
[]byte(`{"models":[{"slug":"gpt-5.5","visibility":"list","priority":1},{"slug":"gpt-5.6-luna","visibility":"list","priority":2}]}`), 0o644)
if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"gpt-5.6-luna"}}); err != nil {
t.Fatal(err)
}
if err := provider.SaveGroup(provider.Group{ID: "luna", Name: "gpt-5.6-luna", Members: []string{"relay/gpt-5.6-luna"}}); err != nil {
t.Fatal(err)
}
return codex(home), read
}
// ownPath is how the model picker tags each of Codex's own models: "via"
// (through magpie) or "direct".
func ownPath(t *testing.T, cx *Agent) map[string]string {
t.Helper()
out := map[string]string{}
for _, o := range cx.Fields[0].Options(cx.Values()) {
if o.Group != "OpenAI" || o.Ref != "" {
continue
}
switch {
case o.Via && o.Direct == "":
out[o.Value] = "via"
case !o.Via && o.Direct != "":
out[o.Value] = "direct"
default:
out[o.Value] = "both or neither"
}
}
if len(out) == 0 {
t.Fatal("no own models in the picker")
}
return out
}
// Connected by the switch, on a routing group, disconnected and restored,
// then switched on again: Codex comes back as the switch first connected
// it, joined, so one of its own models picked on the Agents page stays
// through magpie, as the picker says. Put back on the group alone, it was
// beside its sign-in and not joined: the OpenAI model picked took Codex
// off magpie and every row said direct (NancyTom18's first screenshot).
func TestCodexReconnectStaysJoined(t *testing.T) {
cx, read := nancyCodex(t)
if c, err := cx.ConnectHow(); err != nil || c.How != "joined" {
t.Fatalf("connect: %+v %v", c, err)
}
if err := cx.Apply("model", "group/luna"); err != nil {
t.Fatal(err)
}
if err := cx.Disconnect(); err != nil {
t.Fatal(err)
}
if cfg := read(); strings.Contains(cfg, "openai_base_url") {
t.Fatalf("disconnected:\n%s", cfg)
}
if c, err := cx.ConnectHow(); err != nil || c.How != "again" {
t.Fatalf("connect again: %+v %v", c, err)
}
if cfg := read(); !strings.Contains(cfg, `model = "group/luna"`) || !strings.Contains(cfg, "openai_base_url") {
t.Fatalf("connected again, not on the group:\n%s", cfg)
}
if !cx.Joined() {
t.Fatal("connected again, not joined")
}
for m, p := range ownPath(t, cx) {
if p != "via" {
t.Errorf("connected again: %s says %s", m, p)
}
}
if err := cx.Apply("model", "gpt-5.6-luna"); err != nil {
t.Fatal(err)
}
if cfg := read(); !strings.Contains(cfg, "openai_base_url") || !strings.Contains(cfg, `model = "gpt-5.6-luna"`) || !cx.Wired() {
t.Fatalf("an own model picked took Codex off magpie, wired %v:\n%s", cx.Wired(), cfg)
}
for m, p := range ownPath(t, cx) {
if p != "via" {
t.Errorf("on its own model: %s says %s", m, p)
}
}
}
// Beside its sign-in on a routing group and not joined (a magpie model
// picked while Codex wasn't connected, or a Codex connected again by an
// older magpie), one of its own picked here takes magpie out: each says
// direct, so the picker asks before it does. It said via magpie, and the
// pick went straight to OpenAI (NancyTom18's second screenshot).
func TestCodexBesideOwnModelsSayDirect(t *testing.T) {
cx, read := nancyCodex(t)
if err := cx.Apply("model", "group/luna"); err != nil {
t.Fatal(err)
}
if !cx.Beside() || cx.Joined() || !cx.Wired() {
t.Fatalf("not beside alone: beside %v joined %v wired %v\n%s", cx.Beside(), cx.Joined(), cx.Wired(), read())
}
for m, p := range ownPath(t, cx) {
if p != "direct" {
t.Errorf("beside: %s says %s", m, p)
}
}
// and it is so
if err := cx.Apply("model", "gpt-5.6-luna"); err != nil {
t.Fatal(err)
}
if cfg := read(); strings.Contains(cfg, "openai_base_url") || cx.Wired() {
t.Fatalf("picked one of its own, still on magpie:\n%s", cfg)
}
}