JuliusBrussee/caveman · error
caveman-code: old_string appears
Error message
caveman-code: old_string appears ${occurrences} times in ${input.path}; add surrounding context or pass replace_all What it means
The edit tool counts occurrences of old_string; when it matches more than once and input.replace_all is not exactly true, the replacement would be ambiguous, so it throws 'caveman-code: old_string appears N times in <path>; add surrounding context or pass replace_all'. This prevents silently editing the wrong occurrence.
Solutions
- Expand old_string with surrounding context so it matches exactly one occurrence.
- Pass replace_all: true (boolean) if every occurrence should be replaced.
- Read the file first and pick a unique anchor (include the function/variable name) around the target.
- Perform multiple targeted edits, one unique snippet each, instead of one ambiguous edit.
- Ensure replace_all is the boolean true, not the string "true".
Example fix
// before
edit({ path: "a.ts", old_string: "return null;" })
// after
edit({ path: "a.ts", old_string: "function load() {\n return null;\n}", new_string: "function load() {\n return undefined;\n}" }) Defensive patterns
Strategy: validation
Validate before calling
const content = await fs.promises.readFile(path, "utf8");
const occurrences = content.split(oldString).length - 1;
if (occurrences > 1 && replaceAll !== true) {
throw new Error(`old_string occurs ${occurrences} times; add context or set replace_all: true`);
} Try / catch
try {
await edit(input);
} catch (e) {
if (e.message.includes("times in") && e.message.includes("replace_all")) {
console.error("Ambiguous match: widen old_string with context or pass replace_all: true.");
} else throw e;
} Prevention
- Count occurrences of old_string before editing; require uniqueness unless replace_all is intended.
- Include surrounding lines (function name, distinct context) to make the match unique.
- Pass replace_all as the boolean true, never the string "true".
- Avoid editing highly repetitive boilerplate with short snippets; split into targeted edits.
When it happens
Trigger: Calling the edit tool with an old_string that occurs 2+ times in the file (e.g. a common pattern like `});` or a repeated function call) while omitting replace_all, or setting replace_all to anything other than the boolean true.
Common situations: Replacing boilerplate that appears in many places (imports, closing braces, repeated config lines); editing minified or highly repetitive files; passing replace_all: "true" (string) which fails the !== true check.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- caveman-code: old_string and new_string are identical
- caveman-code: old_string not found in
- cave_sandbox_credential_capability_ambiguous
- caveman-code: multiple provider credentials found; set…
- caveman-code: not a file
AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20).
Data as JSON: /api/errors/7d4f3f3f9354b01d.
Report an issue: GitHub.
Appendix: source
Thrown at packages/agent/src/code.ts:352
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)