{"record":{"id":"291fa4ac7e7ecda5","repo":"Tencent/WeKnora","slug":"old-string-matched-d-times-include-more-surround","errorCode":null,"errorMessage":"old_string matched %d times. Include more surrounding context so it is unique, or set replace_all=true","messagePattern":"old_string matched (.+?) times\\. Include more surrounding context so it is unique, or set replace_all=true","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/agent/tools/sandbox_edit.go","lineNumber":288,"sourceCode":"func (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":270,"sourceCodeEnd":298,"githubUrl":"https://github.com/Tencent/WeKnora/blob/988cbb03305e055d8ebb7d46d9ac6cc0803cd074/internal/agent/tools/sandbox_edit.go#L270-L298","documentation":"applySandboxEdit throws this when old_string matches the file content more than once (strings.Count > 1) and replaceAll is false. Because an ambiguous match could edit the wrong location, the tool refuses the edit and asks for more context or explicit replace-all semantics.","triggerScenarios":"Calling Execute with a short or repeated old_string (e.g. a closing brace, a common import line, a repeated boilerplate block) without setting replace_all=true.","commonSituations":"Editing repeated function signatures, imports, log lines, or closing brackets; refactoring patterns that occur in multiple places; selecting too small a snippet around a common pattern.","solutions":["Expand old_string with more surrounding lines so it matches exactly one location in the file.","Set replace_all=true if you intentionally want every occurrence replaced.","Search the file for the snippet first to see how many places it appears and pick a unique anchor."],"exampleFix":"// before: ambiguous\napplySandboxEdit(content, \"return nil\", \"return err\", false)\n// after: unique context\napplySandboxEdit(content, \"func Foo() error {\\n\\treturn nil\\n}\", \"func Foo() error {\\n\\treturn err\\n}\", false)","handlingStrategy":"validation","validationCode":"if strings.Count(fileContent, oldString) > 1 && !replaceAll {\n    return fmt.Errorf(\"old_string is ambiguous; add context or set replace_all\")\n}","typeGuard":null,"tryCatchPattern":"newContent, n, err := applySandboxEdit(content, oldString, newString, replaceAll)\nif err != nil && strings.Contains(err.Error(), \"matched\") {\n    oldString = widenWithSurroundingContext(path, oldString) // retry with more context\n    newContent, n, err = applySandboxEdit(content, oldString, newString, replaceAll)\n}","preventionTips":["Include distinctive surrounding lines (function signature, unique identifiers) in old_string.","Check strings.Count(content, oldString) == 1 before calling unless replaceAll is intended.","Never use tiny generic snippets like braces or single lines as anchors."],"tags":["edit-tool","ambiguity","string-match","go"],"backgroundTag":"ambiguous-match-requires-unique-string","analyzedSha":"988cbb03305e055d8ebb7d46d9ac6cc0803cd074","analyzedAt":"2026-09-02T14:41:08.344Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}