From e970866b957a28dd1a40ba81f7a48ca975f80f14 Mon Sep 17 00:00:00 2001 From: Kevin Turcios Date: Wed, 24 Dec 2025 21:00:54 -0500 Subject: [PATCH 1/5] let's not clean up traces and rpelays --- codeflash/optimization/optimizer.py | 28 +--------------------------- codeflash/tracer.py | 7 ------- 2 files changed, 1 insertion(+), 34 deletions(-) diff --git a/codeflash/optimization/optimizer.py b/codeflash/optimization/optimizer.py index ac757e6a9..fb004b849 100644 --- a/codeflash/optimization/optimizer.py +++ b/codeflash/optimization/optimizer.py @@ -409,12 +409,6 @@ def run(self) -> None: if self.args.worktree: self.worktree_mode() - if not self.args.replay_test and self.test_cfg.tests_root.exists(): - leftover_trace_files = list(self.test_cfg.tests_root.glob("*.trace")) - if leftover_trace_files: - logger.debug(f"Cleaning up {len(leftover_trace_files)} leftover trace file(s) from previous runs") - cleanup_paths(leftover_trace_files) - cleanup_paths(Optimizer.find_leftover_instrumented_test_files(self.test_cfg.tests_root)) function_optimizer = None @@ -525,8 +519,6 @@ def run(self) -> None: ) if self.functions_checkpoint: self.functions_checkpoint.cleanup() - if hasattr(self.args, "command") and self.args.command == "optimize": - self.cleanup_replay_tests() if optimizations_found == 0: logger.info("❌ No optimizations found.") elif self.args.all: @@ -562,17 +554,6 @@ def find_leftover_instrumented_test_files(test_root: Path) -> list[Path]: file_path for file_path in test_root.rglob("*") if file_path.is_file() and pattern.match(file_path.name) ] - def cleanup_replay_tests(self) -> None: - paths_to_cleanup = [] - if self.replay_tests_dir and self.replay_tests_dir.exists(): - logger.debug(f"Cleaning up replay tests directory: {self.replay_tests_dir}") - paths_to_cleanup.append(self.replay_tests_dir) - if self.trace_file and self.trace_file.exists(): - logger.debug(f"Cleaning up trace file: {self.trace_file}") - paths_to_cleanup.append(self.trace_file) - if paths_to_cleanup: - cleanup_paths(paths_to_cleanup) - def cleanup_temporary_paths(self) -> None: if hasattr(get_run_tmp_file, "tmpdir"): get_run_tmp_file.tmpdir.cleanup() @@ -584,14 +565,7 @@ def cleanup_temporary_paths(self) -> None: if self.current_function_optimizer: self.current_function_optimizer.cleanup_generated_files() - paths_to_cleanup = [self.test_cfg.concolic_test_root_dir, self.replay_tests_dir] - if self.trace_file: - paths_to_cleanup.append(self.trace_file) - if self.test_cfg.tests_root.exists(): - for trace_file in self.test_cfg.tests_root.glob("*.trace"): - if trace_file not in paths_to_cleanup: - paths_to_cleanup.append(trace_file) - cleanup_paths(paths_to_cleanup) + cleanup_paths([self.test_cfg.concolic_test_root_dir]) def worktree_mode(self) -> None: if self.current_worktree: diff --git a/codeflash/tracer.py b/codeflash/tracer.py index eb011befa..a554f02a0 100644 --- a/codeflash/tracer.py +++ b/codeflash/tracer.py @@ -86,7 +86,6 @@ def main(args: Namespace | None = None) -> ArgumentParser: # to the output file at startup. if parsed_args.outfile is not None: parsed_args.outfile = Path(parsed_args.outfile).resolve() - outfile = parsed_args.outfile config, found_config_path = parse_config_file(parsed_args.codeflash_config) project_root = project_root_from_module_root(Path(config["module_root"]), found_config_path) if len(unknown_args) > 0: @@ -216,12 +215,6 @@ def main(args: Namespace | None = None) -> ArgumentParser: optimizer.run_with_args(args) - # Delete the trace file and the replay test file if they exist - if outfile: - outfile.unlink(missing_ok=True) - for replay_test_path in replay_test_paths: - Path(replay_test_path).unlink(missing_ok=True) - except BrokenPipeError as exc: # Prevent "Exception ignored" during interpreter shutdown. sys.stdout = None From 8dd6605c7cccc8653ec98f9b62e088735120df47 Mon Sep 17 00:00:00 2001 From: Kevin Turcios Date: Wed, 24 Dec 2025 21:03:30 -0500 Subject: [PATCH 2/5] let's see --- codeflash/optimization/optimizer.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/codeflash/optimization/optimizer.py b/codeflash/optimization/optimizer.py index fb004b849..f3f28066b 100644 --- a/codeflash/optimization/optimizer.py +++ b/codeflash/optimization/optimizer.py @@ -559,13 +559,15 @@ def cleanup_temporary_paths(self) -> None: get_run_tmp_file.tmpdir.cleanup() del get_run_tmp_file.tmpdir + # Always clean up concolic test directory + cleanup_paths([self.test_cfg.concolic_test_root_dir]) + if self.current_worktree: remove_worktree(self.current_worktree) return if self.current_function_optimizer: self.current_function_optimizer.cleanup_generated_files() - cleanup_paths([self.test_cfg.concolic_test_root_dir]) def worktree_mode(self) -> None: if self.current_worktree: From 9bd6e49344e8d4f266754412ae752c73421c32fb Mon Sep 17 00:00:00 2001 From: Kevin Turcios Date: Wed, 24 Dec 2025 21:08:26 -0500 Subject: [PATCH 3/5] filter these warnings --- codeflash/tracing/replay_test.py | 5 ++++- codeflash/verification/codeflash_capture.py | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/codeflash/tracing/replay_test.py b/codeflash/tracing/replay_test.py index b1b10f56e..0269213a1 100644 --- a/codeflash/tracing/replay_test.py +++ b/codeflash/tracing/replay_test.py @@ -44,7 +44,10 @@ def get_function_alias(module: str, function_name: str) -> str: def create_trace_replay_test(trace_file: str, functions: list[FunctionModules], max_run_count: int = 100) -> str: - imports = """import dill as pickle + imports = """import warnings +import dill as pickle +from dill import PicklingWarning +warnings.filterwarnings("ignore", category=PicklingWarning) from codeflash.tracing.replay_test import get_next_arg_and_return """ diff --git a/codeflash/verification/codeflash_capture.py b/codeflash/verification/codeflash_capture.py index 45d046cf6..f615c919a 100644 --- a/codeflash/verification/codeflash_capture.py +++ b/codeflash/verification/codeflash_capture.py @@ -7,11 +7,15 @@ import os import sqlite3 import time +import warnings from enum import Enum from pathlib import Path from typing import Callable import dill as pickle +from dill import PicklingWarning + +warnings.filterwarnings("ignore", category=PicklingWarning) class VerificationType(str, Enum): From e5f0ba17766b462d606dbaaeb0fb0c08c48898d4 Mon Sep 17 00:00:00 2001 From: Kevin Turcios Date: Wed, 24 Dec 2025 21:19:00 -0500 Subject: [PATCH 4/5] refix --- codeflash/optimization/optimizer.py | 27 +++++++++++++++++++++++++++ codeflash/tracer.py | 7 +++++++ 2 files changed, 34 insertions(+) diff --git a/codeflash/optimization/optimizer.py b/codeflash/optimization/optimizer.py index f3f28066b..c98d37042 100644 --- a/codeflash/optimization/optimizer.py +++ b/codeflash/optimization/optimizer.py @@ -409,6 +409,12 @@ def run(self) -> None: if self.args.worktree: self.worktree_mode() + if not self.args.replay_test and self.test_cfg.tests_root.exists(): + leftover_trace_files = list(self.test_cfg.tests_root.glob("*.trace")) + if leftover_trace_files: + logger.debug(f"Cleaning up {len(leftover_trace_files)} leftover trace file(s) from previous runs") + cleanup_paths(leftover_trace_files) + cleanup_paths(Optimizer.find_leftover_instrumented_test_files(self.test_cfg.tests_root)) function_optimizer = None @@ -519,6 +525,8 @@ def run(self) -> None: ) if self.functions_checkpoint: self.functions_checkpoint.cleanup() + if hasattr(self.args, "command") and self.args.command == "optimize": + self.cleanup_replay_tests() if optimizations_found == 0: logger.info("❌ No optimizations found.") elif self.args.all: @@ -554,6 +562,17 @@ def find_leftover_instrumented_test_files(test_root: Path) -> list[Path]: file_path for file_path in test_root.rglob("*") if file_path.is_file() and pattern.match(file_path.name) ] + def cleanup_replay_tests(self) -> None: + paths_to_cleanup = [] + if self.replay_tests_dir and self.replay_tests_dir.exists(): + logger.debug(f"Cleaning up replay tests directory: {self.replay_tests_dir}") + paths_to_cleanup.append(self.replay_tests_dir) + if self.trace_file and self.trace_file.exists(): + logger.debug(f"Cleaning up trace file: {self.trace_file}") + paths_to_cleanup.append(self.trace_file) + if paths_to_cleanup: + cleanup_paths(paths_to_cleanup) + def cleanup_temporary_paths(self) -> None: if hasattr(get_run_tmp_file, "tmpdir"): get_run_tmp_file.tmpdir.cleanup() @@ -568,6 +587,14 @@ def cleanup_temporary_paths(self) -> None: if self.current_function_optimizer: self.current_function_optimizer.cleanup_generated_files() + paths_to_cleanup = [self.replay_tests_dir] + if self.trace_file: + paths_to_cleanup.append(self.trace_file) + if self.test_cfg.tests_root.exists(): + for trace_file in self.test_cfg.tests_root.glob("*.trace"): + if trace_file not in paths_to_cleanup: + paths_to_cleanup.append(trace_file) + cleanup_paths(paths_to_cleanup) def worktree_mode(self) -> None: if self.current_worktree: diff --git a/codeflash/tracer.py b/codeflash/tracer.py index a554f02a0..eb011befa 100644 --- a/codeflash/tracer.py +++ b/codeflash/tracer.py @@ -86,6 +86,7 @@ def main(args: Namespace | None = None) -> ArgumentParser: # to the output file at startup. if parsed_args.outfile is not None: parsed_args.outfile = Path(parsed_args.outfile).resolve() + outfile = parsed_args.outfile config, found_config_path = parse_config_file(parsed_args.codeflash_config) project_root = project_root_from_module_root(Path(config["module_root"]), found_config_path) if len(unknown_args) > 0: @@ -215,6 +216,12 @@ def main(args: Namespace | None = None) -> ArgumentParser: optimizer.run_with_args(args) + # Delete the trace file and the replay test file if they exist + if outfile: + outfile.unlink(missing_ok=True) + for replay_test_path in replay_test_paths: + Path(replay_test_path).unlink(missing_ok=True) + except BrokenPipeError as exc: # Prevent "Exception ignored" during interpreter shutdown. sys.stdout = None From 592d881cbfac365762e066775a7dc4b3f9a23683 Mon Sep 17 00:00:00 2001 From: Kevin Turcios Date: Wed, 24 Dec 2025 21:25:46 -0500 Subject: [PATCH 5/5] filter here too --- codeflash/picklepatch/pickle_patcher.py | 4 ++++ codeflash/tracing/tracing_new_process.py | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/codeflash/picklepatch/pickle_patcher.py b/codeflash/picklepatch/pickle_patcher.py index 5c635c0dd..ed0cac493 100644 --- a/codeflash/picklepatch/pickle_patcher.py +++ b/codeflash/picklepatch/pickle_patcher.py @@ -8,12 +8,16 @@ import contextlib import pickle +import warnings from typing import Any, ClassVar import dill +from dill import PicklingWarning from .pickle_placeholder import PicklePlaceholder +warnings.filterwarnings("ignore", category=PicklingWarning) + class PicklePatcher: """A utility class for safely pickling objects with unpicklable components. diff --git a/codeflash/tracing/tracing_new_process.py b/codeflash/tracing/tracing_new_process.py index 05e547c6f..52ca31f8b 100644 --- a/codeflash/tracing/tracing_new_process.py +++ b/codeflash/tracing/tracing_new_process.py @@ -12,6 +12,7 @@ import sys import threading import time +import warnings from collections import defaultdict from importlib.util import find_spec from pathlib import Path @@ -26,6 +27,10 @@ from codeflash.picklepatch.pickle_patcher import PicklePatcher from codeflash.tracing.tracing_utils import FunctionModules, filter_files_optimized, module_name_from_file_path +# Suppress dill PicklingWarning +warnings.filterwarnings("ignore", message="Cannot locate reference to") +warnings.filterwarnings("ignore", message="Cannot pickle.*recursive self-references") + if TYPE_CHECKING: from types import FrameType, TracebackType