6565 LINE_SPLITTER_MARKER_PREFIX ,
6666 BestOptimization ,
6767 CodeOptimizationContext ,
68- CodeStringsMarkdown ,
6968 GeneratedTests ,
7069 GeneratedTestsList ,
7170 OptimizationSet ,
9796 from codeflash .either import Result
9897 from codeflash .models .models import (
9998 BenchmarkKey ,
99+ CodeStringsMarkdown ,
100100 CoverageData ,
101101 FunctionCalledInTest ,
102102 FunctionSource ,
@@ -419,7 +419,7 @@ def determine_best_candidate(
419419 try :
420420 did_update = self .replace_function_and_helpers_with_optimized_code (
421421 code_context = code_context ,
422- optimized_code = candidate .source_code . flat ,
422+ optimized_code = candidate .source_code ,
423423 original_helper_code = original_helper_code ,
424424 )
425425 if not did_update :
@@ -680,12 +680,21 @@ def write_code_and_helpers(original_code: str, original_helper_code: dict[Path,
680680 f .write (helper_code )
681681
682682 def reformat_code_and_helpers (
683- self , helper_functions : list [FunctionSource ], path : Path , original_code : str , optimized_code : str
683+ self ,
684+ helper_functions : list [FunctionSource ],
685+ path : Path ,
686+ original_code : str ,
687+ optimized_context : CodeStringsMarkdown ,
684688 ) -> tuple [str , dict [Path , str ]]:
685689 should_sort_imports = not self .args .disable_imports_sorting
686690 if should_sort_imports and isort .code (original_code ) != original_code :
687691 should_sort_imports = False
688692
693+ optimized_code = ""
694+ if optimized_context is not None :
695+ file_to_code_context = optimized_context .file_to_path ()
696+ optimized_code = file_to_code_context .get (str (path .relative_to (self .project_root )), "" )
697+
689698 new_code = format_code (self .args .formatter_cmds , path , optimized_code = optimized_code , check_diff = True )
690699 if should_sort_imports :
691700 new_code = sort_imports (new_code )
@@ -704,16 +713,15 @@ def reformat_code_and_helpers(
704713 return new_code , new_helper_code
705714
706715 def replace_function_and_helpers_with_optimized_code (
707- self , code_context : CodeOptimizationContext , optimized_code : str , original_helper_code : str
716+ self , code_context : CodeOptimizationContext , optimized_code : CodeStringsMarkdown , original_helper_code : str
708717 ) -> bool :
709718 did_update = False
710719 read_writable_functions_by_file_path = defaultdict (set )
711720 read_writable_functions_by_file_path [self .function_to_optimize .file_path ].add (
712721 self .function_to_optimize .qualified_name
713722 )
714723
715- code_strings = CodeStringsMarkdown .parse_splitter_markers (optimized_code ).code_strings
716- file_to_code_context = {str (code_string .file_path ): code_string .code for code_string in code_strings }
724+ file_to_code_context = optimized_code .file_to_path ()
717725
718726 for helper_function in code_context .helper_functions :
719727 if helper_function .jedi_definition .type != "class" :
@@ -739,7 +747,7 @@ def replace_function_and_helpers_with_optimized_code(
739747 preexisting_objects = code_context .preexisting_objects ,
740748 project_root_path = self .project_root ,
741749 )
742- unused_helpers = detect_unused_helper_functions (self .function_to_optimize , code_context , optimized_code )
750+ unused_helpers = detect_unused_helper_functions (self .function_to_optimize , code_context , optimized_code . flat )
743751
744752 # Revert unused helper functions to their original definitions
745753 if unused_helpers :
@@ -1092,15 +1100,15 @@ def find_and_process_best_optimization(
10921100
10931101 self .replace_function_and_helpers_with_optimized_code (
10941102 code_context = code_context ,
1095- optimized_code = best_optimization .candidate .source_code . flat ,
1103+ optimized_code = best_optimization .candidate .source_code ,
10961104 original_helper_code = original_helper_code ,
10971105 )
10981106
10991107 new_code , new_helper_code = self .reformat_code_and_helpers (
11001108 code_context .helper_functions ,
11011109 explanation .file_path ,
11021110 self .function_to_optimize_source_code ,
1103- optimized_code = best_optimization .candidate .source_code . flat ,
1111+ optimized_context = best_optimization .candidate .source_code ,
11041112 )
11051113
11061114 original_code_combined = original_helper_code .copy ()
@@ -1173,8 +1181,9 @@ def process_review(
11731181 )
11741182 new_explanation_raw_str = self .aiservice_client .get_new_explanation (
11751183 source_code = code_context .read_writable_code .flat .replace (
1176- LINE_SPLITTER_MARKER_PREFIX , "# file: "
1177- ), # for better readability to the LLM
1184+ LINE_SPLITTER_MARKER_PREFIX ,
1185+ "# file: " , # for better readability
1186+ ),
11781187 dependency_code = code_context .read_only_context_code ,
11791188 trace_id = self .function_trace_id [:- 4 ] + exp_type if self .experiment_id else self .function_trace_id ,
11801189 optimized_code = best_optimization .candidate .source_code .flat .replace (
0 commit comments