opencode-usage-insights 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/docs/demo.md ADDED
@@ -0,0 +1,207 @@
1
+ # Demo output
2
+
3
+ Generated with `bun run demo 100` against a seeded throwaway database (times
4
+ render in the local timezone). `bun run docs:images` captures the screenshots
5
+ from the same database, through the same components the TUI renders.
6
+
7
+ ## The line
8
+
9
+ ![The telemetry line after a turn](images/line.png)
10
+
11
+ ## The sidebar
12
+
13
+ ![The session sidebar: the session totals and its last turns](images/sidebar.png)
14
+
15
+ ## The panel
16
+
17
+ ![The /usage overview view](images/panel-overview.png)
18
+
19
+ ![The /usage providers view](images/panel-providers.png)
20
+
21
+ ![The /usage cache view](images/panel-cache.png)
22
+
23
+ ## Full output
24
+
25
+ ```text
26
+ turn · prompt cache survived (the provider and model that ran the turn)
27
+ ────────────────────────────────────────────────────────────────────────────────────────────────────
28
+ 09:29:00 · Δ 24m00s · opencode/deepseek-v4.1-flash-free · in 9.4k (1.62M), cached 1.61M, out 1.25k, cost $0.0600 · sess in 107.1k (3M), cached 2.89M, out 7k, cost $0.2700
29
+
30
+ turn · prompt cache expired after an idle gap
31
+ ────────────────────────────────────────────────────────────────────────────────────────────────────
32
+ 15:09:00 · Δ 1d0h · opencode/glm-5.3-flash · in 63.7k (76.1k), cached 12.4k, out 4.8k, cost $0.4400 · sess in 121.7k (754.1k), cached 632.4k, out 9.9k, cost $0.7600 · ⚠ cache expired (−607.6k)
33
+
34
+ turn · provider changed (opencode → anthropic)
35
+ ────────────────────────────────────────────────────────────────────────────────────────────────────
36
+ 09:52:00 · Δ 17m00s · anthropic/claude-opus-4.6 · changed · in 12.8k (108.8k), cached 96k, out 3.3k, cost $1.42 · sess in 130.3k (3.37M), cached 3.24M, out 8.25k, cost $1.69
37
+
38
+ turn · tool-call loop, latest view (the prompt size the turn ended with)
39
+ ────────────────────────────────────────────────────────────────────────────────────────────────────
40
+ 21:27:00 · opencode/deepseek-v4.1-flash-free · 3 steps · in 1.02k (292.6k), cached 291.6k, out 1.49k, cost $0.0900 · sess in 107.1k (3M), cached 2.89M, out 7k, cost $0.2700
41
+
42
+ turn · tool-call loop, sum view (turnTotals: "sum" — every step of the turn)
43
+ ────────────────────────────────────────────────────────────────────────────────────────────────────
44
+ 21:27:00 · opencode/deepseek-v4.1-flash-free · 3 steps · in 2.67k (875.2k), cached 872.6k, out 2.23k, cost $0.0900 · sess in 107.1k (3M), cached 2.89M, out 7k, cost $0.2700
45
+
46
+ turn · response still streaming (no line until the response ends)
47
+ ────────────────────────────────────────────────────────────────────────────────────────────────────
48
+ (no line yet)
49
+
50
+ sidebar (the session's totals, then the turns — three lines each, plus the model at both ends of the block and on every change)
51
+ ────────────────────────────────────────────────────────────────────────────────────────────────────
52
+ session
53
+ 3 steps · in 130.3k
54
+ cached 3.24M · out 8.25k
55
+ hit 96.1% · 1618/s · $1.69
56
+
57
+ turns
58
+ 09:42 anthropic
59
+ claude-opus-4.6
60
+ in 23.8k (119.8k) · out 3.3k
61
+ hit 80.1% · 1179/s · $1.42
62
+ 09:24 opencode
63
+ deepseek-v4.1-flash-free
64
+ in 10.3k (1.62M) · out 1.25k
65
+ hit 99.4% · 2152/s · $0.2700
66
+
67
+ /usage overview
68
+ ────────────────────────────────────────────────────────────────────────────────────────────────────
69
+ all time · 7 steps · 3 sessions · 6 prompts
70
+ steps 7 in 287.1k
71
+ prompt 4.87M cached 4.58M
72
+ hit 94.1% out 26.6k
73
+ write 45.6k reasoning 900
74
+ cost $2.70 tps 2709.2/s
75
+ first 12/10/2026, 09:01:00 last 15/10/2026, 07:20:31
76
+
77
+ activity █▁▁▁▁▁▁▁▁▁▃▁▁▁▁▁▁▁▃▁▁▁▁▁▆ 25 buckets
78
+
79
+ this session
80
+ steps 3 in 130.3k
81
+ prompt 3.37M cached 3.24M
82
+ hit 96.1% out 8.25k
83
+ write 23.9k reasoning 900
84
+ cost $1.69 tps 1617.6/s
85
+
86
+ data coverage
87
+ models 4 · unknown 0.0% providers 3
88
+ unsettled 0 · no tps 0.0%
89
+ window 12/10/2026, 09:01:00 → 15/10/2026, 07:20:00
90
+
91
+ /usage models
92
+ ────────────────────────────────────────────────────────────────────────────────────────────────────
93
+ model steps prompt cached hit out cost providers
94
+ deepseek-v4.1-flash 1 410.6k 402k 97.9% 2.1k $0.0700 opencode-go
95
+ p50 hit 97.9% · 1 switch (1 provider)
96
+ claude-opus-4.6 1 119.8k 96k 80.1% 3.3k $1.42 anthropic
97
+ p50 hit 80.1% · 1 switch (1 provider)
98
+ glm-5.3-flash 2 754.1k 632.4k 83.9% 9.9k $0.7600 opencode
99
+ p50 hit 14.6%
100
+ deepseek-v4.1-flash-free 3 3.59M 3.45M 96.3% 11.3k $0.4500 opencode
101
+ p50 hit 94.1%
102
+
103
+ /usage providers
104
+ ────────────────────────────────────────────────────────────────────────────────────────────────────
105
+ provider models steps share prompt cached hit tps p50 wall
106
+ anthropic 1 1 14% 119.8k 96k 80.1% 1178.6/s 1m14s
107
+ opencode-go 1 1 14% 410.6k 402k 97.9% 2625.0/s 31.0s
108
+ opencode 2 5 71% 4.34M 4.09M 94.1% 3000.0/s 58.0s
109
+
110
+ /usage cache
111
+ ────────────────────────────────────────────────────────────────────────────────────────────────────
112
+ reuse losses 1 event · 607.6k tokens
113
+ expired 1 · 607.6k
114
+
115
+ reuse by model
116
+ model steps prompt cached write hit hit p50
117
+ deepseek-v4.1-flash-free 3 3.59M 3.45M 16.9k 96.3% 94.1%
118
+ glm-5.3-flash 2 754.1k 632.4k 17k 83.9% 14.6%
119
+ deepseek-v4.1-flash 1 410.6k 402k 700 97.9% 97.9%
120
+ claude-opus-4.6 1 119.8k 96k 11k 80.1% 80.1%
121
+
122
+ cache survival against idle time
123
+ gap pairs kept drops lost
124
+ 15–60m 1 100% 0 0
125
+ >6h 1 0% 1 607.6k
126
+
127
+ observed cache TTL bounds
128
+ model hits up to drops from samples
129
+ opencode/deepseek-v4.1-flash-free 22m – 1
130
+ opencode/glm-5.3-flash – 23.9h 1
131
+
132
+ recent losses
133
+ 15:00:00 opencode/glm-5.3-flash −607.6k 23.9h idle · expired
134
+
135
+ /usage periods
136
+ ────────────────────────────────────────────────────────────────────────────────────────────────────
137
+ per day · 4 periods · 7 steps · hit 94.1%
138
+ steps █▃▃▆
139
+ cached █▂▁▃
140
+ cost █▂▃▂
141
+ period steps sess prompt cached hit out cost
142
+ 2026-10-15 2 1 747.1k 712k 95% 8.4k $0.2500
143
+ 2026-10-14 1 1 85.1k 12.4k 15% 4.8k $0.4400
144
+ 2026-10-13 1 1 669k 620k 93% 5.1k $0.3200
145
+ 2026-10-12 3 1 3.37M 3.24M 96% 8.25k $1.69
146
+
147
+ total 7 4 4.87M 4.58M 94% 26.6k $2.70
148
+
149
+ /usage latency
150
+ ────────────────────────────────────────────────────────────────────────────────────────────────────
151
+ wall p50 58.0s p90 1m28s p99 1m28s n 7
152
+ stream p50 1.2s p90 2.8s p99 2.8s
153
+ throughput p50 2642.9/s p90 5250.0/s p99 5250.0/s
154
+
155
+ wall = request to completed · stream = request to last chunk · throughput = output+reasoning tokens…
156
+
157
+ by group
158
+ group n wall p50 wall p90 stream tps p50 tps p90 cost
159
+ claude-opus-4.6 1 1m14s 1m14s 2.8s 1178.6/s 1178.6/s $1.42
160
+ deepseek-v4.1-f… 1 31.0s 31.0s 800ms 2625.0/s 2625.0/s $0.0700
161
+ glm-5.3-flash 2 58.0s 1m01s 1.1s 3000.0/s 4636.4/s $0.7600
162
+ deepseek-v4.1-f… 3 52.0s 1m28s 1.2s 2642.9/s 5250.0/s $0.4500
163
+ n < 5 is dimmed: too few samples to compare
164
+
165
+ /usage sessions
166
+ ────────────────────────────────────────────────────────────────────────────────────────────────────
167
+ title steps prompt cached hit out cost
168
+ Refactor the recorder 3 3.37M 3.24M 96% 8.25k $1.69
169
+ /code/opencode-usage-insights · 2 models
170
+ Fix the cache regression 2 754.1k 632.4k 84% 9.9k $0.7600
171
+ /code/opencode-usage-insights
172
+ Docs pass 2 747.1k 712k 95% 8.4k $0.2500
173
+ /code/opencode-usage-insights · 2 models
174
+
175
+ /usage turns
176
+ ────────────────────────────────────────────────────────────────────────────────────────────────────
177
+ 6 turns · newest first
178
+ Refactor the reco… 09:24:20 · opencode/deepseek-v4.1-flash-free · 2 steps · in 10.3k (1.62M), cach…
179
+ Refactor the reco… 09:42:14 · Δ 40m00s · anthropic/claude-opus-4.6 · changed · in 23.8k (119.8k),…
180
+ Fix the cache reg… 15:03:01 · opencode/glm-5.3-flash · in 49k (669k), cached 620k, out 5.1k, cost…
181
+ Fix the cache reg… 15:00:58 · Δ 23h58m · opencode/glm-5.3-flash · in 72.7k (85.1k), cached 12.4k,…
182
+ Docs pass 07:01:28 · opencode/deepseek-v4.1-flash-free · in 26.5k (336.5k), cached 310k, out 6.3k,…
183
+ Docs pass 07:20:31 · Δ 20m00s · opencode-go/deepseek-v4.1-flash · changed · in 8.6k (410.6k), cach…
184
+
185
+ /usage db
186
+ ────────────────────────────────────────────────────────────────────────────────────────────────────
187
+ /tmp/usage-insights-demo-mFDbHx/usage.db
188
+ 0.0 MB + 0.4 MB wal · schema v11
189
+
190
+ what it holds
191
+ sessions 3 rows
192
+ prompts 6 rows
193
+ events 0 rows
194
+ steps 7 rows
195
+ usage 0 rows
196
+ window 12/10/2026 → 15/10/2026
197
+
198
+ clean up
199
+ ↑↓ pick a retention · enter prunes from its cutoff · the wipe row deletes everything (asks first)…
200
+ list estimates every $0 step has a price
201
+ keep prunes older than
202
+ 30 days 0 steps 16/09/2026
203
+ 90 days 0 steps 18/07/2026
204
+ 365 days 0 steps 16/10/2025
205
+
206
+ wipe everything · 7 steps · 3 sessions · cannot be undone
207
+ ```
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,387 @@
1
+ # Internals
2
+
3
+ How the plugin works under the hood: the local database, where the identity of a
4
+ step comes from, the rules the hot paths keep, and how the clients are tested.
5
+ The [README](../README.md) covers what a user sees; this file covers why it
6
+ behaves the way it does.
7
+
8
+ ## Database
9
+
10
+ The recorder writes to the per-user state directory of the current OS:
11
+
12
+ | OS | Path |
13
+ | --- | --- |
14
+ | Linux / other | `$XDG_STATE_HOME/opencode/opencode-usage-insights/usage.db` (default `~/.local/state/opencode/opencode-usage-insights/usage.db`) |
15
+ | macOS | `~/Library/Application Support/opencode/opencode-usage-insights/usage.db` |
16
+ | Windows | `%LOCALAPPDATA%\opencode\opencode-usage-insights\usage.db` |
17
+
18
+ `OPENCODE_USAGE_INSIGHTS_DB=/path/to/usage.db` overrides the path, and the
19
+ `db` option overrides both. The database is opened with `journal_mode = WAL`, so
20
+ the worker's reader and the server's writer never block each other. The TUI and
21
+ the Desktop GUI never open the file: they call the server's usage RPC.
22
+ Every write is an upsert keyed by message id, which makes recording idempotent:
23
+ replayed events, restarts and re-imports never duplicate rows.
24
+
25
+ Tables: `sessions`, `prompts`, `steps`, `events`, `usage` and `meta` (the
26
+ schema version). The `steps` table is the interesting one: `message_id` (primary key),
27
+ `session_id`, `parent_id`, provider/model/variant, input/output/reasoning and
28
+ cache-read/cache-write tokens, charged `cost`, list-price `list_cost`,
29
+ `created_at`, `streamed_at`, `completed_at`, `finish` and `recorded_at`.
30
+ `list_cost` is nullable on purpose: `NULL` means the step was charged or its
31
+ price was unknown; `0` means the host's model list knows the model is free; a
32
+ positive value is an estimate for a step the host reported as `$0`. Storing zero for a
33
+ free model keeps it out of the backfill's "needs an estimate" set without
34
+ inventing money.
35
+
36
+ Throughput is not stored: `tps` is everything the model generated
37
+ (`output + reasoning`) over the streaming window (`created → streamed`), and the
38
+ `latency` view computes it from the timings and reports its p50/p90/p99. The
39
+ window is the streaming one on purpose — `streamed` marks the end of streaming
40
+ and `completed` the end of the step, which for a tool-call step happens after
41
+ the tool calls were handled — so a step whose window or produced tokens are
42
+ unknown has no throughput and is left out of the percentiles.
43
+
44
+ Two expression indexes carry every ranged view: `steps_at` on
45
+ `COALESCE(created_at, recorded_at)` (the step's own clock, falling back to the
46
+ recording time for a step that was already in flight when the plugin loaded)
47
+ and `steps_session_at` on the same expression scoped to a session, which is what
48
+ the per-prompt history and the sidebar read. Without them a range filter is a
49
+ full table scan on every panel rebuild.
50
+
51
+ The file is private to the user: its directory is created `0700` and the file
52
+ `0600` (POSIX), re-applied every time either is opened. Schema migrations run on
53
+ the server's writer connection. The worker opens a read-only connection for the
54
+ panel and the turn history — it can never write, never migrate and never hold
55
+ the writer's lock — and prune, wipe, the list-price backfill and compact use a
56
+ separate short-lived writer inside the worker with a 250 ms busy timeout, so
57
+ maintenance fails fast with a message instead of stalling the recorder.
58
+
59
+ ## The identity of a step
60
+
61
+ Every view groups a step by one thing: the **provider the model ran through**,
62
+ as OpenCode is configured (`provider_id` in the table, `opencode`,
63
+ `opencode-go`, `deepseek`, …), and the model itself.
64
+
65
+ That is the whole answer available, and it is deliberate. For a while the plugin
66
+ also recorded the *routing endpoint* a gateway named in a response header
67
+ (`x-opencode-endpoint-id`), which said which downstream operator actually ran the
68
+ inference behind a gateway. The Zen routes stopped publishing it — the header
69
+ came from a legacy handler that was removed when the routes became a thin
70
+ forwarding proxy ([anomalyco/opencode#54563](https://github.com/anomalyco/opencode/pull/54563))
71
+ — and no other source names that upstream: the response body of a routed request
72
+ is provider-neutral, the request host is the gateway, and the host binary
73
+ contains no reference to the header. So there is nothing left to discover, and
74
+ nothing is inferred in its place.
75
+
76
+ What the recorder writes is what the event stream carries. `recordStep` takes
77
+ the provider from the step's own model reference (or, for a step that started
78
+ before the plugin loaded, from the last model the session selected), and
79
+ validates it as a single token (letters, digits, `. _ : / -`, at most 64
80
+ characters) before it becomes a grouping key: a value that is not a provider id
81
+ is stored as absent rather than splitting the views on a typo. Case is left
82
+ alone — the provider reads the way it is configured.
83
+
84
+ Two consequences are load-bearing in the analysis layer:
85
+
86
+ - **A provider switch forfeits the cache.** The prompt cache belongs to the
87
+ provider that holds it, so `cacheDrop` — the rule shared with the live line —
88
+ takes a scope. The switch and loss reports measure a drop across a provider
89
+ switch (`"model"`: the model is the same, so the two steps share a cache to
90
+ lose), while the survival report and the TTL evidence keep the strict
91
+ `"provider"` scope: two providers are two caches, and a switch between them is
92
+ not an idle-gap sample.
93
+ - **A model that ran through two providers is one model with two rows.** The
94
+ `models` view keeps the family together and names the providers in a column;
95
+ the `providers` view leads with the provider and lists the models it served.
96
+
97
+ ## What the line counts
98
+
99
+ The line has two totals groups — the turn and the session — and they follow
100
+ different rules on purpose:
101
+
102
+ - **Tokens follow the view.** `turnTotals: "latest"` (the default) reports the
103
+ prompt the turn ended with: summing `input`/`cached` over the steps of a
104
+ tool-call loop would count the same re-sent prompt once per step, which is not
105
+ what a context view means. `"sum"` adds every step instead, which is the
106
+ billing view of the tokens.
107
+ - **Money is always summed.** Every step of a turn is paid for, so the turn's
108
+ cost is the sum over its steps whatever the token view shows (`totalsText`
109
+ takes the cost explicitly for that reason, never the `latest` step's share).
110
+ For the session, the charged cost is the one the host reports; the list
111
+ estimate is `SUM(list_cost)` over the plugin's recorded steps, fetched over
112
+ the RPC and cached by the client, invalidated on every write event.
113
+ - **A zero cost is left out.** A free model reads as free instead of repeating
114
+ `$0`; `fields.cost: false` hides both charged cost and estimate.
115
+ - **A list estimate is labelled.** OpenCode 2.2.0 trusts a reported `cost: 0`
116
+ instead of falling back to its model list, and OpenCode Go reports `0` for
117
+ subscription-covered requests. The recorder keeps that charged `0` and stores
118
+ the model-list price in `list_cost`; the line renders `cost ≈ $0.0344`. A turn
119
+ that mixes charged and estimated steps renders both parts
120
+ (`cost $0.12 + ≈ $3.40`) rather than adding them silently. The `sess`
121
+ segment applies the same rule: the host's charged total plus the estimate
122
+ summed from the recorded steps, so a Go session shows its list cost there
123
+ too.
124
+ - **Switches the line warns about are between turns.** A provider change against
125
+ the previous prompt is `changed`, a model change inside one provider is
126
+ `model changed`; the same comparison between *steps* is what the `models`
127
+ view's detail line counts.
128
+
129
+ ## List-price estimates
130
+
131
+ The “catalog” is the model list exposed by the OpenCode process that is
132
+ currently serving the session — not a file and not a plugin-owned network
133
+ request. The host resolves provider configuration and overrides before
134
+ exposing it. Prices are USD per 1,000,000 tokens. `steps.cost` is always the
135
+ host's reported charge; the plugin does not recalculate it.
136
+
137
+ | Surface | Source |
138
+ | --- | --- |
139
+ | Server recorder and backfill | `ctx.model.list()` → `ModelListOutput.data: ModelInfo[]` |
140
+ | Live line (TUI/Desktop) | `data.location.model.list(location)` → `ModelInfo[] \| undefined` |
141
+ | Panel, sidebar, NDJSON, CLI report | stored `steps.list_cost` |
142
+ | `sess` segment | `SUM(list_cost)` from the database, served over the RPC |
143
+
144
+ Each `ModelInfo` carries the prices in `cost: ModelCost[]`, with
145
+ `input`, `output`, `cache.read`, `cache.write` and an optional
146
+ `tier: { type: "context", size }`. The plugin keys its table by
147
+ `providerID/modelID`; variants do not change the price.
148
+
149
+ The plugin deliberately does **not** read `~/.cache/opencode/models.json`, does
150
+ **not** use `opencode models` for prices (that command lists model IDs only),
151
+ and makes no network call of its own.
152
+
153
+ The server loads the list without blocking the recorder, refreshes it on
154
+ `model.updated` and on a coarse TTL, and keeps the previous table when a refresh
155
+ fails. The backfill uses that table. The TUI and the Desktop build their own
156
+ table for the live line, with the same event invalidation and TTL; a missing
157
+ table means no estimate. The historical views never consult it: they read
158
+ `steps.list_cost`, which is why they work without a host runtime.
159
+
160
+ The estimate is the host's own `cost` fallback:
161
+
162
+ ```text
163
+ input tokens.input (fresh, uncached prompt)
164
+ cache.read tokens.cache.read
165
+ cache.write tokens.cache.write
166
+ output visible output tokens
167
+ reasoning reasoning tokens
168
+
169
+ prompt = input + cache.read + cache.write
170
+ tier = the largest tier with tier.type === "context" and prompt > tier.size
171
+ else the untiered entry
172
+ cost = (input * inputPrice
173
+ + (output + reasoning) * outputPrice
174
+ + cache.read * cacheReadPrice
175
+ + cache.write * cacheWritePrice) / 1e6
176
+ ```
177
+
178
+ Reasoning is billed at the output price. For a model with context tiers, the
179
+ strict `>` matters: at exactly the tier size the untiered price applies. No
180
+ applicable tier or an unknown model means no estimate; an all-zero applicable
181
+ price returns `0`, which records a known free model.
182
+
183
+ These are the same buckets the line folds for display: `in = input +
184
+ cache.write`, `cached = cache.read`, `out = output + reasoning`, and `prompt =
185
+ in + cached`. In particular, the formula's `input` is **not** the line's `in`;
186
+ using the displayed `in` would count cache writes twice.
187
+
188
+ A worked example with the `opencode-go/deepseek-v4.1-flash` prices
189
+ (`input 0.15`, `output 0.6`, `cache.read 0.003`):
190
+
191
+ ```text
192
+ 2,591 × 0.15 + (564 + 3,239) × 0.6 + 514,944 × 0.003
193
+ = 4,215.282 per 1,000,000 tokens = $0.004215282
194
+ ```
195
+
196
+ `list_cost` is `NULL` when the step was charged or its price is unknown, `0`
197
+ for a known free model, and positive for an estimate. The line omits a zero
198
+ cost; the panel has a cell to fill, so it shows `$0` for a free or unknown-price
199
+ step.
200
+
201
+ The estimate is stored instead of recomputed on read for a practical reason:
202
+ the panel, the sidebar, the NDJSON export and `bun run report` read the database
203
+ without a host runtime. `list_cost` freezes the price at record time; the live
204
+ turn prices from the client's current model list, which may be newer. The
205
+ maintenance backfill intentionally uses today's model list and is user-triggered,
206
+ because a model that was free when the step ran may not be free now.
207
+
208
+ The session segment follows a split rule: its charged total is the host's, and
209
+ its estimate is `SUM(list_cost)` over the plugin's recorded steps, fetched over
210
+ the RPC and cached by the client, invalidated on every write event. If the
211
+ plugin began recording mid-session, the estimate covers only the recorded steps;
212
+ the charged total still comes from the host. If the database is unavailable, the
213
+ line falls back to the host total with no estimate.
214
+
215
+ The backfill only considers steps with a provider, a model and at least one
216
+ token; it prices free models as `0` and leaves models the host's current model
217
+ list no longer contains as `NULL`, so `e` can be retried after the list changes.
218
+ `pricing: "off"` stops new estimates, hides stored ones in the line, panel and
219
+ sidebar, and disables the backfill. It does not delete the column.
220
+
221
+ ## Non-step usage
222
+
223
+ The host also publishes `session.usage.recorded` with `source: "compaction"` or
224
+ `source: "title"` for generation that has no assistant step of its own. The
225
+ recorder stores it in the `usage` table (`id` = host event ID, `session_id`,
226
+ `source`, `at`, `cost`, and the token buckets). The primary key makes a
227
+ replayed event a no-op; the compaction event itself is stored in `events` for
228
+ the cache-loss reason and keyed by its own host ID.
229
+
230
+ The host includes this usage in the session's own `cost` and token totals, so
231
+ the views that claim to describe a whole selection include its cost: the
232
+ `overview`, the `periods` buckets and the session rows. The overview names the
233
+ part as `other`; a session row names it on its detail line. Tokens and the
234
+ cache views stay step-based (the `input`/`cache_read` columns are summed over
235
+ steps only), because the non-step record carries no model and no cache
236
+ comparison.
237
+
238
+ The `models`, `providers` and `turns` rows stay step-based: the host's non-step
239
+ usage record carries no model, and a title generation has none at all, so
240
+ attributing the money to a model would invent an identity. The `sess` segment is
241
+ unaffected: it already reads the host's session total, which includes this
242
+ usage; the estimate half is still `SUM(list_cost)` over steps, so nothing is
243
+ added twice.
244
+
245
+ This is host money, not a list-price estimate, so `pricing: "off"` does not hide
246
+ it. A database written by an older plugin is healed at open: the stored
247
+ compaction events already carry `cost` and `tokens`, so a migration recovers
248
+ them with a stable synthetic ID. Title usage was never stored anywhere before
249
+ the upgrade and stays missing; the line keeps showing the host's whole session
250
+ total meanwhile.
251
+
252
+ ### Event idempotency
253
+
254
+ An event with a host ID is stored with `message_id`; a partial unique index
255
+ (`kind, message_id`) turns a replay into `INSERT OR IGNORE`. A migration
256
+ deduplicates compaction rows an older recorder inserted on every delivery
257
+ before the index is created, so the event count and any cost read from the
258
+ table stay honest.
259
+
260
+ ## Privacy and security
261
+
262
+ The plugin is local by construction: no network client, no subprocess, no
263
+ credentials, no telemetry. Everything it records stays in the SQLite file in the
264
+ user's own state directory.
265
+
266
+ **What is stored.** Per step: timings, token counts, charged cost and the
267
+ list-price estimate when the host reported `$0`, the provider/model/variant it
268
+ ran as, the finish reason, the session and parent ids, and the session's title
269
+ and working directory as the host reports them. Per compaction or title
270
+ generation: the host event ID, its source, time, cost and token counts. Prompt
271
+ text and generated text are never stored, including for those records.
272
+ **Prompts and responses are not stored**: the recorder reads event payloads for
273
+ numbers and identifiers and keeps nothing else — no message text, no tool
274
+ arguments, no file contents, no environment variables.
275
+
276
+ **What protects it.**
277
+
278
+ - **Private files.** The database directory is created `0700` and the file
279
+ `0600` (POSIX), re-applied on every open; on Windows the file inherits the
280
+ user profile's ACL.
281
+ - **A read-only reader.** The worker opens the database read-only, so panel and
282
+ history queries can never write, migrate or take the recorder's lock. Prune,
283
+ wipe and compact open a short-lived writer inside the worker with a 250 ms
284
+ busy timeout: a maintenance action that cannot take the lock tells the user
285
+ and stops, instead of stalling the request pipeline that records usage.
286
+ - **A bounded, validated RPC.** The TUI and the Desktop GUI read the worker
287
+ through JSON-Schema-validated methods with closed objects, enums and clamps.
288
+ There is no SQL, path, import or export method. Maintenance is gated by the
289
+ `maintenance` option and an explicit confirmation; the worker runs every query
290
+ off the server's event loop and the client queue is bounded while the worker
291
+ is in use. Reads have a 10 s watchdog; prune, wipe, vacuum and backfill get
292
+ five minutes because rewriting a large file is legitimately slower. The
293
+ in-process fallback shares the server's event loop and has no separate queue.
294
+ - **Validated input.** Anything that reaches the database from outside — a
295
+ provider id the host reports, a session title, an imported NDJSON dump — is
296
+ validated as an identifier token or stripped of control characters and capped
297
+ in length, both when it is stored and again when it is rendered. A terminal
298
+ escape sequence in a title cannot reach the terminal, and a dump cannot import
299
+ a value that does not fit its column.
300
+ - **Parameterized SQL everywhere**, and no dynamic table or column names taken
301
+ from data.
302
+
303
+ ## Performance
304
+
305
+ The plugin sits on paths the host waits for: the recorder's event handlers, the
306
+ TUI line and the Desktop header badge. The rules it keeps are:
307
+
308
+ - **Writes are small and rare.** A step is one upsert, written when it ends;
309
+ nothing is written on a response, because the response carries nothing the
310
+ views read.
311
+ - **Reads are index-driven.** Every range filter compares the expression the
312
+ `steps_at` index covers, and the per-prompt history starts from the newest
313
+ prompts. It widens only until it holds the requested number of finished
314
+ turns, so a session with enough history stays bounded; a session with fewer
315
+ finished turns can walk its history once. The line never scans a session's
316
+ whole history.
317
+ - **Clients read through the worker, at most 300 ms apart.** The TUI and the
318
+ Desktop GUI each call the RPC; a burst of step events is coalesced into one
319
+ request, and the worker runs the query off the server's event loop. The
320
+ panel's and the history's rows are shared; the file's own numbers (`dbInfo`)
321
+ are only asked for by the `db` view.
322
+ - **The panel's cost is the range it shows.** A view folds the steps of its
323
+ range — in SQLite where the answer is a sum or a count, in JavaScript where it
324
+ is a percentile or a comparison between consecutive steps. That is why the
325
+ range is a first-class setting, why each view reads the rows once and shares
326
+ them between its reports, and why the budgets in `scripts/bench.ts` are per
327
+ view.
328
+
329
+ Two rules came out of measuring, and the bench enforces both:
330
+
331
+ - **Aggregate in SQL, fold in JS.** `overview` and the `sessions` and `turns`
332
+ views are sums and counts: they never move a row into JavaScript
333
+ (`coverageOf` is one SQL statement). Percentiles and pair comparisons read the
334
+ four to sixteen columns they use and fold them in JS, because SQLite's window
335
+ functions cost several times more per row than the projection does.
336
+ - **One read per report per view.** `test/panel-build.test.ts` counts the
337
+ statements a view prepares, and fails if one view asks the database for the
338
+ steps more than twice.
339
+
340
+ `bun run bench` is the check: it fails on a budget overrun or on a read that
341
+ stopped using an index. `bun run bench` checks the budgets on 20k steps,
342
+ `bun run bench --full` on 120k, and `--explain` prints the query plans.
343
+
344
+ ## Testing the clients
345
+
346
+ The TUI harness is the same one the tests and the documentation images use. The
347
+ Desktop line helpers have their own pure tests (`test/gui-line.test.ts`),
348
+ `test/gui-bundle.test.ts` evaluates the built CJS bundle with the app's shared
349
+ modules, and `test/server.test.ts` drives the real server RPC through the
350
+ worker. `test/limits.test.ts` keeps the client clamps and the RPC schema bounds
351
+ in step, and `test/stats-client.test.ts` covers the worker start/shutdown and
352
+ the separate read/maintenance watchdogs. `bun test` runs through
353
+ `bunfig.toml`, which preloads `test/preload.ts`.
354
+ Bun resolves the bare `solid-js` specifier to the package's server build, where
355
+ signals do not track and render effects run once; the host resolves the client
356
+ build (the one `@opentui/solid` uses internally). The preload points the
357
+ specifier at the client build so the harness, the plugin and the renderer share
358
+ one instance — otherwise every re-render assertion would pass vacuously.
359
+ `scripts/screenshot.ts` does the same alignment before it loads the harness, so
360
+ the documentation images are rendered by the real components.
361
+
362
+ Two consequences for render tests:
363
+
364
+ - The headless renderer paints a single frame, so assertions read the
365
+ **renderable tree** (`treeText` in `test/plugin.test.ts`) after an update
366
+ rather than the frame buffer.
367
+ - A render test must **drive an update** and check that the output was replaced,
368
+ not appended. The line regressed once exactly that way: a `StyledText` passed
369
+ as a child of `<text>` is appended as chunks that are never removed, so each
370
+ reactive update added another full copy of the line. `bun run demo` shows the
371
+ same cases as plain text.
372
+
373
+ ## Pre-release checklist
374
+
375
+ ```sh
376
+ bun install
377
+ bun run typecheck
378
+ bun run build
379
+ bun test # whole suite, Linux
380
+ bun run check:package
381
+ bun run check:platform
382
+ bun run bench --full # budgets at 120k steps
383
+ npm pack --dry-run
384
+ ```
385
+
386
+ Inspect the pack list (`dist/`, `README.md`, `LICENSE` only). Scan for secrets
387
+ before `npm publish`.