can1357/oh-my-pi · error · ApplyPatchError
Found ${searchResult.matchCount} matches for the text in ${p
Error message
Found ${searchResult.matchCount} matches for the text in ${path}.${strategyHint}${previewText}\n\nAdd more surrounding context or additional @@ anchors to make it unique. What it means
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.
Source
Thrown at packages/coding-agent/src/edit/modes/patch.ts:1306
if ((searchResult.matchCount ?? 0) > 1) {
const hintIndex = matchHint ?? (lineHint ? lineHint - 1 : undefined);
const hinted = chooseHintedMatch(searchResult.matchIndices, hintIndex, AMBIGUITY_HINT_WINDOW);
if (hinted !== undefined) {
searchResult = { ...searchResult, index: hinted, matchCount: 1 };
}
}
if (searchResult.index === undefined) {
if (searchResult.matchCount !== undefined && searchResult.matchCount > 1) {
const previews = formatSequenceMatchPreviews(
originalLines,
searchResult.matchIndices,
searchResult.matchCount,
);
const strategyHint = searchResult.strategy ? ` Matching strategy: ${searchResult.strategy}.` : "";
const previewText = previews ? `\n\n${previews}` : "";
throw new ApplyPatchError(
`Found ${searchResult.matchCount} matches for the text in ${path}.${strategyHint}` +
`${previewText}\n\nAdd more surrounding context or additional @@ anchors to make it unique.`,
);
}
const closest = findClosestSequenceMatch(originalLines, pattern, {
start: lineIndex,
eof: hunk.isEndOfFile,
});
if (closest.index !== undefined && closest.confidence > 0) {
const similarity = Math.round(closest.confidence * 100);
const preview = formatSequenceMatchPreview(originalLines, closest.index);
throw new ApplyPatchError(
`Failed to find expected lines in ${path}:\n${hunk.oldLines.join("\n")}\n\n` +
`Closest match (${similarity}% similar) near line ${closest.index + 1}:\n${preview}`,
);
}
throw new ApplyPatchError(`Failed to find expected lines in ${path}:\n${hunk.oldLines.join("\n")}`);
}View on GitHub (pinned to 9690622007)
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.
Example fix
// before
- }
+ }
// after (added unique context)
- async function load() {
- return cache
- }
+ async function load() {
+ return cache.refresh()
+ } Defensive patterns
Strategy: validation
Validate before calling
const lines = (await Bun.file(path).text()).split('\n');
const norm = (s: string) => s.trim();
const count = lines.filter(l => norm(l) === norm(hunk.oldLines[0])).length;
if (count > 1) {
throw new Error(`Hunk text matches ${count} locations in ${path}; add unique context.`);
} Try / catch
try {
applyPatch(patch);
} catch (err) {
if (err instanceof ApplyPatchError && /Found \d+ matches for the text/.test(err.message)) {
// use the previews in the message to extend context and retry
} else throw err;
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Found ${result.matchCount} matches for context '${displayCon
- patch does not apply: {message}
- No files were modified.
- The first line of the patch must be '*** Begin Patch'
- The last line of the patch must be '*** End Patch'
AI-assisted analysis of can1357/oh-my-pi@9690622007 (2026-08-31).
Data as JSON: /api/errors/141fb305783a86cf.
Report an issue: GitHub.