fix(code-reviews): fix billing query timeout preventing usage footer on v2 reviews#979
Merged
alex-alecu merged 3 commits intomainfrom Mar 10, 2026
Merged
Conversation
v2 (cloud-agent-next) reviews never write usage to the code_reviews record, so the 3-retry poll loop always waited ~1.4s for nothing before falling through to the billing query. Now we check agent_version and skip the loop when it is v2.
The billing fallback query filters on session_id but that column had no index, so it did a full table scan. This table grows with every LLM request across the platform, so the scan gets slower over time.
Contributor
Code Review SummaryStatus: No Issues Found | Recommendation: Merge Files Reviewed (2 files)
|
RSO
requested changes
Mar 10, 2026
Contributor
RSO
left a comment
There was a problem hiding this comment.
Creating an index locks the table, which will essentially take down the production app because microdollar_usage is the table that receives the most writes (I think). At least this should be CREATE INDEX CONCURRENTLY, but we can probably also limit the index even further by only indexing session_id IS NOT NULL
2 tasks
The billing fallback query filtered microdollar_usage_metadata by session_id, but that column has no index on a ~469M row table. The query timed out on every v2 review, so usage was never shown. Add a created_at lower bound (review creation time) so Postgres uses the existing created_at index. This makes the query fast without needing a new index on session_id. Remove the session_id index migration since it is no longer needed.
RSO
approved these changes
Mar 10, 2026
alex-alecu
added a commit
that referenced
this pull request
Mar 11, 2026
…982) ## Summary - Narrow the "skip" rule from "migrations" to "migration snapshots & journals" so auto-generated Drizzle metadata (`meta/_journal.json`, `meta/*_snapshot.json`, `migrations.js`) is still skipped, but hand-written `.sql` migration files are reviewed - Add DB migration review checklist: table-locking DDL without `CONCURRENTLY`, `NOT NULL` without `DEFAULT`, column drops, unbatched backfills, missing partial indexes - Bump prompt template versions (GitHub v5.6.0, GitLab v5.7.0-gitlab) **Context:** PR #979 added a `CREATE INDEX` on a high-write table. A human caught the missing `CONCURRENTLY` but the code reviewer skipped the file entirely because the prompt said to skip all migrations. ## Verification - [x] `pnpm typecheck` passes - [x] `pnpm test generate-prompt` — all 14 tests pass ## Visual Changes N/A ## Reviewer Notes The key change in the `whatToReview` field is the "Skip these" bullet: `Generated files (lock files, migrations)` → `Generated files (lock files, migration snapshots & journals)`. This removes the contradiction with the new "Database migrations (.sql files — DO review these)" section that was flagged by both sentry[bot] and kilo-code-bot[bot].
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Follow-up to PR #978. The billing fallback query that fetches token/model data for v2 reviews was timing out in production, so the usage footer ("Reviewed by model · X tokens") was never shown.
Root cause: the query filters
microdollar_usage_metadatabysession_id, but that column has no index. The table has ~469M rows, so every query did a full table scan and timed out. Thecatchblock silently returnednull, and the footer was skipped.Fix:
created_at >= reviewCreatedAtlower bound to the billing query. This lets Postgres use the existingcreated_atindex (query cost drops from full-scan to ~288). Billing rows can't exist before the review was created, so the bound is exact.session_idindex migration — with the time bound, it's not needed.Verification
pnpm typecheck— no new errors (only pre-existing kiloclaw errors)pnpm test usage-footer— 10/10 passpnpm test schema— 15/15 pass (no unmigrated schema changes)EXPLAINplan on prod DB — query usesidx_microdollar_usage_metadata_created_atwith cost ~288ses_3282e02f5ffe2vPRBSqdpc0e40(PR Increase Vercel AI Gateway traffic to 20% #981 review) — 8 rows returned in <1s with time-bounded queryVisual Changes
N/A
Reviewer Notes
model = NULL— the billing fallback has never worked. This fix unblocks all future v2 reviews.