Skip to content

Commit 74fee30

Browse files
committed
Fix Optimizable Functions issue and add error logs
1 parent 5dc1db8 commit 74fee30

File tree

1 file changed

+59
-8
lines changed

1 file changed

+59
-8
lines changed

codeflash/lsp/beta.py

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,65 @@ 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(f"Found {len(path_to_qualified_names)} files with functions: {path_to_qualified_names}", "Info")
62+
return path_to_qualified_names
63+
finally:
64+
# Restore original args to prevent state corruption
65+
if original_file is not None:
66+
server.optimizer.args.file = original_file
67+
if original_function is not None:
68+
server.optimizer.args.function = original_function
69+
else:
70+
server.optimizer.args.function = None
71+
if original_checkpoint is not None:
72+
server.optimizer.args.previous_checkpoint_functions = original_checkpoint
73+
74+
server.show_message_log(f"Restored args - file: {server.optimizer.args.file}, function: {server.optimizer.args.function}", "Info")
4675

4776

4877
@server.feature("initializeFunctionOptimization")
4978
def initialize_function_optimization(
5079
server: CodeflashLanguageServer, params: FunctionOptimizationParams
5180
) -> dict[str, str]:
5281
file_path = Path(uris.to_fs_path(params.textDocument.uri))
82+
server.show_message_log(f"Initializing optimization for function: {params.functionName} in {file_path}", "Info")
83+
84+
# IMPORTANT: Store the specific function for optimization, but don't corrupt global state
5385
server.optimizer.args.function = params.functionName
5486
server.optimizer.args.file = file_path
87+
88+
server.show_message_log(f"Args set - function: {server.optimizer.args.function}, file: {server.optimizer.args.file}", "Info")
89+
5590
optimizable_funcs, _ = server.optimizer.get_optimizable_functions()
5691
if not optimizable_funcs:
92+
server.show_message_log(f"No optimizable functions found for {params.functionName}", "Warning")
5793
return {"functionName": params.functionName, "status": "not found", "args": None}
94+
5895
fto = optimizable_funcs.popitem()[1][0]
5996
server.optimizer.current_function_being_optimized = fto
97+
server.show_message_log(f"Successfully initialized optimization for {params.functionName}", "Info")
6098
return {"functionName": params.functionName, "status": "success"}
6199

62100

@@ -139,7 +177,12 @@ def generate_tests(server: CodeflashLanguageServer, params: FunctionOptimization
139177
def perform_function_optimization(
140178
server: CodeflashLanguageServer, params: FunctionOptimizationParams
141179
) -> dict[str, str]:
180+
server.show_message_log(f"Starting optimization for function: {params.functionName}", "Info")
142181
current_function = server.optimizer.current_function_being_optimized
182+
183+
if not current_function:
184+
server.show_message_log(f"No current function being optimized for {params.functionName}", "Error")
185+
return {"functionName": params.functionName, "status": "error", "message": "No function currently being optimized"}
143186

144187
module_prep_result = server.optimizer.prepare_module_for_optimization(current_function.file_path)
145188

@@ -214,19 +257,27 @@ def perform_function_optimization(
214257
)
215258

216259
if not best_optimization:
260+
server.show_message_log(f"No best optimizations found for function {function_to_optimize_qualified_name}", "Warning")
217261
return {
218262
"functionName": params.functionName,
219263
"status": "error",
220264
"message": f"No best optimizations found for function {function_to_optimize_qualified_name}",
221265
}
222266

223267
optimized_source = best_optimization.candidate.source_code
268+
speedup = original_code_baseline.runtime / best_optimization.runtime
269+
270+
server.show_message_log(f"Optimization completed for {params.functionName} with {speedup:.2f}x speedup", "Info")
271+
272+
# CRITICAL: Clear the function filter after optimization to prevent state corruption
273+
server.optimizer.args.function = None
274+
server.show_message_log("Cleared function filter to prevent state corruption", "Info")
224275

225276
return {
226277
"functionName": params.functionName,
227278
"status": "success",
228279
"message": "Optimization completed successfully",
229-
"extra": f"Speedup: {original_code_baseline.runtime / best_optimization.runtime:.2f}x faster",
280+
"extra": f"Speedup: {speedup:.2f}x faster",
230281
"optimization": optimized_source,
231282
}
232283

0 commit comments

Comments
 (0)