const ONE_DAY_MS = 24 * 60 * 60 * 1000; const BLOCKING_CLASSIFICATIONS = new Set([ 'WAITING_REQUESTER', 'WAITING_VENDOR', 'SCHEDULED', 'DEPENDENCY_BLOCKED', ]); function isCriticalOverride(ticket) { return Boolean(ticket.sla_vencido) && Number(ticket.prioridade) >= 4; } function computeCooldown({ ticket, classification, contextChanged, memory, now, config }) { const consecutiveDaysSent = memory ? Number(memory.consecutive_days_sent || 0) : 0; const lastSentAt = memory ? memory.last_sent_at : null; const nextReviewAt = memory ? memory.next_review_at : null; const critical = isCriticalOverride(ticket); let blocked = false; let penalty = 0; let reason = 'ok'; let breakCooldown = false; if (classification === 'NEEDS_COORDINATION') { blocked = true; reason = 'needs_coordination'; } else if (nextReviewAt != null && now < nextReviewAt && !contextChanged) { blocked = true; reason = 'waiting_next_review'; } else if (BLOCKING_CLASSIFICATIONS.has(classification) && !contextChanged) { blocked = true; reason = `blocked_${classification.toLowerCase()}`; } const sentRecently = lastSentAt != null && (now - lastSentAt) < ONE_DAY_MS * 1.5; if (!contextChanged && sentRecently && consecutiveDaysSent > 0) { const perDayBase = classification === 'ACTIONABLE_NOW' || classification === 'UNASSIGNED_ACTIONABLE' ? 10 : 15; penalty = Math.min(90, perDayBase * consecutiveDaysSent); if (!blocked) reason = 'repeat_no_change'; } if (blocked && critical && classification !== 'NEEDS_COORDINATION') { breakCooldown = true; blocked = false; penalty = Math.round(penalty / 2); reason = `${reason}_override_critical`; } return { blocked, penalty, reason, breakCooldown, consecutiveDaysSent }; } module.exports = { computeCooldown, isCriticalOverride, BLOCKING_CLASSIFICATIONS };