feat: add FinalToken v1 for Claude and Codex usage #2
+30
-11
@@ -85,20 +85,39 @@ when it changes.
|
|||||||
**Decision.** FinalToken keeps its own history in its data directory, which is
|
**Decision.** FinalToken keeps its own history in its data directory, which is
|
||||||
`Environment.SpecialFolder.LocalApplicationData/FinalToken` (`%LOCALAPPDATA%\FinalToken` on
|
`Environment.SpecialFolder.LocalApplicationData/FinalToken` (`%LOCALAPPDATA%\FinalToken` on
|
||||||
Windows, `~/.local/share/FinalToken` on Linux). It holds hourly token buckets per provider, model
|
Windows, `~/.local/share/FinalToken` on Linux). It holds hourly token buckets per provider, model
|
||||||
and project, and every limit sample (time, provider, window, used percent, reset). The files are
|
and project, and limit samples (time, provider, window, used percent, reset). A sample is kept
|
||||||
JSON or JSON lines through System.Text.Json source generation, written atomically: a temporary
|
when it differs from the window's previous one, and otherwise at most once an hour. All files are
|
||||||
file in the same directory, then a replace. There is no database dependency. Scanning is
|
JSON or JSON lines through System.Text.Json source generation. There is no database dependency.
|
||||||
incremental: for each source file the store remembers how far it read and reads only appended
|
|
||||||
bytes. A file that shrank or was replaced is read again from the start. A response counts once
|
- **Writing.** Each refresh that found something new appends one newline-terminated transaction
|
||||||
across restarts, rescans and resumed sessions. One process writes the store at a time, guarded
|
to a journal. A torn last line is ignored on load and cut off before the next append. When the
|
||||||
by an exclusive lock file in the data directory. While another process holds it, the CLI only
|
journal reaches 8 MiB, it is compacted into one file per month and a state checkpoint, each
|
||||||
reads the current `snapshot.json` and says so. `FINALTOKEN_DATA_DIR` overrides the data
|
written atomically (a temporary file in the same directory, then a replace), and the journal
|
||||||
directory, for tests and parallel checks.
|
starts empty. Transactions hold absolute values, so replaying them after an interrupted
|
||||||
|
compaction gives the same result. A refresh that found nothing new writes no history.
|
||||||
|
- **Reading.** Files that are replaced atomically (`snapshot.json`, settings, the month files, the checkpoint, the
|
||||||
|
activation request) are opened with read, write and delete sharing, and the replace retries
|
||||||
|
briefly, so a reader never makes a write fail on Windows.
|
||||||
|
- **Scanning.** For each source file the store remembers its length, its last write time and how
|
||||||
|
far it read. An unchanged file is not opened; otherwise only appended bytes are read. A file
|
||||||
|
that shrank or was replaced is read again from the start.
|
||||||
|
- **Counting once.** A response counts once across restarts, rescans and resumed sessions. Claude
|
||||||
|
responses are keyed on `message.id` and `requestId` (D3). Their keys are kept for 60 days,
|
||||||
|
twice Claude Code's default cleanup period, and Claude entries older than that are ignored.
|
||||||
|
Codex sessions keep their last cumulative total and its time (D4), so a replayed or archived
|
||||||
|
copy adds nothing.
|
||||||
|
- **One writer.** One process writes the store at a time, guarded by an exclusive lock file in
|
||||||
|
the data directory. While another process holds it, the CLI only reads the current
|
||||||
|
`snapshot.json` and says so. `FINALTOKEN_DATA_DIR` overrides the data directory, for tests and
|
||||||
|
parallel checks.
|
||||||
|
|
||||||
**Why.** R4 needs history longer than the CLIs keep theirs ([D3](#d3--claude-data-sources)). A
|
**Why.** R4 needs history longer than the CLIs keep theirs ([D3](#d3--claude-data-sources)). A
|
||||||
file store has no native dependency to ship with NativeAOT.
|
file store has no native dependency to ship with NativeAOT. Rewriting all history on every
|
||||||
|
refresh wrote about 27 GB a day on the developer's machine; the journal makes writes follow new
|
||||||
|
data.
|
||||||
|
|
||||||
**Gives up.** Ad-hoc queries, and token history finer than an hour.
|
**Gives up.** Ad-hoc queries, token history finer than an hour, limit samples finer than an hour
|
||||||
|
while the value does not change, and Claude history older than 60 days on a fresh install.
|
||||||
|
|
||||||
## D6 — Refresh
|
## D6 — Refresh
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user