{"record":{"id":"141fb305783a86cf","repo":"can1357/oh-my-pi","slug":"found-searchresult-matchcount-matches-for-the-t","errorCode":null,"errorMessage":"Found ${searchResult.matchCount} matches for the text in ${path}.${strategyHint}${previewText}\\n\\nAdd more surrounding context or additional @@ anchors to make it unique.","messagePattern":"Found (.+?) matches for the text in (.+?)\\.(.+?)(.+?)\\\\n\\\\nAdd more surrounding context or additional @@ anchors to make it unique\\.","errorType":"validation","errorClass":"ApplyPatchError","httpStatus":null,"severity":"error","filePath":"packages/coding-agent/src/edit/modes/patch.ts","lineNumber":1306,"sourceCode":"\n\t\tif ((searchResult.matchCount ?? 0) > 1) {\n\t\t\tconst hintIndex = matchHint ?? (lineHint ? lineHint - 1 : undefined);\n\t\t\tconst hinted = chooseHintedMatch(searchResult.matchIndices, hintIndex, AMBIGUITY_HINT_WINDOW);\n\t\t\tif (hinted !== undefined) {\n\t\t\t\tsearchResult = { ...searchResult, index: hinted, matchCount: 1 };\n\t\t\t}\n\t\t}\n\n\t\tif (searchResult.index === undefined) {\n\t\t\tif (searchResult.matchCount !== undefined && searchResult.matchCount > 1) {\n\t\t\t\tconst previews = formatSequenceMatchPreviews(\n\t\t\t\t\toriginalLines,\n\t\t\t\t\tsearchResult.matchIndices,\n\t\t\t\t\tsearchResult.matchCount,\n\t\t\t\t);\n\t\t\t\tconst strategyHint = searchResult.strategy ? ` Matching strategy: ${searchResult.strategy}.` : \"\";\n\t\t\t\tconst previewText = previews ? `\\n\\n${previews}` : \"\";\n\t\t\t\tthrow new ApplyPatchError(\n\t\t\t\t\t`Found ${searchResult.matchCount} matches for the text in ${path}.${strategyHint}` +\n\t\t\t\t\t\t`${previewText}\\n\\nAdd more surrounding context or additional @@ anchors to make it unique.`,\n\t\t\t\t);\n\t\t\t}\n\t\t\tconst closest = findClosestSequenceMatch(originalLines, pattern, {\n\t\t\t\tstart: lineIndex,\n\t\t\t\teof: hunk.isEndOfFile,\n\t\t\t});\n\t\t\tif (closest.index !== undefined && closest.confidence > 0) {\n\t\t\t\tconst similarity = Math.round(closest.confidence * 100);\n\t\t\t\tconst preview = formatSequenceMatchPreview(originalLines, closest.index);\n\t\t\t\tthrow new ApplyPatchError(\n\t\t\t\t\t`Failed to find expected lines in ${path}:\\n${hunk.oldLines.join(\"\\n\")}\\n\\n` +\n\t\t\t\t\t\t`Closest match (${similarity}% similar) near line ${closest.index + 1}:\\n${preview}`,\n\t\t\t\t);\n\t\t\t}\n\t\t\tthrow new ApplyPatchError(`Failed to find expected lines in ${path}:\\n${hunk.oldLines.join(\"\\n\")}`);\n\t\t}","sourceCodeStart":1288,"sourceCodeEnd":1324,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/coding-agent/src/edit/modes/patch.ts#L1288-L1324","documentation":"The hunk's removed-lines block was searched in the file (findSequenceWithHint, possibly via fallback variants) and matched at more than one location, with no @@ anchor, hint, or disambiguation strategy able to pick one. The patcher refuses rather than guessing which occurrence to edit, reporting the match count, matching strategy, and previews of each match.","triggerScenarios":"A replace hunk whose oldLines (e.g. a short block like a single `}` group, a repeated log line, or duplicated helper) occurs multiple times in the file, with no line hint that falls inside the ambiguity hint window and no hierarchical context fallback.","commonSituations":"Editing one of several identical function signatures or repeated config blocks; changing `},` or a bare closing brace; identical repeated error-handling blocks; applying the same template patch to a file with copy-pasted sections.","solutions":["Include more unique surrounding lines in the hunk's removed-lines block so only one match exists.","Add a @@ changeContext anchor naming the enclosing function/class.","Provide a correct oldStartLine hint near the intended occurrence (chooseHintedMatch resolves within the hint window).","Split the file edit so each hunk targets a unique text region; rename duplicates if necessary."],"exampleFix":"// before\n-  }\n+  }\n\n// after (added unique context)\n-  async function load() {\n-    return cache\n-  }\n+  async function load() {\n+    return cache.refresh()\n+  }","handlingStrategy":"validation","validationCode":"const lines = (await Bun.file(path).text()).split('\\n');\nconst norm = (s: string) => s.trim();\nconst count = lines.filter(l => norm(l) === norm(hunk.oldLines[0])).length;\nif (count > 1) {\n  throw new Error(`Hunk text matches ${count} locations in ${path}; add unique context.`);\n}","typeGuard":null,"tryCatchPattern":"try {\n  applyPatch(patch);\n} catch (err) {\n  if (err instanceof ApplyPatchError && /Found \\d+ matches for the text/.test(err.message)) {\n    // use the previews in the message to extend context and retry\n  } else throw err;\n}","preventionTips":["Never patch generic fragments like bare braces or repeated log lines.","Include enclosing function/class lines as context in every hunk.","Count occurrences of the removed block before patching.","Provide a line hint when editing one of several identical regions."],"tags":["patch","ambiguity","duplicate-content"],"backgroundTag":"patch-ambiguous-match","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}