Merge pull request #1610 from qodo-ai/tr/prompts_enhancment

fix: validate one-liner suggestions to prevent repeating existing code
This commit is contained in:
Tal
2025-03-06 08:42:15 +02:00
committed by GitHub
3 changed files with 33 additions and 4 deletions

View File

@ -89,7 +89,7 @@ class PRAgent:
action = action.lstrip("/").lower()
if action not in command2class:
get_logger().error(f"Unknown command: {action}")
get_logger().warning(f"Unknown command: {action}")
return False
with get_logger().contextualize(command=action, pr_url=pr_url):
get_logger().info("PR-Agent request handler started", analytics=True)

View File

@ -83,8 +83,8 @@ The output must be a YAML object equivalent to type $PRCodeSuggestions, accordin
class CodeSuggestion(BaseModel):
relevant_file: str = Field(description="Full path of the relevant file")
language: str = Field(description="Programming language used by the relevant file")
suggestion_content: str = Field(description="An actionable suggestion to enhance, improve or fix the new code introduced in the PR. Don't present here actual code snippets, just the suggestion. Be short and concise")
existing_code: str = Field(description="A short code snippet, from a '__new hunk__' section after the PR changes, that the suggestion aims to enhance or fix. Include only complete code lines. Use ellipsis (...) for brevity if needed. This snippet should represent the specific PR code targeted for improvement.")
suggestion_content: str = Field(description="An actionable suggestion to enhance, improve or fix the new code introduced in the PR. Don't present here actual code snippets, just the suggestion. Be short and concise")
improved_code: str = Field(description="A refined code snippet that replaces the 'existing_code' snippet after implementing the suggestion.")
one_sentence_summary: str = Field(description="A concise, single-sentence overview (up to 6 words) of the suggested improvement. Focus on the 'what'. Be general, and avoid method or variable names.")
{%- if not focus_only_on_problems %}
@ -106,10 +106,10 @@ code_suggestions:
src/file1.py
language: |
python
suggestion_content: |
...
existing_code: |
...
suggestion_content: |
...
improved_code: |
...
one_sentence_summary: |

View File

@ -466,6 +466,8 @@ class PRCodeSuggestions:
suggestion["score"] = 7
suggestion["score_why"] = ""
suggestion = self.validate_one_liner_suggestion_not_repeating_code(suggestion)
# if the before and after code is the same, clear one of them
try:
if suggestion['existing_code'] == suggestion['improved_code']:
@ -631,6 +633,33 @@ class PRCodeSuggestions:
return True
return False
def validate_one_liner_suggestion_not_repeating_code(self, suggestion):
try:
existing_code = suggestion.get('existing_code', '').strip()
if '...' in existing_code:
return suggestion
new_code = suggestion.get('improved_code', '').strip()
relevant_file = suggestion.get('relevant_file', '').strip()
diff_files = self.git_provider.get_diff_files()
for file in diff_files:
if file.filename.strip() == relevant_file:
# protections
if not file.head_file:
get_logger().info(f"head_file is empty")
return suggestion
head_file = file.head_file
base_file = file.base_file
if existing_code in base_file and existing_code not in head_file and new_code in head_file:
suggestion["score"] = 0
get_logger().warning(
f"existing_code is in the base file but not in the head file, setting score to 0",
artifact={"suggestion": suggestion})
except Exception as e:
get_logger().exception(f"Error validating one-liner suggestion", artifact={"error": e})
return suggestion
def remove_line_numbers(self, patches_diff_list: List[str]) -> List[str]:
# create a copy of the patches_diff_list, without line numbers for '__new hunk__' sections
try: