From 0189e12fb159f73c64e64a244883824e58381be4 Mon Sep 17 00:00:00 2001 From: zmeir Date: Wed, 3 Jan 2024 14:21:38 +0200 Subject: [PATCH 1/3] Automatically enable improve extended mode for large PRs --- docs/IMPROVE.md | 4 ++++ pr_agent/settings/configuration.toml | 4 ++++ pr_agent/tools/pr_code_suggestions.py | 17 ++++++++++++++++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/docs/IMPROVE.md b/docs/IMPROVE.md index cb4b47dd..19fa038e 100644 --- a/docs/IMPROVE.md +++ b/docs/IMPROVE.md @@ -29,6 +29,10 @@ Under the section 'pr_code_suggestions', the [configuration file](./../pr_agent/ - `include_improved_code`: if set to true, the tool will include an improved code implementation in the suggestion. Default is true. #### params for '/improve --extended' mode +- `auto_extended_mode`: enable extended mode automatically for large PRs. Default is true. +- `auto_extended_mode_min_files`: minimum number of files in the PR to automatically enable extended mode. Default is 1. +- `auto_extended_mode_min_additions`: minimum number of line additions in the PR to automatically enable extended mode. Default is 500. +- `auto_extended_mode_min_deletions`: minimum number of line deletions in the PR to automatically enable extended mode. Default is 0. - `num_code_suggestions_per_chunk`: number of code suggestions provided by the 'improve' tool, per chunk. Default is 8. - `rank_extended_suggestions`: if set to true, the tool will rank the suggestions, based on importance. Default is true. - `max_number_of_calls`: maximum number of chunks. Default is 5. diff --git a/pr_agent/settings/configuration.toml b/pr_agent/settings/configuration.toml index 04466c72..3a868c7b 100644 --- a/pr_agent/settings/configuration.toml +++ b/pr_agent/settings/configuration.toml @@ -71,6 +71,10 @@ include_improved_code = true extra_instructions = "" rank_suggestions = false # params for '/improve --extended' mode +auto_extended_mode=true +auto_extended_mode_min_files=1 +auto_extended_mode_min_additions=500 +auto_extended_mode_min_deletions=0 num_code_suggestions_per_chunk=8 rank_extended_suggestions = true max_number_of_calls = 5 diff --git a/pr_agent/tools/pr_code_suggestions.py b/pr_agent/tools/pr_code_suggestions.py index b5006b85..1e4f25a8 100644 --- a/pr_agent/tools/pr_code_suggestions.py +++ b/pr_agent/tools/pr_code_suggestions.py @@ -26,7 +26,7 @@ class PRCodeSuggestions: # extended mode try: - self.is_extended = any(["extended" in arg for arg in args]) + self.is_extended = self._get_is_extended(args or []) except: self.is_extended = False if self.is_extended: @@ -206,6 +206,21 @@ class PRCodeSuggestions: return new_code_snippet + def _get_is_extended(self, args: list[str]) -> bool: + """Check if extended mode should be enabled by the `--extended` flag or automatically according to the PR""" + if any(["extended" in arg for arg in args]): + get_logger().info("Extended mode is enabled by the `--extended` flag") + return True + if ( + get_settings().pr_code_suggestions.auto_extended_mode + and self.git_provider.pr.changed_files >= get_settings().pr_code_suggestions.auto_extended_mode_min_files + and self.git_provider.pr.additions >= get_settings().pr_code_suggestions.auto_extended_mode_min_additions + and self.git_provider.pr.deletions >= get_settings().pr_code_suggestions.auto_extended_mode_min_deletions + ): + get_logger().info("Extended mode is enabled automatically based on the PR size") + return True + return False + async def _prepare_prediction_extended(self, model: str) -> dict: get_logger().info('Getting PR diff...') patches_diff_list = get_pr_multi_diffs(self.git_provider, self.token_handler, model, From 2f9fbbf0ac32d2be809c34613d34826529bca924 Mon Sep 17 00:00:00 2001 From: zmeir Date: Wed, 3 Jan 2024 16:26:32 +0200 Subject: [PATCH 2/3] Prevent reducing the number of suggestions if already low enough --- pr_agent/tools/pr_code_suggestions.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/pr_agent/tools/pr_code_suggestions.py b/pr_agent/tools/pr_code_suggestions.py index 1e4f25a8..4490d02c 100644 --- a/pr_agent/tools/pr_code_suggestions.py +++ b/pr_agent/tools/pr_code_suggestions.py @@ -286,8 +286,14 @@ class PRCodeSuggestions: data_sorted[importance_order - 1] = suggestion_list[suggestion_number - 1] if get_settings().pr_code_suggestions.final_clip_factor != 1: - new_len = int(0.5 + len(data_sorted) * get_settings().pr_code_suggestions.final_clip_factor) - data_sorted = data_sorted[:new_len] + max_len = max( + len(data_sorted), + get_settings().pr_code_suggestions.num_code_suggestions, + get_settings().pr_code_suggestions.num_code_suggestions_per_chunk, + ) + new_len = int(0.5 + max_len * get_settings().pr_code_suggestions.final_clip_factor) + if new_len < len(data_sorted): + data_sorted = data_sorted[:new_len] except Exception as e: if get_settings().config.verbosity_level >= 1: get_logger().info(f"Could not sort suggestions, error: {e}") From c2b0891c0baa8c3dd3e57cad0644b58e750168e0 Mon Sep 17 00:00:00 2001 From: Zohar Meir <33152084+zmeir@users.noreply.github.com> Date: Thu, 4 Jan 2024 18:53:45 +0200 Subject: [PATCH 3/3] Simpler auto-extended toggle and keep the default as false --- docs/IMPROVE.md | 5 +---- pr_agent/settings/configuration.toml | 5 +---- pr_agent/tools/pr_code_suggestions.py | 11 +++-------- 3 files changed, 5 insertions(+), 16 deletions(-) diff --git a/docs/IMPROVE.md b/docs/IMPROVE.md index 19fa038e..3f561071 100644 --- a/docs/IMPROVE.md +++ b/docs/IMPROVE.md @@ -29,10 +29,7 @@ Under the section 'pr_code_suggestions', the [configuration file](./../pr_agent/ - `include_improved_code`: if set to true, the tool will include an improved code implementation in the suggestion. Default is true. #### params for '/improve --extended' mode -- `auto_extended_mode`: enable extended mode automatically for large PRs. Default is true. -- `auto_extended_mode_min_files`: minimum number of files in the PR to automatically enable extended mode. Default is 1. -- `auto_extended_mode_min_additions`: minimum number of line additions in the PR to automatically enable extended mode. Default is 500. -- `auto_extended_mode_min_deletions`: minimum number of line deletions in the PR to automatically enable extended mode. Default is 0. +- `auto_extended_mode`: enable extended mode automatically (no need for the `--extended` option). Default is false. - `num_code_suggestions_per_chunk`: number of code suggestions provided by the 'improve' tool, per chunk. Default is 8. - `rank_extended_suggestions`: if set to true, the tool will rank the suggestions, based on importance. Default is true. - `max_number_of_calls`: maximum number of chunks. Default is 5. diff --git a/pr_agent/settings/configuration.toml b/pr_agent/settings/configuration.toml index 3a868c7b..cbd2246c 100644 --- a/pr_agent/settings/configuration.toml +++ b/pr_agent/settings/configuration.toml @@ -71,10 +71,7 @@ include_improved_code = true extra_instructions = "" rank_suggestions = false # params for '/improve --extended' mode -auto_extended_mode=true -auto_extended_mode_min_files=1 -auto_extended_mode_min_additions=500 -auto_extended_mode_min_deletions=0 +auto_extended_mode=false num_code_suggestions_per_chunk=8 rank_extended_suggestions = true max_number_of_calls = 5 diff --git a/pr_agent/tools/pr_code_suggestions.py b/pr_agent/tools/pr_code_suggestions.py index 4490d02c..776b660f 100644 --- a/pr_agent/tools/pr_code_suggestions.py +++ b/pr_agent/tools/pr_code_suggestions.py @@ -207,17 +207,12 @@ class PRCodeSuggestions: return new_code_snippet def _get_is_extended(self, args: list[str]) -> bool: - """Check if extended mode should be enabled by the `--extended` flag or automatically according to the PR""" + """Check if extended mode should be enabled by the `--extended` flag or automatically according to the configuration""" if any(["extended" in arg for arg in args]): get_logger().info("Extended mode is enabled by the `--extended` flag") return True - if ( - get_settings().pr_code_suggestions.auto_extended_mode - and self.git_provider.pr.changed_files >= get_settings().pr_code_suggestions.auto_extended_mode_min_files - and self.git_provider.pr.additions >= get_settings().pr_code_suggestions.auto_extended_mode_min_additions - and self.git_provider.pr.deletions >= get_settings().pr_code_suggestions.auto_extended_mode_min_deletions - ): - get_logger().info("Extended mode is enabled automatically based on the PR size") + if get_settings().pr_code_suggestions.auto_extended_mode: + get_logger().info("Extended mode is enabled automatically based on the configuration toggle") return True return False