From df0b17641115e81579300e985a0a8c23813a2a0a Mon Sep 17 00:00:00 2001 From: aseembits93 Date: Mon, 22 Sep 2025 17:35:53 -0700 Subject: [PATCH 01/10] wip --- codeflash/api/aiservice.py | 106 +++++++++++++++++++ codeflash/optimization/function_optimizer.py | 5 + 2 files changed, 111 insertions(+) diff --git a/codeflash/api/aiservice.py b/codeflash/api/aiservice.py index ca7d13425..fdf3ce106 100644 --- a/codeflash/api/aiservice.py +++ b/codeflash/api/aiservice.py @@ -513,6 +513,112 @@ def generate_regression_tests( # noqa: D417 ph("cli-testgen-error-response", {"response_status_code": response.status_code, "error": response.text}) return None + def get_optimization_impact(self, original_code, new_code, explanation, existing_tests_source, generated_original_test_source, function_trace_id, coverage_message, replay_tests, concolic_tests) -> list[OptimizedCandidate]: + """Optimize the given python code for performance by making a request to the Django endpoint. + + Args: + request: A list of optimization candidate details for refinement + + Returns: + ------- + - List[OptimizationCandidate]: A list of Optimization Candidates. + + """ + # """{ + # "original_code": original_code_combined, + # "new_code": new_code_combined, + # "explanation": new_explanation, + # "existing_tests_source": existing_tests, + # "generated_original_test_source": generated_tests_str, + # "function_trace_id": self.function_trace_id[:-4] + exp_type + # if self.experiment_id + # else self.function_trace_id, + # "coverage_message": coverage_message, + # "replay_tests": replay_tests, + # "concolic_tests": concolic_tests, + # }""" + # logger.info("Creating a new PR with the optimized code...") + # console.rule() + # owner, repo = get_repo_owner_and_name(git_repo, git_remote) + # logger.info(f"Pushing to {git_remote} - Owner: {owner}, Repo: {repo}") + # console.rule() + # if not check_and_push_branch(git_repo, git_remote, wait_for_push=True): + # logger.warning("⏭️ Branch is not pushed, skipping PR creation...") + # return + # relative_path = explanation.file_path.relative_to(root_dir).as_posix() + # base_branch = get_current_branch() + # build_file_changes = { + # Path(p).relative_to(root_dir).as_posix(): FileDiffContent( + # oldContent=original_code[p], newContent=new_code[p] + # ) + # for p in original_code + # } + # + # response = cfapi.create_pr( + # owner=owner, + # repo=repo, + # base_branch=base_branch, + # file_changes=build_file_changes, + # pr_comment=PrComment( + # optimization_explanation=explanation.explanation_message(), + # best_runtime=explanation.best_runtime_ns, + # original_runtime=explanation.original_runtime_ns, + # function_name=explanation.function_name, + # relative_file_path=relative_path, + # speedup_x=explanation.speedup_x, + # speedup_pct=explanation.speedup_pct, + # winning_behavior_test_results=explanation.winning_behavior_test_results, + # winning_benchmarking_test_results=explanation.winning_benchmarking_test_results, + # benchmark_details=explanation.benchmark_details, + # ), + # existing_tests=existing_tests_source, + # generated_tests=generated_original_test_source, + # trace_id=function_trace_id, + # coverage_message=coverage_message, + # replay_tests=replay_tests, + # concolic_tests=concolic_tests, + # ) + # if response.ok: + # pr_id = response.text + # pr_url = github_pr_url(owner, repo, pr_id) + # logger.info(f"Successfully created a new PR #{pr_id} with the optimized code: {pr_url}") + # else: + # logger.error( + # f"Optimization was successful, but I failed to create a PR with the optimized code." + # f" Response from server was: {response.text}" + # ) + # console.rule() + logger.info("!lsp|Computing Optimization Impact…") + payload = { + "original_code": original_code, + "new_code": new_code, + "existing_tests_source": existing_tests_source, + "generated_original_test_source": generated_original_test_source, + "function_trace_id": function_trace_id, + "coverage_message": coverage_message, + "replay_tests": replay_tests, + "concolic_tests": concolic_tests, + } + console.rule() + try: + response = self.make_ai_service_request("/optimization_impact", payload=payload, timeout=600) + except requests.exceptions.RequestException as e: + logger.exception(f"Error generating optimization refinements: {e}") + ph("cli-optimize-error-caught", {"error": str(e)}) + return '' + + if response.status_code == 200: + impact = response.json()["impact"] + return impact + try: + error = response.json()["error"] + except Exception: + error = response.text + logger.error(f"Error generating impact candidates: {response.status_code} - {error}") + ph("cli-optimize-error-response", {"response_status_code": response.status_code, "error": error}) + console.rule() + return [] + class LocalAiServiceClient(AiServiceClient): """Client for interacting with the local AI service.""" diff --git a/codeflash/optimization/function_optimizer.py b/codeflash/optimization/function_optimizer.py index 52a0b6d57..6ec279863 100644 --- a/codeflash/optimization/function_optimizer.py +++ b/codeflash/optimization/function_optimizer.py @@ -1401,6 +1401,11 @@ def process_review( } raise_pr = not self.args.no_pr + # modify argument of staging vs pr based on the impact + opt_impact_response = self.aiservice_client.get_optimization_impact(**data) + if opt_impact_response in ['medium', 'low']: + raise_pr = False + self.args.staging_review = True if raise_pr or self.args.staging_review: data["root_dir"] = git_root_dir() From aefedda73e20f1b0cfcb873dd88e200bbd4fe1af Mon Sep 17 00:00:00 2001 From: aseembits93 Date: Mon, 22 Sep 2025 21:31:35 -0700 Subject: [PATCH 02/10] works, todo cleanup --- codeflash/api/aiservice.py | 107 ++++++------------- codeflash/optimization/function_optimizer.py | 13 ++- 2 files changed, 38 insertions(+), 82 deletions(-) diff --git a/codeflash/api/aiservice.py b/codeflash/api/aiservice.py index fdf3ce106..c570ecfc7 100644 --- a/codeflash/api/aiservice.py +++ b/codeflash/api/aiservice.py @@ -4,14 +4,15 @@ import os import platform import time -from typing import TYPE_CHECKING, Any - +from typing import TYPE_CHECKING, Any, Literal +from pathlib import Path import requests from pydantic.json import pydantic_encoder from codeflash.cli_cmds.console import console, logger from codeflash.code_utils.env_utils import get_codeflash_api_key from codeflash.code_utils.git_utils import get_last_commit_author_if_pr_exists, get_repo_owner_and_name +from codeflash.code_utils.time_utils import humanize_runtime from codeflash.lsp.helpers import is_LSP_enabled from codeflash.models.ExperimentMetadata import ExperimentMetadata from codeflash.models.models import AIServiceRefinerRequest, CodeStringsMarkdown, OptimizedCandidate @@ -19,8 +20,6 @@ from codeflash.version import __version__ as codeflash_version if TYPE_CHECKING: - from pathlib import Path - from codeflash.discovery.functions_to_optimize import FunctionToOptimize from codeflash.models.ExperimentMetadata import ExperimentMetadata from codeflash.models.models import AIServiceRefinerRequest @@ -513,91 +512,45 @@ def generate_regression_tests( # noqa: D417 ph("cli-testgen-error-response", {"response_status_code": response.status_code, "error": response.text}) return None - def get_optimization_impact(self, original_code, new_code, explanation, existing_tests_source, generated_original_test_source, function_trace_id, coverage_message, replay_tests, concolic_tests) -> list[OptimizedCandidate]: + def get_optimization_impact(self, original_code, new_code, explanation, existing_tests_source, generated_original_test_source, function_trace_id, coverage_message, replay_tests, concolic_tests, root_dir, original_line_profiler_results, optimized_line_profiler_results) -> str: """Optimize the given python code for performance by making a request to the Django endpoint. Args: - request: A list of optimization candidate details for refinement + PrComment args Returns: ------- - - List[OptimizationCandidate]: A list of Optimization Candidates. + - 'high','medium' or 'low' optimization impact """ - # """{ - # "original_code": original_code_combined, - # "new_code": new_code_combined, - # "explanation": new_explanation, - # "existing_tests_source": existing_tests, - # "generated_original_test_source": generated_tests_str, - # "function_trace_id": self.function_trace_id[:-4] + exp_type - # if self.experiment_id - # else self.function_trace_id, - # "coverage_message": coverage_message, - # "replay_tests": replay_tests, - # "concolic_tests": concolic_tests, - # }""" - # logger.info("Creating a new PR with the optimized code...") - # console.rule() - # owner, repo = get_repo_owner_and_name(git_repo, git_remote) - # logger.info(f"Pushing to {git_remote} - Owner: {owner}, Repo: {repo}") - # console.rule() - # if not check_and_push_branch(git_repo, git_remote, wait_for_push=True): - # logger.warning("⏭️ Branch is not pushed, skipping PR creation...") - # return - # relative_path = explanation.file_path.relative_to(root_dir).as_posix() - # base_branch = get_current_branch() - # build_file_changes = { - # Path(p).relative_to(root_dir).as_posix(): FileDiffContent( - # oldContent=original_code[p], newContent=new_code[p] - # ) - # for p in original_code - # } - # - # response = cfapi.create_pr( - # owner=owner, - # repo=repo, - # base_branch=base_branch, - # file_changes=build_file_changes, - # pr_comment=PrComment( - # optimization_explanation=explanation.explanation_message(), - # best_runtime=explanation.best_runtime_ns, - # original_runtime=explanation.original_runtime_ns, - # function_name=explanation.function_name, - # relative_file_path=relative_path, - # speedup_x=explanation.speedup_x, - # speedup_pct=explanation.speedup_pct, - # winning_behavior_test_results=explanation.winning_behavior_test_results, - # winning_benchmarking_test_results=explanation.winning_benchmarking_test_results, - # benchmark_details=explanation.benchmark_details, - # ), - # existing_tests=existing_tests_source, - # generated_tests=generated_original_test_source, - # trace_id=function_trace_id, - # coverage_message=coverage_message, - # replay_tests=replay_tests, - # concolic_tests=concolic_tests, - # ) - # if response.ok: - # pr_id = response.text - # pr_url = github_pr_url(owner, repo, pr_id) - # logger.info(f"Successfully created a new PR #{pr_id} with the optimized code: {pr_url}") - # else: - # logger.error( - # f"Optimization was successful, but I failed to create a PR with the optimized code." - # f" Response from server was: {response.text}" - # ) - # console.rule() logger.info("!lsp|Computing Optimization Impact…") + original_code_str = '' + new_code_str = '' + for p in original_code: + original_code_str += f"```python:{Path(p).relative_to(root_dir).as_posix()}" + original_code_str += '\n' + original_code_str += original_code[p] + for p in new_code: + new_code_str += f"```python:{Path(p).relative_to(root_dir).as_posix()}" + new_code_str += '\n' + new_code_str += new_code[p] + payload = { - "original_code": original_code, - "new_code": new_code, - "existing_tests_source": existing_tests_source, - "generated_original_test_source": generated_original_test_source, - "function_trace_id": function_trace_id, + "original_code": original_code_str, + "optimized_code": new_code_str, + "existing_tests": existing_tests_source, + "generated_tests": generated_original_test_source, + "trace_id": function_trace_id, "coverage_message": coverage_message, "replay_tests": replay_tests, "concolic_tests": concolic_tests, + "speedup": f"{1+float(explanation.speedup):.2f}x", + "loop_count": explanation.winning_benchmarking_test_results.number_of_loops(), + "benchmark_details": explanation.benchmark_details if explanation.benchmark_details else None, + "optimized_runtime": humanize_runtime(explanation.best_runtime_ns), + "original_runtime": humanize_runtime(explanation.original_runtime_ns), + "original_line_profiler_results":original_line_profiler_results, + "optimized_line_profiler_results":optimized_line_profiler_results } console.rule() try: @@ -617,7 +570,7 @@ def get_optimization_impact(self, original_code, new_code, explanation, existing logger.error(f"Error generating impact candidates: {response.status_code} - {error}") ph("cli-optimize-error-response", {"response_status_code": response.status_code, "error": error}) console.rule() - return [] + return '' class LocalAiServiceClient(AiServiceClient): diff --git a/codeflash/optimization/function_optimizer.py b/codeflash/optimization/function_optimizer.py index 6ec279863..0c2fffe95 100644 --- a/codeflash/optimization/function_optimizer.py +++ b/codeflash/optimization/function_optimizer.py @@ -1401,15 +1401,18 @@ def process_review( } raise_pr = not self.args.no_pr - # modify argument of staging vs pr based on the impact - opt_impact_response = self.aiservice_client.get_optimization_impact(**data) - if opt_impact_response in ['medium', 'low']: - raise_pr = False - self.args.staging_review = True if raise_pr or self.args.staging_review: data["root_dir"] = git_root_dir() + if raise_pr: + # modify argument of staging vs pr based on the impact + opt_impact_response = self.aiservice_client.get_optimization_impact(**data, original_line_profiler_results=original_code_baseline.line_profile_results["str_out"], + optimized_line_profiler_results=best_optimization.line_profiler_test_results["str_out"],) + if opt_impact_response in ['low', 'medium']: + raise_pr = False + self.args.staging_review = True + if raise_pr and not self.args.staging_review: data["git_remote"] = self.args.git_remote check_create_pr(**data) From 2e8414fc76cf2867b135f12a95d6a075e482b306 Mon Sep 17 00:00:00 2001 From: aseembits93 Date: Mon, 22 Sep 2025 21:50:54 -0700 Subject: [PATCH 03/10] todo cleanup --- codeflash/api/aiservice.py | 64 ++++++++++++++------ codeflash/code_utils/env_utils.py | 1 + codeflash/optimization/function_optimizer.py | 9 ++- 3 files changed, 52 insertions(+), 22 deletions(-) diff --git a/codeflash/api/aiservice.py b/codeflash/api/aiservice.py index c570ecfc7..ef602a5c9 100644 --- a/codeflash/api/aiservice.py +++ b/codeflash/api/aiservice.py @@ -4,8 +4,9 @@ import os import platform import time -from typing import TYPE_CHECKING, Any, Literal from pathlib import Path +from typing import TYPE_CHECKING, Any, cast + import requests from pydantic.json import pydantic_encoder @@ -23,6 +24,7 @@ from codeflash.discovery.functions_to_optimize import FunctionToOptimize from codeflash.models.ExperimentMetadata import ExperimentMetadata from codeflash.models.models import AIServiceRefinerRequest + from codeflash.result.explanation import Explanation class AiServiceClient: @@ -512,11 +514,36 @@ def generate_regression_tests( # noqa: D417 ph("cli-testgen-error-response", {"response_status_code": response.status_code, "error": response.text}) return None - def get_optimization_impact(self, original_code, new_code, explanation, existing_tests_source, generated_original_test_source, function_trace_id, coverage_message, replay_tests, concolic_tests, root_dir, original_line_profiler_results, optimized_line_profiler_results) -> str: + def get_optimization_impact( + self, + original_code: dict[Path, str], + new_code: dict[Path, str], + explanation: Explanation, + existing_tests_source: str, + generated_original_test_source: str, + function_trace_id: str, + coverage_message: str, + replay_tests: str, + concolic_tests: str, + root_dir: Path, + original_line_profiler_results: str, + optimized_line_profiler_results: str, + ) -> str: """Optimize the given python code for performance by making a request to the Django endpoint. Args: - PrComment args + original_code: dict, + new_code: dict, + explanation: Explanation, + existing_tests_source: str, + generated_original_test_source: str, + function_trace_id: str, + coverage_message: str, + replay_tests: str, + concolic_tests: str, + root_dir: Path, + original_line_profiler_results: str, + optimized_line_profiler_results: str, Returns: ------- @@ -524,16 +551,16 @@ def get_optimization_impact(self, original_code, new_code, explanation, existing """ logger.info("!lsp|Computing Optimization Impact…") - original_code_str = '' - new_code_str = '' - for p in original_code: + original_code_str = "" + new_code_str = "" + for p, code in original_code.items(): original_code_str += f"```python:{Path(p).relative_to(root_dir).as_posix()}" - original_code_str += '\n' - original_code_str += original_code[p] - for p in new_code: + original_code_str += "\n" + original_code_str += code + for p, code in new_code.items(): new_code_str += f"```python:{Path(p).relative_to(root_dir).as_posix()}" - new_code_str += '\n' - new_code_str += new_code[p] + new_code_str += "\n" + new_code_str += code payload = { "original_code": original_code_str, @@ -544,13 +571,13 @@ def get_optimization_impact(self, original_code, new_code, explanation, existing "coverage_message": coverage_message, "replay_tests": replay_tests, "concolic_tests": concolic_tests, - "speedup": f"{1+float(explanation.speedup):.2f}x", + "speedup": f"{1 + float(explanation.speedup):.2f}x", "loop_count": explanation.winning_benchmarking_test_results.number_of_loops(), "benchmark_details": explanation.benchmark_details if explanation.benchmark_details else None, "optimized_runtime": humanize_runtime(explanation.best_runtime_ns), "original_runtime": humanize_runtime(explanation.original_runtime_ns), - "original_line_profiler_results":original_line_profiler_results, - "optimized_line_profiler_results":optimized_line_profiler_results + "original_line_profiler_results": original_line_profiler_results, + "optimized_line_profiler_results": optimized_line_profiler_results, } console.rule() try: @@ -558,19 +585,18 @@ def get_optimization_impact(self, original_code, new_code, explanation, existing except requests.exceptions.RequestException as e: logger.exception(f"Error generating optimization refinements: {e}") ph("cli-optimize-error-caught", {"error": str(e)}) - return '' + return "" if response.status_code == 200: - impact = response.json()["impact"] - return impact + return cast("str", response.json()["impact"]) try: - error = response.json()["error"] + error = cast("str", response.json()["error"]) except Exception: error = response.text logger.error(f"Error generating impact candidates: {response.status_code} - {error}") ph("cli-optimize-error-response", {"response_status_code": response.status_code, "error": error}) console.rule() - return '' + return "" class LocalAiServiceClient(AiServiceClient): diff --git a/codeflash/code_utils/env_utils.py b/codeflash/code_utils/env_utils.py index a7bc8a1fa..6d97764e7 100644 --- a/codeflash/code_utils/env_utils.py +++ b/codeflash/code_utils/env_utils.py @@ -42,6 +42,7 @@ def get_codeflash_api_key() -> str: if env_api_key and not shell_api_key: try: from codeflash.either import is_successful + result = save_api_key_to_rc(env_api_key) if is_successful(result): logger.debug(f"Automatically saved API key from environment to shell config: {result.unwrap()}") diff --git a/codeflash/optimization/function_optimizer.py b/codeflash/optimization/function_optimizer.py index 0c2fffe95..3462f45d1 100644 --- a/codeflash/optimization/function_optimizer.py +++ b/codeflash/optimization/function_optimizer.py @@ -1407,9 +1407,12 @@ def process_review( if raise_pr: # modify argument of staging vs pr based on the impact - opt_impact_response = self.aiservice_client.get_optimization_impact(**data, original_line_profiler_results=original_code_baseline.line_profile_results["str_out"], - optimized_line_profiler_results=best_optimization.line_profiler_test_results["str_out"],) - if opt_impact_response in ['low', 'medium']: + opt_impact_response = self.aiservice_client.get_optimization_impact( + **data, + original_line_profiler_results=original_code_baseline.line_profile_results["str_out"], + optimized_line_profiler_results=best_optimization.line_profiler_test_results["str_out"], + ) + if opt_impact_response in ["low", "medium"]: raise_pr = False self.args.staging_review = True From d766dabd0162e6cfc0033cbed82528c5a3a5f90a Mon Sep 17 00:00:00 2001 From: aseembits93 Date: Fri, 26 Sep 2025 14:52:56 -0700 Subject: [PATCH 04/10] logging done, start experimenting, using diff instead of raw strings --- codeflash/api/aiservice.py | 25 ++++++-------------- codeflash/optimization/function_optimizer.py | 17 ++++++------- 2 files changed, 14 insertions(+), 28 deletions(-) diff --git a/codeflash/api/aiservice.py b/codeflash/api/aiservice.py index 0e18769b5..291134c35 100644 --- a/codeflash/api/aiservice.py +++ b/codeflash/api/aiservice.py @@ -11,10 +11,13 @@ from pydantic.json import pydantic_encoder from codeflash.cli_cmds.console import console, logger +from codeflash.code_utils.code_replacer import is_zero_diff +from codeflash.code_utils.code_utils import unified_diff_strings from codeflash.code_utils.config_consts import N_CANDIDATES_EFFECTIVE, N_CANDIDATES_LP_EFFECTIVE from codeflash.code_utils.env_utils import get_codeflash_api_key from codeflash.code_utils.git_utils import get_last_commit_author_if_pr_exists, get_repo_owner_and_name from codeflash.code_utils.time_utils import humanize_runtime +from codeflash.github.PrComment import FileDiffContent from codeflash.lsp.helpers import is_LSP_enabled from codeflash.models.ExperimentMetadata import ExperimentMetadata from codeflash.models.models import AIServiceRefinerRequest, CodeStringsMarkdown, OptimizedCandidate @@ -529,8 +532,6 @@ def get_optimization_impact( replay_tests: str, concolic_tests: str, root_dir: Path, - original_line_profiler_results: str, - optimized_line_profiler_results: str, ) -> str: """Optimize the given python code for performance by making a request to the Django endpoint. @@ -553,34 +554,22 @@ def get_optimization_impact( - 'high','medium' or 'low' optimization impact """ + diff_str = '\n'.join([unified_diff_strings(code1=original_code[p], code2=new_code[p], fromfile=Path(p).relative_to(root_dir).as_posix(), tofile=Path(p).relative_to(root_dir).as_posix()) for p in original_code if not is_zero_diff(original_code[p], new_code[p])]) + code_diff = f"```diff\n{diff_str}\n```" logger.info("!lsp|Computing Optimization Impact…") - original_code_str = "" - new_code_str = "" - for p, code in original_code.items(): - original_code_str += f"```python:{Path(p).relative_to(root_dir).as_posix()}" - original_code_str += "\n" - original_code_str += code - for p, code in new_code.items(): - new_code_str += f"```python:{Path(p).relative_to(root_dir).as_posix()}" - new_code_str += "\n" - new_code_str += code - payload = { - "original_code": original_code_str, - "optimized_code": new_code_str, + "code_diff": code_diff, "existing_tests": existing_tests_source, "generated_tests": generated_original_test_source, "trace_id": function_trace_id, "coverage_message": coverage_message, "replay_tests": replay_tests, "concolic_tests": concolic_tests, - "speedup": f"{1 + float(explanation.speedup):.2f}x", + "speedup": f"{100 + 100*float(explanation.speedup):.2f}%", "loop_count": explanation.winning_benchmarking_test_results.number_of_loops(), "benchmark_details": explanation.benchmark_details if explanation.benchmark_details else None, "optimized_runtime": humanize_runtime(explanation.best_runtime_ns), "original_runtime": humanize_runtime(explanation.original_runtime_ns), - "original_line_profiler_results": original_line_profiler_results, - "optimized_line_profiler_results": optimized_line_profiler_results, } console.rule() try: diff --git a/codeflash/optimization/function_optimizer.py b/codeflash/optimization/function_optimizer.py index d95b3020e..9e2c509d3 100644 --- a/codeflash/optimization/function_optimizer.py +++ b/codeflash/optimization/function_optimizer.py @@ -1406,25 +1406,22 @@ def process_review( } raise_pr = not self.args.no_pr + staging_review = self.args.staging_review - if raise_pr or self.args.staging_review: + if raise_pr or staging_review: data["root_dir"] = git_root_dir() - - if raise_pr: # modify argument of staging vs pr based on the impact opt_impact_response = self.aiservice_client.get_optimization_impact( **data, - original_line_profiler_results=original_code_baseline.line_profile_results["str_out"], - optimized_line_profiler_results=best_optimization.line_profiler_test_results["str_out"], ) - if opt_impact_response in ["low", "medium"]: + if opt_impact_response=='low': raise_pr = False - self.args.staging_review = True + staging_review = True - if raise_pr and not self.args.staging_review: + if raise_pr and not staging_review: data["git_remote"] = self.args.git_remote check_create_pr(**data) - elif self.args.staging_review: + elif staging_review: response = create_staging(**data) if response.status_code == 200: staging_url = f"https://app.codeflash.ai/review-optimizations/{self.function_trace_id[:-4] + exp_type if self.experiment_id else self.function_trace_id}" @@ -1463,7 +1460,7 @@ def process_review( self.revert_code_and_helpers(original_helper_code) return - if self.args.staging_review: + if staging_review: # always revert code and helpers when staging review self.revert_code_and_helpers(original_helper_code) return From 6a4d8426965960d063c34dde25ca5c627a2b07a8 Mon Sep 17 00:00:00 2001 From: aseembits93 Date: Fri, 26 Sep 2025 14:54:46 -0700 Subject: [PATCH 05/10] precommit fix --- codeflash/api/aiservice.py | 16 +++++++++++++--- codeflash/optimization/function_optimizer.py | 6 ++---- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/codeflash/api/aiservice.py b/codeflash/api/aiservice.py index 291134c35..a7dd9edad 100644 --- a/codeflash/api/aiservice.py +++ b/codeflash/api/aiservice.py @@ -17,7 +17,6 @@ from codeflash.code_utils.env_utils import get_codeflash_api_key from codeflash.code_utils.git_utils import get_last_commit_author_if_pr_exists, get_repo_owner_and_name from codeflash.code_utils.time_utils import humanize_runtime -from codeflash.github.PrComment import FileDiffContent from codeflash.lsp.helpers import is_LSP_enabled from codeflash.models.ExperimentMetadata import ExperimentMetadata from codeflash.models.models import AIServiceRefinerRequest, CodeStringsMarkdown, OptimizedCandidate @@ -554,7 +553,18 @@ def get_optimization_impact( - 'high','medium' or 'low' optimization impact """ - diff_str = '\n'.join([unified_diff_strings(code1=original_code[p], code2=new_code[p], fromfile=Path(p).relative_to(root_dir).as_posix(), tofile=Path(p).relative_to(root_dir).as_posix()) for p in original_code if not is_zero_diff(original_code[p], new_code[p])]) + diff_str = "\n".join( + [ + unified_diff_strings( + code1=original_code[p], + code2=new_code[p], + fromfile=Path(p).relative_to(root_dir).as_posix(), + tofile=Path(p).relative_to(root_dir).as_posix(), + ) + for p in original_code + if not is_zero_diff(original_code[p], new_code[p]) + ] + ) code_diff = f"```diff\n{diff_str}\n```" logger.info("!lsp|Computing Optimization Impact…") payload = { @@ -565,7 +575,7 @@ def get_optimization_impact( "coverage_message": coverage_message, "replay_tests": replay_tests, "concolic_tests": concolic_tests, - "speedup": f"{100 + 100*float(explanation.speedup):.2f}%", + "speedup": f"{100 + 100 * float(explanation.speedup):.2f}%", "loop_count": explanation.winning_benchmarking_test_results.number_of_loops(), "benchmark_details": explanation.benchmark_details if explanation.benchmark_details else None, "optimized_runtime": humanize_runtime(explanation.best_runtime_ns), diff --git a/codeflash/optimization/function_optimizer.py b/codeflash/optimization/function_optimizer.py index 9e2c509d3..35635063b 100644 --- a/codeflash/optimization/function_optimizer.py +++ b/codeflash/optimization/function_optimizer.py @@ -1411,10 +1411,8 @@ def process_review( if raise_pr or staging_review: data["root_dir"] = git_root_dir() # modify argument of staging vs pr based on the impact - opt_impact_response = self.aiservice_client.get_optimization_impact( - **data, - ) - if opt_impact_response=='low': + opt_impact_response = self.aiservice_client.get_optimization_impact(**data) + if opt_impact_response == "low": raise_pr = False staging_review = True From 530158c649b303194c4e8139981c05dd2599333c Mon Sep 17 00:00:00 2001 From: aseembits93 Date: Fri, 26 Sep 2025 15:03:10 -0700 Subject: [PATCH 06/10] ready to review --- codeflash/api/aiservice.py | 25 ++++++++------------ codeflash/optimization/function_optimizer.py | 14 ++++++----- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/codeflash/api/aiservice.py b/codeflash/api/aiservice.py index a7dd9edad..495c477f7 100644 --- a/codeflash/api/aiservice.py +++ b/codeflash/api/aiservice.py @@ -529,24 +529,20 @@ def get_optimization_impact( function_trace_id: str, coverage_message: str, replay_tests: str, - concolic_tests: str, root_dir: Path, ) -> str: - """Optimize the given python code for performance by making a request to the Django endpoint. + """Compute the optimization impact of current Pull Request. Args: - original_code: dict, - new_code: dict, - explanation: Explanation, - existing_tests_source: str, - generated_original_test_source: str, - function_trace_id: str, - coverage_message: str, - replay_tests: str, - concolic_tests: str, - root_dir: Path, - original_line_profiler_results: str, - optimized_line_profiler_results: str, + original_code: dict -> data structure mapping file paths to function definition for original code + new_code: dict -> data structure mapping file paths to function definition for optimized code + explanation: Explanation -> data structure containing runtime information + existing_tests_source: str -> existing tests table + generated_original_test_source: str -> annotated generated tests + function_trace_id: str -> traceid of function + coverage_message: str -> coverage information + replay_tests: str -> replay test table + root_dir: Path -> path of git directory Returns: ------- @@ -574,7 +570,6 @@ def get_optimization_impact( "trace_id": function_trace_id, "coverage_message": coverage_message, "replay_tests": replay_tests, - "concolic_tests": concolic_tests, "speedup": f"{100 + 100 * float(explanation.speedup):.2f}%", "loop_count": explanation.winning_benchmarking_test_results.number_of_loops(), "benchmark_details": explanation.benchmark_details if explanation.benchmark_details else None, diff --git a/codeflash/optimization/function_optimizer.py b/codeflash/optimization/function_optimizer.py index 35635063b..6416ad97f 100644 --- a/codeflash/optimization/function_optimizer.py +++ b/codeflash/optimization/function_optimizer.py @@ -1410,12 +1410,14 @@ def process_review( if raise_pr or staging_review: data["root_dir"] = git_root_dir() - # modify argument of staging vs pr based on the impact - opt_impact_response = self.aiservice_client.get_optimization_impact(**data) - if opt_impact_response == "low": - raise_pr = False - staging_review = True - + try: + # modify argument of staging vs pr based on the impact + opt_impact_response = self.aiservice_client.get_optimization_impact(**data) + if opt_impact_response == "low": + raise_pr = False + staging_review = True + except Exception as e: + logger.debug(f"optimization impact response failed, investigate {e}") if raise_pr and not staging_review: data["git_remote"] = self.args.git_remote check_create_pr(**data) From 4d9723d5d4808faf88ba55918cede13c7a8d0901 Mon Sep 17 00:00:00 2001 From: aseembits93 Date: Fri, 26 Sep 2025 17:34:27 -0700 Subject: [PATCH 07/10] bugfix --- codeflash/api/aiservice.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/codeflash/api/aiservice.py b/codeflash/api/aiservice.py index 495c477f7..4cf1119ed 100644 --- a/codeflash/api/aiservice.py +++ b/codeflash/api/aiservice.py @@ -530,6 +530,7 @@ def get_optimization_impact( coverage_message: str, replay_tests: str, root_dir: Path, + concolic_tests: str, ) -> str: """Compute the optimization impact of current Pull Request. @@ -543,6 +544,7 @@ def get_optimization_impact( coverage_message: str -> coverage information replay_tests: str -> replay test table root_dir: Path -> path of git directory + concolic_tests: str -> concolic_tests (not used) Returns: ------- @@ -576,6 +578,7 @@ def get_optimization_impact( "optimized_runtime": humanize_runtime(explanation.best_runtime_ns), "original_runtime": humanize_runtime(explanation.original_runtime_ns), } + logger.debug(f"unused {type(concolic_tests)}") console.rule() try: response = self.make_ai_service_request("/optimization_impact", payload=payload, timeout=600) From ab1c385637cb90b41dadc3f6c22acc699d2b77c7 Mon Sep 17 00:00:00 2001 From: aseembits93 Date: Mon, 29 Sep 2025 17:55:46 -0700 Subject: [PATCH 08/10] add explanation --- codeflash/api/aiservice.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/codeflash/api/aiservice.py b/codeflash/api/aiservice.py index 8ce39afc4..5dacc510f 100644 --- a/codeflash/api/aiservice.py +++ b/codeflash/api/aiservice.py @@ -543,7 +543,7 @@ def get_optimization_impact( coverage_message: str, replay_tests: str, root_dir: Path, - concolic_tests: str, + concolic_tests: str, # noqa: ARG002 ) -> str: """Compute the optimization impact of current Pull Request. @@ -580,6 +580,7 @@ def get_optimization_impact( logger.info("!lsp|Computing Optimization Impact…") payload = { "code_diff": code_diff, + "explanation": explanation.raw_explanation_message, "existing_tests": existing_tests_source, "generated_tests": generated_original_test_source, "trace_id": function_trace_id, @@ -591,7 +592,6 @@ def get_optimization_impact( "optimized_runtime": humanize_runtime(explanation.best_runtime_ns), "original_runtime": humanize_runtime(explanation.original_runtime_ns), } - logger.debug(f"unused {type(concolic_tests)}") console.rule() try: response = self.make_ai_service_request("/optimization_impact", payload=payload, timeout=600) From 90a135578a8b7740863119ce67c4510845aa1020 Mon Sep 17 00:00:00 2001 From: aseembits93 Date: Wed, 1 Oct 2025 13:16:09 -0700 Subject: [PATCH 09/10] speedup relative --- codeflash/api/aiservice.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codeflash/api/aiservice.py b/codeflash/api/aiservice.py index 5dacc510f..4c8eb7e92 100644 --- a/codeflash/api/aiservice.py +++ b/codeflash/api/aiservice.py @@ -586,7 +586,7 @@ def get_optimization_impact( "trace_id": function_trace_id, "coverage_message": coverage_message, "replay_tests": replay_tests, - "speedup": f"{100 + 100 * float(explanation.speedup):.2f}%", + "speedup": f"{(100 * float(explanation.speedup)):.2f}%", "loop_count": explanation.winning_benchmarking_test_results.number_of_loops(), "benchmark_details": explanation.benchmark_details if explanation.benchmark_details else None, "optimized_runtime": humanize_runtime(explanation.best_runtime_ns), From c2ffeae37e3b18f66a956b194b7f921d23a43156 Mon Sep 17 00:00:00 2001 From: Aseem Saxena Date: Mon, 6 Oct 2025 12:05:39 -0700 Subject: [PATCH 10/10] Apply suggestion from @aseembits93 --- codeflash/api/aiservice.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codeflash/api/aiservice.py b/codeflash/api/aiservice.py index 4c8eb7e92..e15333d75 100644 --- a/codeflash/api/aiservice.py +++ b/codeflash/api/aiservice.py @@ -561,7 +561,7 @@ def get_optimization_impact( Returns: ------- - - 'high','medium' or 'low' optimization impact + - 'high' or 'low' optimization impact """ diff_str = "\n".join(