Repository navigation
fix(usage): read CLI 2.x history without double-counting; live Test Connection models - #263
Merged
ltmoerdani merged 2 commits intoOct 20, 2026
Conversation
…ing; live Test Connection models Issue #262: the usage reader queried only the CLI 1.x 'message' table, which CLI >= 2.0 no longer populates, so Today showed 0 tokens. The reader now probes sqlite_master and reads the CLI 2.x session_message projection (role in the type column, provider/model under $.model.*, cwd joined from session_v2.directory) UNION the legacy table — UNION dedups the rows the 2.x migration copied into both schemas. The issue's proposed UNION ALL against session_v2 was rejected on real data: session_v2 is a per-session AGGREGATE (its Go total equals the per-message total), so that patch doubles every dollar/token; and its providerID='opencode' filter imports Zen free-model usage into the Go tracker (Go is 'opencode-go'). Fresh CLI 2.x databases without the legacy table fall back to a v2-only query (caught by the new tests). Issue #261: both testModelIds pointed at models gone from the live catalogs — Zen's deepseek-v4-flash-free is dead upstream (#204) and Go's deepseek-v4-flash was replaced by v4.1. Both now use deepseek-v4.1-flash (live on both gateways, cheapest paid tier). A paid model is deliberate for Zen: free models answer 403 FreeTierError to any non-OpenCode client regardless of key validity, so a free test model can never validate a credential. New src/test/cli2HistoryReader.test.ts builds real SQLite DBs via node:sqlite and proves provider filtering, dedup (3 shared + 1 v2-only = 4 rows, exact total, never 2x), the cwd join, and the no-legacy-table case. npm run lint 7/7.
…vailable The CI matrix also runs Node 20, where node:sqlite does not exist — require() throws instead of returning undefined, failing the three DB-backed tests. Availability is now probed once (try/catch) and the tests skip with a reason there; the probe-parsing test still runs everywhere.
This was referenced Oct 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #262
Fixes #261
What
#262 — usage dashboard showed 0 tokens for today with opencode CLI ≥ 2.0. The reader queried only the CLI 1.x
messagetable, which CLI 2.x no longer populates. It now probessqlite_masterand reads the CLI 2.xsession_messageprojection (role in thetypecolumn, provider/model under$.model.*, cwd joined fromsession_v2.directory)UNIONthe legacy table — deduplicating the rows the 2.x migration copied into both schemas.Why not the patch proposed in the issue: measured on a real migrated DB,
UNION ALL message + session_v2returns exactly 2× the true total ($5.34 vs $2.67) becausesession_v2is a per-session aggregate whose Go total equals the per-message total. ItsproviderID='opencode'filter would also import Zen free-model usage into the Go tracker —opencodeis the Zen provider id; Go isopencode-go(verified against per-provider model-id sets and the CLI's own models.dev snapshot keys).#261 — Test Connection always failed. Both
testModelIds pointed at models gone from the live catalogs (Zen'sdeepseek-v4-flash-freeis dead upstream — #204, already in our ownKNOWN_UNAVAILABLE_MODEL_IDS; Go'sdeepseek-v4-flashwas replaced by v4.1). Both now usedeepseek-v4.1-flash— live on both gateways, cheapest paid tier (max_tokens: 10≈ negligible). Paid is deliberate for Zen: free models answer403 FreeTierErrorto any non-OpenCode client regardless of key validity, so a free test model can never validate a credential.Honest caveat on #261: "Zen missing from + Add Models" itself could not be reproduced — the
opencodezenvendor contribution,whenclause, activation event, and registration are symmetric with Go and correct, and the live Zen catalog returns 85 models (10 free). Most plausible user-side causes: the Agents window "+ Add Models" list (agent vendors hidden by default) or an outdated VS Code. If it persists after this fix we need the reporter'sOpenCode: Model Picker Diagnosticsoutput.How tested
src/test/cli2HistoryReader.test.tsbuilds real SQLite DBs vianode:sqliteand proves end-to-end: provider filtering (Zen/github-copilotexcluded), dedup (3 shared + 1 CLI2-only → 4 rows at the exact total, never 2×), thesession_v2.directorycwd join, and a fresh CLI 2.x DB without the legacymessagetable (case the first implementation missed — the tests caught it).opencode.dbbefore implementation (combined UNION = 191 rows / $2.671938, identical to per-message truth).npm run lint7/7 (incl. typecheck + full suite).Docs
docs/issues/100-20261020-issue262-cli2-history-issue261-zen-test-model.md(root cause, evidence tables, lessons)[0.7.10]