Skip to content

Commit 0749621

Browse files
committed
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.
1 parent 9d23a0e commit 0749621

1 file changed

Lines changed: 35 additions & 3 deletions

File tree

codeflash/optimization/optimizer.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,18 +183,50 @@ def get_optimizable_functions(self) -> tuple[dict[Path, list[FunctionToOptimize]
183183
"""Discover functions to optimize."""
184184
from codeflash.discovery.functions_to_optimize import get_functions_to_optimize
185185

186-
return get_functions_to_optimize(
186+
# In worktree mode for git-diff discovery, file paths come from the original repo
187+
# (via get_git_diff using cwd), but module_root/project_root have been mirrored to
188+
# the worktree. Use the original roots for filtering so path comparisons match,
189+
# then remap the discovered file paths to the worktree.
190+
project_root = self.args.project_root
191+
module_root = self.args.module_root
192+
use_original_roots = (
193+
self.current_worktree and self.original_args_and_test_cfg and not self.args.all and not self.args.file
194+
)
195+
if use_original_roots:
196+
original_args, _ = self.original_args_and_test_cfg
197+
project_root = original_args.project_root
198+
module_root = original_args.module_root
199+
200+
result = get_functions_to_optimize(
187201
optimize_all=self.args.all,
188202
replay_test=self.args.replay_test,
189203
file=self.args.file,
190204
only_get_this_function=self.args.function,
191205
test_cfg=self.test_cfg,
192206
ignore_paths=self.args.ignore_paths,
193-
project_root=self.args.project_root,
194-
module_root=self.args.module_root,
207+
project_root=project_root,
208+
module_root=module_root,
195209
previous_checkpoint_functions=self.args.previous_checkpoint_functions,
196210
)
197211

212+
# Remap discovered file paths from the original repo to the worktree so
213+
# downstream optimization reads/writes happen in the worktree.
214+
if use_original_roots:
215+
import dataclasses
216+
217+
original_git_root = git_root_dir()
218+
file_to_funcs, count, trace = result
219+
remapped: dict[Path, list[FunctionToOptimize]] = {}
220+
for file_path, funcs in file_to_funcs.items():
221+
new_path = mirror_path(Path(file_path), original_git_root, self.current_worktree)
222+
remapped[new_path] = [
223+
dataclasses.replace(func, file_path=mirror_path(func.file_path, original_git_root, self.current_worktree))
224+
for func in funcs
225+
]
226+
return remapped, count, trace
227+
228+
return result
229+
198230
def create_function_optimizer(
199231
self,
200232
function_to_optimize: FunctionToOptimize,

0 commit comments

Comments
 (0)