ESern29751 on X (v0.1.4341): in a routing group with intent recognition, magpie often 499-cancels the intent classifier's request, with MiniMax, GPT-6 Luna and Grok 4.6 as the classifier. Cause: those models reason before they answer. The classifier asks for reasoning_effort none, but MiniMax thinks anyway and Grok 4.6 and Luna go no lower than low. Through the live gateway with the classify prompt over a real routing prompt (~5k tokens of context): grok-4.6 took 4.7-8.1s (288-462 reasoning tokens), minimax-m2.5 3.7-10.3s (439-1012 output tokens), gpt-6-luna 1.2-7.2s; cold calls reached 12-29s. The unset classifier timeout was 8 seconds, so most turns were cut off, fell back to the group's order and lost their intent routing. Each cut call was then logged 499 "the agent canceled the request", which usage counts as no failure, though no agent had canceled anything: askChat's own context.WithTimeout had no cause, the mislabel #2318 fixed for an image's description but never for the classifier. Fix: - settings.ClassifyTimeoutDefault 8 -> 15 (an existing Settings segment). A user who saved 8 keeps 8; only the unset value moves. The GUI's fallback segment follows (app.js), and the docs (en/zh/ja intent and start, gateway-routing.md) say 15. - askChat's wait is context.WithTimeoutCause(..., sideTimedOut{"decision model", wait}): the usage row says "magpie stopped waiting: its decision model had no answer in Ns", a failure. - Siblings checked: the advisor and Codex titles use the client's context, compaction and vision already carry sideTimedOut; askJev (decision providers) logs no 499 row of its own. - Tests that assumed the 8-second unset wait now use 15 (classify, decide, decide_group, nested_classify_wait, settings). - app.js grows by the one byte of the fallback's second digit: both core budgets +1 with the reason (TestCoreScriptBudget was red at exactly 1 over without it). A shorter `|| 15` would show a hand-edited negative value as its own segment, so it wasn't taken. - site: llms-full.txt and the search indexes regenerated for the docs pages (node llms-full.mjs, node search-index.mjs). Test: TestReasoningClassifierAnswersInTheWaitUnset - a classifier answering in 10 units routes the turn with the timeout unset, and one slower than 60 units gives no answer with one 499 usage row that says magpie stopped waiting for its decision model and counts as a failure. Broken by hand (mica-run): with askChat back on a plain WithTimeout the test is red ("the classifier's cut call: 499 "the agent canceled the request""); with the default back at 8 it is red ("gave no answer in 400ms"). Run at 0479372b8, this commit's tree (mica, through ~/suite.lock; mote was over its cores): - gofmt -l: clean. go vet (darwin), GOOS=linux go vet -tags nogui, GOOS=windows go vet: rc 0 (darwin vet rc 0 at the first try on mote). - go test -tags nogui -count=1 -timeout 45m ./...: rc 0, 103 ok. (The first run, on mote before the budget raise and the rebase ontoadfa196db, was red on TestCoreScriptBudget alone.) - node --test site/*.test.mjs: 132 of 132. - GUI (node --test, Chromium and WebKit, five languages, 1100 and 420): classify-timeout-setting, settings-places, i18n-en-no-cjk green; i18n-keys red only on "a plugin's words are its own" over claude_image_budget_test.go's ", with an image", already #6877 from2896e82cc, untouched here. Not tried: the real reporter's group in the GUI with these models after the change (the timings above were measured through the live gateway before it); the whole GUI suite (only a fallback value changed in app.js).
586 lines
24 KiB
Go
586 lines
24 KiB
Go
package gateway
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/access"
|
|
"github.com/yetone/magpie/internal/catalog"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
"github.com/yetone/magpie/internal/usage"
|
|
)
|
|
|
|
// clsUp is a Chat vendor that classifies: it answers what answer says,
|
|
// after wait, and keeps what it was asked.
|
|
type clsUp struct {
|
|
mu sync.Mutex
|
|
answer string
|
|
fail int
|
|
wait time.Duration
|
|
asked []string
|
|
agents []string
|
|
}
|
|
|
|
func (u *clsUp) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
b, _ := io.ReadAll(r.Body)
|
|
u.mu.Lock()
|
|
u.asked = append(u.asked, string(b))
|
|
answer, fail, wait := u.answer, u.fail, u.wait
|
|
u.mu.Unlock()
|
|
if wait > 0 {
|
|
select {
|
|
case <-time.After(wait):
|
|
case <-r.Context().Done():
|
|
return
|
|
}
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
if fail != 0 {
|
|
w.WriteHeader(fail)
|
|
io.WriteString(w, `{"error":{"message":"classifier down"}}`)
|
|
return
|
|
}
|
|
io.WriteString(w, `{"id":"c","choices":[{"index":0,"message":{"role":"assistant","content":`+quote(answer)+`},"finish_reason":"stop"}],"usage":{"prompt_tokens":50,"completion_tokens":1}}`)
|
|
}
|
|
|
|
func (u *clsUp) n() int {
|
|
u.mu.Lock()
|
|
defer u.mu.Unlock()
|
|
return len(u.asked)
|
|
}
|
|
|
|
func (u *clsUp) last() string {
|
|
u.mu.Lock()
|
|
defer u.mu.Unlock()
|
|
return u.asked[len(u.asked)-1]
|
|
}
|
|
|
|
func (u *clsUp) set(answer string, fail int) {
|
|
u.mu.Lock()
|
|
u.answer, u.fail = answer, fail
|
|
u.mu.Unlock()
|
|
}
|
|
|
|
// intented is ruled's group with a classifier, c/cls, and the rules given.
|
|
func intented(t *testing.T, rules ...provider.Rule) (*Server, *ruleUp, *ruleUp, *clsUp) {
|
|
t.Helper()
|
|
s, a, b := ruled(t)
|
|
c := &clsUp{answer: "0"}
|
|
srv := httptest.NewServer(c)
|
|
t.Cleanup(srv.Close)
|
|
if err := provider.Save(provider.Provider{ID: "c", Name: "C", Key: "kc", Models: []string{"cls"}, Chat: srv.URL + "/v1"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := catalog.SaveLive("c", srv.URL+"/v1", []catalog.Model{{ID: "cls", Context: 32000}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
g, _, _ := provider.FindGroup("group/r")
|
|
g.Rules, g.Classifier = rules, "c/cls"
|
|
if err := provider.SaveGroup(g); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return s, a, b, c
|
|
}
|
|
|
|
func postOK(t *testing.T, s *Server, session, body string) (string, Route) {
|
|
t.Helper()
|
|
code, out := postAs(t, s, session, body)
|
|
if code != 200 {
|
|
t.Fatalf("%d %s", code, out)
|
|
}
|
|
return out, lastRoute(s)
|
|
}
|
|
|
|
// A rule with an intent: the classifier is asked as the turn begins, with
|
|
// the user's words only, and the turn goes where its answer says; the
|
|
// turn's tool rounds don't ask again.
|
|
func TestIntentRoutesATurn(t *testing.T) {
|
|
s, _, _, c := intented(t, provider.Rule{Use: "b/big", Intent: "writing or fixing tests"})
|
|
c.set("1", 0)
|
|
msg := "add a unit test for the parser <system-reminder>secret agent context</system-reminder>"
|
|
out, r := postOK(t, s, "s1", chat(msg, nil, 0, ""))
|
|
if !strings.Contains(out, "from kb") || r.Rule == nil || r.Rule.N != 1 || r.Rule.Use != "b/big" {
|
|
t.Fatalf("turn 1: %s %+v", out, r.Rule)
|
|
}
|
|
cl := r.Rule.Classified
|
|
if cl == nil || cl.By != "c/cls" || cl.Intent != "writing or fixing tests" || cl.Error != "" || cl.Cached || len(cl.Intents) != 1 {
|
|
t.Fatalf("classified %+v", cl)
|
|
}
|
|
if len(r.Rule.When) != 1 || r.Rule.When[0] != `intent "writing or fixing tests"` {
|
|
t.Fatalf("when %v", r.Rule.When)
|
|
}
|
|
asked := c.last()
|
|
if !strings.Contains(asked, `"model":"cls"`) || !strings.Contains(asked, `1. writing or fixing tests`) ||
|
|
!strings.Contains(asked, "add a unit test for the parser") || strings.Contains(asked, "secret agent context") ||
|
|
!strings.Contains(asked, `"temperature":0`) {
|
|
t.Fatalf("asked %s", asked)
|
|
}
|
|
// its tool rounds stay, and the classifier isn't asked again
|
|
out, r = postOK(t, s, "s1", chat(msg, nil, 2, ""))
|
|
if !strings.Contains(out, "from kb") || !r.Rule.Held || r.Rule.N != 1 || r.Rule.Classified != nil || c.n() != 1 {
|
|
t.Fatalf("within: %s %+v %d", out, r.Rule, c.n())
|
|
}
|
|
// the next turn is another kind: no rule, so it stays where it was
|
|
// (the group's affinity), with the classifier's answer in the trace
|
|
c.set("0", 0)
|
|
_, r = postOK(t, s, "s1", chat(msg, []string{"what does this function return?"}, 0, ""))
|
|
if r.Rule.N != 0 || r.Rule.Classified == nil || r.Rule.Classified.Intent != "" || c.n() != 2 {
|
|
t.Fatalf("turn 2: %+v %d", r.Rule, c.n())
|
|
}
|
|
// about the new message; the one before is there only as what came
|
|
// before it, the agent's context still left out
|
|
var asked2 struct {
|
|
Messages []struct{ Content string } `json:"messages"`
|
|
}
|
|
if err := json.Unmarshal([]byte(c.last()), &asked2); err != nil || len(asked2.Messages) != 2 {
|
|
t.Fatalf("turn 2 asked %v %s", err, c.last())
|
|
}
|
|
if q := asked2.Messages[1].Content; !strings.Contains(q, "<message>\nwhat does this function return?\n</message>") ||
|
|
!strings.Contains(q, "<previous_user_message>\nadd a unit test for the parser\n</previous_user_message>") ||
|
|
strings.Contains(q, "secret agent context") {
|
|
t.Fatalf("turn 2 asked about %s", c.last())
|
|
}
|
|
// it was told what the turn before was, for a message that only
|
|
// carries on from it; after a turn that was none, nothing
|
|
if r.Rule.Classified.After != "writing or fixing tests" || !strings.Contains(c.last(), "was of kind 1") {
|
|
t.Fatalf("turn 2 not told of turn 1: %+v %s", r.Rule.Classified, c.last())
|
|
}
|
|
_, r = postOK(t, s, "s1", chat(msg, []string{"what does this function return?", "go on"}, 0, ""))
|
|
if r.Rule.Classified == nil || r.Rule.Classified.After != "" || strings.Contains(c.last(), "was of kind") || c.n() != 3 {
|
|
t.Fatalf("turn 3: %+v %s", r.Rule.Classified, c.last())
|
|
}
|
|
// the classifier's call is magpie's own, in the usage, marked as the
|
|
// routing classifier's (#1839)
|
|
var seen bool
|
|
for _, call := range s.Recent() {
|
|
if call.Model == "c/cls" && call.Agent == "magpie-router" && call.Kind == "routing" {
|
|
seen = true
|
|
}
|
|
}
|
|
if !seen {
|
|
t.Fatal("the classifier's call isn't recorded as magpie-router's routing")
|
|
}
|
|
var recs, agents int
|
|
for _, r := range usage.Load(time.Time{}) {
|
|
switch {
|
|
case r.Provider == "c":
|
|
if r.Kind != "routing" || !r.ByMagpie() || usage.PurposeOf(r.Kind) != "kind:routing" {
|
|
t.Fatalf("the classifier's record: %+v", r)
|
|
}
|
|
recs++
|
|
case r.ByMagpie() || r.Kind == "routing":
|
|
t.Fatalf("an agent's record marked magpie's or routing: %+v", r)
|
|
default:
|
|
agents++
|
|
}
|
|
}
|
|
if recs != 3 || agents == 0 {
|
|
t.Fatalf("%d classifier records, %d the agent's", recs, agents)
|
|
}
|
|
}
|
|
|
|
// Several intents: the classifier chooses among those of the rules that
|
|
// may match, each once, and the first rule with its answer decides.
|
|
func TestIntentChoosesAmongRules(t *testing.T) {
|
|
s, _, _, c := intented(t,
|
|
provider.Rule{Use: "a/small", Intent: "a quick question"},
|
|
provider.Rule{Use: "b/big", Intent: "Planning a large change"},
|
|
provider.Rule{Use: "b/big", Intent: "planning a large change", Agents: []string{"codex"}},
|
|
provider.Rule{Use: "b/big", Intent: "reviewing code", Images: true}, // no image: not offered
|
|
)
|
|
c.set("2", 0)
|
|
out, r := postOK(t, s, "s2", chat("design the new sync engine", nil, 0, ""))
|
|
if !strings.Contains(out, "from kb") || r.Rule.N != 2 {
|
|
t.Fatalf("%s %+v", out, r.Rule)
|
|
}
|
|
if got := r.Rule.Classified.Intents; len(got) != 2 || got[0] != "a quick question" || got[1] != "Planning a large change" {
|
|
t.Fatalf("intents %q", got)
|
|
}
|
|
if strings.Contains(c.last(), "reviewing code") || strings.Count(strings.ToLower(c.last()), "planning a large change") != 1 {
|
|
t.Fatalf("asked %s", c.last())
|
|
}
|
|
// an answer that is no number of the list matches nothing
|
|
for _, bad := range []string{"3", "maybe", ""} {
|
|
c.set(bad, 0)
|
|
out, r = postOK(t, s, "s2-"+bad, chat("tell me about "+bad+" things", nil, 0, ""))
|
|
if !strings.Contains(out, "from ka") || r.Rule.N != 0 || r.Rule.Classified.Error == "" {
|
|
t.Fatalf("answer %q: %s %+v", bad, out, r.Rule)
|
|
}
|
|
classified.Lock()
|
|
delete(classified.failed, "c/cls") // an answer that isn't one rests it too; let it be asked again
|
|
classified.Unlock()
|
|
}
|
|
// an answer with words around the number is read
|
|
c.set("Kind 1.", 0)
|
|
_, r = postOK(t, s, "s2-words", chat("what's a goroutine", nil, 0, ""))
|
|
if r.Rule.N != 1 || r.Rule.Classified.Intent != "a quick question" {
|
|
t.Fatalf("%+v", r.Rule)
|
|
}
|
|
}
|
|
|
|
// The classifier isn't asked when no rule that could be the first to
|
|
// match waits on an intent.
|
|
func TestIntentNotAskedWhenItCantMatter(t *testing.T) {
|
|
s, _, _, c := intented(t,
|
|
provider.Rule{Use: "b/big", Tokens: 1000},
|
|
provider.Rule{Use: "b/big", Intent: "debugging", Agents: []string{"codex"}},
|
|
provider.Rule{Use: "a/small", Intent: "debugging"},
|
|
)
|
|
// a long request: the tokens rule is first to match whatever it says
|
|
_, r := postOK(t, s, "n1", chat(long(2000), nil, 0, ""))
|
|
if r.Rule.N != 1 || r.Rule.Classified != nil || c.n() != 0 {
|
|
t.Fatalf("long: %+v %d", r.Rule, c.n())
|
|
}
|
|
// a short one: rule 3 may match, rule 2 isn't for this agent
|
|
c.set("1", 0)
|
|
_, r = postOK(t, s, "n2", chat("why does this panic", nil, 0, ""))
|
|
if r.Rule.N != 3 || r.Rule.Classified == nil || len(r.Rule.Classified.Intents) != 1 || c.n() != 1 {
|
|
t.Fatalf("short: %+v %d", r.Rule, c.n())
|
|
}
|
|
// an image alone has no words to classify: not asked, nothing matches
|
|
g, ms, _ := provider.FindGroup("group/r")
|
|
img := &Request{Messages: []Message{{Role: "user", Parts: []Part{{Kind: Image}}}}}
|
|
asked := 0
|
|
hit := ruleFor("img", g, ms, img, "claude", func(access.Identity, string, []string, before, bool, string) (verdict, error) {
|
|
asked++
|
|
return verdict{Intent: "debugging"}, nil
|
|
}, "")
|
|
if hit.N != 0 || hit.Classified == nil || !strings.Contains(hit.Classified.Error, "no words") || asked != 0 {
|
|
t.Fatalf("image: %+v %+v", hit, hit.Classified)
|
|
}
|
|
}
|
|
|
|
// A classifier that fails or is slow leaves the turn to the rest of the
|
|
// rules; after a failure it rests rather than making each turn wait.
|
|
func TestIntentClassifierFails(t *testing.T) {
|
|
s, _, _, c := intented(t, provider.Rule{Use: "b/big", Intent: "refactoring"})
|
|
c.set("", 500)
|
|
out, r := postOK(t, s, "f1", chat("rename this package", nil, 0, ""))
|
|
if !strings.Contains(out, "from ka") || r.Rule.N != 0 || !strings.Contains(r.Rule.Classified.Error, "classifier down") {
|
|
t.Fatalf("down: %s %+v", out, r.Rule)
|
|
}
|
|
asked := c.n()
|
|
c.set("1", 0)
|
|
out, r = postOK(t, s, "f2", chat("rename that package", nil, 0, ""))
|
|
if !strings.Contains(out, "from ka") || !strings.Contains(r.Rule.Classified.Error, "not asked again") || c.n() != asked {
|
|
t.Fatalf("resting: %s %+v %d/%d", out, r.Rule, c.n(), asked)
|
|
}
|
|
// once it has rested, it's asked again
|
|
classified.Lock()
|
|
f := classified.failed["c/cls"]
|
|
f.at = time.Now().Add(-classifyRest - time.Second)
|
|
classified.failed["c/cls"] = f
|
|
classified.Unlock()
|
|
out, r = postOK(t, s, "f3", chat("rename it back", nil, 0, ""))
|
|
if !strings.Contains(out, "from kb") || r.Rule.N != 1 || r.Rule.Classified.Error != "" {
|
|
t.Fatalf("rested: %s %+v", out, r.Rule)
|
|
}
|
|
// slow: past the timeout it's no answer
|
|
was := classifyUnit
|
|
classifyUnit = 20 * time.Millisecond // the 15 seconds unset are 300ms
|
|
t.Cleanup(func() { classifyUnit = was })
|
|
c.mu.Lock()
|
|
c.wait = 2 * time.Second
|
|
c.mu.Unlock()
|
|
t0 := time.Now()
|
|
out, r = postOK(t, s, "f4", chat("rename everything", nil, 0, ""))
|
|
if !strings.Contains(out, "from ka") || !strings.Contains(r.Rule.Classified.Error, "no answer") || time.Since(t0) > 1500*time.Millisecond {
|
|
t.Fatalf("slow: %s %+v %s", out, r.Rule, time.Since(t0))
|
|
}
|
|
}
|
|
|
|
// #2934 (aurochs-code): a Kiro subscription's haiku answered past the 8
|
|
// seconds a classifier was given, so every turn fell back. Settings'
|
|
// Classifier timeout gives it longer: a classifier slower than the 15
|
|
// seconds unset routes the turn by its answer once it is given 60, and
|
|
// one given 8 is no answer.
|
|
func TestClassifierTimeoutSetting(t *testing.T) {
|
|
was := classifyUnit
|
|
classifyUnit = 100 * time.Millisecond // 15 seconds unset are 1.5s, 8 are 800ms, 60 are 6s
|
|
t.Cleanup(func() { classifyUnit = was })
|
|
s, _, _, c := intented(t, provider.Rule{Use: "b/big", Intent: "refactoring"})
|
|
c.set("1", 0)
|
|
c.mu.Lock()
|
|
c.wait = 2 * time.Second
|
|
c.mu.Unlock()
|
|
for i, x := range []struct {
|
|
seconds int
|
|
answers bool
|
|
wait string
|
|
}{{0, false, "1.5s"}, {60, true, ""}, {8, false, "800ms"}} {
|
|
st := settings.Load()
|
|
st.ClassifyTimeout = x.seconds
|
|
if err := settings.Save(st); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// a failure rests the classifier: each case asks it afresh
|
|
classified.Lock()
|
|
delete(classified.failed, "c/cls")
|
|
classified.Unlock()
|
|
out, r := postOK(t, s, fmt.Sprintf("ct%d", i), chat(fmt.Sprintf("rename package %d", i), nil, 0, ""))
|
|
cl := r.Rule.Classified
|
|
if x.answers && (!strings.Contains(out, "from kb") || r.Rule.N != 1 || cl.Intent != "refactoring" || cl.Error != "") {
|
|
t.Fatalf("given %ds: %s %+v %+v", x.seconds, out, r.Rule, cl)
|
|
}
|
|
if !x.answers && (!strings.Contains(out, "from ka") || r.Rule.N != 0 || !strings.Contains(cl.Error, "no answer in "+x.wait)) {
|
|
t.Fatalf("given %ds: %s %+v %+v", x.seconds, out, r.Rule, cl)
|
|
}
|
|
}
|
|
}
|
|
|
|
// ESern29751 on X: a routing group whose classifier reasons before it
|
|
// answers (MiniMax, which thinks even asked for none; Grok 4.6 and GPT-6
|
|
// Luna, which go no lower than low) lost its intent routing on most turns.
|
|
// Asked with a real routing prompt they answered in 4 to 10 seconds, past
|
|
// the 8 a classifier was given unset, and every such call was logged 499
|
|
// "the agent canceled the request", though no agent had. Unset, a
|
|
// classifier that answers in 10 seconds now routes the turn, and one past
|
|
// the wait is logged as magpie stopping, a failure.
|
|
func TestReasoningClassifierAnswersInTheWaitUnset(t *testing.T) {
|
|
was := classifyUnit
|
|
classifyUnit = 50 * time.Millisecond // 10 seconds are 500ms
|
|
t.Cleanup(func() { classifyUnit = was })
|
|
s, _, _, c := intented(t, provider.Rule{Use: "b/big", Intent: "refactoring"})
|
|
c.set("1", 0)
|
|
c.mu.Lock()
|
|
c.wait = 10 * classifyUnit
|
|
c.mu.Unlock()
|
|
out, r := postOK(t, s, "rs1", chat("rename the parser package and move its tests", nil, 0, ""))
|
|
if cl := r.Rule.Classified; !strings.Contains(out, "from kb") || r.Rule.N != 1 || cl.Intent != "refactoring" || cl.Error != "" {
|
|
t.Fatalf("a classifier answering in 10s, unset: %s %+v %+v", out, r.Rule, cl)
|
|
}
|
|
// past the whole wait it is no answer, and the call magpie stopped is
|
|
// its failure, not an agent canceling
|
|
c.mu.Lock()
|
|
c.wait = time.Duration(settings.ClassifyTimeoutMost) * classifyUnit
|
|
c.mu.Unlock()
|
|
out, r = postOK(t, s, "rs2", chat("rename everything under internal", nil, 0, ""))
|
|
if cl := r.Rule.Classified; !strings.Contains(out, "from ka") || !strings.Contains(cl.Error, "no answer") {
|
|
t.Fatalf("a classifier past its wait: %s %+v %+v", out, r.Rule, cl)
|
|
}
|
|
var cut []usage.Record
|
|
for i := 0; i < 100 && len(cut) == 0; i++ {
|
|
for _, rec := range usage.Load(time.Time{}) {
|
|
if rec.Model == "cls" && rec.Status == usage.StatusCanceled {
|
|
cut = append(cut, rec)
|
|
}
|
|
}
|
|
if len(cut) == 0 {
|
|
time.Sleep(20 * time.Millisecond)
|
|
}
|
|
}
|
|
if len(cut) != 1 {
|
|
t.Fatalf("the classifier's cut call in the usage log: %+v", cut)
|
|
}
|
|
rec := cut[0]
|
|
if !strings.Contains(rec.Error, "magpie stopped waiting") || !strings.Contains(rec.Error, "decision model") || usage.CanceledBy(rec.Status, rec.Error) || !rec.Failed() {
|
|
t.Fatalf("the classifier's cut call: %d %q", rec.Status, rec.Error)
|
|
}
|
|
}
|
|
|
|
// A classifier that fails, times out or rests on a turn whose conversation
|
|
// had an intent keeps that intent, marked as kept, rather than sending the
|
|
// turn to the group's first member (#1826: two timeouts in a row sent
|
|
// 复杂 turns to the cheap member); one with none before has none.
|
|
func TestIntentClassifierFailsKeepsThePreviousIntent(t *testing.T) {
|
|
s, _, _, c := intented(t, provider.Rule{Use: "b/big", Intent: "refactoring"})
|
|
c.set("1", 0)
|
|
out, r := postOK(t, s, "k1", chat("rename this package", nil, 0, ""))
|
|
if !strings.Contains(out, "from kb") || r.Rule.N != 1 || r.Rule.Classified.Fallback != "" {
|
|
t.Fatalf("turn 1: %s %+v", out, r.Rule)
|
|
}
|
|
c.set("", 500)
|
|
out, r = postOK(t, s, "k1", chat("rename this package", []string{"and the tests"}, 0, ""))
|
|
cl := r.Rule.Classified
|
|
if !strings.Contains(out, "from kb") || r.Rule.N != 1 || !strings.Contains(cl.Error, "classifier down") || cl.Intent != "refactoring" || cl.Fallback != "previous" {
|
|
t.Fatalf("down: %s %+v %+v", out, r.Rule, cl)
|
|
}
|
|
// resting after the failure, the turn after keeps it too
|
|
asked := c.n()
|
|
out, r = postOK(t, s, "k1", chat("rename this package", []string{"and the tests", "and the docs"}, 0, ""))
|
|
cl = r.Rule.Classified
|
|
if !strings.Contains(out, "from kb") || !strings.Contains(cl.Error, "not asked again") || cl.Fallback != "previous" || c.n() != asked {
|
|
t.Fatalf("resting: %s %+v %d/%d", out, cl, c.n(), asked)
|
|
}
|
|
// another conversation, with no turn before, has no intent to keep
|
|
out, r = postOK(t, s, "k2", chat("rename that package", nil, 0, ""))
|
|
if cl = r.Rule.Classified; !strings.Contains(out, "from ka") || r.Rule.N != 0 || cl.Intent != "" || cl.Fallback != "" {
|
|
t.Fatalf("no turn before: %s %+v", out, cl)
|
|
}
|
|
}
|
|
|
|
// A classifier that answers something else is up: the turn goes on without
|
|
// an intent, and the next one asks it again rather than resting.
|
|
func TestIntentOddAnswerDoesNotRest(t *testing.T) {
|
|
s, _, _, c := intented(t, provider.Rule{Use: "b/big", Intent: "refactoring"})
|
|
c.set("", 0)
|
|
out, r := postOK(t, s, "o1", chat("rename this package", nil, 0, ""))
|
|
if !strings.Contains(out, "from ka") || r.Rule.N != 0 || !strings.Contains(r.Rule.Classified.Error, "not a number") {
|
|
t.Fatalf("empty: %s %+v", out, r.Rule)
|
|
}
|
|
c.set("1", 0)
|
|
out, r = postOK(t, s, "o2", chat("rename that package", nil, 0, ""))
|
|
if !strings.Contains(out, "from kb") || r.Rule.N != 1 || r.Rule.Classified.Error != "" || c.n() != 2 {
|
|
t.Fatalf("after: %s %+v %d", out, r.Rule, c.n())
|
|
}
|
|
}
|
|
|
|
// A classifier that reasons is asked to reason least, with room for its
|
|
// reasoning and the number; one whose levels aren't known is asked for
|
|
// none (TestClassifierAsksThinkingOff).
|
|
func TestIntentClassifierEffort(t *testing.T) {
|
|
s, _, _, c := intented(t, provider.Rule{Use: "b/big", Intent: "refactoring"})
|
|
c.set("1", 0)
|
|
postOK(t, s, "e1", chat("rename this package", nil, 0, ""))
|
|
if asked := c.last(); !strings.Contains(asked, `"reasoning_effort":"none"`) || !strings.Contains(asked, `"max_tokens":2048`) {
|
|
t.Fatalf("plain: %s", asked)
|
|
}
|
|
p, _ := provider.Find("c")
|
|
for levels, want := range map[string]string{"low,high,max": "low", "none,low,high": "none", "minimal,medium": "minimal"} {
|
|
if err := catalog.SaveLive("c", p.Chat, []catalog.Model{{ID: "cls", Context: 32000, Efforts: strings.Split(levels, ",")}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
postOK(t, s, "e-"+levels, chat("rename "+levels, nil, 0, ""))
|
|
if asked := c.last(); !strings.Contains(asked, `"reasoning_effort":"`+want+`"`) {
|
|
t.Fatalf("%s: %s", levels, asked)
|
|
}
|
|
}
|
|
}
|
|
|
|
// The same message is classified once: another conversation starting with
|
|
// it (an agent retrying, a subagent) reads the answer kept.
|
|
func TestIntentAnswerKept(t *testing.T) {
|
|
s, _, _, c := intented(t, provider.Rule{Use: "b/big", Intent: "refactoring"})
|
|
c.set("1", 0)
|
|
_, r := postOK(t, s, "k1", chat("split main.go", nil, 0, ""))
|
|
if r.Rule.N != 1 || r.Rule.Classified.Cached {
|
|
t.Fatalf("%+v", r.Rule)
|
|
}
|
|
_, r = postOK(t, s, "k2", chat("split main.go", nil, 0, ""))
|
|
if r.Rule.N != 1 || !r.Rule.Classified.Cached || c.n() != 1 {
|
|
t.Fatalf("%+v %d", r.Rule, c.n())
|
|
}
|
|
}
|
|
|
|
func TestUserText(t *testing.T) {
|
|
req := &Request{Messages: []Message{
|
|
{Role: "user", Parts: []Part{{Kind: Text, Text: "first"}}},
|
|
{Role: "assistant", Parts: []Part{{Kind: Text, Text: "ok"}}},
|
|
{Role: "user", Parts: []Part{{Kind: Text, Text: "<system-reminder>\nx\n</system-reminder>"}, {Kind: Text, Text: " second "}}},
|
|
}}
|
|
if got := userText(req); got != "second" {
|
|
t.Fatalf("%q", got)
|
|
}
|
|
big := strings.Repeat("a", 3000) + strings.Repeat("m", 5000) + strings.Repeat("z", 1000)
|
|
got := userText(&Request{Messages: []Message{{Role: "user", Parts: []Part{{Kind: Text, Text: big}}}}})
|
|
if !strings.HasPrefix(got, strings.Repeat("a", 3000)+"\n…\n") || !strings.HasSuffix(got, strings.Repeat("z", 1000)) || strings.Contains(got, "m") {
|
|
t.Fatalf("%d", len(got))
|
|
}
|
|
for in, want := range map[string]string{"1": "x", "2": "y", " 0 ": "", "2\n": "y"} {
|
|
got, said, err := readIntent(in, []string{"x", "y"})
|
|
if err != nil || got != want {
|
|
t.Fatalf("%q: %q %v", in, got, err)
|
|
}
|
|
if said != strings.TrimSpace(in) {
|
|
t.Fatalf("%q: said %q", in, said)
|
|
}
|
|
}
|
|
for _, in := range []string{"3", "-", "none", ""} {
|
|
if _, _, err := readIntent(in, []string{"x", "y"}); err == nil {
|
|
t.Fatalf("%q read", in)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A group whose effort is auto with a classifier that isn't Jev (a chat
|
|
// model, Jev Router on OpenRouter as much as any) has it rate each turn in
|
|
// words: the level's number, the turn's requests at that reasoning. With
|
|
// an intent too, it is asked each in a call of its own.
|
|
func TestChatClassifierPicksTheEffort(t *testing.T) {
|
|
s, a, _, c := intented(t)
|
|
g, _, _ := provider.FindGroup("group/r")
|
|
g.Effort, g.Classifier = provider.EffortAuto, "c/cls"
|
|
if err := provider.SaveGroup(g); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
c.set("3", 0) // high
|
|
_, r := postOK(t, s, "s1", chat("find why the cache races", nil, 0, `,"reasoning_effort":"low"`))
|
|
if r.Rule == nil || r.Rule.Pick != "high" || r.Rule.Classified == nil || r.Rule.Classified.Effort != "high" || r.Rule.Classified.Error != "" || c.n() != 1 {
|
|
t.Fatalf("%+v %d", r.Rule, c.n())
|
|
}
|
|
if asked := c.last(); !strings.Contains(asked, "3. high") || !strings.Contains(asked, "find why the cache races") {
|
|
t.Fatalf("asked %s", asked)
|
|
}
|
|
if !strings.Contains(a.last, `"reasoning_effort":"high"`) {
|
|
t.Fatalf("sent %s", a.last)
|
|
}
|
|
// the next turn is told what the one before needed
|
|
c.set("9", 0)
|
|
_, r = postOK(t, s, "s1", chat("find why the cache races", []string{"go on"}, 0, `,"reasoning_effort":"low"`))
|
|
if r.Rule.Classified.AfterEffort != "high" || !strings.Contains(c.last(), "needed level 3") || !strings.Contains(r.Rule.Classified.Error, "not a level from 1 to 4") || r.Rule.Pick != "" {
|
|
t.Fatalf("turn 2: %+v %s", r.Rule.Classified, c.last())
|
|
}
|
|
// an intent as well: two calls, one for each
|
|
g.Rules = []provider.Rule{{Use: "b/big", Intent: "debugging"}}
|
|
if err := provider.SaveGroup(g); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
c.set("1", 0)
|
|
_, r = postOK(t, s, "s2", chat("the parser drops a token", nil, 0, `,"reasoning_effort":"medium"`))
|
|
if r.Rule.N != 1 || r.Rule.Classified.Intent != "debugging" || r.Rule.Pick != "low" || c.n() != 4 {
|
|
t.Fatalf("both: %+v %+v %d", r.Rule, r.Rule.Classified, c.n())
|
|
}
|
|
}
|
|
|
|
// A group classifies as any request to it goes: its first model down, the
|
|
// next answers. One whose classifier is, round about, the group asking
|
|
// isn't asked again from within its own call: it goes by its other rules.
|
|
func TestIntentClassifierGroup(t *testing.T) {
|
|
s, _, _, c := intented(t, provider.Rule{Use: "b/big", Intent: "refactoring"})
|
|
d := &clsUp{}
|
|
d.set("", 500)
|
|
srv := httptest.NewServer(d)
|
|
t.Cleanup(srv.Close)
|
|
if err := provider.Save(provider.Provider{ID: "d", Name: "D", Key: "kd", Models: []string{"cls"}, Chat: srv.URL + "/v1"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := catalog.SaveLive("d", srv.URL+"/v1", []catalog.Model{{ID: "cls", Context: 32000}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := provider.SaveGroup(provider.Group{Name: "Cls", Members: []string{"d/cls", "c/cls"}, Routing: provider.Ordered}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
g, _, _ := provider.FindGroup("group/r")
|
|
g.Classifier = "group/cls"
|
|
if err := provider.SaveGroup(g); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
c.set("1", 0)
|
|
out, r := postOK(t, s, "g1", chat("rename this package", nil, 0, ""))
|
|
if !strings.Contains(out, "from kb") || r.Rule.N != 1 || r.Rule.Classified.By != "group/cls" || r.Rule.Classified.Error != "" {
|
|
t.Fatalf("failover: %s %+v %+v", out, r.Rule, r.Rule.Classified)
|
|
}
|
|
if d.n() == 0 || c.n() != 1 {
|
|
t.Fatalf("asked d %d, c %d times", d.n(), c.n())
|
|
}
|
|
// cls classifies with r, r with cls: cls's own call for r asks nobody
|
|
cg, _, _ := provider.FindGroup("group/cls")
|
|
cg.Rules, cg.Classifier = []provider.Rule{{Use: "c/cls", Intent: "anything"}}, "group/r"
|
|
if err := provider.SaveGroup(cg); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t0 := time.Now()
|
|
out, r = postOK(t, s, "g2", chat("rename that package", nil, 0, ""))
|
|
if !strings.Contains(out, "from kb") || r.Rule.N != 1 || r.Rule.Classified.Error != "" || c.n() != 2 || time.Since(t0) > 3*time.Second {
|
|
t.Fatalf("loop: %s %+v %+v %d %s", out, r.Rule, r.Rule.Classified, c.n(), time.Since(t0))
|
|
}
|
|
}
|