{"record":{"id":"bb8624b272f5bdd5","repo":"can1357/oh-my-pi","slug":"failed-to-find-expected-lines-in-path-n-hunk","errorCode":null,"errorMessage":"Failed to find expected lines in ${path}:\\n${hunk.oldLines.join(\"\\n\")}\\n\\nClosest match (${similarity}% similar) near line ${closest.index + 1}:\\n${preview}","messagePattern":"Failed to find expected lines in (.+?):\\\\n(.+?)\\\\n\\\\nClosest match \\((.+?)% similar\\) near line (.+?):\\\\n(.+?)","errorType":"validation","errorClass":"ApplyPatchError","httpStatus":null,"severity":"error","filePath":"packages/coding-agent/src/edit/modes/patch.ts","lineNumber":1318,"sourceCode":"\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}\n\n\t\tconst found = searchResult.index;\n\n\t\tif (searchResult.strategy === \"fuzzy-dominant\") {\n\t\t\tconst similarity = Math.round(searchResult.confidence * 100);\n\t\t\twarnings.push(`Dominant fuzzy match selected in ${path} near line ${found + 1} (${similarity}% similar).`);\n\t\t} else if (\n\t\t\tsearchResult.strategy === \"comment-prefix\" ||\n\t\t\tsearchResult.strategy === \"prefix\" ||\n\t\t\tsearchResult.strategy === \"substring\" ||\n\t\t\tsearchResult.strategy === \"fuzzy\" ||\n\t\t\tsearchResult.strategy === \"character\"","sourceCodeStart":1300,"sourceCodeEnd":1336,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/coding-agent/src/edit/modes/patch.ts#L1300-L1336","documentation":"The hunk's expected (removed) lines could not be found in the file, but findClosestSequenceMatch located the most similar region with nonzero confidence. The error includes the requested block, the similarity percentage, and a preview of the closest real location so the caller can see how the file differs. It is the informative variant of the plain 'Failed to find expected lines' error.","triggerScenarios":"A replace hunk whose oldLines differ from the actual file content (different formatting, indentation, renamed identifiers, changed lines) such that exact, prefix, substring, and fuzzy strategies all fail, yet a near match exists.","commonSituations":"Patch written from an outdated read of the file; the model paraphrased code instead of copying it; tabs-vs-spaces or quote-style differences; the target lines were already modified by a previous edit in the same session.","solutions":["Open the file near the reported closest-match line and copy the exact current text into the hunk's removed lines.","Regenerate the patch from a fresh read of the file.","Reduce the matched block to a smaller, definitely-unchanged fragment and patch around it.","Check whitespace/indentation conventions (tabs vs spaces) and match them exactly."],"exampleFix":"// before (hunk assumed old code)\n-  const x = compute(a,b)\n\n// after (matches file: spacing/renames corrected)\n-  const result = compute(a, b);","handlingStrategy":"try-catch","validationCode":"const content = await Bun.file(path).text();\nif (!content.includes(hunk.oldLines.join('\\n'))) {\n  // stale or paraphrased hunk: re-read the file and rebuild oldLines verbatim\n}","typeGuard":null,"tryCatchPattern":"try {\n  applyPatch(patch);\n} catch (err) {\n  if (err instanceof ApplyPatchError && err.message.startsWith('Failed to find expected lines')) {\n    // parse 'Closest match ... near line N' from the message,\n    // re-read around line N, and rebuild the hunk with exact text\n  } else throw err;\n}","preventionTips":["Always generate patches from the most recent file read.","Copy removed lines verbatim — never paraphrase or reformat.","Apply patches promptly before other edits change the target region.","Use the closest-match preview in the error to locate and correct the hunk."],"tags":["patch","fuzzy-match","stale-content","no-match"],"backgroundTag":"patch-expected-lines-not-found","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}