feat(code-reviews): add structured terminal_reason for billing failures#1275
Merged
alex-alecu merged 16 commits intomainfrom Mar 20, 2026
Merged
feat(code-reviews): add structured terminal_reason for billing failures#1275alex-alecu merged 16 commits intomainfrom
alex-alecu merged 16 commits intomainfrom
Conversation
Contributor
Code Review SummaryStatus: 4 Issues Found | Recommendation: Address before merge Overview
Fix these issues in Kilo Cloud Issue Details (click to expand)WARNING
Other Observations (not in diff)Issues found in unchanged code that cannot receive inline comments:
Files Reviewed (2 files)
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.
6a1ff24 to
115c9fd
Compare
jeanduplessis
approved these changes
Mar 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_reasoncolumn 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