@@ -183,18 +183,54 @@ 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+ assert self .original_args_and_test_cfg is not None
197+ original_args , _ = self .original_args_and_test_cfg
198+ project_root = original_args .project_root
199+ module_root = original_args .module_root
200+
201+ result = get_functions_to_optimize (
187202 optimize_all = self .args .all ,
188203 replay_test = self .args .replay_test ,
189204 file = self .args .file ,
190205 only_get_this_function = self .args .function ,
191206 test_cfg = self .test_cfg ,
192207 ignore_paths = self .args .ignore_paths ,
193- project_root = self . args . project_root ,
194- module_root = self . args . module_root ,
208+ project_root = project_root ,
209+ module_root = module_root ,
195210 previous_checkpoint_functions = self .args .previous_checkpoint_functions ,
196211 )
197212
213+ # Remap discovered file paths from the original repo to the worktree so
214+ # downstream optimization reads/writes happen in the worktree.
215+ if use_original_roots :
216+ import dataclasses
217+
218+ assert self .current_worktree is not None
219+ original_git_root = git_root_dir ()
220+ file_to_funcs , count , trace = result
221+ remapped : dict [Path , list [FunctionToOptimize ]] = {}
222+ for file_path , funcs in file_to_funcs .items ():
223+ new_path = mirror_path (Path (file_path ), original_git_root , self .current_worktree )
224+ remapped [new_path ] = [
225+ dataclasses .replace (
226+ func , file_path = mirror_path (func .file_path , original_git_root , self .current_worktree )
227+ )
228+ for func in funcs
229+ ]
230+ return remapped , count , trace
231+
232+ return result
233+
198234 def create_function_optimizer (
199235 self ,
200236 function_to_optimize : FunctionToOptimize ,
0 commit comments