{"record":{"id":"e558f4977d635138","repo":"can1357/oh-my-pi","slug":"failed-to-find-context-displaycontext-in-pa","errorCode":null,"errorMessage":"Failed to find context '${displayContext}' in ${path}","messagePattern":"Failed to find context '(.+?)' in (.+?)","errorType":"validation","errorClass":"ApplyPatchError","httpStatus":null,"severity":"error","filePath":"packages/coding-agent/src/edit/modes/patch.ts","lineNumber":1150,"sourceCode":"\t\t\t\t);\n\t\t\t\tif (fallback !== undefined) {\n\t\t\t\t\tlineIndex = fallback;\n\t\t\t\t} else if (result.matchCount !== undefined && result.matchCount > 1) {\n\t\t\t\t\tconst displayContext = hunk.changeContext.includes(\"\\n\")\n\t\t\t\t\t\t? hunk.changeContext.split(\"\\n\").pop()\n\t\t\t\t\t\t: hunk.changeContext;\n\t\t\t\t\tconst previews = formatSequenceMatchPreviews(originalLines, result.matchIndices, result.matchCount);\n\t\t\t\t\tconst strategyHint = result.strategy ? ` Matching strategy: ${result.strategy}.` : \"\";\n\t\t\t\t\tconst previewText = previews ? `\\n\\n${previews}` : \"\";\n\t\t\t\t\tthrow new ApplyPatchError(\n\t\t\t\t\t\t`Found ${result.matchCount} matches for context '${displayContext}' in ${path}.${strategyHint}` +\n\t\t\t\t\t\t\t`${previewText}\\n\\nAdd more surrounding context or additional @@ anchors to make it unique.`,\n\t\t\t\t\t);\n\t\t\t\t} else {\n\t\t\t\t\tconst displayContext = hunk.changeContext.includes(\"\\n\")\n\t\t\t\t\t\t? hunk.changeContext.split(\"\\n\").join(\" > \")\n\t\t\t\t\t\t: hunk.changeContext;\n\t\t\t\t\tthrow new ApplyPatchError(`Failed to find context '${displayContext}' in ${path}`);\n\t\t\t\t}\n\t\t\t} else {\n\t\t\t\t// If oldLines[0] matches the final context, start search at idx (not idx+1)\n\t\t\t\t// This handles the common case where @@ scope and first context line are identical\n\t\t\t\tconst firstOldLine = hunk.oldLines[0];\n\t\t\t\tconst finalContext = hunk.changeContext.includes(\"\\n\")\n\t\t\t\t\t? hunk.changeContext.split(\"\\n\").pop()?.trim()\n\t\t\t\t\t: hunk.changeContext.trim();\n\t\t\t\tconst isHierarchicalContext =\n\t\t\t\t\thunk.changeContext.includes(\"\\n\") || hunk.changeContext.trim().split(/\\s+/).length > 2;\n\t\t\t\tif (firstOldLine !== undefined && (firstOldLine.trim() === finalContext || isHierarchicalContext)) {\n\t\t\t\t\tlineIndex = idx;\n\t\t\t\t} else {\n\t\t\t\t\tlineIndex = idx + 1;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n","sourceCodeStart":1132,"sourceCodeEnd":1168,"githubUrl":"https://github.com/can1357/oh-my-pi/blob/969062200754ea02cfac922e5ebb8c608c079e15/packages/coding-agent/src/edit/modes/patch.ts#L1132-L1168","documentation":"The hunk declared a @@ changeContext anchor but findHierarchicalContext could not find that anchor text anywhere in the file after the current search position (index undefined, matchCount not > 1), and no fallback strategy succeeded. The anchor is expected to exist in the target file — its absence means the patch does not match the file's current content.","triggerScenarios":"Applying a patch whose `@@ SomeScope` anchor (possibly hierarchical, newline- or space-separated) does not literally appear in the file; the anchor was typed from memory or from an older file revision.","commonSituations":"File was refactored/renamed since the patch was authored; anchor spelled with wrong casing or extra whitespace; patch written against a different branch; the class/function the anchor names was moved to another file.","solutions":["Re-read the current file and update the @@ anchor to text that actually exists.","Remove the @@ anchor and locate the change purely via context lines in the hunk.","Check for casing/whitespace differences between the anchor and the file (matching is normalization-tolerant but not unlimited).","Regenerate the patch against the current file revision (fresh git diff)."],"exampleFix":"// before\n@@ UserServcie\n-importToken()\n+importTokenNew()\n\n// after (anchor fixed to real class name)\n@@ UserService\n-importToken()\n+importTokenNew()","handlingStrategy":"validation","validationCode":"const content = await Bun.file(path).text();\nconst anchorLine = anchor.split('\\n').pop()!.trim();\nif (!content.split('\\n').some(l => l.trim() === anchorLine)) {\n  throw new Error(`@@ anchor '${anchorLine}' does not exist in ${path}; refresh the patch.`);\n}","typeGuard":null,"tryCatchPattern":"try {\n  applyPatch(patch);\n} catch (err) {\n  if (err instanceof ApplyPatchError && err.message.startsWith('Failed to find context')) {\n    // re-read the file and regenerate or drop the anchor\n  } else throw err;\n}","preventionTips":["Derive anchors from a fresh file read, never from memory.","Strip the anchor text exactly as it appears (casing, whitespace).","After refactors/renames, regenerate patches before applying.","Fall back to plain context lines when no stable anchor exists."],"tags":["patch","context-matching","stale-content"],"backgroundTag":"patch-context-not-found","analyzedSha":"969062200754ea02cfac922e5ebb8c608c079e15","analyzedAt":"2026-08-31T10:29:35.737Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}