can1357/oh-my-pi · error · ApplyPatchError

Found ${result.matchCount} matches for context '${displayCon

Error message

Found ${result.matchCount} matches for context '${displayContext}' in ${path}.${strategyHint}${previewText}\n\nAdd more surrounding context or additional @@ anchors to make it unique.

What it means

The hunk had a @@ changeContext anchor, and findHierarchicalContext found that the anchor text matches more than one location in the file (matchCount > 1). Since the patcher cannot decide which occurrence to modify, it throws with the number of matches, the matching strategy, and previews of each match location. This is a deliberate ambiguity guard, not a lookup failure.

Source

Thrown at packages/coding-agent/src/edit/modes/patch.ts:1142

			if (idx === undefined || (result.matchCount !== undefined && result.matchCount > 1)) {
				const fallback = attemptSequenceFallback(
					originalLines,
					hunk,
					lineIndex,
					lineHint,
					allowFuzzy,
					allowAggressiveFallbacks,
				);
				if (fallback !== undefined) {
					lineIndex = fallback;
				} else if (result.matchCount !== undefined && result.matchCount > 1) {
					const displayContext = hunk.changeContext.includes("\n")
						? hunk.changeContext.split("\n").pop()
						: hunk.changeContext;
					const previews = formatSequenceMatchPreviews(originalLines, result.matchIndices, result.matchCount);
					const strategyHint = result.strategy ? ` Matching strategy: ${result.strategy}.` : "";
					const previewText = previews ? `\n\n${previews}` : "";
					throw new ApplyPatchError(
						`Found ${result.matchCount} matches for context '${displayContext}' in ${path}.${strategyHint}` +
							`${previewText}\n\nAdd more surrounding context or additional @@ anchors to make it unique.`,
					);
				} else {
					const displayContext = hunk.changeContext.includes("\n")
						? hunk.changeContext.split("\n").join(" > ")
						: hunk.changeContext;
					throw new ApplyPatchError(`Failed to find context '${displayContext}' in ${path}`);
				}
			} else {
				// If oldLines[0] matches the final context, start search at idx (not idx+1)
				// This handles the common case where @@ scope and first context line are identical
				const firstOldLine = hunk.oldLines[0];
				const finalContext = hunk.changeContext.includes("\n")
					? hunk.changeContext.split("\n").pop()?.trim()
					: hunk.changeContext.trim();
				const isHierarchicalContext =
					hunk.changeContext.includes("\n") || hunk.changeContext.trim().split(/\s+/).length > 2;

View on GitHub (pinned to 9690622007)

Solutions

  1. Make the @@ anchor more specific: use hierarchical anchors (`@@ ClassName\n@@ methodName`) or include more of the enclosing scope.
  2. Add more surrounding context lines to the hunk so the target block is unique.
  3. Add a correct oldStartLine hint — a hint near the intended occurrence resolves ambiguity within the hint window.
  4. Rename duplicated code first, or split the patch into multiple smaller patches each targeting a unique site.

Example fix

// before
@@ handler
-old handler body
+new handler body

// after
@@ RequestController
@@ onTimeout
-old handler body
+new handler body
Defensive patterns

Strategy: validation

Validate before calling

const content = await Bun.file(path).text();
const occurrences = content.split('\n').filter(l => l.includes(anchor)).length;
if (occurrences > 1) {
  throw new Error(`Anchor '${anchor}' is ambiguous (${occurrences} matches); use a hierarchical @@ anchor or line hint.`);
}

Try / catch

try {
  applyPatch(patch);
} catch (err) {
  if (err instanceof ApplyPatchError && /Found \d+ matches for context/.test(err.message)) {
    // inspect previews in the message, add disambiguating context, retry
  } else throw err;
}

Prevention

When it happens

Trigger: An @@ anchor such as `@@ functionName` or a context line matches multiple functions/occurrences in the file, and no line hint or sequence fallback can disambiguate (attemptSequenceFallback returned undefined).

Common situations: Editing a method whose name appears in several classes (overloads, interface + implementation); changing a line like `return null;` that appears many times; duplicated boilerplate blocks; anchor that is too short/generic (e.g. `@@ handler`).

Related errors


AI-assisted analysis of can1357/oh-my-pi@9690622007 (2026-08-31). Data as JSON: /api/errors/881c6f5bd2e16955. Report an issue: GitHub.