{"record":{"id":"79fd003d78b3e527","repo":"danny-avila/LibreChat","slug":"old-text-did-not-match-the-file-content","errorCode":null,"errorMessage":"old_text did not match the file content.","messagePattern":"old_text did not match the file content\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"warning","filePath":"packages/api/src/agents/handlers.ts","lineNumber":1103,"sourceCode":"  }\n  const whitespaceNormalized = findWhitespaceNormalizedMatch(content, needle);\n  if (whitespaceNormalized.status !== 'none') {\n    return whitespaceNormalized;\n  }\n  return findLineWindowMatch(content, needle, 'indentation-flexible');\n}\n\nfunction applyTextEdits(\n  content: string,\n  edits: TextEdit[],\n): { content: string; strategies: string[] } {\n  let working = content;\n  const strategies: string[] = [];\n\n  for (const edit of edits) {\n    const match = findReplacementMatch(working, edit.old_text);\n    if (match.status === 'none') {\n      throw new Error('old_text did not match the file content.');\n    }\n    if (match.status === 'ambiguous') {\n      throw new Error(\n        `old_text matched ${match.count} locations with ${match.strategy}; make it unique before retrying.`,\n      );\n    }\n    working =\n      working.slice(0, match.index) + edit.new_text + working.slice(match.index + match.length);\n    strategies.push(match.strategy);\n  }\n\n  return { content: working, strategies };\n}\n\nfunction formatRange(start: number, count: number): string {\n  return count === 1 ? String(start) : `${start},${count}`;\n}\n","sourceCodeStart":1085,"sourceCodeEnd":1121,"githubUrl":"https://github.com/danny-avila/LibreChat/blob/5ff282f9006c436e561de1afd39a481bea1ef0d8/packages/api/src/agents/handlers.ts#L1085-L1121","documentation":"Thrown by applyTextEdits when findReplacementMatch returns status 'none' for an edit's old_text — i.e. none of the four matching strategies (exact, line-trimmed, whitespace-normalized, indentation-flexible) could locate the text in the current file content. This is the file-authoring tool's 'old_text not found' failure, surfaced to the caller as a tool error via errorResult.","triggerScenarios":"old_text that was never in the file; old_text copied from a different version of the file than the one currently on disk; whitespace/indentation differences larger than the normalization tolerates; old_text pointing at the wrong file path.","commonSituations":"The file was edited concurrently between read and write; the model hallucinated content; line-ending (CRLF vs LF) or tab/space differences exceed normalization; the file was reloaded with different formatting (a linter ran).","solutions":["Re-read the current file content and copy old_text verbatim from it (include surrounding unique context lines).","Check for line-ending and tab/space mismatches; match the file's actual style.","Confirm the correct filePath and that no other process mutated the file since the last read.","Shorten old_text to a distinctive unique fragment if normalization is dropping it."],"exampleFix":"// before\n{ old_text: \"function run() {\\n  return 1\\r\\n}\", new_text: \"function run() {\\n  return 2\\n}\" } // CRLF mismatch\n\n// after\n// re-read file, copy exact bytes including its real line endings\n{ old_text: \"function run() {\\n  return 1\\n}\", new_text: \"function run() {\\n  return 2\\n}\" }","handlingStrategy":"try-catch","validationCode":"function findExactOccurrences(content: string, needle: string): number[] {\n  const out: number[] = [];\n  let from = 0;\n  while (true) {\n    const i = content.indexOf(needle, from);\n    if (i === -1) break;\n    out.push(i);\n    from = i + Math.max(1, needle.length);\n  }\n  return out;\n}\n// before calling applyTextEdits, verify old_text is present:\nfor (const e of edits) {\n  const hits = findExactOccurrences(currentContent, e.old_text);\n  if (hits.length === 0) throw new Error(`old_text not found: ${e.old_text.slice(0, 60)}...`);\n}","typeGuard":"function isOldTextPresent(content: string, oldText: string): boolean {\n  return content.includes(oldText);\n}","tryCatchPattern":"try {\n  const { content, strategies } = applyTextEdits(current.content, edits);\n} catch (e) {\n  if (e instanceof Error && /old_text did not match/.test(e.message)) {\n    // re-read the file, recompute old_text from current bytes, and retry once\n  } else throw e;\n}","preventionTips":["Always read the latest file content immediately before constructing edits; do not cache stale text.","Normalize line endings and indentation in old_text to match the file's actual style.","Include enough surrounding context in old_text that it is present and unique."],"tags":["text-editing","file-authoring","string-matching","tool-error"],"backgroundTag":null,"analyzedSha":"5ff282f9006c436e561de1afd39a481bea1ef0d8","analyzedAt":"2026-08-12T21:38:08.145Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}