Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix(agent): reject overlapping matches in unique sandbox edits (#4002)

澍42cc49333f3d
diff --git a/internal/agent/tools/sandbox_edit.go b/internal/agent/tools/sandbox_edit.go
index 9ce8a243a..41296fd2b 100644
--- a/internal/agent/tools/sandbox_edit.go
+++ b/internal/agent/tools/sandbox_edit.go
@@ -369,7 +369,11 @@ func applySandboxEdits(content string, edits []SandboxEdit) (string, int, error)
if err := validateSandboxEdit(e, i, multi); err != nil {
return "", 0, err
}
- found := indexAllNonOverlapping(content, e.OldString)
+ step := 1 // Uniqueness includes occurrences that overlap each other.
+ if e.ReplaceAll {
+ step = len(e.OldString)
+ }
+ found := indexSandboxMatches(content, e.OldString, step)
if len(found) == 0 {
return "", 0, notFoundSandboxEditError(i, multi)
}
@@ -409,14 +413,21 @@ func applySandboxEdits(content string, edits []SandboxEdit) (string, int, error)
// counting the same way strings.Count does: after a match, scanning resumes
// past it rather than one byte in.
func indexAllNonOverlapping(content, sub string) []int {
+ return indexSandboxMatches(content, sub, len(sub))
+}
+
+func indexSandboxMatches(content, sub string, step int) []int {
var found []int
+ if sub == "" {
+ return found
+ }
for offset := 0; ; {
i := strings.Index(content[offset:], sub)
if i < 0 {
return found
}
found = append(found, offset+i)
- offset += i + len(sub)
+ offset += i + step
}
}
diff --git a/internal/agent/tools/sandbox_edit_ambiguity_test.go b/internal/agent/tools/sandbox_edit_ambiguity_test.go
new file mode 100644
index 000000000..4ce1621c2
--- /dev/null
+++ b/internal/agent/tools/sandbox_edit_ambiguity_test.go
@@ -0,0 +1,67 @@
+package tools
+
+import (
+ "encoding/json"
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+)
+
+func TestSandboxEditRejectsOverlappingOccurrences(t *testing.T) {
+ for _, tc := range []struct {
+ name, content, old string
+ matches string
+ }{
+ {"repeated character", "aaa", "aa", "2 times"},
+ {"repeated pattern", "ababa", "aba", "2 times"},
+ {"three overlapping matches", "aaaa", "aa", "3 times"},
+ {"unicode", "哈哈哈", "哈哈", "2 times"},
+ {"multiline", "a\na\na", "a\na", "2 times"},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ updated, n, err := applySandboxEdit(tc.content, tc.old, "replacement", false)
+ require.Error(t, err)
+ assert.Contains(t, err.Error(), tc.matches)
+ assert.Empty(t, updated)
+ assert.Zero(t, n)
+ })
+ }
+}
+
+func TestSandboxEditReplaceAllRetainsNonOverlappingSemantics(t *testing.T) {
+ for _, tc := range []struct {
+ content, old, want string
+ count int
+ }{
+ {"aaa", "aa", "Xa", 1},
+ {"aaaaa", "aa", "XXa", 2},
+ {"ababa", "aba", "Xba", 1},
+ {"哈哈哈", "哈哈", "X哈", 1},
+ } {
+ t.Run(tc.content, func(t *testing.T) {
+ updated, n, err := applySandboxEdit(tc.content, tc.old, "X", true)
+ require.NoError(t, err)
+ assert.Equal(t, tc.want, updated)
+ assert.Equal(t, tc.count, n)
+ })
+ }
+}
+
+func TestSandboxEditAmbiguousBatchDoesNotWrite(t *testing.T) {
+ const filePath = "/workspace/output/example.txt"
+ const original = "header\nababa\n"
+ editor := &fakeSandboxFileEditor{files: map[string][]byte{filePath: []byte(original)}}
+ args, err := json.Marshal(EditSandboxFileInput{Path: filePath, Edits: sandboxEditList{
+ {OldString: "header", NewString: "updated"},
+ {OldString: "aba", NewString: "X"},
+ }})
+ require.NoError(t, err)
+ result, err := NewEditSandboxFileTool(editor).Execute(sandboxFileTestContext(), args)
+ require.NoError(t, err)
+ require.False(t, result.Success)
+ assert.Contains(t, result.Error, "edits[1]")
+ assert.Contains(t, result.Error, "2 times")
+ assert.Zero(t, editor.writes)
+ assert.Equal(t, original, string(editor.files[filePath]))
+}