monkeyin_92 on X: "can a rule take several models? Otherwise one rule
with several models is several rules." A rule now takes more than one
model (Rule.More after Rule.Use; `more` in the group's JSON, use=a,b
typed). They go first in that order: when the first fails or rests the
rule's next takes the request, ahead of the models the later matching
rules name and the group's others.
Every reader of Rule.Use goes through Uses/SetUses/MapUses/Without/Names
in grouprule.go:
- cleanRules: each must be the group's, each named once; a pause names one.
- gateway: ruleFor's turn hold, tool rounds and outgrown turns, compaction
(the rule's first that takes the summary, the others in Small),
ruleMembers/ruleOver/applyRule, nestedRules and ruleLed (the one a
nested group's turn stays on), modelRuled, ruleEffort (useEffort asked
of each), ThenUses (leaves out all of the rule's own). RuleHit.More is
in the trace; Instead from More has Unready false, one from a later
rule Unready true, as before.
- ruledEntry: what the group takes is what every one of them takes.
- provider rename, auto/ refs, groupfast, a member's removal (a rule
goes with its last), ShareGroup and its import (one this magpie hasn't
is left out of the rule).
- CLI `group rule add use=a,b` and its listing; `group models-=` keeps a
rule's other models. The TUI shows them joined and keeps the rule when
one is deleted.
- GUI (plugins/routing): a chip per model, a + for the next one, an ×
on each while there is more than one, a chip picks another in its place
(app menu, no native select); the card's tooltip and the trace's why
("…so c/mini, which the rule names after it, goes first"), nested too;
en, zh, zh-TW, ja, de.
Old data: a rule of one model writes no `more`, so groups saved before
read and save unchanged (TestRuleMoreOnDisk). An older magpie reads `use`
alone: its requests go to the first model, the others are plain group
members in group order; if it saves the group, `more` is dropped.
Tests: TestRuleSeveralModelsInOrder, TestRuleEffortReachesTheRulesNextModel,
TestRuleCompactPassesOverTheRulesSmallModel (gateway); TestCleanRulesSeveralModels,
TestRuleMoreOnDisk, TestRuleWithout, TestParseRuleSeveralModels,
TestThenUsesLeavesOutTheRulesOwn, TestRuledEntrySeveralModels,
TestSharedGroupRuleSeveralModels, TestRuleMoreRenamedAndKept (provider);
TestGroupRuleSeveralModelsCmd (cli); TestGroupTakeOutKeepsARulesOtherModels
(tui); routing-rule-models.test.cjs. Broken by hand: ruleMembers on Use
alone turns the first two gateway tests red.
Run at this head:
- gofmt -l clean; go vet -tags nogui clean with GOOS=linux and GOOS=windows
for provider, gateway, cli, tui.
- go test -tags nogui -timeout 45m: internal/provider, internal/cli,
internal/tui ok; internal/gateway ok (384s); the new gateway tests
again after; internal/gui budgets ok.
- GUI: every routing-*.test.cjs plus i18n-keys, click-scroll and
group-member-drag, chromium and webkit (en, zh, zh-TW, ja, de where the
file has them): 1319 pass, 57 red. routing-rule-models passes in both.
The reds are in files that are red the same on origin/main 42976f9a0,
run there just now: routing-pause (#3410), routing-intent-entry
(#3408), routing-rule-effort (#3411), routing-rule-remove (#3096,
#3094), routing-manual (#3409), routing-steady (#3412, #1804),
routing-live-patch (#2174), routing-time (#3465). The other 10 reds are
routing-rule-drag.test.cjs, which is the next commit's and not in this one.
- Real run, sandbox HOME, built binary: three fake OpenAI vendors, group
Mix = three/m, one/m, two/m, rule `use=one/m,two/m tokens=1`. one/m
answers 429: the request went one -> two (not three, the group's
first); the next went straight to two, the trace saying
instead=two/m with more=[two/m].
Not tried: a real vendor account; the TUI by hand; Windows and Linux
runs (vet only).
Rebased onto 5b2b3d3f5 (10 commits, gateway, gui and routing's
client.js among them) and run again at the rebased pair's head
68701dfe4: gofmt -l clean; GOOS=linux and windows vet -tags nogui of
provider, gateway, cli, tui clean; go test -tags nogui -timeout 45m
internal/provider (171s), cli, tui, gateway (422s), gui (budgets): all
ok; GUI routing-rule-drag, routing-rule-models, routing-compact,
i18n-keys, click-scroll, group-member-drag in both engines all pass;
routing-rule-remove red as on main (#3096, #3094).
230 lines
7.8 KiB
Go
230 lines
7.8 KiB
Go
package cli
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
func TestParseTokens(t *testing.T) {
|
|
for in, want := range map[string]int{"200000": 200000, "200k": 200000, "200K": 200000, "1m": 1000000, "1.5m": 1500000, "128_000": 128000, " 32k ": 32000} {
|
|
if got, err := parseTokens(in); err != nil || got != want {
|
|
t.Errorf("%q: %d %v", in, got, err)
|
|
}
|
|
}
|
|
for _, in := range []string{"", "k", "lots", "-5", "1g", "nan", "inf", "1e20"} {
|
|
if _, err := parseTokens(in); err == nil {
|
|
t.Errorf("%q read", in)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestGroupRuleCmd(t *testing.T) {
|
|
groupsHome(t)
|
|
if _, err := addGroup("Opus", []string{"models=a/claude-opus-5-5,b/gpt-5.5,a/only-a"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rules := func() []provider.Rule {
|
|
t.Helper()
|
|
g, err := findGroup("opus")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return g.Rules
|
|
}
|
|
uses := func() string {
|
|
var out []string
|
|
for _, r := range rules() {
|
|
out = append(out, r.Use)
|
|
}
|
|
return strings.Join(out, " ")
|
|
}
|
|
if err := ruleCmd([]string{"opus"}); err != nil { // no rules yet: said, not an error
|
|
t.Fatal(err)
|
|
}
|
|
if err := ruleCmd([]string{"nosuch"}); err == nil {
|
|
t.Fatal("a word that is no group and no verb is an error")
|
|
}
|
|
if err := ruleCmd([]string{"add", "opus", "use=gpt-5.5", "tokens=200k"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := ruleCmd([]string{"opus"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := ruleCmd([]string{"add", "opus", "use=a/only-a", "images", "effort=HIGH", "agents=codex, claude-code"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := ruleCmd([]string{"add", "opus", "use=claude-opus-5-5", "thinking", "at=1"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rs := rules()
|
|
if uses() != "a/claude-opus-5-5 b/gpt-5.5 a/only-a" || rs[0].Effort != "on" || rs[1].Tokens != 200000 ||
|
|
!rs[2].Images || rs[2].Effort != "high" || strings.Join(rs[2].Agents, ",") != "codex,claude-code" {
|
|
t.Fatalf("%+v", rs)
|
|
}
|
|
for _, bad := range [][]string{
|
|
{"add", "opus", "tokens=5"}, // no model
|
|
{"add", "opus", "use=a/m", "tokens=5"}, // not in the group
|
|
{"add", "opus", "use=gpt-5.5"}, // no condition
|
|
{"add", "opus", "use=gpt-5.5", "effort=x"}, // no such level
|
|
{"add", "opus", "use=gpt-5.5", "images=maybe"},
|
|
{"add", "opus", "use=gpt-5.5", "tokens=5", "color=red"},
|
|
{"add", "opus", "use=gpt-5.5", "tokens=5", "at=0"},
|
|
{"rm", "opus", "4"},
|
|
{"rm", "opus", "0"},
|
|
{"mv", "opus", "1", "9"},
|
|
{"zap", "opus"},
|
|
{"add", "nope", "use=gpt-5.5", "tokens=5"},
|
|
} {
|
|
if err := ruleCmd(bad); err == nil {
|
|
t.Errorf("%v taken", bad)
|
|
}
|
|
}
|
|
if len(rules()) != 3 {
|
|
t.Fatalf("a refused rule changed them: %+v", rules())
|
|
}
|
|
if err := ruleCmd([]string{"mv", "opus", "1", "3"}); err != nil || uses() != "b/gpt-5.5 a/only-a a/claude-opus-5-5" {
|
|
t.Fatalf("mv: %v %s", err, uses())
|
|
}
|
|
if err := ruleCmd([]string{"rm", "opus", "2"}); err != nil || uses() != "b/gpt-5.5 a/claude-opus-5-5" {
|
|
t.Fatalf("rm: %v %s", err, uses())
|
|
}
|
|
// dropping a model from the group drops its rules
|
|
if _, err := setGroup("opus", []string{"models-=b/gpt-5.5"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if uses() != "a/claude-opus-5-5" {
|
|
t.Fatalf("after models-=: %s", uses())
|
|
}
|
|
// stored as the file keeps it
|
|
gs := storedGroups(t)
|
|
if len(gs) != 1 || len(gs[0]["rules"].([]any)) != 1 {
|
|
t.Fatalf("stored: %v", gs)
|
|
}
|
|
// a found group gets rules and becomes the user's
|
|
if err := ruleCmd([]string{"add", "auto-m", "use=b/vendor/m", "tokens=1k"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if g, _ := findGroup("auto-m"); g.Auto || len(g.Rules) != 1 {
|
|
t.Fatalf("auto-m: %+v", g)
|
|
}
|
|
}
|
|
|
|
// A rule with an intent, and the group's classifier with it.
|
|
func TestGroupRuleIntentCmd(t *testing.T) {
|
|
groupsHome(t)
|
|
if _, err := addGroup("Opus", []string{"models=a/claude-opus-5-5,b/gpt-5.5,a/only-a"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
group := func() provider.Group {
|
|
t.Helper()
|
|
g, err := findGroup("opus")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return g
|
|
}
|
|
for _, bad := range [][]string{
|
|
{"add", "opus", "use=gpt-5.5", "intent=a quick question"}, // no classifier
|
|
{"add", "opus", "use=gpt-5.5", "intent=", "classifier=a/only-a"}, // no words
|
|
{"add", "opus", "use=gpt-5.5", "intent=a quick question", "classifier=z/nothing"}, // unknown model
|
|
{"add", "opus", "use=gpt-5.5", "intent=a quick question", "classifier=group/opus"},
|
|
{"classifier", "opus", "a/only-a"}, // no intent yet
|
|
} {
|
|
if err := ruleCmd(bad); err == nil {
|
|
t.Errorf("%v taken", bad)
|
|
}
|
|
}
|
|
if err := ruleCmd([]string{"add", "opus", "use=gpt-5.5", "intent= a quick question ", "classifier=magpie/a/only-a"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
g := group()
|
|
if len(g.Rules) != 1 || g.Rules[0].Intent != "a quick question" || g.Classifier != "a/only-a" {
|
|
t.Fatalf("%+v", g)
|
|
}
|
|
// a second needs no classifier again
|
|
if err := ruleCmd([]string{"add", "opus", "use=claude-opus-5-5", "use-effort=high", "intent=planning", "agents=codex"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := ruleCmd([]string{"classifier", "opus", "b/gpt-5.5"}); err != nil || group().Classifier != "b/gpt-5.5" {
|
|
t.Fatalf("%v %+v", err, group())
|
|
}
|
|
if err := ruleCmd([]string{"opus"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := ruleLine(group().Rules[1]); !strings.Contains(got, `agent codex · intent "planning"`) || !strings.HasSuffix(got, "high") || group().Rules[1].UseEffort != "high" {
|
|
t.Fatalf("%q", got)
|
|
}
|
|
// with the intents gone, so is the classifier
|
|
for range 2 {
|
|
if err := ruleCmd([]string{"rm", "opus", "1"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if g := group(); len(g.Rules) != 0 || g.Classifier != "" {
|
|
t.Fatalf("%+v", g)
|
|
}
|
|
if gs := storedGroups(t); gs[0]["classifier"] != nil {
|
|
t.Fatalf("stored: %v", gs)
|
|
}
|
|
}
|
|
|
|
func TestGroupMemberNames(t *testing.T) {
|
|
g := provider.Group{ID: "x", Members: []string{"a/m", "b/vendor/m", "a/Big", "c/deepseek/deepseek-v4-pro"}}
|
|
for in, want := range map[string]string{"a/m": "a/m", "magpie/a/m": "a/m", "A/BIG": "a/Big", "big": "a/Big", "vendor/m": "b/vendor/m",
|
|
"deepseek-v4-pro": "c/deepseek/deepseek-v4-pro", "deepseek/deepseek-v4-pro": "c/deepseek/deepseek-v4-pro"} {
|
|
if got, err := groupMember(g, in); err != nil || got != want {
|
|
t.Errorf("%q: %q %v", in, got, err)
|
|
}
|
|
}
|
|
// "m" is a's model by name before b's by its last name
|
|
if got, err := groupMember(g, "m"); err != nil || got != "a/m" {
|
|
t.Errorf("m: %q %v", got, err)
|
|
}
|
|
if _, err := groupMember(g, "gone"); err == nil || !strings.Contains(err.Error(), "a/m") {
|
|
t.Errorf("gone: %v", err)
|
|
}
|
|
}
|
|
|
|
// A rule naming several models (monkeyin_92 on X): use=a,b, in order; a
|
|
// model dropped from the group leaves the rule, which goes with its last.
|
|
func TestGroupRuleSeveralModelsCmd(t *testing.T) {
|
|
groupsHome(t)
|
|
if _, err := addGroup("Opus", []string{"models=a/claude-opus-5-5,b/gpt-5.5,a/only-a"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rule := func() []provider.Rule {
|
|
t.Helper()
|
|
g, err := findGroup("opus")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return g.Rules
|
|
}
|
|
if err := ruleCmd([]string{"add", "opus", "use=gpt-5.5,only-a,claude-opus-5-5", "tokens=200k"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if rs := rule(); len(rs) != 1 || strings.Join(rs[0].Uses(), " ") != "b/gpt-5.5 a/only-a a/claude-opus-5-5" {
|
|
t.Fatalf("%+v", rs)
|
|
}
|
|
if err := ruleCmd([]string{"opus"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := ruleCmd([]string{"add", "opus", "use=gpt-5.5,a/m", "tokens=5"}); err == nil {
|
|
t.Fatal("a model not in the group taken")
|
|
}
|
|
if _, err := setGroup("opus", []string{"models-=b/gpt-5.5"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if rs := rule(); len(rs) != 1 || strings.Join(rs[0].Uses(), " ") != "a/only-a a/claude-opus-5-5" || rs[0].Tokens != 200000 {
|
|
t.Fatalf("after models-=b: %+v", rs)
|
|
}
|
|
if _, err := setGroup("opus", []string{"models-=a/only-a,a/claude-opus-5-5", "models+=b/gpt-5.5"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if rs := rule(); len(rs) != 0 {
|
|
t.Fatalf("a rule left with no model kept: %+v", rs)
|
|
}
|
|
}
|