diff --git a/internal/agent/applied.go b/internal/agent/applied.go index 0b3e01e49..4057b0c8c 100644 --- a/internal/agent/applied.go +++ b/internal/agent/applied.go @@ -901,12 +901,13 @@ func sameHost(a, b string) bool { return hostOf(a) == hostOf(b) } // gateway — pairs of key and value, read with get — and says which no longer // holds, "" when all do. A URL is quoted; a key's value never is. A URL // that reaches magpie another way (reachesMagpie: a reverse proxy, #1985) -// holds. +// holds. So does a key of the gateway's own the user put in place of +// magpie's (keyHolds). func wiringOff(name, file string, get func(string) (string, bool), kvs ...string) string { for i := 0; i+1 < len(kvs); i += 2 { k, want := kvs[i], kvs[i+1] v, _ := get(k) - if v == want || v != "" && reachesMagpie(v, want) { + if keyHolds(v, want) || v != "" && reachesMagpie(v, want) { continue } where := name + "'s " + k + " (" + filepath.Base(file) + ")" diff --git a/internal/agent/codex.go b/internal/agent/codex.go index 6970084f3..96d019059 100644 --- a/internal/agent/codex.go +++ b/internal/agent/codex.go @@ -1261,7 +1261,7 @@ func codexIn(at place) *Agent { if err != nil { return err.Error() } - if t["base_url"] != at.v1() || t["experimental_bearer_token"] != at.gwKey() || t["wire_api"] != "responses" { + if t["base_url"] != at.v1() || !keyHolds(t["experimental_bearer_token"], at.gwKey()) || t["wire_api"] != "responses" { return "Codex's [model_providers.magpie] no longer points at magpie's gateway (" + at.v1() + ")" } if c := get("model_catalog_json"); !ownCatalog(c) { diff --git a/internal/agent/gatewaykey.go b/internal/agent/gatewaykey.go index d360a176d..4217bc441 100644 --- a/internal/agent/gatewaykey.go +++ b/internal/agent/gatewaykey.go @@ -1,6 +1,8 @@ package agent import ( + "strings" + "github.com/yetone/magpie/internal/access" "github.com/yetone/magpie/internal/gateway" ) @@ -63,3 +65,19 @@ func gatewayTakes(k string) bool { _, ok := access.Authenticate(k) return ok } + +// gatewayKey says want is a key magpie gives an agent for its gateway: +// gateway.Token, the LAN sharing key, or one naming the agent +// (gateway.TokenFor). +func gatewayKey(want string) bool { + return ourKey(want) || strings.HasPrefix(want, gateway.Token+"-") && !strings.ContainsAny(want, "/:") +} + +// keyHolds says the value v an agent's config has where magpie wrote want +// still reaches magpie: want itself, or, where want is magpie's key for its +// gateway, a key of the gateway's own the user put there instead (Gateway +// keys), which the gateway takes just the same (PaulT on Discord: Hermes +// with a key of his own was said not to go through magpie). +func keyHolds(v, want string) bool { + return v == want || v != "" && gatewayKey(want) && gatewayTakes(v) +} diff --git a/internal/agent/hermes_test.go b/internal/agent/hermes_test.go index b0fa27229..cfba910f0 100644 --- a/internal/agent/hermes_test.go +++ b/internal/agent/hermes_test.go @@ -8,8 +8,10 @@ import ( "strings" "testing" + "github.com/yetone/magpie/internal/access" "github.com/yetone/magpie/internal/appdir" "github.com/yetone/magpie/internal/edit" + "github.com/yetone/magpie/internal/gateway" "github.com/yetone/magpie/internal/provider" "gopkg.in/yaml.v3" ) @@ -357,3 +359,91 @@ func TestHermesKeepsTheUsersSettings(t *testing.T) { } check("pick") } + +// PaulT on Discord: a key made under Gateway keys, put in Hermes's +// config.yaml in place of magpie's, goes through magpie all the same, so +// the check is quiet and a sync keeps it. A key the gateway doesn't take +// still is drift. +func TestHermesWithAGatewayKeyOfTheUsers(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + t.Setenv("HERMES_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"}}); err != nil { + t.Fatal(err) + } + dir := appdir.HermesHomeIn(home) + path := filepath.Join(dir, "config.yaml") + os.MkdirAll(dir, 0o755) + os.WriteFile(path, []byte("model:\n default: x\n"), 0o644) + a := hermes(home) + if err := a.Field("model").Set("magpie/deepseek/pro"); err != nil { + t.Fatal(err) + } + if d := a.Check(); d != "" { + t.Fatalf("as magpie wrote it: %s", d) + } + secret, err := access.Update("add-key", access.Change{Name: "hermes"}) + if err != nil { + t.Fatal(err) + } + if err := edit.SetYAML(path, edit.KV{Path: "providers.magpie.api_key", Value: secret}); err != nil { + t.Fatal(err) + } + if d := a.Check(); d != "" { + t.Fatalf("a key the gateway issued is drift: %s", d) + } + if err := a.Sync(); err != nil { + t.Fatal(err) + } + if v, _ := edit.GetYAML(path, "providers.magpie.api_key"); v != secret { + t.Fatalf("a sync wrote over the user's key: %q", v) + } + if err := edit.SetYAML(path, edit.KV{Path: "providers.magpie.api_key", Value: "sk-not-this-gateways"}); err != nil { + t.Fatal(err) + } + if d := a.Check(); !strings.Contains(d, "api_key") { + t.Fatalf("a key the gateway doesn't take is not reported: %q", d) + } + if err := a.Sync(); err != nil { + t.Fatal(err) + } + if v, _ := edit.GetYAML(path, "providers.magpie.api_key"); v != gateway.Token { + t.Fatalf("a key the gateway doesn't take is kept: %q", v) + } +} + +// keyHolds, which every agent's check of magpie's key goes through: a key +// of the gateway's own stands in for magpie's, never for a URL or another +// value, and one the gateway doesn't take doesn't. +func TestKeyHoldsTakesAGatewayKey(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config")) + secret, err := access.Update("add-key", access.Change{Name: "mine"}) + if err != nil { + t.Fatal(err) + } + for _, c := range []struct { + v, want string + ok bool + }{ + {gateway.Token, gateway.Token, true}, + {secret, gateway.Token, true}, + {secret, gateway.TokenFor("alma"), true}, + {"sk-other", gateway.Token, false}, + {"", gateway.Token, false}, + {secret, "http://127.0.0.1:3425/v1", false}, + {secret, "chat_completions", false}, + } { + if got := keyHolds(c.v, c.want); got != c.ok { + t.Errorf("keyHolds(%q, %q) = %v", c.v, c.want, got) + } + } + if d := wiringOff("X", "x.yaml", func(string) (string, bool) { return secret, true }, "api_key", gateway.Token); d != "" { + t.Fatalf("wiringOff: %s", d) + } +} diff --git a/internal/agent/qwen.go b/internal/agent/qwen.go index 616362eee..f9d519d3d 100644 --- a/internal/agent/qwen.go +++ b/internal/agent/qwen.go @@ -351,7 +351,7 @@ func qwenIn(at place) *Agent { if c == nil { return "" } - if v, ok := edit.GetJSON(path, qwenEnv); !ok || v != at.gwKey() { + if v, _ := edit.GetJSON(path, qwenEnv); !keyHolds(v, at.gwKey()) { return "Qwen Code's " + qwenEnv + " (settings.json) no longer is magpie's key, so magpie's models have none" } if v, ok := edit.GetJSON(path, qwenAuth); !ok || v != "openai" { diff --git a/internal/agent/reasonix.go b/internal/agent/reasonix.go index 8604d52f8..3f5267555 100644 --- a/internal/agent/reasonix.go +++ b/internal/agent/reasonix.go @@ -261,7 +261,7 @@ func reasonixIn(at place) *Agent { } currentState.AddedAccess = true } - if token, _ := edit.GetEnvFile(env, reasonixKey); token != gwKey() { + if token, _ := edit.GetEnvFile(env, reasonixKey); !keyHolds(token, gwKey()) { if err := edit.SetEnvFile(env, edit.KV{Path: reasonixKey, Value: gwKey()}); err != nil { return err } @@ -331,7 +331,7 @@ func reasonixIn(at place) *Agent { if p.Kind != "openai" || p.BaseURL != at.v1() && !reachesMagpie(p.BaseURL, at.v1()) { return "Reasonix's magpie base_url or kind changed" } - if token, _ := edit.GetEnvFile(env, reasonixKey); token != gwKey() { + if token, _ := edit.GetEnvFile(env, reasonixKey); !keyHolds(token, gwKey()) { return "Reasonix's gateway credential changed" } if !slices.Contains(p.Models, ref) || cfg.Agent.Planner != nil && usesMagpie(*cfg.Agent.Planner) && !slices.Contains(p.Models, strings.TrimPrefix(*cfg.Agent.Planner, magpieID+"/")) { diff --git a/internal/agent/sync.go b/internal/agent/sync.go index 32f252216..d1d0aa0c2 100644 --- a/internal/agent/sync.go +++ b/internal/agent/sync.go @@ -221,6 +221,13 @@ func mergeKept(have, want map[string]any, own []string) map[string]any { } for k, h := range have { if slices.Contains(own, k) { + // magpie's key for the gateway, where the user put a key of + // the gateway's own instead: theirs stays (keyHolds) + if hs, ok := h.(string); ok { + if ws, ok := want[k].(string); ok && hs != ws && keyHolds(hs, ws) { + out[k] = hs + } + } continue } w, ok := want[k]