const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); /** * Retries an async operation with exponential backoff (M13-T05). * `shouldRetry` decides whether an error is transient. */ export async function withRetry( operation, { retries = 2, baseDelayMs = 500, shouldRetry = () => true } = {}, ) { for (let attempt = 0; ; attempt += 1) { try { return await operation(attempt); } catch (error) { if (attempt >= retries || !shouldRetry(error)) throw error; await sleep(baseDelayMs * 2 ** attempt); } } }