JuliusBrussee/caveman · error
caveman-code: old_string not found in
Error message
caveman-code: old_string not found in ${input.path} What it means
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.
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.
Example fix
// before (guessed text)
edit({ path: "a.ts", old_string: "function foo() {\n return 1;\n}" })
// after (exact text from fresh read)
edit({ path: "a.ts", old_string: "function foo() {\n\treturn 1;\n}" }) Defensive patterns
Strategy: validation
Validate before calling
const content = await fs.promises.readFile(path, "utf8");
if (!content.includes(oldString)) {
throw new Error(`old_string not found in ${path}; re-read the file and copy exact text`);
} Try / catch
try {
await edit(input);
} catch (e) {
if (e.message.includes("old_string not found")) {
const content = await fs.promises.readFile(input.path, "utf8");
console.error(`Re-read ${input.path} and copy old_string verbatim (watch whitespace/CRLF).`);
} else throw e;
} Prevention
- 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.
When it happens
Trigger: 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).
Common situations: 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.
Understand the failure class
Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.
Related errors
- caveman-code: not a file
- caveman-code: old_string and new_string are identical
- caveman-code: old_string appears
- awscreds: read container authorization token file
- cannot persist MCP ownership journal at
AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20).
Data as JSON: /api/errors/ccc4a4d095e9bdb4.
Report an issue: GitHub.
Appendix: source
Thrown at packages/agent/src/code.ts:349
"once unless replace_all is set. Writes to disk.",
input: schema.object({
path: schema.string(),
old_string: schema.string(),
new_string: schema.string(),
replace_all: schema.optional(schema.boolean()),
}),
effect: "write",
result: "inline",
timeoutMs: READ_TIMEOUT_MS,
async execute(input) {
if (input.old_string === input.new_string) {
throw new Error("caveman-code: old_string and new_string are identical");
}
const target = await contained(input.path);
const content = await readFile(target, "utf8");
const occurrences = content.split(input.old_string).length - 1;
if (occurrences === 0) {
throw new Error(`caveman-code: old_string not found in ${input.path}`);
}
if (occurrences > 1 && input.replace_all !== true) {
throw new Error(
`caveman-code: old_string appears ${occurrences} times in ${input.path}; ` +
"add surrounding context or pass replace_all",
);
}
// split/join UNCONDITIONALLY. String.prototype.replace
// interprets `$&`, `$\``, `$'`, `$$`, `$1`… in the REPLACEMENT even for a
// string pattern, so a new_string containing any of them would silently
// corrupt the file. The non-replace_all branch is guaranteed exactly one
// occurrence above, so joining replaces precisely that one.
const updated = content.split(input.old_string).join(input.new_string);
await writeFile(target, updated, "utf8");
const replaced = input.replace_all === true ? occurrences : 1;
return capOutput(
`edited ${input.path}: ${replaced} replacement${replaced === 1 ? "" : "s"}`,
caps.edit_file,View on GitHub (pinned to 3ee70a1026)