Files
errors.bitreport.pt/ingest-api/src/lib/noisePolicy.ts
T
Peter Spark b082bc716a ingest: filter BIT-SMOKE- when marker appears in stack_trace only
CRM PHP smoke (BIT-SMOKE-CRM-MAIN-DIRECT) can send the reserved token in
stack_trace while message stays generic, so the prior message-only noise
check missed it. Pass client stack text into evaluateNoise and bump
noise_ruleset_version to 1.0.3.

Paperclip-Agent: 0b1fcf92-5fe3-4ac0-9f29-df4ab72f5376
Paperclip-Issue: BIT-957
Paperclip-Run: d61f6d49-e43c-4ff6-a694-cbc8edff0d33
Made-with: Cursor
2026-04-05 07:22:23 +00:00

173 lines
5.3 KiB
TypeScript

import { normalizeLevelToSeverity } from "./normalize.js";
/**
* Versioned ruleset id (document in release notes when rules change).
* Rule inventory and validation steps: `docs/operations/ingest-hardening-runbook.md`.
*/
export const NOISE_RULESET_VERSION = "1.0.3";
export type NoiseEvaluation =
| { matched: false }
| { matched: true; ruleId: string; reason: string };
type NoiseRule = {
id: string;
reason: string;
match: (input: {
message: string;
messageLower: string;
fileHint: string;
fileHintLower: string;
urlHints: string[];
}) => boolean;
};
function _collectUrlHints(context: Record<string, unknown> | undefined): string[] {
const _out: string[] = [];
if (!context || typeof context !== "object") {
return _out;
}
const _push = (s: unknown) => {
if (typeof s === "string" && s.trim() !== "") {
_out.push(s);
}
};
_push(context.url);
const _bi = context.browser_info;
if (_bi && typeof _bi === "object" && !Array.isArray(_bi)) {
_push(((_bi as Record<string, unknown>).url as string) ?? "");
}
const _pi = context.page_info;
if (_pi && typeof _pi === "object" && !Array.isArray(_pi)) {
_push(((_pi as Record<string, unknown>).page_url as string) ?? "");
}
return _out;
}
/** CRM ErrorMonitoringController::isNoiseExternalResource */
function _matchExternalResourceCloudflareBeacon(input: {
message: string;
messageLower: string;
fileHint: string;
fileHintLower: string;
urlHints: string[];
}): boolean {
if (!input.messageLower.includes("resource failed to load")) {
return false;
}
const _candidates = [input.fileHintLower, ...input.urlHints.map((u) => u.toLowerCase())].filter(
(s) => s.length > 0,
);
for (const _u of _candidates) {
if (_u.includes("cloudflare") || _u.includes("beacon")) {
return true;
}
}
return false;
}
const _RULES: NoiseRule[] = [
{
id: "external_resource_cloudflare_beacon",
reason: "filtered_external_resource_noise",
match: (input) => _matchExternalResourceCloudflareBeacon(input),
},
{
id: "resize_observer_loop",
reason: "filtered_browser_noise",
match: (i) =>
i.message.includes("ResizeObserver loop completed with undelivered notifications"),
},
{
id: "wdmi_media_bitreport_load",
reason: "filtered_media_404_noise",
match: (i) =>
i.message.startsWith("Resource failed to load: https://wdmi-media.bitreport.pt/media/"),
},
{
id: "transient_offset_null",
reason: "filtered_transient_frontend_noise",
match: (i) =>
i.message.includes("Uncaught TypeError: Cannot read properties of null (reading 'offset')"),
},
{
id: "transient_after_undefined",
reason: "filtered_transient_frontend_noise",
match: (i) =>
i.message.includes(
"Uncaught TypeError: Cannot read properties of undefined (reading 'after')",
),
},
{
id: "stale_bundle_switch_selection_tab",
reason: "filtered_stale_frontend_bundle_noise",
match: (i) =>
i.message.includes("Uncaught ReferenceError: switchSelectionEditorTab is not defined"),
},
{
id: "stale_bundle_description_editor_ready",
reason: "filtered_stale_frontend_bundle_noise",
match: (i) =>
i.message.includes("Uncaught ReferenceError: descriptionEditorReady is not defined"),
},
{
id: "transient_parser_promise",
reason: "filtered_transient_frontend_noise",
match: (i) => i.message.includes("Unhandled Promise Rejection: Error: parser Error"),
},
/** Alpine.js v3 internal during teardown / navigation when scope is already gone (crm-wdmi / wdmi-store). */
{
id: "alpine_refresh_x_for_scope_teardown",
reason: "filtered_transient_frontend_noise",
match: (i) =>
i.message.includes("_x_refreshXForScope") ||
i.message.includes("reading '_x_refreshXForScope'"),
},
];
/**
* Conservative: never classify fatal/critical ingest levels as noise (avoid hiding real outages).
* Exception: reserved `BIT-SMOKE-` markers are intentional end-to-end probes (e.g. wdmi-store
* `/smoke/wdmi.js`, CRM `/smoke/crm-main.php`) and are filtered at every ingest level so they do
* not open incidents. Some PHP reporters put the marker only in `stack_trace`, not in `message`.
*/
export function evaluateNoise(input: {
level: string;
message: string;
/** Client `stacktrace` / `stack_trace` when present — used only for BIT-SMOKE- detection. */
stackText?: string;
context?: Record<string, unknown>;
filePathHint: string;
}): NoiseEvaluation {
const _msg = input.message.trim();
const _stack = (input.stackText ?? "").trim();
if (_msg.includes("BIT-SMOKE-") || _stack.includes("BIT-SMOKE-")) {
return {
matched: true,
ruleId: "bitreport_smoke_marker",
reason: "filtered_intentional_smoke_event",
};
}
const _sev = normalizeLevelToSeverity(input.level);
if (_sev === "critical") {
return { matched: false };
}
const _msgLower = _msg.toLowerCase();
const _file = input.filePathHint;
const _fileLower = _file.toLowerCase();
const _urlHints = _collectUrlHints(input.context);
const _env = {
message: _msg,
messageLower: _msgLower,
fileHint: _file,
fileHintLower: _fileLower,
urlHints: _urlHints,
};
for (const _r of _RULES) {
if (_r.match(_env)) {
return { matched: true, ruleId: _r.id, reason: _r.reason };
}
}
return { matched: false };
}