Commit 7e81de7
authored
fix(code-reviews): show model and tokens in review summary for v2 reviews (#978)
## Summary
[PR #407](#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_out` columns
stay null.
**The fix:** when the `code_reviews` record has no usage data, we query
the billing tables (`microdollar_usage` + `microdollar_usage_metadata`)
by `cli_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 the `code_reviews` record so future reads don't repeat the
aggregation.
## Verification
- [x] `pnpm typecheck` — no new errors (pre-existing errors in kiloclaw
only)
- [x] `pnpm test usage-footer` — 10/10 pass
- [x] Read through the billing schema to confirm
`microdollar_usage_metadata.session_id` matches
`code_reviews.cli_session_id`
## Visual Changes
N/A
## Reviewer Notes
- The billing query groups by model and picks the one with the most
tokens (the primary review model). This handles sessions that use
multiple models (e.g. a cheaper model for sub-tasks).
- The back-fill write to `code_reviews` is fire-and-forget (`.catch()`)
— if it fails, the footer still shows correctly; we just won't cache the
result.
- Long-term, cloud-agent-next could include usage data in its
`ExecutionCallbackPayload`, but that's a bigger change. This fix works
today with no changes outside the Next.js app.2 files changed
Lines changed: 125 additions & 9 deletions
File tree
- src
- app/api/internal/code-review-status/[reviewId]
- lib/code-reviews/db
Lines changed: 46 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | | - | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
22 | 27 | | |
23 | 28 | | |
24 | 29 | | |
| |||
112 | 117 | | |
113 | 118 | | |
114 | 119 | | |
115 | | - | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
116 | 128 | | |
117 | 129 | | |
118 | 130 | | |
119 | 131 | | |
120 | 132 | | |
121 | 133 | | |
122 | 134 | | |
| 135 | + | |
123 | 136 | | |
124 | 137 | | |
125 | 138 | | |
126 | 139 | | |
127 | 140 | | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
133 | 172 | | |
134 | 173 | | |
135 | 174 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
10 | | - | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
11 | 15 | | |
12 | 16 | | |
13 | 17 | | |
| |||
479 | 483 | | |
480 | 484 | | |
481 | 485 | | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
0 commit comments