Files
magpie/internal/gateway/cap_parallel_test.go
T
yetone 3b4983710a gateway: a usage cap holds with sessions in parallel (yalifesign on X)
yalifesign runs 4 or 5 sessions on one ChatGPT Plus account with a 90%
cap and capStop/capCareful off, and the five-hour window went to 100%
and Codex's own 429. Caps from 99 down to 90 all overshot.

Cause: a Codex reply reports usage (codex.rate_limits, x-codex-*) as
its turn begins, without that turn's own spend. The near-cap gate
(435f04151) took one turn at a time only once a reading was inside the
5-point band. It neither counted nor waited for the turns already out
on the account, and after waiting it read usage again only when the
reading hadn't changed, which a pre-turn reading always changed. Five
sessions start turns on readings outside the band that lag every turn
in flight, and they pile up past the cap.

Fix, in serve's walk and cap_near.go:
- turnSpends learns what one turn spends per account (the rise in used
  over the turns returned since it last moved, at most 2x the band) and
  Server.nearCapOf judges "near" on the reading plus that spend for
  every turn out or returned since the reading.
- Near the cap, after taking the near slot, a turn waits until no other
  turn is out on the account (lanes.out, lanes.idle; a lane slot handed
  to this request is its own and not waited for). With other
  candidates it moves behind them first, as before.
- nearCapAgain reads usage again (bounded by carefulWait) when a turn on
  the account came back after the last good read (lanes.ended vs
  UsageRead), and still when it waited and the reading is unchanged.
The gate is generic over the account, so Claude accounts and plugin
accounts get the same hold. capStop and capCareful are read as before;
nothing changes a value the user set. A key's usage cap has no
near-cap gate at all, before and after this: #6928.

Tests:
- TestCapHoldsUnderParallelSessions: a fake Codex that reports usage
  before each turn and adds 1.5 points after it, 5 sessions x 40 turns
  from 60% to a 90% cap. Fails above cap + one turn (91.5%).
- TestNearCapReadsAgainAfterATurnCameBack, TestNearCapWaitsForTurnsAlreadyOut,
  TestTurnSpendsAhead, TestLaneIdleLeavesItsOwnSlot.
- TestNearCapWaitsThenHolds now has the usage read after the first turn.

Run:
- origin/main adfa196db with the two gateway tests (-count=10, mote):
  TestCapHoldsUnderParallelSessions red 9 of 10 (91.5 to 97.5%),
  TestNearCapReadsAgainAfterATurnCameBack red 10 of 10.
- With the fix: the parallel test 20 of 20 at 90.0%, earlier 39 of 40
  at 90.0% and 1 at 91.5%; near-cap, lanes, key slot and careful tests
  -count=3 ok.
- Broken by hand: no spend estimate, 8 of 20 red (93.0, 94.5%); no
  re-read after a returned turn, ReadsAgainAfterATurnCameBack red 10 of
  10; no wait for turns out, WaitsForTurnsAlreadyOut red on every run.
- go test ./... -timeout 45m on mote at 884bcadb6 (base ff88ec264): 103
  ok, no FAIL. Rebased on 618bfc1a9 (kimi, agent and gui only), then
  internal/gateway, internal/agent and internal/gui again: gateway, agent and gui ok at bb23d1b69.
- gofmt -l clean; GOOS=windows go vet ./... ok; go vet ./... on Linux
  (wsl-run, magpie-desktop): ./internal/gateway/... and ./internal/provider/... ok; the whole tree stops on
  gtk4/webkitgtk-6.0 missing in that distro. GOOS=linux vet from macOS
  stops in Wails' cgo files, as on main.
- No GUI change, so no GUI suite.

Not tried: a real Codex account at its cap with parallel sessions (the
fake reproduces the reported shape, not OpenAI's exact accounting); the
spend estimate on a window that renews mid-run beyond the unit test;
key caps (#6928).
2026-11-17 14:54:11 +08:00

198 lines
6.7 KiB
Go

package gateway
import (
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"sync"
"testing"
"time"
"github.com/yetone/magpie/internal/provider"
)
// spendingBackend is a ChatGPT backend whose one account spends cost
// points of its five hours on each turn, counted as the turn ends, as
// ChatGPT counts a turn once it is done: the codex.rate_limits event at
// a reply's start, and /wham/usage, say the usage as it is then, without
// the turns still under way.
type spendingBackend struct {
mu sync.Mutex
used, cost float64
out, most int
turns int
five, week int64
}
func newSpendingBackend(t *testing.T, used, cost float64, hold time.Duration) *spendingBackend {
t.Helper()
now := time.Now()
b := &spendingBackend{used: used, cost: cost, five: now.Add(2 * time.Hour).Unix(), week: now.Add(4 * 24 * time.Hour).Unix()}
up := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/backend-api/codex/responses":
io.ReadAll(r.Body)
b.mu.Lock()
used := b.used
if used >= 100 {
b.mu.Unlock()
w.WriteHeader(429)
io.WriteString(w, `{"error":{"type":"usage_limit_reached","message":"The usage limit has been reached"}}`)
return
}
b.out++
b.most = max(b.most, b.out)
b.turns++
b.mu.Unlock()
w.Header().Set("Content-Type", "text/event-stream")
io.WriteString(w, sse(`event: codex.rate_limits`+"\n"+`data: {"type":"codex.rate_limits","plan_type":"plus","rate_limits":{"allowed":true,"limit_reached":false,"primary":{"used_percent":`+jsonFloat(used)+`,"window_minutes":300,"reset_at":`+jsonInt(b.five)+`},"secondary":{"used_percent":10,"window_minutes":10080,"reset_at":`+jsonInt(b.week)+`}},"credits":null,"metered_limit_name":null,"limit_name":null}`,
`data: {"type":"response.created","response":{"id":"r1","model":"gpt-5.5"}}`))
w.(http.Flusher).Flush()
select {
case <-time.After(hold):
case <-r.Context().Done():
}
b.mu.Lock()
b.used += b.cost
b.out--
b.mu.Unlock()
io.WriteString(w, sse(`data: {"type":"response.output_text.delta","delta":"pong"}`,
`data: {"type":"response.completed","response":{"id":"r1","usage":{"input_tokens":7,"output_tokens":1}}}`))
case "/backend-api/wham/usage":
b.mu.Lock()
used := b.used
b.mu.Unlock()
json.NewEncoder(w).Encode(map[string]any{"plan_type": "plus",
"rate_limit": map[string]any{"allowed": true,
"primary_window": map[string]any{"used_percent": used, "limit_window_seconds": 18000, "reset_at": b.five},
"secondary_window": map[string]any{"used_percent": 10, "limit_window_seconds": 604800, "reset_at": b.week}}})
default:
w.WriteHeader(404)
}
}))
t.Cleanup(up.Close)
was := provider.CodexBase
provider.CodexBase = up.URL + "/backend-api/codex"
t.Cleanup(func() { provider.CodexBase = was })
return b
}
// yalifesign on X: a Codex Plus account capped at 90%, four or five
// sessions at once, went to 100% of its five hours. Each session's turns
// come one after another, all of them at once on the one account; each
// turn spends its points as it ends, so a reading taken while others are
// under way doesn't have them yet. The cap holds all the same: what a
// turn spends is learned on the way up, the turns out are counted ahead
// of the reading, and near the cap a turn waits for them and for a
// reading that has them, so the account goes past its cap by one turn at
// most, never on to the vendor's own 100%.
func TestCapHoldsUnderParallelSessions(t *testing.T) {
codexSignedIn(t)
const start, cost, capAt = 60.0, 1.5, 90
b := newSpendingBackend(t, start, cost, 40*time.Millisecond)
nearCapAccounts(t, capAt, start)
srv := New()
const sessions = 5
var wg sync.WaitGroup
var mu sync.Mutex
codes := map[int]int{}
for range sessions {
wg.Go(func() {
for range 40 {
code, _ := resetPost(t, srv)
mu.Lock()
codes[code]++
mu.Unlock()
if code != 200 {
return
}
}
})
}
wg.Wait()
b.mu.Lock()
used, turns := b.used, b.turns
b.mu.Unlock()
t.Logf("turns %d, codes %v, used %.1f%%", turns, codes, used)
// the turn out as the account came to its cap may end past it; none
// goes out once it is there
if most := float64(capAt) + cost; used > most {
t.Fatalf("used %.1f%% of a %d%% cap after %d turns (codes %v); want at most %.1f%%", used, capAt, turns, codes, most)
}
if codes[429] == 0 {
t.Fatalf("codes %v; want the sessions held at the cap", codes)
}
}
// What a turn spends is learned as the usage moves up over the turns come
// back, and counted ahead of the reading for the turns out and those come
// back since it last moved; a renewed window keeps what a turn spent.
func TestTurnSpendsAhead(t *testing.T) {
var ts turnSpends
if got := ts.ahead("a", 60, 0, 3); got != 0 {
t.Fatalf("nothing learned yet: ahead %v; want 0", got)
}
// two turns came back, the reading not moved: still nothing learned
if got := ts.ahead("a", 60, 2, 3); got != 0 {
t.Fatalf("ahead %v; want 0", got)
}
// it moved 3 points over those two: 1.5 a turn, none since
if got := ts.ahead("a", 63, 2, 4); got != 6 {
t.Fatalf("ahead %v; want 4 turns out at 1.5", got)
}
// one more came back unread, three out
if got := ts.ahead("a", 63, 3, 3); got != 6 {
t.Fatalf("ahead %v; want 4 turns at 1.5", got)
}
// renewed: what a turn spends stands, counted from here
if got := ts.ahead("a", 2, 3, 1); got != 1.5 {
t.Fatalf("ahead %v; want one turn at 1.5", got)
}
// a jump the vendor's own app made too is not taken as one turn's
if got := ts.ahead("a", 40, 4, 1); got != maxTurnSpend {
t.Fatalf("ahead %v; want %v", got, maxTurnSpend)
}
if got := ts.ahead("b", 50, 0, 2); got != 0 {
t.Fatalf("another account: ahead %v; want 0", got)
}
}
// A turn near its cap waits for the turns out on its account, not for a
// slot its own request already holds (handed it from another's queue).
func TestLaneIdleLeavesItsOwnSlot(t *testing.T) {
var l lanes
mine, ok := l.tryTake("acct", 0)
if !ok {
t.Fatal("no slot")
}
defer mine()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := l.idle(ctx, "acct", 1); err != nil {
t.Fatalf("waited on its own slot: %v", err)
}
other, _ := l.tryTake("acct", 0)
done := make(chan error, 1)
go func() { done <- l.idle(ctx, "acct", 1) }()
// it waits for a slot to come back: the channel it waits on is made
waitFor(t, "idle waiting", func() bool {
l.mu.Lock()
defer l.mu.Unlock()
return l.freed != nil
})
select {
case err := <-done:
t.Fatalf("went with another turn out: %v", err)
default:
}
other()
if err := <-done; err != nil {
t.Fatalf("still waiting once the other came back: %v", err)
}
if _, ended, turns := l.out("acct"); ended.IsZero() || turns != 1 {
t.Fatalf("ended %v, turns %d; want the other's return noted", ended, turns)
}
}