Files
magpie/internal/agent/reseat_test.go
T
yetone e08902e79d agent, library: Hermes Agent's folder is %LOCALAPPDATA%\hermes on Windows, and rtk's installer is told it (新v学员 on Discord)
新v学员 runs magpie on native Windows. Library -> RTK -> Hermes Agent
failed with "rtk's installer didn't add its hook to Hermes Agent:
Config: C:\Users\admin\AppData\Local\hermes\config.yaml ...". magpie
hard-coded ~/.hermes. Hermes' own default
(hermes_constants._get_platform_default_hermes_home) is
$LOCALAPPDATA\hermes on win32 (~/AppData/Local/hermes when LOCALAPPDATA
is empty) and ~/.hermes elsewhere, with $HERMES_HOME overriding it.
rtk 0.51's resolve_hermes_home knows only $HERMES_HOME and ~/.hermes,
on every OS.

What changed:
- appdir.HermesHome / HermesHomeIn / HermesDefaultIn: $HERMES_HOME,
  else Hermes' per-OS default. This is the one place that resolves it.
- agent: hermesIn (Agents-page model wiring) and hermesProfiles use it
  through place.hermesHome / hermesDefault. A WSL distro keeps
  ~/.hermes (Linux paths, Windows env not read). The Agents-page wiring
  was wrong on native Windows too: without HERMES_HOME it wrote
  %USERPROFILE%\.hermes\config.yaml, which Hermes never reads.
- sessions.HermesDir uses it, so Windows sessions are found.
- library/rtk: the hermes spec reads and writes the agent's own folder
  (a.Dir), and rtkSpec.env passes HERMES_HOME=<that folder> to
  `rtk init -g --agent hermes --auto-patch`.
- Tests resolve the folder through appdir instead of home/.hermes. New
  TestHermesDefault/TestHermesHome (appdir), TestHermesFolder (agent:
  Windows default, profiles under it, WSL unchanged), and
  TestRTKHermesHome (library, a fake rtk that behaves like 0.51).
- docs/reference.md and docs/subsystems/library.md describe the
  Windows folder and the env.

Run:
- gofmt -l clean. go vet ./... on darwin, GOOS=linux -tags nogui and
  GOOS=windows.
- darwin: go test ./internal/appdir ./internal/library
  ./internal/sessions ./internal/agent ./internal/tui.
- TestRTKHermesHome against the old rtk.go fails with the reporter's
  exact message ("didn't add its hook ... AppData/Local/hermes/
  config.yaml"), and its default subtest fails with HERMES_HOME="".
- Windows box (cross-compiled test binaries, testenv sandbox
  USERPROFILE/LOCALAPPDATA): appdir Hermes tests, agent
  Hermes|Reseat|Effort (incl. TestHermesFolder) and library Hermes|RTK
  pass. TestRTKHermesHome skips there (shell fake). The sessions binary
  failed only TestHermesMessageVisibility, because its testdata wasn't
  copied to the box.
- Real rtk.exe 0.51.0 on the Windows box: with
  HERMES_HOME=<sandbox>\AppData\Local\hermes it wrote the plugin there
  and patched that config.yaml. Without HERMES_HOME it wrote
  C:\Users\<user>\.hermes, ignoring a sandbox USERPROFILE (rtk asks
  Windows for the home folder). That confirms the cause and the fix.
  The stray folder it made on the test box was removed.

Not run: real Hermes on Windows (no install, and running hermes under
a temp HERMES_HOME rewrites the live launcher); the GUI suite; the full
go test ./... ; the reporter's own machine.
2026-10-13 00:01:46 +08:00

240 lines
8.4 KiB
Go

package agent
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/appdir"
"github.com/yetone/magpie/internal/provider"
)
// reseatHome is a sandbox HOME with two providers, one of them "cop" in the
// place of Copilot, and Claude Code and Hermes Agent there.
func reseatHome(t *testing.T) string {
t.Helper()
home := effortHome(t)
t.Setenv("PATH", t.TempDir()) // no agent's binary is found, or run
for _, p := range []provider.Provider{
{ID: "cop", Name: "Cop", Chat: "https://cop.example/v1", Key: "k", Models: []string{"gpt-5", "claude-sonnet-4.5", "only-here"}},
{ID: "ds", Name: "DS", Chat: "https://ds.example/v1", Key: "k", Models: []string{"gpt-5", "claude-sonnet-4-5-20250929"}},
} {
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
}
os.MkdirAll(filepath.Join(home, ".claude"), 0o755)
os.MkdirAll(appdir.HermesHomeIn(home), 0o755)
return home
}
// A group Claude Code is on carries its [1m] mark in the agent's own
// settings, as a model's does. sameModel reads that ref back when the
// group's model is down to one provider and the agent has to be moved to
// the same model elsewhere, so the mark has to come off there too: leaving
// it on compares "auto-gpt-5[1m]" with "auto-gpt-5" and finds nothing,
// and an agent whose group is gone is left on it.
func TestSameModelTakesTheOneMMarkOffAGroupRef(t *testing.T) {
reseatHome(t)
for _, p := range []provider.Provider{
{ID: "other", Name: "Other", Chat: "https://other.example/v1", Key: "k", Models: []string{"gpt-5"}},
} {
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
}
opts := []Option{{Value: "magpie/group/auto-gpt-5", Ref: "group/auto-gpt-5"}, {Value: "other/gpt-5", Ref: "other/gpt-5"}}
if got := sameModel(opts, "group/auto-gpt-5[1m]"); got.Ref != "other/gpt-5" {
t.Errorf("[1m] on the group: %+v", got)
}
if got := sameModel(opts, "group/auto-gpt-5"); got.Ref != "other/gpt-5" {
t.Errorf("the bare group: %+v", got)
}
// a group that is nobody else's still gives nothing (nothing to move to)
none := []Option{{Value: "magpie/group/mine", Ref: "group/mine"}, {Value: "other/gpt-5", Ref: "other/gpt-5"}}
if got := sameModel(none, "group/mine[1m]"); got.Ref != "" {
t.Errorf("a group of the user's own: %+v", got)
}
}
func mustFind(t *testing.T, id string) *Agent {
t.Helper()
a, err := Find(id)
if err != nil {
t.Fatal(err)
}
return a
}
func mustApply(t *testing.T, a *Agent, key, v string) {
t.Helper()
if err := a.Apply(key, v); err != nil {
t.Fatalf("%s %s=%s: %v", a.ID, key, v, err)
}
if got := a.Field(key).Get(); got != v {
t.Fatalf("%s %s: %q, want %q", a.ID, key, got, v)
}
}
// #200: a provider switched off takes its models away from the agents on
// them — to the same model from a provider still on, the same model spelt
// another way, else the agent's own default — not left for the next
// request to be refused.
func TestReseatOff(t *testing.T) {
reseatHome(t)
c, h := mustFind(t, "claude"), mustFind(t, "hermes")
mustApply(t, c, "model", "cop/gpt-5")
mustApply(t, c, "opus", "cop/claude-sonnet-4.5")
mustApply(t, c, "haiku", "cop/only-here")
mustApply(t, c, "sonnet", "ds/gpt-5")
mustApply(t, h, "model", "magpie/cop/only-here")
moves, err := Reseat(func() error { return provider.SetOff("cop", true) })
if err != nil {
t.Fatal(err)
}
want := map[string]string{"model": "ds/gpt-5", "opus": "ds/claude-sonnet-4-5-20250929",
"haiku": "", "sonnet": "ds/gpt-5", "fable": ""} // haiku follows the main model; sonnet keeps the ds/gpt-5 picked for it, now the main model too (#1050)
for k, w := range want {
if got := c.Field(k).Get(); got != w {
t.Errorf("claude %s: %q, want %q", k, got, w)
}
}
if got := h.Field("model").Get(); got != "" {
t.Errorf("hermes: %q, want its own default", got)
}
got := map[string]Move{}
for _, m := range moves {
got[m.Agent+" "+m.Field] = m
}
for k, w := range map[string]Move{
"Claude Code model": {Agent: "Claude Code", Field: "model", From: "cop/gpt-5", To: "ds/gpt-5"},
"Claude Code opus": {Agent: "Claude Code", Field: "opus", From: "cop/claude-sonnet-4.5", To: "ds/claude-sonnet-4-5-20250929"},
"Claude Code haiku": {Agent: "Claude Code", Field: "haiku", From: "cop/only-here"}, // follows the main model
"Hermes Agent model": {Agent: "Hermes Agent", Field: "model", From: "cop/only-here"},
} {
if got[k] != w {
t.Errorf("%s: %+v, want %+v", k, got[k], w)
}
}
if len(moves) != 4 {
t.Errorf("moves: %+v", moves)
}
// switched on again, nothing is moved back or elsewhere
moves, err = Reseat(func() error { return provider.SetOff("cop", false) })
if err != nil || len(moves) != 0 {
t.Fatalf("on again: %+v %v", moves, err)
}
}
// A provider removed moves its agents as one switched off does; one no
// agent is on moves nothing.
func TestReseatDelete(t *testing.T) {
reseatHome(t)
c := mustFind(t, "claude")
mustApply(t, c, "model", "cop/only-here")
moves, err := Reseat(func() error { return provider.Delete("ds") })
if err != nil || len(moves) != 0 {
t.Fatalf("an unused provider: %+v %v", moves, err)
}
if moves, err = Reseat(func() error { return provider.Delete("cop") }); err != nil || len(moves) != 1 {
t.Fatalf("delete: %+v %v", moves, err)
}
// nothing else serves it: Claude Code as installed
if got := c.Field("model").Get(); got != "" {
t.Fatalf("claude: %q", got)
}
}
func TestModelKey(t *testing.T) {
for a, b := range map[string]string{
"claude-sonnet-4.5": "claude-sonnet-4-5-20250929",
"anthropic/claude-opus-4.6": "claude-opus-4-6",
"gpt-5.1-codex": "GPT-5.1-codex",
"gemini-2.5-pro-2025-06-17": "gemini-2.5-pro",
} {
if modelKey(a) != modelKey(b) {
t.Errorf("%s ≠ %s", a, b)
}
}
if modelKey("gpt-5") == modelKey("gpt-5-mini") {
t.Error("gpt-5 = gpt-5-mini")
}
}
// An agent that can't be moved (its config unwritable) doesn't fail the
// change, which is made already: the others are moved and it is a move with
// its error. An error here kept a removed provider's editor open in the
// panel, the provider still listed, and a second Remove said "no provider".
func TestReseatUnmovable(t *testing.T) {
home := reseatHome(t)
c, h := mustFind(t, "claude"), mustFind(t, "hermes")
mustApply(t, c, "model", "cop/only-here")
mustApply(t, h, "model", "magpie/cop/only-here")
dir := appdir.HermesHomeIn(home)
if err := os.Chmod(dir, 0o555); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { os.Chmod(dir, 0o755) })
if f, err := os.Create(filepath.Join(dir, "probe")); err == nil {
f.Close()
t.Skip("a read-only folder is writable here (root?)")
}
os.Chmod(filepath.Join(dir, "config.yaml"), 0o444)
moves, err := Reseat(func() error { return provider.Delete("cop") })
if err != nil {
t.Fatalf("the provider is removed, but Reseat said: %v", err)
}
if _, err := provider.Find("cop"); err == nil {
t.Fatal("cop still there")
}
if got := c.Field("model").Get(); got != "" {
t.Errorf("claude not moved: %q", got)
}
var stuck *Move
for i, m := range moves {
if m.Agent == "Hermes Agent" {
stuck = &moves[i]
}
}
if len(moves) != 2 || stuck == nil || stuck.Error == "" || stuck.To != "" || stuck.From != "cop/only-here" {
t.Fatalf("moves: %+v", moves)
}
if s := stuck.String(); !strings.Contains(s, "not moved") {
t.Errorf("String: %q", s)
}
}
// Found groups turned off (蓝猫 on Discord) take every group/auto-… away:
// an agent on one is moved to its model from the first provider still
// serving it, however that one spells it, rather than to its own default.
func TestReseatFoundGroupsOff(t *testing.T) {
reseatHome(t)
c, h := mustFind(t, "claude"), mustFind(t, "hermes")
mustApply(t, c, "model", "group/auto-gpt-5")
mustApply(t, c, "opus", "group/auto-claude-sonnet-4-5")
mustApply(t, h, "model", "magpie/group/auto-gpt-5")
moves, err := Reseat(func() error { return provider.SetAutoGroups(false) })
if err != nil {
t.Fatal(err)
}
for k, w := range map[string]string{"model": "cop/gpt-5", "opus": "cop/claude-sonnet-4.5"} {
if got := c.Field(k).Get(); got != w {
t.Errorf("claude %s: %q, want %q", k, got, w)
}
}
if got := h.Field("model").Get(); got != "magpie/cop/gpt-5" {
t.Errorf("hermes: %q", got)
}
if len(moves) != 3 || moves[0].From != "group/auto-gpt-5" || moves[0].To != "cop/gpt-5" {
t.Errorf("moves: %+v", moves)
}
// on again, nobody is moved back or elsewhere
if moves, err = Reseat(func() error { return provider.SetAutoGroups(true) }); err != nil || len(moves) != 0 {
t.Fatalf("on again: %+v %v", moves, err)
}
}