#16132

fix(agent-usage): count Codex usage from opencode v2 sessions

Opensuleman-dawoodwants to merge intofrom

What

Reads Codex usage from opencode v2's session_message table in addition to the legacy message table. An upgraded database keeps both, so both are scanned and their session ids are prefixed to avoid collisions. The provider and model come from data.model for v2 rows, where the role moves to the type column.

Why

scan_opencode_sessions() only queried message, which opencode v2 no longer writes. A Codex subscription used entirely through opencode v2 therefore reported todayPrompts: 0 and an empty week while the plan meters kept moving (#16129). The Claude collector already made this move in #14049; this is the same treatment for Codex.

A database with neither table (a migration in flight or a corrupt file) now reports the scan as incomplete instead of returning a clean zero, so a transient schema state is not cached as the whole story.

Tests

test/shell.d/agent-usage-codex-scanner-test.sh gains the same three cases the Claude scanner already has:

  • v1 + v2 rows in one database both count, with the v2 model read from data.model
  • v2-only database (no message table) counts
  • user rows, prefix-colliding providers (openai-local), and malformed rows are ignored

Run:

$ bash test/shell.d/agent-usage-codex-scanner-test.sh
... all pass

Also ran agent-usage-codex-notifications-test.sh, agent-usage-fallback-test.sh, and agent-usage-update-test.sh; all pass.

I did not run the collector against a real opencode v2 database; the fixture uses the row shape quoted in the issue.

Notes

  • The v2 SQL prefilters mirror the v1 ones: type = 'assistant' plus a json_valid-guarded exact check on $.model.providerID.
  • No changes to the Claude collector or to the output schema.

Activity

2
greptile-apps[bot]15h ago

Confidence Score: 5/5

[Medium impact] The PR appears safe to merge; no actionable issues were found.

Summary

Adds Codex usage from OpenCode v2 while keeping legacy database support.

  • The Codex collector counts usage from OpenCode v2 sessions.

Reviews (1) · Last reviewed commit: "fix(agent-usage): count Codex usage from..." · Reviewed by Greptile