{"record":{"id":"ccc4a4d095e9bdb4","repo":"JuliusBrussee/caveman","slug":"caveman-code-old-string-not-found-in-input-path","errorCode":null,"errorMessage":"caveman-code: old_string not found in ${input.path}","messagePattern":"caveman-code: old_string not found in (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/agent/src/code.ts","lineNumber":349,"sourceCode":"      \"once unless replace_all is set. Writes to disk.\",\n    input: schema.object({\n      path: schema.string(),\n      old_string: schema.string(),\n      new_string: schema.string(),\n      replace_all: schema.optional(schema.boolean()),\n    }),\n    effect: \"write\",\n    result: \"inline\",\n    timeoutMs: READ_TIMEOUT_MS,\n    async execute(input) {\n      if (input.old_string === input.new_string) {\n        throw new Error(\"caveman-code: old_string and new_string are identical\");\n      }\n      const target = await contained(input.path);\n      const content = await readFile(target, \"utf8\");\n      const occurrences = content.split(input.old_string).length - 1;\n      if (occurrences === 0) {\n        throw new Error(`caveman-code: old_string not found in ${input.path}`);\n      }\n      if (occurrences > 1 && input.replace_all !== true) {\n        throw new Error(\n          `caveman-code: old_string appears ${occurrences} times in ${input.path}; ` +\n          \"add surrounding context or pass replace_all\",\n        );\n      }\n      // split/join UNCONDITIONALLY. String.prototype.replace\n      // interprets `$&`, `$\\``, `$'`, `$$`, `$1`… in the REPLACEMENT even for a\n      // string pattern, so a new_string containing any of them would silently\n      // corrupt the file. The non-replace_all branch is guaranteed exactly one\n      // occurrence above, so joining replaces precisely that one.\n      const updated = content.split(input.old_string).join(input.new_string);\n      await writeFile(target, updated, \"utf8\");\n      const replaced = input.replace_all === true ? occurrences : 1;\n      return capOutput(\n        `edited ${input.path}: ${replaced} replacement${replaced === 1 ? \"\" : \"s\"}`,\n        caps.edit_file,","sourceCodeStart":331,"sourceCodeEnd":367,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/agent/src/code.ts#L331-L367","documentation":"The edit tool in packages/agent/src/code.ts reads the file and counts occurrences of old_string via content.split(old_string).length - 1. If the count is 0, the search text does not exist in the file and it throws 'caveman-code: old_string not found in <path>'. The match is exact substring matching — no fuzzy or whitespace tolerance.","triggerScenarios":"Calling the edit tool with old_string that does not appear verbatim in the file: stale content after another edit changed it, mismatched indentation/whitespace, wrong file path, or unicode/line-ending differences (CRLF vs LF, smart quotes).","commonSituations":"The model reconstructed the snippet from memory instead of copying it from a fresh read; the file changed since the last read (another tool edit or user modification); the snippet came from a different file; editors normalized quotes or tabs.","solutions":["Re-read the file and copy old_string exactly as it appears, including indentation and line endings.","Verify you are editing the correct file path.","Shrink old_string to a smaller unique snippet that certainly exists.","Check for CRLF vs LF and non-ASCII characters (smart quotes, zero-width spaces); normalize them.","If the file changed concurrently, re-read before editing to get the current content."],"exampleFix":"// before (guessed text)\nedit({ path: \"a.ts\", old_string: \"function foo() {\\n  return 1;\\n}\" })\n// after (exact text from fresh read)\nedit({ path: \"a.ts\", old_string: \"function foo() {\\n\\treturn 1;\\n}\" })","handlingStrategy":"validation","validationCode":"const content = await fs.promises.readFile(path, \"utf8\");\nif (!content.includes(oldString)) {\n  throw new Error(`old_string not found in ${path}; re-read the file and copy exact text`);\n}","typeGuard":null,"tryCatchPattern":"try {\n  await edit(input);\n} catch (e) {\n  if (e.message.includes(\"old_string not found\")) {\n    const content = await fs.promises.readFile(input.path, \"utf8\");\n    console.error(`Re-read ${input.path} and copy old_string verbatim (watch whitespace/CRLF).`);\n  } else throw e;\n}","preventionTips":["Always read the file immediately before editing; never reconstruct snippets from memory.","Copy old_string verbatim, preserving indentation, tabs, and line endings.","Beware CRLF vs LF and smart quotes introduced by editors or copy/paste.","Re-read after concurrent modifications before retrying an edit."],"tags":["editing","filesystem","tooling"],"backgroundTag":"resource-not-found","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}