Real bug confirmed in the prior cycle's Run A: WAITING_VENDOR tickets
entered the Top10 solely because contextChanged=true (a fresh AI call
happened this run) -- contextChanged should only decide whether to
reanalyze (logic/cache.js), never whether an already-still-blocked
ticket can compete.
logic/cooldown.js: removed the `&& !contextChanged` condition from the
BLOCKING_CLASSIFICATIONS check in computeCooldown -- a ticket whose
CURRENT classification (fresh or cached) is in BLOCKING_CLASSIFICATIONS
is now excluded unconditionally. Also expanded BLOCKING_CLASSIFICATIONS
to the full list required this cycle: WAITING_REQUESTER, WAITING_VENDOR,
WAITING_APPROVAL, SCHEDULED, DEPENDENCY_BLOCKED, PROJECT_LONG_TERM,
DUPLICATE_OR_OBSOLETE (WAITING_APPROVAL/PROJECT_LONG_TERM/
DUPLICATE_OR_OBSOLETE previously only had a score penalty, not exclusion
-- WAITING_APPROVAL has positive base score points, so it could
genuinely win a Top10 slot before this fix). The critical-override escape
hatch (SLA vencido + prioridade alta, etc.) is untouched -- still the
only way a blocked classification can surface.
logic/cooldown.test.js: rewrote the one test that was actually asserting
the buggy behavior ("mudanca de contexto real desbloqueia mesmo em
estado de espera" -> now proves the opposite, with a comment explaining
why); added tests for the chronology-driven unblock case (ticket moves
out of a blocking classification entirely) and the two newly-blocking
classes.
logic/score.test.js: added end-to-end tests through computeScore()
proving WAITING_VENDOR and WAITING_APPROVAL are excludedFromTop10 even
with contextChanged=true.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
90 lines
4.2 KiB
JavaScript
90 lines
4.2 KiB
JavaScript
const ONE_DAY_MS = 24 * 60 * 60 * 1000;
|
|
|
|
// Classes que NUNCA disputam o Top10 operacional -- bloqueadas por definicao, nao por cache.
|
|
// Lista expandida na missao "finalizacao para producao" (secao 10): WAITING_APPROVAL,
|
|
// PROJECT_LONG_TERM e DUPLICATE_OR_OBSOLETE entraram aqui (antes so tinham penalidade de
|
|
// score, o que deixava WAITING_APPROVAL -- pontuacao base positiva -- competir de verdade).
|
|
const BLOCKING_CLASSIFICATIONS = new Set([
|
|
'WAITING_REQUESTER', 'WAITING_VENDOR', 'WAITING_APPROVAL', 'SCHEDULED',
|
|
'DEPENDENCY_BLOCKED', 'PROJECT_LONG_TERM', 'DUPLICATE_OR_OBSOLETE',
|
|
]);
|
|
|
|
// Regra centralizada de "isso e critico o bastante pra furar uma exclusao normal" --
|
|
// usada tanto pelo cooldown de reenvio quanto pela politica de AI_FALLBACK/
|
|
// INSUFFICIENT_CONTEXT (applyFallbackPolicy abaixo). Unico lugar que define "critico".
|
|
function isCriticalOverride(ticket) {
|
|
const prioridadeAlta = Number(ticket.prioridade) >= 4 || Number(ticket.urgencia) >= 4;
|
|
const slaVencidoCritico = Boolean(ticket.sla_vencido) && prioridadeAlta;
|
|
const semTecnicoCritico = Boolean(ticket.sem_tecnico) && prioridadeAlta;
|
|
return slaVencidoCritico || semTecnicoCritico;
|
|
}
|
|
|
|
// AI_FALLBACK (chamado nunca analisado pela IA de verdade) e INSUFFICIENT_CONTEXT
|
|
// (analisado, mas sem evidencia suficiente) NAO disputam o Top10 normalmente -- so entram
|
|
// via excecao deterministica forte (isCriticalOverride). Centraliza a regra pedida:
|
|
// "AI_FALLBACK nao pode competir como se fosse acionavel confirmado".
|
|
function applyFallbackPolicy({ aiClassification, ticket }) {
|
|
const critical = isCriticalOverride(ticket);
|
|
|
|
if (aiClassification.is_fallback) {
|
|
return critical
|
|
? { blocked: false, decisionReason: 'ai_fallback_critical_override' }
|
|
: { blocked: true, decisionReason: 'ai_fallback_no_override' };
|
|
}
|
|
|
|
if (aiClassification.classification === 'INSUFFICIENT_CONTEXT') {
|
|
return critical
|
|
? { blocked: false, decisionReason: 'insufficient_context_critical_override' }
|
|
: { blocked: true, decisionReason: 'insufficient_context_no_override' };
|
|
}
|
|
|
|
return { blocked: false, decisionReason: null };
|
|
}
|
|
|
|
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)) {
|
|
// BUG REAL comprovado na Run A anterior: WAITING_VENDOR entrou no Top10 so porque o
|
|
// contexto tinha mudado nesta execucao (contextChanged=true). contextChanged decide SE
|
|
// reanalisamos (logic/cache.js) -- nunca decide se um chamado que CONTINUA classificado
|
|
// numa classe bloqueada pode competir. Por isso este bloqueio e incondicional aqui: se a
|
|
// classificacao atual (fresca ou cacheada, tanto faz) esta em BLOCKING_CLASSIFICATIONS,
|
|
// fica fora do Top10, ponto. Missao "finalizacao para producao", secao 10.
|
|
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, applyFallbackPolicy, BLOCKING_CLASSIFICATIONS };
|