Skip to content

feat(code-reviews): add structured terminal_reason for billing failures#1275

Merged
alex-alecu merged 16 commits intomainfrom
feat/billing-terminal-reason
Mar 20, 2026
Merged

feat(code-reviews): add structured terminal_reason for billing failures#1275
alex-alecu merged 16 commits intomainfrom
feat/billing-terminal-reason

Conversation

@alex-alecu
Copy link
Contributor

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

Summary

Currently we exclude billing errors manually based on upstream error message which turns into a mess when comparing v1 with v2 agent.

Adds a terminal_reason column to code reviews so we know why a review failed (billing, timeout, upstream error) instead of just "failed". This lets admin analytics show billing failures separately and gives users clearer error messages on GitHub/GitLab.

Visual Changes

Screenshot 2026-03-20 at 16 26 22

@kilo-code-bot
Copy link
Contributor

kilo-code-bot bot commented Mar 19, 2026

Code Review Summary

Status: 4 Issues Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 0
WARNING 4
SUGGESTION 0

Fix these issues in Kilo Cloud

Issue Details (click to expand)

WARNING

File Line Issue
packages/db/src/migrations/0056_backfill_terminal_reason.sql 1 Backfill runs as one unbounded migration transaction on an active table.
Other Observations (not in diff)

Issues found in unchanged code that cannot receive inline comments:

File Line Issue
cloudflare-code-review-infra/src/code-review-orchestrator.ts 466 CodeReviewStatusResponse includes terminalReason, but status() still omits it from the returned payload.
src/routers/kiloclaw-router.ts 1197 Standard-plan coupon gating excludes expired trial rows because they are also status = 'canceled', so users who never had a paid subscription can miss the first-month discount after their trial lapses.
src/scripts/d2026-03-19_migrate-kiloclaw-prices.ts 123 Schedule migration omits top-level proration_behavior: 'none', so active schedules can create immediate prorations instead of deferring the new price to the next billing boundary.
Files Reviewed (2 files)
  • packages/db/src/migrations/0055_add_terminal_reason.sql
  • packages/db/src/migrations/0056_backfill_terminal_reason.sql - 1 issue

Reviewed by gpt-5.4-20260305 · 255,684 tokens

Add a structured terminal_reason column (text, nullable) to classify
why a code review ended in a terminal state. Backfills historical
billing failures using existing error_message heuristics.

Recommended values: billing, user_cancelled, superseded, interrupted,
timeout, upstream_error, unknown.
…next client

Surface 402 responses and billing-related error bodies as
CloudAgentNextBillingError instead of generic CloudAgentNextError,
so the orchestrator can classify and handle billing failures without
string matching. Export CloudAgentTerminalReason type for shared use.
…fallback

Detect billing errors via CloudAgentNextBillingError and write
terminal_reason='billing' on failure. Prevent runWithCloudAgentNextFollowup
from falling back to a fresh session on 402 billing failures.
Classify timeout and upstream errors as structured terminal reasons.
… messaging

Pass terminal_reason through the callback normalization and persist it
alongside status. Map billing failures to GitHub action_required
conclusion and explicit billing text for GitHub/GitLab gate checks.
Add terminalReason parameter to updateCodeReviewStatus.
…ation

Prefer terminal_reason='billing' over error_message heuristics in
isBillingError/excludeBillingErrors conditions (with regex fallback for
historical rows). Remove dead billing branch from cancellation analysis.
Add billingRate KPI. Fix stale comment about billing in error analysis.
…ation reasons

Change billing errors bar to rose-400 (#fb7185) to visually distinguish
from cancelled (yellow-500). Show billingRate percentage on Billing
Errors KPI card. Remove dead 'No credits (billing)' entry from
cancellation reason color map.
Verify that updateCodeReviewStatus correctly persists terminal_reason
when a review fails with a billing error.
Worker-utils: verify CloudAgentNextBillingError is thrown on 402,
billing body patterns, and that non-billing errors remain generic.

Route handler: verify interrupted→cancelled normalization, billing
failure stays as failed with terminal_reason, action_required GitHub
conclusion for billing, and historical error_message fallback detection.
Move canonical CodeReviewTerminalReason type to @kilocode/db/schema-types
and import from there in the Next.js app. Add sync comment to the
worker-utils copy. Validate terminalReason against the allowlist in
normalizePayload to prevent arbitrary values in the DB.
…ized errors

Unclassified errors now leave terminal_reason NULL in the DB,
preserving the distinction between 'not yet classified' and an
explicitly categorized failure. This prevents the 'unknown' bucket
from growing large and masking novel failure modes.
Add the structured terminal_reason column to the getExportData query
so admins can filter and classify billing failures in external tooling.
@alex-alecu alex-alecu force-pushed the feat/billing-terminal-reason branch from 6a1ff24 to 115c9fd Compare March 20, 2026 09:45
@alex-alecu alex-alecu merged commit ab96954 into main Mar 20, 2026
18 checks passed
@alex-alecu alex-alecu deleted the feat/billing-terminal-reason branch March 20, 2026 15:12
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