Skip to content

Conversation

@KJ7LNW
Copy link
Contributor

@KJ7LNW KJ7LNW commented May 27, 2025

Context

This pull request addresses an issue where :start_line: and :end_line: markers could be erroneously included in the REPLACE section of an apply_diff operation. These markers are intended only for the SEARCH section to specify the target lines for modification. Including them in the REPLACE section could lead to unexpected behavior or errors.

This change ensures that apply_diff operations are more robust by validating the placement of these markers.

Fixes: #4013

Implementation

The MultiSearchReplaceDiffStrategy in src/core/diff/strategies/multi-search-replace.ts has been updated to include a validation step within the validateMarkerSequencing method. This new validation specifically checks if :start_line: or :end_line: markers (when not escaped) are present in the content between ======= and >>>>>>> REPLACE.

If such markers are found, the diff operation will fail with an informative error message guiding the user on the correct format and the allowed placement of these markers.

Corresponding unit tests have been added to src/core/diff/strategies/__tests__/multi-search-replace.test.ts to verify this new validation logic, ensuring that invalid placements are rejected and valid placements (including escaped markers in the REPLACE section) are correctly processed.

How to Test

The core functionality can be tested by attempting apply_diff operations:

  1. With :start_line: or :end_line: incorrectly placed in the REPLACE section (should fail with an error).
  2. With these markers correctly placed in the SEARCH section (should succeed).
  3. With escaped line markers (e.g., \:start_line:) in the REPLACE section (should succeed, treating them as literal text).

Get in Touch

Discord: KJ7LNW


Important

Adds validation to prevent :start_line: and :end_line: markers in REPLACE section of apply_diff, with tests to verify behavior.

  • Behavior:
    • Prevents :start_line: and :end_line: markers in REPLACE section of apply_diff.
    • Adds validation in validateMarkerSequencing() in multi-search-replace.ts.
    • If markers are found, operation fails with error message.
  • Tests:
    • Adds tests in multi-search-replace.test.ts to verify validation logic.
    • Tests include cases for invalid and valid marker placements, including escaped markers.

This description was created by Ellipsis for 3f86541. You can customize this summary. It will automatically update as commits are pushed.

Adds validation to ensure that `:start_line:` and `:end_line:`
markers do not appear in the REPLACE section of an apply_diff
operation. These markers are only valid within the SEARCH section.

This change prevents potential errors and confusion when users
might inadvertently include these markers in the replacement content.
New tests have been added to verify this validation.

Fixes: #4013
Signed-off-by: Eric Wheeler <[email protected]>
@KJ7LNW KJ7LNW requested review from cte and mrubens as code owners May 27, 2025 00:50
@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label May 27, 2025
@daniel-lxs
Copy link
Member

Hey @KJ7LNW, is there a reason to prevent the tool from executing instead of just removing the incorrect :start_line: or :end_line:?

We already handle some errors by the models this way, for example, when the closing tag of a tool call is omitted, does it make sense to do the same here? Maybe in the future we can call a tool but also provide feedback to the model at the same time.

@daniel-lxs daniel-lxs moved this from Triage to PR [Changes Requested] in Roo Code Roadmap May 28, 2025
@daniel-lxs daniel-lxs added PR - Changes Requested and removed Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. labels May 28, 2025
@KJ7LNW
Copy link
Contributor Author

KJ7LNW commented May 29, 2025

Hey @KJ7LNW, is there a reason to prevent the tool from executing instead of just removing the incorrect :start_line: or :end_line:?

We already handle some errors by the models this way, for example, when the closing tag of a tool call is omitted, does it make sense to do the same here? Maybe in the future we can call a tool but also provide feedback to the model at the same time.

This pull requests fixes an unlikely event. It happens so rarely that I do not think it justifies intentionally breaking data integrity. This is why:

When designing these tools we should always support creation of any arbitrary string: what if it really needs to create :end_line: for some unknown future use case by some random developer? For that case the model must receive feedback and be told to use \:end_line:, otherwise it would never be able to perform the action.

@daniel-lxs
Copy link
Member

daniel-lxs commented May 29, 2025

For that case the model must receive feedback and be told to use \:end_line:, otherwise it would never be able to perform the action.

I agree, this looks good to me then, since I've had this happen a couple of times with Gemini.

@daniel-lxs daniel-lxs moved this from PR [Changes Requested] to PR [Needs Review] in Roo Code Roadmap May 29, 2025
@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label May 31, 2025
@mrubens mrubens merged commit 53f94a7 into RooCodeInc:main May 31, 2025
21 checks passed
@github-project-automation github-project-automation bot moved this from New to Done in Roo Code Roadmap May 31, 2025
@github-project-automation github-project-automation bot moved this from PR [Needs Review] to Done in Roo Code Roadmap May 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm This PR has been approved by a maintainer PR - Needs Review

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

bug: apply_diff mishandles :start_line: markers in REPLACE section

4 participants