TestCapHoldsUnderParallelSessions went red on ubuntu CI at 93% of a 90%
cap (#6958). Five sessions' turns on one account each checked whether it
was near its cap, and two gaps let a round of them past it:
- A turn found not near was counted out on the account (lanes.out) only
once its try took its lane. Turns checking in between saw none of the
others. Now the check and that count are one step under
nearCaps.check: a turn found not near is counted from its check
(lanes.plan) until its lane counts it, or it leaves. The near path's
wait for turns out (lanes.idle) waits for those too. The key's slot is
taken before the check, as the near path takes it, so a counted turn
never waits for a key slot that a near turn holds while it waits for
the count to drop.
- turnSpends took every turn come back as in the reading when the
reading moved. A reply tells the usage as its turn begins, so the
reading it brings lacks the round out then. At 20 turns come back, the
reading said 82.5% while the account was at 90%. What a turn spent was
halved (7.5 points over 10 turns, not 5), and the 5 turns since were
never ahead of it. lanes.began now notes the turns come back as each
try takes its lane, and ahead counts what that reading has (read).
Turns come back since are ahead of it.
nearChecked is a test hook after the check. TestCapHoldsWithAPauseAfterTheCheck
runs the parallel test with a 15ms pause there, as a busy machine
widens it. TestTurnSpendsAhead gains the case of a round out as the
reading was told.
Run at this change on f9492fd8e:
- mote: go test -race -count=10 -run 'TestCapHolds|TestTurnSpendsAhead|TestLane|Near' ./internal/gateway: ok (31.7s)
- mote: go vet ./internal/gateway: ok
- mote: go test -timeout 45m ./internal/gateway: ok (982.9s)
- mica (mote over its cores then): the cap tests -count=5: ok
Rebased onto f1a4de736 (49e29de7b and 52d527f7b touch the gateway).
At the pushed tree, on mote: the cap, turnSpends, lane and near tests
-count=3 ok; go vet, GOOS=linux and GOOS=windows go vet ./internal/gateway
ok; gofmt -l clean.
Broken by hand, the pause test, 3 runs each:
- On the old code, red 3 of 3: "used 97.5% of a 90% cap after 25
turns; want at most 91.5%".
- With only the read count taken out (ahead given turns as read): red,
93.0%.
- With only the planned count taken out of out and idle: red, 93.0%.
- First cut, with the hook inside the check's lock: green with the
count taken out too, because the lock held the next check for the
pause. The hook now runs after the unlock, where the real gap is.
Not tried: a real Codex account at its cap with parallel sessions. The
reading's turns are those come back at the latest lane take, so a
reading still on its way (lane taken, reply not begun yet) is taken as
having them: a gap of one reply's first byte, not of a whole turn.
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).