{"record":{"id":"d5fd6345a75e245e","repo":"JuliusBrussee/caveman","slug":"caveman-code-old-string-and-new-string-are-identi","errorCode":null,"errorMessage":"caveman-code: old_string and new_string are identical","messagePattern":"caveman-code: old_string and new_string are identical","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/agent/src/code.ts","lineNumber":343,"sourceCode":"  });\n\n  const editTool = tool({\n    name: \"edit_file\",\n    description:\n      \"Replace an exact string in a workspace file. The old string must appear exactly \" +\n      \"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.","sourceCodeStart":325,"sourceCodeEnd":361,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/agent/src/code.ts#L325-L361","documentation":"The edit/replace tool in packages/agent/src/code.ts rejects a no-op edit up front: if old_string exactly equals new_string, nothing would change, so it throws 'caveman-code: old_string and new_string are identical' before reading the file. This catches agent loops that re-emit the same replacement.","triggerScenarios":"Calling the edit tool where input.old_string === input.new_string byte-for-byte — e.g. re-running a previous successful edit, copy-pasting the same block into both fields, or attempting an 'edit' that only reformats via regex which the tool does not support.","commonSituations":"LLM retries replaying an identical edit after a failure elsewhere; generating edits programmatically without checking the strings differ; intending a regex replacement but passing literal identical strings; whitespace normalization silently making both sides equal.","solutions":["Change new_string to the desired different content — an edit must actually modify the text.","If the edit was already applied, skip it instead of re-issuing the same call.","If you only wanted to locate text, use the read or grep tool rather than edit.","Trim or normalize the strings and confirm they truly differ (compare with ===) before calling.","For regex-style transformations, compute the new text yourself and pass it as new_string."],"exampleFix":"// before\nedit({ path: \"a.ts\", old_string: \"const x = 1;\", new_string: \"const x = 1;\" })\n// after\nedit({ path: \"a.ts\", old_string: \"const x = 1;\", new_string: \"const x = 2;\" })","handlingStrategy":"validation","validationCode":"if (input.old_string === input.new_string) {\n  throw new Error(\"old_string and new_string are identical; the edit is a no-op\");\n}","typeGuard":null,"tryCatchPattern":"try {\n  await edit(input);\n} catch (e) {\n  if (e.message.includes(\"old_string and new_string are identical\")) {\n    console.error(\"Skipping no-op edit.\");\n  } else throw e;\n}","preventionTips":["Assert old_string !== new_string before every edit call in generated tooling.","After a successful edit, update the agent's file snapshot so it never replays the same edit.","Use read/grep to inspect content instead of issuing zero-diff edits.","Precompute regex transformations into a concrete new_string that differs."],"tags":["editing","invalid-argument","tooling"],"backgroundTag":"invalid-argument-value","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"}