diff --git a/cmd/supervisor/stages/unified_diff.go b/cmd/supervisor/stages/unified_diff.go index 53fae5d..be26b5a 100644 --- a/cmd/supervisor/stages/unified_diff.go +++ b/cmd/supervisor/stages/unified_diff.go @@ -1,6 +1,7 @@ package stages import ( + "cmp" "fmt" "regexp" "slices" @@ -29,10 +30,15 @@ type hunk struct { // is data from an agent: anything it cannot read exactly is refused. func parseUnifiedDiff(diff string) ([]filePatch, error) { var files []filePatch + block := "" // the path named by the current "diff --git" header lines := strings.Split(strings.ReplaceAll(diff, "\r\n", "\n"), "\n") for i := 0; i < len(lines); i++ { line := lines[i] switch { + case strings.HasPrefix(line, "diff --git "): + block = gitHeaderPath(line) + case strings.HasPrefix(line, "GIT binary patch") || strings.HasPrefix(line, "Binary files "): + return nil, fmt.Errorf("line %d: the patch changes binary file %s, which cannot be applied", i+1, orUnnamed(block)) case strings.HasPrefix(line, "--- "): if i+1 >= len(lines) || !strings.HasPrefix(lines[i+1], "+++ ") { return nil, fmt.Errorf("line %d: a --- header must be followed by +++", i+1) @@ -88,12 +94,41 @@ func parseUnifiedDiff(diff string) ([]filePatch, error) { files[len(files)-1].hunks = append(files[len(files)-1].hunks, h) } } - if len(files) == 0 || len(files) > maxPatchFiles { - return nil, fmt.Errorf("a patch must change between 1 and %d files", maxPatchFiles) + if len(files) == 0 { + return nil, fmt.Errorf("the patch changes no file: the agent made no change (a patch must change between 1 and %d files)", maxPatchFiles) + } + if len(files) > maxPatchFiles { + return nil, fmt.Errorf("the patch changes %d files, more than %d (first: %s)", len(files), maxPatchFiles, firstPaths(files, 5)) } return files, nil } +// gitHeaderPath is the new path of a "diff --git a/X b/Y" header, or "" when +// it cannot be read plainly. +func gitHeaderPath(line string) string { + _, b, ok := strings.Cut(strings.TrimPrefix(line, "diff --git "), " b/") + if !ok { + return "" + } + return b +} + +func orUnnamed(path string) string { + if path == "" { + return "(unnamed)" + } + return path +} + +// firstPaths names the first n files of a patch, for a refusal message. +func firstPaths(files []filePatch, n int) string { + var names []string + for _, f := range files[:min(n, len(files))] { + names = append(names, orUnnamed(cmp.Or(f.newPath, f.oldPath))) + } + return strings.Join(names, ", ") +} + func count(raw string) int { if raw == "" { return 1 diff --git a/cmd/supervisor/stages/unified_diff_test.go b/cmd/supervisor/stages/unified_diff_test.go index f9ace62..54f74b2 100644 --- a/cmd/supervisor/stages/unified_diff_test.go +++ b/cmd/supervisor/stages/unified_diff_test.go @@ -1,6 +1,10 @@ package stages -import "testing" +import ( + "fmt" + "strings" + "testing" +) func TestAUnifiedDiffIsAppliedToTheFilesItNames(t *testing.T) { t.Parallel() @@ -45,3 +49,24 @@ func TestAPatchThatDoesNotMatchOrLeavesTheRepositoryIsRefused(t *testing.T) { } } } + +// The refusal of a patch says why, so the Owner can tell an attempt that +// changed nothing from one that swept in too many or binary files. +func TestARefusedPatchSaysWhyItWasRefused(t *testing.T) { + t.Parallel() + var many strings.Builder + for i := range maxPatchFiles + 1 { + fmt.Fprintf(&many, "--- /dev/null\n+++ b/gen/f%d.txt\n@@ -0,0 +1 @@\n+x\n", i) + } + for name, c := range map[string]struct{ diff, want string }{ + "empty": {"", "the agent made no change"}, + "many": {many.String(), "changes 65 files, more than 64 (first: gen/f0.txt, gen/f1.txt"}, + "binary": {"diff --git a/img/logo.png b/img/logo.png\nindex 1..2 100644\nGIT binary patch\nliteral 3\n", "binary file img/logo.png"}, + "binary summary": {"diff --git a/a.bin b/a.bin\nBinary files a/a.bin and b/a.bin differ\n", "binary file a.bin"}, + } { + _, err := parseUnifiedDiff(c.diff) + if err == nil || !strings.Contains(err.Error(), c.want) { + t.Errorf("%s: err = %v, want it to say %q", name, err, c.want) + } + } +}