Skip to content

Commit b955ad8

Browse files
worktrees changes
1 parent c595a7a commit b955ad8

File tree

5 files changed

+30
-13
lines changed

5 files changed

+30
-13
lines changed

codeflash/code_utils/git_utils.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from rich.prompt import Confirm
1616
from unidiff import PatchSet
1717

18-
from codeflash.cli_cmds.console import logger
18+
from codeflash.cli_cmds.console import console, logger
1919
from codeflash.code_utils.compat import codeflash_cache_dir
2020
from codeflash.code_utils.config_consts import N_CANDIDATES
2121

@@ -212,13 +212,17 @@ def create_detached_worktree(module_root: Path) -> Optional[Path]:
212212
current_time_str = time.strftime("%Y%m%d-%H%M%S")
213213
worktree_dir = worktree_dirs / f"{git_root.name}-{current_time_str}"
214214

215-
result = subprocess.run(["git", "worktree", "add", "-d", str(worktree_dir)], cwd=git_root, check=True)
215+
result = subprocess.run(
216+
["git", "worktree", "add", "-d", str(worktree_dir)],
217+
cwd=git_root,
218+
check=True,
219+
stdout=subprocess.DEVNULL if console.quiet else None,
220+
stderr=subprocess.DEVNULL if console.quiet else None,
221+
)
216222
if result.returncode != 0:
217223
logger.error(f"Failed to create worktree: {result.stderr}")
218224
return None
219225

220-
print(result.stdout)
221-
222226
# Get uncommitted diff from the original repo
223227
repository = git.Repo(module_root, search_parent_directories=True)
224228
uni_diff_text = repository.git.diff(None, "HEAD", ignore_blank_lines=True, ignore_space_at_eol=True)

codeflash/lsp/beta.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from pygls import uris
1010

1111
from codeflash.api.cfapi import get_codeflash_api_key, get_user_id
12+
from codeflash.code_utils.git_utils import create_diff_from_worktree
1213
from codeflash.code_utils.shell_utils import save_api_key_to_rc
1314
from codeflash.either import is_successful
1415
from codeflash.lsp.server import CodeflashLanguageServer, CodeflashLanguageServerProtocol
@@ -331,17 +332,23 @@ def perform_function_optimization( # noqa: PLR0911
331332
"message": f"No best optimizations found for function {function_to_optimize_qualified_name}",
332333
}
333334

335+
# generate a patch for the optimization
336+
relative_file_paths = [code_string.file_path for code_string in code_context.read_writable_code.code_strings]
337+
patch_path = create_diff_from_worktree(
338+
server.optimizer.current_worktree,
339+
relative_file_paths,
340+
server.optimizer.current_function_optimizer.function_to_optimize.qualified_name,
341+
)
342+
334343
optimized_source = best_optimization.candidate.source_code.markdown
335344
speedup = original_code_baseline.runtime / best_optimization.runtime
336345

337346
server.show_message_log(f"Optimization completed for {params.functionName} with {speedup:.2f}x speedup", "Info")
338-
diff_patch_files = server.optimizer.patch_files
339347

340348
# CRITICAL: Clear the function filter after optimization to prevent state corruption
349+
server.optimizer.cleanup_temporary_paths()
341350
server.optimizer.args.function = None
342-
server.optimizer.patch_files = []
343351
server.optimizer.current_worktree = None
344-
server.optimizer.cleanup_temporary_paths()
345352
server.show_message_log("Cleared function filter to prevent state corruption", "Info")
346353

347354
return {
@@ -350,5 +357,5 @@ def perform_function_optimization( # noqa: PLR0911
350357
"message": "Optimization completed successfully",
351358
"extra": f"Speedup: {speedup:.2f}x faster",
352359
"optimization": optimized_source,
353-
"diff_patch_files": diff_patch_files,
360+
"patch_path": patch_path,
354361
}

codeflash/lsp/server.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def prepare_optimizer_arguments(self, config_file: Path) -> None:
5555
args = parse_args()
5656
args.config_file = config_file
5757
args.no_pr = True # LSP server should not create PRs
58+
args.worktree = True
5859
args = process_pyproject_config(args)
5960
self.args = args
6061
# avoid initializing the optimizer during initialization, because it can cause an error if the api key is invalid

codeflash/optimization/function_optimizer.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,6 +1252,10 @@ def process_review(
12521252
trace_id=self.function_trace_id, is_optimization_found=best_optimization is not None
12531253
)
12541254

1255+
# If worktree mode, do not revert code and helpers, we create snapshot commit after each function optimization
1256+
if self.args.worktree:
1257+
return
1258+
12551259
if raise_pr and (
12561260
self.args.all
12571261
or env_utils.get_pr_number()

codeflash/optimization/optimizer.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -414,16 +414,17 @@ def cleanup_replay_tests(self) -> None:
414414
cleanup_paths([self.replay_tests_dir])
415415

416416
def cleanup_temporary_paths(self) -> None:
417-
if self.current_function_optimizer:
418-
self.current_function_optimizer.cleanup_generated_files()
419-
420417
if hasattr(get_run_tmp_file, "tmpdir"):
421418
get_run_tmp_file.tmpdir.cleanup()
422419
del get_run_tmp_file.tmpdir
423-
paths_to_clean = [self.test_cfg.concolic_test_root_dir, self.replay_tests_dir]
420+
424421
if self.current_worktree:
425422
remove_worktree(self.current_worktree)
426-
cleanup_paths(paths_to_clean)
423+
return
424+
425+
if self.current_function_optimizer:
426+
self.current_function_optimizer.cleanup_generated_files()
427+
cleanup_paths([self.test_cfg.concolic_test_root_dir, self.replay_tests_dir])
427428

428429
def worktree_mode(self) -> None:
429430
if self.current_worktree:

0 commit comments

Comments
 (0)