Tencent/WeKnora · error
old_string was not found in the file. Copy the exact text (i
Error message
old_string was not found in the file. Copy the exact text (including whitespace) from the file
What it means
applySandboxEdit searches the file content with strings.Count(content, oldString) and throws this error when the count is 0, i.e. the exact old_string (including whitespace) does not appear in the file. The edit requires an exact substring match; fuzzy or normalized matching is not performed.
Source
Thrown at internal/agent/tools/sandbox_edit.go:285
}
// Cleanup releases any resources.
func (t *EditSandboxFileTool) Cleanup(ctx context.Context) error {
return nil
}
// applySandboxEdit performs an exact string replacement. replaceAll=false
// requires a unique match.
func applySandboxEdit(content, oldString, newString string, replaceAll bool) (string, int, error) {
if oldString == "" {
return "", 0, fmt.Errorf("old_string is required; copy the exact text to change, including whitespace")
}
if oldString == newString {
return "", 0, fmt.Errorf("old_string and new_string are identical; no change would be made")
}
n := strings.Count(content, oldString)
if n == 0 {
return "", 0, fmt.Errorf("old_string was not found in the file. Copy the exact text (including whitespace) from the file")
}
if n > 1 && !replaceAll {
return "", 0, fmt.Errorf(
"old_string matched %d times. Include more surrounding context so it is unique, or set replace_all=true",
n,
)
}
if replaceAll {
return strings.ReplaceAll(content, oldString, newString), n, nil
}
return strings.Replace(content, oldString, newString, 1), 1, nil
}
View on GitHub (pinned to 988cbb0330)
Solutions
- Re-read the file and copy old_string exactly as it appears, preserving indentation, tabs, and line endings.
- Shorten old_string to a small, definitely-unique snippet you can verify verbatim in the file.
- Check for line-ending or trailing-whitespace mismatches (CRLF vs LF, trailing spaces).
- Retry after a failed edit — the file may have been modified by a previous operation in the same run.
Example fix
// before: snippet from stale copy
oldString := "func Foo(x int) error {\n return nil\n}"
// after: exact text re-read from the file (matching tabs/indentation)
oldString := "func Foo(x int) error {\n\treturn nil\n}" Defensive patterns
Strategy: validation
Validate before calling
if !strings.Contains(fileContent, oldString) {
return fmt.Errorf("old_string %q not present in file; re-read the file", oldString)
} Try / catch
newContent, n, err := applySandboxEdit(content, oldString, newString, replaceAll)
if err != nil && strings.Contains(err.Error(), "was not found") {
content, err = reReadFile(path) // refresh stale content and retry once
} Prevention
- Always copy old_string from the latest file read, never from memory or docs.
- Normalize line endings (LF vs CRLF) and preserve exact indentation/tabs.
- Prefer short, verifiable snippets over large copied blocks.
When it happens
Trigger: Calling Execute with an old_string that differs from the file text in whitespace, indentation, line endings (CRLF vs LF), unicode characters, or was taken from an outdated version of the file.
Common situations: The file changed (or was reformatted) after the snippet was copied; copying code from docs/diffs that doesn't match actual indentation; CRLF on Windows; tab/space mismatches; agent hallucinating the surrounding context.
Related errors
- old_string matched %d times. Include more surrounding contex
- old_string and new_string are identical; no change would be
- model ID cannot be empty
- model is currently downloading
- model download failed
AI-assisted analysis of Tencent/WeKnora@988cbb0330 (2026-09-02).
Data as JSON: /api/errors/fadd7fd2d46aad51.
Report an issue: GitHub.