Files
SLACK_COORD_RELATORIO/n8n/nodes/prepare-ai-prompt.node.test.js
T
GustavoandClaude Sonnet 5 482b7ef48e fix(v2): redefine AI actionability semantics + version the classifier (AI_CLASSIFIER_VERSION)
Production-readiness cycle. Classifier was excessively conservative (real
Run A: ACTIONABLE_NOW/UNASSIGNED_ACTIONABLE both 0) and made concrete
semantic mistakes on real tickets:
- #34419: technician "Rafael Brito" (internal) classified as WAITING_VENDOR
  waiting_for -- an internal technician is not a vendor.
- #37389: ticket had concrete open tasks ("Realizar um inventário das
  máquinas", "Alinhar o dia...") but was classified INSUFFICIENT_CONTEXT.
- Generic next_action ("Revisar chamado") repeated across unrelated
  tickets, proving nothing about real actionability.

Rewrote the system prompt (n8n/nodes/prepare-ai-prompt.node.js) with:
practical actionability definition ("suporte consegue fazer algo util
agora", explicit inclusion list); contatar-vs-aguardar distinction with
examples; WAITING_* requires positive evidence (action already taken AND
external dependency still open); technician-vs-vendor distinction citing
the TECNICO context field; STATUS Planejado alone != blocked/scheduled;
SCHEDULED requires an explicit date/window; mandatory chronological
interpretation (most recent apontamento wins over a superseded old
blocker); next_action must be concrete, with explicit accepted/rejected
examples; INSUFFICIENT_CONTEXT bar raised (ask "is there a diagnostic
step available even without full info" first).
n8n/nodes/prepare-ai-prompt.node.test.js: 10 new tests asserting each
rule's text is still present in the prompt (deterministic proxy --
behavioral proof comes from the real DEV run, inspected manually).

logic/classification.js: added AI_CLASSIFIER_VERSION=3 (stamped on every
valid classification); added a deterministic safety net (not just a
prompt hope) -- isWaitingForInternalTechnician() rejects WAITING_VENDOR
when waiting_for matches the ticket's own TECNICO field, routing to a new
fallback_reason 'ai_semantic_conflict' instead of accepting a known-wrong
semantic classification; added next_action genericness rejection
(isGenericNextAction) for ACTIONABLE_NOW/UNASSIGNED_ACTIONABLE only.
n8n/nodes/validate-ai-response.node.js: now passes the ticket into
validateAiClassification() so the technician guard has the facts it needs.

logic/cache.js: added buildCacheKey(contextHash, classifierVersion) ->
"<hash>:v<version>", stored in the same last_context_hash column (no
schema migration) -- bumping AI_CLASSIFIER_VERSION now forces a cache
miss on every ticket without manually clearing the Data Table.
n8n/nodes/decide-needs-ai.node.js / prepare-memoria-analise-record.node.js
updated to compute/persist the composite key.

logic/config.js: AI_MAX_CANDIDATES default raised 30->60 (now equals
CANDIDATE_POOL_SIZE) -- no candidate should go unanalyzed by an
artificial cap now that Run B already proved the cache makes repeat runs
cheap; AI_FALLBACK stays reserved for real provider failures. Also added
SEND_ENABLED (default false) and ENVIRONMENT config fields for the real
Slack send gate (used by logic/slack.js, next commit).

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

70 lines
3.0 KiB
JavaScript

const { test } = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
// O prompt em si nao pode ser testado deterministicamente sem chamar a IA de verdade -- estes
// testes garantem que as REGRAS exigidas pela missao "finalizacao para producao" continuam
// escritas no prompt (nao foram removidas/reescritas por acidente numa edicao futura). A prova
// comportamental real fica nas execucoes reais contra o GLPI (Run com AI_CLASSIFIER_VERSION
// nova), inspecionadas manualmente no relatorio.
function readPrompt() {
return fs.readFileSync(path.join(__dirname, 'prepare-ai-prompt.node.js'), 'utf8');
}
test('prompt define acionabilidade de forma pratica ("suporte consegue fazer algo util agora")', () => {
const src = readPrompt();
assert.ok(/suporte consegue fazer algo util agora/i.test(src));
});
test('prompt distingue contatar (ainda nao feito) de aguardar (ja feito) -- teste obrigatorio #2/#3', () => {
const src = readPrompt();
assert.ok(/AINDA NAO foi contatado/.test(src));
assert.ok(/ja foi contatado/i.test(src) || /Ja ter contatado/.test(src));
});
test('prompt exige evidencia dupla (acao ja executada + dependencia externa aberta) pra WAITING_*', () => {
const src = readPrompt();
assert.ok(/evidencia POSITIVA/.test(src));
assert.ok(/dependencia externa ainda aberta/.test(src));
});
test('prompt instrui que tecnico interno (campo TECNICO) nunca vira WAITING_VENDOR -- teste obrigatorio #1', () => {
const src = readPrompt();
assert.ok(/NUNCA classifique como WAITING_VENDOR/.test(src));
assert.ok(/tecnicos internos da equipe de suporte/.test(src));
});
test('prompt instrui que STATUS Planejado sozinho nao significa bloqueio -- teste obrigatorio #6', () => {
const src = readPrompt();
assert.ok(/Planejado sozinho NAO significa/.test(src));
});
test('prompt exige data/horario/janela explicita para SCHEDULED', () => {
const src = readPrompt();
assert.ok(/SCHEDULED apenas quando existir data, horario, janela/.test(src));
});
test('prompt exige interpretacao cronologica (apontamento mais recente prevalece) -- teste obrigatorio #8', () => {
const src = readPrompt();
assert.ok(/ordem temporal/.test(src));
assert.ok(/mais recente relevante prevalece/.test(src));
});
test('prompt rejeita next_action generico com exemplos explicitos', () => {
const src = readPrompt();
for (const frase of ['Revisar chamado', 'Dar andamento', 'Analisar situacao', 'Verificar chamado', 'Acompanhar']) {
assert.ok(src.includes(frase), `prompt deveria citar "${frase}" como exemplo generico rejeitado`);
}
});
test('prompt pede para considerar acao de diagnostico antes de usar INSUFFICIENT_CONTEXT -- teste obrigatorio #7', () => {
const src = readPrompt();
assert.ok(/Falta de diagnostico nao significa falta de acao/.test(src));
});
test('prompt continua exigindo resposta em JSON estrito com o dominio de classes correto', () => {
const src = readPrompt();
assert.ok(src.includes('VALID_CLASSIFICATIONS.join'));
});