99from pygls import uris
1010
1111from codeflash .api .cfapi import get_codeflash_api_key , get_user_id
12+ from codeflash .code_utils .git_utils import create_diff_from_worktree
1213from codeflash .code_utils .shell_utils import save_api_key_to_rc
1314from codeflash .either import is_successful
1415from 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 }
0 commit comments