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>
53 lines
2.4 KiB
JavaScript
53 lines
2.4 KiB
JavaScript
const { test } = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const { needsReanalysis, buildCacheKey } = require('./cache');
|
|
|
|
test('sem memoria previa, sempre precisa de IA', () => {
|
|
const result = needsReanalysis({ memory: null, currentHash: 'abc', now: 1000 });
|
|
assert.equal(result.needsAi, true);
|
|
assert.equal(result.reason, 'no_cache');
|
|
});
|
|
|
|
test('hash igual e sem next_review_at vencido usa cache', () => {
|
|
const memory = { last_context_hash: 'abc', next_review_at: null };
|
|
const result = needsReanalysis({ memory, currentHash: 'abc', now: 1000 });
|
|
assert.equal(result.needsAi, false);
|
|
assert.equal(result.reason, 'cache_valid');
|
|
});
|
|
|
|
test('hash diferente forca reavaliacao (mudanca significativa)', () => {
|
|
const memory = { last_context_hash: 'abc', next_review_at: null };
|
|
const result = needsReanalysis({ memory, currentHash: 'xyz', now: 1000 });
|
|
assert.equal(result.needsAi, true);
|
|
assert.equal(result.reason, 'context_changed');
|
|
});
|
|
|
|
test('next_review_at vencido forca reavaliacao mesmo com hash igual', () => {
|
|
const memory = { last_context_hash: 'abc', next_review_at: 500 };
|
|
const result = needsReanalysis({ memory, currentHash: 'abc', now: 1000 });
|
|
assert.equal(result.needsAi, true);
|
|
assert.equal(result.reason, 'review_due');
|
|
});
|
|
|
|
test('buildCacheKey combina hash de conteudo e versao do classificador', () => {
|
|
assert.equal(buildCacheKey('abc123', 3), 'abc123:v3');
|
|
assert.equal(buildCacheKey('abc123', 4), 'abc123:v4');
|
|
});
|
|
|
|
test('mudanca de AI_CLASSIFIER_VERSION invalida o cache mesmo com conteudo identico -- missao secao 12', () => {
|
|
const contentHash = 'abc123';
|
|
const oldCacheKey = buildCacheKey(contentHash, 2);
|
|
const newCacheKey = buildCacheKey(contentHash, 3);
|
|
const memory = { last_context_hash: oldCacheKey, next_review_at: null };
|
|
const result = needsReanalysis({ memory, currentHash: newCacheKey, now: 1000 });
|
|
assert.equal(result.needsAi, true, 'bump de versao deve forcar reclassificacao mesmo com o mesmo conteudo');
|
|
});
|
|
|
|
test('mesma versao + mesmo hash de conteudo usa cache normalmente -- missao secao 12', () => {
|
|
const cacheKey = buildCacheKey('abc123', 3);
|
|
const memory = { last_context_hash: cacheKey, next_review_at: null };
|
|
const result = needsReanalysis({ memory, currentHash: cacheKey, now: 1000 });
|
|
assert.equal(result.needsAi, false);
|
|
assert.equal(result.reason, 'cache_valid');
|
|
});
|