feat(v2): add context fingerprint hash that ignores time-only fields

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Gustavo
2026-08-24 15:14:17 -03:00
co-authored by Claude Sonnet 5
parent 32ebe0d4f2
commit 30e532f811
2 changed files with 66 additions and 0 deletions
+28
View File
@@ -0,0 +1,28 @@
function stableStringify(value) {
if (value === null || typeof value !== 'object') return JSON.stringify(value);
if (Array.isArray(value)) return `[${value.map(stableStringify).join(',')}]`;
const keys = Object.keys(value).sort();
return `{${keys.map((k) => `${JSON.stringify(k)}:${stableStringify(value[k])}`).join(',')}}`;
}
function djb2Hash(str) {
let hash = 5381;
for (let i = 0; i < str.length; i += 1) {
hash = ((hash << 5) + hash + str.charCodeAt(i)) >>> 0;
}
return hash.toString(16).padStart(8, '0');
}
const FINGERPRINT_FIELDS = ['status', 'tecnico_atribuido', 'categoria', 'entidade'];
function buildContextFingerprint(ticket) {
const followups = (ticket.followups || []).map((f) => `${f.id}:${f.date}`).sort();
const tasks = (ticket.tasks || []).map((t) => `${t.id}:${t.status}:${t.date}`).sort();
const picked = {};
for (const field of FINGERPRINT_FIELDS) picked[field] = ticket[field];
picked.followups = followups;
picked.tasks = tasks;
return djb2Hash(stableStringify(picked));
}
module.exports = { stableStringify, djb2Hash, buildContextFingerprint };