diff --git a/docs/subsystems/agent-wiring.md b/docs/subsystems/agent-wiring.md index b18145b67..30160ddb9 100644 --- a/docs/subsystems/agent-wiring.md +++ b/docs/subsystems/agent-wiring.md @@ -51,6 +51,8 @@ something else shows up as drift. - 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). + - The config magpie connects is the profile `hermes` starts in: `HERMES_HOME` when it names a profile, else the one `hermes profile use` made sticky in `/active_profile` (a live profile folder; `hermes model` writes the same one), else `HERMES_HOME` or the default folder (`hermesActive`; `_apply_profile_override` in Hermes's `hermes_cli/main.py`). The root's config is then one more profile. A bot on the model the connected config is on through magpie follows too, besides one on the recorded model (`hermes model` there before the bot was made). + - Disconnect moves a profile on magpie onto its own model from when magpie connected it, else the connected config's own model (with that provider's `providers` entry when the profile lacks it, as Hermes's `launch_model_seed` does); with neither, its model goes. What it was on is kept (`hermes::off`), and connecting again puts it back when nothing has changed it, and gives a profile with no model at all magpie's (`hermesBack`). The recorded model stays through Disconnect. Before, Off emptied every such profile and `hermes` there failed at start after Off and On (tjhhhh on Discord, `TestHermesOffAndOnKeepsEveryBotsModel`, `TestHermesStickyProfile`). - 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/gone.go b/internal/agent/gone.go index 2f3dab212..5aa369a73 100644 --- a/internal/agent/gone.go +++ b/internal/agent/gone.go @@ -64,6 +64,11 @@ func (a *Agent) cliHeld() (held, known bool) { at = []string{filepath.Join(home, ".codex", "packages", "standalone")} case "hermes": at = []string{filepath.Join(a.Dir, "hermes-agent")} + // a profile magpie connects (hermesActive): the checkout is in + // the root its profiles are under + if filepath.Base(filepath.Dir(a.Dir)) == "profiles" { + at = append(at, filepath.Join(filepath.Dir(filepath.Dir(a.Dir)), "hermes-agent")) + } } for _, p := range at { if _, err := os.Stat(p); err == nil { diff --git a/internal/agent/hermes.go b/internal/agent/hermes.go index 60068e33c..54e82941b 100644 --- a/internal/agent/hermes.go +++ b/internal/agent/hermes.go @@ -23,9 +23,12 @@ package agent // 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). +// bot kept the old model). The config magpie connects is the profile +// `hermes` starts in (hermesActive), and Disconnect leaves each bot on a +// model it can start with, which connecting again puts back (hermesBack). import ( + "encoding/json" "errors" "os" "path/filepath" @@ -35,6 +38,7 @@ import ( "github.com/yetone/magpie/internal/appdir" "github.com/yetone/magpie/internal/dirent" "github.com/yetone/magpie/internal/edit" + "gopkg.in/yaml.v3" ) func hermes(home string) *Agent { return hermesIn(here(home)) } @@ -43,7 +47,7 @@ func hermes(home string) *Agent { return hermesIn(here(home)) } // (see wsl.go), where HERMES_HOME isn't read and its provider names the // gateway as the distro reaches it. func hermesIn(at place) *Agent { - dir := at.hermesHome() + dir := hermesActive(at) path := filepath.Join(dir, "config.yaml") hermesProvider := func(p string) any { return hermesKept(at, p, hermesProviderAt(at.gw())) } // spread puts magpie's provider, as it is now, into every other @@ -111,12 +115,20 @@ func hermesIn(at place) *Agent { // magpie's, goes back by the field Unwire: func() error { var errs []error + // the model the user had in the connected config, which a bot + // on magpie goes onto, read before the field's Set puts it back + // and forgets it; while the config is on a model of its own, that + // one. The model magpie set (setKey) stays recorded, for the bots + // put back on it to follow the next one once connected again. + own := hermesModel{Provider: stashLoad()[key+"model.provider"], Default: stashLoad()[key+"model.default"]} + if !onMagpie() { + own = hermesModel{Provider: getKey("model.provider"), Default: getKey("model.default")} + } if joined() { errs = append(errs, edit.DelYAML(path, "providers."+magpieID)) } - forget(setKey) for _, p := range hermesOthers(at, dir) { - errs = append(errs, hermesUnwireProfile(p)) + errs = append(errs, hermesUnwireProfile(path, p, own)) } return errors.Join(errs...) }, @@ -164,9 +176,13 @@ func hermesIn(at place) *Agent { 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 + // and the one it is on through magpie now, which Hermes + // seeds a new bot with too, picked there by `hermes + // model` or before the record was kept was := stashLoad()[setKey] - if was == "" && onMagpie() { - was = getKey("model.default") + now := "" + if onMagpie() { + now = getKey("model.default") } if !onMagpie() { stash(map[string]string{ @@ -182,7 +198,7 @@ func hermesIn(at place) *Agent { return err } stash(map[string]string{setKey: ref}) - return errors.Join(spread(), hermesFollow(at, dir, was, ref)) + return errors.Join(spread(), hermesBack(at, dir, ref), hermesFollow(at, dir, was, now, ref)) } if onMagpie() { if err := restore(); err != nil { @@ -226,6 +242,27 @@ var hermesProfileID = regexp.MustCompile(`^[a-z0-9][a-z0-9_-]{0,63}$`) // /profiles/, a profile of (get_default_hermes_root). // A profile with no config.yaml yet is left out: magpie doesn't make one. func hermesProfiles(at place) []string { + root := hermesRoot(at) + paths := []string{filepath.Join(root, "config.yaml")} + ps := filepath.Join(root, "profiles") + es, _ := os.ReadDir(ps) + for _, e := range es { + name := e.Name() + if !dirent.IsFolder(ps, e) || !hermesLive(root, name) { + continue + } + if st, err := os.Stat(filepath.Join(ps, name, "config.yaml")); err == nil && st.Mode().IsRegular() { + paths = append(paths, filepath.Join(ps, name, "config.yaml")) + } + } + return paths +} + +// hermesRoot is the folder Hermes's profiles are under +// (get_default_hermes_root): its default folder, which a HERMES_HOME under +// it is a profile of; a HERMES_HOME elsewhere is the root itself, or, as +// /profiles/, a profile of . +func hermesRoot(at place) string { root := at.hermesDefault() if env := at.getenv("HERMES_HOME"); env != "" { if rel, err := filepath.Rel(root, env); err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) { @@ -235,22 +272,47 @@ func hermesProfiles(at place) []string { } } } - paths := []string{filepath.Join(root, "config.yaml")} - ps := filepath.Join(root, "profiles") - es, _ := os.ReadDir(ps) - for _, e := range es { - name := e.Name() - if !dirent.IsFolder(ps, e) || name == "default" || !hermesProfileID.MatchString(name) { - continue - } - if _, err := os.Stat(filepath.Join(ps, ".deleted", name)); err == nil { - continue - } - if st, err := os.Stat(filepath.Join(ps, name, "config.yaml")); err == nil && st.Mode().IsRegular() { - paths = append(paths, filepath.Join(ps, name, "config.yaml")) - } + return root +} + +// hermesLive is whether name is a profile of root Hermes runs: a name it +// allows, not "default" (the root's own), not deleted (a tombstone at +// profiles/.deleted/) +func hermesLive(root, name string) bool { + if name == "default" || !hermesProfileID.MatchString(name) { + return false } - return paths + _, err := os.Stat(filepath.Join(root, "profiles", ".deleted", name)) + return err != nil +} + +// hermesActive is the folder of the profile `hermes` starts in, whose +// config.yaml magpie connects: HERMES_HOME when it names a profile +// (/profiles/); else the profile `hermes profile use ` +// made sticky in /active_profile, when it is there; else HERMES_HOME +// or Hermes's default folder (hermes_cli/main.py _apply_profile_override). +// `hermes model` writes the same profile's config. With a sticky profile, +// magpie wrote the root's config, which `hermes` doesn't read, so a model +// picked in magpie didn't reach the bot the user runs (tjhhhh on Discord). +func hermesActive(at place) string { + home := at.hermesHome() + if at.getenv("HERMES_HOME") != "" && filepath.Base(filepath.Dir(home)) == "profiles" { + return home + } + root := hermesRoot(at) + b, err := os.ReadFile(filepath.Join(root, "active_profile")) + if err != nil { + return home + } + name := strings.TrimSpace(strings.TrimPrefix(string(b), "\ufeff")) + dir := filepath.Join(root, "profiles", name) + if !hermesLive(root, name) { + return home + } + if st, err := os.Stat(dir); err != nil || !st.IsDir() { + return home + } + return dir } // hermesOthers is every profile's config but the one at dir, which magpie @@ -276,8 +338,9 @@ func hermesOthers(at place, dir string) []string { // 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 { +func hermesFollow(at place, dir, was, now, ref string) error { + from := func(v string) bool { return v != "" && v != ref && (v == was || v == now) } + if !from(was) && !from(now) { return nil } var errs []error @@ -293,27 +356,135 @@ func hermesFollow(at place, dir, was, ref string) error { k = "model.model" v, _ = edit.GetYAML(p, k) } - if v == was { + if from(v) { errs = append(errs, edit.SetYAML(p, edit.KV{Path: k, Value: ref})) } } return errors.Join(errs...) } +// hermesModel is a config's model: model.provider, and model.default, or +// model.model, which Hermes reads with default unset (Key says which) +type hermesModel struct { + Provider string `json:"provider,omitempty"` + Default string `json:"default,omitempty"` + Key string `json:"key,omitempty"` +} + +func hermesModelOf(p string) hermesModel { + m := hermesModel{Key: "model.default"} + m.Provider, _ = edit.GetYAML(p, "model.provider") + m.Default, _ = edit.GetYAML(p, "model.default") + if m.Default == "" { + if v, _ := edit.GetYAML(p, "model.model"); v != "" { + m.Default, m.Key = v, "model.model" + } + } + return m +} + +// hermesOffKey, after a profile's config path, holds in the stash the +// model on magpie Disconnect took the profile off and what it left there +// (hermesOff), for connecting again to put back +func hermesOffKey(p string) string { return "hermes:" + p + ":off" } + +type hermesOff struct { + Magpie hermesModel `json:"magpie"` + Left hermesModel `json:"left"` +} + // 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 -// gone, so its provider and model go too, and Hermes takes its own -// defaults, as the main config does with nothing stashed. -func hermesUnwireProfile(p string) error { +// profile whose model is on magpie (one Hermes seeded or cloned from a +// connected config, or picked in that bot) would be left naming a provider +// that is gone. It goes back to its own model from before, when magpie once +// connected this profile (the stash under its path), else onto own, the +// model the user had in the connected config (path), with that provider's +// entry when the profile lacks it, as Hermes seeds a new profile +// (launch_model_seed). With no model of the user's anywhere its model goes, +// and Hermes takes its own defaults, as the connected config does. Before, +// it lost its model every time, and `hermes` in that bot failed at start +// after magpie was switched off and on again (tjhhhh on Discord). What it +// was on is kept for hermesBack. +func hermesUnwireProfile(path, p string, own hermesModel) error { if edit.GetYAMLMap(p, "providers."+magpieID) == nil { return nil } - del := []string{"providers." + magpieID} - if v, _ := edit.GetYAML(p, "model.provider"); v == magpieID { - del = append(del, "model.provider", "model.default") + cur := hermesModelOf(p) + if cur.Provider != magpieID { + return edit.DelYAML(p, "providers."+magpieID) } - return edit.DelYAML(p, del...) + k := "hermes:" + p + ":" + st := stashLoad() + if mine := (hermesModel{Provider: st[k+"model.provider"], Default: st[k+"model.default"]}); mine.Provider != "" || mine.Default != "" { + own = mine + forget(k+"model.provider", k+"model.default") + } + if err := edit.DelYAML(p, "providers."+magpieID, "model.provider", "model.default", "model.model"); err != nil { + return err + } + var kvs []edit.KV + if own.Provider != "" { + kvs = append(kvs, edit.KV{Path: "model.provider", Value: own.Provider}) + if edit.GetYAMLMap(p, "providers."+own.Provider) == nil { + if e := hermesEntry(path, own.Provider); e != nil { + kvs = append(kvs, edit.KV{Path: "providers." + own.Provider, Value: e}) + } + } + } + if own.Default != "" { + kvs = append(kvs, edit.KV{Path: "model.default", Value: own.Default}) + } + if len(kvs) > 0 { + if err := edit.SetYAML(p, kvs...); err != nil { + return err + } + } + b, _ := json.Marshal(hermesOff{Magpie: cur, Left: hermesModelOf(p)}) + stash(map[string]string{hermesOffKey(p): string(b)}) + return nil +} + +// hermesEntry is the entry under providers named name in the config at +// path, whole, or nil +func hermesEntry(path, name string) any { + b, err := os.ReadFile(path) + if err != nil { + return nil + } + var c struct { + Providers map[string]any `yaml:"providers"` + } + if yaml.Unmarshal(b, &c) != nil { + return nil + } + return c.Providers[name] +} + +// hermesBack, as magpie connects Hermes again, puts every other profile +// Disconnect took off magpie back on the model it was on, when nothing has +// changed it since. A profile with no model at all (one an older magpie's +// Disconnect emptied, or made from a config with none), which `hermes` +// can't start in, gets ref, the one magpie sets, as Hermes would seed it. +func hermesBack(at place, dir, ref string) error { + var errs []error + for _, p := range hermesOthers(at, dir) { + cur := hermesModelOf(p) + var off hermesOff + if raw := unstash(hermesOffKey(p)); raw != "" && json.Unmarshal([]byte(raw), &off) == nil && off.Magpie.Default != "" { + if cur.Provider != off.Left.Provider || cur.Default != off.Left.Default { + continue + } + if off.Magpie.Key == "model.model" { + errs = append(errs, edit.DelYAML(p, "model.default")) + } + errs = append(errs, edit.SetYAML(p, edit.KV{Path: "model.provider", Value: magpieID}, edit.KV{Path: off.Magpie.Key, Value: off.Magpie.Default})) + continue + } + if v, ok := edit.GetYAML(p, "model"); cur.Provider == "" && cur.Default == "" && (!ok || v == "") { + errs = append(errs, edit.SetYAML(p, edit.KV{Path: "model.provider", Value: magpieID}, edit.KV{Path: "model.default", Value: ref})) + } + } + return errors.Join(errs...) } type hermesProviderEntry struct { diff --git a/internal/agent/hermes_test.go b/internal/agent/hermes_test.go index d96203494..6f6a30377 100644 --- a/internal/agent/hermes_test.go +++ b/internal/agent/hermes_test.go @@ -201,10 +201,12 @@ func TestHermesProfiles(t *testing.T) { 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 + // the clone isn't left naming a provider that is gone: it goes onto + // the model the user had in the main config, as Hermes would have + // seeded it from there c, raw = read(clone) m, _ = c["model"].(map[string]any) - if has(clone) || m["provider"] != nil || m["default"] != nil { + if has(clone) || m["provider"] != "openrouter" || m["default"] != "anthropic/claude-opus-5" { t.Fatalf("writer after disconnect:\n%s", raw) } // disconnected: a sync doesn't put it back @@ -641,3 +643,198 @@ func TestHermesProfilesFollowMagpieModel(t *testing.T) { check("pro", seeded, "deepseek/flash", "magpie") check("pro", old, "deepseek/pro", "magpie") } + +// Switching magpie off and on again leaves every bot on a model `hermes` +// starts with (tjhhhh on Discord: after Off and On the bot's default model +// was gone and the hermes CLI failed at start). Off puts a bot on magpie +// onto the model the user had in the main config, with that provider's +// entry, or onto its own from when magpie connected it; On puts it back on +// the model it was on. A bot the user changed in between keeps their pick, +// and a bot an older magpie left with no model gets magpie's. +func TestHermesOffAndOnKeepsEveryBotsModel(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", "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 + } + main := write("config.yaml", "model:\n default: kimi-k3\n provider: mine\nproviders:\n mine:\n base_url: https://x/v1\n api_key: sk-mine\n") + a := hermes(home) + if c, err := a.ConnectHow(); err != nil || !a.Wired() { + t.Fatalf("connect: %+v %v", c, err) + } + ref := hermesModelOf(main).Default + // seeded from the connected config, picked in the bot, the bot's own + seeded := write("profiles/writer/config.yaml", "model:\n default: "+ref+"\n provider: magpie\n") + picked := write("profiles/coder/config.yaml", "model:\n model: deepseek/lite\n provider: magpie\n") + own := write("profiles/chat/config.yaml", "model:\n default: gpt-9\n provider: openai\n") + // emptied by an older magpie's Off + empty := write("profiles/empty/config.yaml", "agent:\n max_turns: 9\n") + if err := a.Sync(); err != nil { + t.Fatal(err) + } + check := func(when, p string, want hermesModel) { + t.Helper() + if got := hermesModelOf(p); got.Provider != want.Provider || got.Default != want.Default { + b, _ := os.ReadFile(p) + t.Fatalf("%s: %s is on %+v, want %+v:\n%s", when, filepath.Base(filepath.Dir(p)), got, want, b) + } + } + + if err := a.Disconnect(); err != nil { + t.Fatal(err) + } + check("off", main, hermesModel{Provider: "mine", Default: "kimi-k3"}) + check("off", seeded, hermesModel{Provider: "mine", Default: "kimi-k3"}) + check("off", picked, hermesModel{Provider: "mine", Default: "kimi-k3"}) + check("off", own, hermesModel{Provider: "openai", Default: "gpt-9"}) + // the provider the bot is put on is there for it, as Hermes seeds it + if v, _ := edit.GetYAML(seeded, "providers.mine.base_url"); v != "https://x/v1" { + t.Fatalf("writer has no entry for its provider:\n%s", func() []byte { b, _ := os.ReadFile(seeded); return b }()) + } + if v, _ := edit.GetYAML(picked, "model.model"); v != "" { + t.Fatalf("coder keeps magpie's model.model %q", v) + } + + // the user moves the coder bot to a model of their own while off + edit.SetYAML(picked, edit.KV{Path: "model.default", Value: "gpt-9"}, edit.KV{Path: "model.provider", Value: "openai"}) + + if c, err := a.ConnectHow(); err != nil || !a.Wired() { + t.Fatalf("connect again: %+v %v", c, err) + } + check("on", main, hermesModel{Provider: "magpie", Default: ref}) + check("on", seeded, hermesModel{Provider: "magpie", Default: ref}) + check("on", picked, hermesModel{Provider: "openai", Default: "gpt-9"}) + check("on", own, hermesModel{Provider: "openai", Default: "gpt-9"}) + check("on", empty, hermesModel{Provider: "magpie", Default: ref}) + if v, _ := edit.GetYAML(empty, "agent.max_turns"); v != "9" { + t.Fatal("the empty bot lost its other settings") + } + + // the bots put back still follow the next model magpie sets + next := "deepseek/flash" + if ref == next { + next = "deepseek/pro" + } + if err := a.Apply("model", "magpie/"+next); err != nil { + t.Fatal(err) + } + check("next", seeded, hermesModel{Provider: "magpie", Default: next}) +} + +// A model changed in Hermes itself (`hermes model` in the main config) +// onto another of magpie's is what a bot made after holds: such a bot +// follows the next model magpie sets too, not only one on the model magpie +// had recorded (tjhhhh on Discord: switching for the other bots didn't +// take). +func TestHermesBotsFollowTheModelHermesPicked(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"}}); err != nil { + t.Fatal(err) + } + root := appdir.HermesHomeIn(home) + os.MkdirAll(root, 0o755) + main := filepath.Join(root, "config.yaml") + os.WriteFile(main, []byte("model:\n default: x\n provider: openrouter\n"), 0o644) + a := hermes(home) + if err := a.Apply("model", "magpie/deepseek/pro"); err != nil { + t.Fatal(err) + } + // `hermes model` in the main config, then a bot made from it + edit.SetYAML(main, edit.KV{Path: "model.default", Value: "deepseek/flash"}) + bot := filepath.Join(root, "profiles", "writer", "config.yaml") + os.MkdirAll(filepath.Dir(bot), 0o755) + os.WriteFile(bot, []byte("model:\n default: deepseek/flash\n provider: magpie\n"), 0o644) + if err := a.Apply("model", "magpie/deepseek/max"); err != nil { + t.Fatal(err) + } + if v, _ := edit.GetYAML(bot, "model.default"); v != "deepseek/max" { + b, _ := os.ReadFile(bot) + t.Fatalf("writer stayed:\n%s", b) + } +} + +// `hermes profile use coder` makes coder the bot `hermes` starts in +// (/active_profile), and `hermes model` writes its config: magpie +// connects that one, so a model picked in magpie reaches the bot the user +// runs (tjhhhh on Discord), and the main config, another bot now, keeps the +// model the user picked there. A sticky name that is gone, deleted or not +// Hermes's, and a HERMES_HOME naming a profile, leave it as before. +func TestHermesStickyProfile(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) + } + 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 + } + main := write("config.yaml", "model:\n default: kimi-k3\n provider: moonshot\n") + coder := write("profiles/coder/config.yaml", "model:\n default: gpt-9\n provider: openai\n") + write("profiles/old/config.yaml", "model:\n default: x\n") + write("profiles/.deleted/old", "") + + for _, c := range []struct{ sticky, want string }{ + {"coder\n", filepath.Join(root, "profiles", "coder")}, + {"\ufeffcoder", filepath.Join(root, "profiles", "coder")}, + {"default\n", root}, + {"", root}, + {"gone\n", root}, + {"old\n", root}, + {"../x\n", root}, + } { + write("active_profile", c.sticky) + if got := hermes(home).Dir; got != c.want { + t.Errorf("active_profile %q: connects %s, want %s", c.sticky, got, c.want) + } + } + t.Setenv("HERMES_HOME", filepath.Join(root, "profiles", "old")) + write("active_profile", "coder\n") + if got := hermes(home).Dir; got != filepath.Join(root, "profiles", "old") { + t.Errorf("HERMES_HOME naming a profile: connects %s", got) + } + t.Setenv("HERMES_HOME", "") + + a := hermes(home) + if err := a.Apply("model", "magpie/deepseek/pro"); err != nil { + t.Fatal(err) + } + if m := hermesModelOf(coder); m.Provider != "magpie" || m.Default != "deepseek/pro" { + t.Fatalf("the bot hermes starts in: %+v", m) + } + if m := hermesModelOf(main); m.Provider != "moonshot" || m.Default != "kimi-k3" { + t.Fatalf("main: %+v", m) + } + if v, _ := edit.GetYAML(main, "providers.magpie.base_url"); v == "" { + t.Fatal("main has no magpie in its picker") + } + if err := a.Disconnect(); err != nil { + t.Fatal(err) + } + if m := hermesModelOf(coder); m.Provider != "openai" || m.Default != "gpt-9" { + t.Fatalf("coder after Off: %+v", m) + } +} diff --git a/internal/agent/install_missing_test.go b/internal/agent/install_missing_test.go index 3e5c7cf5f..3e90a9f4b 100644 --- a/internal/agent/install_missing_test.go +++ b/internal/agent/install_missing_test.go @@ -124,6 +124,13 @@ func TestCLIMissingHermes(t *testing.T) { if h.CLIMissing() { t.Fatal("hermes with its checkout taken for missing") } + // connected in the profile `hermes profile use` made sticky: the + // checkout is in the root + bot := filepath.Join(dir, "profiles", "coder") + writeT(t, filepath.Join(bot, "config.yaml"), "model: {}\n") + if p := (&Agent{ID: "hermes", Name: "Hermes Agent", Bin: h.Bin, Dir: bot, Path: filepath.Join(bot, "config.yaml")}); p.CLIMissing() { + t.Fatal("hermes in a profile, with the root's checkout, taken for missing") + } if err := os.RemoveAll(filepath.Join(dir, "hermes-agent")); err != nil { t.Fatal(err) }