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).