{"record":{"id":"de3b55c28126c42f","repo":"Mintplex-Labs/anything-llm","slug":"could-not-find-exact-match-for-edit-n-edit-oldte","errorCode":null,"errorMessage":"Could not find exact match for edit:\\n${edit.oldText}","messagePattern":"Could not find exact match for edit:\\\\n(.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"server/utils/agents/aibitat/plugins/filesystem/lib.js","lineNumber":587,"sourceCode":"              const relativeIndent = newIndent.length - oldIndent.length;\n              return (\n                originalIndent +\n                \" \".repeat(Math.max(0, relativeIndent)) +\n                line.trimStart()\n              );\n            }\n            return line;\n          });\n\n          contentLines.splice(i, oldLines.length, ...newLines);\n          modifiedContent = contentLines.join(\"\\n\");\n          matchFound = true;\n          break;\n        }\n      }\n\n      if (!matchFound) {\n        throw new Error(\n          `Could not find exact match for edit:\\n${edit.oldText}`\n        );\n      }\n    }\n\n    const diffResult = this.#createUnifiedDiff(\n      content,\n      modifiedContent,\n      filePath\n    );\n\n    let numBackticks = 3;\n    while (diffResult.includes(\"`\".repeat(numBackticks))) {\n      numBackticks++;\n    }\n    const formattedDiff = `${\"`\".repeat(numBackticks)}diff\\n${diffResult}${\"`\".repeat(numBackticks)}\\n\\n`;\n\n    if (!dryRun) {","sourceCodeStart":569,"sourceCodeEnd":605,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/526360e320da9d1b36074be5ed64fe76e5bbfbbd/server/utils/agents/aibitat/plugins/filesystem/lib.js#L569-L605","documentation":"Thrown by applyFileEdits() after it tries two matching strategies for edit.oldText and finds neither: first an exact substring match on normalized content, then a whitespace-insensitive line-by-line match (comparing trimmed lines). If both fail, the edit cannot be applied and this error reports the unmatched oldText.","triggerScenarios":"edit.oldText does not appear in the file content - either the content changed since oldText was captured, the case differs, line endings differ in a way the normalization does not cover, or oldText was composed for a different file. The trim-based fallback only helps with indentation differences, not content differences.","commonSituations":"The file was modified between the read and the edit (stale snapshot); oldText was hand-written rather than copied from the file; Unicode/encoding differences; the edit targets the wrong file.","solutions":["Re-read the current file content and rebuild oldText from the actual current text.","Use a smaller, uniquely identifiable snippet of oldText that is certain to be present.","Confirm the edit targets the correct file path.","Normalize line endings and check for invisible/whitespace differences."],"exampleFix":"// before - oldText no longer matches (file changed)\nedit.oldText = \"function oldName() {\";\n// after - re-read the file and copy the exact current text\nedit.oldText = \"function currentName() {\";","handlingStrategy":"validation","validationCode":"// verify oldText is present in the current file before applying edits\nconst content = await fs.readFile(filePath, \"utf-8\");\nfor (const edit of edits) {\n  if (!content.includes(edit.oldText))\n    throw new Error(`oldText not found in ${filePath}; re-read and update the edit`);\n}","typeGuard":null,"tryCatchPattern":"try {\n  await filesystem.applyFileEdits(filePath, edits, dryRun);\n} catch (e) {\n  if (e.message.startsWith(\"Could not find exact match\")) {\n    // re-read file, rebuild edits from current content, retry\n  } else throw e;\n}","preventionTips":["Re-read the file immediately before constructing edit.oldText to avoid stale snapshots.","Copy oldText verbatim from the current file content rather than typing it.","Use the smallest unique snippet that unambiguously identifies the edit location."],"tags":["filesystem","edit","search","validation"],"backgroundTag":null,"analyzedSha":"526360e320da9d1b36074be5ed64fe76e5bbfbbd","analyzedAt":"2026-08-13T01:45:47.170Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}