Files
loki/scripts/test
27b7e403f8 feat(ai): what is left at each vendor, and what happens when it runs out (#677)
FleetCrown enforced a daily AI budget with no interface at all: a user was
refused and never learned why, or what their share had been. And nothing
anywhere knew how much was left at the VENDORS — the constraint that actually
decides whether the next answer happens.

Settings → AI now shows it, and the numbers come from the answers we already
paid for rather than from asking.

WHY OBSERVED AND NOT POLLED

The obvious design is a cron that asks each provider how much quota remains.
That design reports a FULL TANK DURING AN OUTAGE. Measured on one live key,
within the same second:

  GET  /api/v1/key        ->  limit_remaining: null,  usage_daily: 0
  POST /chat/completions  ->  429, x-ratelimit-remaining: 0 of 50,
                              "Rate limit exceeded: free-models-per-day"

Both are OpenRouter's answers about the same account, and the account was
locked out. Its usage endpoint tracks money, free models cost nothing, and the
limit that binds is a request count that endpoint never reports.

So the recorder hangs off the existing model call and reads the rate-limit
headers off every response, success and refusal alike. No extra request, and no
quota spent to discover how much quota is left.

A NUMBER WITHOUT AN ACTION IS DECORATION

No row shows a bare percentage. Each says what is left in ANSWERS, when it
returns, and which provider takes over — because "78%" tells you nothing you
can do and "about 40 answers, then it moves to OpenRouter" tells you whether to
care. Tokens are translated because nobody has an intuition for a token.

THREE STATES, NEVER TWO

known · unknown · exhausted. A vendor nobody has called today has no reading,
and it is drawn as UNKNOWN with the reason stated — listing only what has been
measured would let the page read as the whole fleet while omitting the provider
about to serve the next turn. A per-minute counter read six minutes ago also
reverts to unknown rather than reporting a number that expired.

DETAILS THAT ARE LOAD-BEARING

- `key_owner` is a non-null text column, not a nullable user_id. Postgres treats
  NULLs as DISTINCT in a unique index, so a nullable owner would let the
  platform accumulate one duplicate row per observation while the upsert never
  found its target — a rollup silently becoming an append-only log.
- The recorder is never awaited, cannot throw, and imports the db lazily. A
  telemetry write must not add latency to an answer someone is waiting for, nor
  fail it and demote a working vendor off the chain.
- A unique INDEX rather than a CONSTRAINT so the migration can carry
  IF NOT EXISTS, which the box's forward-only applier requires.
- 0067 is hand-written: drizzle-kit generate stops on an interactive prompt in
  this schema, and apply-schema.sh globs numbered .sql rather than reading the
  journal — as 0065 and 0066 already did.

FIVE DEFECTS CAUGHT ON THE WAY IN

Two by reading it back: a stale-write guard that was a tautology
(eq(observedAt, observedAt) is always true), and an unreachable sub-minute
branch (40 seconds rounds UP to 1). Three by the gates: prettier on the
workspace file, react-hooks on a synchronous setState in an effect (which also
exposed a real unmount race), and the dead-exports ratchet on a single-counter
read written for a router that does not exist yet — deleted rather than
baselined.

163/163 unit test files; 15 new checks on the presenter, which is pure.

Co-authored-by: Cato <georgy.butaev@revamp-it.ch>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-12 10:37:38 -07:00
..
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00
2026-08-31 09:23:30 +00:00