Skip to content

[BUG] [v0.1.0] generate_diff_preview() produces double-prefixed diff lines (--old / ++new) for Edit tool previews #162

@climax-dev-1

Description

@climax-dev-1

Bug Description

generate_diff_preview() in cortex-tui/src/runner/event_loop/streaming.rs produces malformed unified diff output for the Edit tool. The first line of old_str gets a -- prefix and the first line of new_str gets a ++ prefix, because the format string template already includes - and + prefixes, and then the .map() closures add another - or + to every line.

Location

src/cortex-tui/src/runner/event_loop/streaming.rs, lines 889–903

Root Cause

The format string on line 889-890 is:

"--- a/{}\n+++ b/{}\n@@ Edit Preview @@\n-{}\n+{}"

The \n-{} and \n+{} already prefix the first interpolated block with - and +. But the interpolated values are built by mapping each line with an additional prefix:

old_str
    .lines()
    .map(|l| format!("-{}", l))   // adds - to EVERY line including the first
    .collect::<Vec<_>>()
    .join("\n"),
new_str
    .lines()
    .map(|l| format!("+{}", l))   // adds + to EVERY line including the first
    .collect::<Vec<_>>()
    .join("\n"),

For a single-line edit like old_str = "foo", new_str = "bar", the output is:

--- a/file.rs
+++ b/file.rs
@@ Edit Preview @@
--foo
++bar

The correct output should be:

--- a/file.rs
+++ b/file.rs
@@ Edit Preview @@
-foo
+bar

Fix

Remove the - and + from the format template so the .map() closures are the sole source of line prefixes:

let preview = format!(
    "--- a/{}\n+++ b/{}\n@@ Edit Preview @@\n{}\n{}",
    file_path,
    file_path,
    old_str.lines().map(|l| format!("-{}", l)).collect::<Vec<_>>().join("\n"),
    new_str.lines().map(|l| format!("+{}", l)).collect::<Vec<_>>().join("\n"),
);

Impact

Every Edit tool diff preview shown in the approval UI has malformed diff syntax. The first line of removed and added content is double-prefixed, making the diff harder to read and potentially confusing diff parsers or syntax highlighters that consume this output.

Version

v0.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions