/** * Retry/backoff dos AI jobs (agente.md secao 108): "retry, exponential * backoff, dead-letter strategy, limitar tentativas" — nunca retry * infinito. */ const BASE_DELAY_MS = 30_000; // 30s const MAX_DELAY_MS = 30 * 60_000; // 30min /** Backoff exponencial com teto — 2^attempt * base, nunca passa do teto. */ export function computeBackoffDelayMs(attemptCount: number): number { const delay = BASE_DELAY_MS * 2 ** Math.max(0, attemptCount - 1); return Math.min(delay, MAX_DELAY_MS); } export function computeNextScheduledAt(attemptCount: number, now: Date = new Date()): Date { return new Date(now.getTime() + computeBackoffDelayMs(attemptCount)); } /** Secao 108: "limitar tentativas" — dead-letter (FAILED terminal) quando * esgotar. */ export function isDeadLetter(attemptCount: number, maxAttempts: number): boolean { return attemptCount >= maxAttempts; }