/** * Equal jitter: half of the exponential delay is fixed, half is random. The * result never exceeds `capMs`, so `max_attempts` bounds both attempts and * total wait time. */ export function retryDelayMs( attempt: number, baseMs: number, capMs: number, random: () => number = Math.random, ): number { const exponential = Math.min(capMs, baseMs * 2 ** (attempt - 1)); return Math.floor(exponential * (0.5 + 0.5 * random())); }