fix(code-reviews): show model and tokens in review summary for v2 reviews#978
fix(code-reviews): show model and tokens in review summary for v2 reviews#978alex-alecu merged 2 commits intomainfrom
Conversation
…eviews PR #407 added model/token tracking to the review summary, but it only works for v1 (SSE) reviews. All reviews now use v2 (cloud-agent-next), which skips SSE stream processing entirely, so usage is never reported. Fix: when the code_reviews record has no usage data, query the billing tables (microdollar_usage + metadata) by cli_session_id to get the model, tokens, and cost. Back-fill the code_reviews record so future reads skip the aggregation.
Code Review SummaryStatus: 2 Issues Found | Recommendation: Address before merge Overview
Issue Details (click to expand)WARNING
Fix these issues in Kilo Cloud Other Observations (not in diff)None. Files Reviewed (2 files)
|
The billing query grouped by model and picked one row, so tokens from other models in the same session were lost. Split into two queries: one for session-wide totals, one for the dominant model name.
| let review = await getCodeReviewById(reviewId); | ||
|
|
||
| // Short poll: usage may arrive from the orchestrator just before the callback | ||
| for (let attempt = 0; attempt < MAX_RETRIES && review && !review.model; attempt++) { |
There was a problem hiding this comment.
WARNING: This adds a fixed delay to every v2 completion callback
The comment above says cloud-agent-next reviews never receive orchestrator usage, but this loop still waits through the full exponential backoff whenever review.model is empty. For v2 reviews that means every completion path pays ~1.4s before we even try the billing fallback, which delays the reaction/comment update for every successful review.
| cliSessionId: string | ||
| ): Promise<SessionUsageSummary | null> { | ||
| try { | ||
| const sessionFilter = eq(microdollar_usage_metadata.session_id, cliSessionId); |
There was a problem hiding this comment.
WARNING: This fallback filters on an unindexed column
microdollar_usage_metadata only has a created_at index in the schema today, so both aggregation queries will end up scanning the metadata table by session_id on every completed v2 review. Because this runs on the completion callback path, larger billing tables will make review completion and summary updates noticeably slower.
…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.
Summary
PR #407 added model + token info to the PR review summary comment (the "Reviewed by claude-sonnet-4.6 · 12,345 tokens" footer). But it only works for v1 (SSE-based) reviews. All reviews now run on v2 (cloud-agent-next), which is callback-based — it never streams SSE events, so usage data is never collected and the
model,total_tokens_in,total_tokens_outcolumns stay null.The fix: when the
code_reviewsrecord has no usage data, we query the billing tables (microdollar_usage+microdollar_usage_metadata) bycli_session_id. The billing system already tracks every LLM call with model, tokens, and cost — we just aggregate it per session. We also back-fill thecode_reviewsrecord so future reads don't repeat the aggregation.Verification
pnpm typecheck— no new errors (pre-existing errors in kiloclaw only)pnpm test usage-footer— 10/10 passmicrodollar_usage_metadata.session_idmatchescode_reviews.cli_session_idVisual Changes
N/A
Reviewer Notes
code_reviewsis fire-and-forget (.catch()) — if it fails, the footer still shows correctly; we just won't cache the result.ExecutionCallbackPayload, but that's a bigger change. This fix works today with no changes outside the Next.js app.