Files
SLACK_COORD_RELATORIO/logic/classification.js
T
GustavoandClaude Sonnet 5 00a42db269 fix(v2): AI_FALLBACK replaces the ACTIONABLE_NOW-with-confidence-0 lie, normalize null-like strings
Fallback (ai_disabled, ai_error, invalid_json, ai_max_candidates_cap --
any candidate the AI never actually analyzed) now produces classification
'AI_FALLBACK', an explicit technical state that is never part of the
11-class enum sent to OpenAI (the AI itself can never legitimately return
it). Previously it silently claimed ACTIONABLE_NOW with confidence 0,
which made unanalyzed tickets indistinguishable from genuinely confirmed
ones at the classification-label level.

Also normalizes blocker_type/waiting_for when the model returns the
literal string "null"/"none"/"n/a"/"" instead of JSON null -- done
once, in the validation layer (logic/classification.js), not scattered
around the workflow.

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

57 lines
2.4 KiB
JavaScript

const VALID_CLASSIFICATIONS = [
'ACTIONABLE_NOW', 'UNASSIGNED_ACTIONABLE', 'WAITING_REQUESTER', 'WAITING_VENDOR',
'WAITING_APPROVAL', 'SCHEDULED', 'DEPENDENCY_BLOCKED', 'NEEDS_COORDINATION',
'PROJECT_LONG_TERM', 'DUPLICATE_OR_OBSOLETE', 'INSUFFICIENT_CONTEXT',
];
// O modelo, apesar do schema permitir null de verdade em blocker_type/waiting_for, as vezes
// devolve a STRING "null" (ou variantes equivalentes) em vez do valor JSON null. Normaliza
// aqui, na camada de validacao -- unico lugar que toca a resposta crua da IA.
const NULL_LIKE_STRINGS = new Set(['null', 'none', 'n/a', 'na', '']);
function normalizeNullableString(value) {
if (value === null || value === undefined) return null;
const trimmed = String(value).trim();
if (NULL_LIKE_STRINGS.has(trimmed.toLowerCase())) return null;
return trimmed;
}
function validateAiClassification(raw) {
const errors = [];
if (!raw || typeof raw !== 'object') {
return { valid: false, data: null, errors: ['resposta nao e um objeto JSON'] };
}
if (!VALID_CLASSIFICATIONS.includes(raw.classification)) {
errors.push(`classification invalida: ${raw.classification}`);
}
if (typeof raw.actionable_today !== 'boolean') errors.push('actionable_today deve ser boolean');
const confidence = Number(raw.confidence);
if (!Number.isFinite(confidence) || confidence < 0 || confidence > 1) errors.push('confidence deve estar entre 0 e 1');
if (typeof raw.next_action !== 'string' || raw.next_action.length > 200) errors.push('next_action invalido');
if (raw.evidence != null && !Array.isArray(raw.evidence)) errors.push('evidence deve ser array');
if (typeof raw.reason !== 'string') errors.push('reason deve ser string');
if (errors.length) return { valid: false, data: null, errors };
return {
valid: true,
errors: [],
data: {
classification: raw.classification,
actionable_today: raw.actionable_today,
confidence,
blocker_type: normalizeNullableString(raw.blocker_type),
waiting_for: normalizeNullableString(raw.waiting_for),
next_action: raw.next_action,
suggested_action_type: raw.suggested_action_type ?? 'technical_action',
business_risk: raw.business_risk ?? 'medium',
reason: raw.reason,
evidence: Array.isArray(raw.evidence) ? raw.evidence.slice(0, 5) : [],
is_fallback: false,
fallback_reason: null,
},
};
}
module.exports = { VALID_CLASSIFICATIONS, validateAiClassification, normalizeNullableString };