Files
magpie/internal/agent/fx_test.go
T
yetone 2eee900dc9 agent: eight more syncs keep what the user set on magpie's block (#2030's siblings)
1e654d8ce fixed T3 Code's sync, which rewrote magpie's provider instance
whole. The same shape lost user-set keys in eight more agents. On every
Sync and every pick, each one rewrote magpie's own block in the agent's
config whole. Each app's schema was taken from its own source, never
guessed:

- Qoder 1.1.70 (@qoder-ai/qodercli bundle): providers.magpie lost routing
  (the compact and subagent models), firstPayloadTimeoutMs,
  streamIdleTimeoutMs, type, authType, anthropic and a displayName.
- fx (vercel-labs/fx configured_provider.zig): reviewer_model was lost,
  and a tool_choice_mode of "omit" went back to "send". auth stays
  magpie's whole, since fx refuses an env beside type none.
- Hermes (hermes_cli/config_providers.py): `hermes model` itself writes
  default_model into providers.magpie. That was lost, as were key_cmd,
  the timeouts, extra_body, a name and extra headers. This held in every
  profile spread() writes.
- omp (models-config-schema-bundle.ts): modelOverrides, discovery,
  disableStrictTools, headers and compat flags were lost. auth and
  apiKey stay magpie's, or go, so a loopback's auth: none never sits
  beside a WSL key.
- Kimi Code (configSection.ts ProviderConfig): SetTOMLTable replaced
  [providers.magpie], losing inline custom_headers, env and
  default_model. Its three keys are now set in place.
- Cline (local-provider-registry.ts): models.json lost the provider's
  other keys, and the per-model overrides that the VS Code model editor
  writes. A model magpie no longer lists still goes.
- Goose (declarative.rs): the custom provider file was rewritten whole,
  losing display_name, timeout_seconds, toolshim, other headers and the
  like. Goose's own editor carries those keys over.
- ZCode (provider-data-schema.ts, strict): the provider rule lost
  providerName, logo, visibility, api.headers and
  access.apiKeyManagementUrl. Only keys already in the file, which ZCode
  accepted, are kept.

Each merge lays magpie's block over what is there. magpie's parts (the
address, the key, the format and the model list) are named, and a key
magpie stops writing goes with them. The JSON ones use theirsKept or
mergeKept. A new yamlKept does the same for Hermes and omp.
agent-wiring.md lists what each one keeps.

Tests: Test{Qoder,Fx,Hermes,Omp,Kimi,Cline,Goose,ZCode}KeepsTheUsersSettings.
Each one:
- connects;
- sets the user's keys the way the app writes them, plus a stale
  address and a stale model;
- adds a provider, then syncs 3 times and picks again;
- checks that the user's keys stay, that magpie's parts are rewritten,
  and that the stale model is gone.

All eight fail on origin/main's agent code (each reports "the user's
settings not kept") and pass with this change.

Run, in a sandbox HOME, with GOCACHE in the scratchpad and Go 1.26.8:
- gofmt -l on the changed files: clean.
- go vet ./... (darwin), GOOS=linux go vet -tags nogui ./... and
  GOOS=windows go vet ./...: clean.
- go test -tags nogui -timeout 45m ./internal/agent: ok, 805s.
- go test -tags nogui -timeout 45m ./...: all ok.

Not tried: none of these apps was run against a real config written by
this change. Their schemas come from their source (Qoder's npm bundle,
fx, Hermes, oh-my-pi, kimi-code, cline, goose and ZCode at their current
heads), not from a live install.
2026-10-14 09:21:18 +08:00

174 lines
6.2 KiB
Go

package agent
import (
"encoding/json"
"os"
"path/filepath"
"testing"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/testenv"
)
func TestFx(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, ".fx", "settings.json")
read := func() map[string]any {
var m map[string]any
b, _ := os.ReadFile(path)
json.Unmarshal(b, &m)
return m
}
os.MkdirAll(filepath.Dir(path), 0o755)
os.WriteFile(path, []byte(`{"theme":"dark","model":"anthropic/claude-sonnet-5","providers":{"local":{"protocol":"openai-chat-completions","base_url":"http://localhost:11434/v1","auth":{"type":"none"}}}}`), 0o644)
a := fx(home)
f := a.Field("model")
if f.Get() != "anthropic/claude-sonnet-5" {
t.Fatalf("get: %q", f.Get())
}
if err := f.Set("magpie/deepseek/flash"); err != nil {
t.Fatal(err)
}
s := read()
ps := s["providers"].(map[string]any)
m, ok := ps["magpie"].(map[string]any)
if !ok || ps["local"] == nil || s["provider"] != "magpie" || s["models"].(map[string]any)["magpie"] != "deepseek/flash" || s["theme"] != "dark" {
t.Fatalf("settings: %v", s)
}
if m["protocol"] != "openai-chat-completions" || m["base_url"] != gatewayV1() || m["auth"].(map[string]any)["type"] != "none" {
t.Fatalf("provider: %v", m)
}
if ms := m["model_metadata"].(map[string]any); ms["deepseek/pro"] == nil || ms["deepseek/flash"] == nil {
t.Fatalf("models: %v", ms)
}
if f.Get() != "magpie/deepseek/flash" || a.Check() != "" {
t.Fatalf("get: %q, check: %q", f.Get(), a.Check())
}
// its own model: magpie steps out, and the gateway's model is set
if err := f.Set("openai/gpt-6-sol"); err != nil {
t.Fatal(err)
}
s = read()
if s["provider"] != nil || s["providers"].(map[string]any)["magpie"] != nil || s["providers"].(map[string]any)["local"] == nil ||
s["models"].(map[string]any)["gateway"] != "openai/gpt-6-sol" || f.Get() != "openai/gpt-6-sol" {
t.Fatalf("own: %v", s)
}
// one of the user's own providers keeps its model
os.WriteFile(path, []byte(`{"provider":"local","models":{"local":"qwen"}}`), 0o644)
if f.Get() != "qwen" {
t.Fatalf("local: %q", f.Get())
}
// through magpie, then reset
f.Set("magpie/deepseek/pro")
if err := f.Set(""); err != nil {
t.Fatal(err)
}
s = read()
if s["provider"] != nil || s["providers"].(map[string]any)["magpie"] != nil || s["models"].(map[string]any)["magpie"] != nil {
t.Fatalf("reset: %v", s)
}
}
// fx is also the JSON viewer's name: a command of that name on PATH is no
// sign of the agent, its folder is.
func TestFxDetected(t *testing.T) {
home := t.TempDir()
bin := t.TempDir()
t.Setenv("PATH", bin)
testenv.Program(t, filepath.Join(bin, "fx"), "#!/bin/sh\n")
if fx(home).Detected() {
t.Error("an fx command alone is taken for the agent")
}
if err := os.WriteFile(filepath.Join(home, ".fx"), nil, 0o644); err != nil {
t.Fatal(err)
}
if fx(home).Detected() {
t.Error("a file named .fx is taken for the agent's folder")
}
os.Remove(filepath.Join(home, ".fx"))
if err := os.MkdirAll(filepath.Join(home, ".fx"), 0o755); err != nil {
t.Fatal(err)
}
if !fx(home).Detected() {
t.Error("~/.fx is not taken for the agent")
}
}
// What the user set on magpie's provider in fx's settings stays through
// every sync and pick, as T3 Code's did after #2030: the reviewer model fx's
// auto permission mode asks, and a tool_choice_mode turned to omit. The
// address, auth and model list stay magpie's; an auth the user changed goes
// back whole, as fx refuses an env beside type none.
func TestFxKeepsTheUsersSettings(t *testing.T) {
home := syncHome(t)
path := filepath.Join(home, ".fx", "settings.json")
os.MkdirAll(filepath.Dir(path), 0o755)
os.WriteFile(path, []byte(`{"theme":"dark","providers":{"local":{"protocol":"openai-chat-completions","base_url":"http://localhost:11434/v1","auth":{"type":"none"}}}}`), 0o644)
a := fx(home)
f := a.Field("model")
if err := f.Set("magpie/relay/glm-4.6"); err != nil {
t.Fatal(err)
}
if err := edit.SetJSON(path,
edit.KV{Path: "providers.magpie.reviewer_model", Value: "relay/glm-4.6"},
edit.KV{Path: "providers.magpie.tool_choice_mode", Value: "omit"},
edit.KV{Path: "providers.magpie.auth", Value: map[string]any{"type": "bearer", "env": "MY_KEY"}},
edit.KV{Path: "providers.magpie.base_url", Value: "http://127.0.0.1:1/v1"},
edit.KV{Path: "providers.magpie.model_metadata", Value: map[string]any{"gone/old": map[string]any{}}},
); err != nil {
t.Fatal(err)
}
if err := provider.Save(provider.Provider{ID: "deepseek", Name: "DeepSeek", Key: "k", Chat: "http://127.0.0.1:1/v1", Models: []string{"pro"}}); err != nil {
t.Fatal(err)
}
for range 3 {
if err := a.Sync(); err != nil {
t.Fatal(err)
}
}
check := func(when string) {
t.Helper()
var s struct {
Theme string `json:"theme"`
Providers map[string]struct {
ReviewerModel string `json:"reviewer_model"`
ToolChoiceMode string `json:"tool_choice_mode"`
BaseURL string `json:"base_url"`
Auth map[string]any `json:"auth"`
ModelMetadata map[string]any `json:"model_metadata"`
} `json:"providers"`
}
raw := readFile(path)
if err := json.Unmarshal([]byte(raw), &s); err != nil {
t.Fatalf("%v\n%s", err, raw)
}
p := s.Providers[magpieID]
if p.ReviewerModel != "relay/glm-4.6" || p.ToolChoiceMode != "omit" {
t.Errorf("%s: the user's settings not kept:\n%s", when, raw)
}
if p.BaseURL == "http://127.0.0.1:1/v1" || p.BaseURL == "" || len(p.Auth) != 1 || p.Auth["type"] != "none" || p.ModelMetadata["gone/old"] != nil || p.ModelMetadata["deepseek/pro"] == nil {
t.Errorf("%s: magpie's own parts not written:\n%s", when, raw)
}
if s.Theme != "dark" || s.Providers["local"].BaseURL == "" {
t.Errorf("%s: the user's other settings changed:\n%s", when, raw)
}
}
check("sync")
if err := f.Set("magpie/deepseek/pro"); err != nil {
t.Fatal(err)
}
check("pick")
}