Ran the deployed workflow for real (temporary webhook trigger, briefly
activated, immediately deactivated and removed after each attempt) against
the real GLPI MySQL database and a real OpenAI credential. Found and fixed:
1. $env access is blocked on this n8n instance (N8N_BLOCK_ENV_ACCESS_IN_NODE).
parseConfig($env) threw "access to env vars denied" on the very first
Code node. Config now comes from the "Config - Coordenador GLPI v2" Set
node instead (same pattern the V1 workflow already used) -- expanded
that node with all 13 tunable parameters, and pointed the 3 affected
node drivers (build-sql-candidatos, decide-needs-ai, select-top10) at
it instead of $env.
2. Data Table "upsert" operation requires both `columns` (values) and a
separate `filters` (match condition) -- I had only set columns with
matchingColumns, which isn't enough; n8n refused to activate the
workflow ("Missing or invalid required parameters: filters"). Added
filters.conditions matching on ticket_id to both memory-writing Data
Table nodes.
3. Real architecture bug: "Data Table - Buscar Memoria" sat inline in the
41-candidate chain (Mesclar Contexto -> Buscar Memoria -> Decidir
Necessidade de IA). A bulk "get all rows" Data Table node run with 41
input items produced 41 identical fetches and its OWN output (the empty
memory table on a first run) replaced the 41 candidates entirely --
"Decidir Necessidade de IA" received 0 items and the whole main branch
silently skipped. Moved the memory fetch to a parallel branch off
Config (fires once) and added a real Merge node ("Merge - Aguardar
Memoria") to guarantee both the candidates and the memory rows have
executed before "Decidir Necessidade de IA" runs -- same synchronization
pattern already used for context and for the shadow comparison.
Also fixed a cosmetic bug found in the real message output: buildTopMessage
produced a quadruple newline between the header and the first ticket block
([header, '', ...blocks].join('\n\n') double-counts the blank line already
inserted by the '\n\n' join). Added a regression test.
Verified with a real execution (id 13599, 24/08/2026 19:25 UTC) against the
live GLPI database: 41 real candidates, 30 sent to OpenAI (capped), 11
fallback-capped, memory table populated with 41 real rows including
genuine WAITING_VENDOR/WAITING_REQUESTER classifications with real
blocker_type/waiting_for fields extracted from real ticket task content,
and a real shadow_runs row comparing Legacy x V2 (4 tickets removed by V2:
2 for INSUFFICIENT_CONTEXT, 1 WAITING_REQUESTER, 1 WAITING_VENDOR; 4 added).
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
47 lines
1.8 KiB
JavaScript
47 lines
1.8 KiB
JavaScript
// @requires config.js cache.js
|
|
// Instancia bloqueia $env em Code nodes -- config vem do node "Config - Coordenador GLPI v2".
|
|
const config = parseConfig($('Config - Coordenador GLPI v2').first().json);
|
|
const now = Date.now();
|
|
|
|
// $input vem do Merge - Aguardar Memoria (so existe para GARANTIR que os candidatos e a
|
|
// memoria ja foram buscados antes deste node rodar); os dados de verdade sao lidos por nome.
|
|
const items = $('Code - Mesclar Contexto e Calcular Hash').all().map((item) => item.json); // { ticket, contextHash }
|
|
const memoryRows = $('Data Table - Buscar Memoria').all().map((item) => item.json);
|
|
const memoryByTicket = {};
|
|
for (const row of memoryRows) memoryByTicket[String(row.ticket_id)] = row;
|
|
|
|
const withDecision = items.map((item) => {
|
|
const memory = memoryByTicket[String(item.ticket.id)] || null;
|
|
const analysis = needsReanalysis({ memory, currentHash: item.contextHash, now });
|
|
return { ticket: item.ticket, contextHash: item.contextHash, memory, needsAiReason: analysis.reason, needsAiRaw: analysis.needsAi };
|
|
});
|
|
|
|
const needingAi = withDecision
|
|
.filter((item) => item.needsAiRaw)
|
|
.sort((a, b) => (Number(b.ticket.score_criticidade) || 0) - (Number(a.ticket.score_criticidade) || 0));
|
|
|
|
const allowedIds = new Set(needingAi.slice(0, config.aiMaxCandidates).map((item) => item.ticket.id));
|
|
|
|
return withDecision.map((item) => {
|
|
let resolucao;
|
|
if (!item.needsAiRaw) {
|
|
resolucao = 'cache';
|
|
} else if (allowedIds.has(item.ticket.id)) {
|
|
resolucao = 'ia';
|
|
} else {
|
|
resolucao = 'fallback_cap';
|
|
}
|
|
return {
|
|
json: {
|
|
ticket: item.ticket,
|
|
contextHash: item.contextHash,
|
|
memory: item.memory,
|
|
needsAiReason: item.needsAiReason,
|
|
resolucao,
|
|
precisa_ia: resolucao === 'ia',
|
|
config,
|
|
now,
|
|
},
|
|
};
|
|
});
|