fix(schema): create per-workspace token_usage table (#712) - #784
Conversation
No production path created token_usage — only test fixtures did — so every save_token_usage() raised 'no such table' (swallowed at debug in react_agent), dropping all cost/token data and making cf stats tokens crash. - add _create_token_usage_schema() and wire it into _init_database (fresh workspaces) and _ensure_schema_upgrades (existing DBs), with indexes on timestamp/task_id/agent_id - react_agent._persist_token_usage: log at WARNING, not debug, on failure - test_costs_v2 fixture delegates to the real schema builder (no hand-rolled DDL) Closes #712
|
Warning Review limit reachedYou’ve reached a temporary PR review limit under our Fair Usage Limits Policy. Next review available in: 32 minutes Your organization has reached its usage spending cap. Adjust your spending cap in the billing tab. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (4)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
Code Review — fix(schema): create per-workspace token_usage table (#712)This is a clean, well-scoped P0.1 fix. The root cause analysis is accurate, the implementation is minimal, and the TDD approach (new What works well
Issues / nits1. The old fixture had 2. In 3. The old fixture had Test coverageThe four tests in The roundtrip test calling SummaryApprove with minor nits. The core fix is correct and the test coverage is solid. The most actionable item is changing |
|
Thanks for the review. Addressed the nits:
|
Follow-up reviewAll three nits from my prior review are resolved. Here's the close-out: #1 (nullability comment) — confirmed addressed. #2 (project_id=0 as int) — confirmed correct.
#3 (session_id dropped) — confirmed safe. One minor docstring observation (non-blocking): Verdict: ready to merge. Core fix is correct, schema/INSERT alignment verified, test coverage is solid, and all prior nits are closed. |
What & why
token_usagewas never created by any production code path — the onlyCREATE TABLE token_usagelived in test fixtures. So everysave_token_usage()raisedOperationalError: no such table(swallowed at debug inreact_agent._persist_token_usage), silently dropping all cost/token data and crashingcf stats tokens/ zeroing the/costspage. This contradicts shipped Phase 5.2 cost tracking.Fixes #712 [P0.1] — first launch-blocker from the SaaS launch-readiness audit.
Changes
core/workspace.py: new_create_token_usage_schema(cursor)(columns match the repository INSERT;task_id/agent_id/project_idTEXT for v2 UUID task IDs) with indexes ontimestamp,task_id,agent_id. Wired into:_init_database→ fresh workspaces get the table._ensure_schema_upgrades→ existing workspaces are upgraded idempotently.core/react_agent.py:_persist_token_usagenow logs at WARNING (not debug) on failure, so a future persistence break is visible.tests/ui/test_costs_v2.py: the_ensure_token_usage_tablefixture delegates to the real schema builder instead of hand-rolling DDL (can't drift).Tests (TDD, RED→GREEN)
New
tests/core/test_token_usage_schema.py:Database(the exact react_agent path) with a UUID task_id_ensure_schema_upgradesadds the table to a pre-existing DB that lacked it42 passedacrosstest_workspace.py,test_token_usage_schema.py,test_costs_v2.py.ruffclean.Demo (acceptance criteria)
Fresh
cf init→token_usagepresent →cf stats tokensrenders a zero-state table (exit 0) instead of crashing onno such table.Acceptance criteria
token_usagecreated bycreate_or_load_workspace()with expected columns + indexes on timestamp/task_id/agent_idsave_token_usage()and read it back with no error_persist_token_usagelogs at WARNING on failureScope / not in this PR
The react_agent persist path still routes through the control-plane
Database.initialize(), which pollutes the workspace DB with auth tables — that's the separate #713 [P0.2] (this PR does not touch it). Fixing the missing table here is sufficient to make writes succeed and reads/cf statswork.