Files
errors.bitreport.pt/ingest-api/src/lib/fingerprintPolicy.ts
T
David SantosandCursor 8773f12b25
CI / Validate Docker build (pull_request) Failing after 12m46s
CI / Lint and typecheck admin-ui (pull_request) Failing after 13m16s
CI / Test api (pull_request) Failing after 13m20s
CI / Test ingest-api (pull_request) Successful in 15m35s
feat(hub): retire WDMI projects from error reporting (BIT-24704)
Block ingest for retired WDMI slugs, purge project data via migration,
remove WDMI runbooks, and add offboard-project helper for future retirements.

Co-authored-by: Cursor <[email protected]>
2026-06-08 14:26:06 +00:00

110 lines
3.9 KiB
TypeScript

import { sha256Hex } from "./hash.js";
/** Bumped when fingerprint basis algorithm changes (dedup / parity). */
export const FINGERPRINT_POLICY_VERSION = 2;
/**
* Normalize message text for stable dedup (CRM-style: trim, collapse whitespace, NFKC).
*/
export function normalizeMessageForFingerprint(message: string): string {
const _line = message.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
const _oneLine = _line.split(/\n/)[0] ?? _line;
try {
return _oneLine.normalize("NFKC").trim().replace(/\s+/g, " ");
} catch {
return _oneLine.trim().replace(/\s+/g, " ");
}
}
function _asTrimmedString(value: unknown): string {
return typeof value === "string" ? value.trim() : "";
}
/**
* Pull file + line from ingest `context` (CRM ErrorCollector shapes).
*/
export function extractLocationFromContext(
context: Record<string, unknown> | null | undefined,
): { filePath: string; lineNumber: number } {
if (!context || typeof context !== "object") {
return { filePath: "", lineNumber: 0 };
}
const _directFile = _asTrimmedString(context.file_path) || _asTrimmedString(context.file);
if (_directFile !== "") {
const _ln = context.line_number ?? context.line ?? context.lineno;
return { filePath: _directFile, lineNumber: _parseLine(_ln) };
}
const _si = context.stack_info;
if (_si !== null && typeof _si === "object" && !Array.isArray(_si)) {
const _o = _si as Record<string, unknown>;
const _fn = _asTrimmedString(_o.filename) || _asTrimmedString(_o.file);
if (_fn !== "") {
const _ln = _o.line_number ?? _o.line ?? _o.lineno;
return { filePath: _fn, lineNumber: _parseLine(_ln) };
}
}
return { filePath: "", lineNumber: 0 };
}
function _parseLine(value: unknown): number {
if (typeof value === "number" && Number.isFinite(value)) {
return Math.trunc(value);
}
if (typeof value === "string" && value.trim() !== "") {
const _n = parseInt(value, 10);
return Number.isFinite(_n) ? _n : 0;
}
return 0;
}
/**
* Stable product key: `project_slug` (hub / Paperclip) or `project_name` fallback.
*/
export function extractProjectStableKey(context: Record<string, unknown> | null | undefined): string {
if (!context || typeof context !== "object") {
return "";
}
const _slug = _asTrimmedString(context.project_slug);
if (_slug !== "") {
return _slug;
}
return _asTrimmedString(context.project_name) || _asTrimmedString(context.project);
}
/** Any 8-4-4-4-12 hex segment shape (not strict RFC variant; good enough for noise stripping). */
const _UUID_RE = /\b[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\b/gi;
/**
* Normalize stack text so identical logical stacks hash the same across machines
* (addresses, UUIDs, temp paths damped). Used only for server-side fingerprints.
*/
export function normalizeStacktraceForFingerprint(stack: string): string {
const _lines = stack.replace(/\r\n/g, "\n").replace(/\r/g, "\n").split("\n");
const _maxLines = 28;
const _trimmed = _lines.slice(0, _maxLines).map((ln) => ln.trim());
let _joined = _trimmed.join("\n");
_joined = _joined.replace(/0x[0-9a-f]{4,}/gi, "0x_");
_joined = _joined.replace(/\b0x[0-9a-f]+\b/gi, "0x_");
_joined = _joined.replace(_UUID_RE, "<uuid>");
_joined = _joined.replace(/:\d{1,5}\b/g, ":_");
return _joined;
}
/**
* Hub fingerprint when the client does not send one: CRM capture parity
* (`project|message|file|line|stackDigest`) then SHA-256 hex (64 chars).
* `stackDigest` is sha256 of {@link normalizeStacktraceForFingerprint} or empty
* when no stack was sent.
*/
export function computeFingerprintFromBasis(input: {
projectStableKey: string;
normalizedMessage: string;
filePath: string;
lineNumber: number;
stackDigest: string;
}): string {
const _sd = input.stackDigest.trim() === "" ? "-" : input.stackDigest.trim();
const _basis = `${input.projectStableKey}|${input.normalizedMessage}|${input.filePath}|${input.lineNumber}|${_sd}`;
return sha256Hex(_basis);
}