Skip to content

Commit

Permalink
Merge pull request qodo-ai#1218 from Codium-ai/tr/updates_and_fixes
Browse files Browse the repository at this point in the history
feat: enhance error handling and logging, update AI metadata terminology
  • Loading branch information
mrT23 authored Sep 10, 2024
2 parents 74f9da1 + 1451d82 commit cc1b65f
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 41 deletions.
2 changes: 1 addition & 1 deletion docs/docs/core-abilities/metadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ For example, when generating code suggestions for different files, PR-Agent can

```
## File: 'src/file1.py'
### AI-generated file summary:
### AI-generated changes summary:
- edited function `func1` that does X
- Removed function `func2` that was not used
- ....
Expand Down
55 changes: 34 additions & 21 deletions pr_agent/algo/pr_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,27 +496,40 @@ def add_ai_metadata_to_diff_files(git_provider, pr_description_files):
"""
Adds AI metadata to the diff files based on the PR description files (FilePatchInfo.ai_file_summary).
"""
diff_files = git_provider.get_diff_files()
for file in diff_files:
filename = file.filename.strip()
found = False
for pr_file in pr_description_files:
if filename == pr_file['full_file_name'].strip():
file.ai_file_summary = pr_file
found = True
break
if not found:
get_logger().info(f"File {filename} not found in the PR description files",
artifacts=pr_description_files)
try:
if not pr_description_files:
get_logger().warning(f"PR description files are empty.")
return
available_files = {pr_file['full_file_name'].strip(): pr_file for pr_file in pr_description_files}
diff_files = git_provider.get_diff_files()
found_any_match = False
for file in diff_files:
filename = file.filename.strip()
if filename in available_files:
file.ai_file_summary = available_files[filename]
found_any_match = True
if not found_any_match:
get_logger().error(f"Failed to find any matching files between PR description and diff files.",
artifact={"pr_description_files": pr_description_files})
except Exception as e:
get_logger().error(f"Failed to add AI metadata to diff files: {e}",
artifact={"traceback": traceback.format_exc()})


def add_ai_summary_top_patch(file, full_extended_patch):
# below every instance of '## File: ...' in the patch, add the ai-summary metadata
full_extended_patch_lines = full_extended_patch.split("\n")
for i, line in enumerate(full_extended_patch_lines):
if line.startswith("## File:") or line.startswith("## file:"):
full_extended_patch_lines.insert(i + 1,
f"### AI-generated file summary:\n{file.ai_file_summary['long_summary']}")
break
full_extended_patch = "\n".join(full_extended_patch_lines)
return full_extended_patch
try:
# below every instance of '## File: ...' in the patch, add the ai-summary metadata
full_extended_patch_lines = full_extended_patch.split("\n")
for i, line in enumerate(full_extended_patch_lines):
if line.startswith("## File:") or line.startswith("## file:"):
full_extended_patch_lines.insert(i + 1,
f"### AI-generated changes summary:\n{file.ai_file_summary['long_summary']}")
full_extended_patch = "\n".join(full_extended_patch_lines)
return full_extended_patch

# if no '## File: ...' was found
return full_extended_patch
except Exception as e:
get_logger().error(f"Failed to add AI summary to the top of the patch: {e}",
artifact={"traceback": traceback.format_exc()})
return full_extended_patch
10 changes: 6 additions & 4 deletions pr_agent/git_providers/azuredevops_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ def get_diff_files(self) -> list[FilePatchInfo]:

new_file_content_str = new_file_content_str.content
except Exception as error:
get_logger().error(f"Failed to retrieve new file content of {file} at version {version}. Error: {str(error)}")
get_logger().error(f"Failed to retrieve new file content of {file} at version {version}", error=error)
# get_logger().error(
# "Failed to retrieve new file content of %s at version %s. Error: %s",
# file,
Expand Down Expand Up @@ -347,7 +347,7 @@ def get_diff_files(self) -> list[FilePatchInfo]:
)
original_file_content_str = original_file_content_str.content
except Exception as error:
get_logger().error(f"Failed to retrieve original file content of {file} at version {version}. Error: {str(error)}")
get_logger().error(f"Failed to retrieve original file content of {file} at version {version}", error=error)
original_file_content_str = ""

patch = load_large_diff(
Expand Down Expand Up @@ -375,7 +375,7 @@ def get_diff_files(self) -> list[FilePatchInfo]:
self.diff_files = diff_files
return diff_files
except Exception as e:
print(f"Error: {str(e)}")
get_logger().exception(f"Failed to get diff files, error: {e}")
return []

def publish_comment(self, pr_comment: str, is_temporary: bool = False, thread_context=None):
Expand Down Expand Up @@ -519,7 +519,6 @@ def get_pr_branch(self):
def get_user_id(self):
return 0


def get_issue_comments(self):
threads = self.azure_devops_client.get_threads(repository_id=self.repo_slug, pull_request_id=self.pr_num, project=self.workspace_slug)
threads.reverse()
Expand Down Expand Up @@ -614,3 +613,6 @@ def get_pr_id(self):
get_logger().error(f"Failed to get pr id, error: {e}")
return ""

def publish_file_comments(self, file_comments: list) -> bool:
pass

22 changes: 13 additions & 9 deletions pr_agent/servers/github_polling.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,14 @@ async def is_valid_notification(notification, headers, handled_ids, session, use
if not comment_body:
get_logger().debug(f"no comment_body")
check_prev_comments = True
commenter_github_user = comment['user']['login'] \
if 'user' in comment else ''
get_logger().info(f"Polling, pr_url: {pr_url}",
artifact={"comment": comment_body})
user_tag = "@" + user_id
if user_tag not in comment_body:
get_logger().debug(f"user_tag not in comment_body")
check_prev_comments = True
else:
user_tag = "@" + user_id
if user_tag not in comment_body:
get_logger().debug(f"user_tag not in comment_body")
check_prev_comments = True
else:
get_logger().info(f"Polling, pr_url: {pr_url}",
artifact={"comment": comment_body})

if not check_prev_comments:
return True, handled_ids, comment, comment_body, pr_url, user_tag
Expand All @@ -125,6 +125,8 @@ async def is_valid_notification(notification, headers, handled_ids, session, use
continue
if user_tag in comment_body:
get_logger().info("found user tag in previous comments")
get_logger().info(f"Polling, pr_url: {pr_url}",
artifact={"comment": comment_body})
return True, handled_ids, comment, comment_body, pr_url, user_tag

get_logger().error(f"Failed to fetch comments for PR: {pr_url}")
Expand Down Expand Up @@ -188,6 +190,8 @@ async def polling_loop():
get_logger().info(f"Received {len(notifications)} notifications")
task_queue = deque()
for notification in notifications:
if not notification:
continue
# mark notification as read
await mark_notification_as_read(headers, notification, session)

Expand All @@ -204,7 +208,7 @@ async def polling_loop():
task_queue.append((process_comment_sync, (pr_url, rest_of_comment, comment_id)))
get_logger().info(f"Queued comment processing for PR: {pr_url}")
else:
get_logger().debug(f"Skipping comment processing for PR: {pr_url}")
get_logger().debug(f"Skipping comment processing for PR")

max_allowed_parallel_tasks = 10
if task_queue:
Expand Down
8 changes: 4 additions & 4 deletions pr_agent/settings/pr_code_suggestions_prompts.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ The format we will use to present the PR code diff:
======
## File: 'src/file1.py'
{%- if is_ai_metadata %}
### AI-generated file summary:
### AI-generated changes summary:
* ...
* ...
{%- endif %}
Expand Down Expand Up @@ -39,7 +39,7 @@ __old hunk__
- We also added line numbers for the '__new hunk__' code, to help you refer to the code lines in your suggestions. These line numbers are not part of the actual code, and should only used for reference.
- Code lines are prefixed with symbols ('+', '-', ' '). The '+' symbol indicates new code added in the PR, the '-' symbol indicates code removed in the PR, and the ' ' symbol indicates unchanged code. \
{%- if is_ai_metadata %}
- If available, an AI-generated summary will appear and provide a high-level overview of the file changes.
- If available, an AI-generated summary will appear and provide a high-level overview of the file changes. Note that this summary may not be fully accurate or complete.
{%- endif %}
Specific instructions for generating code suggestions:
Expand Down Expand Up @@ -131,7 +131,7 @@ The format we will use to present the PR code diff:
======
## File: 'src/file1.py'
{%- if is_ai_metadata %}
### AI-generated file summary:
### AI-generated changes summary:
* ...
* ...
{%- endif %}
Expand Down Expand Up @@ -163,7 +163,7 @@ __old hunk__
- We also added line numbers for the '__new hunk__' code, to help you refer to the code lines in your suggestions. These line numbers are not part of the actual code, and should only used for reference.
- Code lines are prefixed with symbols ('+', '-', ' '). The '+' symbol indicates new code added in the PR, the '-' symbol indicates code removed in the PR, and the ' ' symbol indicates unchanged code. \
{%- if is_ai_metadata %}
- If available, an AI-generated summary will appear and provide a high-level overview of the file changes.
- If available, an AI-generated summary will appear and provide a high-level overview of the file changes. Note that this summary may not be fully accurate or complete.
{%- endif %}
Specific instructions for generating code suggestions:
Expand Down
4 changes: 2 additions & 2 deletions pr_agent/settings/pr_reviewer_prompts.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ The format we will use to present the PR code diff:
======
## File: 'src/file1.py'
{%- if is_ai_metadata %}
### AI-generated file summary:
### AI-generated changes summary:
* ...
* ...
{%- endif %}
Expand Down Expand Up @@ -46,7 +46,7 @@ __old hunk__
- Code lines are prefixed with symbols ('+', '-', ' '). The '+' symbol indicates new code added in the PR, the '-' symbol indicates code removed in the PR, and the ' ' symbol indicates unchanged code. \
The review should address new code added in the PR code diff (lines starting with '+')
{%- if is_ai_metadata %}
- If available, an AI-generated summary will appear and provide a high-level overview of the file changes.
- If available, an AI-generated summary will appear and provide a high-level overview of the file changes. Note that this summary may not be fully accurate or complete.
{%- endif %}
- When quoting variables or names from the code, use backticks (`) instead of single quote (').
Expand Down

0 comments on commit cc1b65f

Please sign in to comment.