Files
magpie/internal/gateway/decide_intentsure_test.go
T
yetoneandggbdpq 7e5a54147f gateway: a routing group's intent sureness is its own, intent-sure= (#5101)
Jev's sure of an intent had to reach jevSure, a fixed 0.4, for every
group. zminds measured 39 of 237 turns in one day (16.5%) refused at sure
0.153 to 0.395, and those turns went by the group's routing as if it had
no intent rules. A group can now set its own threshold: intentSure, from
0 (take Jev's likeliest intent however unsure) to 1. Missing keeps 0.4.

readJev keeps Jev's likeliest intent, its sure and Topic (an answer of
intents rather than levels). ruleFor applies the group's own sure through
intentOf, so two groups sharing one classifier and its cached answer each
judge it at their own. "none of these" is never an intent, at 0 too.
Levels and effort are as before.

Set from a terminal: magpie group set <group> intent-sure=0.25, empty to
unset. SaveGroup and the CLI refuse a value outside 0 to 1, NaN too: the
patch's `v < 0 || v > 1` let intent-sure=NaN through the CLI, which
SaveGroup then failed on as "json: unsupported value". cloneGroups copies
the new pointer, since TestCloneGroupsSharesNothing went red on it.

Changed from ggbdpq's patch on #5101:
- The group editor's "Intent sure" row is left out. Its hints were long
  secondary text, it showed for every classifier when only Jev answers
  with a sure, and it had no GUI test. The Routing page instead carries
  intentSure through each save it makes of a group (the editor's Save,
  the routing menu, the manual pick), so a value set from the CLI isn't
  wiped by the next GUI save.
- Its i18n strings and site/public/docs/intent.html are left out. A
  site/ change waits for the maintainer (docs/contributors.md). The site
  diff was documentation text only: a paragraph id="sure", a sentence
  in "Match the rules" and one table row. It had no links or scripts.
- docs/subsystems/gateway-routing.md has a bullet for IntentSure.

Run on mote (mote-run):
- gofmt -l internal/: clean.
- go vet -tags nogui, also with GOOS=linux and GOOS=windows, on
  internal/gateway, internal/cli and internal/provider: clean.
- go test -tags nogui -run 'TestGroupIntentSure|TestJev|Rule|Classif|Decide'
  ./internal/gateway: ok.
- -run 'Group|Apply' ./internal/cli: ok.
- -run 'Group|Save|Clone' ./internal/provider: ok.
- go test -run Budget ./internal/gui: ok.
- node --test group-intent-sure (new), routing-group-strategy and
  group-first-token, in Chromium and WebKit: 42 pass, 0 fail.
- i18n-keys has one red, "a plugin's words are its own". It fails the
  same on origin/main b712adb84 and is filed as #5327, #5332 and #5426.
  i18n-en-no-cjk: ok.

Broken by hand, each red, then restored:
- sureNeeded always returns 0.4: TestGroupIntentSureThreshold,
  ZeroTakesTheArgmax and IsPerGroup FAIL.
- The patch's `< 0 || > 1` checks put back: TestGroupIntentSurePairs
  FAILs on "NaN" and "nan"; TestGroupIntentSureRange FAILs on NaN.
- The cloneGroups copy removed: TestCloneGroupsSharesNothing FAILs.
- The editor's Save sending intentSure null: group-intent-sure FAILs 4/4
  ("the editor's Save keeps it").
- The routing menu's pick sending null: it FAILs 4/4 ("a routing pick
  keeps it").

Not tried: a real Jev account with a group at 0.25; the full suite.

Co-authored-by: ggbdpq <ggbdpq@gmail.com>
2026-11-06 10:44:24 +08:00

146 lines
5.6 KiB
Go

package gateway
import (
"fmt"
"strings"
"testing"
"github.com/yetone/magpie/internal/provider"
)
// #5101: the confidence an intent needs of Jev was fixed at 0.4 for every
// group. A group may set its own (IntentSure): lower it and Jev's unsure
// picks count, raise it and fewer do, set it to 0 and every pick but
// "none of these" counts.
// chatFor is chat for the group named, not group/r.
func chatFor(group, first string, extra string) string {
if extra != "" {
extra = "," + extra
}
return fmt.Sprintf(`{"model":%s%s,"messages":[{"role":"user","content":%s}]}`, quote(group), extra, quote(first))
}
// ptrSure is the group's IntentSure as a test names it.
func ptrSure(sure float64) *float64 { return &sure }
// sure 0.3 misses a group's intent rule at the default 0.4, hits it with
// IntentSure 0.25, and counts again at 0.4 once the field is gone.
func TestGroupIntentSureThreshold(t *testing.T) {
s, _, _, j := jevved(t, "", provider.Rule{Use: "b/big", Intent: "debugging"})
j.choice, j.sure = "debugging", 0.3
_, r := postOK(t, s, "d0", chat("why does this crash?", nil, 0, ""))
if r.Rule.N != 0 || r.Rule.Classified.Intent != "" || r.Rule.Classified.Sure != 0.3 {
t.Fatalf("default 0.4: %+v", r.Rule.Classified)
}
// a torn pick stays none at 0.4 but matches at 0.25
g, _, _ := provider.FindGroup("group/r")
sure := 0.25
g.IntentSure = &sure
if err := provider.SaveGroup(g); err != nil {
t.Fatal(err)
}
_, r = postOK(t, s, "d1", chat("why does this crash, again?", nil, 0, ""))
if r.Rule.N != 1 || r.Rule.Classified.Intent != "debugging" || r.Rule.Classified.Sure != 0.3 {
t.Fatalf("0.25: %+v", r.Rule.Classified)
}
// the trace keeps the sure and the said with it, whichever way the
// threshold went
if r.Rule.Classified.Said != "debugging" {
t.Fatalf("said: %+v", r.Rule.Classified)
}
// unset: 0.4 again
g.IntentSure = nil
if err := provider.SaveGroup(g); err != nil {
t.Fatal(err)
}
_, r = postOK(t, s, "d2", chat("why does this crash, still?", nil, 0, ""))
if r.Rule.N != 0 || r.Rule.Classified.Intent != "" {
t.Fatalf("unset: %+v", r.Rule.Classified)
}
}
// IntentSure 0 takes Jev's pick whatever its sure, "none of these" still
// not an intent; the effort is picked as ever.
func TestGroupIntentSureZeroTakesTheArgmax(t *testing.T) {
s, _, _, j := jevved(t, provider.EffortAuto, provider.Rule{Use: "b/big", Intent: "debugging"})
j.choice, j.sure = "debugging", 0.01
g, _, _ := provider.FindGroup("group/r")
zero := 0.0
g.IntentSure = &zero
if err := provider.SaveGroup(g); err != nil {
t.Fatal(err)
}
out, r := postOK(t, s, "z1", chat("why does this crash?", nil, 0, `,"reasoning_effort":"low"`))
if !strings.Contains(out, "from kb") || r.Rule.N != 1 || r.Rule.Classified.Intent != "debugging" || r.Rule.Classified.Sure != 0.01 {
t.Fatalf("zero: %s %+v", out, r.Rule.Classified)
}
// none of these is no intent, not even at 0
j.choice = noIntent
_, r = postOK(t, s, "z2", chat("what now?", nil, 0, `,"reasoning_effort":"low"`))
if r.Rule.N != 0 || r.Rule.Classified.Intent != "" || r.Rule.Classified.Said != noIntent {
t.Fatalf("none of these at 0: %+v", r.Rule.Classified)
}
if r.Rule.Classified.Effort == "" {
t.Fatalf("effort not asked: %+v", r.Rule.Classified)
}
}
// A group's IntentSure is of the group's own: another group classifying
// with the same classifier keeps the default 0.4.
func TestGroupIntentSureIsPerGroup(t *testing.T) {
s, _, _, j := jevved(t, "", provider.Rule{Use: "b/big", Intent: "debugging"})
j.choice, j.sure = "debugging", 0.3
g, _, _ := provider.FindGroup("group/r")
sure := 0.25
g.IntentSure = &sure
if err := provider.SaveGroup(g); err != nil {
t.Fatal(err)
}
other := provider.Group{ID: "other", Name: "Other", Members: []string{"a/small", "b/big"}, Routing: provider.Rotate, Classifier: "ts/jev-latest",
Rules: []provider.Rule{{Use: "a/small", Intent: "debugging"}}}
if err := provider.SaveGroup(other); err != nil {
t.Fatal(err)
}
out, r := postOK(t, s, "p1", chatFor("group/other", "why does this crash?", ""))
if !strings.Contains(out, "from ka") || r.Rule.N != 0 || r.Rule.Classified.Intent != "" {
t.Fatalf("other group at 0.4: %s %+v", out, r.Rule.Classified)
}
out, r = postOK(t, s, "p2", chat("why does this crash, again?", nil, 0, ""))
if !strings.Contains(out, "from kb") || r.Rule.N != 1 || r.Rule.Classified.Intent != "debugging" {
t.Fatalf("ruled group at 0.25: %s %+v", out, r.Rule.Classified)
}
}
// The group's IntentSure out of range is refused, 0 through 1 kept.
func TestGroupIntentSureRange(t *testing.T) {
for _, x := range []float64{-0.1, 1.1} {
g := provider.Group{ID: "bad", Name: "Bad", Members: []string{"a/small", "b/big"}, Classifier: "c/cls",
Rules: []provider.Rule{{Use: "a/small", Intent: "debugging"}}}
sure := x
g.IntentSure = &sure
if err := provider.SaveGroup(g); err == nil {
t.Fatalf("%v saved", x)
}
}
}
// Levels are another scale (every message at one of them): the torn keep
// the turn before's level, IntentSure or no IntentSure (#5101 leaves them
// as they are).
func TestGroupIntentSureLeavesLevels(t *testing.T) {
s, _, _, j := jevved(t, provider.EffortAuto, provider.Rule{Use: "a/small", Intent: "简单任务"}, provider.Rule{Use: "b/big", Intent: "复杂任务"})
j.levels, j.sure = 0.9, 0.09
g, _, _ := provider.FindGroup("group/r")
sure := 0.0
g.IntentSure = &sure
if err := provider.SaveGroup(g); err != nil {
t.Fatal(err)
}
// torn over the level, the turn before's is kept, as ever
_, r := postOK(t, s, "l1", chat("写个纸牌游戏", nil, 0, ""))
if r.Rule.N != 0 || r.Rule.Classified.Intent != "" || r.Rule.Classified.Sure != 0.09 {
t.Fatalf("torn level, none before: %+v", r.Rule.Classified)
}
}