Skip to content

Commit 900fe5c

Browse files
tweak(edit): separate edit tool error message with clearer guidance to avoid llm doom editing loop (#2051)
1 parent 66a5d58 commit 900fe5c

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

packages/opencode/src/tool/edit.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,8 @@ export function replace(content: string, oldString: string, newString: string, r
594594
throw new Error("oldString and newString must be different")
595595
}
596596

597+
let notFound = true
598+
597599
for (const replacer of [
598600
SimpleReplacer,
599601
LineTrimmedReplacer,
@@ -608,6 +610,7 @@ export function replace(content: string, oldString: string, newString: string, r
608610
for (const search of replacer(content, oldString)) {
609611
const index = content.indexOf(search)
610612
if (index === -1) continue
613+
notFound = false
611614
if (replaceAll) {
612615
return content.replaceAll(search, newString)
613616
}
@@ -616,5 +619,9 @@ export function replace(content: string, oldString: string, newString: string, r
616619
return content.substring(0, index) + newString + content.substring(index + search.length)
617620
}
618621
}
619-
throw new Error("oldString not found in content or was found multiple times")
622+
623+
if (notFound) {
624+
throw new Error("oldString not found in content")
625+
}
626+
throw new Error("oldString found multiple times and requires more code context to uniquely identify the intended match")
620627
}

packages/opencode/src/tool/edit.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ Usage:
55
- When editing text from Read tool output, ensure you preserve the exact indentation (tabs/spaces) as it appears AFTER the line number prefix. The line number prefix format is: spaces + line number + tab. Everything after that tab is the actual file content to match. Never include any part of the line number prefix in the oldString or newString.
66
- ALWAYS prefer editing existing files in the codebase. NEVER write new files unless explicitly required.
77
- Only use emojis if the user explicitly requests it. Avoid adding emojis to files unless asked.
8-
- The edit will FAIL if `oldString` is not unique in the file. Either provide a larger string with more surrounding context to make it unique or use `replaceAll` to change every instance of `oldString`.
8+
- The edit will FAIL if `oldString` is not found in the file with an error "oldString not found in content".
9+
- The edit will FAIL if `oldString` is found multiple times in the file with an error "oldString found multiple times and requires more code context to uniquely identify the intended match". Either provide a larger string with more surrounding context to make it unique or use `replaceAll` to change every instance of `oldString`.
910
- Use `replaceAll` for replacing and renaming strings across the file. This parameter is useful if you want to rename a variable for instance.

0 commit comments

Comments
 (0)