Skip to content

Commit

Permalink
protections
Browse files Browse the repository at this point in the history
  • Loading branch information
mrT23 committed Aug 9, 2024
1 parent 89e9413 commit e531245
Showing 1 changed file with 42 additions and 15 deletions.
57 changes: 42 additions & 15 deletions pr_agent/tools/pr_description.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ async def run(self):
pr_body += "<hr>\n\n<details> <summary><strong>✨ Describe tool usage guide:</strong></summary><hr> \n\n"
pr_body += HelpMessage.get_describe_usage_guide()
pr_body += "\n</details>\n"
elif get_settings().pr_description.enable_help_comment:
elif self.git_provider.is_supported("gfm_markdown") and get_settings().pr_description.enable_help_comment:

Check warning on line 120 in pr_agent/tools/pr_description.py

View check run for this annotation

Codecov / codecov/patch

pr_agent/tools/pr_description.py#L120

Added line #L120 was not covered by tests
pr_body += "\n\n___\n\n> 💡 **PR-Agent usage**:"
pr_body += "\n>Comment `/help` on the PR to get a list of all available PR-Agent tools and their descriptions\n\n"

Expand Down Expand Up @@ -167,11 +167,13 @@ async def run(self):

async def _prepare_prediction(self, model: str) -> None:
if get_settings().pr_description.use_description_markers and 'pr_agent:' not in self.user_description:
get_logger().info("Markers were enabled, but user description does not contain markers. skipping AI prediction")
get_logger().info(

Check warning on line 170 in pr_agent/tools/pr_description.py

View check run for this annotation

Codecov / codecov/patch

pr_agent/tools/pr_description.py#L170

Added line #L170 was not covered by tests
"Markers were enabled, but user description does not contain markers. skipping AI prediction")
return None

large_pr_handling = get_settings().pr_description.enable_large_pr_handling and "pr_description_only_files_prompts" in get_settings()
output = get_pr_diff(self.git_provider, self.token_handler, model, large_pr_handling=large_pr_handling, return_remaining_files=True)
output = get_pr_diff(self.git_provider, self.token_handler, model, large_pr_handling=large_pr_handling,

Check warning on line 175 in pr_agent/tools/pr_description.py

View check run for this annotation

Codecov / codecov/patch

pr_agent/tools/pr_description.py#L175

Added line #L175 was not covered by tests
return_remaining_files=True)
if isinstance(output, tuple):
patches_diff, remaining_files_list = output
else:
Expand Down Expand Up @@ -214,11 +216,12 @@ async def _prepare_prediction(self, model: str) -> None:
else: # async calls
tasks = []
for i, patches in enumerate(patches_compressed_list):
patches_diff = "\n".join(patches)
get_logger().debug(f"PR diff number {i + 1} for describe files")
task = asyncio.create_task(
self._get_prediction(model, patches_diff, prompt="pr_description_only_files_prompts"))
tasks.append(task)
if patches:
patches_diff = "\n".join(patches)
get_logger().debug(f"PR diff number {i + 1} for describe files")
task = asyncio.create_task(

Check warning on line 222 in pr_agent/tools/pr_description.py

View check run for this annotation

Codecov / codecov/patch

pr_agent/tools/pr_description.py#L219-L222

Added lines #L219 - L222 were not covered by tests
self._get_prediction(model, patches_diff, prompt="pr_description_only_files_prompts"))
tasks.append(task)

Check warning on line 224 in pr_agent/tools/pr_description.py

View check run for this annotation

Codecov / codecov/patch

pr_agent/tools/pr_description.py#L224

Added line #L224 was not covered by tests
# Wait for all tasks to complete
results = await asyncio.gather(*tasks)
file_description_str_list = []
Expand All @@ -238,14 +241,23 @@ async def _prepare_prediction(self, model: str) -> None:
get_settings().pr_description_only_description_prompts.user)
files_walkthrough = "\n".join(file_description_str_list)
files_walkthrough_prompt = copy.deepcopy(files_walkthrough)
MAX_EXTRA_FILES_TO_PROMPT = 50

Check warning on line 244 in pr_agent/tools/pr_description.py

View check run for this annotation

Codecov / codecov/patch

pr_agent/tools/pr_description.py#L244

Added line #L244 was not covered by tests
if remaining_files_list:
files_walkthrough_prompt += "\n\nNo more token budget. Additional unprocessed files:"
for file in remaining_files_list:
for i, file in enumerate(remaining_files_list):

Check warning on line 247 in pr_agent/tools/pr_description.py

View check run for this annotation

Codecov / codecov/patch

pr_agent/tools/pr_description.py#L247

Added line #L247 was not covered by tests
files_walkthrough_prompt += f"\n- {file}"
if i >= MAX_EXTRA_FILES_TO_PROMPT:
get_logger().debug(f"Too many remaining files, clipping to {MAX_EXTRA_FILES_TO_PROMPT}")
files_walkthrough_prompt += f"\n... and {len(remaining_files_list) - MAX_EXTRA_FILES_TO_PROMPT} more"
break

Check warning on line 252 in pr_agent/tools/pr_description.py

View check run for this annotation

Codecov / codecov/patch

pr_agent/tools/pr_description.py#L249-L252

Added lines #L249 - L252 were not covered by tests
if deleted_files_list:
files_walkthrough_prompt += "\n\nAdditional deleted files:"
for file in deleted_files_list:
for i, file in enumerate(deleted_files_list):

Check warning on line 255 in pr_agent/tools/pr_description.py

View check run for this annotation

Codecov / codecov/patch

pr_agent/tools/pr_description.py#L255

Added line #L255 was not covered by tests
files_walkthrough_prompt += f"\n- {file}"
if i >= MAX_EXTRA_FILES_TO_PROMPT:
get_logger().debug(f"Too many deleted files, clipping to {MAX_EXTRA_FILES_TO_PROMPT}")
files_walkthrough_prompt += f"\n... and {len(deleted_files_list) - MAX_EXTRA_FILES_TO_PROMPT} more"
break

Check warning on line 260 in pr_agent/tools/pr_description.py

View check run for this annotation

Codecov / codecov/patch

pr_agent/tools/pr_description.py#L257-L260

Added lines #L257 - L260 were not covered by tests
tokens_files_walkthrough = len(
token_handler_only_description_prompt.encoder.encode(files_walkthrough_prompt))
total_tokens = token_handler_only_description_prompt.prompt_tokens + tokens_files_walkthrough
Expand All @@ -263,8 +275,9 @@ async def _prepare_prediction(self, model: str) -> None:
prediction_headers = prediction_headers.strip().removeprefix('```yaml').strip('`').strip()

# manually add extra files to final prediction
MAX_EXTRA_FILES_TO_OUTPUT = 100

Check warning on line 278 in pr_agent/tools/pr_description.py

View check run for this annotation

Codecov / codecov/patch

pr_agent/tools/pr_description.py#L278

Added line #L278 was not covered by tests
if get_settings().pr_description.mention_extra_files:
for file in remaining_files_list:
for i, file in enumerate(remaining_files_list):

Check warning on line 280 in pr_agent/tools/pr_description.py

View check run for this annotation

Codecov / codecov/patch

pr_agent/tools/pr_description.py#L280

Added line #L280 was not covered by tests
extra_file_yaml = f"""\
- filename: |
{file}
Expand All @@ -276,6 +289,20 @@ async def _prepare_prediction(self, model: str) -> None:
additional files (token-limit)
"""
files_walkthrough = files_walkthrough.strip() + "\n" + extra_file_yaml.strip()
if i >= MAX_EXTRA_FILES_TO_OUTPUT:
files_walkthrough += f"""\

Check warning on line 293 in pr_agent/tools/pr_description.py

View check run for this annotation

Codecov / codecov/patch

pr_agent/tools/pr_description.py#L292-L293

Added lines #L292 - L293 were not covered by tests
extra_file_yaml =
- filename: |
Additional {len(remaining_files_list) - MAX_EXTRA_FILES_TO_OUTPUT} files not shown
changes_summary: |
...
changes_title: |
...
label: |
additional files (token-limit)
"""
break

Check warning on line 304 in pr_agent/tools/pr_description.py

View check run for this annotation

Codecov / codecov/patch

pr_agent/tools/pr_description.py#L304

Added line #L304 was not covered by tests

# final processing
self.prediction = prediction_headers + "\n" + "pr_files:\n" + files_walkthrough
if not load_yaml(self.prediction):
Expand Down Expand Up @@ -304,10 +331,10 @@ async def extend_additional_files(self, remaining_files_list) -> str:
prediction_extra_dict = load_yaml(prediction_extra)
# merge the two dictionaries
if isinstance(original_prediction_dict, dict) and isinstance(prediction_extra_dict, dict):
original_prediction_dict["pr_files"].extend(prediction_extra_dict["pr_files"])
new_yaml = yaml.dump(original_prediction_dict)
if load_yaml(new_yaml):
prediction = new_yaml
original_prediction_dict["pr_files"].extend(prediction_extra_dict["pr_files"])
new_yaml = yaml.dump(original_prediction_dict)
if load_yaml(new_yaml):
prediction = new_yaml

Check warning on line 337 in pr_agent/tools/pr_description.py

View check run for this annotation

Codecov / codecov/patch

pr_agent/tools/pr_description.py#L334-L337

Added lines #L334 - L337 were not covered by tests
return prediction
except Exception as e:
get_logger().error(f"Error extending additional files {self.pr_id}: {e}")
Expand Down

0 comments on commit e531245

Please sign in to comment.