/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * * @format * @oncall memory_lab */ import {createHash} from 'crypto'; /** * Prose written about a finding (an explanation, a root cause, a fix's * rationale, a round write-up) leaves the host in a record, so it is scrubbed * first. What an agent quotes from a heap or a page is replaced by a short * hash: URLs, email addresses, quoted strings, long digit and hex runs (ids, * tokens) and identifiers carrying such a run. A hash is the same in every * session, so two notes that quote the same value still match. Class names, * edge names, code identifiers, counts and diff numbers stay readable. * * The plugin's `run_record.py` applies the same rules to round write-ups; * `note-scrub-vectors.json` holds the cases both must agree on. */ /** Characters kept per field. */ export const MAX_NOTE_CHARS = 8000; function shortHash(text: string): string { return createHash('sha256').update(text).digest('hex').slice(0, 12); } const RULES: ReadonlyArray<{kind: string; pattern: RegExp}> = [ { kind: 'url', pattern: /\b(?:https?|wss?|ftp|file|blob|data|mailto|javascript|chrome-extension):[^\s<>"'`)\]]+/gi, }, {kind: 'email', pattern: /[\w.%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}/g}, {kind: 'string', pattern: /"[^"\n]{1,500}"/g}, {kind: 'string', pattern: /“[^”\n]{1,500}”/g}, // A single-quoted span only between word boundaries, so an apostrophe // (the app's state) is left alone. { kind: 'string', pattern: /(? { replacements.push(`(${kind} ${shortHash(match)})`); return `\u0000${replacements.length - 1}\u0000`; }); } // eslint-disable-next-line no-control-regex -- the placeholder's delimiters out = out.replace(/\u0000(\d+)\u0000/g, (_, i: string) => replacements[+i]); const scrubbed = replacements.length; const truncated = out.length > maxChars; if (truncated) out = `${out.slice(0, maxChars - 1)}…`; return {text: out.trim(), scrubbed, truncated}; }