|
1 | 1 | import contextlib |
2 | 2 | import os |
3 | | -from pathlib import Path |
4 | 3 |
|
5 | 4 | from codeflash.cli_cmds.console import code_print |
6 | 5 | from codeflash.code_utils.git_worktree_utils import create_diff_patch_from_worktree |
7 | 6 | from codeflash.either import is_successful |
8 | 7 | from codeflash.lsp.server import CodeflashLanguageServer |
9 | 8 |
|
10 | 9 |
|
11 | | -# ruff: noqa: PLR0911, ANN001 |
12 | | -def sync_perform_optimization(server: CodeflashLanguageServer, params) -> dict[str, str]: |
| 10 | +def sync_perform_optimization(server: CodeflashLanguageServer, params) -> dict[str, str]: # noqa: ANN001 |
13 | 11 | server.show_message_log(f"Starting optimization for function: {params.functionName}", "Info") |
14 | | - current_function = server.optimizer.current_function_being_optimized |
15 | | - |
16 | | - if not current_function: |
17 | | - server.show_message_log(f"No current function being optimized for {params.functionName}", "Error") |
18 | | - return { |
19 | | - "functionName": params.functionName, |
20 | | - "status": "error", |
21 | | - "message": "No function currently being optimized", |
22 | | - } |
23 | | - |
24 | | - module_prep_result = server.optimizer.prepare_module_for_optimization(current_function.file_path) |
25 | | - if not module_prep_result: |
26 | | - return { |
27 | | - "functionName": params.functionName, |
28 | | - "status": "error", |
29 | | - "message": "Failed to prepare module for optimization", |
30 | | - } |
31 | | - |
32 | | - validated_original_code, original_module_ast = module_prep_result |
33 | | - |
34 | | - function_optimizer = server.optimizer.create_function_optimizer( |
35 | | - current_function, |
36 | | - function_to_optimize_source_code=validated_original_code[current_function.file_path].source_code, |
37 | | - original_module_ast=original_module_ast, |
38 | | - original_module_path=current_function.file_path, |
39 | | - function_to_tests={}, |
40 | | - ) |
41 | | - |
42 | | - server.optimizer.current_function_optimizer = function_optimizer |
43 | | - if not function_optimizer: |
44 | | - return {"functionName": params.functionName, "status": "error", "message": "No function optimizer found"} |
45 | | - |
46 | | - initialization_result = function_optimizer.can_be_optimized() |
47 | | - if not is_successful(initialization_result): |
48 | | - return {"functionName": params.functionName, "status": "error", "message": initialization_result.failure()} |
49 | | - |
50 | | - should_run_experiment, code_context, original_helper_code = initialization_result.unwrap() |
| 12 | + should_run_experiment, code_context, original_helper_code = server.current_optimization_init_result |
| 13 | + function_optimizer = server.optimizer.current_function_optimizer |
| 14 | + current_function = function_optimizer.function_to_optimize |
51 | 15 |
|
52 | 16 | code_print( |
53 | 17 | code_context.read_writable_code.flat, |
@@ -124,29 +88,26 @@ def sync_perform_optimization(server: CodeflashLanguageServer, params) -> dict[s |
124 | 88 | # generate a patch for the optimization |
125 | 89 | relative_file_paths = [code_string.file_path for code_string in code_context.read_writable_code.code_strings] |
126 | 90 | speedup = original_code_baseline.runtime / best_optimization.runtime |
127 | | - # get the original file path in the actual project (not in the worktree) |
128 | | - original_args, _ = server.optimizer.original_args_and_test_cfg |
129 | | - relative_file_path = current_function.file_path.relative_to(server.optimizer.current_worktree) |
130 | | - original_file_path = Path(original_args.project_root / relative_file_path).resolve() |
131 | | - |
132 | | - metadata = create_diff_patch_from_worktree( |
133 | | - server.optimizer.current_worktree, |
134 | | - relative_file_paths, |
135 | | - metadata_input={ |
136 | | - "fto_name": function_to_optimize_qualified_name, |
137 | | - "explanation": best_optimization.explanation_v2, |
138 | | - "file_path": str(original_file_path), |
139 | | - "speedup": speedup, |
140 | | - }, |
| 91 | + |
| 92 | + patch_path = create_diff_patch_from_worktree( |
| 93 | + server.optimizer.current_worktree, relative_file_paths, function_to_optimize_qualified_name |
141 | 94 | ) |
142 | 95 |
|
| 96 | + if not patch_path: |
| 97 | + return { |
| 98 | + "functionName": params.functionName, |
| 99 | + "status": "error", |
| 100 | + "message": "Failed to create a patch for optimization", |
| 101 | + } |
| 102 | + |
143 | 103 | server.show_message_log(f"Optimization completed for {params.functionName} with {speedup:.2f}x speedup", "Info") |
| 104 | + |
144 | 105 | return { |
145 | 106 | "functionName": params.functionName, |
146 | 107 | "status": "success", |
147 | 108 | "message": "Optimization completed successfully", |
148 | 109 | "extra": f"Speedup: {speedup:.2f}x faster", |
149 | | - "patch_file": metadata["patch_path"], |
150 | | - "patch_id": metadata["id"], |
| 110 | + "patch_file": str(patch_path), |
| 111 | + "task_id": params.task_id, |
151 | 112 | "explanation": best_optimization.explanation_v2, |
152 | 113 | } |
0 commit comments