From 444b75a295f723d6ab4e44b41887f2f6a7a22954 Mon Sep 17 00:00:00 2001 From: Saurabh Misra Date: Fri, 27 Jun 2025 17:18:33 -0700 Subject: [PATCH 1/3] cleanup really thoroughly --- codeflash/optimization/optimizer.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/codeflash/optimization/optimizer.py b/codeflash/optimization/optimizer.py index 32a825b76..f2e42c69c 100644 --- a/codeflash/optimization/optimizer.py +++ b/codeflash/optimization/optimizer.py @@ -248,7 +248,6 @@ def run(self) -> None: return if not env_utils.check_formatter_installed(self.args.formatter_cmds): return - if self.args.no_draft and is_pr_draft(): logger.warning("PR is in draft mode, skipping optimization") return @@ -326,12 +325,32 @@ def run(self) -> None: self.cleanup_temporary_paths() + @staticmethod + def find_leftover_instrumented_test_files(test_root: Path) -> list[Path]: + """Search for all paths within the test_root that match the following patterns. + + - 'test.*__perf_test_{0,1}.py' + - 'test_.*__unit_{0,1}.py' + - 'test_.*__perfinstrumented.py' + - 'test_.*__perfonlyinstrumented.py' + Returns a list of matching file paths. + """ + import re + + pattern = re.compile( + r"^(test.*__perf_test_?\.py|test_.*__unit_?\.py|test_.*__perfinstrumented\.py|test_.*__perfonlyinstrumented\.py)$" + ) + + return [file for file in test_root.rglob("test_*.py") if pattern.match(file.name)] + def cleanup_temporary_paths(self) -> None: from codeflash.code_utils.code_utils import cleanup_paths if self.current_function_optimizer: self.current_function_optimizer.cleanup_generated_files() + cleanup_paths(Optimizer.find_leftover_instrumented_test_files(self.test_cfg.tests_root)) + cleanup_paths([self.test_cfg.concolic_test_root_dir, self.replay_tests_dir]) From 87fd97bd2fbd793ffb9b7b8f9f2a69686827436a Mon Sep 17 00:00:00 2001 From: Saurabh Misra Date: Fri, 27 Jun 2025 17:21:35 -0700 Subject: [PATCH 2/3] cleanup the cleanup --- codeflash/optimization/optimizer.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/codeflash/optimization/optimizer.py b/codeflash/optimization/optimizer.py index f2e42c69c..ce7fd5ace 100644 --- a/codeflash/optimization/optimizer.py +++ b/codeflash/optimization/optimizer.py @@ -12,6 +12,7 @@ from codeflash.api.aiservice import AiServiceClient, LocalAiServiceClient from codeflash.cli_cmds.console import console, logger, progress_bar from codeflash.code_utils import env_utils +from codeflash.code_utils.code_utils import cleanup_paths from codeflash.code_utils.env_utils import get_pr_number from codeflash.either import is_successful from codeflash.models.models import ValidCode @@ -251,6 +252,7 @@ def run(self) -> None: if self.args.no_draft and is_pr_draft(): logger.warning("PR is in draft mode, skipping optimization") return + cleanup_paths(Optimizer.find_leftover_instrumented_test_files(self.test_cfg.tests_root)) function_optimizer = None file_to_funcs_to_optimize, num_optimizable_functions = self.get_optimizable_functions() @@ -344,13 +346,9 @@ def find_leftover_instrumented_test_files(test_root: Path) -> list[Path]: return [file for file in test_root.rglob("test_*.py") if pattern.match(file.name)] def cleanup_temporary_paths(self) -> None: - from codeflash.code_utils.code_utils import cleanup_paths - if self.current_function_optimizer: self.current_function_optimizer.cleanup_generated_files() - cleanup_paths(Optimizer.find_leftover_instrumented_test_files(self.test_cfg.tests_root)) - cleanup_paths([self.test_cfg.concolic_test_root_dir, self.replay_tests_dir]) From a756e56a86894419dcf26222110cb6d90788cdf0 Mon Sep 17 00:00:00 2001 From: Saurabh Misra Date: Fri, 27 Jun 2025 17:53:42 -0700 Subject: [PATCH 3/3] fix the regex --- codeflash/optimization/optimizer.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/codeflash/optimization/optimizer.py b/codeflash/optimization/optimizer.py index ce7fd5ace..70ba8cc12 100644 --- a/codeflash/optimization/optimizer.py +++ b/codeflash/optimization/optimizer.py @@ -332,7 +332,7 @@ def find_leftover_instrumented_test_files(test_root: Path) -> list[Path]: """Search for all paths within the test_root that match the following patterns. - 'test.*__perf_test_{0,1}.py' - - 'test_.*__unit_{0,1}.py' + - 'test_.*__unit_test_{0,1}.py' - 'test_.*__perfinstrumented.py' - 'test_.*__perfonlyinstrumented.py' Returns a list of matching file paths. @@ -340,10 +340,12 @@ def find_leftover_instrumented_test_files(test_root: Path) -> list[Path]: import re pattern = re.compile( - r"^(test.*__perf_test_?\.py|test_.*__unit_?\.py|test_.*__perfinstrumented\.py|test_.*__perfonlyinstrumented\.py)$" + r"(?:test.*__perf_test_\d?\.py|test_.*__unit_test_\d?\.py|test_.*__perfinstrumented\.py|test_.*__perfonlyinstrumented\.py)$" ) - return [file for file in test_root.rglob("test_*.py") if pattern.match(file.name)] + return [ + file_path for file_path in test_root.rglob("*") if file_path.is_file() and pattern.match(file_path.name) + ] def cleanup_temporary_paths(self) -> None: if self.current_function_optimizer: