mirror of
https://github.com/coder/coder.git
synced 2026-06-05 14:08:20 +00:00
6b0bb02e5d
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.
111 lines
3.2 KiB
Go
111 lines
3.2 KiB
Go
package chattool
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"charm.land/fantasy"
|
|
|
|
"github.com/coder/coder/v2/codersdk/workspacesdk"
|
|
)
|
|
|
|
type EditFilesOptions struct {
|
|
GetWorkspaceConn func(context.Context) (workspacesdk.AgentConn, error)
|
|
ResolvePlanPath func(context.Context) (chatPath string, home string, err error)
|
|
IsPlanTurn bool
|
|
}
|
|
|
|
type EditFilesArgs struct {
|
|
Files []workspacesdk.FileEdits `json:"files"`
|
|
}
|
|
|
|
func EditFiles(options EditFilesOptions) fantasy.AgentTool {
|
|
return fantasy.NewAgentTool(
|
|
"edit_files",
|
|
"Perform search-and-replace edits on one or more files in the workspace."+
|
|
" Each file can have multiple edits applied atomically.",
|
|
func(ctx context.Context, args EditFilesArgs, _ fantasy.ToolCall) (fantasy.ToolResponse, error) {
|
|
var planPath string
|
|
if options.IsPlanTurn && len(args.Files) > 0 {
|
|
resolvedPlanPath, err := resolvePlanTurnPath(ctx, options.ResolvePlanPath)
|
|
if err != nil {
|
|
return fantasy.NewTextErrorResponse(err.Error()), nil
|
|
}
|
|
for i := range args.Files {
|
|
args.Files[i].Path = strings.TrimSpace(args.Files[i].Path)
|
|
if args.Files[i].Path != resolvedPlanPath {
|
|
return fantasy.NewTextErrorResponse("during plan turns, edit_files is restricted to " + resolvedPlanPath), nil
|
|
}
|
|
}
|
|
planPath = resolvedPlanPath
|
|
}
|
|
if options.GetWorkspaceConn == nil {
|
|
return fantasy.NewTextErrorResponse("workspace connection resolver is not configured"), nil
|
|
}
|
|
conn, err := options.GetWorkspaceConn(ctx)
|
|
if err != nil {
|
|
return fantasy.NewTextErrorResponse(err.Error()), nil
|
|
}
|
|
if planPath != "" {
|
|
if err := ensurePlanPathResolvesToItself(ctx, conn, planPath); err != nil {
|
|
return fantasy.NewTextErrorResponse(err.Error()), nil
|
|
}
|
|
}
|
|
return executeEditFilesTool(ctx, conn, args, options.ResolvePlanPath)
|
|
},
|
|
)
|
|
}
|
|
|
|
func executeEditFilesTool(
|
|
ctx context.Context,
|
|
conn workspacesdk.AgentConn,
|
|
args EditFilesArgs,
|
|
resolvePlanPath func(context.Context) (chatPath string, home string, err error),
|
|
) (fantasy.ToolResponse, error) {
|
|
if len(args.Files) == 0 {
|
|
return fantasy.NewTextErrorResponse("files is required"), nil
|
|
}
|
|
|
|
var (
|
|
chatPath string
|
|
home string
|
|
planPathErr error
|
|
planPathLoaded bool
|
|
)
|
|
for i := range args.Files {
|
|
args.Files[i].Path = strings.TrimSpace(args.Files[i].Path)
|
|
file := args.Files[i]
|
|
|
|
hasPlanFileName := looksLikePlanFileName(file.Path)
|
|
if hasPlanFileName && !isAbsolutePath(file.Path) {
|
|
return fantasy.NewTextErrorResponse(
|
|
"plan files must use absolute paths; use the chat-specific absolute plan path; no files in this batch were applied",
|
|
), nil
|
|
}
|
|
if resolvePlanPath == nil || !hasPlanFileName {
|
|
continue
|
|
}
|
|
if !planPathLoaded {
|
|
chatPath, home, planPathErr = resolvePlanPath(ctx)
|
|
planPathLoaded = true
|
|
}
|
|
if resp, rejected := rejectSharedPlanPath(file.Path, home, chatPath, planPathErr); rejected {
|
|
return fantasy.NewTextErrorResponse(
|
|
resp.Content + "; no files in this batch were applied",
|
|
), nil
|
|
}
|
|
}
|
|
|
|
resp, err := conn.EditFiles(ctx, workspacesdk.FileEditRequest{
|
|
Files: args.Files,
|
|
IncludeDiff: true,
|
|
})
|
|
if err != nil {
|
|
return fantasy.NewTextErrorResponse(err.Error()), nil
|
|
}
|
|
return toolResponse(map[string]any{
|
|
"ok": true,
|
|
"files": resp.Files,
|
|
}), nil
|
|
}
|