{"record":{"id":"af31d1217894b035","repo":"CherryHQ/cherry-studio","slug":"found-multiple-matches-for-old-string-provide-mor","errorCode":null,"errorMessage":"Found multiple matches for old_string. Provide more surrounding lines in old_string to identify the correct match.","messagePattern":"Found multiple matches for old_string\\. Provide more surrounding lines in old_string to identify the correct match\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/main/ai/mcp/servers/filesystem/types.ts","lineNumber":547,"sourceCode":"\n  for (const replacer of ALL_REPLACERS) {\n    for (const search of replacer(content, oldString)) {\n      const index = content.indexOf(search)\n      if (index === -1) continue\n      notFound = false\n      if (replaceAll) {\n        return content.replaceAll(search, newString)\n      }\n      const lastIndex = content.lastIndexOf(search)\n      if (index !== lastIndex) continue\n      return content.substring(0, index) + newString + content.substring(index + search.length)\n    }\n  }\n\n  if (notFound) {\n    throw new Error('old_string not found in content')\n  }\n  throw new Error(\n    'Found multiple matches for old_string. Provide more surrounding lines in old_string to identify the correct match.'\n  )\n}\n\n// ============================================================================\n// Binary File Detection\n// ============================================================================\n\n// Check if a file is likely binary\nexport async function isBinaryFile(filePath: string): Promise<boolean> {\n  try {\n    const buffer = Buffer.alloc(4096)\n    const fd = await fs.open(filePath, 'r')\n    const { bytesRead } = await fd.read(buffer, 0, buffer.length, 0)\n    await fd.close()\n\n    if (bytesRead === 0) return false\n","sourceCodeStart":529,"sourceCodeEnd":565,"githubUrl":"https://github.com/CherryHQ/cherry-studio/blob/726446b54cd69ffe51a276638672f6d95ca0768c/src/main/ai/mcp/servers/filesystem/types.ts#L529-L565","documentation":"Thrown by replaceWithFuzzyMatch when a replacer yielded a search substring that was found, but content.indexOf(search) !== content.lastIndexOf(search) for every candidate, and replaceAll is false. The matched text occurs more than once in the file, so a single replacement would be ambiguous. notFound is false (a match exists), so this branch rather than the not-found branch fires.","triggerScenarios":"Editing a common snippet (e.g. a return statement, an import, a log line) that appears multiple times in the file without replaceAll=true and without enough surrounding context to disambiguate. The fuzzy replacers reduce whitespace/indentation differences but still surface the same substring at multiple indices.","commonSituations":"Boilerplate lines (closing braces, blank lines, identical log calls) repeated across the file; editing a symbol referenced in many places; old_string trimmed to the minimal unique-looking line that is actually not unique.","solutions":["Add more surrounding lines (above and below) to old_string until it is unique in the file.","If you intend to replace every occurrence, pass replaceAll=true.","Include unique anchors (function name, comment, distinctive nearby line) in old_string.","Count occurrences of old_string before editing to detect ambiguity early."],"exampleFix":"// before: 'return null;' appears 3 times\nreplaceWithFuzzyMatch(content, 'return null;', 'return undefined;') // throws: Found multiple matches\n\n// after: anchor with unique surrounding context\nreplaceWithFuzzyMatch(content, 'function getUser() {\\n  return null;\\n}', 'function getUser() {\\n  return undefined;\\n}')","handlingStrategy":"validation","validationCode":"function isUnique(content: string, oldString: string): boolean {\n  const first = content.indexOf(oldString)\n  return first !== -1 && first === content.lastIndexOf(oldString)\n}","typeGuard":null,"tryCatchPattern":"try {\n  replaceWithFuzzyMatch(content, oldString, newString)\n} catch (e) {\n  if (e instanceof Error && e.message.startsWith('Found multiple matches')) {\n    // expand old_string with surrounding lines until unique, or set replaceAll=true\n  } else throw e\n}","preventionTips":["Count occurrences of old_string before editing; if >1, add context or use replaceAll.","Include a unique anchor (function/comment name) in old_string.","Reserve replaceAll=true for intentional global replacements and verify the count first."],"tags":["filesystem","validation","mcp","edit-tool","fuzzy-match"],"backgroundTag":null,"analyzedSha":"726446b54cd69ffe51a276638672f6d95ca0768c","analyzedAt":"2026-08-12T17:30:37.448Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}