mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
fix: server-side diffs and stricter fuzzy splicing for edit_files (#24454)
Fixes three classes of edit_files bugs and adds structured per-file
diff output for tool callers:
- New IncludeDiff flag on FileEditRequest; when set, the agent
returns FileEditResponse.Files[]{Path, Diff} with unified diffs
computed via go-udiff v0.4.1 Lines + ToUnified (not Unified,
which calls log.Fatalf on internal error).
- Fuzzy match comparators split each line into leading whitespace,
body, trailing whitespace, and ending. The splice substitutes at
each position: on agreement between search and replace the file's
bytes win; on disagreement the replacement's bytes are spliced
verbatim. Carve-outs for empty-body lines, multi-line EOF splices,
and level-aware indent translation for inserted lines.
- Indent-unit detection (GCD for spaces, tab-priority) lets a 4sp
LLM search insert correctly into tab or 2sp files. Falls back to
the previous cLead-inheritance path when units can't be detected
cleanly.
- Empty search is rejected with "search string must not be empty".
- Duplicate file paths in one request are rejected; symlink aliases
resolved via api.resolvePath before the dedup check.
- Frontend EditFilesRenderer consumes the structured files array by
explicit path (no label munging) with per-file synthetic fallback
for older agents or mismatched paths. On error, no diff is
rendered so the synthetic fallback doesn't misrepresent a
rejected edit as applied.
Breaking change: AgentConn.EditFiles changes from (ctx, req) error
to (ctx, req) (FileEditResponse, error) in codersdk/workspacesdk.
Source-breaking for external Go consumers; no compat shim per plan
owner.
Out of scope (tracked in CODAGT-214): level-aware indent for
middle-substituted splice lines. Locked in
TestEditFiles_FuzzyIndent_InsertionLevelAware's Lock_* cases plus
TestEditFiles_ReplaceAll_FuzzyIndentGap.
This commit is contained in:
+30
-12
@@ -1665,7 +1665,19 @@ type WorkspaceEditFileArgs struct {
|
||||
Edits []workspacesdk.FileEdit `json:"edits"`
|
||||
}
|
||||
|
||||
var WorkspaceEditFile = Tool[WorkspaceEditFileArgs, codersdk.Response]{
|
||||
// WorkspaceEditFilesResponse is the response shape for the edit-file
|
||||
// and edit-files tools. Message preserves the existing success text.
|
||||
// Files carries the per-file results returned by the agent
|
||||
// (populated when the agent-side IncludeDiff flag was set). The
|
||||
// field is named Files (matching the agent's FileEditResponse.Files)
|
||||
// so future per-file error or status fields can be added without a
|
||||
// second wire break.
|
||||
type WorkspaceEditFilesResponse struct {
|
||||
Message string `json:"message"`
|
||||
Files []workspacesdk.FileEditResult `json:"files,omitempty"`
|
||||
}
|
||||
|
||||
var WorkspaceEditFile = Tool[WorkspaceEditFileArgs, WorkspaceEditFilesResponse]{
|
||||
Tool: aisdk.Tool{
|
||||
Name: ToolNameWorkspaceEditFile,
|
||||
Description: `Edit a file in a workspace.`,
|
||||
@@ -1703,27 +1715,29 @@ var WorkspaceEditFile = Tool[WorkspaceEditFileArgs, codersdk.Response]{
|
||||
},
|
||||
MCPAnnotations: mcpDestructiveAnnotations,
|
||||
UserClientOptional: true,
|
||||
Handler: func(ctx context.Context, deps Deps, args WorkspaceEditFileArgs) (codersdk.Response, error) {
|
||||
Handler: func(ctx context.Context, deps Deps, args WorkspaceEditFileArgs) (WorkspaceEditFilesResponse, error) {
|
||||
conn, err := newAgentConn(ctx, deps.coderClient, args.Workspace)
|
||||
if err != nil {
|
||||
return codersdk.Response{}, err
|
||||
return WorkspaceEditFilesResponse{}, err
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
err = conn.EditFiles(ctx, workspacesdk.FileEditRequest{
|
||||
resp, err := conn.EditFiles(ctx, workspacesdk.FileEditRequest{
|
||||
Files: []workspacesdk.FileEdits{
|
||||
{
|
||||
Path: args.Path,
|
||||
Edits: args.Edits,
|
||||
},
|
||||
},
|
||||
IncludeDiff: true,
|
||||
})
|
||||
if err != nil {
|
||||
return codersdk.Response{}, err
|
||||
return WorkspaceEditFilesResponse{}, err
|
||||
}
|
||||
|
||||
return codersdk.Response{
|
||||
return WorkspaceEditFilesResponse{
|
||||
Message: "File edited successfully.",
|
||||
Files: resp.Files,
|
||||
}, nil
|
||||
},
|
||||
}
|
||||
@@ -1733,7 +1747,7 @@ type WorkspaceEditFilesArgs struct {
|
||||
Files []workspacesdk.FileEdits `json:"files"`
|
||||
}
|
||||
|
||||
var WorkspaceEditFiles = Tool[WorkspaceEditFilesArgs, codersdk.Response]{
|
||||
var WorkspaceEditFiles = Tool[WorkspaceEditFilesArgs, WorkspaceEditFilesResponse]{
|
||||
Tool: aisdk.Tool{
|
||||
Name: ToolNameWorkspaceEditFiles,
|
||||
Description: `Edit one or more files in a workspace.`,
|
||||
@@ -1785,20 +1799,24 @@ var WorkspaceEditFiles = Tool[WorkspaceEditFilesArgs, codersdk.Response]{
|
||||
},
|
||||
MCPAnnotations: mcpDestructiveAnnotations,
|
||||
UserClientOptional: true,
|
||||
Handler: func(ctx context.Context, deps Deps, args WorkspaceEditFilesArgs) (codersdk.Response, error) {
|
||||
Handler: func(ctx context.Context, deps Deps, args WorkspaceEditFilesArgs) (WorkspaceEditFilesResponse, error) {
|
||||
conn, err := newAgentConn(ctx, deps.coderClient, args.Workspace)
|
||||
if err != nil {
|
||||
return codersdk.Response{}, err
|
||||
return WorkspaceEditFilesResponse{}, err
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
err = conn.EditFiles(ctx, workspacesdk.FileEditRequest{Files: args.Files})
|
||||
resp, err := conn.EditFiles(ctx, workspacesdk.FileEditRequest{
|
||||
Files: args.Files,
|
||||
IncludeDiff: true,
|
||||
})
|
||||
if err != nil {
|
||||
return codersdk.Response{}, err
|
||||
return WorkspaceEditFilesResponse{}, err
|
||||
}
|
||||
|
||||
return codersdk.Response{
|
||||
return WorkspaceEditFilesResponse{
|
||||
Message: "File(s) edited successfully.",
|
||||
Files: resp.Files,
|
||||
}, nil
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user