Skip to content

Commit e5f0ba1

Browse files
committed
refix
1 parent 9bd6e49 commit e5f0ba1

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

codeflash/optimization/optimizer.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,12 @@ def run(self) -> None:
409409
if self.args.worktree:
410410
self.worktree_mode()
411411

412+
if not self.args.replay_test and self.test_cfg.tests_root.exists():
413+
leftover_trace_files = list(self.test_cfg.tests_root.glob("*.trace"))
414+
if leftover_trace_files:
415+
logger.debug(f"Cleaning up {len(leftover_trace_files)} leftover trace file(s) from previous runs")
416+
cleanup_paths(leftover_trace_files)
417+
412418
cleanup_paths(Optimizer.find_leftover_instrumented_test_files(self.test_cfg.tests_root))
413419

414420
function_optimizer = None
@@ -519,6 +525,8 @@ def run(self) -> None:
519525
)
520526
if self.functions_checkpoint:
521527
self.functions_checkpoint.cleanup()
528+
if hasattr(self.args, "command") and self.args.command == "optimize":
529+
self.cleanup_replay_tests()
522530
if optimizations_found == 0:
523531
logger.info("❌ No optimizations found.")
524532
elif self.args.all:
@@ -554,6 +562,17 @@ def find_leftover_instrumented_test_files(test_root: Path) -> list[Path]:
554562
file_path for file_path in test_root.rglob("*") if file_path.is_file() and pattern.match(file_path.name)
555563
]
556564

565+
def cleanup_replay_tests(self) -> None:
566+
paths_to_cleanup = []
567+
if self.replay_tests_dir and self.replay_tests_dir.exists():
568+
logger.debug(f"Cleaning up replay tests directory: {self.replay_tests_dir}")
569+
paths_to_cleanup.append(self.replay_tests_dir)
570+
if self.trace_file and self.trace_file.exists():
571+
logger.debug(f"Cleaning up trace file: {self.trace_file}")
572+
paths_to_cleanup.append(self.trace_file)
573+
if paths_to_cleanup:
574+
cleanup_paths(paths_to_cleanup)
575+
557576
def cleanup_temporary_paths(self) -> None:
558577
if hasattr(get_run_tmp_file, "tmpdir"):
559578
get_run_tmp_file.tmpdir.cleanup()
@@ -568,6 +587,14 @@ def cleanup_temporary_paths(self) -> None:
568587

569588
if self.current_function_optimizer:
570589
self.current_function_optimizer.cleanup_generated_files()
590+
paths_to_cleanup = [self.replay_tests_dir]
591+
if self.trace_file:
592+
paths_to_cleanup.append(self.trace_file)
593+
if self.test_cfg.tests_root.exists():
594+
for trace_file in self.test_cfg.tests_root.glob("*.trace"):
595+
if trace_file not in paths_to_cleanup:
596+
paths_to_cleanup.append(trace_file)
597+
cleanup_paths(paths_to_cleanup)
571598

572599
def worktree_mode(self) -> None:
573600
if self.current_worktree:

codeflash/tracer.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ def main(args: Namespace | None = None) -> ArgumentParser:
8686
# to the output file at startup.
8787
if parsed_args.outfile is not None:
8888
parsed_args.outfile = Path(parsed_args.outfile).resolve()
89+
outfile = parsed_args.outfile
8990
config, found_config_path = parse_config_file(parsed_args.codeflash_config)
9091
project_root = project_root_from_module_root(Path(config["module_root"]), found_config_path)
9192
if len(unknown_args) > 0:
@@ -215,6 +216,12 @@ def main(args: Namespace | None = None) -> ArgumentParser:
215216

216217
optimizer.run_with_args(args)
217218

219+
# Delete the trace file and the replay test file if they exist
220+
if outfile:
221+
outfile.unlink(missing_ok=True)
222+
for replay_test_path in replay_test_paths:
223+
Path(replay_test_path).unlink(missing_ok=True)
224+
218225
except BrokenPipeError as exc:
219226
# Prevent "Exception ignored" during interpreter shutdown.
220227
sys.stdout = None

0 commit comments

Comments
 (0)