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

315 lines
11 KiB
Go

package agent
import (
"encoding/json"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"gopkg.in/yaml.v3"
"github.com/yetone/magpie/internal/edit"
)
// Goose (Block's goose, its CLI and desktop app alike) reaches magpie as a
// custom provider: a JSON file of its own in custom_providers beside
// config.yaml, named by the provider's id, which goose loads at start-up
// (crates/goose/src/config/declarative_providers.rs). Its schema is
// DeclarativeProviderConfig (crates/goose-providers/src/declarative.rs), and
// each of its models a ModelInfo (crates/goose-provider-types/src/base.rs):
//
// - context_limit is the window goose's ContextLimitResolver takes for the
// model before anything else but GOOSE_CONTEXT_LIMIT
// (crates/goose-provider-types/src/context_limit.rs). Without it goose
// guesses from its own registry by the model's name, or 128K.
// - reasoning is what the desktop app's model picker asks before it shows
// its Thinking Level (fetchModelReasoning, ui/desktop/src/components/
// settings/models/modelInterface.ts); a model declared without it is
// taken for one that doesn't think.
//
// dynamic_models false keeps goose to this list rather than the gateway's
// /v1/models (every model, none of their windows), and
// skip_canonical_filtering keeps magpie's order and models goose's registry
// doesn't know.
// gooseProviderID is magpie's file in Goose's custom_providers and the
// provider GOOSE_PROVIDER names.
const gooseProviderID = magpieID
// gooseThinks matches the model names goose sends its thinking level for on
// an OpenAI-compatible provider: OpenAI's reasoning models, as goose's
// is_openai_responses_model tells them (crates/goose-provider-types/src/
// formats/openai.rs). For any other model goose keeps the level to itself —
// thinking_effort is one of its own request params, never sent — so a
// level offered for it would change nothing; it is declared without
// reasoning rather than shown a picker that does nothing.
var gooseThinks = regexp.MustCompile(`(?i)(?:^|[-/])(?:o\d+(?:$|-)|gpt-(?:5|6)(?:$|[-.]))`)
// gooseKeys are the secrets goose reads for the native providers whose
// models magpie can list, and which goose takes from the environment before
// its keyring (Config::get_secret, crates/goose/src/config/base.rs; the keys
// from crates/goose/src/providers/<name>_def.rs).
var gooseKeys = map[string]string{
"anthropic": "ANTHROPIC_API_KEY",
"openai": "OPENAI_API_KEY",
"google": "GOOGLE_API_KEY",
"openrouter": "OPENROUTER_API_KEY",
}
// gooseConfigured is the providers other than magpie that the Goose whose
// config.yaml is at cfg has set up: the ones its providers map marks
// configured or that it uses now (set_active_provider, crates/goose/src/
// config/providers.rs), the ones a legacy <name>_configured marker names
// (migrate_provider_config, config/migrations.rs), and a provider whose key
// is in the environment. A key in goose's keyring can't be seen from here;
// such a provider still counts once goose has used it, as each provider it
// is set up with gets an entry. getenv is the environment goose runs in:
// none of a WSL distro's is read.
func gooseConfigured(cfg string, getenv func(string) string) []string {
set := map[string]bool{}
if b, err := os.ReadFile(cfg); err == nil {
var c map[string]any
if yaml.Unmarshal(b, &c) == nil {
if ps, ok := c["providers"].(map[string]any); ok {
for name, v := range ps {
if e, ok := v.(map[string]any); ok && e["configured"] == true {
set[name] = true
}
}
}
for k, v := range c {
if name, ok := strings.CutSuffix(k, "_configured"); ok && v == true {
set[name] = true
}
}
for _, k := range []string{"active_provider", "GOOSE_PROVIDER"} {
if p, ok := c[k].(string); ok && p != "" {
set[p] = true
}
}
}
}
for p, k := range gooseKeys {
if getenv(k) != "" {
set[p] = true
}
}
delete(set, gooseProviderID)
out := make([]string, 0, len(set))
for p := range set {
out = append(out, p)
}
sort.Strings(out)
return out
}
// gooseProviderPath is magpie's custom provider file for the Goose whose
// config.yaml is at cfg.
func gooseProviderPath(cfg string) string {
return filepath.Join(filepath.Dir(cfg), "custom_providers", gooseProviderID+".json")
}
// gooseProviderJSON is magpie as a custom provider of the Goose at a
// place, its models the catalog as Goose is shown it.
func gooseProviderJSON(at place) map[string]any {
models := []map[string]any{}
for _, m := range magpieModels("goose") {
e := map[string]any{"name": m.ID, "reasoning": gooseThinks.MatchString(m.ID)}
if m.Context > 0 {
e["context_limit"] = m.Context
}
models = append(models, e)
}
p := map[string]any{
"name": gooseProviderID,
"engine": "openai",
"display_name": "magpie",
"description": "Every model magpie has, through its local gateway",
"api_key_env": "",
"base_url": at.v1(),
"headers": map[string]any{"Authorization": "Bearer " + gooseKey(at)},
"requires_auth": false,
"skip_canonical_filtering": true,
"models": models,
}
// goose refuses a provider whose static list is empty
if len(models) > 0 {
p["dynamic_models"] = false
}
return p
}
// gooseKey is the key magpie's custom provider hands the Goose at a place:
// one that names Goose (agentKeyAt), since goose's HTTP client sends no
// User-Agent (reqwest's default, crates/goose-providers/src/api_client.rs),
// so the plain key left its requests to "your agent" on Routing and "other"
// in Usage (#1083).
func gooseKey(at place) string { return agentKeyAt("goose", at.gw()) }
// gooseProviderKept is magpie's custom provider laid over the file at path.
// Goose's own editor (update_custom_provider, crates/goose/src/config/
// declarative_providers.rs) carries the keys it doesn't show into the file it
// writes, and the user sets timeout_seconds, toolshim, supports_streaming,
// preserves_thinking, a header or a name there (DeclarativeProviderConfig).
// Each stays (#2030); what reaches the gateway, its key and the model list
// are magpie's.
func gooseProviderKept(path string, at place) map[string]any {
want := gooseProviderJSON(at)
raw, err := edit.Read(path)
if err != nil || raw == nil {
return want
}
var have map[string]any
if json.Unmarshal(raw, &have) != nil || have == nil {
return want
}
if name, _ := have["display_name"].(string); strings.TrimSpace(name) != "" {
want["display_name"] = name
}
if h, _ := have["headers"].(map[string]any); h != nil {
// a header of the user's, beside magpie's Authorization
want["headers"] = mergeKept(h, want["headers"].(map[string]any), nil)
}
return mergeKept(have, want, []string{"name", "engine", "api_key_env", "base_url", "requires_auth", "dynamic_models", "models", "headers"})
}
// writeGooseProvider writes magpie's custom provider file for Goose.
func writeGooseProvider(path string, at place) error {
b, err := json.MarshalIndent(gooseProviderKept(path, at), "", " ")
if err != nil {
return err
}
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return err
}
return edit.WriteAtomic(path, append(b, '\n'))
}
// syncGooseProvider rewrites magpie's custom provider file for Goose with
// the catalog as it is now, where magpie wrote one and it differs.
func syncGooseProvider(path string, at place) error {
raw, err := edit.Read(path)
if err != nil || raw == nil {
return nil
}
if sameJSON(string(raw), gooseProviderKept(path, at)) {
return nil
}
return writeGooseProvider(path, at)
}
// removeGooseProvider takes magpie's custom provider file away once Goose's
// model is no longer one of magpie's.
func removeGooseProvider(path string) error {
if err := edit.Remove(path); err != nil && !errors.Is(err, fs.ErrNotExist) {
return err
}
return nil
}
// Goose keeps the provider and model it uses in one of two layouts of its
// config.yaml (crates/goose/src/config/providers.rs, migrations.rs). The
// old one is two flat keys, GOOSE_PROVIDER and GOOSE_MODEL. The new one,
// which goose migrates a config to on its next write, is active_provider
// and an entry in the providers map holding that provider's model:
//
// active_provider: magpie
// providers:
// magpie:
// enabled: true
// model: zhipu/glm-5.3
// configured: true
//
// goose reads active_provider before GOOSE_PROVIDER and the entry's model
// before GOOSE_MODEL (get_active_provider, get_active_model), and once a
// providers map is there its migration deletes the flat keys on the next
// write (cleanup_legacy_provider_keys). So a model set with the flat keys
// alone on a migrated config is never used, and lost.
// gooseNewLayout reports whether the Goose config at cfg is in the new
// layout: it has a providers map or an active_provider.
func gooseNewLayout(cfg string) bool {
b, err := edit.Read(cfg)
if err != nil || b == nil {
return false
}
var c map[string]any
if yaml.Unmarshal(b, &c) != nil {
return false
}
_, providers := c["providers"]
_, active := c["active_provider"]
return providers || active
}
// gooseActive is the provider and model the Goose config at cfg uses, read
// as goose reads them (the environment aside).
func gooseActive(cfg string) (provider, model string) {
provider, _ = edit.GetYAMLTop(cfg, "active_provider")
if provider == "" {
provider, _ = edit.GetYAMLTop(cfg, "GOOSE_PROVIDER")
}
if provider != "" && !strings.Contains(provider, ".") {
model, _ = edit.GetYAML(cfg, "providers."+provider+".model")
}
if model == "" {
model, _ = edit.GetYAMLTop(cfg, "GOOSE_MODEL")
}
return provider, model
}
// gooseModel is the Goose config at cfg's model as provider/model.
func gooseModel(cfg string) string {
p, m := gooseActive(cfg)
switch {
case m == "":
return ""
case p == "":
return m
}
return p + "/" + m
}
// setGooseModel makes v, a provider/model, the model the Goose config at
// cfg uses, in the layout the config is in: on a new one as goose's own
// set_active_provider writes it, other providers' entries kept; on an old
// one the flat keys, which an old goose reads and a new one migrates.
func setGooseModel(cfg, v string) error {
p, m, ok := strings.Cut(v, "/")
if !ok || p == "" || m == "" {
return fmt.Errorf("expected provider/model, got %q", v)
}
if !gooseNewLayout(cfg) {
return edit.SetYAMLTop(cfg, edit.KV{Path: "GOOSE_PROVIDER", Value: p}, edit.KV{Path: "GOOSE_MODEL", Value: m})
}
if strings.Contains(p, ".") {
return fmt.Errorf("goose provider %q: a dot can't be written into its providers map", p)
}
if err := edit.SetYAML(cfg,
edit.KV{Path: "active_provider", Value: p},
edit.KV{Path: "providers." + p + ".enabled", Value: true},
edit.KV{Path: "providers." + p + ".model", Value: m},
edit.KV{Path: "providers." + p + ".configured", Value: true},
); err != nil {
return err
}
// flat keys left beside the map would be read by no goose that reads
// the map, and deleted by the next one that writes
return edit.DelYAMLTop(cfg, "GOOSE_PROVIDER", "GOOSE_MODEL")
}
// clearGooseModel takes the Goose config at cfg off any model, as goose's
// clear_active_provider does, and drops magpie's own providers entry, as
// magpie's custom provider goes with it; other providers' entries are kept.
func clearGooseModel(cfg string) error {
if err := edit.DelYAMLTop(cfg, "active_provider", "GOOSE_PROVIDER", "GOOSE_MODEL"); err != nil {
return err
}
return edit.DelYAML(cfg, "providers."+gooseProviderID)
}