Files
ai-kit/test
Mao NakamotoandClaude Opus 5 71b665e5b5 feat: ai-ration — free-tier chain, 429 classification, fair-share rationing
Three pure modules extracted from FleetCrown, where each was written after the
matching failure had already happened in production:

  chain      — a fallback list ACROSS VENDORS. A single pinned free model is a
               scheduled outage, and stepping down to a smaller model at the
               SAME vendor buys nothing: it draws on the same org-wide daily
               budget, so when the day runs dry every link is already dead.

  limits     — the three kinds of 429 share a status, a `type` and a `code`, and
               need OPPOSITE responses. Only the body separates them. Treating
               `size` as `capacity` broke a tool loop on every single turn;
               treating `daily` as `capacity` is the same mistake one level
               deeper.

  fair-share — divide a fixed daily pool across users active TODAY, paced through
               the day, with a one-turn floor so nobody's first question is
               refused. The goal is not throughput; it is that every active user
               gets a usable amount every day.

No HTTP client on purpose. Every consumer already has its own calling
conventions, retries and logging; replacing those is a rewrite rather than an
adoption. This package supplies the decisions and leaves the fetch alone.

Two things fixed in the extraction rather than carried over:

- `humanizeWait`'s singular hour was unreachable. With the boundary at 90
  minutes, anything reaching the hours branch divided to at least 1.5, which
  rounds to 2 — so "about 1 hour" could never print and an hour-long wait was
  announced as two. Boundary moved to 60 and the case is now tested.
- The fair-share header quoted a 10k turn cost. A measured turn was ~16k, so the
  worked example understated the squeeze by nearly half.

Tests import the package BY NAME rather than reaching into dist/, so a broken
`exports` map or a `files` list that forgets to ship dist/ fails here instead of
at the first consumer's install.

31 tests, all green via `npm run verify`.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-16 11:49:17 +02:00
..