Files
magpie/internal/agent/own_direct_test.go
T
yetone 5573f79231 agent: an agent's own models say they go direct, and a pick of one asks before 接入 goes off (maxleebeta)
maxleebeta in #feedback: Pi 1.5.0 connected to magpie; picking one of
Pi's own OpenCode Go models (opencode-go/<model>) turned 「接入」 off at
once, with nothing said, while magpie's models kept it on.

The cause: the pick writes defaultProvider opencode-go, so Pi asks
OpenCode Go itself and magpie is no longer in its config, and Wired()
is rightly false. What was missing is that the model said so. Options
from ownProvider (every ownOptions agent: Pi, OpenCode, Crush, Goose,
Cline, Hermes, Morph, OpenChamber, Qoder, StepCode, plugged agents, the
WSL twins) and ompOwnOptions carried no Direct, unlike Claude Code's
and Codex's own models. So the picker neither tagged them "direct, not
via magpie" nor asked before a pick took the agent off magpie. Each
now carries Direct = its vendor's name. The GUI already does the rest
(pathTag, the leave-ask, direct options listed after magpie's).

Tests:
- internal/agent TestOwnModelsSayTheyGoDirect: Pi, OpenCode, Crush and
  omp, signed in to OpenCode Go, each give every opencode-go/ option
  Direct "OpenCode Go"; magpie's options have none. Picking one writes
  defaultProvider opencode-go.
- internal/gui/tests/agent-own-direct.test.cjs: the tags, magpie's
  first, the ask (Cancel keeps 接入 on, nothing written), magpie's other
  model not asked, confirmed → written and off. Run in chromium and
  webkit × en/zh/zh-TW/ja/de × 980/420.

Run at this head (rebased on 2c2d2a5e5, mote):
- gofmt -l clean
- CGO_ENABLED=0 GOOS=linux go vet -tags nogui and GOOS=windows go vet
  of ./internal/agent ./internal/gui: ok
- go test ./internal/agent (TestOwnModelsSayTheyGoDirect, Codex
  Own/Pi/Omp/Crush/OpenCode runs): ok
- GUI agent-own-direct, agent-leave-magpie, agent-own-path,
  agent-own-providers, claude-default-off, pi-default-note: 56/56
  pass, both engines

Before the rebase (base 4cd01c964), go test of ./internal/agent and its
importers (backup, cli, davsync, gui, library, profile, stats, tui,
uninstall): all ok except internal/gui's
TestMainWindowCorners/macOS_26_SDK, which is #3949 (red on mote's
macOS 27). This change doesn't touch it.

Broken by hand:
- Without Direct in agents.go/omp.go, the Go test goes red for all
  four agents.
- With `direct` stripped from the GUI fixture, the GUI test goes red.

Not tried: a real Pi 1.5.0 signed in to OpenCode Go.
2026-11-03 10:08:44 +08:00

73 lines
2.9 KiB
Go

package agent
import (
"os"
"path/filepath"
"strings"
"testing"
"github.com/yetone/magpie/internal/catalog"
"github.com/yetone/magpie/internal/edit"
)
// maxleebeta in #feedback: Pi connected to magpie, picking one of Pi's own
// models in the OPENCODE GO section (opencode-go/<model>, Pi signed in to
// OpenCode Go itself) turned 「接入」 off, with nothing said. The pick
// writes defaultProvider opencode-go, so Pi asks OpenCode Go itself and
// magpie isn't in its config any more; what was missing is that the model
// said so. Each of an agent's own models now says who it asks (Direct), as
// Claude Code's and Codex's do, so the picker tags it "direct, not via
// magpie" and asks before a pick takes the agent off magpie. Every agent
// whose own models come from ownOptions shares this, so OpenCode, Crush
// and omp are held to it too; magpie's own models stay unmarked.
func TestOwnModelsSayTheyGoDirect(t *testing.T) {
home := syncHome(t)
os.MkdirAll(filepath.Dir(catalog.CachePath()), 0o755)
if err := os.WriteFile(catalog.CachePath(), []byte(`{"opencode-go":{"name":"OpenCode Go","models":{
"glm-5.1":{"id":"glm-5.1","name":"GLM-5.1"},
"kimi-k2.6":{"id":"kimi-k2.6","name":"Kimi K2.6"}}}}`), 0o644); err != nil {
t.Fatal(err)
}
catalog.Reset()
t.Cleanup(catalog.Reset)
auth := `{"opencode-go": {"type": "api", "key": "test-key"}}`
piDir := filepath.Join(home, ".pi", "agent")
writeFile(t, filepath.Join(piDir, "auth.json"), auth)
writeFile(t, filepath.Join(piDir, "settings.json"), `{"defaultProvider": "magpie", "defaultModel": "deepseek/deepseek-v4.1-flash"}`)
writeFile(t, filepath.Join(home, ".local", "share", "opencode", "auth.json"), auth)
cfg := filepath.Join(home, ".config")
writeFile(t, filepath.Join(cfg, "crush", "crush.json"), `{"providers": {"opencode-go": {"api_key": "test-key"}}}`)
writeFile(t, filepath.Join(home, ".omp", "agent", "models.yml"), "providers:\n opencode-go:\n models:\n - id: glm-5.1\n name: GLM-5.1\n")
for _, a := range []*Agent{pi(home), opencode(home, cfg), crush(home, cfg), omp(home)} {
f := a.Field("model")
if f == nil {
t.Fatalf("%s has no model field", a.ID)
}
opts := f.Options(a.Values())
own := 0
for _, o := range opts {
if strings.HasPrefix(o.Value, "opencode-go/") {
own++
if o.Direct != "OpenCode Go" {
t.Errorf("%s: own model %s says it goes direct to %q, want OpenCode Go", a.ID, o.Value, o.Direct)
}
} else if o.Ref != "" && o.Direct != "" {
t.Errorf("%s: magpie's %s said to go direct to %q", a.ID, o.Value, o.Direct)
}
}
if own == 0 {
t.Errorf("%s offered none of its own OpenCode Go models: %v", a.ID, pickRows(opts, ""))
}
}
// what the tag says: picked, Pi asks OpenCode Go itself
if err := pi(home).Field("model").Set("opencode-go/glm-5.1"); err != nil {
t.Fatal(err)
}
if p, _ := edit.GetJSON(filepath.Join(piDir, "settings.json"), "defaultProvider"); p != "opencode-go" {
t.Fatalf("defaultProvider %q", p)
}
}