diff --git a/internal/agent/codex.go b/internal/agent/codex.go index 2b8014b4c..4cf68af07 100644 --- a/internal/agent/codex.go +++ b/internal/agent/codex.go @@ -1269,16 +1269,20 @@ func codexIn(at place) *Agent { return nil } // giveTables puts the base URLs back, on the tables still pointed at - // magpie; one changed since is left as it is now + // magpie; one changed since is left as it is now. Each table is read + // as it is, not through ccSwitchTables: that counts a table of the + // user's own only while a thread names it, and bringOver had moved + // its threads onto magpie by then, so the Disconnect forgot the + // relay's URL and left the user's provider pointed at magpie + // (darwish4021 on Discord) giveTables := func() error { if err := giveMirrors(); err != nil { return err } var was map[string]string json.Unmarshal([]byte(stashShared()[at.key("codex.tables")]), &was) - now := ccSwitchTables() for id, u := range was { - if now[id] != at.v1() { + if t, err := edit.GetTOMLTable(path, "model_providers."+id); err != nil || t["base_url"] != at.v1() { continue } if err := edit.SetTOMLKey(path, "model_providers."+id, "base_url", u); err != nil { diff --git a/internal/agent/codex_off_keeps_test.go b/internal/agent/codex_off_keeps_test.go new file mode 100644 index 000000000..d0eeaa48e --- /dev/null +++ b/internal/agent/codex_off_keeps_test.go @@ -0,0 +1,161 @@ +package agent + +import ( + "os" + "path/filepath" + "strings" + "testing" + + "github.com/pelletier/go-toml/v2" +) + +// a config.toml as a Codex user of months has one: comments, a profile, +// MCP servers with their env tables, projects Codex trusts, notify, tui, +// features, sandbox settings, skills and a provider of their own. Codex +// 0.164 loads it as it is (no top-level profile, which it refuses), and +// what the Disconnect leaves of it too. +const codexOffUserConfig = `# my codex +model = "gpt-5.5" +model_reasoning_effort = "high" +approval_policy = "on-request" +sandbox_mode = "workspace-write" +notify = ["bash", "-lc", "afplay /System/Library/Sounds/Glass.aiff"] +personality = "pragmatic" + +[features] +web_search_request = true + +[tui] +notifications = true + +[sandbox_workspace_write] +network_access = true + +[shell_environment_policy] +inherit = "core" +set = { PATH = "/usr/bin:/bin" } + +[profiles.work] +model = "gpt-5.4" +approval_policy = "never" + +[model_providers.myrelay] +name = "My relay" +base_url = "https://relay.example.com/v1" + +[mcp_servers.github] +command = "npx" +args = ["-y", "@modelcontextprotocol/server-github"] + +[mcp_servers.github.env] +GITHUB_TOKEN = "ghp_x" + +[mcp_servers.figma] +url = "https://mcp.figma.com/mcp" + +[projects."/Users/me/work/api"] +trust_level = "trusted" + +[projects."/Users/me/work/web"] +trust_level = "trusted" + +[projects.'C:\Users\me\work'] +trust_level = "untrusted" + +[[skills.config]] +path = "/Users/me/.codex/skills/x" +enabled = false +` + +func codexOffUserLines() []string { + var out []string + for _, l := range strings.Split(codexOffUserConfig, "\n") { + if l = strings.TrimSpace(l); l != "" && !strings.HasPrefix(l, "model ") && !strings.HasPrefix(l, "model_reasoning_effort") { + out = append(out, l) + } + } + return out +} + +// darwish4021 on Discord: switching Codex off on the Agents page lost +// Codex's configuration. Switched on and off again, every line of the +// user's config.toml is there, an edit made while on magpie too, the file +// still parses, nothing of magpie's but its provider table (kept for the +// threads started on it) is left, and a chat is back on the provider it +// was started on. The relay cases were red: bringOver had moved the +// relay's threads onto magpie, so giveTables no longer counted the +// relay's table and forgot its base_url, leaving the user's provider +// pointed at magpie's gateway. +func TestCodexOffKeepsUsersConfig(t *testing.T) { + for _, tc := range []struct{ name, auth, extra string }{ + {"chatgpt", `{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`, ""}, + {"api", `{"OPENAI_API_KEY":"sk-x"}`, ""}, + {"none", "", ""}, + {"relay", `{"OPENAI_API_KEY":"sk-x"}`, "model_provider = \"myrelay\"\n"}, + {"relay-chatgpt", `{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`, "model_provider = \"myrelay\"\n"}, + } { + for _, edited := range []bool{false, true} { + t.Run(tc.name+map[bool]string{true: "-edited"}[edited], func(t *testing.T) { + home, read := codexHome(t, tc.auth, tc.extra+codexOffUserConfig) + dir := filepath.Join(home, ".codex") + own := "openai" + if tc.extra != "" { + own = "myrelay" + } + const thread = "019a0000-0000-7000-8000-00000000c0ff" + codexThreadOn(t, dir, thread, own) + cx := codex(home) + if _, err := cx.ConnectHow(); err != nil { + t.Fatal(err) + } + SyncCatalog() + on := read() + if edited { + on += "\n[projects.\"/Users/me/new\"]\ntrust_level = \"trusted\"\n\n[mcp_servers.added]\ncommand = \"added\"\n" + os.WriteFile(filepath.Join(home, ".codex", "config.toml"), []byte(on), 0o644) + } + if err := codex(home).Disconnect(); err != nil { + t.Fatal(err) + } + off := read() + // the user's chats are listed where Codex is again + if p := codexRowOn(t, dir, thread); p != own { + t.Errorf("thread left on %q, want %q", p, own) + } + want := codexOffUserLines() + if edited { + want = append(want, `[projects."/Users/me/new"]`, `[mcp_servers.added]`, `command = "added"`) + } + for _, l := range want { + if !strings.Contains(off, l) { + t.Errorf("lost %q", l) + } + } + if tc.extra != "" && !strings.Contains(off, strings.TrimSpace(tc.extra)) { + t.Errorf("provider not back") + } + if !strings.Contains(off, `model = "gpt-5.5"`) { + t.Errorf("model not back") + } + var parsed map[string]any + if err := toml.Unmarshal([]byte(off), &parsed); err != nil { + t.Errorf("left unloadable: %v", err) + } + // the provider table stays for the threads started on it; + // nothing else of magpie's does + for k, v := range parsed { + if s, ok := v.(string); ok && strings.Contains(s, "magpie") { + t.Errorf("magpie's %s left: %q", k, s) + } + } + if d := os.Getenv("CODEX_OFF_DUMP"); d != "" { + os.MkdirAll(filepath.Join(d, t.Name()), 0o755) + os.WriteFile(filepath.Join(d, t.Name(), "config.toml"), []byte(off), 0o644) + } + if t.Failed() { + t.Logf("connected:\n%s\n\noff:\n%s", on, off) + } + }) + } + } +} diff --git a/internal/library/codex_off_app_config_test.go b/internal/library/codex_off_app_config_test.go new file mode 100644 index 000000000..a515bdfac --- /dev/null +++ b/internal/library/codex_off_app_config_test.go @@ -0,0 +1,100 @@ +package library + +import ( + "path/filepath" + "strings" + "testing" + + "github.com/yetone/magpie/internal/agent" +) + +// what the Codex app (ChatGPT.app 26.1021) writes into config.toml for its +// browser, Chrome and computer use: plugins, marketplaces, the node_repl +// server with its env table and env_vars, a server it keeps off +const codexAppOwnConfig = `model = "gpt-5.5" + +[marketplaces.openai-bundled] +source_type = "local" +source = "/Users/me/.codex/.tmp/bundled-marketplaces/openai-bundled" + +[plugins."chrome@openai-bundled"] +enabled = true + +[plugins."browser@openai-bundled"] +enabled = true + +[features] +hooks = true + +[mcp_servers.node_repl] +args = [] +command = "/Applications/ChatGPT.app/Contents/Resources/cua_node/bin/node_repl" +env_vars = ["BROWSER_USE_ACCESS_EXECUTOR_ID", "BROWSER_USE_ACCESS_PIPE_PATH"] +startup_timeout_sec = 120 + +[mcp_servers.node_repl.env] +NODE_REPL_NODE_PATH = "/Applications/ChatGPT.app/Contents/Resources/cua_node/bin/node" +BROWSER_USE_AVAILABLE_BACKENDS = "chrome,iab,mcpapps" +NODE_REPL_TRUSTED_SERVICES = '{"browser":"/Users/me/.codex/plugins/cache/openai-bundled/browser/26.1021.31345/scripts/browser-service.mjs"}' + +[mcp_servers.computer-use] +command = "./Codex Computer Use.app/Contents/SharedSupport/SkyComputerUseClient.app/Contents/MacOS/SkyComputerUseClient" +args = ["mcp"] +cwd = "." +enabled = false + +[shell_environment_policy.set] +NODE_REPL_TRUSTED_BROWSER_CLIENT_SHA256S = "9230e2bd" + +[desktop] +open_links_in = "app" +` + +// darwish4021 on Discord: Codex switched off on magpie, its app's browser +// configuration was gone. The library gives Codex its servers one way on +// magpie and another off it (agent.Rewired); the app's own servers and +// everything else it wrote stay through both, imported into the library +// or not. +func TestCodexOffKeepsAppsOwnConfig(t *testing.T) { + for _, imported := range []bool{false, true} { + t.Run(map[bool]string{false: "own", true: "imported"}[imported], func(t *testing.T) { + h := sandbox(t) + resetRuns(t) + runOn = true + wired := true + was := runWired + runWired = func(a *agent.Agent) bool { return wired } + t.Cleanup(func() { runOn, runWired = false, was }) + path := filepath.Join(h, ".codex", "config.toml") + write(t, path, codexAppOwnConfig) + if _, err := SaveServer("", Server{Name: "mem", Transport: "stdio", Command: "node", Args: []string{"server.js"}, Agents: []string{"codex"}}); err != nil { + t.Fatal(err) + } + if imported { + if _, err := ImportServer("node_repl"); err != nil { + t.Fatal(err) + } + } + if _, err := Sync(); err != nil { + t.Fatal(err) + } + on := read(t, path) + if !strings.Contains(on, RunPath+"mem") { + t.Fatalf("on magpie, not given through the gateway as it should be:\n%s", on) + } + wired = false + if _, err := Sync(); err != nil { + t.Fatal(err) + } + off := read(t, path) + for _, l := range strings.Split(codexAppOwnConfig, "\n") { + if l = strings.TrimSpace(l); l != "" && !strings.Contains(off, l) { + t.Errorf("lost %q", l) + } + } + if t.Failed() { + t.Logf("on magpie:\n%s\n\noff:\n%s", on, off) + } + }) + } +}