gateway: another magpie added by hand rests only the model it refused
lagrangepoint1813 on Discord: Pi on a magpie whose provider is another
computer's magpie got "Remote Magpie: Claude Code: You've hit your
session limit" for every model of it, whichever Pi was switched to.
remoteModelRest rests such a refusal by model (restID) only for the
Remote magpie preset; that magpie was added as an OpenAI-compatible
provider of the user's own name (preset null in providers.json, the
reporter found), so the 429 rested the whole key
(restKey) and quotaHeld answered every model of it from that rest for
the five hours it named. A magpie's reply names the provider that
answered or held it (X-Magpie-Provider), as no vendor's does: keepRetry now notes it as
answeredThereHeader (this magpie writes its own provider into the same
header of its reply, so it can't be read there; the note is kept from
the client), and remoteModelRest reads a refusal carrying it as another
magpie's model's, whatever the provider was added as. A quotaHeld or
verifyHeld answer now names its member too (noteMember), as an asked
one does, so a refusal the other magpie held is read the same. The
key's own limit there (remoteKeyLimitHeader) still rests it whole.
TestAnotherMagpieAddedByHandRestsTheModel (two gateways over HTTP; the
other magpie asking Claude Code, and already holding it): both subtests
red on the old code with the reporter's message, green with this
change; -count=3 with TestRemoteMagpie* and Verify* green. Broken by
hand: reading X-Magpie-Provider straight from the reply header rested
by model everywhere (TestOwnQuotaRest, TestVerifyRefusalRestsTheLastOne,
TestAntigravityQuotaStillRests, TestCodexCompactCutSummaryFailsOver,
TestOpenRouterFreeAccountRateLimitKeepsProviderRest went red, the header
being this magpie's own); without noteMember on the held answer the
held-there subtest stays red.
Run at this head (on origin/main a1174674d):
- go test ./internal/gateway -count=1 -timeout 45m, both
commits together: ok (391s)
- gofmt -l internal/gateway: clean; go vet ./internal/gateway, also
GOOS=linux and GOOS=windows: ok
Not changed: a held answer's "(out of its allowance: …)" is said
twice when the other magpie held it too (seen in the old code's red);
the model alone is held now, the wording is left as it was (#3701).
Not changed: a Remote magpie's key-limit header doesn't reach
remoteModelRest in the main loop either (keepRetry doesn't copy it),
filed as #3705. A reply from a hand-added magpie that breaks off after
its 200 still rests the key whole (no quotaHeld answer follows from it).
Not tried: the reporter's real Claude subscription on the other magpie.
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -1817,7 +1817,7 @@ func (h *holdWriter) pass() {
|
||||
}
|
||||
dst := h.w.Header()
|
||||
for k, v := range h.header {
|
||||
if k != resetsHeader && k != refusedHeader && k != zdrHeader { // magpie's own notes, for restAfter and settle
|
||||
if k != resetsHeader && k != refusedHeader && k != zdrHeader && k != answeredThereHeader { // magpie's own notes, for restAfter and settle
|
||||
dst[k] = v
|
||||
}
|
||||
}
|
||||
@@ -2222,7 +2222,7 @@ func (h *holdWriter) keepAlive() {
|
||||
}
|
||||
dst := h.w.Header()
|
||||
for k, v := range src {
|
||||
if k != resetsHeader && k != refusedHeader && k != zdrHeader && k != "Content-Length" {
|
||||
if k != resetsHeader && k != refusedHeader && k != zdrHeader && k != answeredThereHeader && k != "Content-Length" {
|
||||
dst[k] = slices.Clone(v)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2768,6 +2768,7 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
|
||||
// reconnects are told so again, not sent on to a vendor that
|
||||
// just refused it
|
||||
held, call.Status, call.Error = true, http.StatusForbidden, said
|
||||
noteMember(hw, r, c.p, c.model)
|
||||
writeError(hw, from, call.Status, said)
|
||||
} else if rested, back, ok := quotaHeld(cands[i:]); ok && last {
|
||||
// it rests out of its allowance, and so does every key that
|
||||
@@ -2778,6 +2779,9 @@ func (s *Server) serve(w http.ResponseWriter, r *http.Request, from provider.Pro
|
||||
// told to ask then rather than at once
|
||||
msg := fmt.Sprintf("%s (out of its allowance: not asked again before %s)", rested.said, heldClock(back, time.Now()))
|
||||
held, heldBack, call.Status, call.Error = true, back, rested.Status, c.p.Name+": "+msg
|
||||
// the answer names who held it, as one asked would: a magpie
|
||||
// using this one rests that model of it alone (remoteModelRest)
|
||||
noteMember(hw, r, c.p, c.model)
|
||||
writeError(hw, from, call.Status, msg)
|
||||
} else {
|
||||
// a stream that fails before anything is said is let go at
|
||||
|
||||
@@ -101,3 +101,95 @@ func TestRemoteMagpieKeyLimitRestsTheKey(t *testing.T) {
|
||||
t.Error("a provider that isn't a remote magpie rests the model on a quota refusal")
|
||||
}
|
||||
}
|
||||
|
||||
// lagrangepoint1813 on Discord: another computer's magpie added as an
|
||||
// OpenAI-compatible provider of its own name, not the Remote magpie
|
||||
// preset, said its Claude Code was out of its session limit, and every
|
||||
// model of it was refused here with that 429 for the five hours it
|
||||
// named, whichever one Pi was switched to. A reply that came from a
|
||||
// magpie (it names the provider that answered, providerHeader) is that
|
||||
// magpie's model's, whatever the provider here was added as.
|
||||
func TestAnotherMagpieAddedByHandRestsTheModel(t *testing.T) {
|
||||
t.Run("asked there", func(t *testing.T) { anotherMagpieAddedByHand(t, false) })
|
||||
t.Run("held there", func(t *testing.T) { anotherMagpieAddedByHand(t, true) })
|
||||
}
|
||||
|
||||
func anotherMagpieAddedByHand(t *testing.T, held bool) {
|
||||
clineRestsCleared(t)
|
||||
spent := `{"type":"error","error":{"type":"rate_limit_error","message":"You've hit your session limit · resets 8:10pm (America/Los_Angeles)"}}`
|
||||
cc := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
io.Copy(io.Discard, r.Body)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusTooManyRequests)
|
||||
io.WriteString(w, spent)
|
||||
}))
|
||||
t.Cleanup(cc.Close)
|
||||
var asked atomic.Int32
|
||||
q := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
io.Copy(io.Discard, r.Body)
|
||||
asked.Add(1)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
io.WriteString(w, `{"id":"c1","object":"chat.completion","model":"fast","choices":[{"index":0,"message":{"role":"assistant","content":"hello"},"finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":1}}`)
|
||||
}))
|
||||
t.Cleanup(q.Close)
|
||||
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
||||
t.Setenv("XDG_CACHE_HOME", t.TempDir())
|
||||
for _, p := range []provider.Provider{
|
||||
{ID: "cc", Name: "Claude Code", Key: "k1", Models: []string{"claude-opus-5-5"}, Anthropic: cc.URL},
|
||||
{ID: "qd", Name: "Qoder", Key: "k2", Models: []string{"fast"}, Chat: q.URL},
|
||||
} {
|
||||
if err := provider.Save(p); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
remote := httptest.NewServer(New().Handler())
|
||||
t.Cleanup(remote.Close)
|
||||
// as it is added by hand: an OpenAI-compatible endpoint, a key, the
|
||||
// other magpie's model ids
|
||||
if err := provider.Save(provider.Provider{ID: "rm", Name: "Remote Magpie", Key: "k3", Chat: remote.URL + "/v1",
|
||||
Models: []string{"cc/claude-opus-5-5", "qd/fast"}}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
send := func(model string) *httptest.ResponseRecorder {
|
||||
t.Helper()
|
||||
req := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(`{"model":"`+model+`","messages":[{"role":"user","content":"hi"}]}`))
|
||||
rec := httptest.NewRecorder()
|
||||
New().Handler().ServeHTTP(rec, req)
|
||||
return rec
|
||||
}
|
||||
if held {
|
||||
// the other magpie already holds Claude Code, refused twice
|
||||
// there, and answers it without asking: its answer names the
|
||||
// provider all the same
|
||||
for range 2 {
|
||||
send("cc/claude-opus-5-5")
|
||||
}
|
||||
if rec := send("cc/claude-opus-5-5"); !strings.Contains(rec.Body.String(), "not asked again") {
|
||||
t.Fatalf("the other magpie asked Claude Code again: %d %s; want it held there", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
if rec := send("rm/cc/claude-opus-5-5"); rec.Code != http.StatusTooManyRequests {
|
||||
t.Fatalf("Claude Code out of its session limit there: %d %s; want its 429", rec.Code, rec.Body.String())
|
||||
}
|
||||
rec := send("rm/qd/fast")
|
||||
if rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), "hello") {
|
||||
t.Fatalf("Qoder there, after Claude Code's 429: %d %s; want Qoder's answer", rec.Code, rec.Body.String())
|
||||
}
|
||||
if asked.Load() != 1 {
|
||||
t.Errorf("Qoder was asked %d times; want once", asked.Load())
|
||||
}
|
||||
if rec := send("rm/cc/claude-opus-5-5"); rec.Code != http.StatusTooManyRequests {
|
||||
t.Fatalf("Claude Code asked again: %d %s; want its 429", rec.Code, rec.Body.String())
|
||||
}
|
||||
if rec := send("rm/cc/claude-opus-5-5"); rec.Code != http.StatusTooManyRequests || !strings.Contains(rec.Body.String(), "not asked again") {
|
||||
t.Errorf("Claude Code asked again while it rests: %d %s; want its 429 held here", rec.Code, rec.Body.String())
|
||||
}
|
||||
// held here now, Claude Code holds nothing else of that magpie's
|
||||
rec = send("rm/qd/fast")
|
||||
if rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), "hello") {
|
||||
t.Fatalf("Qoder there, while Claude Code is held here: %d %s; want Qoder's answer", rec.Code, rec.Body.String())
|
||||
}
|
||||
if asked.Load() != 2 {
|
||||
t.Errorf("Qoder was asked %d times; want twice", asked.Load())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1177,9 +1177,14 @@ func (s *Server) restAfterMarked(c candidate, status int, header http.Header, bo
|
||||
// the refusal is read whoever answered). Its key refused, or the computer
|
||||
// unreachable, still rests it whole, and so does the key's own limit there
|
||||
// (keyLimited's 429, with remoteKeyLimitHeader), which refuses every model
|
||||
// alike (#2077).
|
||||
// alike (#2077). A magpie added by hand, as an OpenAI-compatible provider
|
||||
// of its own name, is one all the same: its reply names the provider that
|
||||
// answered there (providerHeader, which keepRetry notes as
|
||||
// answeredThereHeader), as no vendor's does (lagrangepoint1813
|
||||
// on Discord: "Remote Magpie: Claude Code: You've hit your session
|
||||
// limit" answered every model of it for the five hours it named).
|
||||
func remoteModelRest(c candidate, why string, header http.Header) bool {
|
||||
if !c.p.IsRemoteMagpie() || header.Get(remoteKeyLimitHeader) != "" {
|
||||
if !c.p.IsRemoteMagpie() && header.Get(answeredThereHeader) == "" || header.Get(remoteKeyLimitHeader) != "" {
|
||||
return false
|
||||
}
|
||||
return why == failQuota || why == failCredit || why == failRate || why == failRestricted || why == failVerify
|
||||
@@ -1357,6 +1362,12 @@ func keepRetry(dst, src http.Header, body []byte) {
|
||||
dst.Set("Retry-After", strconv.Itoa(int(d.Seconds())))
|
||||
}
|
||||
}
|
||||
if who := src.Get(providerHeader); who != "" {
|
||||
// another magpie refused it, naming its provider that did: this
|
||||
// magpie's own name for the provider goes in the same header
|
||||
// (noteMember), so it is noted beside it, for restAfter
|
||||
dst.Set(answeredThereHeader, who)
|
||||
}
|
||||
if note, ok := policyRefusal(body); ok {
|
||||
// the error the agent gets keeps the vendor's words but not its
|
||||
// code (bio_policy): that it was the safety filter goes on beside
|
||||
@@ -1370,6 +1381,11 @@ func keepRetry(dst, src http.Header, body []byte) {
|
||||
}
|
||||
}
|
||||
|
||||
// answeredThereHeader carries, from keepRetry to restAfter, the provider
|
||||
// another magpie named on its refusal (providerHeader): the refusal is
|
||||
// that magpie's model's (remoteModelRest).
|
||||
const answeredThereHeader = "X-Magpie-Answered-There"
|
||||
|
||||
// refusedHeader carries, from keepRetry to settle, that an error status
|
||||
// was the safety filter refusing the request, and what it said.
|
||||
const refusedHeader = "X-Magpie-Refused"
|
||||
|
||||
Reference in New Issue
Block a user