Files
magpie/internal/library/hermes_mcp_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

151 lines
5.3 KiB
Go

package library
import (
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/appdir"
"gopkg.in/yaml.v3"
)
// hermesConfig is a Hermes config.yaml as the user keeps it: their model,
// a comment, and a server of their own with settings magpie doesn't write.
const hermesConfig = `# my Hermes settings
model:
default: anthropic/claude-sonnet-4.6 # the one I like
provider: openrouter
mcp_servers:
mine:
command: uvx
args: [mine-mcp]
timeout: 30
toolsets:
- hermes-cli
`
// EUPH on Discord: the Library's MCP servers couldn't be given to Hermes.
// Hermes reads mcp_servers from $HERMES_HOME/config.yaml (tools/mcp_tool.py):
// command/args/env, url/headers, transport: sse for SSE; each is written
// there beside the user's settings, comments and servers, read back, and
// only magpie's taken out again.
func TestHermesMCP(t *testing.T) {
h := sandbox(t)
dir := filepath.Join(h, "hermes-home")
t.Setenv("HERMES_HOME", dir)
p := filepath.Join(dir, "config.yaml")
write(t, p, hermesConfig)
tg := targetByID("hermes")
if tg == nil || tg.MCP == nil || tg.MCP.Path != p {
t.Fatalf("hermes has no MCP target: %+v", tg)
}
if id, err := Takes("hermes-agent", "mcp"); id != "hermes" || err != nil {
t.Errorf("Takes: %q %v", id, err)
}
if tg.Skills != filepath.Join(dir, "skills") {
t.Errorf("skills: %q", tg.Skills)
}
agents := []string{"hermes"}
stdio := Server{Name: "fs", Transport: "stdio", Command: "npx", Args: []string{"-y", "@mcp/fs"}, Env: map[string]string{"K": "V"}, Agents: agents}
web := Server{Name: "web", Transport: "http", URL: "https://example.com/mcp", Headers: map[string]string{"Authorization": "Bearer x"}, Agents: agents}
sse := Server{Name: "old", Transport: "sse", URL: "https://example.com/sse", Agents: agents}
for _, s := range []Server{stdio, web, sse} {
ok(t)(SaveServer("", s))
}
got := read(t, p)
for _, want := range []string{"# my Hermes settings", "# the one I like", "provider: openrouter", "- hermes-cli"} {
if !strings.Contains(got, want) {
t.Errorf("lost %q:\n%s", want, got)
}
}
var f struct {
Servers map[string]map[string]any `yaml:"mcp_servers"`
}
if err := yaml.Unmarshal([]byte(got), &f); err != nil {
t.Fatalf("%v:\n%s", err, got)
}
if fs := f.Servers["fs"]; fs["command"] != "npx" || fs["url"] != nil || fs["transport"] != nil {
t.Errorf("fs: %v", fs)
} else if e, _ := fs["env"].(map[string]any); e["K"] != "V" {
t.Errorf("fs env: %v", fs)
}
if w := f.Servers["web"]; w["url"] != "https://example.com/mcp" || w["transport"] != nil || w["command"] != nil {
t.Errorf("web: %v", w)
} else if hd, _ := w["headers"].(map[string]any); hd["Authorization"] != "Bearer x" {
t.Errorf("web headers: %v", w)
}
if o := f.Servers["old"]; o["url"] != "https://example.com/sse" || o["transport"] != "sse" {
t.Errorf("sse: %v", o)
}
if m := f.Servers["mine"]; m["command"] != "uvx" || m["timeout"] != 30 {
t.Errorf("the user's server: %v", m)
}
// written in Hermes' own order, command before args
if i, j := strings.Index(got, "command: npx"), strings.Index(got, "- '@mcp/fs'"); i < 0 || j < i {
t.Errorf("fs isn't written as Hermes writes it:\n%s", got)
}
read1, err := tg.MCP.read()
if err != nil {
t.Fatal(err)
}
for _, want := range []Server{stdio, web, sse} {
if s := read1[want.Name]; s == nil || !s.same(&want) {
t.Errorf("%s read back as %+v", want.Name, s)
}
}
before := read(t, p)
ok(t)(Sync())
if read(t, p) != before {
t.Errorf("a sync rewrote servers that are as the library has them:\n%s", read(t, p))
}
// a timeout or enabled the user sets on magpie's server stays when it
// is written again
got = strings.Replace(read(t, p), " url: https://example.com/mcp\n", " url: https://example.com/mcp\n enabled: false\n timeout: 90\n", 1)
write(t, p, got)
ok(t)(SaveServer("web", Server{Name: "web", Transport: "http", URL: "https://example.com/v2", Agents: agents}))
yaml.Unmarshal([]byte(read(t, p)), &f)
if w := f.Servers["web"]; w["url"] != "https://example.com/v2" || w["enabled"] != false || w["timeout"] != 90 || w["headers"] != nil {
t.Errorf("web after edit: %v\n%s", w, read(t, p))
}
for _, s := range []string{"fs", "web", "old"} {
ok(t)(RemoveServer(s))
}
after, _ := tg.MCP.read()
if len(after) != 1 || after["mine"] == nil || after["mine"].Command != "uvx" {
t.Errorf("after: %+v\n%s", after, read(t, p))
}
got = read(t, p)
for _, want := range []string{"# my Hermes settings", "# the one I like", "timeout: 30"} {
if !strings.Contains(got, want) {
t.Errorf("lost %q:\n%s", want, got)
}
}
}
// A server Hermes has that the library doesn't is found, to bring in.
func TestHermesMCPFound(t *testing.T) {
sandbox(t)
dir := appdir.HermesHome()
write(t, filepath.Join(dir, "config.yaml"), strings.Replace(hermesConfig, "toolsets:", " remote:\n url: https://r.example/sse\n transport: sse\ntoolsets:", 1))
l, err := load()
if err != nil {
t.Fatal(err)
}
found := map[string]*Server{}
for _, f := range foundServers(l) {
found[f.Server.Name] = f.Server
}
if s := found["mine"]; s == nil || s.Command != "uvx" || !strings.Contains(strings.Join(s.Agents, ","), "hermes") {
t.Errorf("mine: %+v", s)
}
if s := found["remote"]; s == nil || s.Transport != "sse" || s.URL != "https://r.example/sse" {
t.Errorf("remote: %+v", s)
}
}