Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions docs/ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,9 @@ Complete uncapped listings retire deleted file checkpoints without deleting thei

`usage_ledger::store` owns `usage.sqlite3`, named by `hide_kit::layout::usage_store` under the core state directory.
Normalized usage events contain native call identity, model, time, session, working folder, provider, tokens and recorded cost, with no conversation fields.
A stored call holds each of those once, in the columns its readers use (the calls format, with-hide/hide-private#672): its key as 32 bytes, its node by the number of a `nodes` row, its tokens and cost as numbers, and its session and working folder by the number of a `places` row, one per (node, agent, session, folder), where a call that named no folder keeps an empty one, which no folder can be.
Nothing reads a call but the dedup lookup, a Claude call's later view, the projects' costs and the pruning, and none of them needs the whole event, so a call is about a fifth of the whole-event JSON version 3 kept beside a hexadecimal key: the operator's 638,000-call store of 512 MiB was 96 MiB converted and compacted (19%), under the 512 MiB bound with five machines of that history.
Whether a later view bills the same call is read from the stored values (time, model, tokens, cost, provider, incomplete), the same values version 3 hashed into a fingerprint column it no longer needs.
An ingestion transaction adds a new call and its daily `(native node, agent, local day, model)` totals together with the file checkpoint; repeated calls add nothing, and changed completed calls keep the first accounting value and report a count-only conflict.
Claude streaming records retain each token field's maximum for that native call, replacing its daily contribution and whole-call pricing band in the same transaction without another call; a later view that raises no maximum writes nothing and counts as a repeat.
A persisted checkpoint distinguishes a yielded read from EOF, so a restart cannot freeze an unread suffix.
Expand All @@ -1140,11 +1143,22 @@ New calls use the current core time zone.
Each agent's first backfill may fill older days within the 400-day retention window, and its event keys stay until that backfill's durable completion marker is written.
After completion, whole days before the 45-day event horizon are frozen: replayed or late calls before that boundary are counted as skipped and never change those totals.
The boundary is retained as a monotonic instant so a clock rollback or time-zone change cannot admit a call whose dedup key already expired.
Maintenance removes at most 512 expired events and 512 expired daily rows per turn; the event bound is one million per native node, the candidate bound 10,000 per agent, the daily-row bound 100,000, the native-node bound 32 and the database bound 512 MiB, with each refusal reported rather than silently growing storage.
Maintenance removes at most 512 expired calls, 512 places and 512 expired daily rows per turn; the call bound is one million per native node, the candidate bound 10,000 per agent, the daily-row bound 100,000, the native-node bound 32 and the database bound 512 MiB, with each refusal reported rather than silently growing storage.
A place keeps its newest call's time and is pruned once that call and every other expired call of its agent are gone, so a place outlives no call of its own and a node's places never outnumber its calls.
The store uses SQLite WAL and full synchronous commits, refuses a damaged or newer store without rebuilding it, and travels with `MOVES_WITH_CORE` through the existing consistent SQLite copy.
The `events_device_at` index on `(device, at)` serves the projects' costs below; like the Hide AI tables it is created `IF NOT EXISTS` on open without raising `user_version`, so a build from before it still opens the store and keeps the index current, where a version bump would make that build refuse the store (`usage_store_newer`) and lose its usage after a rollback.
Building it on an existing store reads every event once, about a second for 486,000 events on a loaded machine, so it runs under its own 30-second budget rather than an open's 250 ms one.
The read orders calls of one time by agent and key, which SQLite sorts one time at a time (at most 21 calls in the operator's store); an index holding agent and key too was 62 MB against this one's 25 MB.
The `calls_node_at` index on `(node, at DESC)` serves the projects' costs below and the pruning; beside a table without rowids it carries the primary key `(node, agent, key)` too, ascending after the newest-first time, so a read of a node's newest calls ordered by time, agent and key sorts nothing.

A version 3 store is converted in place, never rebuilt, and never all at once.
Its first open by a build of the calls format adds the new tables, makes each device a node and drops the two `events` indexes that would slow each move, in one transaction; from then on the worker moves at most 4,096 calls a step, each step one transaction that deletes them from `events`, writes them as calls and moves their count from the device to the node (`Store::convert`), and puts `events_at` back with the last of them.
Until `events` is empty the worker reads, counts and prunes no call and the projects' costs stay pending, since a call still waiting in `events` would not be found as already counted; the daily totals and Hide AI's tables stay readable throughout.
A step that fails or is ended changes nothing, and the next step, in the same process or after a restart or a core move, takes the same calls again; a damaged call fails its step with its cause, logged and on the usage page, and is tried again a minute later rather than skipped.
The operator's 638,000 calls took 156 steps, 76 to 95 seconds in all and 1.8 to 5.5 seconds the slowest, in two runs on a Mac under a load average of 20 to 60.
Moving leaves the old calls' pages free, and SQLite reuses free pages but never returns them, so whenever more than half the file is free pages, after a conversion or at any open, the worker compacts it once with `VACUUM` and empties the write-ahead log (`Store::compact`, its own two-minute budget, four seconds for the operator's store on that Mac); a compaction that fails is logged and the store stays as it was, larger, until the next open tries again.

Going back to a build from before the calls format loses no usage, Hide AI's included.
The store's version stays 3, the oldest layout a build reads and writes it correctly in, and every table a version 3 build uses stays as it made it, `events` and `devices` empty; such a build opens the store as its own and goes on counting, its calls into `events` and Hide AI's into `ai_daily`, which the calls format did not change.
Back on a newer build, the calls it counted are moved like a version 3 store's, and one it counted a second time, which it could not find in `events` because this build had moved it (a later view of a Claude message, or a file read again from its start), is taken back from the day it was added to and taken as a later view of the call kept, as ingest takes one (`an_older_build_counts_into_a_converted_store_and_the_return_counts_each_call_once`).
A change that keeps an older build able to open and write the store adds its tables and indexes beside the ones it uses without raising the version, as Hide AI's tables and the calls format do; one an older build would misread raises it, and that build then refuses the store (`usage_store_newer`) and loses Hide AI's calls of that time when it exits.
Native physical node identities and their qualified source paths stay unchanged when the core's owner changes.

The same store retains the public price cache and the last network-attempt time; that state moves with the ledger so a core move cannot bypass the daily fetch bound.
Expand Down Expand Up @@ -1172,7 +1186,7 @@ A local agent that stops working has its project's worktrees read again (`read_w
The detail item `remains` names a worktree; while it is the shown top of the 보기 slot and no Changes surface is visible, `changes_target` reads that worktree's changes, in front or not.

A project's cost (`usage_ledger/project_cost.rs`) is prepared on the ledger worker, never under `Mutex<Runtime>`.
The runtime tells the worker each local, non-Home project's checkout paths (`feed_usage_projects`, on a catalog replacement, notifying only on a change); the worker reads the last eight days of its own node's `events` (`store::recent_calls`, newest first by `events_device_at` and calls of one time by agent and key, a total order since `(device, agent, key)` is the primary key, so a capped read keeps the same calls under any plan; at most `EVENT_LIMIT` 200,000 calls with a two-second progress budget, `limited` past it), keeps the seven days ending today, gives a call to the project whose deepest checkout holds its session's folder (`files::path_inside_root`, decided once per folder), and prices each (agent, day, model) and (agent, session, model) group with `prices::cost` as a day is priced.
The runtime tells the worker each local, non-Home project's checkout paths (`feed_usage_projects`, on a catalog replacement, notifying only on a change); the worker reads the last eight days of its own node's `calls` (`store::recent_calls`, newest first by `calls_node_at` and calls of one time by agent and key, a total order since `(node, agent, key)` is the primary key, so a capped read keeps the same calls under any plan; at most `EVENT_LIMIT` 200,000 calls with a two-second progress budget, `limited` past it), keeps the seven days ending today, gives a call to the project whose deepest checkout holds its session's folder (`files::path_inside_root`, decided once per folder), and prices each (agent, day, model) and (agent, session, model) group with `prices::cost` as a day is priced.
Each call is decoded, added to its groups by `project_cost::Preparation` and dropped, so a build holds the groups and the folder owners, not the calls.
It rebuilds when the projects or the day change, and when ingestion added or rewrote a call of those eight days or a new price table dirtied it, at most once per `COST_REBUILD` (30 s); a backfill of older history and pruning, which removes only calls past the 45-day horizon, leave it as built; a failed build keeps the last value, stays due, is tried again after the same wait and logs each new cause once.
The result rides the usage frame as `nodes[].projects`, absent until a build succeeded so a tile tells a cost not read yet from a read that found nothing (B33), with `projects_failed` while the latest build failed or the usage store did not open; a project that ran no call is not listed, and an agent with an unpriced call is named in `unpriced_agents` with its priced calls still summed and the total marked `partial`; the daily totals, the Overview's Usage and the calendars below are unchanged, and a project on a connected device has no cost here.
Expand Down
2 changes: 1 addition & 1 deletion hide-core/src/usage_ledger/project_cost.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! A project's last seven days of cost (PRD lane-two-columns D-19, B31),
//! prepared on the ledger worker from the 45-day `events` table: the calls
//! prepared on the ledger worker from the 45-day `calls` table: the calls
//! whose session ran inside one of the project's checkouts, priced the way a
//! day is (`prices::cost`). The daily totals and the Overview's Usage are
//! untouched; a call outside every checkout counts for no project.
Expand Down
Loading
Loading