Repository navigation
Commit 9cdbb73
feat(server): keep a history of provider usage-limit readings (#24)
* feat(server): keep a history of provider usage-limit readings
Pacing needs to know how fast each account's meter moves, not just where it
stands now. The server only keeps the last usage-limit snapshot per provider
instance, so the burn rate, an early reset or another client's use cannot be
seen.
UsageReadingsHistory appends one JSON line per reading to
<state>/usage-readings/<instanceId>.jsonl whenever an instance's meter moved
(0.1 point), its account or availability changed, or 30 minutes passed
unchanged. A republished or older snapshot is never recorded. At start it
compacts every history: readings older than 35 days and lines torn by a crash
are dropped, at most 20 000 readings are kept per instance, and a history with
nothing left is removed. It is file-based on purpose: the fork carries no
database migrations.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
* Usage readings: account per series, safe reads, bounded and thinned history
- A read error other than "file missing" no longer counts as an empty
history: that instance is skipped and logged, its file is never
compacted or removed, and readings() fails with UsageHistoryUnreadable.
- Each reading records the account the Limits UI groups by
(providerAccountKey, from accountKey), as a short sha256 hash. An
account change starts a new series; readingsOfAccount groups instances.
- Record rule matches its words: a window moved |delta| >= 0.1 point, a
reset moved >= 1 minute, or availability, window set, kind or length
changed; windows are compared by id.
- Compaction interval, cap, retention, heartbeat and thinning are options.
Readings older than 2 days thin to one per 15 minutes plus every
structural change, so 35 days fit the cap.
- Retention and cap are based on the newest reading, not the clock, so a
clock far ahead at start cannot wipe the history. Readings stamped
ahead of the clock are not served or used, but stay on disk.
- A torn last line gets a newline before the next append.
- Lines that no longer decode are skipped one by one and kept on disk.
- Reads are served from memory after the first load.
- Tests: 21, each new guard checked to fail its test when removed.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
* Usage readings: keep the account on a failed probe, clock rule by append order
- A probe that reports less of the account (no identity, or no workspace for
the same login) keeps the instance's last known account, or the one
workspace other instances of that login report in the snapshot, so a
failed probe never looks like an account swap. Accounts are stored as
hashed <base> or <base>.<workspace>.
- A reading appended before an earlier-stamped one came from a clock that
was ahead: readers ignore it and compaction drops it. Readings ahead of a
clock that is behind, with no later append, stay.
- A failing history (unreadable, appends failing) is logged once per
instance until it works again.
- Compaction removes a temp folder left by a crash during compaction.
- Tests for reset appear/disappear, window kind, window replaced, a torn
append in a running server, reads from memory and lock-free reads.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
* Usage readings: no deletion on clock changes, segments, real-write recovery, no borrowed carry
- Clock: drop the append-order rule. No reading is deleted or hidden because
its stamp disagrees with another or with the clock; deletion is only
retention, the cap and thinning, counted from the newest reading. Readers
still skip readings stamped ahead of now. `readings` returns append order
with a `segment` per reading that goes up where a stamp goes backwards;
thinning starts again at a break. `newestMs` is the newest reached stamp.
- Log "recording works again" only after a real write or a real read of a
file that had failed, never after an update with nothing to write.
- A workspace borrowed from another instance of the same login is marked
`borrowed` and never carried as the instance's last known account; two
workspaces of one login in a snapshot are never merged.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---------
Co-authored-by: Claude Opus 5.5 <[email protected]>1 parent 954235f commit 9cdbb73
4 files changed
Lines changed: 1756 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
115 | 115 | | |
116 | 116 | | |
117 | 117 | | |
| 118 | + | |
118 | 119 | | |
119 | 120 | | |
120 | 121 | | |
| |||
681 | 682 | | |
682 | 683 | | |
683 | 684 | | |
| 685 | + | |
684 | 686 | | |
685 | 687 | | |
686 | 688 | | |
| |||
0 commit comments