Files
magpie/internal/agent/kimi_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

251 lines
7.8 KiB
Go

package agent
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/pelletier/go-toml/v2"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/provider"
)
func TestKimi(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"))
t.Setenv("KIMI_SHARE_DIR", "")
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, ".kimi", "config.toml")
os.MkdirAll(filepath.Dir(path), 0o755)
// as kimi writes it after a Kimi Code sign-in
os.WriteFile(path, []byte(`default_model = "kimi-code/kimi-for-coding"
default_thinking = true
[models."kimi-code/kimi-for-coding"]
provider = "managed:kimi-code"
model = "kimi-for-coding"
max_context_size = 262144
[providers."managed:kimi-code"]
type = "kimi"
base_url = "https://api.kimi.com/coding/v1"
api_key = ""
[providers."managed:kimi-code".oauth]
storage = "file"
key = "oauth/kimi-code"
[loop_control]
max_steps_per_turn = 100
`), 0o644)
read := func() string { b, _ := os.ReadFile(path); return string(b) }
table := func(name string) map[string]string {
t.Helper()
got, err := edit.GetTOMLTable(path, name)
if err != nil {
t.Fatal(err)
}
return got
}
// what kimi's own check wants: the default among the models, and each
// model's provider among the providers
valid := func() {
t.Helper()
var c struct {
Default string `toml:"default_model"`
Models map[string]struct{ Provider string }
Providers map[string]any
}
if err := toml.Unmarshal([]byte(read()), &c); err != nil {
t.Fatalf("%v\n%s", err, read())
}
if _, ok := c.Models[c.Default]; c.Default != "" && !ok {
t.Fatalf("default %q is not a model:\n%s", c.Default, read())
}
for k, m := range c.Models {
if c.Providers[m.Provider] == nil {
t.Fatalf("%s: provider %q missing:\n%s", k, m.Provider, read())
}
}
}
a := kimi(home)
if a.Path != path {
t.Fatalf("path: %s", a.Path)
}
f := a.Field("model")
if f.Get() != "kimi-code/kimi-for-coding" {
t.Fatalf("get: %q", f.Get())
}
var own []string
for _, o := range f.Options(a.Values()) {
if !strings.HasPrefix(o.Value, "magpie/") {
own = append(own, o.Value)
}
}
if strings.Join(own, ",") != "kimi-code/kimi-for-coding" {
t.Fatalf("own options: %v", own)
}
if err := f.Set("magpie/deepseek/pro"); err != nil {
t.Fatal(err)
}
valid()
raw := read()
p := table("providers.magpie")
m := table(`models."magpie/deepseek/pro"`)
if p["type"] != "kimi" || p["api_key"] != "magpie" || !strings.HasSuffix(p["base_url"], "/v1") ||
m["provider"] != "magpie" || m["model"] != "deepseek/pro" || m["max_context_size"] == "" ||
table(`models."magpie/deepseek/flash"`) == nil {
t.Fatalf("tables:\n%s", raw)
}
if f.Get() != "magpie/deepseek/pro" || table(`providers."managed:kimi-code".oauth`)["key"] != "oauth/kimi-code" ||
table("loop_control")["max_steps_per_turn"] != "100" || !strings.Contains(raw, "default_thinking = true") {
t.Fatalf("config:\n%s", raw)
}
if a.Check() != "" {
t.Fatalf("check: %s", a.Check())
}
// a provider added later reaches Kimi's picker
if err := provider.Save(provider.Provider{ID: "glm", Name: "GLM", Chat: "https://open.bigmodel.cn/api/paas/v4", Key: "k", Models: []string{"glm-5"}}); err != nil {
t.Fatal(err)
}
if err := a.Sync(); err != nil {
t.Fatal(err)
}
valid()
if table(`models."magpie/glm/glm-5"`) == nil || strings.Count(read(), "[providers.magpie]") != 1 {
t.Fatalf("sync:\n%s", read())
}
// its wiring gone while the default is still magpie's
edit.SetTOMLKey(path, "providers.magpie", "base_url", "http://elsewhere/v1")
if a.Check() == "" {
t.Fatal("check missed the moved base_url")
}
// reset: back to the Kimi Code model the user had, magpie's tables gone
if err := f.Set(""); err != nil {
t.Fatal(err)
}
valid()
raw = read()
if f.Get() != "kimi-code/kimi-for-coding" || strings.Contains(raw, "magpie") {
t.Fatalf("reset:\n%s", raw)
}
// and Sync, with magpie out, writes nothing back
if err := a.Sync(); err != nil || read() != raw {
t.Fatalf("sync after reset: %v\n%s", err, read())
}
// its own model from magpie: magpie's tables go too
f.Set("magpie/deepseek/flash")
if err := f.Set("kimi-code/kimi-for-coding"); err != nil {
t.Fatal(err)
}
valid()
if strings.Contains(read(), "magpie") {
t.Fatalf("own:\n%s", read())
}
}
// A kimi that has never run has no config; magpie writes one Kimi takes.
func TestKimiNoConfig(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"))
t.Setenv("KIMI_SHARE_DIR", filepath.Join(home, "share"))
if err := provider.Save(provider.Provider{ID: "deepseek", Name: "DeepSeek", Chat: "https://api.deepseek.com/v1", Key: "k", Models: []string{"pro"}}); err != nil {
t.Fatal(err)
}
a := kimi(home)
if a.Path != filepath.Join(home, "share", "config.toml") {
t.Fatalf("path: %s", a.Path)
}
f := a.Field("model")
if err := f.Set("magpie/deepseek/pro"); err != nil {
t.Fatal(err)
}
b, _ := os.ReadFile(a.Path)
var c map[string]any
if err := toml.Unmarshal(b, &c); err != nil || c["default_model"] != "magpie/deepseek/pro" {
t.Fatalf("%v\n%s", err, b)
}
if err := f.Set(""); err != nil {
t.Fatal(err)
}
b, _ = os.ReadFile(a.Path)
if strings.Contains(string(b), "magpie") {
t.Fatalf("reset:\n%s", b)
}
}
// What the user set on magpie's provider table in Kimi Code's config.toml
// stays through every sync and pick (#2030's siblings): headers, env and a
// default model of its own. The type, address and key stay magpie's.
func TestKimiKeepsTheUsersSettings(t *testing.T) {
home := syncHome(t)
t.Setenv("KIMI_SHARE_DIR", "")
path := filepath.Join(home, ".kimi", "config.toml")
os.MkdirAll(filepath.Dir(path), 0o755)
os.WriteFile(path, []byte("default_model = \"x\"\n"), 0o644)
a := kimi(home)
f := a.Field("model")
if err := f.Set("magpie/relay/glm-4.6"); err != nil {
t.Fatal(err)
}
raw := readFile(path)
if !strings.Contains(raw, "[providers.magpie]\n") {
t.Fatalf("no table:\n%s", raw)
}
raw = strings.Replace(raw, "[providers.magpie]\n", "[providers.magpie]\ndefault_model = \"relay/glm-4.6\"\ncustom_headers = { X-Mine = \"1\" }\nenv = { HTTPS_PROXY = \"http://proxy:8080\" }\n", 1)
os.WriteFile(path, []byte(raw), 0o644)
edit.SetTOMLKey(path, "providers.magpie", "base_url", "http://127.0.0.1:1/v1")
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 c struct {
Providers map[string]struct {
Type string `toml:"type"`
BaseURL string `toml:"base_url"`
APIKey string `toml:"api_key"`
Default string `toml:"default_model"`
Headers map[string]string `toml:"custom_headers"`
Env map[string]string `toml:"env"`
} `toml:"providers"`
}
raw := readFile(path)
if err := toml.Unmarshal([]byte(raw), &c); err != nil {
t.Fatalf("%v\n%s", err, raw)
}
p := c.Providers[magpieID]
if p.Default != "relay/glm-4.6" || p.Headers["X-Mine"] != "1" || p.Env["HTTPS_PROXY"] != "http://proxy:8080" {
t.Errorf("%s: the user's settings not kept:\n%s", when, raw)
}
if p.Type != "kimi" || p.BaseURL == "http://127.0.0.1:1/v1" || p.BaseURL == "" || p.APIKey == "" {
t.Errorf("%s: magpie's own parts not written:\n%s", when, raw)
}
}
check("sync")
if err := f.Set("magpie/deepseek/pro"); err != nil {
t.Fatal(err)
}
check("pick")
}