From 5dc1db82cba5344c10f9b8e2eb831f9e27b980c5 Mon Sep 17 00:00:00 2001 From: Saga4 Date: Fri, 27 Jun 2025 23:11:39 +0530 Subject: [PATCH 1/3] Add no-pr argument to CF --- codeflash/lsp/server.py | 1 + 1 file changed, 1 insertion(+) diff --git a/codeflash/lsp/server.py b/codeflash/lsp/server.py index 222a1318c..dff1085ac 100644 --- a/codeflash/lsp/server.py +++ b/codeflash/lsp/server.py @@ -52,5 +52,6 @@ def initialize_optimizer(self, config_file: Path) -> None: args = parse_args() args.config_file = config_file + args.no_pr = True # LSP server should not create PRs args = process_pyproject_config(args) self.optimizer = Optimizer(args) From 74fee304c6b87b53c0c0f32462328a08d1069e41 Mon Sep 17 00:00:00 2001 From: Saga4 Date: Sat, 28 Jun 2025 04:59:26 +0530 Subject: [PATCH 2/3] Fix Optimizable Functions issue and add error logs --- codeflash/lsp/beta.py | 67 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 59 insertions(+), 8 deletions(-) diff --git a/codeflash/lsp/beta.py b/codeflash/lsp/beta.py index aae2b7bee..e6363dccd 100644 --- a/codeflash/lsp/beta.py +++ b/codeflash/lsp/beta.py @@ -36,13 +36,42 @@ def get_optimizable_functions( server: CodeflashLanguageServer, params: OptimizableFunctionsParams ) -> dict[str, list[str]]: file_path = Path(uris.to_fs_path(params.textDocument.uri)) - server.optimizer.args.file = file_path - server.optimizer.args.previous_checkpoint_functions = False - optimizable_funcs, _ = server.optimizer.get_optimizable_functions() - path_to_qualified_names = {} - for path, functions in optimizable_funcs.items(): - path_to_qualified_names[path.as_posix()] = [func.qualified_name for func in functions] - return path_to_qualified_names + server.show_message_log(f"Getting optimizable functions for: {file_path}", "Info") + + # Save original args to restore later + original_file = getattr(server.optimizer.args, 'file', None) + original_function = getattr(server.optimizer.args, 'function', None) + original_checkpoint = getattr(server.optimizer.args, 'previous_checkpoint_functions', None) + + server.show_message_log(f"Original args - file: {original_file}, function: {original_function}", "Info") + + try: + # Set temporary args for this request only + server.optimizer.args.file = file_path + server.optimizer.args.function = None # Always get ALL functions, not just one + server.optimizer.args.previous_checkpoint_functions = False + + server.show_message_log("Calling get_optimizable_functions...", "Info") + optimizable_funcs, _ = server.optimizer.get_optimizable_functions() + + path_to_qualified_names = {} + for path, functions in optimizable_funcs.items(): + path_to_qualified_names[path.as_posix()] = [func.qualified_name for func in functions] + + server.show_message_log(f"Found {len(path_to_qualified_names)} files with functions: {path_to_qualified_names}", "Info") + return path_to_qualified_names + finally: + # Restore original args to prevent state corruption + if original_file is not None: + server.optimizer.args.file = original_file + if original_function is not None: + server.optimizer.args.function = original_function + else: + server.optimizer.args.function = None + if original_checkpoint is not None: + server.optimizer.args.previous_checkpoint_functions = original_checkpoint + + server.show_message_log(f"Restored args - file: {server.optimizer.args.file}, function: {server.optimizer.args.function}", "Info") @server.feature("initializeFunctionOptimization") @@ -50,13 +79,22 @@ def initialize_function_optimization( server: CodeflashLanguageServer, params: FunctionOptimizationParams ) -> dict[str, str]: file_path = Path(uris.to_fs_path(params.textDocument.uri)) + server.show_message_log(f"Initializing optimization for function: {params.functionName} in {file_path}", "Info") + + # IMPORTANT: Store the specific function for optimization, but don't corrupt global state server.optimizer.args.function = params.functionName server.optimizer.args.file = file_path + + server.show_message_log(f"Args set - function: {server.optimizer.args.function}, file: {server.optimizer.args.file}", "Info") + optimizable_funcs, _ = server.optimizer.get_optimizable_functions() if not optimizable_funcs: + server.show_message_log(f"No optimizable functions found for {params.functionName}", "Warning") return {"functionName": params.functionName, "status": "not found", "args": None} + fto = optimizable_funcs.popitem()[1][0] server.optimizer.current_function_being_optimized = fto + server.show_message_log(f"Successfully initialized optimization for {params.functionName}", "Info") return {"functionName": params.functionName, "status": "success"} @@ -139,7 +177,12 @@ def generate_tests(server: CodeflashLanguageServer, params: FunctionOptimization def perform_function_optimization( server: CodeflashLanguageServer, params: FunctionOptimizationParams ) -> dict[str, str]: + server.show_message_log(f"Starting optimization for function: {params.functionName}", "Info") current_function = server.optimizer.current_function_being_optimized + + if not current_function: + server.show_message_log(f"No current function being optimized for {params.functionName}", "Error") + return {"functionName": params.functionName, "status": "error", "message": "No function currently being optimized"} module_prep_result = server.optimizer.prepare_module_for_optimization(current_function.file_path) @@ -214,6 +257,7 @@ def perform_function_optimization( ) if not best_optimization: + server.show_message_log(f"No best optimizations found for function {function_to_optimize_qualified_name}", "Warning") return { "functionName": params.functionName, "status": "error", @@ -221,12 +265,19 @@ def perform_function_optimization( } optimized_source = best_optimization.candidate.source_code + speedup = original_code_baseline.runtime / best_optimization.runtime + + server.show_message_log(f"Optimization completed for {params.functionName} with {speedup:.2f}x speedup", "Info") + + # CRITICAL: Clear the function filter after optimization to prevent state corruption + server.optimizer.args.function = None + server.show_message_log("Cleared function filter to prevent state corruption", "Info") return { "functionName": params.functionName, "status": "success", "message": "Optimization completed successfully", - "extra": f"Speedup: {original_code_baseline.runtime / best_optimization.runtime:.2f}x faster", + "extra": f"Speedup: {speedup:.2f}x faster", "optimization": optimized_source, } From 13885aa15764d88250c9f84d47be7a523baef06b Mon Sep 17 00:00:00 2001 From: Kevin Turcios Date: Fri, 27 Jun 2025 16:55:00 -0700 Subject: [PATCH 3/3] lint --- codeflash/lsp/beta.py | 58 ++++++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 23 deletions(-) diff --git a/codeflash/lsp/beta.py b/codeflash/lsp/beta.py index e6363dccd..63f811cf9 100644 --- a/codeflash/lsp/beta.py +++ b/codeflash/lsp/beta.py @@ -37,28 +37,30 @@ def get_optimizable_functions( ) -> dict[str, list[str]]: file_path = Path(uris.to_fs_path(params.textDocument.uri)) server.show_message_log(f"Getting optimizable functions for: {file_path}", "Info") - + # Save original args to restore later - original_file = getattr(server.optimizer.args, 'file', None) - original_function = getattr(server.optimizer.args, 'function', None) - original_checkpoint = getattr(server.optimizer.args, 'previous_checkpoint_functions', None) - + original_file = getattr(server.optimizer.args, "file", None) + original_function = getattr(server.optimizer.args, "function", None) + original_checkpoint = getattr(server.optimizer.args, "previous_checkpoint_functions", None) + server.show_message_log(f"Original args - file: {original_file}, function: {original_function}", "Info") - + try: # Set temporary args for this request only server.optimizer.args.file = file_path server.optimizer.args.function = None # Always get ALL functions, not just one server.optimizer.args.previous_checkpoint_functions = False - + server.show_message_log("Calling get_optimizable_functions...", "Info") optimizable_funcs, _ = server.optimizer.get_optimizable_functions() - + path_to_qualified_names = {} for path, functions in optimizable_funcs.items(): path_to_qualified_names[path.as_posix()] = [func.qualified_name for func in functions] - - server.show_message_log(f"Found {len(path_to_qualified_names)} files with functions: {path_to_qualified_names}", "Info") + + server.show_message_log( + f"Found {len(path_to_qualified_names)} files with functions: {path_to_qualified_names}", "Info" + ) return path_to_qualified_names finally: # Restore original args to prevent state corruption @@ -70,8 +72,10 @@ def get_optimizable_functions( server.optimizer.args.function = None if original_checkpoint is not None: server.optimizer.args.previous_checkpoint_functions = original_checkpoint - - server.show_message_log(f"Restored args - file: {server.optimizer.args.file}, function: {server.optimizer.args.function}", "Info") + + server.show_message_log( + f"Restored args - file: {server.optimizer.args.file}, function: {server.optimizer.args.function}", "Info" + ) @server.feature("initializeFunctionOptimization") @@ -80,18 +84,20 @@ def initialize_function_optimization( ) -> dict[str, str]: file_path = Path(uris.to_fs_path(params.textDocument.uri)) server.show_message_log(f"Initializing optimization for function: {params.functionName} in {file_path}", "Info") - + # IMPORTANT: Store the specific function for optimization, but don't corrupt global state server.optimizer.args.function = params.functionName server.optimizer.args.file = file_path - - server.show_message_log(f"Args set - function: {server.optimizer.args.function}, file: {server.optimizer.args.file}", "Info") - + + server.show_message_log( + f"Args set - function: {server.optimizer.args.function}, file: {server.optimizer.args.file}", "Info" + ) + optimizable_funcs, _ = server.optimizer.get_optimizable_functions() if not optimizable_funcs: server.show_message_log(f"No optimizable functions found for {params.functionName}", "Warning") return {"functionName": params.functionName, "status": "not found", "args": None} - + fto = optimizable_funcs.popitem()[1][0] server.optimizer.current_function_being_optimized = fto server.show_message_log(f"Successfully initialized optimization for {params.functionName}", "Info") @@ -174,15 +180,19 @@ def generate_tests(server: CodeflashLanguageServer, params: FunctionOptimization @server.feature("performFunctionOptimization") -def perform_function_optimization( +def perform_function_optimization( # noqa: PLR0911 server: CodeflashLanguageServer, params: FunctionOptimizationParams ) -> dict[str, str]: server.show_message_log(f"Starting optimization for function: {params.functionName}", "Info") current_function = server.optimizer.current_function_being_optimized - + if not current_function: server.show_message_log(f"No current function being optimized for {params.functionName}", "Error") - return {"functionName": params.functionName, "status": "error", "message": "No function currently being optimized"} + return { + "functionName": params.functionName, + "status": "error", + "message": "No function currently being optimized", + } module_prep_result = server.optimizer.prepare_module_for_optimization(current_function.file_path) @@ -257,7 +267,9 @@ def perform_function_optimization( ) if not best_optimization: - server.show_message_log(f"No best optimizations found for function {function_to_optimize_qualified_name}", "Warning") + server.show_message_log( + f"No best optimizations found for function {function_to_optimize_qualified_name}", "Warning" + ) return { "functionName": params.functionName, "status": "error", @@ -266,9 +278,9 @@ def perform_function_optimization( optimized_source = best_optimization.candidate.source_code speedup = original_code_baseline.runtime / best_optimization.runtime - + server.show_message_log(f"Optimization completed for {params.functionName} with {speedup:.2f}x speedup", "Info") - + # CRITICAL: Clear the function filter after optimization to prevent state corruption server.optimizer.args.function = None server.show_message_log("Cleared function filter to prevent state corruption", "Info")