-
Notifications
You must be signed in to change notification settings - Fork 9
feat(code reviewer): tell customers when their own provider key is rate limited #4806
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
45 changes: 45 additions & 0 deletions
45
apps/web/src/lib/code-reviews/terminal-reason-copy.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { CODE_REVIEW_ACTION_REQUIRED_REASONS } from './action-required-shared'; | ||
| import { getCodeReviewTerminalReasonCopy } from './terminal-reason-copy'; | ||
|
|
||
| describe('getCodeReviewTerminalReasonCopy', () => { | ||
| it('returns null for reasons without customer-facing copy', () => { | ||
| expect(getCodeReviewTerminalReasonCopy(null)).toBeNull(); | ||
| expect(getCodeReviewTerminalReasonCopy(undefined)).toBeNull(); | ||
| expect(getCodeReviewTerminalReasonCopy('sandbox_error')).toBeNull(); | ||
| expect(getCodeReviewTerminalReasonCopy('assistant_failed')).toBeNull(); | ||
| }); | ||
|
|
||
| it('names the customer key for a byok rate limit', () => { | ||
| expect(getCodeReviewTerminalReasonCopy('assistant_rate_limited_byok')).toEqual({ | ||
| label: 'Rate limited', | ||
| message: 'Your provider API key hit its rate limit.', | ||
| checkTitle: 'Kilo Code Review rate limited', | ||
| checkSummary: 'Your provider API key hit its rate limit.', | ||
| }); | ||
| }); | ||
|
|
||
| // 'managed' means the request used Kilo's credentials, which currently | ||
| // conflates our upstream quota with our own abuse rules. Telling a customer to | ||
| // check their key would be wrong in both cases. | ||
| it('does not claim a customer key for managed or unqualified rate limits', () => { | ||
| expect(getCodeReviewTerminalReasonCopy('assistant_rate_limited_managed')).toBeNull(); | ||
| expect(getCodeReviewTerminalReasonCopy('assistant_rate_limited')).toBeNull(); | ||
| }); | ||
|
|
||
| // This map is notification only. Action-required reasons additionally disable | ||
| // Code Reviewer and own their own copy, so overlapping the two would give a | ||
| // reason two competing messages. | ||
| it('does not overlap with the action-required reasons', () => { | ||
| const overlapping = CODE_REVIEW_ACTION_REQUIRED_REASONS.filter(reason => | ||
| getCodeReviewTerminalReasonCopy(reason) | ||
| ); | ||
|
|
||
| expect(overlapping).toEqual([]); | ||
| }); | ||
|
|
||
| it('ignores prototype keys', () => { | ||
| expect(getCodeReviewTerminalReasonCopy('constructor')).toBeNull(); | ||
| expect(getCodeReviewTerminalReasonCopy('__proto__')).toBeNull(); | ||
| expect(getCodeReviewTerminalReasonCopy('toString')).toBeNull(); | ||
| }); | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /** | ||
| * Customer-facing copy for terminal reasons that need something friendlier than | ||
| * the raw `error_message`. | ||
| * | ||
| * Deliberately narrow. Most failures either have nothing useful to say to the | ||
| * customer or are already handled by the action-required path, which owns its | ||
| * own copy AND disables Code Reviewer. This map is the opposite: it is | ||
| * notification only and changes no behaviour. | ||
| * | ||
| * A reason belongs here only when we can state the cause accurately. Notably | ||
| * `assistant_rate_limited_managed` is absent: it means the request used Kilo's | ||
| * provider credentials, which currently conflates our upstream quota running out | ||
| * with our own abuse rules throttling the request. Those mean different things | ||
| * to a customer, so until they can be told apart it keeps the generic message | ||
| * rather than getting a sentence that would be wrong half the time. | ||
| */ | ||
|
|
||
| import type { CodeReviewTerminalReason } from '@kilocode/db/schema-types'; | ||
|
|
||
| export type CodeReviewTerminalReasonCopy = { | ||
| /** Replaces the default "Error" label in the app. */ | ||
| label: string; | ||
| /** Shown in place of the raw error_message. */ | ||
| message: string; | ||
| /** GitHub check run title. Kept short; GitHub truncates aggressively. */ | ||
| checkTitle: string; | ||
| /** GitHub check run summary and the GitLab commit status description. */ | ||
| checkSummary: string; | ||
| }; | ||
|
|
||
| /** | ||
| * A Map rather than an object literal. Callers pass the raw `terminal_reason` | ||
| * column, so a value of 'constructor' or '__proto__' would resolve an inherited | ||
| * Object.prototype member on an object literal. That value is truthy, defeating | ||
| * the null fallback and returning a function where copy is expected. Map keys | ||
| * have no prototype chain. | ||
| */ | ||
| const COPY_BY_TERMINAL_REASON = new Map<CodeReviewTerminalReason, CodeReviewTerminalReasonCopy>([ | ||
| [ | ||
| 'assistant_rate_limited_byok', | ||
| { | ||
| label: 'Rate limited', | ||
| message: 'Your provider API key hit its rate limit.', | ||
| checkTitle: 'Kilo Code Review rate limited', | ||
| checkSummary: 'Your provider API key hit its rate limit.', | ||
| }, | ||
| ], | ||
| ]); | ||
|
|
||
| /** | ||
| * Resolve customer-facing copy for a terminal reason, or null when the raw | ||
| * error message should be shown as-is. | ||
| * | ||
| * Accepts the loose `string | null` the DB column and UI props carry, so callers | ||
| * do not have to narrow before asking. | ||
| */ | ||
| export function getCodeReviewTerminalReasonCopy( | ||
| terminalReason: string | null | undefined | ||
| ): CodeReviewTerminalReasonCopy | null { | ||
| if (!terminalReason) return null; | ||
| return COPY_BY_TERMINAL_REASON.get(terminalReason as CodeReviewTerminalReason) ?? null; | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.