Files
magpie/internal/agent/omp_scope_test.go
T
yetone e6753190cd agent: a model picked for omp's default role joins its enabledModels list when there is one, so omp starts on it and doesn't pick another (#1769)
real-jacket: with enabledModels in ~/.omp/agent/config.yml, magpie wrote
only modelRoles.default, and omp kept starting on a model of the list. omp
holds a new session's default role to that list while it names anything
(main.ts buildSessionOptions, sdk.ts resolveAllowedModels and
pickDefaultAvailableModel); its other roles resolve against every model it
has, so only the default role is held to it. Pi had the same rule, fixed
in #160.

omp_scope.go: a model picked for the default role is added to the end of a
list that doesn't already take it in, by name or by one of omp's globs
(Bun.Glob, where * doesn't cross "/"), comments and order kept
(edit.EditYAMLNode, new). No list, or an empty one, is left as it is: omp
then takes every model. Entries scoped to folders ({path, models}) are the
user's and never changed. Disconnect takes the plain magpie/... entries out
once nothing else in config.yml is on magpie (a list left empty goes,
never []); a scoped entry on magpie still counts as in use. A renamed
provider's entries are renamed once (ompScopeOnce). Check reports a
default role on magpie the list leaves out as drift, which Reapply mends.

docs/subsystems/agent-wiring.md says the same.

Run:
- omp_scope_test.go: the default joins the list (plain, glob, level
  suffix, comments kept), a list that takes it in or none is left alone,
  Disconnect clears magpie's entries and keeps the user's and scoped ones,
  and the leaving-out is drift. Each goes red with its part taken out
  (ompScopeWith not appending, dropMagpie not calling ompScopeWithout,
  Check without the list).
- go test -tags nogui ./internal/agent ./internal/edit.
- gofmt -l, go vet ./..., GOOS=linux go vet -tags nogui ./...,
  GOOS=windows go vet ./..., sandboxed go test ./... at the rebased head.
Not run: a real omp binary starting a session on the config written. Pi
has no RenameRefs, so a renamed provider's magpie/<old>/... entry stays in
Pi's enabledModels (not changed here).
2026-10-12 22:55:22 +08:00

207 lines
7.8 KiB
Go

package agent
import (
"os"
"path/filepath"
"slices"
"strings"
"testing"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/provider"
"gopkg.in/yaml.v3"
)
// ompScopeHome is a home with omp's agent folder and a provider of magpie's.
func ompScopeHome(t *testing.T) (home, configPath, modelsPath string) {
t.Helper()
home = t.TempDir()
t.Setenv("HOME", home)
t.Setenv("USERPROFILE", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
for _, k := range []string{"PI_CODING_AGENT_DIR", "PI_CONFIG_DIR", "OMP_PROFILE", "PI_PROFILE"} {
t.Setenv(k, "")
}
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)
}
dir := filepath.Join(home, ".omp", "agent")
os.MkdirAll(dir, 0o755)
return home, filepath.Join(dir, "config.yml"), filepath.Join(dir, "models.yml")
}
func ompScopeRead(t *testing.T, p string) (map[string]any, string) {
t.Helper()
b, _ := os.ReadFile(p)
var m map[string]any
yaml.Unmarshal(b, &m)
return m, string(b)
}
// The reporter's case (#1769): omp's config.yml already has a non-empty
// enabledModels, and the default role is set to one of magpie's models
// outside it. omp would start new sessions on a model from the list, so the
// model joins its end, without its level; the user's entries, their order
// and comments stay. Only the default role is held to the list (omp
// resolves its other roles against every model it has).
func TestOmpDefaultJoinsEnabledModels(t *testing.T) {
home, configPath, _ := ompScopeHome(t)
os.WriteFile(configPath, []byte("# mine\nenabledModels:\n - provider-a/model-a # first\n - provider-b/model-b\nmodelRoles:\n default: provider-a/model-a\n"), 0o644)
a := omp(home)
if err := a.Field("model").Set("magpie/deepseek/pro:high"); err != nil {
t.Fatal(err)
}
want := []string{"provider-a/model-a", "provider-b/model-b", "magpie/deepseek/pro"}
if got := edit.GetYAMLList(configPath, "enabledModels"); !slices.Equal(got, want) {
t.Fatalf("enabledModels: %q, want %q", got, want)
}
if _, raw := ompScopeRead(t, configPath); !strings.Contains(raw, "# mine") || !strings.Contains(raw, "# first") {
t.Fatalf("comments lost:\n%s", raw)
}
if v, _ := edit.GetYAML(configPath, "modelRoles.default"); v != "magpie/deepseek/pro:high" {
t.Fatalf("default: %q", v)
}
// set again, or at another level: listed once
for _, v := range []string{"magpie/deepseek/pro:high", "magpie/deepseek/pro:low"} {
if err := a.Field("model").Set(v); err != nil {
t.Fatal(err)
}
}
if got := edit.GetYAMLList(configPath, "enabledModels"); !slices.Equal(got, want) {
t.Fatalf("set again: %q", got)
}
// a role other than the default isn't held to the list
if err := a.Field("small").Set("magpie/deepseek/flash"); err != nil {
t.Fatal(err)
}
if got := edit.GetYAMLList(configPath, "enabledModels"); !slices.Equal(got, want) {
t.Fatalf("smol: %q", got)
}
// the user's own model outside the list joins it too, as it would never
// start either
if err := a.Field("model").Set("provider-c/model-c"); err != nil {
t.Fatal(err)
}
if got := edit.GetYAMLList(configPath, "enabledModels"); !slices.Contains(got, "provider-c/model-c") {
t.Fatalf("own model: %q", got)
}
}
// No list is made where there is none, an empty one stays as it is, and so
// does one with only entries scoped to folders (a plain entry would hold
// every other folder to it). An entry that already takes the model in —
// the same model at a level, a glob — is enough.
func TestOmpEnabledModelsLeftAlone(t *testing.T) {
cases := []struct{ name, config string }{
{"none", "modelRoles:\n default: a/b\n"},
{"empty", "enabledModels: []\nmodelRoles:\n default: a/b\n"},
{"folders only", "enabledModels:\n - path: /work\n models:\n - a/b\n"},
{"same at a level", "enabledModels:\n - a/b\n - magpie/deepseek/pro:max\n"},
{"glob", "enabledModels:\n - a/b\n - magpie/deepseek/*\n"},
// omp matches a glob against provider/id and the id alone (Bun.Glob,
// whose * stops at a slash), and a model of magpie's id is
// provider/model
{"glob on the id", "enabledModels:\n - a/b\n - deepseek/*\n"},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
home, configPath, _ := ompScopeHome(t)
os.WriteFile(configPath, []byte(c.config), 0o644)
before, _ := ompScopeRead(t, configPath)
if err := omp(home).Field("model").Set("magpie/deepseek/pro"); err != nil {
t.Fatal(err)
}
after, raw := ompScopeRead(t, configPath)
bs, _ := yaml.Marshal(before["enabledModels"])
as, _ := yaml.Marshal(after["enabledModels"])
if string(bs) != string(as) {
t.Fatalf("enabledModels changed:\n%s", raw)
}
})
}
}
// Once nothing else is on magpie, its models leave the list with its
// provider; a list left with nothing goes, never written as []. One scoped
// to folders is the user's and keeps the provider.
func TestOmpEnabledModelsCleared(t *testing.T) {
hasMagpie := func(modelsPath string) bool {
m, _ := ompScopeRead(t, modelsPath)
p, _ := m["providers"].(map[string]any)
return p["magpie"] != nil
}
home, configPath, modelsPath := ompScopeHome(t)
os.WriteFile(configPath, []byte("enabledModels:\n - provider-a/model-a # mine\nmodelRoles:\n default: provider-a/model-a\n"), 0o644)
f := omp(home).Field("model")
if err := f.Set("magpie/deepseek/pro"); err != nil {
t.Fatal(err)
}
if err := f.Set(""); err != nil {
t.Fatal(err)
}
if v, _ := edit.GetYAML(configPath, "modelRoles.default"); v != "provider-a/model-a" {
t.Fatalf("reset: %q", v)
}
if got := edit.GetYAMLList(configPath, "enabledModels"); !slices.Equal(got, []string{"provider-a/model-a"}) {
t.Fatalf("enabledModels: %q", got)
}
if _, raw := ompScopeRead(t, configPath); !strings.Contains(raw, "# mine") {
t.Fatalf("comment lost:\n%s", raw)
}
if hasMagpie(modelsPath) {
t.Fatal("magpie's provider stayed")
}
// the list had only magpie's model: the key goes
os.WriteFile(configPath, []byte("enabledModels:\n - magpie/deepseek/flash\nmodelRoles:\n default: magpie/deepseek/flash\n"), 0o644)
if err := f.Set(""); err != nil {
t.Fatal(err)
}
if c, raw := ompScopeRead(t, configPath); c["enabledModels"] != nil || strings.Contains(raw, "enabledModels") {
t.Fatalf("emptied list kept:\n%s", raw)
}
// a folder's own list on magpie keeps it, and its entries
os.WriteFile(configPath, []byte("enabledModels:\n - magpie/deepseek/flash\n - path: /work\n models:\n - magpie/deepseek/pro\nmodelRoles:\n default: magpie/deepseek/flash\n"), 0o644)
if err := f.Set("magpie/deepseek/flash"); err != nil {
t.Fatal(err)
}
if err := f.Set(""); err != nil {
t.Fatal(err)
}
if !hasMagpie(modelsPath) {
t.Fatal("a folder's list on magpie lost magpie's provider")
}
if _, raw := ompScopeRead(t, configPath); !strings.Contains(raw, "magpie/deepseek/pro") {
t.Fatalf("folder's entry dropped:\n%s", raw)
}
}
// A default role on magpie that a list written by hand leaves out shows as
// a model that won't start, and setting it again (Reapply) mends it.
func TestOmpEnabledModelsLeavingOutIsDrift(t *testing.T) {
home, configPath, _ := ompScopeHome(t)
os.WriteFile(configPath, []byte("modelRoles:\n default: provider-a/model-a\n"), 0o644)
a := omp(home)
if err := a.Field("model").Set("magpie/deepseek/pro:high"); err != nil {
t.Fatal(err)
}
if d := a.Check(); d != "" {
t.Fatalf("no list: %q", d)
}
if err := edit.SetYAML(configPath, edit.KV{Path: "enabledModels", Value: []string{"provider-a/model-a"}}); err != nil {
t.Fatal(err)
}
if d := a.Check(); !strings.Contains(d, "enabledModels") || !strings.Contains(d, "magpie/deepseek/pro") {
t.Fatalf("check: %q", d)
}
if err := a.Field("model").Set(a.Field("model").Get()); err != nil {
t.Fatal(err)
}
if d := a.Check(); d != "" {
t.Fatalf("set again: %q", d)
}
}