feat(codex): include OpenCode token usage in local totals#250
feat(codex): include OpenCode token usage in local totals#250flmalte wants to merge 3 commits intorobinebers:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Extends the Codex provider plugin to include local token usage from OpenCode’s SQLite history (opencode.db) in the existing “local totals” token lines, while keeping cost estimation limited to known Codex model IDs.
Changes:
- Add OpenCode SQLite discovery + query logic and merge its daily token totals with
@ccusage/codexdaily usage. - Add synthetic cost estimation for a fixed set of known Codex model IDs; unknown variants contribute tokens but omit cost.
- Update Codex docs and the supported-provider list to reflect the expanded local token sources.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| plugins/codex/plugin.js | Adds OpenCode SQLite querying, cost estimation, and merges results into local token totals. |
| plugins/codex/plugin.test.js | Adds tests for OpenCode DB access/query failure handling and merged token totals behavior. |
| docs/providers/codex.md | Documents the two local token sources and the “known models only” pricing behavior. |
| README.md | Updates the Codex provider summary to mention local token usage from Codex CLI + OpenCode. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| function parseYmdToLocalStartMs(ymd) { | ||
| if (typeof ymd !== "string") return null | ||
| const value = ymd.trim() | ||
| if (!value) return null | ||
|
|
There was a problem hiding this comment.
The PR currently fails the repo’s global coverage thresholds (90% branches/lines/functions/statements). The new OpenCode helpers add multiple new branches that aren’t fully exercised (e.g., dash-format parsing and invalid inputs in parseYmdToLocalStartMs, plus the various null/fallback paths in cost estimation). Add targeted tests so bun run test:coverage meets the configured thresholds.
There was a problem hiding this comment.
Thanks for the review. I fixed the fallback issue and added a regression test in commit 0d56ec8 so non-array SQLite payloads continue to later DB paths. I also added extra error-path coverage tests for the new OpenCode logic.
For the global coverage gate: bun run test:coverage currently fails on main too (branch coverage is below 90% there as well), so this PR is not introducing a new gate failure. Happy to add more targeted tests if you want this PR to actively raise overall branch coverage.
There was a problem hiding this comment.
1 issue found across 4 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="plugins/codex/plugin.js">
<violation number="1" location="plugins/codex/plugin.js:498">
P2: `return []` exits the entire function instead of `continue`-ing to the next DB path. If `tryParseJson` returns a non-array result (e.g. an object or `null`), no other paths are tried — unlike the `catch` block which correctly falls through. Use `continue` to stay consistent with the error-recovery strategy.</violation>
</file>
Since this is your first cubic review, here's how it works:
- cubic automatically reviews your code and comments on bugs and improvements
- Teach cubic by replying to its comments. cubic learns from your replies and gets better over time
- Add one-off context when rerunning by tagging
@cubic-dev-aiwith guidance or docs links (includingllms.txt) - Ask questions if you need clarification on any suggestion
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 67b15e475d
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| const daily = Array.from(byDay.values()) | ||
| daily.sort((a, b) => b.date.localeCompare(a.date)) | ||
| return daily |
There was a problem hiding this comment.
Continue searching fallback DBs when first has no usage rows
queryOpenCodeTokenUsage returns immediately after parsing the first existing DB, even when that DB has no Codex rows (daily is empty). If a stale/empty DB exists at an earlier path (for example from a previous install path) and the actual data is in a later fallback path, token lines will be silently omitted despite available usage data. The loop should only return early when it finds non-empty usage (or explicitly merge multiple DBs), otherwise continue to later fallback paths.
Useful? React with 👍 / 👎.
|
@cubic-dev-ai review |
@robinebers I have started the AI code review. It will take a few minutes to complete. |
There was a problem hiding this comment.
1 issue found across 4 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="plugins/codex/plugin.test.js">
<violation number="1" location="plugins/codex/plugin.test.js:314">
P2: Hard-coded OpenCode `date` values in new fake-timer tests are timezone-sensitive and can cause flaky `Today` assertions across environments.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
| if (dbPath === "~/Library/Application Support/opencode/opencode.db") { | ||
| return JSON.stringify([ | ||
| { | ||
| date: "2026-02-20", |
There was a problem hiding this comment.
P2: Hard-coded OpenCode date values in new fake-timer tests are timezone-sensitive and can cause flaky Today assertions across environments.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At plugins/codex/plugin.test.js, line 314:
<comment>Hard-coded OpenCode `date` values in new fake-timer tests are timezone-sensitive and can cause flaky `Today` assertions across environments.</comment>
<file context>
@@ -215,6 +215,138 @@ describe("codex plugin", () => {
+ if (dbPath === "~/Library/Application Support/opencode/opencode.db") {
+ return JSON.stringify([
+ {
+ date: "2026-02-20",
+ modelID: "gpt-5.3-codex",
+ inputTokens: 1000000,
</file context>
Summary
opencode.db) with existing@ccusage/codexusage.gpt-5.3-codex-sparkadd tokens but do not get synthetic cost pricing.Testing
bunx vitest run plugins/codex/plugin.test.js✅ (52 passed)bun run test:coverage❌ test suite passes (828/828), but coverage gate fails: global branch coverage is89.07%vs required90%Risk & Rollback
Today,Yesterday,Last 30 Days) and runs only when local OpenCode DB paths are present.Linked Issues
Summary by cubic
Extend the Codex plugin to include OpenCode SQLite token usage in local totals, merged with Codex CLI data. Updates Today/Yesterday/Last 30 Days and shows costs for known Codex models, even when Codex CLI data isn’t available.
Written for commit 67b15e4. Summary will update on new commits.