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).
104 lines
4.5 KiB
Go
104 lines
4.5 KiB
Go
package gateway
|
|
|
|
import (
|
|
"net/http/httptest"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// withFlash adds c/flash (128k, images) to ruled's providers and gives
|
|
// the group the members and rules given.
|
|
func withFlash(t *testing.T, members []string, rules ...provider.Rule) *ruleUp {
|
|
t.Helper()
|
|
c := &ruleUp{key: "kc"}
|
|
srv := httptest.NewServer(c)
|
|
t.Cleanup(srv.Close)
|
|
yes := true
|
|
if err := provider.Save(provider.Provider{ID: "c", Name: "C", Key: "kc", Models: []string{"flash"}, Chat: srv.URL + "/v1"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := catalog.SaveLive("c", srv.URL+"/v1", []catalog.Model{{ID: "flash", Context: 128000, Images: true, ImageInput: &yes}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
setMembers(t, members, rules...)
|
|
return c
|
|
}
|
|
|
|
// A rule naming several models (monkeyin_92 on X): the first goes first,
|
|
// and when it fails or rests the rule's next one takes the request, ahead
|
|
// of the group's own first member; the trace says the rule's next went
|
|
// first, the rule's member being ready. The turn's tool rounds stay on it.
|
|
func TestRuleSeveralModelsInOrder(t *testing.T) {
|
|
s, a, b := ruled(t)
|
|
c := withFlash(t, []string{"a/small", "b/big", "c/flash"}, provider.Rule{Use: "b/big", More: []string{"c/flash"}, Tokens: 20000})
|
|
b.mu.Lock()
|
|
b.fail = 429
|
|
b.mu.Unlock()
|
|
code, out := postAs(t, s, "one", chat(long(25000), nil, 0, ""))
|
|
r := lastRoute(s)
|
|
if code != 200 || !strings.Contains(out, "from kc") || r.Rule.N != 1 || !slices.Equal(r.Rule.More, []string{"c/flash"}) || len(r.Tries) != 2 {
|
|
t.Fatalf("b failing: %d %s %+v %d", code, out, r.Rule, len(r.Tries))
|
|
}
|
|
b.mu.Lock()
|
|
b.fail = 0
|
|
b.mu.Unlock()
|
|
code, out = postAs(t, s, "two", chat(long(25000), nil, 0, ""))
|
|
r = lastRoute(s)
|
|
if code != 200 || !strings.Contains(out, "from kc") || r.Rule.Unready || r.Rule.Instead != "c/flash" || len(r.Tries) != 1 {
|
|
t.Fatalf("b resting: %d %s %+v %d", code, out, r.Rule, len(r.Tries))
|
|
}
|
|
code, out = postAs(t, s, "two", chat(long(25000), nil, 1, ""))
|
|
r = lastRoute(s)
|
|
if code != 200 || !strings.Contains(out, "from kc") || !r.Rule.Held || r.Rule.Unready || r.Rule.Instead != "c/flash" {
|
|
t.Fatalf("tool round: %d %s %+v", code, out, r.Rule)
|
|
}
|
|
if a.n() != 0 || b.n() != 1 || c.n() != 3 {
|
|
t.Fatalf("a %d b %d c %d", a.n(), b.n(), c.n())
|
|
}
|
|
|
|
// the rule's models before the later rules' and the group's others
|
|
setMembers(t, []string{"a/small", "c/flash", "b/big"}, provider.Rule{Use: "b/big", More: []string{"a/small"}, Tokens: 20000},
|
|
provider.Rule{Use: "c/flash", Tokens: 10000})
|
|
_, r = postOK(t, s, "three", chat(long(25000), nil, 0, ""))
|
|
if !slices.Equal(r.Rule.Then, []string{"c/flash"}) || len(r.Tries) != 1 || !strings.HasPrefix(r.Tries[0].ID, "a") || r.Rule.Instead != "a/small" {
|
|
t.Fatalf("order: %+v %+v", r.Rule, r.Tries)
|
|
}
|
|
}
|
|
|
|
// The rule's useEffort is asked of each model it names, the next one too.
|
|
func TestRuleEffortReachesTheRulesNextModel(t *testing.T) {
|
|
s, a, b := ruled(t)
|
|
if err := provider.SetModelEfforts("a/small", []string{"low", "medium", "high"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
setMembers(t, []string{"b/big", "a/small"}, provider.Rule{Use: "b/big", More: []string{"a/small"}, Tokens: 1000, UseEffort: "low"})
|
|
b.mu.Lock()
|
|
b.fail = 500
|
|
b.mu.Unlock()
|
|
out, r := postOK(t, s, "", chat(long(2000), nil, 0, `,"reasoning_effort":"high"`))
|
|
if sent := sentBody(t, a); !strings.Contains(out, "from ka") || sent["reasoning_effort"] != "low" {
|
|
t.Fatalf("sent %v (%s)", sent, out)
|
|
}
|
|
if n := len(r.Tries); n != 2 || r.Tries[1].RuleEffort != "low" {
|
|
t.Fatalf("traced %+v", r.Tries)
|
|
}
|
|
}
|
|
|
|
// A compaction longer than a rule's first model takes goes to the rule's
|
|
// next one that takes it, by the same rule.
|
|
func TestRuleCompactPassesOverTheRulesSmallModel(t *testing.T) {
|
|
s, _, _ := ruled(t, provider.Rule{Use: "a/small", More: []string{"b/big"}, Compact: true})
|
|
ccSystem := `{"role":"system","content":"You are a helpful AI assistant tasked with summarizing conversations."},`
|
|
body := `{"model":"group/r","messages":[` + ccSystem + `{"role":"user","content":` + quote(long(70000)) + `},{"role":"assistant","content":"done"},` +
|
|
`{"role":"user","content":"Your task is to create a detailed summary of the conversation so far, paying close attention to the user's explicit requests."}]}`
|
|
code, out := postAs(t, s, "big", body)
|
|
r := lastRoute(s)
|
|
if code != 200 || !strings.Contains(out, "from kb") || !r.Rule.Compact || r.Rule.N != 1 || r.Rule.Use != "b/big" || !slices.Equal(r.Rule.Small, []string{"a/small"}) {
|
|
t.Fatalf("%d %s %+v", code, out, r.Rule)
|
|
}
|
|
}
|