Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/api/providers/openrouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,27 @@ export class OpenRouterHandler extends BaseProvider implements SingleCompletionH

let stream
try {
stream = await this.client.chat.completions.create(completionParams, requestOptions)
// kilocode_change start
let attempts = 0
while (true) {
try {
attempts++
stream = await this.client.chat.completions.create(completionParams, requestOptions)
break
} catch (error: any) {
const msg = error?.message || ""
const isBedrockNetworkError =
msg.includes("Amazon Bedrock error") && msg.includes("Network connection lost")

Comment on lines +444 to +448
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Action required

2. Retry misses wrapped errors 🐞 Bug ✓ Correctness

The new retry condition only checks error.message, but OpenRouter errors are often wrapped with
the meaningful message inside error.error.message and/or error.error.metadata.raw. This can
cause the Bedrock network retry to never trigger even when the underlying error matches.
Agent Prompt
### Issue description
The Bedrock retry logic only checks `error.message`, but OpenRouter errors are frequently wrapped (e.g., `error.error.message` and/or `error.error.metadata.raw`). As a result, the intended Bedrock retry may not trigger.

### Issue Context
This file already contains utilities and schema definitions for extracting the “real” upstream error message from `metadata.raw`. The retry logic should use the same extraction approach.

### Fix Focus Areas
- src/api/providers/openrouter.ts[435-455]
- src/api/providers/openrouter.ts[76-140]
- src/api/providers/openrouter.ts[865-881]

### Implementation notes
- Create a small helper (local function) like `getOpenRouterErrorMessage(error: any): string` that tries, in order:
  - `error?.message`
  - `error?.error?.message`
  - `extractErrorFromMetadataRaw(error?.metadata?.raw)`
  - `extractErrorFromMetadataRaw(error?.error?.metadata?.raw)`
  - fallback `String(error)`
- Use the returned string for `isBedrockNetworkError` matching.

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

if (isBedrockNetworkError && attempts < 3) {
console.log(`[OpenRouter] Retrying Bedrock network error (attempt ${attempts}/3): ${msg}`)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Action required

1. console.log leaks error details 📘 Rule violation ⛨ Security

The new retry logic logs the raw error.message, which may contain sensitive/internal details and
is unstructured for auditing. This violates secure logging requirements and can expose information
in logs.
Agent Prompt
## Issue description
The retry code logs raw `error.message` via `console.log`, which is unstructured and may leak sensitive/internal error details.

## Issue Context
This code runs in a provider integration path; logs should be structured and must not include sensitive data.

## Fix Focus Areas
- src/api/providers/openrouter.ts[445-451]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

await new Promise((resolve) => setTimeout(resolve, attempts * 2000))
continue
}
throw error
}
}
// kilocode_change end
} catch (error) {
// kilocode_change start
// KiloCode backend errors are already user-readable and should be handled upstream.
Expand Down
Loading