The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu
Go agent-ai-cliagent-harnessesagent-securitybrowser-extensionciclaude-codecloudcodexcursordetection-engineeringjevknowledge-basememorymobile-device-managementobservabilitysecuritysecurity-information-and-event-managementsecurity-toolstelemetrytraces
fix(hooks): stop reconstructed Write diffs adding a phantom final line (#827)
Rudy Mizrahi Celeklib76b2eda36bb
diff --git a/cli/beacon-hooks/internal/diff/diff.go b/cli/beacon-hooks/internal/diff/diff.goindex 9a5b5576..b4601f1d 100644--- a/cli/beacon-hooks/internal/diff/diff.go+++ b/cli/beacon-hooks/internal/diff/diff.go@@ -282,37 +282,36 @@ func fromWriteTool(filePath, content, originalFile string) string {filename := filepath.Base(filePath)newLines := splitLines(content)oldLines := splitLines(originalFile)-- lines := []string{- fmt.Sprintf("--- a/%s", filename),- fmt.Sprintf("+++ b/%s", filename),- }-+ lines := []string{fmt.Sprintf("--- a/%s", filename), fmt.Sprintf("+++ b/%s", filename)}if originalFile != "" {- // This is an update - show as replacementlines = append(lines, fmt.Sprintf("@@ -1,%d +1,%d @@", len(oldLines), len(newLines)))- for _, line := range oldLines {- lines = append(lines, "-"+line)- }- for _, line := range newLines {- lines = append(lines, "+"+line)- }+ lines = appendWholeFileLines(lines, oldLines, originalFile, "-")} else {- // This is a new filelines = append(lines, fmt.Sprintf("@@ -0,0 +1,%d @@", len(newLines)))- for _, line := range newLines {- lines = append(lines, "+"+line)- }}-+ lines = appendWholeFileLines(lines, newLines, content, "+")return strings.Join(lines, "\n")}+// Whole-file observations establish EOF; edit fragments do not. Emit the unified+// diff marker only here so an unterminated replaced span is not mistaken for EOF.+func appendWholeFileLines(lines, fileLines []string, content, prefix string) []string {+ for _, line := range fileLines {+ lines = append(lines, prefix+line)+ }+ if content != "" && !strings.HasSuffix(content, "\n") {+ lines = append(lines, "\\ No newline at end of file")+ }+ return lines+}+func splitLines(s string) []string {if s == "" {return []string{}}- return strings.Split(s, "\n")+ // A trailing newline terminates the last line; it is not another blank line.+ // Remove just one terminator so actual trailing blank lines remain.+ return strings.Split(strings.TrimSuffix(s, "\n"), "\n")}// NormalizePath strips surrounding double quotes and whitespace from a path string.diff --git a/cli/beacon-hooks/internal/diff/diff_test.go b/cli/beacon-hooks/internal/diff/diff_test.goindex 3fa3405d..25282c2c 100644--- a/cli/beacon-hooks/internal/diff/diff_test.go+++ b/cli/beacon-hooks/internal/diff/diff_test.go@@ -391,3 +391,34 @@ func TestFromEditFragmentsReturnsNothingForAnUnchangedSpan(t *testing.T) {t.Fatalf("FromEditFragments = %q, want empty without a path", got)}}++// A whole-file Write diff carries the file's exact line boundaries: a terminating newline is not+// an extra blank line, real trailing blank lines stay, and unterminated content gets the unified+// diff EOF marker.+func TestWholeFileWriteDiffPreservesFinalLineBoundaries(t *testing.T) {+ const header = "--- a/result.txt\n+++ b/result.txt\n"+ const eof = "\\ No newline at end of file"+ for _, tc := range []struct{ name, before, after, want string }{+ {"create terminated", "", "new\n", "@@ -0,0 +1,1 @@\n+new"},+ {"create with trailing blank line", "", "new\n\n", "@@ -0,0 +1,2 @@\n+new\n+"},+ {"create a lone newline", "", "\n", "@@ -0,0 +1,1 @@\n+"},+ {"create unterminated", "", "new", "@@ -0,0 +1,1 @@\n+new\n" + eof},+ {"replace both unterminated", "old", "new", "@@ -1,1 +1,1 @@\n-old\n" + eof + "\n+new\n" + eof},+ {"remove final newline", "same\n", "same", "@@ -1,1 +1,1 @@\n-same\n+same\n" + eof},+ {"add final newline", "same", "same\n", "@@ -1,1 +1,1 @@\n-same\n" + eof + "\n+same"},+ {"replace trailing blank line", "old\n\n", "new", "@@ -1,2 +1,1 @@\n-old\n-\n+new\n" + eof},+ } {+ t.Run(tc.name, func(t *testing.T) {+ got := FromToolResponse("Write", map[string]interface{}{"file_path": "/repo/result.txt", "content": tc.after}, map[string]interface{}{"originalFile": tc.before})+ if want := header + tc.want; got != want {+ t.Fatalf("diff:\n%s\nwant:\n%s", got, want)+ }+ })+ }+}++func TestEditFragmentsDoNotClaimEndOfFile(t *testing.T) {+ if got := FromEditFragments("/repo/result.txt", "old", "new"); strings.Contains(got, "\\ No newline at end of file") {+ t.Fatalf("partial edit claims EOF: %s", got)+ }+}