gateway: a decision model's tries share the one classifier wait

Settings says the Classifier timeout is "how long the classifier is
given to answer, its provider's failover included". askDecideTries gave
each key, account or decision-group member a fresh classifyTimeout of
its own (askJev made its own context), so a decision provider whose keys
didn't answer held the turn one whole wait per try before its route
began: 3 keys at the 60s maximum is 3m, past the 2m unrouted limit that
stops a request with a 504. Found while looking for what held Jason's
Codex request for 2m (Discord, #general). Whether his setup has a
decision model is not known, so this is one possible cause, not the
confirmed one.

Now askDecideTries makes one context of classifyTimeout for all its
tries, and askJev asks within it. Once a try spends the wait, the rest
are neither asked nor rested. The chat-model classifier (askChat)
already had one wait, failover included, through s.serve.

TestDecisionTriesShareTheClassifierWait: a decision provider of three
keys that never answer, the 8s default at 25ms a second. It wants the
error "no answer in 200ms" with only the first key asked. Broken by
hand (a fresh wait per try, the stop removed): red, "asked [w1 w2 w3]
in one classifier's wait, want the first key alone". Fixed: green.

Run:
- mote: go test ./internal/gateway -run
  'Decision|Decide|Jev|Classif|SystemOne|Unrouted|Held': ok (63s).
- Full suite: go test ./... -timeout 45m -count=1 on the Windows box at
  this commit (mote was unreachable), still running at push; owner rule: ship
  at once, any new red filed afterwards.
Not tried: a real decision provider with several keys.
This commit is contained in:
yetone
2026-10-29 15:10:34 +08:00
parent 29185e3bbc
commit be8b0f3816
4 changed files with 62 additions and 8 deletions
+5 -1
View File
@@ -458,7 +458,11 @@ a key out of quota, fflake33 on Discord) is asked the same way, at
In order), `probeOne` leaves out all but the resting one back soonest, and
`serveDecideTries` / `askDecideTries` ask each in turn as for a group, a
refused one resting. Before, every call went to its first key, whatever the
others' state. A gateway key held to some accounts is asked only on those
others' state. As a classifier, every try shares the one classifier's wait
(Settings' Classifier timeout, "its provider's failover included"): once a
try spends it, the rest are neither asked nor rested. Each try was given
the whole wait before, so a request was held a wait per key or member
before its route began (`TestDecisionTriesShareTheClassifierWait`). A gateway key held to some accounts is asked only on those
(`allowedCandidates`), and refused 403 when none of them serves the model.
`TestDecisionProviderKeysFailOver`.
[`embeddings.go`](../../internal/gateway/embeddings.go) relays retrieval
+4 -5
View File
@@ -354,10 +354,10 @@ func (s *Server) jevLevelled(ctx context.Context, p provider.Provider, model str
// says which provider and how long it was given.
var errNoAnswer = errors.New("no answer in the classifier's wait")
// askJev asks a decision provider's model through its System One API.
func (s *Server) askJev(p provider.Provider, model string, intents []string, prev before, effort bool, text string) (v verdict, err error) {
wait := classifyTimeout()
ctx, cancel := context.WithTimeout(context.Background(), wait)
// askJev asks a decision provider's model through its System One API,
// within ctx: the classifier's wait, which is wait long, that every try
// of askDecideTries shares.
func (s *Server) askJev(ctx context.Context, wait time.Duration, p provider.Provider, model string, intents []string, prev before, effort bool, text string) (v verdict, err error) {
defer func() {
// only a call cut off by the wait is reworded: a refusal or an
// answer read as the wait ran out keeps its own type, which
@@ -365,7 +365,6 @@ func (s *Server) askJev(p provider.Provider, model string, intents []string, pre
if errors.Is(err, errNoAnswer) {
err = fmt.Errorf("%s gave no answer in %s", p.Name, wait)
}
cancel()
}()
if model == "" {
model = p.Jev()
+11 -2
View File
@@ -294,10 +294,19 @@ func (s *Server) askDecider(p provider.Provider, model string, intents []string,
// askDecideTries asks each of cs as Jev is (askJev) until one answers, the
// one that failed resting as it would for a chat; the last one's error
// when none does.
// when none does. Every try shares the one classifier's wait, as Settings
// says it is given, its failover included: a try each given the whole
// wait held a request N waits before routing it (Jason on Discord, a 2m
// hold).
func (s *Server) askDecideTries(cs []candidate, intents []string, prev before, effort bool, text string) (v verdict, err error) {
wait := classifyTimeout()
ctx, cancel := context.WithTimeout(context.Background(), wait)
defer cancel()
for _, c := range cs {
if v, err = s.askJev(c.p, c.model, intents, prev, effort, text); err == nil {
if ctx.Err() != nil {
break // the wait is spent: the rest are not asked, nor rested
}
if v, err = s.askJev(ctx, wait, c.p, c.model, intents, prev, effort, text); err == nil {
servedCandidate(c, 0)
return v, nil
}
+42
View File
@@ -8,6 +8,7 @@ import (
"strings"
"sync"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
@@ -247,3 +248,44 @@ func TestDecisionProviderKeysFailOver(t *testing.T) {
})
}
}
// A decision model's keys share the one classifier's wait, as Settings
// says it is given, its failover included: a key that never answers
// spends it, and the next is not asked with a whole wait of its own. Each
// try given the whole wait held a request a wait per key before routing
// it (Jason on Discord, held 2m and stopped with a 504).
func TestDecisionTriesShareTheClassifierWait(t *testing.T) {
fresh(t)
was := classifyUnit
classifyUnit = 25 * time.Millisecond // the 8 seconds unset are 200ms
t.Cleanup(func() { classifyUnit = was })
var mu sync.Mutex
var asked []string
stop := make(chan struct{})
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mu.Lock()
asked = append(asked, strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer "))
mu.Unlock()
// never answers: the body read, so a client gone ends r's context
io.Copy(io.Discard, r.Body)
select {
case <-r.Context().Done():
case <-stop:
}
}))
t.Cleanup(up.Close)
t.Cleanup(func() { close(stop) }) // runs before up.Close
if err := provider.Save(provider.Provider{ID: "jw", Name: "JW", Key: "w1", Keys: []provider.KeyAccount{{Key: "w2"}, {Key: "w3"}}, Decide: up.URL + "/v1"}); err != nil {
t.Fatal(err)
}
s := New()
_, err := s.askClassifier("jw/jev-latest", []string{"feature", "bug"}, before{}, false, "fix this")
if err == nil || !strings.Contains(err.Error(), "no answer in 200ms") {
t.Fatalf("a decision model that never answers: %v", err)
}
mu.Lock()
defer mu.Unlock()
if len(asked) != 1 {
t.Fatalf("asked %v in one classifier's wait, want the first key alone", asked)
}
}