feat(core): increase thought signature retry resilience (#22202)

Co-authored-by: Aishanee Shah <aishaneeshah@google.com>
This commit is contained in:
Bryan Morgan
2026-03-16 17:35:33 -04:00
committed by GitHub
parent 8f22ffd2b1
commit b6c6da3618
+12 -8
View File
@@ -84,13 +84,16 @@ export type StreamEvent =
interface MidStreamRetryOptions { interface MidStreamRetryOptions {
/** Total number of attempts to make (1 initial + N retries). */ /** Total number of attempts to make (1 initial + N retries). */
maxAttempts: number; maxAttempts: number;
/** The base delay in milliseconds for linear backoff. */ /** The base delay in milliseconds for backoff. */
initialDelayMs: number; initialDelayMs: number;
/** Whether to use exponential backoff instead of linear. */
useExponentialBackoff: boolean;
} }
const MID_STREAM_RETRY_OPTIONS: MidStreamRetryOptions = { const MID_STREAM_RETRY_OPTIONS: MidStreamRetryOptions = {
maxAttempts: 4, // 1 initial call + 3 retries mid-stream maxAttempts: 4, // 1 initial call + 3 retries mid-stream
initialDelayMs: 500, initialDelayMs: 1000,
useExponentialBackoff: true,
}; };
export const SYNTHETIC_THOUGHT_SIGNATURE = 'skip_thought_signature_validator'; export const SYNTHETIC_THOUGHT_SIGNATURE = 'skip_thought_signature_validator';
@@ -433,7 +436,10 @@ export class GeminiChat {
attempt < maxAttempts - 1 && attempt < maxAttempts - 1 &&
attempt < maxMidStreamAttempts - 1 attempt < maxMidStreamAttempts - 1
) { ) {
const delayMs = MID_STREAM_RETRY_OPTIONS.initialDelayMs; const delayMs = MID_STREAM_RETRY_OPTIONS.useExponentialBackoff
? MID_STREAM_RETRY_OPTIONS.initialDelayMs *
Math.pow(2, attempt)
: MID_STREAM_RETRY_OPTIONS.initialDelayMs * (attempt + 1);
if (isContentError) { if (isContentError) {
logContentRetry( logContentRetry(
@@ -447,7 +453,7 @@ export class GeminiChat {
attempt + 1, attempt + 1,
maxAttempts, maxAttempts,
errorType, errorType,
delayMs * (attempt + 1), delayMs,
model, model,
), ),
); );
@@ -455,13 +461,11 @@ export class GeminiChat {
coreEvents.emitRetryAttempt({ coreEvents.emitRetryAttempt({
attempt: attempt + 1, attempt: attempt + 1,
maxAttempts: Math.min(maxAttempts, maxMidStreamAttempts), maxAttempts: Math.min(maxAttempts, maxMidStreamAttempts),
delayMs: delayMs * (attempt + 1), delayMs,
error: errorType, error: errorType,
model, model,
}); });
await new Promise((res) => await new Promise((res) => setTimeout(res, delayMs));
setTimeout(res, delayMs * (attempt + 1)),
);
continue; continue;
} }
} }