Files
SLACK_COORD_RELATORIO/logic/message.js
T
GustavoandClaude Sonnet 5 124516e64a fix(v2): fix "Validar e executar" bug (real root cause), expand generic-phrase blacklist, add FORCE_SEND
Root cause found for why the real Slack message showed "→ Validar e
executar" for nearly every ACTIONABLE_NOW ticket: logic/message.js's
ACTION_LABELS map had a hardcoded entry for ACTIONABLE_NOW that always
won over the AI's actual next_action field in shortActionLabel(). The
AI's real, specific, already-validated-as-non-generic next_action (e.g.
"Realizar inventário das máquinas") was being silently discarded for
display purposes on every single message. Fixed by removing the
ACTIONABLE_NOW entry -- it now falls through to the existing
next_action-based fallback (already there, just unreachable for that
class before). UNASSIGNED_ACTIONABLE intentionally keeps its fixed
"Atribuir técnico" label -- that IS the correct display for that class.

logic/classification.js: expanded GENERIC_NEXT_ACTION_PHRASES with the
exact phrase that leaked through ("validar e executar") plus the
mission's additional list (validar chamado, prosseguir atendimento,
executar chamado, tratar chamado, analisar chamado). Bumped
AI_CLASSIFIER_VERSION 3->4 since this changes what next_action values
validate as non-generic -- forces reclassification of any cached
candidate that only passed under the older, narrower blacklist.

n8n/nodes/validate-ai-response.node.js: generic-next-action rejections
now get their own distinct fallback_reason ('generic_or_unsupported_
next_action') instead of being lumped into 'invalid_json', matching the
same pattern already used for the technician-vendor conflict.

logic/slack.js + logic/config.js: added FORCE_SEND (default false).
Manual-triggered runs now never send for real even with SEND_ENABLED=
true in production, unless FORCE_SEND is also explicitly set -- protects
against someone clicking "Execute Workflow" in production and
accidentally re-sending today's digest.

Added format-safety regression tests (no literal backslash-n, URL never
bleeds into the next item's id, no markdown-wrapped URLs) -- verified
these were never actually triggered in a real send (the reported
corruption was n8n's raw JSON data-inspector view, not a delivered
Slack message; both manual runs in question had shouldSend:false).

169/169 tests passing.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-24 21:49:42 -03:00

63 lines
2.8 KiB
JavaScript

// BUG REAL (missao "correcao final", secao 5): ACTIONABLE_NOW tinha um rotulo fixo aqui
// ("Validar e executar") que sempre vencia o next_action de verdade da IA -- por isso TODA
// mensagem real mostrava a mesma frase generica pra praticamente todo chamado acionavel,
// mesmo com a IA ja tendo gerado (e validado como nao-generica, ver logic/classification.js)
// um next_action especifico como "Realizar inventario das maquinas". Removido daqui: agora
// ACTIONABLE_NOW cai no fallback abaixo, que usa o next_action real (truncado).
// UNASSIGNED_ACTIONABLE continua com rotulo fixo de proposito -- pra esse caso "atribuir
// tecnico" E a acao real, independente do que o next_action interno da IA descreva.
const ACTION_LABELS = {
UNASSIGNED_ACTIONABLE: 'Atribuir técnico',
WAITING_APPROVAL: 'Cobrar aprovação',
INSUFFICIENT_CONTEXT: 'Completar informação',
};
function shortActionLabel(aiClassification) {
if (aiClassification.is_fallback) return 'Revisar chamado';
const fromMap = ACTION_LABELS[aiClassification.classification];
if (fromMap) return fromMap;
const raw = String(aiClassification.next_action || 'Revisar chamado').trim();
return raw.length > 40 ? `${raw.slice(0, 37)}...` : raw;
}
function tituloCurto(texto) {
const limpo = String(texto || 'Sem titulo').replace(/\s+/g, ' ').trim();
return limpo.length > 60 ? `${limpo.slice(0, 57)}...` : limpo;
}
function signalLine(ticket) {
const parts = [];
parts.push(ticket.sem_tecnico ? 'SEM TÉCNICO' : ticket.tecnico_atribuido);
if (ticket.sla_vencido) parts.push('SLA vencido');
parts.push(`parado ${ticket.dias_sem_interacao}d`);
return parts.join(' • ');
}
function buildTopMessage(selected, dateStr, config) {
if (!selected.length) {
return `@time Coordenador GLPI - ${dateStr}\n\nNenhum chamado acionável encontrado hoje.`;
}
const header = `@time ${selected.length} chamados para atenção hoje — ${dateStr}`;
const blocks = selected.map((item, index) => {
const t = item.ticket;
const linhasDoBloco = [
`${index + 1}. #${t.id} • ${tituloCurto(t.titulo)}`,
signalLine(t),
`→ ${shortActionLabel(item.aiClassification)}`,
t.link_glpi,
];
return linhasDoBloco.join('\n');
});
const partesDaMensagem = [header, ...blocks];
return partesDaMensagem.join('\n\n');
}
function buildCoordinationDigest(changedBlocked, dateStr) {
if (!changedBlocked.length) return null;
const lines = changedBlocked.map((item) => `#${item.ticket.id} • ${(item.aiClassification && item.aiClassification.reason) || item.exclusionReason}`);
const partesDoDigest = [`Coordenação — bloqueios novos/alterados — ${dateStr}`, '', ...lines];
return partesDoDigest.join('\n');
}
module.exports = { shortActionLabel, tituloCurto, signalLine, buildTopMessage, buildCoordinationDigest };