Skip to content

fix(usage): read CLI 2.x history without double-counting; live Test Connection models - #263

Merged
ltmoerdani merged 2 commits into
mainfrom
fix/issue262-cli2-history-and-issue261-zen-test-model
Oct 20, 2026
Merged

ltmoerdani merged 2 commits into
mainfrom
fix/issue262-cli2-history-and-issue261-zen-test-model

Conversation

@ltmoerdani

Copy link
Copy Markdown
Owner

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 message table, which CLI 2.x no longer populates. It 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 — 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_v2 returns exactly 2× the true total ($5.34 vs $2.67) because session_v2 is a per-session aggregate whose Go total equals the per-message total. Its providerID='opencode' filter would also import Zen free-model usage into the Go tracker — opencode is the Zen provider id; Go is opencode-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's deepseek-v4-flash-free is dead upstream — #204, already in our own KNOWN_UNAVAILABLE_MODEL_IDS; Go's deepseek-v4-flash was replaced by v4.1). Both now use deepseek-v4.1-flash — live on both gateways, cheapest paid tier (max_tokens: 10 ≈ negligible). Paid 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.

Honest caveat on #261: "Zen missing from + Add Models" itself could not be reproduced — the opencodezen vendor contribution, when clause, 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's OpenCode: Model Picker Diagnostics output.

How tested

  • New src/test/cli2HistoryReader.test.ts builds real SQLite DBs via node:sqlite and proves end-to-end: provider filtering (Zen/github-copilot excluded), dedup (3 shared + 1 CLI2-only → 4 rows at the exact total, never 2×), the session_v2.directory cwd join, and a fresh CLI 2.x DB without the legacy message table (case the first implementation missed — the tests caught it).
  • SQL variants validated against a real migrated opencode.db before implementation (combined UNION = 191 rows / $2.671938, identical to per-message truth).
  • npm run lint 7/7 (incl. typecheck + full suite).

Docs

  • docs/issues/100-20261020-issue262-cli2-history-issue261-zen-test-model.md (root cause, evidence tables, lessons)
  • CHANGELOG under [0.7.10]

…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.
@ltmoerdani
ltmoerdani merged commit fce3dd3 into main Oct 20, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

usage: dashboard shows 0 tokens for today with opencode CLI >= 2.0 [BUG] Doesnt not work, Opencode Zen does not show in add models,

1 participant