From 0749621bee492f29128ca447e0c2c69c9e3f92e4 Mon Sep 17 00:00:00 2001 From: Kevin Turcios Date: Wed, 18 Feb 2026 05:57:17 -0500 Subject: [PATCH 1/4] fix: use original repo roots for filtering in worktree diff mode In --worktree mode, get_git_diff resolves file paths from cwd (the original repo), but module_root/project_root are mirrored to the worktree. This caused filter_functions to reject all diff-discovered functions as "outside module-root". Use the pre-mirror roots for filtering, then remap file paths to the worktree for downstream use. --- codeflash/optimization/optimizer.py | 38 ++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/codeflash/optimization/optimizer.py b/codeflash/optimization/optimizer.py index b8f42010f..15284ba6f 100644 --- a/codeflash/optimization/optimizer.py +++ b/codeflash/optimization/optimizer.py @@ -183,18 +183,50 @@ class Optimizer: """Discover functions to optimize.""" from codeflash.discovery.functions_to_optimize import get_functions_to_optimize - return get_functions_to_optimize( + # In worktree mode for git-diff discovery, file paths come from the original repo + # (via get_git_diff using cwd), but module_root/project_root have been mirrored to + # the worktree. Use the original roots for filtering so path comparisons match, + # then remap the discovered file paths to the worktree. + project_root = self.args.project_root + module_root = self.args.module_root + use_original_roots = ( + self.current_worktree and self.original_args_and_test_cfg and not self.args.all and not self.args.file + ) + if use_original_roots: + original_args, _ = self.original_args_and_test_cfg + project_root = original_args.project_root + module_root = original_args.module_root + + result = get_functions_to_optimize( optimize_all=self.args.all, replay_test=self.args.replay_test, file=self.args.file, only_get_this_function=self.args.function, test_cfg=self.test_cfg, ignore_paths=self.args.ignore_paths, - project_root=self.args.project_root, - module_root=self.args.module_root, + project_root=project_root, + module_root=module_root, previous_checkpoint_functions=self.args.previous_checkpoint_functions, ) + # Remap discovered file paths from the original repo to the worktree so + # downstream optimization reads/writes happen in the worktree. + if use_original_roots: + import dataclasses + + original_git_root = git_root_dir() + file_to_funcs, count, trace = result + remapped: dict[Path, list[FunctionToOptimize]] = {} + for file_path, funcs in file_to_funcs.items(): + new_path = mirror_path(Path(file_path), original_git_root, self.current_worktree) + remapped[new_path] = [ + dataclasses.replace(func, file_path=mirror_path(func.file_path, original_git_root, self.current_worktree)) + for func in funcs + ] + return remapped, count, trace + + return result + def create_function_optimizer( self, function_to_optimize: FunctionToOptimize, From d43d9aeb4b8875144c677c09ab6f010c07573508 Mon Sep 17 00:00:00 2001 From: "claude[bot]" <41898282+claude[bot]@users.noreply.github.com> Date: Wed, 18 Feb 2026 11:00:58 +0000 Subject: [PATCH 2/4] style: auto-fix ruff formatting for long line --- 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 15284ba6f..7964e31a7 100644 --- a/codeflash/optimization/optimizer.py +++ b/codeflash/optimization/optimizer.py @@ -220,7 +220,9 @@ class Optimizer: for file_path, funcs in file_to_funcs.items(): new_path = mirror_path(Path(file_path), original_git_root, self.current_worktree) remapped[new_path] = [ - dataclasses.replace(func, file_path=mirror_path(func.file_path, original_git_root, self.current_worktree)) + dataclasses.replace( + func, file_path=mirror_path(func.file_path, original_git_root, self.current_worktree) + ) for func in funcs ] return remapped, count, trace From 6a19b9d4b8232b8c034e8b424f21d4846ecfd7cb Mon Sep 17 00:00:00 2001 From: "claude[bot]" <41898282+claude[bot]@users.noreply.github.com> Date: Wed, 18 Feb 2026 11:02:09 +0000 Subject: [PATCH 3/4] fix: add type assertions for mypy narrowing in worktree path remapping --- codeflash/optimization/optimizer.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/codeflash/optimization/optimizer.py b/codeflash/optimization/optimizer.py index 7964e31a7..06540ca85 100644 --- a/codeflash/optimization/optimizer.py +++ b/codeflash/optimization/optimizer.py @@ -193,6 +193,7 @@ class Optimizer: self.current_worktree and self.original_args_and_test_cfg and not self.args.all and not self.args.file ) if use_original_roots: + assert self.original_args_and_test_cfg is not None original_args, _ = self.original_args_and_test_cfg project_root = original_args.project_root module_root = original_args.module_root @@ -214,6 +215,7 @@ class Optimizer: if use_original_roots: import dataclasses + assert self.current_worktree is not None original_git_root = git_root_dir() file_to_funcs, count, trace = result remapped: dict[Path, list[FunctionToOptimize]] = {} From 305210b1f7b4ba9b5042658db9d10638393cc15b Mon Sep 17 00:00:00 2001 From: Kevin Turcios Date: Wed, 18 Feb 2026 06:32:12 -0500 Subject: [PATCH 4/4] fix: resolve git root from module_root for worktree PR creation git_root_dir() searches from CWD (original repo), but in worktree mode file paths have been remapped to the worktree. This caused relative_to() to raise ValueError when creating PRs. Search from module_root instead so root_dir is always in the same path space as the file paths. --- codeflash/optimization/function_optimizer.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/codeflash/optimization/function_optimizer.py b/codeflash/optimization/function_optimizer.py index 0a515076c..372d7892f 100644 --- a/codeflash/optimization/function_optimizer.py +++ b/codeflash/optimization/function_optimizer.py @@ -13,6 +13,7 @@ from pathlib import Path from typing import TYPE_CHECKING, Callable import libcst as cst +from git import Repo as GitRepo from rich.console import Group from rich.panel import Panel from rich.syntax import Syntax @@ -2222,11 +2223,11 @@ class FunctionOptimizer: console.print(Panel(panel_content, title="Optimization Review", border_style=display_info[1])) if raise_pr or staging_review: - data["root_dir"] = git_root_dir() + data["root_dir"] = git_root_dir(GitRepo(str(self.args.module_root), search_parent_directories=True)) if raise_pr and not staging_review and opt_review_result.review != "low": # Ensure root_dir is set for PR creation (needed for async functions that skip opt_review) if "root_dir" not in data: - data["root_dir"] = git_root_dir() + data["root_dir"] = git_root_dir(GitRepo(str(self.args.module_root), search_parent_directories=True)) data["git_remote"] = self.args.git_remote # Remove language from data dict as check_create_pr doesn't accept it pr_data = {k: v for k, v in data.items() if k != "language"}