Files
magpie/internal/gateway/try_now_test.go
T
yetone 67361b54fb routing: Try now from the editor reaches the Routing page, and puts a sunk key back
zoecats (Discord #feedback, 10-25, v0.1.1477): a local aggregator answered
"quota exhausted" while it switched upstreams; Try now in the provider's
editor left Routing showing 429 until magpie restarted.

Cause, in three holders:
- The Routing page kept lifts in its own liftedAt, filled only by its own
  Try now. A lift from the editor (or anywhere else) never reached it, and
  the kept routes' snapshots went on drawing the rest. The trace now
  carries every rest lifted (TraceState.Lifted, from restingUntil.lifted,
  kept up to longestMonthly), Unrest/UnrestAll/renewed wake the long-poll
  (trace.lifted), and the page leaves out a rest, a sunk key and an
  Accounts and keys count that a lift came after.
- With Sink on, a rate-limited key stayed at the back of the order until
  restart: Unrest never cleared sunk. unsink(key) now does.
- The gate itself: checked on the old binary in a sandbox HOME (single key
  over a fake answering 429 insufficient_quota "quota exhausted"). magpie
  rests it 30 min and answers later requests itself ("out of its
  allowance: not asked again before …"); Try now with the editor's key
  lifted that and the next request reached the upstream (200), on
  /v1/chat/completions and /v1/messages, streaming and not. So the stuck
  state was the display and the sink, not quotaHeld.
  Left alone, filed: #3555 (Try now lifts nothing when another magpie
  serves the gateway, unrest 400) and #3556 (the held reply's
  Retry-After, 900s here, outlives the lift for an agent that honours it).

Run at this change on its first base, 42976f9a0:
- gofmt -l clean; node --check client.js; go vet ./... on darwin, and
  GOOS=linux/windows -tags nogui: ok.
- go test -count=1 -timeout 45m ./internal/gateway/ ./internal/gui/ (suite
  lock): ok 404.9s, ok 128.9s.
- GUI, chromium and webkit, 5 languages, narrow and wide where the files
  do: rest-try-now, rate-limited-sink, routing-model-rest,
  routing-wb-restricted, routing-lifted-elsewhere (new), routing-rest-off,
  i18n-keys: 132/132. routing-lifted-elsewhere 20/20 alone. Every
  routing*, panel-routing* and rest-* file (108
  files, 1406 tests): 48 red in 8 files. routing-rule-effort, routing-pause,
  routing-live-patch, routing-rule-remove, routing-intent-entry,
  routing-manual and routing-time are red the same on origin/main
  15f0d4cde (a detached worktree, no changes) and filed (#3408-#3411,
  #2174, #3094, #3096, #3465, #1612). routing-steady flakes in both
  engines on both trees: WebKit alone, interleaved, 3 runs each, base 1
  of 3 green, this tree 1 of 3; the last group is read while the editor
  still opens (#3412, #1804).
- Sandbox HOME, fixed binary, two keys: after the editor's unrest the
  trace carried lifted, the next request went to key A (200), and the live
  Routing page went from "rest: A m1 rate limited · back in 10 min … Try
  now" to no rest.

Again on origin/main 1b039a3c7 with the next commit (its body says
what): vet, ./internal/gui, the gateway's rest and Try now tests, and
the Routing GUI files above, ok.

Broken by hand, each red, then restored:
- no unsink: TestTryNowPutsASunkAccountBack fails (order stays b,a).
- no trace.lifted, or no liftSeq wake: TestTryNowToldToTheRoutingPage's
  waiting Trace times out.
- no Lifted in Trace: Lifted empty.
- no lift recorded in renewed/UnrestAll: both tests fail.
- old client.js: routing-lifted-elsewhere times out in both engines; only
  the sunk check removed: "key c still at the back"; only the acts check:
  "Accounts and keys still counts key a resting".

Not tried: zoecats' own aggregator and harness; two magpies at once
(#3555). The Sink help text still says "Kept until magpie restarts", left
as it is: a key whose rest has ended shows no Try now.
2026-10-25 14:00:59 +08:00

144 lines
4.5 KiB
Go

package gateway
import (
"context"
"net/http"
"slices"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
func forgetLifts(t *testing.T) {
t.Helper()
clear := func() {
restingUntil.Lock()
restingUntil.lifted = map[string]time.Time{}
restingUntil.Unlock()
}
clear()
t.Cleanup(clear)
}
// zoecats on Discord: an account rate limited (429) with Sink on, lifted
// with Try now in the provider's editor, is asked again in its place, not
// left at the back of the order until magpie restarts.
func TestTryNowPutsASunkAccountBack(t *testing.T) {
forgetSinks(t)
forgetLifts(t)
old := allowances
defer func() { allowances = old }()
allowances = func(string) map[string]provider.Allowance {
out := map[string]provider.Allowance{}
for _, u := range []string{"a@x.com", "b@x.com"} {
out[u] = provider.Allowance{{Used: 10, Span: 30 * 24 * time.Hour, Resets: time.Now().Add(24 * time.Hour)}}
}
return out
}
s := New()
acct := func(user string) candidate {
return candidate{p: provider.Provider{ID: "workbuddy", Routing: provider.Ordered, Account: &provider.Account{Agent: "workbuddy", User: user}}, model: "m", rest: "workbuddy@" + user}
}
cs := []candidate{acct("a@x.com"), acct("b@x.com")}
p := provider.Provider{ID: "workbuddy", Routing: provider.Ordered, Sink: true}
order := func() []string {
out, _ := weigh(p, slices.Clone(cs), "m", "")
out, _ = restLast(out, planned{order: make([]Weighed, len(out))})
return whoOf(out)
}
rate := []byte(`{"error":{"message":"Too many requests","type":"rate_limit_error"}}`)
if r := s.restAfter(cs[0], 429, http.Header{}, rate); r.Why != failRate {
t.Fatalf("a: %s", r.Why)
}
if got := order(); !slices.Equal(got, []string{"b@x.com", "a@x.com"}) {
t.Fatalf("rate limited: order %v", got)
}
if !s.Unrest(cs[0].restKey()) {
t.Fatal("Unrest: a wasn't resting")
}
if got := order(); !slices.Equal(got, []string{"a@x.com", "b@x.com"}) {
t.Fatalf("after Try now: order %v, want a back in its place", got)
}
// the same through UnrestAll (a key's value replaced)
s.restAfter(cs[0], 429, http.Header{}, rate)
s.UnrestAll(cs[0].restKey())
if got := order(); !slices.Equal(got, []string{"a@x.com", "b@x.com"}) {
t.Fatalf("after UnrestAll: order %v", got)
}
}
// A rest lifted before its time is told to the Routing page, wherever it
// was lifted: the trace says when, by the rest's key, and a page waiting
// on the trace hears of it at once though no route changed. Before, the
// page kept showing the routes' old 429 until magpie restarted.
func TestTryNowToldToTheRoutingPage(t *testing.T) {
forgetSinks(t)
forgetLifts(t)
s := New()
key := "fakeco#8a197f6f60"
restingUntil.Lock()
restingUntil.m[key] = time.Now().Add(10 * time.Minute)
restingUntil.m[key+"/m1"] = time.Now().Add(10 * time.Minute)
restingUntil.Unlock()
st := s.Trace(context.Background(), 0, 0)
if len(st.Lifted) != 0 {
t.Fatalf("lifted before any lift: %v", st.Lifted)
}
seq := st.Seq
woke := make(chan TraceState, 1)
go func() { woke <- s.Trace(context.Background(), seq, time.Minute) }()
// the waiter is waiting once it made the wake channel
for {
s.trace.mu.Lock()
w := s.trace.wake != nil
s.trace.mu.Unlock()
if w {
break
}
time.Sleep(time.Millisecond)
}
before := time.Now()
s.Unrest(key)
select {
case st = <-woke:
case <-time.After(30 * time.Second):
t.Fatal("a page waiting on the trace didn't hear of the lift")
}
if at, ok := st.Lifted[key]; !ok || at.Before(before) {
t.Fatalf("trace lifted %v, want %s at or after %s", st.Lifted, key, before)
}
if st.Seq <= seq {
t.Fatalf("seq %d, not past %d", st.Seq, seq)
}
// read again from where it is, it doesn't wake on the same lift
if again := s.Trace(context.Background(), st.Seq, 0); again.Seq != st.Seq {
t.Fatalf("seq moved without a change: %d to %d", st.Seq, again.Seq)
}
// UnrestAll tells each model's rest too
s.UnrestAll(key)
st = s.Trace(context.Background(), 0, 0)
if _, ok := st.Lifted[key+"/m1"]; !ok {
t.Fatalf("UnrestAll: lifted %v, want %s/m1", st.Lifted, key)
}
}
// A quota rest lifted when the account's windows renewed is told too.
func TestRenewedToldToTheRoutingPage(t *testing.T) {
forgetSinks(t)
forgetLifts(t)
key := "codex@a@x.com"
restingUntil.Lock()
restingUntil.m[key] = time.Now().Add(time.Hour)
restingUntil.note[key] = Rest{Why: failQuota, agent: "codex", user: "a@x.com", Key: key}
restingUntil.Unlock()
renewed("codex", "A@x.com")
if _, ok := liftedRests()[key]; !ok {
t.Fatalf("renewed: lifted %v, want %s", liftedRests(), key)
}
}