Files
magpie/internal/agent/omp_test.go
T
cyxer000andyetone 1ec5495b26 agents: omp's roles pick their own models (subagents, smol, slow), keep their thinking level, and get back what they had before magpie (#325)
* agents: omp's subagents and small role pick their own models, through magpie too

* agents: omp keeps a role's thinking level and the model it had before magpie, and offers the slow and designer roles

A role set to "anthropic/claude-opus-5:max" and switched to a magpie model lost its ":max", and resetting it deleted the key rather than putting the user's model back. The level now goes with the new model (omp clamps one the model lacks, never fails), the model the role had before magpie is stashed and restored, and the reviewer's @slow and the designer's @designer roles are fields beside subagents and small.

* gui: omp's slow and designer roles follow the model, as its subagents do

Unset, both roles take the session's model, yet the picker offered them omp's Default and no typed model. They are now squares beside subagents, with Same as model first and a model typed in taken as it is.

* agents: omp's thinking level no longer hides a role's model from magpie, and the model alone clears it — magpie omp magpie/x/y:high is taken, a role moved back to omp's own is drift, a list of models comes back on reset, and designer (gone in omp 18.1.5) goes

A role on "magpie/deepseek/pro:max" matched the picker only through the
option offered for its level beside the model, and only for the value in
the file now. So the CLI refused a model typed at a level, a recorded
value at a level the current one didn't share wasn't known for magpie's
(no "replaced" drift), and a level could never be taken away: a model
picked without one took the old one on.

An agent may now say what it keeps after the model (Agent.SplitSuffix;
omp's is its level). Drift, Reseat, RenameProvider, Spell and the CLI's
field-for-value match the model and put the suffix back after the one
they move to; agents without it are unchanged. The option at the level
stays: it keeps the level. Another model keeps it too; the same model
picked alone clears it.

A role omp falls back through (a YAML list, or the comma list omp joins
it into, settings.ts modelRoleValueFromUnknown) read as nothing, so it
wasn't stashed and a reset deleted it. It reads as that comma list now,
is stashed and comes back; a list has no level of its own.

omp 18.1.5 removed the designer role (CHANGELOG), so its field goes. The
smol role, like task and slow, gives its agents the session's model when
unset; its field is labelled smol, as omp names it, so the GUI can show it
as the others without touching other agents' "small".

* gui: omp's smol role follows the model as its subagents and slow role do, and designer goes

smol unset gives omp's scout and sonic the session's model, so it is a
square after the pickers with "Same as model" first, as subagents and
slow are (a feather). Its label is smol, not small: OpenCode's, Crush's
and OpenChamber's small model stays a picker. designer, a role omp 18.1.5
removed, leaves FOLLOWS_MODEL, the glyphs and i18n.

* agents: the note on omp's fallback chains says their keys are roles, and from omp 18 models too

ompRefKeys' comment had retry.fallbackChains keyed by model. omp 16.3.5 takes only a role's name there (session/agent-session.ts: "a mapping of role names to selector arrays"); omp 18.4.4 takes a role's name or a model selector (session/retry-fallback-chains.ts). Walking the keys stays: a role's name is no magpie model and is passed over. Follow-up to the review of #340.

* agents: omp reads whether it is on magpie one way, and a role at a level that a fallback chain names too is renamed once in each

Rebased on #340, one scan over omp's config (ompRefs) answers both questions: magpie's provider stays in models.yml while anything under ompRefKeys is on magpie, as #340 has it, and Check looks at the wiring while any role under modelRoles is, as this PR had it; a fallback chain or enabledModels alone keeps the provider without being checked.

A provider renamed moves a role at a thinking level through SplitSuffix first, then RenameRefs the rest, which finds no old name left in the roles: each place moves once, the level kept.

* agents: a fallback chain alone on magpie has omp's wiring checked, and a role set off magpie mends it

Check now reads the same keys dropMagpie does (ompRefKeys: every role, retry.fallbackChains, enabledModels, task.agentModelOverrides): whatever keeps magpie's provider in models.yml has its baseUrl checked, so a chain on magpie whose wiring something else moved no longer fails its fallback silently. Applying again sets the model role, which may be omp's own; a role set off magpie while something else still names it now writes magpie's provider afresh instead of leaving it as it is, so that mends the wiring. Review of #325.

* agents: an omp role that is a list of models stays the user's own, even starting on one of magpie's, and comes back as it was written

A role omp falls back through ([magpie/deepseek/flash, openai/gpt-6], or the same as a comma-separated string) was taken for one of magpie's models because it started with magpie/: magpie taking the role over stashed nothing, and a reset deleted it. Only one model, no comma, is magpie's now: SplitSuffix answers whether a value is one model, and Spell, drift (magpieValue), Reseat, RenameProvider and the CLI's fieldForValue leave a list alone. The stash keeps a YAML list as the YAML it was written in (edit.GetYAMLText), and a reset puts it back so, flow or block; a comma-separated string comes back a string. A provider renamed moves the names inside a list through RenameRefs, once, and in the lists the stash keeps too. Review of #325.

* gui: a test holds omp's subagents, smol and slow roles as squares that follow the model, and OpenCode's small model a picker

omp-roles.test.cjs, in Chromium and WebKit, English and Chinese, API faked: the three roles are squares after the model's picker, unset named "same as model" (「同主模型」) and set naming their model; smol's picker opens on "Same as model" with the model beside it, and a pick sends it and shows it. OpenCode's small model stays a picker. With smol and slow taken out of FOLLOWS_MODEL it fails at "small is a square". Review of #325.

* gateway: a Claude Code run whose subagent ended on StructuredOutput is let go, and runs left waiting on tool calls are bounded (#345) — a workflow's subagents end on StructuredOutput, which the client answers itself, so the bridge's claude -p waited 30 minutes for a result that never came, one process per subagent. Such a run now waits 30s (for a schema error the client sends at once) and ends; of other runs waiting 5 minutes or more on their caller's tools, 16 are kept, the longest waiting let go first (one that then gets its results starts anew with the whole conversation). TestClaudeRunEndsAfterStructuredOutput and TestClaudeParkedRunsAreBounded fail without the change. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass.

* plugins: a built-in moved onto its plugin looks and works as it did (你确保插件版的订阅 provider 和其对应的内置的…都保持一模一样了吗?) — moved onto its plugin, a built-in lost pieces: its tile's name, icon, plans and risk note went to the plugin's own, "Add account" still opened the built-in's sign-in (in the app, on the server and in `magpie accounts add`), an agent's own sign-in the built-in still found showed a second usage card of the same account, the account's plan badge went, WorkBuddy's daily check-in stopped, the provider's icon and site were the plugin's, WorkBuddy AI's card was named WorkBuddy, and Devin, which had no card of its own, lost the plugin's. Now a moved built-in keeps its tile and risk sheet and signs in through its plugin; its cards take the built-in's place and name, and it has none of its own; the plan the plugin tells stays with the account; WorkBuddy checks in through the plugin, which signs the call; a card is left out only where its built-in's slot showed it. TestMovedBuiltinQuiet, TestWorkBuddyCheckinThroughPlugin, TestMoveToPlugin (Devin-like card) and TestPluginUsage (plan kept) fail without the change. Command Code and Devin moved and back in a sandbox HOME: one card each, plan, name. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass; plugin-move, plugin-signin, commandcode-risk, old-webkit pass; click-scroll's WebKit fails the same on origin/main under this load.

* provider: the Providers and Gateway pages no longer wait on `claude --version` (deluxe on Discord: on Omarchy the two pages never loaded) — on Omarchy `claude` is a mise wrapper that installs or updates Claude Code before it answers, which on a slow network takes minutes or never ends; magpie asked it with no limit and with a lock held, so /api/providers (fetching the Claude account's models) and every Claude request waited with it. The CLI is now asked in the background, given a minute, and a caller waits at most 3s before the version it has stands in; a late answer is taken once it comes. Reproduced on an Omarchy VM (/api/providers unanswered after 100s on v0.1.516, 3.7s with the change). TestClaudeVersionDoesNotWaitForAStalledCLI and TestClaudeVersionTakenWhenItComes fail without the change. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass.

* plugins: a plugin's account behaves as its built-in's (the user: 插件版的订阅 provider 和内置的…保持一模一样了吗) — a plugin's failed sign-in now says why ("the sign-in failed: the sign-in page expired") instead of a bare failure; a plugin account the provider refuses with 401 is marked lapsed, "<user>'s <provider> sign-in has expired — sign in again", beside the account and on its usage card as a built-in's is, and the mark goes when it answers again or is signed in anew; a Devin or Command Code account taken from the agent's CLI moves to the plugin marked as the CLI's and back as the agent's own, so the one it follows stays the one the CLI is signed in to. TestPluginProvider (lapsed), TestMoveSideState (cli) and TestPluginSignIn (reason) fail without the change. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass.

* agents: "Hide all" in an agent's model list keeps its line, and an uninstalled Gemini CLI goes from the Agents page (#356, #230) — the line under an agent ("Showing 0 / N models") came only when a picker still listed one of magpie's models, so hiding them all took away the one way back; it now stays while any is hidden. Gemini CLI was taken as present from what it leaves in ~/.gemini (settings.json, .env, installation_id…), which stays after it is uninstalled, so its row stayed, offering to set up a CLI that can't run; it is now its gemini binary. TestAgentModelLineAllHidden and TestGeminiNotAntigravity fail without the change; agent-models-all-hidden.test.cjs (Chromium, WebKit, en/zh) added. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./internal/agent ./internal/gui ./internal/library ./internal/provider pass; agent-models, click-scroll, old-webkit pass.

* gateway: Codex's Responses Lite tools reach SuperGrok and every other backend, and a group moves on when a member can't read a request (#350) — Codex sends a Lite model's tools as an additional_tools input item, its own functions in a "functions" namespace; magpie relayed the item to xAI, which 422s on it (unknown item type), and translated routes (Cursor, Anthropic, Chat…) dropped it, so the model had no tools. Outside OpenAI the item's tools now go in tools, the functions namespace opened into plain tools and earlier calls un-namespaced; OpenAI and the ChatGPT account still get Codex's own form. A 400/422 over the request's shape (failed to deserialize, unknown item type/parameter…) now moves to the group's next member, each asked once, the member not resting; the Routing page says so. TestAdditionalToolsRelayed, TestAdditionalToolsTranslated and TestGroupFailsOverARequestShape fail without the change; routing-shape.test.cjs (Chromium, WebKit, en/zh) added. go vet, go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui ./... pass; click-scroll, old-webkit, routing-wb-refused pass.

---------

Co-authored-by: yetone <yetoneful@gmail.com>
2026-10-01 01:15:55 +08:00

607 lines
24 KiB
Go

package agent
import (
"os"
"path/filepath"
"slices"
"strings"
"testing"
"github.com/yetone/magpie/internal/edit"
"github.com/yetone/magpie/internal/provider"
"github.com/yetone/magpie/internal/usage"
"gopkg.in/yaml.v3"
)
func TestOmp(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", 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")
configPath, modelsPath := filepath.Join(dir, "config.yml"), filepath.Join(dir, "models.yml")
read := func(p string) (map[string]any, string) {
var m map[string]any
b, _ := os.ReadFile(p)
yaml.Unmarshal(b, &m)
return m, string(b)
}
roles := func(c map[string]any) map[string]any { r, _ := c["modelRoles"].(map[string]any); return r }
providers := func(m map[string]any) map[string]any { p, _ := m["providers"].(map[string]any); return p }
os.MkdirAll(dir, 0o755)
os.WriteFile(configPath, []byte("# mine\ntheme: dark\nmodelRoles:\n default: anthropic/claude-opus-5 # main\n smol: openai/gpt-6-mini\n"), 0o644)
// an older omp's models.json, not yet moved to models.yml
os.WriteFile(filepath.Join(dir, "models.json"), []byte(`{"providers":{"mine":{"baseUrl":"https://x","apiKey":"X","api":"openai-completions","models":[{"id":"a"}]}}}`), 0o644)
f := omp(home).Field("model")
if f.Get() != "anthropic/claude-opus-5" {
t.Fatalf("get: %q", f.Get())
}
if err := f.Set("magpie/deepseek/pro"); err != nil {
t.Fatal(err)
}
c, raw := read(configPath)
if roles(c)["default"] != "magpie/deepseek/pro" || roles(c)["smol"] != "openai/gpt-6-mini" || c["theme"] != "dark" ||
!strings.Contains(raw, "# mine") || !strings.Contains(raw, "# main") {
t.Fatalf("config:\n%s", raw)
}
m, raw := read(modelsPath)
ps := providers(m)
mp, ok := ps["magpie"].(map[string]any)
if !ok || ps["mine"] == nil || mp["auth"] != "none" || mp["api"] != "openai-completions" || !strings.HasSuffix(mp["baseUrl"].(string), "/v1") {
t.Fatalf("models:\n%s", raw)
}
ids := map[string]bool{}
for _, x := range mp["models"].([]any) {
ids[x.(map[string]any)["id"].(string)] = true
}
if !ids["deepseek/pro"] || !ids["deepseek/flash"] {
t.Fatalf("models: %v", ids)
}
// its own model: magpie steps out
if err := f.Set("moonshotai/kimi-k3"); err != nil {
t.Fatal(err)
}
c, _ = read(configPath)
m, _ = read(modelsPath)
if roles(c)["default"] != "moonshotai/kimi-k3" || providers(m)["magpie"] != nil || providers(m)["mine"] == nil {
t.Fatalf("own: %v %v", c, m)
}
// another role through magpie keeps it in models.yml; the reset puts
// back the model default had before magpie took it over
f.Set("magpie/deepseek/flash")
os.WriteFile(configPath, []byte("modelRoles:\n default: magpie/deepseek/flash\n smol: magpie/deepseek/pro\n"), 0o644)
if err := f.Set(""); err != nil {
t.Fatal(err)
}
c, _ = read(configPath)
m, _ = read(modelsPath)
if roles(c)["default"] != "moonshotai/kimi-k3" || roles(c)["smol"] != "magpie/deepseek/pro" || providers(m)["magpie"] == nil {
t.Fatalf("reset with smol: %v %v", c, m)
}
// reset with nothing stashed: only default goes, and magpie with it
os.WriteFile(configPath, []byte("theme: dark\nmodelRoles:\n default: magpie/deepseek/flash\n smol: openai/gpt-6-mini\n"), 0o644)
if err := f.Set(""); err != nil {
t.Fatal(err)
}
c, _ = read(configPath)
m, _ = read(modelsPath)
if roles(c)["default"] != nil || roles(c)["smol"] != "openai/gpt-6-mini" || c["theme"] != "dark" || providers(m)["magpie"] != nil || providers(m)["mine"] == nil {
t.Fatalf("reset: %v %v", c, m)
}
}
// omp names models beyond modelRoles.default: the other roles, the retry
// fallback chains, enabledModels and the task agents' overrides. magpie
// stays in models.yml while any of them is on it, and a provider renamed
// takes them along, their thinking levels and the comments around kept.
func TestOmpRefsBeyondDefault(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", 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")
configPath, modelsPath := filepath.Join(dir, "config.yml"), filepath.Join(dir, "models.yml")
os.MkdirAll(dir, 0o755)
hasMagpie := func() bool {
var m struct{ Providers map[string]any }
b, _ := os.ReadFile(modelsPath)
yaml.Unmarshal(b, &m)
return m.Providers["magpie"] != nil
}
f := omp(home).Field("model")
// the last role leaves magpie; a fallback chain still goes through it
os.WriteFile(configPath, []byte("modelRoles:\n default: magpie/deepseek/flash\nretry:\n fallbackChains:\n default:\n - magpie/deepseek/pro:max\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() {
t.Fatal("a fallback chain on magpie/deepseek/pro:max lost magpie's provider")
}
// none left: it goes
os.WriteFile(configPath, []byte("modelRoles:\n default: magpie/deepseek/flash\nretry:\n fallbackChains:\n default:\n - openai/gpt-6\n"), 0o644)
if err := f.Set(""); err != nil {
t.Fatal(err)
}
if hasMagpie() {
t.Fatal("magpie's provider stayed with nothing on it")
}
const before = `# mine
modelRoles:
default: magpie/deepseek/pro
slow: magpie/deepseek/pro:max, openai/gpt-6 # slow ones
plan:
- magpie/deepseek/flash:high
- openai/gpt-6
retry:
fallbackChains:
# tried in order
default:
- magpie/deepseek/flash:max
- magpie/deepseekx/flash
magpie/deepseek/pro:
- magpie/deepseek/*
enabledModels:
- magpie/deepseek/pro
- path: /work
models:
- magpie/deepseek/flash
task:
agentModelOverrides:
explore: magpie/deepseek/flash,openai/gpt-6
review: [magpie/deepseek/pro:low]
`
os.WriteFile(configPath, []byte(before), 0o644)
if err := f.Set("magpie/deepseek/pro"); err != nil {
t.Fatal(err)
}
moved, err := RenameProvider("deepseek", "ds")
if err != nil {
t.Fatal(err)
}
b, _ := os.ReadFile(configPath)
got := string(b)
want := strings.NewReplacer("magpie/deepseek/", "magpie/ds/").Replace(before)
var gotDoc, wantDoc any
yaml.Unmarshal(b, &gotDoc)
yaml.Unmarshal([]byte(want), &wantDoc)
gotY, _ := yaml.Marshal(gotDoc)
wantY, _ := yaml.Marshal(wantDoc)
if string(gotY) != string(wantY) || strings.Contains(got, "magpie/deepseek/") || !strings.Contains(got, "magpie/deepseekx/flash") ||
!strings.Contains(got, "# mine") || !strings.Contains(got, "# slow ones") || !strings.Contains(got, "# tried in order") {
t.Fatalf("renamed:\n%s", got)
}
if !slices.Contains(moved, "omp") {
t.Fatalf("moved: %v", moved)
}
}
// omp 16.x asks with Bun's User-Agent (Bun/1.3.14), which the gateway can't
// tell for omp's; magpie's provider names it, and the gateway knows that
// name for omp.
func TestOmpNamedToTheGateway(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", 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"}}); err != nil {
t.Fatal(err)
}
if err := omp(home).Field("model").Set("magpie/deepseek/pro"); err != nil {
t.Fatal(err)
}
var m struct {
Providers map[string]struct {
Headers map[string]string `yaml:"headers"`
} `yaml:"providers"`
}
b, _ := os.ReadFile(filepath.Join(home, ".omp", "agent", "models.yml"))
if err := yaml.Unmarshal(b, &m); err != nil {
t.Fatal(err)
}
ua := m.Providers[magpieID].Headers["User-Agent"]
if ua == "" || usage.AgentOf(ua) != "omp" {
t.Fatalf("magpie's provider names omp as %q, which the gateway takes for %q:\n%s", ua, usage.AgentOf(ua), b)
}
if usage.AgentOf("Bun/1.3.14") == "omp" {
t.Fatal("Bun's own User-Agent is taken for omp's, so the header proves nothing")
}
}
// omp's own agents pick their models apart from the main one: through
// magpie on its own, a role brings magpie's provider in, has its wiring
// checked, and takes the provider out again when it is the last.
func TestOmpRoles(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", 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")
configPath, modelsPath := filepath.Join(dir, "config.yml"), filepath.Join(dir, "models.yml")
os.MkdirAll(dir, 0o755)
os.WriteFile(configPath, []byte("modelRoles:\n default: anthropic/claude-opus-5\n"), 0o644)
roles := func() map[string]any {
var c map[string]any
b, _ := os.ReadFile(configPath)
yaml.Unmarshal(b, &c)
r, _ := c["modelRoles"].(map[string]any)
return r
}
magpieIn := func() bool { v, ok := edit.GetYAML(modelsPath, "providers.magpie.baseUrl"); return ok && v != "" }
a := omp(home)
sub, small := a.Field("subagent"), a.Field("small")
if small.Label != "smol" || a.Field("designer") != nil {
t.Fatalf("small labelled %q; designer: %+v", small.Label, a.Field("designer"))
}
for _, k := range []string{"subagent", "small", "slow"} {
if f := a.Field(k); f == nil || !f.Quiet {
t.Fatalf("field %s: %+v", k, f)
}
}
if err := sub.Set("magpie/deepseek/flash"); err != nil {
t.Fatal(err)
}
if r := roles(); r["task"] != "magpie/deepseek/flash" || r["default"] != "anthropic/claude-opus-5" || !magpieIn() {
t.Fatalf("subagent on magpie: %v, magpie in models.yml: %v", r, magpieIn())
}
if sub.Get() != "magpie/deepseek/flash" {
t.Fatalf("get: %q", sub.Get())
}
// the main model isn't magpie's, the subagents' is: its wiring is checked
if a.Check() != "" {
t.Fatalf("check, wired: %q", a.Check())
}
edit.SetYAML(modelsPath, edit.KV{Path: "providers.magpie.baseUrl", Value: "http://127.0.0.1:1/v1"})
if a.Check() == "" {
t.Fatal("check missed the subagents' broken wiring")
}
a.Sync()
if err := small.Set("openai/gpt-6-mini"); err != nil {
t.Fatal(err)
}
if r := roles(); r["smol"] != "openai/gpt-6-mini" || !magpieIn() {
t.Fatalf("small, own: %v", r)
}
// the last role on magpie goes: so does magpie's provider
if err := sub.Set(""); err != nil {
t.Fatal(err)
}
if r := roles(); r["task"] != nil || r["smol"] != "openai/gpt-6-mini" || r["default"] != "anthropic/claude-opus-5" || magpieIn() {
t.Fatalf("subagent reset: %v, magpie in models.yml: %v", r, magpieIn())
}
}
// A role's thinking level ("…:max") stays with it when another model is
// picked, and the model the user had before magpie comes back, level and
// all, when the role is reset.
func TestOmpLevels(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
t.Setenv("PATH", t.TempDir())
for _, k := range []string{"PI_CODING_AGENT_DIR", "PI_CONFIG_DIR", "OMP_PROFILE", "PI_PROFILE"} {
t.Setenv(k, "")
}
for _, p := range []provider.Provider{
{ID: "deepseek", Name: "DeepSeek", Chat: "https://api.deepseek.com/v1", Key: "k", Models: []string{"pro", "flash"}},
{ID: "other", Name: "Other", Chat: "https://other.example/v1", Key: "k", Models: []string{"pro"}},
} {
if err := provider.Save(p); err != nil {
t.Fatal(err)
}
}
dir := filepath.Join(home, ".omp", "agent")
configPath := filepath.Join(dir, "config.yml")
os.MkdirAll(dir, 0o755)
os.WriteFile(configPath, []byte("modelRoles:\n default: anthropic/claude-opus-5:max\n slow: openai/gpt-6:auto\n"), 0o644)
a := omp(home)
get := func(k string) string { return a.Field(k).Get() }
apply := func(k, v, want string) {
t.Helper()
if err := a.Apply(k, v); err != nil {
t.Fatal(err)
}
if got := get(k); got != want {
t.Fatalf("%s set to %q: %q, want %q", k, v, got, want)
}
}
// deepseek/pro has no max of its own: omp clamps it, so it stays
apply("model", "magpie/deepseek/pro", "magpie/deepseek/pro:max")
if d := a.Drift(); d != nil {
t.Fatalf("drift on what magpie set: %+v", d)
}
apply("model", "magpie/deepseek/flash", "magpie/deepseek/flash:max")
apply("model", "magpie/deepseek/pro:low", "magpie/deepseek/pro:low") // its own level wins
// offered at its level beside the model: that keeps the level, the
// model alone clears it
var at, plain bool
for _, o := range a.Field("model").Options(a.Values()) {
at = at || o.Value == "magpie/deepseek/pro:low" && strings.HasSuffix(o.Label, " · low")
plain = plain || o.Value == "magpie/deepseek/pro"
}
if !at || !plain {
t.Fatalf("options: at its level %v, the model %v", at, plain)
}
apply("model", "magpie/deepseek/pro:low", "magpie/deepseek/pro:low")
apply("model", "magpie/deepseek/pro", "magpie/deepseek/pro")
apply("model", "magpie/deepseek/flash", "magpie/deepseek/flash")
apply("model", "magpie/deepseek/pro:low", "magpie/deepseek/pro:low")
apply("slow", "magpie/deepseek/pro", "magpie/deepseek/pro:auto")
// the first model stashed, not the magpie ones after it
apply("model", "", "anthropic/claude-opus-5:max")
apply("slow", "", "openai/gpt-6:auto")
// a colon that is no level is the model's
apply("model", "ollama/qwen3:8b", "ollama/qwen3:8b:max")
// a model of the user's own in between is the one put back
apply("model", "magpie/deepseek/pro", "magpie/deepseek/pro:max")
apply("model", "openai/gpt-6", "openai/gpt-6:max")
apply("model", "magpie/deepseek/pro", "magpie/deepseek/pro:max")
apply("model", "", "openai/gpt-6:max")
// a provider removed moves the role to the same model elsewhere, level
// and all
apply("model", "magpie/deepseek/pro", "magpie/deepseek/pro:max")
if _, err := Reseat(func() error { return provider.Delete("deepseek") }); err != nil {
t.Fatal(err)
}
if got := get("model"); got != "magpie/other/pro:max" {
t.Fatalf("reseated: %q", got)
}
}
// A role on one of magpie's models with a thinking level is that model
// wherever magpie matches a value against the picker: offered once, typed
// at a level on the command line, missed when omp puts it back to its own,
// and moved, level and all, when its provider is renamed.
func TestOmpLevelSuffix(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", home)
t.Setenv("XDG_CONFIG_HOME", filepath.Join(home, ".config"))
t.Setenv("XDG_CACHE_HOME", filepath.Join(home, ".cache"))
t.Setenv("PATH", t.TempDir())
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")
configPath := filepath.Join(dir, "config.yml")
os.MkdirAll(dir, 0o755)
os.WriteFile(configPath, []byte("modelRoles:\n default: anthropic/claude-opus-5:max\n"), 0o644)
a := omp(home)
f := a.Field("model")
v, err := a.Spell("model", "magpie/deepseek/pro:high")
if err != nil || v != "magpie/deepseek/pro:high" {
t.Fatalf("spell at a level: %q %v", v, err)
}
if err := a.Apply("model", v); err != nil {
t.Fatal(err)
}
// the subagents on the same model at another level: moved with it too
if err := a.Apply("subagent", "magpie/deepseek/pro:max"); err != nil {
t.Fatal(err)
}
if d := a.Drift(); d != nil {
t.Fatalf("drift on what magpie set: %+v", d)
}
// omp's own /model puts the role back on the user's model
edit.SetYAML(configPath, edit.KV{Path: "modelRoles.default", Value: "anthropic/claude-opus-5:high"})
if d := a.Drift(); d == nil || d.Kind != "replaced" || d.Want != "magpie/deepseek/pro:high" {
t.Fatalf("drift, replaced: %+v", d)
}
if err := a.Reapply(); err != nil || f.Get() != "magpie/deepseek/pro:high" {
t.Fatalf("reapplied: %q %v", f.Get(), err)
}
if _, err := RenameProvider("deepseek", "ds"); err != nil {
t.Fatal(err)
}
if f.Get() != "magpie/ds/pro:high" || a.Field("subagent").Get() != "magpie/ds/pro:max" {
t.Fatalf("renamed: %q, subagent %q", f.Get(), a.Field("subagent").Get())
}
}
// A role omp falls back through, a list of models (a YAML list or the
// comma-separated string omp also takes), is the user's to have back when
// magpie lets go of the role; a level on its last model is no role's level.
func TestOmpRoleList(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", 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")
configPath := filepath.Join(dir, "config.yml")
os.MkdirAll(dir, 0o755)
os.WriteFile(configPath, []byte("modelRoles:\n slow:\n - openai/gpt-6\n - anthropic/claude-opus-5:high\n task: openai/gpt-6,anthropic/claude-opus-5:high\n"), 0o644)
a := omp(home)
for _, k := range []string{"slow", "subagent"} {
if got := a.Field(k).Get(); got != "openai/gpt-6,anthropic/claude-opus-5:high" {
t.Fatalf("%s reads %q", k, got)
}
if err := a.Apply(k, "magpie/deepseek/pro"); err != nil {
t.Fatal(err)
}
if got := a.Field(k).Get(); got != "magpie/deepseek/pro" {
t.Fatalf("%s on magpie: %q", k, got)
}
if err := a.Apply(k, ""); err != nil {
t.Fatal(err)
}
if got := a.Field(k).Get(); got != "openai/gpt-6,anthropic/claude-opus-5:high" {
t.Fatalf("%s reset: %q", k, got)
}
}
}
// A role on magpie at a thinking level that a fallback chain names too: a
// provider renamed moves each once, the level kept. The role back on omp's
// own, the chain alone keeps magpie's provider, has its wiring checked, and
// applying again mends it.
func TestOmpRoleAndChain(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", 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")
configPath, modelsPath := filepath.Join(dir, "config.yml"), filepath.Join(dir, "models.yml")
os.MkdirAll(dir, 0o755)
os.WriteFile(configPath, []byte("modelRoles:\n default: anthropic/claude-opus-5\nretry:\n fallbackChains:\n slow:\n - magpie/deepseek/pro:max\n - openai/gpt-6\n"), 0o644)
a := omp(home)
if err := a.Apply("slow", "magpie/deepseek/pro:max"); err != nil {
t.Fatal(err)
}
if _, err := RenameProvider("deepseek", "ds"); err != nil {
t.Fatal(err)
}
chain := edit.GetYAMLList(configPath, "retry.fallbackChains.slow")
if s := a.Field("slow").Get(); s != "magpie/ds/pro:max" || !slices.Equal(chain, []string{"magpie/ds/pro:max", "openai/gpt-6"}) {
t.Fatalf("renamed: slow %q, chain %v", s, chain)
}
if err := a.Apply("slow", ""); err != nil {
t.Fatal(err)
}
if v, _ := edit.GetYAML(modelsPath, "providers.magpie.baseUrl"); v == "" || a.Field("slow").Get() != "" {
t.Fatalf("the chain lost magpie's provider, or slow stayed: %q", a.Field("slow").Get())
}
edit.SetYAML(modelsPath, edit.KV{Path: "providers.magpie.baseUrl", Value: "http://127.0.0.1:1/v1"})
if d := a.Drift(); d == nil || d.Kind != "unwired" {
t.Fatalf("a chain's broken wiring: %+v", d)
}
if err := a.Reapply(); err != nil {
t.Fatal(err)
}
if c := a.Check(); c != "" {
t.Fatalf("applying again left: %s", c)
}
}
// A role that is a list of models omp falls back through is the user's own,
// even when it starts on one of magpie's: magpie taking the role over
// stashes it, and a reset puts it back written as it was — a YAML list or a
// comma-separated string.
func TestOmpListRole(t *testing.T) {
for _, slow := range []string{
" [magpie/deepseek/flash, openai/gpt-6]",
" magpie/deepseek/flash, openai/gpt-6",
"\n - magpie/deepseek/flash\n - openai/gpt-6",
} {
t.Run(slow, func(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", 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")
configPath := filepath.Join(dir, "config.yml")
os.MkdirAll(dir, 0o755)
before := "modelRoles:\n default: anthropic/claude-opus-5\n slow:" + slow + "\n"
os.WriteFile(configPath, []byte(before), 0o644)
a := omp(home)
if err := a.Apply("slow", "magpie/deepseek/pro"); err != nil {
t.Fatal(err)
}
if err := a.Apply("slow", ""); err != nil {
t.Fatal(err)
}
b, _ := os.ReadFile(configPath)
if string(b) != before {
t.Fatalf("reset:\n%s\nwant:\n%s", b, before)
}
})
}
}
// A provider renamed moves the magpie models in a list role once, the list
// written as it was, and in the list the stash keeps for a role magpie took
// over, which a reset then puts back.
func TestOmpListRoleRenamed(t *testing.T) {
home := t.TempDir()
t.Setenv("HOME", 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")
configPath := filepath.Join(dir, "config.yml")
os.MkdirAll(dir, 0o755)
os.WriteFile(configPath, []byte("modelRoles:\n default: anthropic/claude-opus-5\n slow: [magpie/deepseek/flash, openai/gpt-6]\n task: [magpie/deepseek/pro:high, openai/gpt-6]\n"), 0o644)
a := omp(home)
if err := a.Apply("slow", "magpie/deepseek/pro:max"); err != nil {
t.Fatal(err)
}
if _, err := RenameProvider("deepseek", "ds"); err != nil {
t.Fatal(err)
}
b, _ := os.ReadFile(configPath)
task := edit.GetYAMLList(configPath, "modelRoles.task")
if s := a.Field("slow").Get(); s != "magpie/ds/pro:max" || !slices.Equal(task, []string{"magpie/ds/pro:high", "openai/gpt-6"}) || !strings.Contains(string(b), "task: [") {
t.Fatalf("renamed: slow %q\n%s", s, b)
}
if err := a.Apply("slow", ""); err != nil {
t.Fatal(err)
}
b, _ = os.ReadFile(configPath)
if !strings.Contains(string(b), "slow: [magpie/ds/flash, openai/gpt-6]") {
t.Fatalf("reset:\n%s", b)
}
}