Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

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

yetonebe8b0f381653
diff --git a/docs/subsystems/gateway-routing.md b/docs/subsystems/gateway-routing.md
index 2b0941170..ddd4e0a14 100644
--- a/docs/subsystems/gateway-routing.md
+++ b/docs/subsystems/gateway-routing.md
@@ -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
diff --git a/internal/gateway/decide.go b/internal/gateway/decide.go
index 04395f167..fc1fb91fe 100644
--- a/internal/gateway/decide.go
+++ b/internal/gateway/decide.go
@@ -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()
diff --git a/internal/gateway/decide_group.go b/internal/gateway/decide_group.go
index a230acf62..e711a5df2 100644
--- a/internal/gateway/decide_group.go
+++ b/internal/gateway/decide_group.go
@@ -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
}
diff --git a/internal/gateway/decide_group_test.go b/internal/gateway/decide_group_test.go
index 1670baca3..8384e5701 100644
--- a/internal/gateway/decide_group_test.go
+++ b/internal/gateway/decide_group_test.go
@@ -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)
+ }
+}