Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supports updating multiple locations of a file in one call of the apply_diff tool #1234

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

qdaxb
Copy link

@qdaxb qdaxb commented Feb 27, 2025

Description

apply_diff tool (search/replace) seems to only be able to modify a continuous text at a time. Some changes require modifying multiple places in the file at once, which can only be achieved through multiple rounds of interaction.

This pr attempts to implement the function of updating multiple locations of a file in one apply_diff tool call by modifying the format of the apply_diff tool.

Tested with DeepSeek Model.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Checklist:

  • My code follows the patterns of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation

Additional context

changes:

  1. modify the parameters of <apply_diff>, remove start_line and end_line parameter.
  2. modify the format of diff format, it's now likes:
<apply_diff>
<path>xxx</path>
<diff>
<<<<<<< SEARCH
:start_line:1
:end_line:2
------------
def calculate_sum(items):
    sum = 0
=======
def calculate_sum(items):
    sum = 0
>>>>>>> REPLACE

<<<<<<< SEARCH
:start_line:4
:end_line:5
------------
        total += item
    return total
=======
        sum += item
    return sum 
>>>>>>> REPLACE
</diff>
</apply_diff>
  1. update regex
  2. Support applying only the valid parts when apply_diff returns multiple changes at once. Add fail_parts field in DiffResult to record these failures.
  3. If failure happens, prompt the model to re-read the file and retry.
  4. Update buffer line count to 40.

result:
image
(Ignore the prompt word format, I was still testing it at the time)

Related Issues

Reviewers


Important

Enhances applyDiff in search-replace.ts to support multiple search/replace operations in one call by modifying the diff format.

  • New Feature:
    • applyDiff in search-replace.ts now supports multiple search/replace operations in one call.
    • Modifies diff format to include multiple search/replace blocks with line numbers.
  • Functionality:
    • Iterates over matched blocks, applying replacements sequentially.
    • Updates tool description and usage examples to reflect new capability.

This description was created by Ellipsis for 7941474. It will automatically update as commits are pushed.

@qdaxb qdaxb requested review from mrubens and cte as code owners February 27, 2025 07:28
Copy link

changeset-bot bot commented Feb 27, 2025

⚠️ No Changeset found

Latest commit: 7fe0172

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Feb 27, 2025
startLine?: number,
endLine?: number,
_paramStartLine?: number,
_paramEndLIne?: number,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parameter naming: The parameter _paramEndLIne is inconsistently cased (should be _paramEndLine). Please fix for consistency with our coding standards.

Suggested change
_paramEndLIne?: number,
_paramEndLine?: number,

replaceContent: match[3],
endLine: Number(match[4]),
}))
.sort((a, b) => a.startLine - b.startLine)
// Detect line ending from original content
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overlap validation: Replacements are sorted by startLine, but there’s no check for overlapping or adjacent diff blocks. Consider validating to prevent unintended behavior.

bestMatchScore = similarity
matchIndex = leftIndex
bestMatchContent = originalChunk
// If no match found yet, try middle-out search within bounds
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modularize fuzzy matching logic: The search and error construction code (lines ~204–265) is quite dense. Extracting this into helper functions would improve readability and testability.

@dosubot dosubot bot added the enhancement New feature or request label Feb 27, 2025
@samhvw8
Copy link
Collaborator

samhvw8 commented Feb 27, 2025

cool

@qdaxb qdaxb force-pushed the support_edit_multi_locations_in_apply_diff branch 2 times, most recently from b2d4caa to 17aa4ae Compare February 28, 2025 05:24
@qdaxb qdaxb force-pushed the support_edit_multi_locations_in_apply_diff branch from 17aa4ae to 7fe0172 Compare February 28, 2025 15:18
@qdaxb qdaxb changed the title [draft]Supports updating multiple locations of a file in one call of the apply_diff tool Supports updating multiple locations of a file in one call of the apply_diff tool Feb 28, 2025
@qdaxb
Copy link
Author

qdaxb commented Feb 28, 2025

@mrubens I think we can see if the idea is ok first. If this idea works, I will try to add a new strategy class and corresponding experiments, and update the test cases.

I did some testing on some files > 1000 lines and DeepSeek models, and the results seem to be ok.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants