From 21f6e6c0c570c8199cba8c568af40668b810e4ab Mon Sep 17 00:00:00 2001 From: ali Date: Tue, 24 Mar 2026 15:37:00 +0200 Subject: [PATCH 1/3] fix: harden worktree creation and improve subagent mode support - Make worktree creation mandatory by exiting on failure instead of silently skipping - Remove redundant git repo check from create_detached_worktree (caller handles it) - Skip duplicate optimization checks in subagent mode (same as LSP) - Log optimization results as markdown in subagent mode instead of Rich tree - Disable custom benchmark loop overrides for subagent runs - Downgrade Jest benchmarking failure logs from info to debug Co-Authored-By: Claude Opus 4.6 --- codeflash/code_utils/git_worktree_utils.py | 7 ++---- codeflash/discovery/functions_to_optimize.py | 4 ++-- codeflash/languages/function_optimizer.py | 4 +++- codeflash/languages/javascript/test_runner.py | 6 ++--- codeflash/optimization/optimizer.py | 23 +++++++++++-------- 5 files changed, 24 insertions(+), 20 deletions(-) diff --git a/codeflash/code_utils/git_worktree_utils.py b/codeflash/code_utils/git_worktree_utils.py index 3dcba708e..bf9c15324 100644 --- a/codeflash/code_utils/git_worktree_utils.py +++ b/codeflash/code_utils/git_worktree_utils.py @@ -13,7 +13,7 @@ from codeflash.cli_cmds.console import logger from codeflash.code_utils.compat import codeflash_cache_dir -from codeflash.code_utils.git_utils import check_running_in_git_repo, git_root_dir +from codeflash.code_utils.git_utils import git_root_dir worktree_dirs = codeflash_cache_dir / "worktrees" patches_dir = codeflash_cache_dir / "patches" @@ -53,10 +53,7 @@ def create_worktree_snapshot_commit(worktree_dir: Path, commit_message: str) -> cw.remove_option("user", "email") -def create_detached_worktree(module_root: Path) -> Optional[Path]: - if not check_running_in_git_repo(module_root): - logger.warning("Module is not in a git repository. Skipping worktree creation.") - return None +def create_detached_worktree() -> Optional[Path]: git_root = git_root_dir() current_time_str = time.strftime("%Y%m%d-%H%M%S") worktree_dir = worktree_dirs / f"{git_root.name}-{current_time_str}" diff --git a/codeflash/discovery/functions_to_optimize.py b/codeflash/discovery/functions_to_optimize.py index 5780f4def..a494d90b0 100644 --- a/codeflash/discovery/functions_to_optimize.py +++ b/codeflash/discovery/functions_to_optimize.py @@ -28,7 +28,7 @@ from codeflash.discovery.discover_unit_tests import discover_unit_tests from codeflash.languages.language_enum import Language from codeflash.languages.registry import get_language_support, get_supported_extensions, is_language_supported -from codeflash.lsp.helpers import is_LSP_enabled +from codeflash.lsp.helpers import is_LSP_enabled, is_subagent_mode from codeflash.models.function_types import FunctionParent, FunctionToOptimize from codeflash.telemetry.posthog_cf import ph @@ -759,7 +759,7 @@ def was_function_previously_optimized( Tuple of (filtered_functions_dict, remaining_count) """ - if is_LSP_enabled(): + if is_LSP_enabled() or is_subagent_mode(): # was_function_previously_optimized is for the checking the optimization duplicates in the github action, no need to do this in the LSP mode return False diff --git a/codeflash/languages/function_optimizer.py b/codeflash/languages/function_optimizer.py index 9b6e01976..f95f2d481 100644 --- a/codeflash/languages/function_optimizer.py +++ b/codeflash/languages/function_optimizer.py @@ -1297,6 +1297,8 @@ def process_single_candidate( # Display runtime information if is_LSP_enabled(): lsp_log(LspMarkdownMessage(markdown=tree_to_markdown(tree))) + elif is_subagent_mode(): + logger.info(tree_to_markdown(tree)) else: console.print(tree) if self.args.benchmark and benchmark_tree: @@ -3009,7 +3011,7 @@ def run_optimized_candidate( optimization_iteration=optimization_candidate_index, testing_time=TOTAL_LOOPING_TIME_EFFECTIVE, enable_coverage=False, - **({"pytest_min_loops": 3, "pytest_max_loops": 100} if subagent else {}), + # **({"pytest_min_loops": 3, "pytest_max_loops": 100} if subagent else {}), ) finally: if self.function_to_optimize.is_async: diff --git a/codeflash/languages/javascript/test_runner.py b/codeflash/languages/javascript/test_runner.py index b32b5c52e..a5f654b40 100644 --- a/codeflash/languages/javascript/test_runner.py +++ b/codeflash/languages/javascript/test_runner.py @@ -1134,9 +1134,9 @@ def run_jest_benchmarking_tests( # Create result with combined stdout result = subprocess.CompletedProcess(args=result.args, returncode=result.returncode, stdout=stdout, stderr="") if result.returncode != 0: - logger.info(f"Jest benchmarking failed with return code {result.returncode}") - logger.info(f"Jest benchmarking stdout: {result.stdout}") - logger.info(f"Jest benchmarking stderr: {result.stderr}") + logger.debug(f"Jest benchmarking failed with return code {result.returncode}") + logger.debug(f"Jest benchmarking stdout: {result.stdout}") + logger.debug(f"Jest benchmarking stderr: {result.stderr}") except subprocess.TimeoutExpired: logger.warning(f"Jest benchmarking timed out after {total_timeout}s") diff --git a/codeflash/optimization/optimizer.py b/codeflash/optimization/optimizer.py index 8e9c08ac2..2638ee3d8 100644 --- a/codeflash/optimization/optimizer.py +++ b/codeflash/optimization/optimizer.py @@ -2,6 +2,7 @@ import copy import os +import sys import tempfile import time from collections import defaultdict @@ -780,15 +781,19 @@ def worktree_mode(self) -> None: if self.current_worktree: return - if check_running_in_git_repo(self.args.module_root): - worktree_dir = create_detached_worktree(self.args.module_root) - if worktree_dir is None: - logger.warning("Failed to create worktree. Skipping optimization.") - return - self.current_worktree = worktree_dir - self.mirror_paths_for_worktree_mode(worktree_dir) - # make sure the tests dir is created in the worktree, this can happen if the original tests dir is empty - Path(self.args.tests_root).mkdir(parents=True, exist_ok=True) + if not check_running_in_git_repo(self.args.module_root): + logger.error("Worktree creation failed because the current directory is not part of a Git repository.") + sys.exit(1) + + worktree_dir = create_detached_worktree() + if worktree_dir is None: + logger.error("Failed to create worktree. Skipping optimization.") + sys.exit(1) + + self.current_worktree = worktree_dir + self.mirror_paths_for_worktree_mode(worktree_dir) + # make sure the tests dir is created in the worktree, this can happen if the original tests dir is empty + Path(self.args.tests_root).mkdir(parents=True, exist_ok=True) def mirror_paths_for_worktree_mode(self, worktree_dir: Path) -> None: original_args = copy.deepcopy(self.args) From c799fde84682fcc5d085108d11a2425ca3e0f2ee Mon Sep 17 00:00:00 2001 From: ali Date: Tue, 24 Mar 2026 15:39:29 +0200 Subject: [PATCH 2/3] typo --- codeflash/languages/function_optimizer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codeflash/languages/function_optimizer.py b/codeflash/languages/function_optimizer.py index f95f2d481..b109f47f9 100644 --- a/codeflash/languages/function_optimizer.py +++ b/codeflash/languages/function_optimizer.py @@ -3011,7 +3011,7 @@ def run_optimized_candidate( optimization_iteration=optimization_candidate_index, testing_time=TOTAL_LOOPING_TIME_EFFECTIVE, enable_coverage=False, - # **({"pytest_min_loops": 3, "pytest_max_loops": 100} if subagent else {}), + **({"pytest_min_loops": 3, "pytest_max_loops": 100} if subagent else {}), ) finally: if self.function_to_optimize.is_async: From 0f24be96d820d7b608610c9434e426d0c37cb1b8 Mon Sep 17 00:00:00 2001 From: ali Date: Tue, 24 Mar 2026 16:32:07 +0200 Subject: [PATCH 3/3] better message --- codeflash/discovery/functions_to_optimize.py | 1 - codeflash/optimization/optimizer.py | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/codeflash/discovery/functions_to_optimize.py b/codeflash/discovery/functions_to_optimize.py index a494d90b0..4d98fbde9 100644 --- a/codeflash/discovery/functions_to_optimize.py +++ b/codeflash/discovery/functions_to_optimize.py @@ -760,7 +760,6 @@ def was_function_previously_optimized( """ if is_LSP_enabled() or is_subagent_mode(): - # was_function_previously_optimized is for the checking the optimization duplicates in the github action, no need to do this in the LSP mode return False # Check optimization status if repository info is provided diff --git a/codeflash/optimization/optimizer.py b/codeflash/optimization/optimizer.py index 2638ee3d8..08e84559f 100644 --- a/codeflash/optimization/optimizer.py +++ b/codeflash/optimization/optimizer.py @@ -787,7 +787,7 @@ def worktree_mode(self) -> None: worktree_dir = create_detached_worktree() if worktree_dir is None: - logger.error("Failed to create worktree. Skipping optimization.") + logger.error("Failed to create a worktree.") sys.exit(1) self.current_worktree = worktree_dir