Files
Kiro/scripts/retry_utils.ts
Anil Maktala 2916647997 feat: add comprehensive GitHub issue automation system
- Add GitHub Actions workflows for issue triage, duplicate detection, and stale issue management
- Add AWS Bedrock integration for AI-powered issue classification and duplicate detection
- Add TypeScript scripts for issue automation including labeling, triaging, and closing workflows
- Add comprehensive documentation for setup, implementation, and testing procedures
- Add security fixes for prompt injection vulnerabilities in AI model interactions
- Add local testing framework with Jest configuration and test utilities
- Add rate limiting and retry utilities for GitHub API and AWS Bedrock calls
- Add environment configuration examples and ESLint setup for TypeScript scripts
- Add detailed testing results and completion status documentation
- Implement automated duplicate detection using semantic similarity analysis
- Implement stale issue closing with configurable timelines and grace periods
- Implement issue classification and auto-labeling based on content analysis
2026-01-14 12:25:29 -05:00

93 lines
2.3 KiB
TypeScript

/**
* Retry utilities with exponential backoff
*/
export interface RetryOptions {
maxRetries?: number;
baseDelay?: number; // in milliseconds
maxDelay?: number;
retryableErrors?: string[];
}
const DEFAULT_OPTIONS: Required<RetryOptions> = {
maxRetries: 3,
baseDelay: 1000, // 1 second
maxDelay: 8000, // 8 seconds
retryableErrors: ["ThrottlingException", "ServiceUnavailable", "ECONNRESET", "ETIMEDOUT"],
};
/**
* Sleep for specified milliseconds
*/
function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
/**
* Calculate exponential backoff delay
*/
function calculateDelay(attempt: number, baseDelay: number, maxDelay: number): number {
const delay = baseDelay * Math.pow(2, attempt);
return Math.min(delay, maxDelay);
}
/**
* Check if error is retryable
*/
function isRetryableError(error: any, retryableErrors: string[]): boolean {
if (!error) return false;
const errorString = error.toString();
const errorName = error.name || "";
const errorCode = String(error.code || "");
const errorStatus = String(error.status || "");
return retryableErrors.some(
(retryable) =>
errorString.includes(retryable) ||
errorName.includes(retryable) ||
errorCode.includes(retryable) ||
errorStatus.includes(retryable)
);
}
/**
* Retry a function with exponential backoff
*/
export async function retryWithBackoff<T>(
fn: () => Promise<T>,
options: RetryOptions = {}
): Promise<T> {
const opts = { ...DEFAULT_OPTIONS, ...options };
let lastError: any;
for (let attempt = 0; attempt <= opts.maxRetries; attempt++) {
try {
return await fn();
} catch (error) {
lastError = error;
// Don't retry if this is the last attempt
if (attempt === opts.maxRetries) {
break;
}
// Don't retry if error is not retryable
if (!isRetryableError(error, opts.retryableErrors)) {
console.error(`Non-retryable error encountered:`, error);
throw error;
}
const delay = calculateDelay(attempt, opts.baseDelay, opts.maxDelay);
console.log(
`Attempt ${attempt + 1} failed. Retrying in ${delay}ms... Error: ${error}`
);
await sleep(delay);
}
}
console.error(`All ${opts.maxRetries + 1} attempts failed. Last error:`, lastError);
throw lastError;
}