{"record":{"id":"fadd7fd2d46aad51","repo":"Tencent/WeKnora","slug":"old-string-was-not-found-in-the-file-copy-the-exa","errorCode":null,"errorMessage":"old_string was not found in the file. Copy the exact text (including whitespace) from the file","messagePattern":"old_string was not found in the file\\. Copy the exact text \\(including whitespace\\) from the file","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/agent/tools/sandbox_edit.go","lineNumber":285,"sourceCode":"}\n\n// Cleanup releases any resources.\nfunc (t *EditSandboxFileTool) Cleanup(ctx context.Context) error {\n\treturn nil\n}\n\n// applySandboxEdit performs an exact string replacement. replaceAll=false\n// requires a unique match.\nfunc applySandboxEdit(content, oldString, newString string, replaceAll bool) (string, int, error) {\n\tif oldString == \"\" {\n\t\treturn \"\", 0, fmt.Errorf(\"old_string is required; copy the exact text to change, including whitespace\")\n\t}\n\tif oldString == newString {\n\t\treturn \"\", 0, fmt.Errorf(\"old_string and new_string are identical; no change would be made\")\n\t}\n\tn := strings.Count(content, oldString)\n\tif n == 0 {\n\t\treturn \"\", 0, fmt.Errorf(\"old_string was not found in the file. Copy the exact text (including whitespace) from the file\")\n\t}\n\tif n > 1 && !replaceAll {\n\t\treturn \"\", 0, fmt.Errorf(\n\t\t\t\"old_string matched %d times. Include more surrounding context so it is unique, or set replace_all=true\",\n\t\t\tn,\n\t\t)\n\t}\n\tif replaceAll {\n\t\treturn strings.ReplaceAll(content, oldString, newString), n, nil\n\t}\n\treturn strings.Replace(content, oldString, newString, 1), 1, nil\n}\n","sourceCodeStart":267,"sourceCodeEnd":298,"githubUrl":"https://github.com/Tencent/WeKnora/blob/988cbb03305e055d8ebb7d46d9ac6cc0803cd074/internal/agent/tools/sandbox_edit.go#L267-L298","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before: snippet from stale copy\noldString := \"func Foo(x int) error {\\n    return nil\\n}\"\n// after: exact text re-read from the file (matching tabs/indentation)\noldString := \"func Foo(x int) error {\\n\\treturn nil\\n}\"","handlingStrategy":"validation","validationCode":"if !strings.Contains(fileContent, oldString) {\n    return fmt.Errorf(\"old_string %q not present in file; re-read the file\", oldString)\n}","typeGuard":null,"tryCatchPattern":"newContent, n, err := applySandboxEdit(content, oldString, newString, replaceAll)\nif err != nil && strings.Contains(err.Error(), \"was not found\") {\n    content, err = reReadFile(path) // refresh stale content and retry once\n}","preventionTips":["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."],"tags":["edit-tool","string-match","whitespace","go"],"backgroundTag":"old-string-not-found","analyzedSha":"988cbb03305e055d8ebb7d46d9ac6cc0803cd074","analyzedAt":"2026-09-02T14:41:08.344Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}