Skip to content

fix(code-reviews): fix billing query timeout preventing usage footer on v2 reviews#979

Merged
alex-alecu merged 3 commits intomainfrom
fix/code-review-usage-followup
Mar 10, 2026
Merged

fix(code-reviews): fix billing query timeout preventing usage footer on v2 reviews#979
alex-alecu merged 3 commits intomainfrom
fix/code-review-usage-followup

Conversation

@alex-alecu
Copy link
Copy Markdown
Contributor

@alex-alecu alex-alecu commented Mar 10, 2026

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_metadata by session_id, but that column has no index. The table has ~469M rows, so every query did a full table scan and timed out. The catch block silently returned null, and the footer was skipped.

Fix:

  • Add a created_at >= reviewCreatedAt lower bound to the billing query. This lets Postgres use the existing created_at index (query cost drops from full-scan to ~288). Billing rows can't exist before the review was created, so the bound is exact.
  • Skip the v1 poll loop for v2 reviews (saves ~1.4s of wasted retries).
  • Remove the session_id index migration — with the time bound, it's not needed.
  • Clean up the admin dashboard: remove agent version filter and performance chart that are no longer useful now that all reviews are v2.

Verification

  • pnpm typecheck — no new errors (only pre-existing kiloclaw errors)
  • pnpm test usage-footer — 10/10 pass
  • pnpm test schema — 15/15 pass (no unmigrated schema changes)
  • Checked EXPLAIN plan on prod DB — query uses idx_microdollar_usage_metadata_created_at with cost ~288
  • Confirmed billing data exists for test session ses_3282e02f5ffe2vPRBSqdpc0e40 (PR Increase Vercel AI Gateway traffic to 20% #981 review) — 8 rows returned in <1s with time-bounded query

Visual Changes

N/A

Reviewer Notes

  • Every completed v2 review in prod has model = NULL — the billing fallback has never worked. This fix unblocks all future v2 reviews.
  • The back-fill write (fire-and-forget) still runs after fetching billing data, so repeat reads skip the aggregation.

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.
@kilo-code-bot
Copy link
Copy Markdown
Contributor

kilo-code-bot Bot commented Mar 10, 2026

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Files Reviewed (2 files)
  • src/app/api/internal/code-review-status/[reviewId]/route.ts
  • src/lib/code-reviews/db/code-reviews.ts

Copy link
Copy Markdown
Contributor

@RSO RSO left a comment

Choose a reason for hiding this comment

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

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

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.
@alex-alecu alex-alecu changed the title perf(code-reviews): fix v2 review completion delay and add missing index fix(code-reviews): fix billing query timeout preventing usage footer on v2 reviews Mar 10, 2026
@alex-alecu alex-alecu merged commit d47f4f6 into main Mar 10, 2026
19 checks passed
@alex-alecu alex-alecu deleted the fix/code-review-usage-followup branch March 10, 2026 14:13
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].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants