From c633c2bd3fd6699a13a7a8e277913e15a1ee2116 Mon Sep 17 00:00:00 2001 From: Florian Schmidt Date: Wed, 14 Oct 2026 20:00:18 +0200 Subject: [PATCH] docs: record the journaled history store in D5 The store now appends changes to a journal and compacts into monthly files, shares files for readers on Windows, skips unchanged sources and bounds Claude dedupe to 60 days. D5 says so, and what it gives up. Refs #1 --- docs/decisions.md | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/docs/decisions.md b/docs/decisions.md index f0d98a1..90251e4 100644 --- a/docs/decisions.md +++ b/docs/decisions.md @@ -85,20 +85,39 @@ when it changes. **Decision.** FinalToken keeps its own history in its data directory, which is `Environment.SpecialFolder.LocalApplicationData/FinalToken` (`%LOCALAPPDATA%\FinalToken` on 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 -JSON or JSON lines through System.Text.Json source generation, written atomically: a temporary -file in the same directory, then a replace. There is no database dependency. Scanning is -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 -across restarts, rescans and resumed sessions. 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. +and project, and limit samples (time, provider, window, used percent, reset). A sample is kept +when it differs from the window's previous one, and otherwise at most once an hour. All files are +JSON or JSON lines through System.Text.Json source generation. There is no database dependency. + +- **Writing.** Each refresh that found something new appends one newline-terminated transaction + to a journal. A torn last line is ignored on load and cut off before the next append. When the + journal reaches 8 MiB, it is compacted into one file per month and a state checkpoint, each + written atomically (a temporary file in the same directory, then a replace), and the journal + 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 -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