{"record":{"id":"335219644bbc14e5","repo":"plandex-ai/plandex","slug":"plan-replacement-failed-s","errorCode":null,"errorMessage":"plan replacement failed - %s","messagePattern":"plan replacement failed - (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"app/shared/plan_result_replacements.go","lineNumber":253,"sourceCode":"\t\t\t\t}\n\n\t\t\t\t// log.Println(\"Before replacements. updated:\")\n\t\t\t\t// log.Println(updated)\n\n\t\t\t\tupdated, allSucceeded = ApplyReplacements(maybeWithLineNums, replacements, false)\n\n\t\t\t\tupdated = string(RemoveLineNums(LineNumberedTextType(updated)))\n\n\t\t\t\tif !allSucceeded {\n\t\t\t\t\tlog.Println(\"Failed to apply replacements\")\n\n\t\t\t\t\t// log.Println(\"replacements:\")\n\t\t\t\t\t// log.Println(spew.Sdump(replacements))\n\n\t\t\t\t\t// log.Println(\"updated:\")\n\t\t\t\t\t// log.Println(updated)\n\n\t\t\t\t\treturn nil, fmt.Errorf(\"plan replacement failed - %s\", path)\n\t\t\t\t}\n\n\t\t\t\t// log.Println(\"Updated content: \")\n\t\t\t\t// log.Println(updated)\n\n\t\t\t\tupdatedAtByPath[path] = planRes.CreatedAt\n\t\t\t}\n\n\t\t\tif foundTarget {\n\t\t\t\tbreak PlanResLoop\n\t\t\t}\n\t\t}\n\n\t\t// log.Println(\"Setting updated content for path: \", path)\n\n\t\tfiles[path] = updated\n\t}\n","sourceCodeStart":235,"sourceCodeEnd":271,"githubUrl":"https://github.com/plandex-ai/plandex/blob/e2d772072efadbe41d2946d97d79be55532dbab5/app/shared/plan_result_replacements.go#L235-L271","documentation":"During the replacement phase, the library attempted to apply search/replace edits to the file content for a path and the update did not produce content — the replacement operation failed for that path. The library aborts with the offending path so the caller knows which file's edits could not be applied.","triggerScenarios":"Calling GetFiles when a plan result contains replacement sections that fail to match/apply against the current `updated` content for the path (e.g. search text not found, or replacements left the content empty/unchanged in an invalid way).","commonSituations":"LLM-generated search/replace blocks that don't exactly match the file (whitespace, stale line numbers); file content changed between context capture and replacement application; overlapping replacements in the same file.","solutions":["Regenerate the plan/replacements for the failing path with the current file content as context","Enable the commented debug logging (spew.Sdump of replacements and `updated`) to see why the replacement did not apply","Verify the file content on disk/context matches the Sha captured in planState (re-capture if the file drifted)","Normalize whitespace/line endings between search text and file content"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"func validateReplacementsApplicable(content string, replacements []shared.Replacement) error {\n    for _, r := range replacements {\n        if !strings.Contains(content, r.Search) {\n            return fmt.Errorf(\"search text not found: %q\", truncate(r.Search))\n        }\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":"files, err := GetFiles(results, state)\nif err != nil {\n    if strings.Contains(err.Error(), \"plan replacement failed\") {\n        path := extractPath(err.Error())\n        return fmt.Errorf(\"re-generate replacements for %s (content drifted or search text mismatch): %w\", path, err)\n    }\n    return err\n}","preventionTips":["Re-capture context/Sha if the file changed after context creation","Normalize whitespace/line endings between search text and file content","Avoid overlapping replacements within the same file"],"tags":["go","search-replace","plan-results","patching"],"backgroundTag":"patch-application-failed","analyzedSha":"e2d772072efadbe41d2946d97d79be55532dbab5","analyzedAt":"2026-09-05T20:56:53.631Z","contentChangedAt":"2026-09-05T20:56:53.631Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}