Skip to content

Commit 2bcd91c

Browse files
authored
Merge pull request #1522 from codeflash-ai/cf-fix-worktree-module-root-path
Fix worktree mode filtering out all diff-discovered functions
2 parents 9d23a0e + 305210b commit 2bcd91c

2 files changed

Lines changed: 42 additions & 5 deletions

File tree

codeflash/optimization/function_optimizer.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from typing import TYPE_CHECKING, Callable
1414

1515
import libcst as cst
16+
from git import Repo as GitRepo
1617
from rich.console import Group
1718
from rich.panel import Panel
1819
from rich.syntax import Syntax
@@ -2222,11 +2223,11 @@ def process_review(
22222223
console.print(Panel(panel_content, title="Optimization Review", border_style=display_info[1]))
22232224

22242225
if raise_pr or staging_review:
2225-
data["root_dir"] = git_root_dir()
2226+
data["root_dir"] = git_root_dir(GitRepo(str(self.args.module_root), search_parent_directories=True))
22262227
if raise_pr and not staging_review and opt_review_result.review != "low":
22272228
# Ensure root_dir is set for PR creation (needed for async functions that skip opt_review)
22282229
if "root_dir" not in data:
2229-
data["root_dir"] = git_root_dir()
2230+
data["root_dir"] = git_root_dir(GitRepo(str(self.args.module_root), search_parent_directories=True))
22302231
data["git_remote"] = self.args.git_remote
22312232
# Remove language from data dict as check_create_pr doesn't accept it
22322233
pr_data = {k: v for k, v in data.items() if k != "language"}

codeflash/optimization/optimizer.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)