Skip to content

feat: show and redeem each gateway account's banked resets - #124

Merged
jamesainslie merged 13 commits into
mainfrom
gateway-reset-credits
Nov 18, 2026
Merged

jamesainslie merged 13 commits into
mainfrom
gateway-reset-credits

Conversation

@jamesainslie

Copy link
Copy Markdown
Collaborator

What this adds

The gateway panel now shows how many limit resets each pooled modelproxy account has banked. The docked card can redeem one.

  • Chip on every row. ↻1 for one weekly reset, with · F3 appended when Fable-only resets are banked too. The chip is muted when none of the weekly resets would act yet. Codex reports that state until a limit is reached. The tooltip lists the counts and the next expiry.
  • Redeem from the card. Expanding an account in the docked card shows "Use reset" for weekly and Fable, each behind the existing confirm dialog. The hover peek never offers redemption, because a dialog cannot open from inside its popover. The answer stays on the card even when the reset moves the account between the available and spent groups.

How it works

  • Contract. ServerProviderResetCredits gains optional usableNowCount and fable.
  • Server. modelproxyApi decodes the gateway's accounts[].resetCredits into usageLimits.resetCredits. It decodes forward-compatibly, so a field this build cannot read is dropped rather than costing the whole status. The creditId for a gateway account is weekly or fable, and the gateway resolves the real grant.
  • Redeem. UsageLimitSources.consumeResetCredit gains a modelproxy branch that calls the gateway's POST /api/accounts/{name}/reset-credits/redeem.

Spending safety

Credits are real and non-refundable.

  • Lathe keeps one request id per source, account and kind until the gateway gives a settled answer: reset, nothingToReset, noCredit or alreadyRedeemed.
  • Every other answer keeps the id. That covers unconfirmed, coolingDown, rateLimited, signedOut, a 5xx, a timeout and a dropped connection. The gateway binds an id to one credit, so a retry can never spend a second one.
  • A 401 or 403 retries once with a fresh token and the same id.
  • The redeem call has one 90 second timeout covering the request and the body.
  • Pending ids are held in memory, so a Lathe restart forgets them.

Notes

  • Depends on modelproxy#57. Older gateways omit the field, so they show no chip and nothing else changes.
  • One deliberate reading of the spec. The chip is muted only when weekly resets exist and none is usable now. The gateway's usable-now count covers weekly resets only, so a Fable-only account is not muted.
  • Browser test runtime. The new browser render test needs Playwright's pinned chromium_headless_shell-1223. This host has only 1234 and 1248, so the test was run against an aliased browser. CI or a playwright install covers it.

Testing

  • Every task was built test-first, with a spec and quality review per task, a whole-branch review and a review of the fix wave.
  • Passing: contracts tests (271), server src/usage tests (204), web unit tests (1740 under src/components, and 473 in chat after the last commit) and the new browser render test.
  • effect-tsgo typecheck exits 0 in contracts, server and web. Repo lint and fmt:check are clean.

Known follow-ups

  • A redeem can hold the source refresh lock for up to about 3 minutes in the worst case: two token fetches, two 90 second calls and the follow-up read. Interval refreshes and stream publishes wait during that time.
  • A 502's message does not yet say that a retry is safe, though the id is kept.
  • The card's own reset summary still reads "next expires in 0m" for a passed expiry. The row tooltip is fixed.

… is safe

The gateway spends the credit and re-probes the account before it answers,
which can take up to a minute. The 30s request timeout gave up early, and the
body read had no bound at all while the refresh lock was held. One 90s
timeout now covers the request, the body and its decoding.

A timeout or a dropped connection says the reset may have applied and that
trying again will not spend a second credit. A redeem the gateway answers
with an error carries its HTTP status.
…a redeem fails

After a redemption the gateway branch published the raw re-read, skipping
the settling that refresh applies. A failed read right after a reset then
published no accounts and no routes, and routed models left the picker.
The re-read after a redemption now goes through the same settling: a
transient failure keeps the last good snapshot and any failure keeps the
gateway's routes.
…malformed one

The gateway can reject a token its expiry still vouches for. A 401 or 403
on a redeem now signs in again and sends the same request id once more,
the way status reads already do. If the fresh token is rejected too, the
existing message stands and the id is kept.

A 400 from the gateway means Lathe sent something it does not accept, so
it is logged at warn with the source, account and kind. The id is kept.
A dropped connection and an answer Lathe does not know (such as
"constructor") keep the id. alreadyRedeemed, noCredit and nothingToReset
each release it, so the next redemption gets a fresh one.
…e one

The reset tooltip read "next expires in now" once an expiry had passed and
"next expires in " when the timestamp could not be read. It now says
"next expires now" and leaves the clause out. The weekly bucket no longer
carries an explicit undefined Fable field.
A successful reset clears the spent window and usually the last credit, so
the account moves to the other group and its weekly bucket disappears. Both
remount the row and the redeem status went with it, so the answer was
almost never seen.

The card's ledger now keeps each redemption's progress and answer, keyed
by account and kind, and the row shows a bucket while it holds one even
after its count reaches zero. ResetCredits takes an optional store for
this; elsewhere it keeps its own state as before. The hover peek still
offers no redemption.
@jamesainslie
jamesainslie merged commit cff2d97 into main Nov 18, 2026
6 of 7 checks passed
@jamesainslie
jamesainslie deleted the gateway-reset-credits branch November 18, 2026 19:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant