/** * 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 type {McpServer} from '@modelcontextprotocol/sdk/server/mcp.js'; import fs from 'fs'; import path from 'path'; import {z} from 'zod'; import {scrubNote} from '../note-scrub.js'; import {noteRoundPath} from '../read-scope.js'; import {noteRecord, recordFiles} from '../session-record.js'; import {errorResult, toolResult} from '../utils.js'; const NOTE_FIELDS = [ 'explanation', 'root_cause', 'suggested_fix', 'fix_rationale', ] as const; /** The fix's diff number. Nothing else, since it is stored as given. */ const FIX_REF = /^D\d{1,12}$/; export function registerFindingNote(server: McpServer): void { server.tool( 'memlab_finding_note', "Record what you concluded about one finding so it reaches the leak-hunt runs app with the round's numbers: `explanation` (what leaks and through which retainer), `root_cause`, `suggested_fix`, and once a fix exists `fix_diff` with its `fix_rationale`. Call it once per finding when you have written it up, and again with `fix_diff` and `fix_rationale` once the fix exists: a later call for the same `fingerprint` updates only the fields it gives. The text is scrubbed before it leaves the host: URLs, email addresses, quoted strings and long ids become short hashes, so write class names, edge names and counts, never page content.", { run_dir: z .string() .min(1) .describe( "The round's directory (the one holding `run.json` and the snapshot ladder), so the note joins that round's run.", ), fingerprint: z .string() .min(1) .max(256) .describe( 'The finding it is about: the fingerprint `memlab_finding_index` matched, or `class:type` for a new one.', ), explanation: z .string() .max(20000) .optional() .describe('What leaks, through which retainer path, and how fast.'), root_cause: z .string() .max(20000) .optional() .describe('The code that keeps it alive and why it never lets go.'), suggested_fix: z .string() .max(20000) .optional() .describe('The change that would release it.'), fix_diff: z .string() .max(64) .optional() .describe('The fix, once there is one: a diff number such as D123.'), fix_rationale: z .string() .max(20000) .optional() .describe('Why that fix releases the finding.'), }, async params => { try { const present = NOTE_FIELDS.filter(field => params[field]?.trim()); if (present.length === 0) { return errorResult( 'Nothing to record: give at least one of `explanation`, `root_cause`, `suggested_fix` or `fix_rationale`.', ); } const runDir = path.resolve(params.run_dir); if (!fs.statSync(runDir, {throwIfNoEntry: false})?.isDirectory()) { return errorResult( `\`run_dir\` ${runDir} is not a directory: give the round's directory.`, ); } if (params.fix_diff != null && !FIX_REF.test(params.fix_diff)) { return errorResult( "`fix_diff` must be the fix's diff number, such as D123456789.", ); } if (params.fix_rationale != null && params.fix_diff == null) { return errorResult( '`fix_rationale` explains a fix: give the fix as `fix_diff` too.', ); } const data: Record = { fingerprint: scrubNote(params.fingerprint, 256).text, }; let scrubbed = 0; let truncated: string[] = []; for (const field of present) { const note = scrubNote(params[field] ?? ''); data[field] = note.text; scrubbed += note.scrubbed; if (note.truncated) truncated = [...truncated, field]; } if (params.fix_diff != null) data.fix_diff = params.fix_diff; data.scrubbed = scrubbed; if (recordFiles().length === 0) { return toolResult( 'Recording is off on this host (no records file and no uploader), so the note was not recorded. Nothing was sent.', ); } noteRoundPath(path.join(runDir, 'run.json')); noteRecord('memlab_finding_note', 'finding_note', data); const lines = [ `Recorded the note on \`${data.fingerprint as string}\` (${present.join(', ')}). It is uploaded with the round and appears on the finding in the runs app.`, ]; if (scrubbed > 0) { lines.push( `${scrubbed} quoted value(s) were replaced by short hashes before recording.`, ); } if (truncated.length > 0) { lines.push(`Cut to 8,000 characters: ${truncated.join(', ')}.`); } return toolResult(lines.join('\n')); } catch (err) { return errorResult(err); } }, ); }