In the GUI, an agent magpie is in has "Disconnect from magpie" (断开 magpie(还原配置)) in its row's menu (its logo, or a right-click; beside Apply again), with a tooltip saying what it does, and in its model picker right under Default, so the two aren't taken for each other; the logo's title says the menu has it. It asks first in the app's own dialog (askDisconnect, as askReset and askForgetSaved do; no native confirm), and the row is then drawn from the answer: the model the agent had, no longer wired, and the status line says so. Every new string has its Chinese. The drag grip stays hover-only; no click moves the page. What it leaves: Codex keeps magpie's [model_providers.magpie] table, as its own-model pick does, for threads started on it; with several ChatGPT accounts in magpie, Codex failover may write openai_base_url again on a later sync. A relay's own ANTHROPIC_MODEL in Claude Code's env was never stashed, so it doesn't come back; modelSettings effort entries for magpie models may stay in settings.json. Tests: TestCodexDisconnect (own provider, own provider signed in, nothing set: the top keys as before, the user's provider table untouched, no subagent model, no catalog file, no stash, not wired, no drift), TestClaudeDisconnect (own relay and nothing set: settings.json as before), TestGeminiDisconnect, TestDisconnectUnwiredLeavesConfig, TestAgentDisconnectAPI; internal/gui/tests/agent-disconnect.test.cjs (WebKit, English and Chinese: only a wired agent has it in the menu and the picker, the in-app dialog, Cancel posts nothing, Disconnect posts and the row shows the old model, no native dialog, the page doesn't move). click-scroll, agent-grip, agent-layout, agent-models, agent-model-groups, model-pick, favorite-star, claude-picker-fold and panel-effort pass under WebKit; menu-scroll fails on /api/plugins/updates unmocked, as it does on main. go vet ./internal/..., go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui -count=1 ./... under a temp HOME pass. Not run against a live agent config.
204 lines
6.7 KiB
Go
204 lines
6.7 KiB
Go
package agent
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/edit"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// Disconnect leaves Codex as it was before magpie was wired in (Fate on
|
|
// Discord asked for one action that takes out what magpie wrote): the
|
|
// provider, model and effort the user had come back, and magpie's base URL,
|
|
// catalog, subagent model and the effort it set go. Picking the agent's
|
|
// default instead leaves Codex as installed, the user's own provider gone.
|
|
func TestCodexDisconnect(t *testing.T) {
|
|
const own = "model_provider = \"mine\"\nmodel = \"gpt-5.4\"\nmodel_reasoning_effort = \"high\"\n\n" +
|
|
"[model_providers.mine]\nname = \"mine\"\nbase_url = \"https://mine.example/v1\"\n"
|
|
for _, tc := range []struct{ name, config, auth string }{
|
|
{"own provider", own, ""},
|
|
{"own provider, signed in", own, `{"tokens":{"access_token":"x","id_token":"x.e30.x"}}`},
|
|
{"nothing set", "", ""},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
home, read := codexHome(t, tc.auth, tc.config)
|
|
path := filepath.Join(home, ".codex", "config.toml")
|
|
top := func() map[string]string {
|
|
m := map[string]string{}
|
|
for _, k := range []string{"model", "model_provider", "model_reasoning_effort", "openai_base_url", "model_catalog_json"} {
|
|
if v, _ := edit.GetTOMLTop(path, k); v != "" {
|
|
m[k] = v
|
|
}
|
|
}
|
|
return m
|
|
}
|
|
was := top()
|
|
cx := codex(home)
|
|
if cx.Wired() {
|
|
t.Fatal("wired before magpie set anything")
|
|
}
|
|
if err := cx.Apply("model", "fake/m1"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := cx.Apply("effort", "low"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := cx.Apply("subagent", "fake/m1"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !cx.Wired() {
|
|
t.Fatalf("not wired on a magpie model:\n%s", read())
|
|
}
|
|
if err := cx.Disconnect(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := top(); !reflect.DeepEqual(got, was) {
|
|
t.Fatalf("after disconnect %v, want %v:\n%s", got, was, read())
|
|
}
|
|
if agents, _ := edit.GetTOMLTable(path, "agents"); agents["default_subagent_model"] != "" {
|
|
t.Fatalf("subagent model left:\n%s", read())
|
|
}
|
|
if mine, _ := edit.GetTOMLTable(path, "model_providers.mine"); tc.config != "" && mine["base_url"] != "https://mine.example/v1" {
|
|
t.Fatalf("own provider table changed:\n%s", read())
|
|
}
|
|
if _, err := os.Stat(filepath.Join(home, ".codex", "magpie-models.json")); err == nil {
|
|
t.Error("magpie's catalog left")
|
|
}
|
|
if cx.Wired() || cx.Drift() != nil {
|
|
t.Fatalf("still wired (drift %+v):\n%s", cx.Drift(), read())
|
|
}
|
|
if _, ok := appliedLoad()["codex"]; ok {
|
|
t.Error("magpie still remembers setting Codex")
|
|
}
|
|
for _, k := range []string{"codex.model", "codex.effort", "codex.provider", "codex.catalog"} {
|
|
if v, ok := stashLoad()[here(home).key(k)]; ok {
|
|
t.Errorf("stash keeps %s = %q", k, v)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// Disconnect leaves Claude Code's settings.json as it was before magpie
|
|
// was wired in: the relay's endpoint and token and the model the user had
|
|
// come back, and every env key, tier and capability magpie wrote goes.
|
|
func TestClaudeDisconnect(t *testing.T) {
|
|
for name, before := range map[string]string{
|
|
"own relay": `{"theme":"dark","model":"opus","env":{"ANTHROPIC_BASE_URL":"https://relay.example","ANTHROPIC_AUTH_TOKEN":"sk-relay","DISABLE_TELEMETRY":"1"}}`,
|
|
"nothing set": `{"theme":"dark"}`,
|
|
} {
|
|
t.Run(name, func(t *testing.T) {
|
|
home := t.TempDir()
|
|
t.Setenv("HOME", home)
|
|
t.Setenv("USERPROFILE", home)
|
|
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
|
|
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
|
|
if err := provider.Save(provider.Provider{ID: "deepseek", Name: "DeepSeek", Chat: "https://api.deepseek.com/v1", Key: "k", Models: []string{"pro", "flash"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
path := filepath.Join(home, ".claude", "settings.json")
|
|
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(path, []byte(before), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
parse := func() map[string]any {
|
|
b, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var m map[string]any
|
|
if err := json.Unmarshal(b, &m); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// an object magpie emptied out is the same as none
|
|
for k, v := range m {
|
|
if o, ok := v.(map[string]any); ok && len(o) == 0 {
|
|
delete(m, k)
|
|
}
|
|
}
|
|
return m
|
|
}
|
|
want := parse()
|
|
a := claude(home)
|
|
if a.Wired() {
|
|
t.Fatal("wired before magpie set anything")
|
|
}
|
|
if err := a.Apply("model", "deepseek/pro"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := a.Apply("haiku", "deepseek/flash"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := a.Apply("subagent", "deepseek/flash"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !a.Wired() {
|
|
t.Fatal("not wired on a magpie model")
|
|
}
|
|
if err := a.Disconnect(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := parse(); !reflect.DeepEqual(got, want) {
|
|
b, _ := os.ReadFile(path)
|
|
t.Fatalf("after disconnect:\n%s\nwant %v", b, want)
|
|
}
|
|
if a.Wired() {
|
|
t.Fatal("still wired")
|
|
}
|
|
if _, ok := appliedLoad()["claude"]; ok {
|
|
t.Error("magpie still remembers setting Claude Code")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// Gemini CLI's model default forgets the sign-in it had before magpie:
|
|
// Disconnect brings back the sign-in, the model and the user's own key.
|
|
func TestGeminiDisconnect(t *testing.T) {
|
|
home, _ := codexHome(t, "", "")
|
|
dir := filepath.Join(home, ".gemini")
|
|
os.MkdirAll(dir, 0o755)
|
|
settings := `{"security":{"auth":{"selectedType":"gemini-api-key"}},"model":{"name":"gemini-2.5-pro"}}`
|
|
os.WriteFile(filepath.Join(dir, "settings.json"), []byte(settings), 0o644)
|
|
os.WriteFile(filepath.Join(dir, ".env"), []byte("GEMINI_API_KEY=mine\n"), 0o600)
|
|
g := gemini(home)
|
|
was := g.Values()
|
|
if err := g.Apply("model", "fake/m1"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !g.Wired() {
|
|
t.Fatal("not wired on a magpie model")
|
|
}
|
|
if err := g.Disconnect(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := g.Values(); !reflect.DeepEqual(got, was) || g.Wired() {
|
|
t.Fatalf("after disconnect %v, want %v", got, was)
|
|
}
|
|
if k, _ := edit.GetEnvFile(filepath.Join(dir, ".env"), "GEMINI_API_KEY"); k != "mine" {
|
|
t.Fatalf("own key %q", k)
|
|
}
|
|
if u, _ := edit.GetEnvFile(filepath.Join(dir, ".env"), "GOOGLE_GEMINI_BASE_URL"); u != "" {
|
|
t.Fatalf("base URL left: %q", u)
|
|
}
|
|
}
|
|
|
|
// An agent magpie is not in has nothing to disconnect: its config stays
|
|
// byte for byte.
|
|
func TestDisconnectUnwiredLeavesConfig(t *testing.T) {
|
|
home, read := codexHome(t, "", "model = \"gpt-5.4\"\nmodel_reasoning_effort = \"high\"\n")
|
|
was := read()
|
|
if err := codex(home).Disconnect(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := read(); got != was {
|
|
t.Fatalf("changed:\n%s\nwant\n%s", got, was)
|
|
}
|
|
}
|