Files
magpie/internal/agent/mu_test.go
T
yetone 5a02bf8c14 agent: mu, Pi with a judgment kernel, wired in ~/.mu/agent (xteam3301 on X)
mu (github.com/qybaihe/mu, npm mu-agent, the mu command and the mu
desktop app) is Pi 1.4.0 with a judgment-kernel extension. Its
kyrn/bin/mu.mjs starts Pi on its own agent folder, so Pi's settings.json
and models.json live in ~/.mu/agent, and the desktop app runs the same
Pi on the same folder. magpie now wires it as it wires Pi.

- agent: mu (piLikeKeyed). Its folder is found as mu.mjs finds it:
  $MU_AGENT_DIR, $KYRN_AGENT_DIR, $MU_CODING_AGENT_DIR,
  $KYRN_CODING_AGENT_DIR ("~" expanded), else ~/.mu/agent, or
  ~/.kyrn/agent while ~/.kyrn is a folder and ~/.mu isn't. Its requests
  say pi/<version>, so the provider's key magpie-mu names it. The alias
  is mu-agent. Its judge (~/.mu/mu.json) is left alone.
- `mu` is also maildir-utils' command (brew, distros). A mu on PATH
  that neither is nor names mu.mjs / mu-agent is not taken for it
  (notMuAgent).
- Gemini models stay on Chat for mu, as for Pi and StepCode (piAsked).
- WSL kind at ~/.mu/agent (found by its folder, not the command); CLI
  update through npm mu-agent; the desktop app (io.github.qybaihe.mu,
  mu.app, %LOCALAPPDATA%\Programs\mu\mu.exe, /opt/mu).
- agentenv and appdir know the four variables.
- library: mu's AGENTS.md / AGENTS.override.md, its mcp.json (Pi's
  native format) and skills in its folder; it reads ~/.agents/skills.
- GUI: a one-tone icon. Default in its model picker says it clears the
  default and mu picks one itself (PICKS_ITSELF).
- docs: READMEs, reference.md, agent-wiring.md.

Run (mote, at this tree before the rebase):
- gofmt -l internal/ clean; go vet darwin, GOOS=linux -tags nogui and
  GOOS=windows of agent, library, appdir (and agentenv on darwin): clean.
- go test -count=1 ./internal/agent ./internal/library ./internal/appdir
  ./internal/agentenv ./internal/usage: ok (agent after its maildir
  fixture stopped using testenv.Program, which refuses a non-script).
  New: TestMu, TestMuDir, TestMuNotMaildirUtils, TestMuTarget; mu added
  to the handwired, spelled, Pi scope, scope-gone and Gemini-on-Chat
  lists.
- Windows (win-run on xiaomi-gpu): go test -run 'TestMu|TestPiScope|
  TestScopeGone|TestHandwired|TestSpelled|TestPiFolderKeepsGemini'
  ./internal/agent ./internal/library ./internal/appdir: ok.
- Real mu-agent 0.3.9 (Node 22.22.0) on mote in a sandbox HOME, a
  sandbox gateway on 47822 over a fake OpenAI upstream: `magpie agents`
  found mu with the user's deepseek model; `magpie mu magpie/fakeup/m1`
  wrote defaultProvider magpie and providers.magpie with key magpie-mu,
  keeping the user's deepseek provider; `mu -p ping` answered "pong from
  fake upstream" (upstream saw POST /v1/chat/completions, model m1,
  UA pi/0.3.9); `magpie usage today` counted the call under agent mu;
  `magpie mu default` put settings.json and models.json back byte for
  byte (cmp).
- GUI, chromium and webkit: pi-default-note (with mu added),
  icon-one-tone, icon-tile, mono-icon-redraw, routing-client-icons,
  usage-agent-filter-icons, i18n-keys, i18n-en-no-cjk, agent-connect,
  agent-models, agent-layout: 88 of 88.
- Rebased onto 1b2ad557f: gofmt clean; vet darwin (agent, library,
  gui), linux nogui and windows (agent, library) clean; go test
  ./internal/library ./internal/appdir ok; ./internal/agent -run
  'TestMu|TestPi|TestHandwired|TestSpelled|TestScopeGone|TestDesktop|
  TestCLIUpdate|TestWSL' ok; ./internal/gui ok but two reds that are red
  on an origin/main 1b2ad557f copy as well: TestCoreScriptBudget
  (1,534,470 > 1,533,802 on main, #5918; this change adds 6 bytes) and
  TestGatewayModeStatsNameTheGatewaySessions (run at 00:xx local, #5690).
  The rebase onto 4c5b89489 only adds LESSONS.md.

Broken by hand, each red:
- no binNot: TestMuNotMaildirUtils (maildir-utils' mu detected as mu).
- no ~/.kyrn fallback: TestMuDir.
- mu out of piAsked: TestPiFolderKeepsGeminiOnChatForRaft.
- mu out of PICKS_ITSELF: pi-default-note, 2 of 2 red (chromium).

Not tried: the mu desktop app (its install paths are read from its
electron-builder config, not an installed copy); a real vendor behind
the gateway (fake upstream only); a Gemini model through mu; WSL with a
real distro; mu's own session files are not read for usage outside
magpie.
2026-11-10 16:27:14 +08:00

218 lines
7.5 KiB
Go

package agent
import (
"encoding/json"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/gateway"
"github.com/yetone/magpie/internal/usage"
)
// mu (qybaihe/mu, xteam3301 on X) is Pi in ~/.mu/agent: a model through
// magpie sets settings.json's defaultProvider/defaultModel and magpie's
// provider in models.json there, beside the user's own. Its requests say
// pi/<version>, so the provider's key names it; Disconnect puts both files
// back byte for byte.
func TestMu(t *testing.T) {
home := syncHome(t)
var a *Agent
for _, x := range All() {
if x.ID == "mu" {
a = x
}
}
if a == nil {
t.Fatal("no mu in All()")
}
dir := filepath.Join(home, ".mu", "agent")
settings, models := filepath.Join(dir, "settings.json"), filepath.Join(dir, "models.json")
if a.Name != "mu" || a.Icon != "mu" || a.Bin != "mu" || a.Dir != dir || a.Path != settings || a.Detected() {
t.Fatalf("%+v", a)
}
if f, err := Find("mu-agent"); err != nil || f.ID != "mu" {
t.Fatalf("mu-agent: %v %v", f, err)
}
// as `mu setup` leaves them, with a judge of its own beside them
userSettings := "{\n \"defaultProvider\": \"deepseek\",\n \"defaultModel\": \"deepseek-v4-pro\",\n \"defaultThinkingLevel\": \"high\"\n}\n"
userModels := "{\n \"providers\": {\n \"deepseek\": {\n \"baseUrl\": \"https://api.deepseek.com/v1\",\n \"api\": \"openai-completions\",\n \"apiKey\": \"DEEPSEEK_API_KEY\",\n \"models\": [{\"id\": \"deepseek-v4-pro\"}]\n }\n }\n}\n"
judge := "{\"judge\": {\"provider\": \"deepseek\", \"model\": \"deepseek-v4-flash\"}}\n"
writeFile(t, settings, userSettings)
writeFile(t, models, userModels)
writeFile(t, filepath.Join(home, ".mu", "mu.json"), judge)
if !a.Detected() || a.Field("model").Get() != "deepseek/deepseek-v4-pro" {
t.Fatalf("detected %v, model %q", a.Detected(), a.Field("model").Get())
}
if err := a.Connect(); err != nil {
t.Fatal(err)
}
for k, want := range map[string]string{"defaultProvider": "magpie", "defaultModel": "relay/glm-4.6"} {
if v, _ := edit.GetJSON(settings, k); v != want {
t.Fatalf("%s = %q:\n%s", k, v, readFile(settings))
}
}
var file struct {
Providers map[string]map[string]any `json:"providers"`
}
if err := json.Unmarshal([]byte(readFile(models)), &file); err != nil {
t.Fatal(err)
}
if theirs := file.Providers["deepseek"]; theirs == nil || theirs["apiKey"] != "DEEPSEEK_API_KEY" {
t.Fatalf("their provider:\n%s", readFile(models))
}
got := file.Providers["magpie"]
want := magpieProviderJSON("pi").(map[string]any)
want["apiKey"] = gateway.TokenFor("mu")
wb, _ := json.Marshal(want)
if b, _ := json.Marshal(got); string(b) != string(wb) || got["apiKey"] != "magpie-mu" {
t.Fatalf("not Pi's block with mu's key:\n got %s\nwant %s", b, wb)
}
if c := a.Check(); c != "" {
t.Fatal(c)
}
edit.SetJSON(models, edit.KV{Path: "providers.magpie.apiKey", Value: gateway.Token})
if a.Check() == "" {
t.Fatal("the shared key reads as mu's")
}
if err := a.Sync(); err != nil {
t.Fatal(err)
}
if k, _ := edit.GetJSON(models, "providers.magpie.apiKey"); k != "magpie-mu" || a.Check() != "" {
t.Fatalf("sync left %q: %s", k, a.Check())
}
for _, other := range []string{".pi", ".omo", ".prime", ".kyrn"} {
if _, err := os.Stat(filepath.Join(home, other)); !os.IsNotExist(err) {
t.Fatalf("~/%s was touched: %v", other, err)
}
}
if usage.AgentOf("mu") != "mu" || usage.AgentOf("mu-agent") != "mu" {
t.Fatalf("usage: %q %q", usage.AgentOf("mu"), usage.AgentOf("mu-agent"))
}
if err := a.Disconnect(); err != nil {
t.Fatal(err)
}
if a.Wired() {
t.Fatal("still connected")
}
if b := readFile(settings); b != userSettings {
t.Fatalf("settings.json not as it was:\n%s", b)
}
if b := readFile(models); b != userModels {
t.Fatalf("models.json not as it was:\n%s", b)
}
if b := readFile(filepath.Join(home, ".mu", "mu.json")); b != judge {
t.Fatalf("its judge changed:\n%s", b)
}
}
// mu's agent folder is found as its launcher finds it (agentDirFor and
// muHome in kyrn/bin/mu.mjs): MU_AGENT_DIR, KYRN_AGENT_DIR,
// MU_CODING_AGENT_DIR, KYRN_CODING_AGENT_DIR in that order, else agent/ in
// ~/.mu, or in ~/.kyrn while that is a folder and ~/.mu isn't. A distro's
// is at ~/.mu/agent whatever this machine's says.
func TestMuDir(t *testing.T) {
home := syncHome(t)
def := filepath.Join(home, ".mu", "agent")
if a := mu(home); a.Dir != def {
t.Fatalf("default %s", a.Dir)
}
// a machine not moved from kyrn yet
os.MkdirAll(filepath.Join(home, ".kyrn", "agent"), 0o755)
if a := mu(home); a.Dir != filepath.Join(home, ".kyrn", "agent") || a.Path != filepath.Join(home, ".kyrn", "agent", "settings.json") {
t.Fatalf("kyrn: %s", a.Dir)
}
os.MkdirAll(filepath.Join(home, ".mu"), 0o755)
if a := mu(home); a.Dir != def {
t.Fatalf("~/.mu beside ~/.kyrn: %s", a.Dir)
}
vars := []string{"MU_AGENT_DIR", "KYRN_AGENT_DIR", "MU_CODING_AGENT_DIR", "KYRN_CODING_AGENT_DIR"}
for i, k := range vars {
// each one wins over those after it
for _, after := range vars[i+1:] {
t.Setenv(after, filepath.Join(home, "not-"+after))
}
own := filepath.Join(home, "own-"+k)
t.Setenv(k, own)
if a := mu(home); a.Dir != own {
t.Errorf("%s: %s", k, a.Dir)
}
t.Setenv(k, "~/m")
if a := mu(home); a.Dir != filepath.Join(home, "m") {
t.Errorf("%s=~/m: %s", k, a.Dir)
}
for _, v := range vars {
t.Setenv(v, "")
}
}
t.Setenv("MU_AGENT_DIR", "rel/dir")
if a := mu(home); a.Dir != def {
t.Errorf("relative: %s", a.Dir)
}
t.Setenv("MU_AGENT_DIR", filepath.Join(home, "own"))
root := t.TempDir()
d := distro{Name: "Ubuntu", Root: root, Home: "/home/me", Has: map[string]bool{"dir:.mu/agent": true}, Mirrored: true, Running: true}
k := wslKindOf("mu")
if k.bin != "" {
t.Fatalf("a distro's mu found by its command: %q", k.bin)
}
a := wslAgent(k, d)
if a.ID != "mu@wsl:Ubuntu" || a.Name != "mu · WSL Ubuntu" || a.Dir != filepath.Join(root, "home", "me", ".mu", "agent") {
t.Fatalf("%+v", a)
}
}
// mu is maildir-utils' command too (Homebrew's and the distros' mu, a
// compiled program): that one is not taken for mu-agent, which is its
// kyrn/bin/mu.mjs, through npm's link or a shim that names it.
func TestMuNotMaildirUtils(t *testing.T) {
home := syncHome(t)
bin := filepath.Join(home, "bin")
maildir := filepath.Join(home, "brew", "mu")
if runtime.GOOS == "windows" {
maildir += ".exe"
}
// a compiled program, never run here
os.MkdirAll(filepath.Dir(maildir), 0o755)
if err := os.WriteFile(maildir, []byte("\x7fELF\x02\x01\x01 maildir-utils mu index"), 0o755); err != nil {
t.Fatal(err)
}
if !notMuAgent(maildir) {
t.Fatal("maildir-utils' mu taken for mu-agent")
}
pkg := filepath.Join(home, "lib", "node_modules", "mu-agent", "kyrn", "bin", "mu.mjs")
writeFile(t, pkg, "#!/usr/bin/env node\nimport { main } from \"./main.mjs\";\n")
link := filepath.Join(bin, "mu")
os.MkdirAll(bin, 0o755)
if runtime.GOOS != "windows" {
if err := os.Symlink(pkg, link); err != nil {
t.Fatal(err)
}
if notMuAgent(link) {
t.Fatal("npm's link to mu.mjs not taken")
}
}
// npm's Windows shim
shim := filepath.Join(home, "npm", "mu.cmd")
writeFile(t, shim, "@ECHO off\r\n\"%_prog%\" \"%dp0%\\node_modules\\mu-agent\\kyrn\\bin\\mu.mjs\" %*\r\n")
if notMuAgent(shim) {
t.Fatal("npm's mu.cmd not taken")
}
if !notMuAgent(filepath.Join(home, "nothing")) {
t.Fatal("a mu that can't be read taken")
}
// on PATH, maildir-utils' mu alone leaves mu not installed
t.Setenv("PATH", filepath.Dir(maildir))
a := mu(home)
if a.Detected() || a.binsOwn(binAt(a.Bin)) {
t.Fatal("maildir-utils' mu detected as mu")
}
}