Skip to content

feat(web): show session cost on review and session trace screens - #6886

Merged
eshurakov merged 3 commits into
mainfrom
eshurakov/session-cost
Sep 29, 2026
Merged

eshurakov merged 3 commits into
mainfrom
eshurakov/session-cost

Conversation

@eshurakov

@eshurakov eshurakov commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Session cost was not visible where it matters. The code review details screen only showed review.total_cost_musd, which stays 0 for v2 (cloud-agent-next) reviews because the orchestrator accumulates nothing from SSE, so failed / cancelled / interrupted reviews showed no cost. The admin session trace viewer never rendered cost at all.

This change:

  • Code review details (codeReviews.get): returns a new sessionCostMusd derived from the billing aggregate getSessionUsageFromBilling(...) that the router already computes for token usage (sums microdollar_usage.cost joined via microdollar_usage_metadata.session_id, bounded by the review's created/completed times). CodeReviewDetailClient prefers it and falls back to review.total_cost_musd.
  • Admin session trace viewer: renders cli_sessions_v2.total_cost_microdollars as a Cost row.
  • Admin v1 session shape: nulls total_cost_microdollars (v1 cli_sessions has no cost column) so the v1/v2 response shape stays consistent.

Both screens render the value through the existing formatSessionCost helper (the same one the cloud agent session UI uses), so the cost is shown to the cent ($12.35) rather than four decimals, and the row is hidden below half a cent (isRenderableSessionCost) instead of showing $0.00.

Note: the completion status route already copies the billing cost into total_cost_musd on success, so the visible gain is mainly failed / cancelled / interrupted reviews and cases where that copy did not happen.

Verification

Manually tested on the local dev stack (this worktree's nextjs on port 7700 and its own Postgres), against seeded rows only — no production data.

  • Admin Session Trace Viewer, /admin/session-traces?sessionId=ses_...: shows Cost: $12.35 for a v2 session with total_cost_microdollars = 12345678.
  • Code review details, /code-reviews/<id>: shows Cost $4.32 for a completed review whose total_cost_musd = 0 and whose only cost is a billing row (microdollar_usage.cost = 4321000). This exercises the billing path, not the fallback.
  • Fallback path: a completed review with no cli_session_id and total_cost_musd = 999000 shows Cost $1.00.
  • Automated gates in this worktree (apps/web): typecheck exit 0; oxlint 0 warnings / 0 errors on the changed files; jest code-reviews-router.test.ts + admin-session-traces.test.ts 115/115 passed.

Visual Changes

Before After
No Cost row Admin session trace viewer with Cost row
Cost row hidden because total_cost_musd = 0 Code review details with billing-derived Cost row

Fallback (no linked session, cost from review.total_cost_musd):

Code review details fallback cost

Reviewer Notes

  • Authorization: sessionCostMusd is not role-gated, but it exposes no raw ledger identifiers, and the pre-existing Cost row already rendered review.total_cost_musd. listForOrganization deliberately keeps cost visible to non-admin members.
  • Formatting is owned by formatSessionCost / isRenderableSessionCost, shared with the cloud agent session UI, so the cent rounding and the sub-half-cent cutoff stay consistent with the rest of the product.
  • Test coverage: the added test covers the billing-derived path (review.total_cost_musd = 0). Not covered by unit tests: sessionCostMusd === null when there are no billing rows, and the client-side fallback (both verified manually above).

…creens

Code review details now prefer the billing-derived session cost (getSessionUsageFromBilling) over the orchestrator-accumulated total_cost_musd, which stays 0 for v2 cloud-agent-next reviews and for failed, cancelled or interrupted runs. The admin session trace viewer renders cli_sessions_v2.total_cost_microdollars, and the v1 session shape nulls that field so v1/v2 responses stay consistent.
@kilo-code-bot

kilo-code-bot Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Executive Summary

The incremental change (HEAD 09eb2869) swaps the code-review detail and admin session-trace Cost rendering to the shared formatSessionCost / isRenderableSessionCost helpers, with microdollar→USD conversion at the call sites and matching render gates; units, null-guards, and $ prefixing are correct and no issues were found.

Files Reviewed (2 source files + 3 screenshot assets)
  • apps/web/src/app/(app)/code-reviews/[reviewId]/CodeReviewDetailClient.tsx
  • apps/web/src/app/admin/components/SessionTraceViewer.tsx
  • .github/pr-assets/session-cost/01-admin-session-trace-cost.png
  • .github/pr-assets/session-cost/02-code-review-detail-cost.png
  • .github/pr-assets/session-cost/03-code-review-detail-fallback-cost.png
Previous Review Summary (commit e5e9754)

Current summary above is authoritative. Previous snapshots are kept for context only.

Previous review (commit e5e9754)

Status: No Issues Found | Recommendation: Merge

Executive Summary

Verified the billing-derived sessionCostMusd addition on codeReviews.get, its client fallback and unit test, the admin session trace Cost row, and the v1/v2 response-shape alignment; the units, null-handling, and role/authorization behavior are consistent with existing conventions.

Files Reviewed (5 files)
  • apps/web/src/app/(app)/code-reviews/[reviewId]/CodeReviewDetailClient.tsx
  • apps/web/src/app/admin/components/SessionTraceViewer.tsx
  • apps/web/src/routers/admin-router.ts
  • apps/web/src/routers/code-reviews-router.test.ts
  • apps/web/src/routers/code-reviews/code-reviews-router.ts

Reviewed by deepseek-v4.1-flash · Input: 0 · Output: 0 · Cached: 0

Review guidance: REVIEW.md from base branch main

Both cost rows now use the shared formatSessionCost/isRenderableSessionCost helpers from the cloud agent session UI, so the value is shown to the cent instead of four decimals, and the row is hidden below half a cent rather than showing $0.00. Screenshots refreshed without the source-survey popup.
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