Skip to content

feat(codex): include OpenCode token usage in local totals#250

Open
flmalte wants to merge 3 commits intorobinebers:mainfrom
flmalte:feat/codex-opencode-token-usage
Open

feat(codex): include OpenCode token usage in local totals#250
flmalte wants to merge 3 commits intorobinebers:mainfrom
flmalte:feat/codex-opencode-token-usage

Conversation

@flmalte
Copy link

@flmalte flmalte commented Feb 27, 2026

Summary

  • Extend the Codex plugin to merge local token usage from OpenCode SQLite history (opencode.db) with existing @ccusage/codex usage.
  • Keep Spark safe: unknown variants like gpt-5.3-codex-spark add tokens but do not get synthetic cost pricing.
  • Update Codex docs and the supported-provider list to reflect Codex local token coverage from both Codex CLI and OpenCode.

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 is 89.07% vs required 90%

Risk & Rollback

  • Risk: low to medium. Added logic only affects Codex local token lines (Today, Yesterday, Last 30 Days) and runs only when local OpenCode DB paths are present.
  • Rollback: revert this PR to restore Codex local token behavior to ccusage-only.

Linked Issues

  • None.

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.

  • New Features
    • Merge OpenCode (~/.local/share/opencode/opencode.db with platform fallbacks) daily tokens with ccusage results; show lines if either source has data.
    • Estimate costs for known Codex models; unknown variants (e.g. gpt-5.3-codex-spark) count tokens without pricing.
    • Continue fallback across DB paths on permission errors, sqlite query failures, or malformed/non-array rows.
    • Update docs and provider list to reflect local token coverage from Codex CLI + OpenCode.

Written for commit 67b15e4. Summary will update on new commits.

@flmalte flmalte requested a review from robinebers as a code owner February 27, 2026 21:24
Copilot AI review requested due to automatic review settings February 27, 2026 21:24
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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/codex daily 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.

Comment on lines +402 to +406
function parseYmdToLocalStartMs(ymd) {
if (typeof ymd !== "string") return null
const value = ymd.trim()
if (!value) return null

Copy link

Copilot AI Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Copy link
Author

@flmalte flmalte Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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-ai with guidance or docs links (including llms.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.

Repository owner deleted a comment from chatgpt-codex-connector bot Mar 1, 2026
@validatedev
Copy link
Collaborator

@codex review

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment on lines +534 to +536
const daily = Array.from(byDay.values())
daily.sort((a, b) => b.date.localeCompare(a.date))
return daily

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

@robinebers
Copy link
Owner

@cubic-dev-ai review

@cubic-dev-ai
Copy link
Contributor

cubic-dev-ai bot commented Mar 3, 2026

@cubic-dev-ai review

@robinebers I have started the AI code review. It will take a few minutes to complete.

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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",
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Fix with Cubic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants