feat: add multi-edit support to edit tool

Implement multi-edit functionality matching Pi's approach:
- Add 'edits' array parameter for multiple disjoint replacements
- All edits matched against original content (non-incremental)
- Overlap detection prevents conflicting edits
- Duplicate detection ensures unique matches
- Atomic operations: all succeed or none applied
- Detailed error messages with edit indices (edits[0], etc.)
- Fuzzy matching works with multi-edit mode
- Backward compatible with single-edit mode (old_text/new_text)

Changes:
- internal/core/edit.go: Multi-edit logic, validation, overlap detection
- internal/ui/messages.go: Add 'edits' to body keys
- internal/ui/tool_renderers.go: Render multi-edit diffs
- internal/core/edit_test.go: 9 comprehensive multi-edit tests
This commit is contained in:
Ed Zynda
2026-03-27 10:34:43 +03:00
parent 941f1daf0b
commit 8b1665a4ce
4 changed files with 573 additions and 47 deletions
+234 -44
View File
@@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"sort"
"strings"
"unicode"
"unicode/utf8"
@@ -13,19 +14,45 @@ import (
udiff "github.com/aymanbagabas/go-udiff"
)
type editArgs struct {
Path string `json:"path"`
// Edit represents a single replacement in a multi-edit operation.
type Edit struct {
OldText string `json:"old_text"`
NewText string `json:"new_text"`
}
// editArgs holds the arguments for the edit tool.
// Supports both single-edit mode (old_text/new_text) and multi-edit mode (edits array).
type editArgs struct {
Path string `json:"path"`
OldText string `json:"old_text"` // Single-edit mode
NewText string `json:"new_text"` // Single-edit mode
Edits []Edit `json:"edits"` // Multi-edit mode
}
// replacement represents a normalized edit ready for processing.
type replacement struct {
oldText string // normalized old text for matching
newText string // normalized new text
originalOld string // original old text for metadata
originalNew string // original new text for metadata
index int // index in the original edits array (for error messages)
}
// matchedReplacement represents a replacement with its match location.
type matchedReplacement struct {
replacement
start int // start index in normalized content
end int // end index in normalized content
usedFuzzyMatch bool // true if fuzzy matching was used
}
// NewEditTool creates the edit core tool.
func NewEditTool(opts ...ToolOption) fantasy.AgentTool {
cfg := ApplyOptions(opts)
return &coreTool{
info: fantasy.ToolInfo{
Name: "edit",
Description: "Edit a file by replacing exact text. The old_text must match exactly (including whitespace). Use this for precise, surgical edits. Fails if old_text is not found or matches multiple locations.",
Description: "Edit a file by replacing exact text. Supports single edit via old_text/new_text, or multiple edits via the edits array. All edits in the array are matched against the original file content (non-incremental) and must be non-overlapping.",
Parameters: map[string]any{
"path": map[string]any{
"type": "string",
@@ -33,14 +60,32 @@ func NewEditTool(opts ...ToolOption) fantasy.AgentTool {
},
"old_text": map[string]any{
"type": "string",
"description": "Exact text to find and replace (must match exactly)",
"description": "Exact text to find and replace (single-edit mode). Must not be used with 'edits' array.",
},
"new_text": map[string]any{
"type": "string",
"description": "New text to replace the old text with",
"description": "New text to replace the old text with (single-edit mode). Must not be used with 'edits' array.",
},
"edits": map[string]any{
"type": "array",
"description": "Array of edits for multi-region replacement. Each edit must have unique, non-overlapping old_text. All matches are against the original file content.",
"items": map[string]any{
"type": "object",
"properties": map[string]any{
"old_text": map[string]any{
"type": "string",
"description": "Exact text to find and replace for this edit",
},
"new_text": map[string]any{
"type": "string",
"description": "New text for this edit",
},
},
"required": []string{"old_text", "new_text"},
},
},
},
Required: []string{"path", "old_text", "new_text"},
Required: []string{"path"},
},
handler: func(ctx context.Context, call fantasy.ToolCall) (fantasy.ToolResponse, error) {
return executeEdit(ctx, call, cfg.WorkDir)
@@ -51,7 +96,7 @@ func NewEditTool(opts ...ToolOption) fantasy.AgentTool {
func executeEdit(ctx context.Context, call fantasy.ToolCall, workDir string) (fantasy.ToolResponse, error) {
var args editArgs
if err := parseArgs(call.Input, &args); err != nil {
return fantasy.NewTextErrorResponse("path, old_text, and new_text parameters are required"), nil
return fantasy.NewTextErrorResponse("failed to parse arguments: " + err.Error()), nil
}
if args.Path == "" {
return fantasy.NewTextErrorResponse("path parameter is required"), nil
@@ -69,56 +114,201 @@ func executeEdit(ctx context.Context, call fantasy.ToolCall, workDir string) (fa
content := string(contentBytes)
// Normalize line endings for matching
normalized := strings.ReplaceAll(content, "\r\n", "\n")
normalizedOld := strings.ReplaceAll(args.OldText, "\r\n", "\n")
// Try exact match first
count := strings.Count(normalized, normalizedOld)
// If no exact match, try fuzzy matching
if count == 0 {
if idx, matchLen := fuzzyMatch(normalized, normalizedOld); idx >= 0 {
// Apply fuzzy match — the matched text is the original content slice
matchedText := normalized[idx : idx+matchLen]
newContent := normalized[:idx] + args.NewText + normalized[idx+matchLen:]
if err := os.WriteFile(absPath, []byte(newContent), 0644); err != nil {
return fantasy.NewTextErrorResponse(fmt.Sprintf("failed to write file: %v", err)), nil
}
diff := generateDiff(absPath, normalized, newContent)
resp := fantasy.NewTextResponse(fmt.Sprintf("Applied edit (fuzzy match) to %s\n%s", args.Path, diff))
return fantasy.WithResponseMetadata(resp, editDiffMeta(absPath, matchedText, args.NewText)), nil
}
return fantasy.NewTextErrorResponse(fmt.Sprintf("old_text not found in %s", args.Path)), nil
// Normalize and validate input
replacements, err := normalizeEditInput(args)
if err != nil {
return fantasy.NewTextErrorResponse(err.Error()), nil
}
if count > 1 {
return fantasy.NewTextErrorResponse(fmt.Sprintf("found %d matches for old_text in %s. Provide more context to identify the correct match.", count, args.Path)), nil
// Apply all edits
newContent, applied, err := applyEdits(content, replacements)
if err != nil {
return fantasy.NewTextErrorResponse(err.Error()), nil
}
// Apply the edit
newContent := strings.Replace(normalized, normalizedOld, args.NewText, 1)
// Write the file
if err := os.WriteFile(absPath, []byte(newContent), 0644); err != nil {
return fantasy.NewTextErrorResponse(fmt.Sprintf("failed to write file: %v", err)), nil
}
diff := generateDiff(absPath, normalized, newContent)
resp := fantasy.NewTextResponse(fmt.Sprintf("Applied edit to %s\n%s", args.Path, diff))
return fantasy.WithResponseMetadata(resp, editDiffMeta(absPath, normalizedOld, args.NewText)), nil
// Generate diff
normalizedContent := strings.ReplaceAll(content, "\r\n", "\n")
diff := generateDiff(absPath, normalizedContent, newContent)
// Build response with fuzzy match indication
fuzzyCount := 0
for _, m := range applied {
if m.usedFuzzyMatch {
fuzzyCount++
}
}
var msg string
if len(applied) == 1 {
if fuzzyCount > 0 {
msg = fmt.Sprintf("Applied edit (fuzzy match) to %s\n%s", args.Path, diff)
} else {
msg = fmt.Sprintf("Applied edit to %s\n%s", args.Path, diff)
}
} else {
if fuzzyCount > 0 {
msg = fmt.Sprintf("Applied %d edits (%d fuzzy) to %s\n%s", len(applied), fuzzyCount, args.Path, diff)
} else {
msg = fmt.Sprintf("Applied %d edits to %s\n%s", len(applied), args.Path, diff)
}
}
resp := fantasy.NewTextResponse(msg)
return fantasy.WithResponseMetadata(resp, editDiffMeta(absPath, applied)), nil
}
// normalizeEditInput validates and normalizes the edit input.
// Returns error if both single-edit and multi-edit modes are used.
func normalizeEditInput(args editArgs) ([]replacement, error) {
singleMode := args.OldText != "" || args.NewText != ""
multiMode := len(args.Edits) > 0
if singleMode && multiMode {
return nil, fmt.Errorf("cannot use old_text/new_text together with edits array")
}
if !singleMode && !multiMode {
return nil, fmt.Errorf("must provide either old_text/new_text or edits array")
}
if singleMode {
if args.OldText == "" {
return nil, fmt.Errorf("old_text is required when using single-edit mode")
}
if args.NewText == "" {
return nil, fmt.Errorf("new_text is required when using single-edit mode")
}
return []replacement{{
oldText: strings.ReplaceAll(args.OldText, "\r\n", "\n"),
newText: strings.ReplaceAll(args.NewText, "\r\n", "\n"),
originalOld: args.OldText,
originalNew: args.NewText,
index: 0,
}}, nil
}
// Multi-edit mode
var reps []replacement
for i, edit := range args.Edits {
if edit.OldText == "" {
return nil, fmt.Errorf("edits[%d].old_text is required", i)
}
reps = append(reps, replacement{
oldText: strings.ReplaceAll(edit.OldText, "\r\n", "\n"),
newText: strings.ReplaceAll(edit.NewText, "\r\n", "\n"),
originalOld: edit.OldText,
originalNew: edit.NewText,
index: i,
})
}
return reps, nil
}
// applyEdits applies multiple replacements to the content.
// All matches are against the original content (non-incremental).
// Returns the new content, the applied matches, and any error.
func applyEdits(content string, edits []replacement) (string, []matchedReplacement, error) {
normalizedContent := strings.ReplaceAll(content, "\r\n", "\n")
// Find all matches
var matched []matchedReplacement
for _, edit := range edits {
m, err := findMatch(normalizedContent, edit)
if err != nil {
return "", nil, err
}
matched = append(matched, *m)
}
// Sort by position
sort.Slice(matched, func(i, j int) bool {
return matched[i].start < matched[j].start
})
// Check for overlaps
for i := 1; i < len(matched); i++ {
if matched[i-1].end > matched[i].start {
return "", nil, fmt.Errorf("edits[%d] and edits[%d] overlap; merge them into a single edit",
matched[i-1].index, matched[i].index)
}
}
// Apply edits in reverse order (end to start) to maintain stable offsets
result := normalizedContent
for i := len(matched) - 1; i >= 0; i-- {
m := matched[i]
result = result[:m.start] + m.newText + result[m.end:]
}
return result, matched, nil
}
// findMatch finds a unique match for the edit in the content.
// Returns error if not found or ambiguous.
func findMatch(content string, edit replacement) (*matchedReplacement, error) {
// Try exact match first
count := strings.Count(content, edit.oldText)
if count == 0 {
// Try fuzzy match
idx, matchLen := fuzzyMatch(content, edit.oldText)
if idx < 0 {
return nil, fmt.Errorf("edits[%d]: could not find old_text in file. The text must match exactly (including whitespace)", edit.index)
}
// Use the matched text from content for the replacement
matchedText := content[idx : idx+matchLen]
return &matchedReplacement{
replacement: replacement{
oldText: matchedText,
newText: edit.newText,
originalOld: edit.originalOld,
originalNew: edit.originalNew,
index: edit.index,
},
start: idx,
end: idx + matchLen,
usedFuzzyMatch: true,
}, nil
}
if count > 1 {
return nil, fmt.Errorf("found %d matches for edits[%d].old_text; each old_text must be unique, provide more context to identify the correct match", count, edit.index)
}
// Single exact match
idx := strings.Index(content, edit.oldText)
return &matchedReplacement{
replacement: edit,
start: idx,
end: idx + len(edit.oldText),
}, nil
}
// editDiffMeta builds the structured metadata attached to edit tool responses.
func editDiffMeta(path, oldText, newText string) map[string]any {
func editDiffMeta(path string, applied []matchedReplacement) map[string]any {
var diffBlocks []map[string]any
totalAdditions, totalDeletions := 0, 0
for _, m := range applied {
diffBlocks = append(diffBlocks, map[string]any{
"old_text": m.originalOld,
"new_text": m.originalNew,
})
totalAdditions += strings.Count(m.originalNew, "\n") + 1
totalDeletions += strings.Count(m.originalOld, "\n") + 1
}
return map[string]any{
"file_diffs": []map[string]any{{
"path": path,
"additions": strings.Count(newText, "\n") + 1,
"deletions": strings.Count(oldText, "\n") + 1,
"diff_blocks": []map[string]any{{
"old_text": oldText,
"new_text": newText,
}},
"path": path,
"additions": totalAdditions,
"deletions": totalDeletions,
"diff_blocks": diffBlocks,
}},
}
}
+312
View File
@@ -715,3 +715,315 @@ func TestExecuteEdit_MetadataContainsFileDiffs(t *testing.T) {
t.Fatal("file_diffs should be a non-empty array")
}
}
// ---------------------------------------------------------------------------
// Multi-edit tests
// ---------------------------------------------------------------------------
func TestExecuteEdit_MultiEdit_Basic(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "multi.txt")
writeFileOrFail(t, path, "line1\nline2\nline3\nline4\n")
input, _ := json.Marshal(editArgs{
Path: path,
Edits: []Edit{
{OldText: "line1", NewText: "LINE1"},
{OldText: "line3", NewText: "LINE3"},
},
})
resp, err := executeEdit(t.Context(), fantasy.ToolCall{Input: string(input)}, dir)
if err != nil {
t.Fatalf("executeEdit error: %v", err)
}
if resp.IsError {
t.Fatalf("tool returned error: %s", resp.Content)
}
got, _ := os.ReadFile(path)
gotStr := string(got)
if !strings.Contains(gotStr, "LINE1") {
t.Error("first edit not applied: missing LINE1")
}
if !strings.Contains(gotStr, "LINE3") {
t.Error("second edit not applied: missing LINE3")
}
if !strings.Contains(gotStr, "line2") {
t.Error("line2 was modified but should be untouched")
}
if !strings.Contains(gotStr, "line4") {
t.Error("line4 was modified but should be untouched")
}
// Check response mentions multiple edits
if !strings.Contains(resp.Content, "2 edits") {
t.Errorf("response should mention '2 edits', got: %s", resp.Content)
}
}
func TestExecuteEdit_MultiEdit_NonIncrementalMatching(t *testing.T) {
// All edits are matched against the original content, not incrementally
dir := t.TempDir()
path := filepath.Join(dir, "noninc.txt")
writeFileOrFail(t, path, "aaa\nbbb\nccc\n")
input, _ := json.Marshal(editArgs{
Path: path,
Edits: []Edit{
{OldText: "aaa", NewText: "AAA"},
{OldText: "bbb", NewText: "BBB"},
},
})
resp, err := executeEdit(t.Context(), fantasy.ToolCall{Input: string(input)}, dir)
if err != nil {
t.Fatalf("executeEdit error: %v", err)
}
if resp.IsError {
t.Fatalf("tool returned error: %s", resp.Content)
}
got, _ := os.ReadFile(path)
gotStr := string(got)
want := "AAA\nBBB\nccc\n"
if gotStr != want {
t.Errorf("got %q, want %q", gotStr, want)
}
}
func TestExecuteEdit_MultiEdit_OverlapDetection(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "overlap.txt")
writeFileOrFail(t, path, "hello world\n")
input, _ := json.Marshal(editArgs{
Path: path,
Edits: []Edit{
{OldText: "hello", NewText: "HELLO"},
{OldText: "hello world", NewText: "GOODBYE"}, // Overlaps with first edit
},
})
resp, err := executeEdit(t.Context(), fantasy.ToolCall{Input: string(input)}, dir)
if err != nil {
t.Fatalf("executeEdit error: %v", err)
}
if !resp.IsError {
t.Error("expected error for overlapping edits")
}
if !strings.Contains(resp.Content, "overlap") {
t.Errorf("expected 'overlap' in error, got: %s", resp.Content)
}
// File should be untouched
got, _ := os.ReadFile(path)
if string(got) != "hello world\n" {
t.Error("file was modified despite error")
}
}
func TestExecuteEdit_MultiEdit_DuplicateDetection(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "dup.txt")
writeFileOrFail(t, path, "hello\nworld\nhello\n")
input, _ := json.Marshal(editArgs{
Path: path,
Edits: []Edit{
{OldText: "hello", NewText: "HELLO"},
{OldText: "world", NewText: "WORLD"},
},
})
resp, err := executeEdit(t.Context(), fantasy.ToolCall{Input: string(input)}, dir)
if err != nil {
t.Fatalf("executeEdit error: %v", err)
}
if !resp.IsError {
t.Error("expected error for ambiguous old_text (duplicate matches)")
}
if !strings.Contains(resp.Content, "unique") {
t.Errorf("expected 'unique' in error, got: %s", resp.Content)
}
// File should be untouched
got, _ := os.ReadFile(path)
if string(got) != "hello\nworld\nhello\n" {
t.Error("file was modified despite error")
}
}
func TestExecuteEdit_MultiEdit_NotFound(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "notfound.txt")
writeFileOrFail(t, path, "hello world\n")
input, _ := json.Marshal(editArgs{
Path: path,
Edits: []Edit{
{OldText: "nonexistent", NewText: "REPLACEMENT"},
},
})
resp, err := executeEdit(t.Context(), fantasy.ToolCall{Input: string(input)}, dir)
if err != nil {
t.Fatalf("executeEdit error: %v", err)
}
if !resp.IsError {
t.Error("expected error for not found")
}
if !strings.Contains(resp.Content, "edits[0]") {
t.Errorf("expected 'edits[0]' in error, got: %s", resp.Content)
}
// File should be untouched
got, _ := os.ReadFile(path)
if string(got) != "hello world\n" {
t.Error("file was modified despite error")
}
}
func TestExecuteEdit_MultiEdit_EmptyArray(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "empty.txt")
writeFileOrFail(t, path, "hello\n")
input, _ := json.Marshal(editArgs{
Path: path,
Edits: []Edit{},
})
resp, err := executeEdit(t.Context(), fantasy.ToolCall{Input: string(input)}, dir)
if err != nil {
t.Fatalf("executeEdit error: %v", err)
}
if !resp.IsError {
t.Error("expected error for empty edits array")
}
}
func TestExecuteEdit_MultiEdit_MixedWithSingleMode(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "mixed.txt")
writeFileOrFail(t, path, "hello\n")
input, _ := json.Marshal(map[string]any{
"path": path,
"old_text": "hello",
"new_text": "HELLO",
"edits": []Edit{
{OldText: "hello", NewText: "HI"},
},
})
resp, err := executeEdit(t.Context(), fantasy.ToolCall{Input: string(input)}, dir)
if err != nil {
t.Fatalf("executeEdit error: %v", err)
}
if !resp.IsError {
t.Error("expected error when mixing single and multi-edit modes")
}
if !strings.Contains(resp.Content, "cannot use") {
t.Errorf("expected 'cannot use' in error, got: %s", resp.Content)
}
}
func TestExecuteEdit_MultiEdit_FuzzyMatch(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "fuzzy_multi.txt")
// File has trailing whitespace
original := "func foo() { \n\treturn 1 \n}\nfunc bar() { \n\treturn 2 \n}\n"
writeFileOrFail(t, path, original)
// Search without trailing whitespace (common LLM behavior)
input, _ := json.Marshal(editArgs{
Path: path,
Edits: []Edit{
{OldText: "func foo() {\n\treturn 1\n}", NewText: "func foo() {\n\treturn 10\n}"},
{OldText: "func bar() {\n\treturn 2\n}", NewText: "func bar() {\n\treturn 20\n}"},
},
})
resp, err := executeEdit(t.Context(), fantasy.ToolCall{Input: string(input)}, dir)
if err != nil {
t.Fatalf("executeEdit error: %v", err)
}
if resp.IsError {
t.Fatalf("tool returned error: %s", resp.Content)
}
got, _ := os.ReadFile(path)
gotStr := string(got)
if !strings.Contains(gotStr, "return 10") {
t.Error("first edit not applied")
}
if !strings.Contains(gotStr, "return 20") {
t.Error("second edit not applied")
}
// Response should mention fuzzy match
if !strings.Contains(resp.Content, "fuzzy") {
t.Errorf("response should mention 'fuzzy', got: %s", resp.Content)
}
}
func TestExecuteEdit_MultiEdit_Metadata(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "meta_multi.txt")
writeFileOrFail(t, path, "aaa\nbbb\nccc\n")
input, _ := json.Marshal(editArgs{
Path: path,
Edits: []Edit{
{OldText: "aaa", NewText: "AAA"},
{OldText: "bbb", NewText: "BBB"},
},
})
resp, err := executeEdit(t.Context(), fantasy.ToolCall{Input: string(input)}, dir)
if err != nil {
t.Fatalf("error: %v", err)
}
if resp.IsError {
t.Fatalf("tool returned error: %s", resp.Content)
}
var meta map[string]any
if err := json.Unmarshal([]byte(resp.Metadata), &meta); err != nil {
t.Fatalf("metadata is not valid JSON: %v", err)
}
diffs, ok := meta["file_diffs"].([]any)
if !ok || len(diffs) == 0 {
t.Fatal("metadata missing file_diffs")
}
firstDiff, ok := diffs[0].(map[string]any)
if !ok {
t.Fatal("first diff is not an object")
}
// Check that diff_blocks contains both edits
diffBlocks, ok := firstDiff["diff_blocks"].([]any)
if !ok || len(diffBlocks) != 2 {
t.Fatalf("expected 2 diff_blocks, got %d", len(diffBlocks))
}
// Verify each block has old_text and new_text
for i, block := range diffBlocks {
b, ok := block.(map[string]any)
if !ok {
t.Fatalf("diff_block[%d] is not an object", i)
}
if _, ok := b["old_text"]; !ok {
t.Fatalf("diff_block[%d] missing old_text", i)
}
if _, ok := b["new_text"]; !ok {
t.Fatalf("diff_block[%d] missing new_text", i)
}
}
}
+1
View File
@@ -119,6 +119,7 @@ func formatToolParams(toolArgs string, maxWidth int) string {
"new_text": true,
"oldText": true,
"newText": true,
"edits": true,
"todos": true,
}
var remaining []string
+26 -3
View File
@@ -64,21 +64,44 @@ func renderToolBody(toolName, toolArgs, toolResult string, width int) string {
// ---------------------------------------------------------------------------
// renderEditBody renders a side-by-side diff from old_text/new_text in toolArgs.
// Supports both single-edit mode and multi-edit mode (edits array).
func renderEditBody(toolArgs, toolResult string, width int) string {
var args map[string]any
if err := json.Unmarshal([]byte(toolArgs), &args); err != nil {
return ""
}
// Try to extract the starting line number from the unified diff in the result
startLine := extractDiffStartLine(toolResult)
// Check for multi-edit mode (edits array)
if editsArr, ok := args["edits"].([]any); ok && len(editsArr) > 0 {
var results []string
for _, edit := range editsArr {
if e, ok := edit.(map[string]any); ok {
oldText, _ := e["old_text"].(string)
newText, _ := e["new_text"].(string)
if oldText != "" || newText != "" {
diff := renderDiffBlock(oldText, newText, startLine, width)
if diff != "" {
results = append(results, diff)
}
}
}
}
if len(results) > 0 {
return strings.Join(results, "\n")
}
return ""
}
// Single-edit mode (legacy)
oldText, _ := args["old_text"].(string)
newText, _ := args["new_text"].(string)
if oldText == "" && newText == "" {
return ""
}
// Try to extract the starting line number from the unified diff in the result
startLine := extractDiffStartLine(toolResult)
return renderDiffBlock(oldText, newText, startLine, width)
}