Skip to content

Commit b759192

Browse files
Saga4KRRT7
andauthored
saga4/fix optimizable functions (#452)
* Add no-pr argument to CF * Fix Optimizable Functions issue and add error logs * lint --------- Co-authored-by: Kevin Turcios <[email protected]>
1 parent 2b5fa6e commit b759192

File tree

1 file changed

+72
-9
lines changed

1 file changed

+72
-9
lines changed

codeflash/lsp/beta.py

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,71 @@ def get_optimizable_functions(
3636
server: CodeflashLanguageServer, params: OptimizableFunctionsParams
3737
) -> dict[str, list[str]]:
3838
file_path = Path(uris.to_fs_path(params.textDocument.uri))
39-
server.optimizer.args.file = file_path
40-
server.optimizer.args.previous_checkpoint_functions = False
41-
optimizable_funcs, _ = server.optimizer.get_optimizable_functions()
42-
path_to_qualified_names = {}
43-
for path, functions in optimizable_funcs.items():
44-
path_to_qualified_names[path.as_posix()] = [func.qualified_name for func in functions]
45-
return path_to_qualified_names
39+
server.show_message_log(f"Getting optimizable functions for: {file_path}", "Info")
40+
41+
# Save original args to restore later
42+
original_file = getattr(server.optimizer.args, "file", None)
43+
original_function = getattr(server.optimizer.args, "function", None)
44+
original_checkpoint = getattr(server.optimizer.args, "previous_checkpoint_functions", None)
45+
46+
server.show_message_log(f"Original args - file: {original_file}, function: {original_function}", "Info")
47+
48+
try:
49+
# Set temporary args for this request only
50+
server.optimizer.args.file = file_path
51+
server.optimizer.args.function = None # Always get ALL functions, not just one
52+
server.optimizer.args.previous_checkpoint_functions = False
53+
54+
server.show_message_log("Calling get_optimizable_functions...", "Info")
55+
optimizable_funcs, _ = server.optimizer.get_optimizable_functions()
56+
57+
path_to_qualified_names = {}
58+
for path, functions in optimizable_funcs.items():
59+
path_to_qualified_names[path.as_posix()] = [func.qualified_name for func in functions]
60+
61+
server.show_message_log(
62+
f"Found {len(path_to_qualified_names)} files with functions: {path_to_qualified_names}", "Info"
63+
)
64+
return path_to_qualified_names
65+
finally:
66+
# Restore original args to prevent state corruption
67+
if original_file is not None:
68+
server.optimizer.args.file = original_file
69+
if original_function is not None:
70+
server.optimizer.args.function = original_function
71+
else:
72+
server.optimizer.args.function = None
73+
if original_checkpoint is not None:
74+
server.optimizer.args.previous_checkpoint_functions = original_checkpoint
75+
76+
server.show_message_log(
77+
f"Restored args - file: {server.optimizer.args.file}, function: {server.optimizer.args.function}", "Info"
78+
)
4679

4780

4881
@server.feature("initializeFunctionOptimization")
4982
def initialize_function_optimization(
5083
server: CodeflashLanguageServer, params: FunctionOptimizationParams
5184
) -> dict[str, str]:
5285
file_path = Path(uris.to_fs_path(params.textDocument.uri))
86+
server.show_message_log(f"Initializing optimization for function: {params.functionName} in {file_path}", "Info")
87+
88+
# IMPORTANT: Store the specific function for optimization, but don't corrupt global state
5389
server.optimizer.args.function = params.functionName
5490
server.optimizer.args.file = file_path
91+
92+
server.show_message_log(
93+
f"Args set - function: {server.optimizer.args.function}, file: {server.optimizer.args.file}", "Info"
94+
)
95+
5596
optimizable_funcs, _ = server.optimizer.get_optimizable_functions()
5697
if not optimizable_funcs:
98+
server.show_message_log(f"No optimizable functions found for {params.functionName}", "Warning")
5799
return {"functionName": params.functionName, "status": "not found", "args": None}
100+
58101
fto = optimizable_funcs.popitem()[1][0]
59102
server.optimizer.current_function_being_optimized = fto
103+
server.show_message_log(f"Successfully initialized optimization for {params.functionName}", "Info")
60104
return {"functionName": params.functionName, "status": "success"}
61105

62106

@@ -136,11 +180,20 @@ def generate_tests(server: CodeflashLanguageServer, params: FunctionOptimization
136180

137181

138182
@server.feature("performFunctionOptimization")
139-
def perform_function_optimization(
183+
def perform_function_optimization( # noqa: PLR0911
140184
server: CodeflashLanguageServer, params: FunctionOptimizationParams
141185
) -> dict[str, str]:
186+
server.show_message_log(f"Starting optimization for function: {params.functionName}", "Info")
142187
current_function = server.optimizer.current_function_being_optimized
143188

189+
if not current_function:
190+
server.show_message_log(f"No current function being optimized for {params.functionName}", "Error")
191+
return {
192+
"functionName": params.functionName,
193+
"status": "error",
194+
"message": "No function currently being optimized",
195+
}
196+
144197
module_prep_result = server.optimizer.prepare_module_for_optimization(current_function.file_path)
145198

146199
validated_original_code, original_module_ast = module_prep_result
@@ -214,19 +267,29 @@ def perform_function_optimization(
214267
)
215268

216269
if not best_optimization:
270+
server.show_message_log(
271+
f"No best optimizations found for function {function_to_optimize_qualified_name}", "Warning"
272+
)
217273
return {
218274
"functionName": params.functionName,
219275
"status": "error",
220276
"message": f"No best optimizations found for function {function_to_optimize_qualified_name}",
221277
}
222278

223279
optimized_source = best_optimization.candidate.source_code
280+
speedup = original_code_baseline.runtime / best_optimization.runtime
281+
282+
server.show_message_log(f"Optimization completed for {params.functionName} with {speedup:.2f}x speedup", "Info")
283+
284+
# CRITICAL: Clear the function filter after optimization to prevent state corruption
285+
server.optimizer.args.function = None
286+
server.show_message_log("Cleared function filter to prevent state corruption", "Info")
224287

225288
return {
226289
"functionName": params.functionName,
227290
"status": "success",
228291
"message": "Optimization completed successfully",
229-
"extra": f"Speedup: {original_code_baseline.runtime / best_optimization.runtime:.2f}x faster",
292+
"extra": f"Speedup: {speedup:.2f}x faster",
230293
"optimization": optimized_source,
231294
}
232295

0 commit comments

Comments
 (0)