diff options
| author | spoons-and-mirrors <[email protected]> | 2025-09-05 18:36:13 +0200 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-09-05 12:36:13 -0400 |
| commit | 900fe5ca041c5b9e2de9449c441dd9cb10beed0a (patch) | |
| tree | 3158de216f7dafec5ed39919bed1d2db5403aa83 /packages | |
| parent | 66a5d582211b444679eea69d30fa8cafe88dc76f (diff) | |
| download | opencode-900fe5ca041c5b9e2de9449c441dd9cb10beed0a.tar.gz opencode-900fe5ca041c5b9e2de9449c441dd9cb10beed0a.zip | |
tweak(edit): separate edit tool error message with clearer guidance to avoid llm doom editing loop (#2051)
Diffstat (limited to 'packages')
| -rw-r--r-- | packages/opencode/src/tool/edit.ts | 9 | ||||
| -rw-r--r-- | packages/opencode/src/tool/edit.txt | 3 |
2 files changed, 10 insertions, 2 deletions
diff --git a/packages/opencode/src/tool/edit.ts b/packages/opencode/src/tool/edit.ts index 928188c07..f4fd87feb 100644 --- a/packages/opencode/src/tool/edit.ts +++ b/packages/opencode/src/tool/edit.ts @@ -594,6 +594,8 @@ export function replace(content: string, oldString: string, newString: string, r throw new Error("oldString and newString must be different") } + let notFound = true + for (const replacer of [ SimpleReplacer, LineTrimmedReplacer, @@ -608,6 +610,7 @@ export function replace(content: string, oldString: string, newString: string, r for (const search of replacer(content, oldString)) { const index = content.indexOf(search) if (index === -1) continue + notFound = false if (replaceAll) { return content.replaceAll(search, newString) } @@ -616,5 +619,9 @@ export function replace(content: string, oldString: string, newString: string, r return content.substring(0, index) + newString + content.substring(index + search.length) } } - throw new Error("oldString not found in content or was found multiple times") + + if (notFound) { + throw new Error("oldString not found in content") + } + throw new Error("oldString found multiple times and requires more code context to uniquely identify the intended match") } diff --git a/packages/opencode/src/tool/edit.txt b/packages/opencode/src/tool/edit.txt index 8bf8844d3..863efb840 100644 --- a/packages/opencode/src/tool/edit.txt +++ b/packages/opencode/src/tool/edit.txt @@ -5,5 +5,6 @@ Usage: - 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. - ALWAYS prefer editing existing files in the codebase. NEVER write new files unless explicitly required. - Only use emojis if the user explicitly requests it. Avoid adding emojis to files unless asked. -- 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`. +- The edit will FAIL if `oldString` is not found in the file with an error "oldString not found in content". +- 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`. - Use `replaceAll` for replacing and renaming strings across the file. This parameter is useful if you want to rename a variable for instance. |
