Files
magpie/internal/agent/pi_scope_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

210 lines
7.6 KiB
Go

package agent
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/provider"
)
func TestPiTakes(t *testing.T) {
for _, c := range []struct {
pattern, ref string
want bool
}{
{"xai/grok-4.6", "xai/grok-4.6", true},
{"XAI/Grok-4.6", "xai/grok-4.6", true},
{"xai/grok-4.6:high", "xai/grok-4.6", true},
{"xai/*", "xai/grok-4.6", true},
{"*grok*", "xai/grok-4.6", true},
{"magpie/*", "magpie/deepseek/deepseek-v4", false}, // Pi's * stops at a /
{"magpie/*/*", "magpie/deepseek/deepseek-v4", true},
{"deepseek/*:low", "magpie/deepseek/deepseek-v4", true}, // the model id fits
{"grok-4.6", "xai/grok-4.6", false}, // a bare id: added beside
{"xai/grok-4.6", "xai/grok-4.7", false},
{"openai-codex/gpt-5.6-sol", "opencode-go/deepseek-v4-flash", false},
} {
if got := piTakes(c.pattern, c.ref); got != c.want {
t.Errorf("piTakes(%q, %q) = %v", c.pattern, c.ref, got)
}
}
}
// Pi starts a new session on defaultModel only when enabledModels takes it
// in: the model magpie picks is added to the list, the user's entries
// kept; there is no list made where there was none, and none left empty
// (#160)
func TestPiModelJoinsEnabledModels(t *testing.T) {
home := syncHome(t)
settings := filepath.Join(home, ".pi", "agent", "settings.json")
os.MkdirAll(filepath.Dir(settings), 0o755)
os.WriteFile(settings, []byte(`{
// the user's own
"enabledModels": ["xai/grok-4.6:high", "openai-codex/gpt-5.6-sol", "deepseek/*"],
"defaultProvider": "xai",
"defaultModel": "grok-4.6"
}
`), 0o644)
f := pi(home).Field("model")
if got := f.Get(); got != "xai/grok-4.6" {
t.Fatalf("Get = %q", got)
}
if err := f.Set("opencode-go/deepseek-v4-flash-vision-exp"); err != nil {
t.Fatal(err)
}
want := `["xai/grok-4.6:high","openai-codex/gpt-5.6-sol","deepseek/*","opencode-go/deepseek-v4-flash-vision-exp"]`
if got := strings.Join(strings.Fields(mustGet(t, settings, "enabledModels")), ""); got != want {
t.Errorf("enabledModels = %s, want %s", got, want)
}
if !strings.Contains(readFile(settings), "// the user's own") {
t.Errorf("comment lost: %s", readFile(settings))
}
if got := f.Get(); got != "opencode-go/deepseek-v4-flash-vision-exp" {
t.Errorf("Get = %q", got)
}
// a model the list takes in already isn't added again
before := mustGet(t, settings, "enabledModels")
for _, v := range []string{"openai-codex/gpt-5.6-sol", "deepseek/deepseek-v4-pro", "xai/grok-4.6"} {
if err := f.Set(v); err != nil {
t.Fatal(err)
}
}
if got := mustGet(t, settings, "enabledModels"); got != before {
t.Errorf("enabledModels grew: %s", got)
}
// set outside magpie: the model shown is the one Pi starts on
edit.SetJSON(settings, edit.KV{Path: "defaultModel", Value: "grok-3"})
if got := f.Get(); got != "xai/grok-4.6" {
t.Errorf("Get with a default outside the list = %q, want the list's first", got)
}
// no list: none is made
os.WriteFile(settings, []byte(`{"defaultProvider":"xai","defaultModel":"grok-4.6"}`), 0o644)
if err := f.Set("opencode-go/deepseek-v4-flash"); err != nil {
t.Fatal(err)
}
if _, ok := edit.GetJSON(settings, "enabledModels"); ok {
t.Errorf("enabledModels made: %s", readFile(settings))
}
// an empty one means no list to Pi, and stays as it is
os.WriteFile(settings, []byte(`{"enabledModels":[]}`), 0o644)
if err := f.Set("opencode-go/deepseek-v4-flash"); err != nil {
t.Fatal(err)
}
if got := mustGet(t, settings, "enabledModels"); got != "[]" {
t.Errorf("empty enabledModels = %s", got)
}
}
// Turning Pi's model back to its own takes magpie's models out of the list
// with magpie's provider; a list left with nothing goes, not left empty
func TestPiModelOffLeavesEnabledModels(t *testing.T) {
home := syncHome(t)
settings := filepath.Join(home, ".pi", "agent", "settings.json")
os.MkdirAll(filepath.Dir(settings), 0o755)
f := pi(home).Field("model")
os.WriteFile(settings, []byte(`{"enabledModels":["xai/grok-4.6","magpie/*/*"]}`), 0o644)
if err := f.Set("magpie/relay/glm-4.6"); err != nil {
t.Fatal(err)
}
if err := f.Set("magpie/deepseek/deepseek-v4"); err != nil {
t.Fatal(err)
}
if err := f.Set(""); err != nil {
t.Fatal(err)
}
// the user's glob is theirs
if got := strings.Join(strings.Fields(mustGet(t, settings, "enabledModels")), ""); got != `["xai/grok-4.6","magpie/*/*"]` {
t.Errorf("enabledModels = %s", got)
}
os.WriteFile(settings, []byte(`{"enabledModels":["magpie/relay/glm-4.6"],"defaultProvider":"magpie","defaultModel":"relay/glm-4.6"}`), 0o644)
if err := f.Set(""); err != nil {
t.Fatal(err)
}
if _, ok := edit.GetJSON(settings, "enabledModels"); ok {
t.Errorf("enabledModels left: %s", readFile(settings))
}
}
func mustGet(t *testing.T, path, key string) string {
t.Helper()
v, ok := edit.GetJSON(path, key)
if !ok {
t.Fatalf("%s: no %s in %s", path, key, readFile(path))
}
return v
}
// A provider renamed moves the magpie/<id>/ entries in Pi's enabledModels
// onto the new id, in Pi and in its forks that keep the same settings.json
// (OmO, Prime Agent): the model it is on, kept at its place and not listed
// twice, another of that provider's with a thinking level, and a glob. The
// user's own entries stay, and so does a name that only shares a prefix
// (#1919, #1828).
func TestPiEnabledModelsFollowRename(t *testing.T) {
for _, c := range []struct {
id string
dir []string
a func(string) *Agent
}{
{"pi", []string{".pi", "agent"}, pi},
{"omo", []string{".omo", "agent"}, omo},
{"prime-agent", []string{".prime", "agent"}, primeAgent},
{"mu", []string{".mu", "agent"}, mu},
} {
t.Run(c.id, func(t *testing.T) {
home := syncHome(t)
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)
}
// a provider whose id only shares a prefix; its model is offered,
// so the sync after the rename keeps its entry (#2055)
if err := provider.Save(provider.Provider{ID: "deepseekx", Name: "DeepSeekX", Chat: "https://deepseekx.invalid/v1", Key: "k", Models: []string{"flash"}}); err != nil {
t.Fatal(err)
}
settings := filepath.Join(append([]string{home}, append(c.dir, "settings.json")...)...)
os.MkdirAll(filepath.Dir(settings), 0o755)
os.WriteFile(settings, []byte(`{
// the user's own
"enabledModels": ["xai/grok-4.6:high", "magpie/deepseek/flash:low", "magpie/deepseek/*", "magpie/deepseekx/flash", "magpie/deepseek/pro"],
"defaultProvider": "xai",
"defaultModel": "grok-4.6"
}
`), 0o644)
a := c.a(home)
if err := a.Apply("model", "magpie/deepseek/pro"); err != nil {
t.Fatal(err)
}
if _, err := RenameProvider("deepseek", "ds"); err != nil {
t.Fatal(err)
}
want := `["xai/grok-4.6:high","magpie/ds/flash:low","magpie/ds/*","magpie/deepseekx/flash","magpie/ds/pro"]`
if got := strings.Join(strings.Fields(mustGet(t, settings, "enabledModels")), ""); got != want {
t.Errorf("enabledModels = %s, want %s", got, want)
}
if !strings.Contains(readFile(settings), "// the user's own") {
t.Errorf("comment lost: %s", readFile(settings))
}
if got := a.Field("model").Get(); got != "magpie/ds/pro" {
t.Errorf("model = %q", got)
}
// a list naming none of the provider's models is left byte for byte
const mine = `{"enabledModels": ["xai/grok-4.6", "xai/grok-4.6"]}` + "\n"
os.WriteFile(settings, []byte(mine), 0o644)
if _, err := RenameProvider("ds", "deepseek"); err != nil {
t.Fatal(err)
}
if got := readFile(settings); got != mine {
t.Errorf("user list rewritten: %s", got)
}
})
}
}