Merge pull request #64 from BrunoMarc/fix/empty-response-retry

fix: add retry mechanism for empty API responses
This commit is contained in:
Badri Narayanan S
2026-01-08 17:38:18 +05:30
committed by GitHub
5 changed files with 273 additions and 39 deletions

View File

@@ -7,6 +7,7 @@
import crypto from 'crypto';
import { MIN_SIGNATURE_LENGTH, getModelFamily } from '../constants.js';
import { EmptyResponseError } from '../errors.js';
import { cacheSignature, cacheThinkingSignature } from '../format/signature-cache.js';
import { logger } from '../utils/logger.js';
@@ -226,39 +227,10 @@ export async function* streamSSEResponse(response, originalModel) {
}
}
// Handle no content received
// Handle no content received - throw error to trigger retry in streaming-handler
if (!hasEmittedStart) {
logger.warn('[CloudCode] No content parts received, emitting empty message');
yield {
type: 'message_start',
message: {
id: messageId,
type: 'message',
role: 'assistant',
content: [],
model: originalModel,
stop_reason: null,
stop_sequence: null,
usage: {
input_tokens: inputTokens - cacheReadTokens,
output_tokens: 0,
cache_read_input_tokens: cacheReadTokens,
cache_creation_input_tokens: 0
}
}
};
yield {
type: 'content_block_start',
index: 0,
content_block: { type: 'text', text: '' }
};
yield {
type: 'content_block_delta',
index: 0,
delta: { type: 'text_delta', text: '[No response received from API]' }
};
yield { type: 'content_block_stop', index: 0 };
logger.warn('[CloudCode] No content parts received, throwing for retry');
throw new EmptyResponseError('No content parts received from API');
} else {
// Close any open block
if (currentBlockType !== null) {

View File

@@ -8,16 +8,17 @@
import {
ANTIGRAVITY_ENDPOINT_FALLBACKS,
MAX_RETRIES,
MAX_EMPTY_RESPONSE_RETRIES,
MAX_WAIT_BEFORE_ERROR_MS
} from '../constants.js';
import { isRateLimitError, isAuthError } from '../errors.js';
import { isRateLimitError, isAuthError, isEmptyResponseError } from '../errors.js';
import { formatDuration, sleep, isNetworkError } from '../utils/helpers.js';
import { logger } from '../utils/logger.js';
import { parseResetTime } from './rate-limit-parser.js';
import { buildCloudCodeRequest, buildHeaders } from './request-builder.js';
import { streamSSEResponse } from './sse-streamer.js';
import { getFallbackModel } from '../fallback-config.js';
import crypto from 'crypto';
/**
* Send a streaming request to Cloud Code with multi-account support
@@ -143,16 +144,90 @@ export async function* sendMessageStream(anthropicRequest, accountManager, fallb
continue;
}
// Stream the response - yield events as they arrive
yield* streamSSEResponse(response, anthropicRequest.model);
// Stream the response with retry logic for empty responses
// Uses a for-loop for clearer retry semantics
let currentResponse = response;
logger.debug('[CloudCode] Stream completed');
return;
for (let emptyRetries = 0; emptyRetries <= MAX_EMPTY_RESPONSE_RETRIES; emptyRetries++) {
try {
yield* streamSSEResponse(currentResponse, anthropicRequest.model);
logger.debug('[CloudCode] Stream completed');
return;
} catch (streamError) {
// Only retry on EmptyResponseError
if (!isEmptyResponseError(streamError)) {
throw streamError;
}
// Check if we have retries left
if (emptyRetries >= MAX_EMPTY_RESPONSE_RETRIES) {
logger.error(`[CloudCode] Empty response after ${MAX_EMPTY_RESPONSE_RETRIES} retries`);
yield* emitEmptyResponseFallback(anthropicRequest.model);
return;
}
// Exponential backoff: 500ms, 1000ms, 2000ms
const backoffMs = 500 * Math.pow(2, emptyRetries);
logger.warn(`[CloudCode] Empty response, retry ${emptyRetries + 1}/${MAX_EMPTY_RESPONSE_RETRIES} after ${backoffMs}ms...`);
await sleep(backoffMs);
// Refetch the response
currentResponse = await fetch(url, {
method: 'POST',
headers: buildHeaders(token, model, 'text/event-stream'),
body: JSON.stringify(payload)
});
// Handle specific error codes on retry
if (!currentResponse.ok) {
const retryErrorText = await currentResponse.text();
// Rate limit error - mark account and throw to trigger account switch
if (currentResponse.status === 429) {
const resetMs = parseResetTime(currentResponse, retryErrorText);
accountManager.markRateLimited(account.email, resetMs, model);
throw new Error(`429 RESOURCE_EXHAUSTED during retry: ${retryErrorText}`);
}
// Auth error - clear caches and throw with recognizable message
if (currentResponse.status === 401) {
accountManager.clearTokenCache(account.email);
accountManager.clearProjectCache(account.email);
throw new Error(`401 AUTH_INVALID during retry: ${retryErrorText}`);
}
// For 5xx errors, don't pass to streamer - just continue to next retry
if (currentResponse.status >= 500) {
logger.warn(`[CloudCode] Retry got ${currentResponse.status}, will retry...`);
// Don't continue here - let the loop increment and refetch
// Set currentResponse to null to force refetch at loop start
emptyRetries--; // Compensate for loop increment since we didn't actually try
await sleep(1000);
// Refetch immediately for 5xx
currentResponse = await fetch(url, {
method: 'POST',
headers: buildHeaders(token, model, 'text/event-stream'),
body: JSON.stringify(payload)
});
if (currentResponse.ok) {
continue; // Try streaming with new response
}
// If still failing, let it fall through to throw
}
throw new Error(`Empty response retry failed: ${currentResponse.status} - ${retryErrorText}`);
}
// Response is OK, loop will continue to try streamSSEResponse
}
}
} catch (endpointError) {
if (isRateLimitError(endpointError)) {
throw endpointError; // Re-throw to trigger account switch
}
if (isEmptyResponseError(endpointError)) {
throw endpointError; // Re-throw empty response errors to outer handler
}
logger.warn(`[CloudCode] Stream error at ${endpoint}:`, endpointError.message);
lastError = endpointError;
}
@@ -201,3 +276,49 @@ export async function* sendMessageStream(anthropicRequest, accountManager, fallb
throw new Error('Max retries exceeded');
}
/**
* Emit a fallback message when all retry attempts fail with empty response
* @param {string} model - The model name
* @yields {Object} Anthropic-format SSE events for empty response fallback
*/
function* emitEmptyResponseFallback(model) {
// Use proper message ID format consistent with Anthropic API
const messageId = `msg_${crypto.randomBytes(16).toString('hex')}`;
yield {
type: 'message_start',
message: {
id: messageId,
type: 'message',
role: 'assistant',
content: [],
model: model,
stop_reason: null,
stop_sequence: null,
usage: { input_tokens: 0, output_tokens: 0 }
}
};
yield {
type: 'content_block_start',
index: 0,
content_block: { type: 'text', text: '' }
};
yield {
type: 'content_block_delta',
index: 0,
delta: { type: 'text_delta', text: '[No response after retries - please try again]' }
};
yield { type: 'content_block_stop', index: 0 };
yield {
type: 'message_delta',
delta: { stop_reason: 'end_turn', stop_sequence: null },
usage: { output_tokens: 0 }
};
yield { type: 'message_stop' };
}

View File

@@ -76,6 +76,7 @@ export const ANTIGRAVITY_DB_PATH = getAntigravityDbPath();
export const DEFAULT_COOLDOWN_MS = 10 * 1000; // 10 second default cooldown
export const MAX_RETRIES = 5; // Max retry attempts across accounts
export const MAX_EMPTY_RESPONSE_RETRIES = 2; // Max retries for empty API responses
export const MAX_ACCOUNTS = 10; // Maximum number of accounts allowed
// Rate limit wait thresholds
@@ -249,6 +250,7 @@ export default {
ANTIGRAVITY_DB_PATH,
DEFAULT_COOLDOWN_MS,
MAX_RETRIES,
MAX_EMPTY_RESPONSE_RETRIES,
MAX_ACCOUNTS,
MAX_WAIT_BEFORE_ERROR_MS,
MIN_SIGNATURE_LENGTH,

View File

@@ -135,6 +135,20 @@ export class NativeModuleError extends AntigravityError {
}
}
/**
* Empty response error - thrown when API returns no content
* Used to trigger retry logic in streaming handler
*/
export class EmptyResponseError extends AntigravityError {
/**
* @param {string} message - Error message
*/
constructor(message = 'No content received from API') {
super(message, 'EMPTY_RESPONSE', true, {});
this.name = 'EmptyResponseError';
}
}
/**
* Check if an error is a rate limit error
* Works with both custom error classes and legacy string-based errors
@@ -164,6 +178,16 @@ export function isAuthError(error) {
msg.includes('TOKEN REFRESH FAILED');
}
/**
* Check if an error is an empty response error
* @param {Error} error - Error to check
* @returns {boolean}
*/
export function isEmptyResponseError(error) {
return error instanceof EmptyResponseError ||
error?.name === 'EmptyResponseError';
}
export default {
AntigravityError,
RateLimitError,
@@ -172,6 +196,8 @@ export default {
MaxRetriesError,
ApiError,
NativeModuleError,
EmptyResponseError,
isRateLimitError,
isAuthError
isAuthError,
isEmptyResponseError
};