Commit d47f4f6
authored
fix(code-reviews): fix billing query timeout preventing usage footer on v2 reviews (#979)
## Summary
Follow-up to [PR #978](#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
- [x] `pnpm typecheck` — no new errors (only pre-existing kiloclaw
errors)
- [x] `pnpm test usage-footer` — 10/10 pass
- [x] `pnpm test schema` — 15/15 pass (no unmigrated schema changes)
- [x] Checked `EXPLAIN` plan on prod DB — query uses
`idx_microdollar_usage_metadata_created_at` with cost ~288
- [x] Confirmed billing data exists for test session
`ses_3282e02f5ffe2vPRBSqdpc0e40` (PR #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.2 files changed
Lines changed: 29 additions & 24 deletions
File tree
- src
- app/api/internal/code-review-status/[reviewId]
- lib/code-reviews/db
Lines changed: 19 additions & 16 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
116 | 116 | | |
117 | 117 | | |
118 | 118 | | |
119 | | - | |
| 119 | + | |
120 | 120 | | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
125 | 125 | | |
126 | | - | |
127 | | - | |
| 126 | + | |
| 127 | + | |
128 | 128 | | |
129 | 129 | | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | 130 | | |
134 | 131 | | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
139 | 142 | | |
140 | 143 | | |
141 | 144 | | |
| |||
147 | 150 | | |
148 | 151 | | |
149 | 152 | | |
150 | | - | |
151 | | - | |
| 153 | + | |
| 154 | + | |
152 | 155 | | |
153 | 156 | | |
154 | 157 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
| 14 | + | |
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| |||
502 | 502 | | |
503 | 503 | | |
504 | 504 | | |
505 | | - | |
506 | | - | |
507 | | - | |
508 | | - | |
509 | | - | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
510 | 508 | | |
511 | 509 | | |
512 | | - | |
| 510 | + | |
| 511 | + | |
513 | 512 | | |
514 | 513 | | |
515 | | - | |
516 | 514 | | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
517 | 519 | | |
518 | 520 | | |
519 | 521 | | |
| |||
0 commit comments