Skip to content

Commit 6b1a24c

Browse files
Merge pull request #457 from codeflash-ai/cleanup-the-right-files
cleanup
2 parents b759192 + e4640e9 commit 6b1a24c

File tree

2 files changed

+33
-27
lines changed

2 files changed

+33
-27
lines changed

codeflash/optimization/function_optimizer.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,11 +1457,9 @@ def submit_test_generation_tasks(
14571457
]
14581458

14591459
def cleanup_generated_files(self) -> None:
1460-
paths_to_cleanup = [self.test_cfg.concolic_test_root_dir]
1460+
paths_to_cleanup = []
14611461
for test_file in self.test_files:
14621462
paths_to_cleanup.append(test_file.instrumented_behavior_file_path)
14631463
paths_to_cleanup.append(test_file.benchmarking_file_path)
14641464

14651465
cleanup_paths(paths_to_cleanup)
1466-
if hasattr(get_run_tmp_file, "tmpdir"):
1467-
get_run_tmp_file.tmpdir.cleanup()

codeflash/optimization/optimizer.py

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from codeflash.api.aiservice import AiServiceClient, LocalAiServiceClient
1313
from codeflash.cli_cmds.console import console, logger, progress_bar
1414
from codeflash.code_utils import env_utils
15-
from codeflash.code_utils.code_utils import cleanup_paths
15+
from codeflash.code_utils.code_utils import cleanup_paths, get_run_tmp_file
1616
from codeflash.code_utils.env_utils import get_pr_number
1717
from codeflash.either import is_successful
1818
from codeflash.models.models import ValidCode
@@ -289,31 +289,36 @@ def run(self) -> None:
289289
f"{function_to_optimize.qualified_name}"
290290
)
291291
console.rule()
292+
function_optimizer = None
293+
try:
294+
function_optimizer = self.create_function_optimizer(
295+
function_to_optimize,
296+
function_to_tests=function_to_tests,
297+
function_to_optimize_source_code=validated_original_code[original_module_path].source_code,
298+
function_benchmark_timings=function_benchmark_timings,
299+
total_benchmark_timings=total_benchmark_timings,
300+
original_module_ast=original_module_ast,
301+
original_module_path=original_module_path,
302+
)
292303

293-
function_optimizer = self.create_function_optimizer(
294-
function_to_optimize,
295-
function_to_tests=function_to_tests,
296-
function_to_optimize_source_code=validated_original_code[original_module_path].source_code,
297-
function_benchmark_timings=function_benchmark_timings,
298-
total_benchmark_timings=total_benchmark_timings,
299-
original_module_ast=original_module_ast,
300-
original_module_path=original_module_path,
301-
)
302-
303-
self.current_function_optimizer = (
304-
function_optimizer # needed to clean up from the outside of this function
305-
)
306-
best_optimization = function_optimizer.optimize_function()
307-
if self.functions_checkpoint:
308-
self.functions_checkpoint.add_function_to_checkpoint(
309-
function_to_optimize.qualified_name_with_modules_from_root(self.args.project_root)
304+
self.current_function_optimizer = (
305+
function_optimizer # needed to clean up from the outside of this function
310306
)
311-
if is_successful(best_optimization):
312-
optimizations_found += 1
313-
else:
314-
logger.warning(best_optimization.failure())
315-
console.rule()
316-
continue
307+
best_optimization = function_optimizer.optimize_function()
308+
if self.functions_checkpoint:
309+
self.functions_checkpoint.add_function_to_checkpoint(
310+
function_to_optimize.qualified_name_with_modules_from_root(self.args.project_root)
311+
)
312+
if is_successful(best_optimization):
313+
optimizations_found += 1
314+
else:
315+
logger.warning(best_optimization.failure())
316+
console.rule()
317+
continue
318+
finally:
319+
if function_optimizer is not None:
320+
function_optimizer.cleanup_generated_files()
321+
317322
ph("cli-optimize-run-finished", {"optimizations_found": optimizations_found})
318323
if self.functions_checkpoint:
319324
self.functions_checkpoint.cleanup()
@@ -351,6 +356,9 @@ def cleanup_temporary_paths(self) -> None:
351356
if self.current_function_optimizer:
352357
self.current_function_optimizer.cleanup_generated_files()
353358

359+
if hasattr(get_run_tmp_file, "tmpdir"):
360+
get_run_tmp_file.tmpdir.cleanup()
361+
del get_run_tmp_file.tmpdir
354362
cleanup_paths([self.test_cfg.concolic_test_root_dir, self.replay_tests_dir])
355363

356364

0 commit comments

Comments
 (0)