Files
magpie/internal/agent/hermes_test.go
T
yetone b9d1cd36f5 agent: Hermes's api_key as "${VAR}" with the key in its .env reaches magpie (PaulT on Discord)
PaulT (magpie 0.1.1291) keeps Hermes's key the way Hermes keeps keys:
providers.magpie.api_key: "${MAGPIE_API_KEY}" in config.yaml, the key in
~/.hermes/.env. Hermes expands ${VAR} and ${env:VAR} as it loads the
config (hermes_cli/config.py _ENV_REF_RE, _env_expand_match), from the
.env beside it, which load_hermes_dotenv loads over the environment.
magpie compared the literal "${MAGPIE_API_KEY}" with its key, so Check
marked Hermes as needing attention, and a sync (mergeKept) wrote the
literal key over the reference.

Now hermesKey expands the reference the way Hermes does: the .env
beside the config (main or a profile's), then this machine's
environment. Check reads that key through keyHolds, so magpie's key or a
Gateway key is not drift. A variable magpie can't see (only a shell
profile sets it) or another secret source (${vault:...}) is unknown:
not drift, and kept. A sync or pick keeps the reference
(hermesKeptKey). When it names a key the gateway doesn't take, magpie
keeps the reference and writes its key into that .env under the
variable (made 0600 when there was none). A value with more than the
one reference in it is replaced by magpie's key, as before.

Siblings not covered here: other agents whose configs take env
references in the key (Droid's "${VAR}", OpenCode's {env:VAR}) still
compare literally.

Test: TestHermesWithAKeyInItsEnvFile uses the reporter's provider block
as written: a Gateway key and magpie's own (quoted, export) in .env,
${env:VAR} from the environment, an unseen variable, a wrong key (.env
rewritten in place, other lines kept), and a composite value.
Broken by hand: with hermes.go at origin/main it fails "a gateway key in
.env is drift: Hermes's api_key (config.yaml) was changed"; with only
the sync half removed it fails "the user's reference was written over".

Run (worktree, sandbox HOME, GOCACHE pinned, macOS, load ~300):
- gofmt -l internal/agent: nothing. go vet ./..., GOOS=linux go vet
  -tags nogui ./..., GOOS=windows go vet ./...: ok (at b10788745 and
  again at 94df23926). go build, GOOS=linux -tags nogui and
  GOOS=windows builds: ok at b10788745.
- go test -timeout 45m ./... at b10788745 (none of these three files
  changed up to 94df23926): 63 packages ok, internal/agent ok in 2092s.
  One red: internal/gateway TestMaxRPMTooLongIs429WithRetryAfter
  ("second = 200 Retry-After"), the load red filed as #1791; this change
  doesn't reach the gateway.
- At 94df23926: go test -run 'TestHermes|TestKeyHolds|TestGatewayKey|
  TestFolderVars' ./internal/agent/: ok.
Not tried: a real Hermes reading the config (its binary rewrites the
live launcher under a temp HERMES_HOME); the expansion rules were read
from Hermes's source (hermes_cli/config.py, env_loader.py).
2026-10-19 22:49:49 +08:00

561 lines
21 KiB
Go

package agent
import (
"os"
"path/filepath"
"runtime"
"slices"
"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"
)
func TestHermes(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", "flash"}}); err != nil {
t.Fatal(err)
}
dir := appdir.HermesHomeIn(home)
path := filepath.Join(dir, "config.yaml")
read := func() (map[string]any, map[string]any, string) {
var c map[string]any
b, _ := os.ReadFile(path)
yaml.Unmarshal(b, &c)
m, _ := c["model"].(map[string]any)
return c, m, string(b)
}
providers := func(c map[string]any) map[string]any { p, _ := c["providers"].(map[string]any); return p }
os.MkdirAll(dir, 0o755)
os.WriteFile(path, []byte("# mine\nmodel:\n default: anthropic/claude-opus-5 # main\n provider: openrouter\n base_url: https://openrouter.ai/api/v1\nproviders:\n mine:\n base_url: https://x\n"), 0o644)
f := hermes(home).Field("model")
if f.Get() != "anthropic/claude-opus-5" {
t.Fatalf("get: %q", f.Get())
}
if err := f.Set("magpie/deepseek/pro"); err != nil {
t.Fatal(err)
}
c, m, raw := read()
mp, _ := providers(c)["magpie"].(map[string]any)
if m["default"] != "deepseek/pro" || m["provider"] != "magpie" || m["base_url"] != "https://openrouter.ai/api/v1" ||
providers(c)["mine"] == nil || mp == nil || mp["api_mode"] != "chat_completions" || !strings.HasSuffix(mp["base_url"].(string), "/v1") ||
!strings.Contains(raw, "# mine") {
t.Fatalf("config:\n%s", raw)
}
ids := map[string]bool{}
for _, x := range mp["models"].([]any) {
ids[x.(string)] = true
}
if !ids["deepseek/pro"] || !ids["deepseek/flash"] {
t.Fatalf("models: %v", ids)
}
if f.Get() != "magpie/deepseek/pro" {
t.Fatalf("get: %q", f.Get())
}
// another magpie model keeps what was stashed first
if err := f.Set("magpie/deepseek/flash"); err != nil {
t.Fatal(err)
}
// its own model: the user's provider comes back, magpie's entry stays
// beside it, so Hermes's session picker still offers magpie's models
// and it is still connected (lijiho96940561 on X)
if err := f.Set("moonshotai/kimi-k3"); err != nil {
t.Fatal(err)
}
c, m, raw = read()
if m["default"] != "moonshotai/kimi-k3" || m["provider"] != "openrouter" || providers(c)["magpie"] == nil || providers(c)["mine"] == nil {
t.Fatalf("own:\n%s", raw)
}
a := hermes(home)
if !a.Wired() || a.Drift() != nil {
t.Fatalf("own model: wired %v, drift %+v", a.Wired(), a.Drift())
}
// the default changed in Hermes itself (`hermes model`): still connected,
// not told as drift
if err := a.Apply("model", "magpie/deepseek/flash"); err != nil {
t.Fatal(err)
}
if err := edit.SetYAML(path, edit.KV{Path: "model.provider", Value: "anthropic"}, edit.KV{Path: "model.default", Value: "claude-opus-5"}); err != nil {
t.Fatal(err)
}
if !a.Wired() || a.Drift() != nil {
t.Fatalf("changed in Hermes: wired %v, drift %+v", a.Wired(), a.Drift())
}
// Disconnect takes magpie's entry out
if err := a.Disconnect(); err != nil {
t.Fatal(err)
}
c, m, raw = read()
if m["default"] != "claude-opus-5" || m["provider"] != "anthropic" || providers(c)["magpie"] != nil || providers(c)["mine"] == nil || a.Wired() {
t.Fatalf("disconnect, own model:\n%s", raw)
}
// Disconnect from magpie's model: back to what the user had
f.Set("magpie/deepseek/flash")
if err := a.Disconnect(); err != nil {
t.Fatal(err)
}
c, m, raw = read()
if m["default"] != "claude-opus-5" || m["provider"] != "anthropic" || providers(c)["magpie"] != nil {
t.Fatalf("disconnect:\n%s", raw)
}
// a config with no provider of its own: none comes back
os.WriteFile(path, []byte("agent:\n max_turns: 60\n"), 0o644)
f.Set("magpie/deepseek/pro")
if err := a.Disconnect(); err != nil {
t.Fatal(err)
}
c, m, raw = read()
if m["provider"] != nil || m["default"] != nil || providers(c)["magpie"] != nil || c["agent"] == nil {
t.Fatalf("disconnect, no provider:\n%s", raw)
}
}
// Each of Hermes's bots is a profile under ~/.hermes/profiles/<name> with a
// config.yaml of its own, which doesn't read the main one's: connected,
// magpie is offered in every bot's picker, each keeps its own model, and
// Disconnect takes magpie out of each (lijiho96940561 on X: only the main
// bot had magpie).
func TestHermesProfiles(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)
}
root := appdir.HermesHomeIn(home)
write := func(rel, s string) string {
p := filepath.Join(root, rel)
os.MkdirAll(filepath.Dir(p), 0o755)
os.WriteFile(p, []byte(s), 0o644)
return p
}
write("config.yaml", "model:\n default: anthropic/claude-opus-5\n provider: openrouter\n")
// as `hermes profile create` leaves them: config.yaml, .env, SOUL.md
bot := write("profiles/coder/config.yaml", "# coder bot\nmodel:\n default: kimi-k3\n provider: moonshot\nproviders:\n mine:\n base_url: https://x\n")
write("profiles/coder/SOUL.md", "coder\n")
// cloned from a main config already on magpie
clone := write("profiles/writer/config.yaml", "model:\n default: deepseek/pro\n provider: magpie\n")
// deleted (a tombstone beside it), and a name Hermes doesn't allow
gone := write("profiles/old/config.yaml", "model:\n default: x\n")
write("profiles/.deleted/old", "")
bad := write("profiles/Bad/config.yaml", "model:\n default: x\n")
read := func(p string) (map[string]any, string) {
var c map[string]any
b, _ := os.ReadFile(p)
yaml.Unmarshal(b, &c)
return c, string(b)
}
has := func(p string) bool {
c, _ := read(p)
ps, _ := c["providers"].(map[string]any)
return ps["magpie"] != nil
}
a := hermes(home)
if err := a.Apply("model", "magpie/deepseek/pro"); err != nil {
t.Fatal(err)
}
c, raw := read(bot)
m, _ := c["model"].(map[string]any)
if !has(bot) || !has(clone) || m["default"] != "kimi-k3" || m["provider"] != "moonshot" || !strings.Contains(raw, "# coder bot") ||
c["providers"].(map[string]any)["mine"] == nil {
t.Fatalf("coder after connect:\n%s", raw)
}
if has(gone) || has(bad) {
t.Fatal("a deleted or invalid profile got magpie")
}
// a profile made later gets it at the next sync
later := write("profiles/later/config.yaml", "model:\n default: x\n")
if err := a.Sync(); err != nil {
t.Fatal(err)
}
if !has(later) {
t.Fatal("sync left a new profile out")
}
if err := a.Disconnect(); err != nil {
t.Fatal(err)
}
c, raw = read(bot)
m, _ = c["model"].(map[string]any)
if has(bot) || has(later) || m["default"] != "kimi-k3" || m["provider"] != "moonshot" || c["providers"].(map[string]any)["mine"] == nil {
t.Fatalf("coder after disconnect:\n%s", raw)
}
// the clone isn't left naming a provider that is gone
c, raw = read(clone)
m, _ = c["model"].(map[string]any)
if has(clone) || m["provider"] != nil || m["default"] != nil {
t.Fatalf("writer after disconnect:\n%s", raw)
}
// disconnected: a sync doesn't put it back
if err := a.Sync(); err != nil || has(bot) {
t.Fatalf("sync after disconnect: %v", err)
}
}
// HERMES_HOME set to a profile outside ~/.hermes (Docker): magpie connects
// that profile, and the root's config and its other profiles get the
// provider too
func TestHermesProfilesOfCustomRoot(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"}}); err != nil {
t.Fatal(err)
}
root := filepath.Join(home, "data")
for _, p := range []string{"config.yaml", "profiles/a/config.yaml", "profiles/b/config.yaml"} {
os.MkdirAll(filepath.Dir(filepath.Join(root, p)), 0o755)
os.WriteFile(filepath.Join(root, p), []byte("model:\n default: x\n"), 0o644)
}
t.Setenv("HERMES_HOME", filepath.Join(root, "profiles", "a"))
if got := hermesProfiles(here(home)); len(got) != 3 {
t.Fatalf("profiles: %v", got)
}
if err := hermes(home).Apply("model", "magpie/deepseek/pro"); err != nil {
t.Fatal(err)
}
for _, p := range []string{"config.yaml", "profiles/b/config.yaml"} {
v, ok := edit.GetYAML(filepath.Join(root, p), "providers.magpie.base_url")
if !ok || !strings.HasSuffix(v, "/v1") {
t.Fatalf("%s: %q", p, v)
}
}
if v, _ := edit.GetYAML(filepath.Join(root, "config.yaml"), "model.default"); v != "x" {
t.Fatalf("root model: %q", v)
}
}
// TestHermesFolder (新v学员 on Discord): on this machine Hermes's folder is
// its own default, %LOCALAPPDATA%\hermes on native Windows, not ~/.hermes
// there, and its profiles are under it; in a WSL distro, which is Linux and
// whose variables magpie can't read, it is ~/.hermes.
func TestHermesFolder(t *testing.T) {
home := t.TempDir()
local := filepath.Join(home, "AppData", "Local")
t.Setenv("LOCALAPPDATA", local)
t.Setenv("HERMES_HOME", "")
want := filepath.Join(home, ".hermes")
if runtime.GOOS == "windows" {
want = filepath.Join(local, "hermes")
}
if a := hermes(home); a.Dir != want || a.Path != filepath.Join(want, "config.yaml") {
t.Fatalf("Hermes at %s (%s), want %s", a.Dir, a.Path, want)
}
os.MkdirAll(filepath.Join(want, "profiles", "bot"), 0o755)
os.WriteFile(filepath.Join(want, "profiles", "bot", "config.yaml"), []byte("model: {}\n"), 0o644)
if got := hermesProfiles(here(home)); len(got) != 2 || got[0] != filepath.Join(want, "config.yaml") {
t.Fatalf("profiles %v, want under %s", got, want)
}
t.Setenv("HERMES_HOME", filepath.Join(home, "custom"))
wsl := place{home: home, spell: func(s string) string { return s }}
if a := hermesIn(wsl); a.Dir != filepath.Join(home, ".hermes") {
t.Fatalf("Hermes in WSL at %s, want ~/.hermes", a.Dir)
}
}
// What the user, or Hermes itself, set on magpie's provider stays through
// every sync and pick, in the connected config and in each profile's
// (#2030's siblings): the default_model `hermes model` writes there, a name,
// a timeout, a key command, a header beside magpie's. The address, key, API
// mode and model list stay magpie's.
func TestHermesKeepsTheUsersSettings(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)
}
root := appdir.HermesHomeIn(home)
path := filepath.Join(root, "config.yaml")
bot := filepath.Join(root, "profiles", "coder", "config.yaml")
os.MkdirAll(filepath.Dir(bot), 0o755)
os.WriteFile(path, []byte("model:\n default: anthropic/claude-opus-5\n provider: openrouter\n"), 0o644)
os.WriteFile(bot, []byte("model:\n default: kimi-k3\n provider: moonshot\n"), 0o644)
a := hermes(home)
f := a.Field("model")
if err := f.Set("magpie/deepseek/pro"); err != nil {
t.Fatal(err)
}
for _, p := range []string{path, bot} {
if err := edit.SetYAML(p,
edit.KV{Path: "providers.magpie.name", Value: "My GW"},
edit.KV{Path: "providers.magpie.default_model", Value: "deepseek/pro"},
edit.KV{Path: "providers.magpie.request_timeout_seconds", Value: 900},
edit.KV{Path: "providers.magpie.key_cmd", Value: "cat ~/.gw-key"},
edit.KV{Path: "providers.magpie.extra_headers.X-Mine", Value: "1"},
edit.KV{Path: "providers.magpie.base_url", Value: "http://127.0.0.1:1/v1"},
edit.KV{Path: "providers.magpie.models", Value: []string{"gone/old"}},
); err != nil {
t.Fatal(err)
}
}
if err := provider.Save(provider.Provider{ID: "kimi", Name: "Kimi", Chat: "https://api.moonshot.ai/v1", Key: "k", Models: []string{"k3"}}); err != nil {
t.Fatal(err)
}
for range 3 {
if err := a.Sync(); err != nil {
t.Fatal(err)
}
}
check := func(when string) {
t.Helper()
for _, p := range []string{path, bot} {
var c struct {
Providers map[string]struct {
Name string `yaml:"name"`
Default string `yaml:"default_model"`
Timeout int `yaml:"request_timeout_seconds"`
KeyCmd string `yaml:"key_cmd"`
Headers map[string]string `yaml:"extra_headers"`
BaseURL string `yaml:"base_url"`
APIKey string `yaml:"api_key"`
APIMode string `yaml:"api_mode"`
Models []string `yaml:"models"`
} `yaml:"providers"`
}
raw := readFile(p)
if err := yaml.Unmarshal([]byte(raw), &c); err != nil {
t.Fatalf("%v\n%s", err, raw)
}
mp := c.Providers[magpieID]
if mp.Name != "My GW" || mp.Default != "deepseek/pro" || mp.Timeout != 900 || mp.KeyCmd != "cat ~/.gw-key" || mp.Headers["X-Mine"] != "1" {
t.Errorf("%s: the user's settings not kept in %s:\n%s", when, p, raw)
}
if mp.BaseURL == "http://127.0.0.1:1/v1" || mp.APIKey == "" || mp.APIMode != "chat_completions" || mp.Headers["User-Agent"] != "hermes-agent" ||
slices.Contains(mp.Models, "gone/old") || !slices.Contains(mp.Models, "kimi/k3") {
t.Errorf("%s: magpie's own parts not written in %s:\n%s", when, p, raw)
}
}
}
check("sync")
if err := f.Set("magpie/kimi/k3"); err != nil {
t.Fatal(err)
}
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)
}
}
// PaulT on Discord: Hermes's provider written as Hermes keeps keys, the
// api_key "${MAGPIE_API_KEY}" with the key in Hermes's .env, was marked as
// needing attention. A reference names the key Hermes sends: one that
// reaches magpie is not drift and a sync keeps it; one magpie can't see is
// unknown, kept and not drift; one naming a key the gateway doesn't take
// is drift, and a sync puts magpie's key into the .env, the reference kept.
func TestHermesWithAKeyInItsEnvFile(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"))
t.Setenv("MAGPIE_API_KEY", "")
os.Unsetenv("MAGPIE_API_KEY")
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")
env := filepath.Join(dir, ".env")
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)
}
secret, err := access.Update("add-key", access.Change{Name: "hermes"})
if err != nil {
t.Fatal(err)
}
// the reporter's provider, as he wrote it
mine := "model:\n default: deepseek/pro\n provider: magpie\nproviders:\n magpie:\n name: magpie\n api_key: \"${MAGPIE_API_KEY}\"\n api_mode: chat_completions\n base_url: " + here(home).v1() + "\n"
if err := os.WriteFile(path, []byte(mine), 0o644); err != nil {
t.Fatal(err)
}
ref := func(what string) {
t.Helper()
if v, _ := edit.GetYAML(path, "providers.magpie.api_key"); v != "${MAGPIE_API_KEY}" {
t.Fatalf("%s: the user's reference was written over: %q", what, v)
}
}
for _, c := range []struct{ name, line string }{
{"a gateway key", "MAGPIE_API_KEY=" + secret},
{"magpie's own key, quoted", "export MAGPIE_API_KEY=\"" + gateway.Token + "\""},
} {
os.WriteFile(env, []byte("OPENROUTER_API_KEY=sk-or\n"+c.line+"\n"), 0o600)
if d := a.Check(); d != "" {
t.Fatalf("%s in .env is drift: %s", c.name, d)
}
if err := a.Sync(); err != nil {
t.Fatal(err)
}
ref(c.name)
if err := a.Field("model").Set("magpie/deepseek/pro"); err != nil {
t.Fatal(err)
}
ref(c.name + ", then a pick")
}
// ${env:VAR}, Hermes's other spelling, from the environment
os.Remove(env)
if err := edit.SetYAML(path, edit.KV{Path: "providers.magpie.api_key", Value: "${env:MAGPIE_API_KEY}"}); err != nil {
t.Fatal(err)
}
t.Setenv("MAGPIE_API_KEY", secret)
if d := a.Check(); d != "" {
t.Fatalf("a gateway key in the environment is drift: %s", d)
}
os.Unsetenv("MAGPIE_API_KEY")
if err := edit.SetYAML(path, edit.KV{Path: "providers.magpie.api_key", Value: "${MAGPIE_API_KEY}"}); err != nil {
t.Fatal(err)
}
// a variable only the shell sets, which magpie doesn't see: unknown
if d := a.Check(); d != "" {
t.Fatalf("a variable magpie can't see is drift: %s", d)
}
if err := a.Sync(); err != nil {
t.Fatal(err)
}
ref("a variable magpie can't see")
if _, err := os.Stat(env); err == nil {
t.Fatal("a sync made a .env for a variable magpie can't see")
}
// a key the gateway doesn't take
os.WriteFile(env, []byte("# keys\nMAGPIE_API_KEY=sk-not-this-gateways\nOTHER=1\n"), 0o600)
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)
}
ref("a key the gateway doesn't take")
b, _ := os.ReadFile(env)
if want := "# keys\nMAGPIE_API_KEY=" + gateway.Token + "\nOTHER=1\n"; string(b) != want {
t.Fatalf(".env after a sync:\n%s\nwant:\n%s", b, want)
}
if d := a.Check(); d != "" {
t.Fatalf("after a sync: %s", d)
}
// more than the one reference: magpie's key, as before
if err := edit.SetYAML(path, edit.KV{Path: "providers.magpie.api_key", Value: "x${MAGPIE_API_KEY}"}); err != nil {
t.Fatal(err)
}
if err := a.Sync(); err != nil {
t.Fatal(err)
}
if v, _ := edit.GetYAML(path, "providers.magpie.api_key"); v != gateway.Token {
t.Fatalf("a value that doesn't reach magpie 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)
}
}