diff --git a/docs/subsystems/agent-wiring.md b/docs/subsystems/agent-wiring.md index 7302b60cd..bf40f7a69 100644 --- a/docs/subsystems/agent-wiring.md +++ b/docs/subsystems/agent-wiring.md @@ -49,6 +49,7 @@ something else shows up as drift. - Qoder's `providers.magpie` keeps `routing`, the timeouts, `type`, `authType`, `anthropic` and a non-empty `displayName` (`qoderKept`). - fx keeps `reviewer_model` and a `tool_choice_mode` of `omit`. Its `auth` is magpie's whole, since fx refuses an `env` beside `type: none` (`fxKept`). - Hermes keeps the `default_model` that `hermes model` writes there, `key_cmd`, the timeouts, `extra_body`, a name and headers beside magpie's (`hermesKept`). This holds in every profile. + - Hermes copies the launch profile's model into a new profile (`_seed_model_config`, or `--clone`'s `config.yaml`) and never reads it again. A model picked in magpie moves every other profile still on `magpie` with the model magpie had set (recorded in `stash.json` as `hermes::magpie.default`, or the connected config's model before that record) onto the new one; a profile on any other model was set by its user and stays (`hermesFollow` in [`hermes.go`](../../internal/agent/hermes.go); tjhhhh_84378 on Discord). - Hermes's `api_key` may be a reference, `${VAR}` or `${env:VAR}`, which Hermes expands from the `.env` beside the config (its own or a profile's) and then the environment. `Check` and a sync read the key it names (`hermesKey`): one that holds (`keyHolds`) stays and isn't drift, and so does one naming a variable magpie can't see (only a shell profile sets it), which is unknown. One naming a key the gateway doesn't take is drift, and a sync keeps the reference and writes magpie's key into that `.env` under the variable, made `0600` when there is none (`hermesKeptKey`; PaulT on Discord, `TestHermesWithAKeyInItsEnvFile`). A value with more than the one reference in it is replaced by magpie's key, as before. - omp's `models.yml` keeps `modelOverrides`, `discovery`, `disableStrictTools`, headers and compat flags (`ompKept`). Its `auth` and `apiKey` are magpie's, or gone. - Kimi Code's `[providers.magpie]` has magpie's three keys set in place, so `custom_headers`, `env` and `default_model` stay (`kimiProvider`). diff --git a/internal/agent/hermes.go b/internal/agent/hermes.go index b1bd7a73c..60068e33c 100644 --- a/internal/agent/hermes.go +++ b/internal/agent/hermes.go @@ -20,7 +20,10 @@ package agent // into every profile's too, so magpie is offered in every bot's picker, not // the main one's alone (lijiho96940561 on X). A profile's own model and // provider are left as its user set them, and Disconnect takes the provider -// out of each. +// out of each. A profile still on the model magpie last set in the +// connected config (one Hermes seeded or cloned from it) follows the next +// one magpie sets there (hermesFollow; tjhhhh_84378 on Discord: another +// bot kept the old model). import ( "errors" @@ -57,6 +60,9 @@ func hermesIn(at place) *Agent { return errors.Join(errs...) } key := "hermes:" + path + ":" + // setKey holds the model magpie last set in the connected config, + // which the profiles still on it follow + setKey := key + "magpie.default" getKey := func(k string) string { v, _ := edit.GetYAML(path, k); return v } onMagpie := func() bool { return getKey("model.provider") == magpieID } // joined: magpie's provider is in providers, though the model is one of @@ -108,6 +114,7 @@ func hermesIn(at place) *Agent { if joined() { errs = append(errs, edit.DelYAML(path, "providers."+magpieID)) } + forget(setKey) for _, p := range hermesOthers(at, dir) { errs = append(errs, hermesUnwireProfile(p)) } @@ -155,6 +162,12 @@ func hermesIn(at place) *Agent { return edit.DelYAML(path, "model.default") } if ref, ok := strings.CutPrefix(v, magpieID+"/"); ok && isMagpie(ref) { + // the model magpie had set: recorded, or, before the + // record was kept, the one the config is on through magpie + was := stashLoad()[setKey] + if was == "" && onMagpie() { + was = getKey("model.default") + } if !onMagpie() { stash(map[string]string{ key + "model.provider": getKey("model.provider"), @@ -168,7 +181,8 @@ func hermesIn(at place) *Agent { ); err != nil { return err } - return spread() + stash(map[string]string{setKey: ref}) + return errors.Join(spread(), hermesFollow(at, dir, was, ref)) } if onMagpie() { if err := restore(); err != nil { @@ -255,6 +269,37 @@ func hermesOthers(at place, dir string) []string { return out } +// hermesFollow moves every profile but the one at dir that is on magpie's +// model was, the one magpie had set in the connected config, onto ref, the +// one it sets now. Hermes gives a new profile a copy of the launch +// profile's model (_seed_model_config, or --clone's config.yaml) and never +// reads it again, so without this each bot stayed on the model magpie set +// when it was made. A profile on another model, magpie's or its own, was +// set there by its user and is left alone. +func hermesFollow(at place, dir, was, ref string) error { + if was == "" || was == ref { + return nil + } + var errs []error + for _, p := range hermesOthers(at, dir) { + if v, _ := edit.GetYAML(p, "model.provider"); v != magpieID { + continue + } + k := "model.default" + v, _ := edit.GetYAML(p, k) + if v == "" { + // Hermes reads model.model when default is unset + // (_read_config_model) + k = "model.model" + v, _ = edit.GetYAML(p, k) + } + if v == was { + errs = append(errs, edit.SetYAML(p, edit.KV{Path: k, Value: ref})) + } + } + return errors.Join(errs...) +} + // hermesUnwireProfile takes magpie's provider out of a profile's config. A // profile whose model is on magpie (one cloned from a connected main // config, or picked in that bot) would be left naming a provider that is diff --git a/internal/agent/hermes_test.go b/internal/agent/hermes_test.go index 495db7e2f..d96203494 100644 --- a/internal/agent/hermes_test.go +++ b/internal/agent/hermes_test.go @@ -558,3 +558,86 @@ func TestKeyHoldsTakesAGatewayKey(t *testing.T) { t.Fatalf("wiringOff: %s", d) } } + +// A bot Hermes seeded or cloned from the connected config holds a copy of +// the model magpie set there and never reads the main config again: it +// follows the next model magpie sets, while a bot on a model its user +// picked keeps it (tjhhhh_84378 on Discord: another profile still showed +// the old model). +func TestHermesProfilesFollowMagpieModel(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", "max", "lite"}}); 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 + } + model := func(p string) (string, string) { + var c struct { + Model struct{ Default, Model, Provider string } + } + b, _ := os.ReadFile(p) + yaml.Unmarshal(b, &c) + return c.Model.Default + c.Model.Model, c.Model.Provider + } + // connected before magpie kept a record of the model it set + write("config.yaml", "model:\n default: deepseek/pro\n provider: magpie\n") + // seeded by `hermes profile create` from that config + seeded := write("profiles/writer/config.yaml", "# writer bot\nmodel:\n default: deepseek/pro\n provider: magpie\n") + // a model its user picked in that bot, magpie's or its own + picked := write("profiles/coder/config.yaml", "model:\n default: deepseek/lite\n provider: magpie\n") + own := write("profiles/chat/config.yaml", "model:\n default: deepseek/pro\n provider: deepseek\n") + // model.model, which Hermes reads with default unset + old := write("profiles/old/config.yaml", "model:\n model: deepseek/pro\n provider: magpie\n") + + a := hermes(home) + if err := a.Apply("model", "magpie/deepseek/flash"); err != nil { + t.Fatal(err) + } + check := func(when, p, want, wantProv string) { + t.Helper() + if m, prov := model(p); m != want || prov != wantProv { + b, _ := os.ReadFile(p) + t.Fatalf("%s: %s is on %s/%s, want %s/%s:\n%s", when, filepath.Base(filepath.Dir(p)), prov, m, wantProv, want, b) + } + } + check("flash", seeded, "deepseek/flash", "magpie") + check("flash", old, "deepseek/flash", "magpie") + check("flash", picked, "deepseek/lite", "magpie") + check("flash", own, "deepseek/pro", "deepseek") + if b, _ := os.ReadFile(seeded); !strings.Contains(string(b), "# writer bot") { + t.Fatalf("writer lost its comment:\n%s", b) + } + + // the connected config on a model of its own between: the bots on + // magpie's last model still follow the next one magpie sets + if err := a.Apply("model", "moonshotai/kimi-k3"); err != nil { + t.Fatal(err) + } + check("own", seeded, "deepseek/flash", "magpie") + if err := a.Apply("model", "magpie/deepseek/max"); err != nil { + t.Fatal(err) + } + check("max", seeded, "deepseek/max", "magpie") + check("max", old, "deepseek/max", "magpie") + check("max", picked, "deepseek/lite", "magpie") + check("max", own, "deepseek/pro", "deepseek") + + // a bot whose user moves it off magpie's model, onto the one magpie + // had before, keeps it + edit.SetYAML(seeded, edit.KV{Path: "model.default", Value: "deepseek/flash"}) + if err := a.Apply("model", "magpie/deepseek/pro"); err != nil { + t.Fatal(err) + } + check("pro", seeded, "deepseek/flash", "magpie") + check("pro", old, "deepseek/pro", "magpie") +}