gateway: a remote magpie's out-of-quota refusal is asked again, not replayed here (zaidu259434 on X)
A routing group on another computer's magpie had one member, Open Design, out of its Coding Plan quota and wallet. This magpie rested that remote model (remoteModelRest) and, through quotaHeld, gave the 402 again on every request until the time it named, never asking the remote. The user moved the group to another vendor there, and Codex here still got "Remote magpie: Open Design: [code=insufficient_balance] ... (out of its allowance: not asked again before 06:19)". The remote magpie routes its own groups and holds its own rests, and what it serves under a name can change there at any moment. A rest from remoteModelRest is now marked (Rest.there), and quotaHeld passes it by: the remote is asked, and answers from its own hold at once, or from what it serves now. The rest still puts the model behind the others in a local group. verifyHeld is unchanged: its hold is a minute, and TestRemoteMagpieVerifyHeldOnTheModel asks for it. Run (on mote, atcc81ad4eaplus this change): - TestRemoteMagpieQuotaIsAskedNotReplayed and TestHandAddedMagpieQuotaIsAskedNotReplayed: red oncc81ad4ea(402 replayed, remote asked 0 times), green with the change. - go test ./internal/gateway -run 'QuotaIsAskedNotReplayed|RemoteMagpie |AddedByHand|QuotaHeld|1772': ok. A wider run (Held|Quota|Verify| Spent|Credit) is ok except TestAntigravityTurnedAwayLastIsNoQuota and TestCodexSpentGrokFailingIsTold, red alone on untouchedcc81ad4eatoo (#4362). - go vet ./internal/gateway, GOOS=linux (nogui) and GOOS=windows: ok. - Two sandbox magpies (own HOMEs, free ports) and a fake vendor: R with group never-stuck = opendesign/od-model (402 insufficient_balance), L with R as a remote-magpie provider. Old code: after `group set never-stuck models=newvendor/new-model` on R, L kept answering the reporter's exact 402 and the vendor never saw new-model. With the change: 200 from new-model at once. With both members in the group (order), R falls through: od-model asked once, then new-model answers every request. Broken by hand: the change taken out, both new tests go red as above. Not tried: a real Open Design account, the reporter's phone setup, and the full Go suite (to run on mote when it is free).
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -1076,14 +1076,18 @@ func restingOf(c candidate) (Rest, bool) {
|
||||
// exhausted" for the moment it switched upstreams, and every request,
|
||||
// the agent's retry first, was answered from its 15-minute rest. A
|
||||
// subscription account isn't held so: its resets may be spent for it
|
||||
// (autoReset), and its windows are read again as it is planned.
|
||||
// (autoReset), and its windows are read again as it is planned. Nor is a
|
||||
// model of another computer's magpie (Rest.there): that magpie holds its
|
||||
// own rests, and the user may have changed what it serves since
|
||||
// (zaidu259434 on X: a group there moved off a spent vendor, and its
|
||||
// refusal was still given here until the time it named).
|
||||
func quotaHeld(cs []candidate) (Rest, time.Time, bool) {
|
||||
var first Rest
|
||||
var back time.Time
|
||||
now := time.Now()
|
||||
for i, c := range cs {
|
||||
r, ok := restingOf(c)
|
||||
if !ok || c.p.Account != nil || r.Why != failQuota && r.Why != failCredit || !quotaStatus(r.Status) || r.said == "" {
|
||||
if !ok || c.p.Account != nil || r.there || r.Why != failQuota && r.Why != failCredit || !quotaStatus(r.Status) || r.said == "" {
|
||||
return Rest{}, time.Time{}, false
|
||||
}
|
||||
at := r.asksAt(now)
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
package gateway
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/yetone/magpie/internal/provider"
|
||||
)
|
||||
|
||||
// zaidu259434 on X: a routing group on another computer's magpie ("永不卡住")
|
||||
// had one member, Open Design, out of its Coding Plan quota and wallet. The
|
||||
// remote answered 402, its own hold naming when it would ask again
|
||||
// (Retry-After), and this magpie rested the remote's group model until then
|
||||
// and gave the 402 again on every request, never asking the remote. The
|
||||
// user moved the group to another vendor there, and Codex here still got
|
||||
// Open Design's refusal: the remote magpie routes its own groups and holds
|
||||
// its own rests, and what it serves under a name can change at any moment,
|
||||
// so a refusal of its is never given again here without asking it.
|
||||
func TestRemoteMagpieQuotaIsAskedNotReplayed(t *testing.T) {
|
||||
fresh(t)
|
||||
var mu sync.Mutex
|
||||
spent := true
|
||||
asked := 0
|
||||
remote := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var req struct {
|
||||
Model string `json:"model"`
|
||||
}
|
||||
json.NewDecoder(r.Body).Decode(&req)
|
||||
mu.Lock()
|
||||
asked++
|
||||
out := spent
|
||||
mu.Unlock()
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if out {
|
||||
// what a remote magpie holding its group's lone member says
|
||||
w.Header().Set("Retry-After", "3600")
|
||||
w.Header().Set(providerHeader, "opendesign")
|
||||
w.WriteHeader(http.StatusPaymentRequired)
|
||||
io.WriteString(w, `{"error":{"type":"api_error","message":"Open Design: [code=insufficient_balance] Coding Plan quota is exhausted and wallet balance is insufficient (out of its allowance: not asked again before 06:19 UTC+1)"}}`)
|
||||
return
|
||||
}
|
||||
io.WriteString(w, `{"id":"x","choices":[{"index":0,"message":{"role":"assistant","content":"from `+req.Model+` on its new member"},"finish_reason":"stop"}]}`)
|
||||
}))
|
||||
t.Cleanup(remote.Close)
|
||||
if err := provider.Save(provider.Provider{ID: "office", Name: "Remote magpie", Preset: provider.RemoteMagpiePreset, Key: "k",
|
||||
Chat: remote.URL + "/v1", Models: []string{"group/never-stuck"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s := New()
|
||||
body := `{"model":"office/group/never-stuck","messages":[{"role":"user","content":"hi"}]}`
|
||||
if code, got := postAs(t, s, "", body); code != http.StatusPaymentRequired {
|
||||
t.Fatalf("the remote's 402: %d %s", code, got)
|
||||
}
|
||||
// a second refusal, as the agent's retry meets it
|
||||
postAs(t, s, "", body)
|
||||
// the group is changed on the remote
|
||||
mu.Lock()
|
||||
spent, asked = false, 0
|
||||
mu.Unlock()
|
||||
code, got := postAs(t, s, "", body)
|
||||
mu.Lock()
|
||||
n := asked
|
||||
mu.Unlock()
|
||||
if code != http.StatusOK || !strings.Contains(got, "new member") || n != 1 {
|
||||
t.Fatalf("after the group changed on the remote: %d %s; the remote was asked %d times", code, got, n)
|
||||
}
|
||||
}
|
||||
|
||||
// The same for a magpie added by hand, as a plain OpenAI-compatible
|
||||
// provider: its reply names the provider that answered there.
|
||||
func TestHandAddedMagpieQuotaIsAskedNotReplayed(t *testing.T) {
|
||||
fresh(t)
|
||||
var mu sync.Mutex
|
||||
spent := true
|
||||
remote := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
mu.Lock()
|
||||
out := spent
|
||||
mu.Unlock()
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if out {
|
||||
w.Header().Set("Retry-After", "3600")
|
||||
w.Header().Set(providerHeader, "opendesign")
|
||||
w.WriteHeader(http.StatusPaymentRequired)
|
||||
io.WriteString(w, `{"error":{"type":"api_error","message":"Open Design: [code=insufficient_balance] wallet balance is insufficient"}}`)
|
||||
return
|
||||
}
|
||||
io.WriteString(w, `{"id":"x","choices":[{"index":0,"message":{"role":"assistant","content":"answered"},"finish_reason":"stop"}]}`)
|
||||
}))
|
||||
t.Cleanup(remote.Close)
|
||||
if err := provider.Save(provider.Provider{ID: "desk", Name: "Desk", Key: "k",
|
||||
Chat: remote.URL + "/v1", Models: []string{"group/g"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
s := New()
|
||||
body := `{"model":"desk/group/g","messages":[{"role":"user","content":"hi"}]}`
|
||||
postAs(t, s, "", body)
|
||||
postAs(t, s, "", body)
|
||||
mu.Lock()
|
||||
spent = false
|
||||
mu.Unlock()
|
||||
if code, got := postAs(t, s, "", body); code != http.StatusOK || !strings.Contains(got, "answered") {
|
||||
t.Fatalf("after the group changed there: %d %s", code, got)
|
||||
}
|
||||
}
|
||||
@@ -595,6 +595,14 @@ type Rest struct {
|
||||
// held every request for 15 minutes or more
|
||||
first bool
|
||||
askAt time.Time
|
||||
// there: another computer's magpie refused one of its models
|
||||
// (remoteModelRest). It routes that model there and holds its own
|
||||
// rests, and what it serves under a name can change at any moment (a
|
||||
// group moved to another vendor), so its refusal out of quota or
|
||||
// credit is never given again here without asking it (quotaHeld):
|
||||
// asked, it answers from its own hold at once, or from what it serves
|
||||
// now
|
||||
there bool
|
||||
}
|
||||
|
||||
// asksAt is when r, a key's rest out of its allowance or credit, has it
|
||||
@@ -1316,7 +1324,7 @@ func (s *Server) restAfterMarked(c candidate, status int, header http.Header, bo
|
||||
id = c.restID()
|
||||
}
|
||||
if remoteModelRest(c, why, header) {
|
||||
id = c.restID()
|
||||
id, r.there = c.restID(), true
|
||||
}
|
||||
r.Key = id
|
||||
if id == c.restID() {
|
||||
|
||||
Reference in New Issue
Block a user