From 347fa9c238ab61b3faeb69ae80f6e8648fba8105 Mon Sep 17 00:00:00 2001 From: HeshamHM28 Date: Sun, 18 May 2025 15:25:10 +0300 Subject: [PATCH 1/5] save event creator --- codeflash/api/aiservice.py | 5 +++++ codeflash/code_utils/git_utils.py | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/codeflash/api/aiservice.py b/codeflash/api/aiservice.py index f6482898b..67d6a261e 100644 --- a/codeflash/api/aiservice.py +++ b/codeflash/api/aiservice.py @@ -12,6 +12,7 @@ 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.models.models import OptimizedCandidate from codeflash.telemetry.posthog_cf import ph from codeflash.version import __version__ as codeflash_version @@ -98,6 +99,7 @@ def optimize_python_code( """ start_time = time.perf_counter() + git_repo_owner, git_repo_name = get_repo_owner_and_name() payload = { "source_code": source_code, "dependency_code": dependency_code, @@ -106,6 +108,9 @@ def optimize_python_code( "python_version": platform.python_version(), "experiment_metadata": experiment_metadata, "codeflash_version": codeflash_version, + "current_username" : get_last_commit_author_if_pr_exists(None), + "repo_owner": git_repo_owner, + "repo_name": git_repo_name, } logger.info("Generating optimized candidates…") diff --git a/codeflash/code_utils/git_utils.py b/codeflash/code_utils/git_utils.py index 2982b05fa..8f97b805c 100644 --- a/codeflash/code_utils/git_utils.py +++ b/codeflash/code_utils/git_utils.py @@ -1,5 +1,6 @@ from __future__ import annotations +import os import shutil import subprocess import sys @@ -174,3 +175,16 @@ def remove_git_worktrees(worktree_root: Path | None, worktrees: list[Path]) -> N logger.warning(f"Error removing worktrees: {e}") if worktree_root: shutil.rmtree(worktree_root) + + +def get_last_commit_author_if_pr_exists(repo: Repo | None = None) -> str | None: + """ + Return the author's name of the last commit in the current branch if PR_NUMBER is set. + Otherwise, return None. + """ + if "PR_NUMBER" not in os.environ: + return None + + repository: Repo = repo if repo else git.Repo(search_parent_directories=True) + last_commit = repository.head.commit + return last_commit.author.name From 1f893ea124db16553e219f30e68df70641a95c76 Mon Sep 17 00:00:00 2001 From: HeshamHM28 Date: Mon, 19 May 2025 09:24:35 +0300 Subject: [PATCH 2/5] Add error handling to get_last_commit_author_if_pr_exists --- codeflash/code_utils/git_utils.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/codeflash/code_utils/git_utils.py b/codeflash/code_utils/git_utils.py index 8f97b805c..3d3ea26e2 100644 --- a/codeflash/code_utils/git_utils.py +++ b/codeflash/code_utils/git_utils.py @@ -182,9 +182,13 @@ def get_last_commit_author_if_pr_exists(repo: Repo | None = None) -> str | None: Return the author's name of the last commit in the current branch if PR_NUMBER is set. Otherwise, return None. """ - if "PR_NUMBER" not in os.environ: + try: + if "PR_NUMBER" not in os.environ: + return None + + repository: Repo = repo if repo else git.Repo(search_parent_directories=True) + last_commit = repository.head.commit + return last_commit.author.name + except Exception as e: + logger.exception("Failed to get last commit author.") return None - - repository: Repo = repo if repo else git.Repo(search_parent_directories=True) - last_commit = repository.head.commit - return last_commit.author.name From 2c49ec7bbcbd5cbc4822109546538f83c9830fe9 Mon Sep 17 00:00:00 2001 From: HeshamHM28 Date: Tue, 10 Jun 2025 01:50:03 +0300 Subject: [PATCH 3/5] fix: ensure is_optimization_found is set in no-pr flow --- codeflash/api/cfapi.py | 15 +++++++++++++++ codeflash/optimization/function_optimizer.py | 8 +++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/codeflash/api/cfapi.py b/codeflash/api/cfapi.py index 87c54b148..0aa4a3170 100644 --- a/codeflash/api/cfapi.py +++ b/codeflash/api/cfapi.py @@ -224,3 +224,18 @@ def add_code_context_hash(code_context_hash: str) -> None: "POST", {"owner": owner, "repo": repo, "pr_number": pr_number, "code_hash": code_context_hash}, ) + +def mark_optimization_success(trace_id: str, is_optimization_found: bool) -> Response: + """Mark an optimization event as success or not. + + :param trace_id: The unique identifier for the optimization event. + :param is_optimization_found: Boolean indicating whether the optimization was found. + :return: The response object from the API. + """ + payload = { + "trace_id": trace_id, + "is_optimization_found": is_optimization_found, + } + return make_cfapi_request( + endpoint="/mark-as-success", method="POST", payload=payload + ) diff --git a/codeflash/optimization/function_optimizer.py b/codeflash/optimization/function_optimizer.py index 4edbf8974..ce9bdc6a3 100644 --- a/codeflash/optimization/function_optimizer.py +++ b/codeflash/optimization/function_optimizer.py @@ -19,7 +19,7 @@ from rich.tree import Tree from codeflash.api.aiservice import AiServiceClient, LocalAiServiceClient -from codeflash.api.cfapi import add_code_context_hash +from codeflash.api.cfapi import add_code_context_hash, mark_optimization_success from codeflash.benchmarking.utils import process_benchmark_data from codeflash.cli_cmds.console import code_print, console, logger, progress_bar from codeflash.code_utils import env_utils @@ -387,6 +387,12 @@ def optimize_function(self) -> Result[BestOptimization, str]: # noqa: PLR0911 original_helper_code, self.function_to_optimize.file_path, ) + else: + # Mark optimization success since no PR will be created + mark_optimization_success( + trace_id=self.function_trace_id, + is_optimization_found=best_optimization is not None + ) self.log_successful_optimization(explanation, generated_tests, exp_type) # Add function to code context hash if in gh actions From 297d9bd0fd1eaad452a8c6dc3cc4cf2150b4759f Mon Sep 17 00:00:00 2001 From: HeshamHM28 Date: Tue, 10 Jun 2025 02:06:03 +0300 Subject: [PATCH 4/5] apply pre-commit lint fixes --- codeflash/api/aiservice.py | 2 +- codeflash/api/cfapi.py | 12 ++++-------- codeflash/code_utils/git_utils.py | 14 +++++++------- codeflash/optimization/function_optimizer.py | 3 +-- 4 files changed, 13 insertions(+), 18 deletions(-) diff --git a/codeflash/api/aiservice.py b/codeflash/api/aiservice.py index 1fda78e92..dc60e3452 100644 --- a/codeflash/api/aiservice.py +++ b/codeflash/api/aiservice.py @@ -107,7 +107,7 @@ def optimize_python_code( # noqa: D417 "python_version": platform.python_version(), "experiment_metadata": experiment_metadata, "codeflash_version": codeflash_version, - "current_username" : get_last_commit_author_if_pr_exists(None), + "current_username": get_last_commit_author_if_pr_exists(None), "repo_owner": git_repo_owner, "repo_name": git_repo_name, } diff --git a/codeflash/api/cfapi.py b/codeflash/api/cfapi.py index 0aa4a3170..b908a57ea 100644 --- a/codeflash/api/cfapi.py +++ b/codeflash/api/cfapi.py @@ -225,17 +225,13 @@ def add_code_context_hash(code_context_hash: str) -> None: {"owner": owner, "repo": repo, "pr_number": pr_number, "code_hash": code_context_hash}, ) -def mark_optimization_success(trace_id: str, is_optimization_found: bool) -> Response: + +def mark_optimization_success(trace_id: str, *, is_optimization_found: bool) -> Response: """Mark an optimization event as success or not. :param trace_id: The unique identifier for the optimization event. :param is_optimization_found: Boolean indicating whether the optimization was found. :return: The response object from the API. """ - payload = { - "trace_id": trace_id, - "is_optimization_found": is_optimization_found, - } - return make_cfapi_request( - endpoint="/mark-as-success", method="POST", payload=payload - ) + payload = {"trace_id": trace_id, "is_optimization_found": is_optimization_found} + return make_cfapi_request(endpoint="/mark-as-success", method="POST", payload=payload) diff --git a/codeflash/code_utils/git_utils.py b/codeflash/code_utils/git_utils.py index 757648cb5..78873fd28 100644 --- a/codeflash/code_utils/git_utils.py +++ b/codeflash/code_utils/git_utils.py @@ -180,17 +180,17 @@ def remove_git_worktrees(worktree_root: Path | None, worktrees: list[Path]) -> N def get_last_commit_author_if_pr_exists(repo: Repo | None = None) -> str | None: - """ - Return the author's name of the last commit in the current branch if PR_NUMBER is set. + """Return the author's name of the last commit in the current branch if PR_NUMBER is set. + Otherwise, return None. """ + if "PR_NUMBER" not in os.environ: + return None try: - if "PR_NUMBER" not in os.environ: - return None - repository: Repo = repo if repo else git.Repo(search_parent_directories=True) last_commit = repository.head.commit - return last_commit.author.name - except Exception as e: + except Exception: logger.exception("Failed to get last commit author.") return None + else: + return last_commit.author.name diff --git a/codeflash/optimization/function_optimizer.py b/codeflash/optimization/function_optimizer.py index ce9bdc6a3..0e165622d 100644 --- a/codeflash/optimization/function_optimizer.py +++ b/codeflash/optimization/function_optimizer.py @@ -390,8 +390,7 @@ def optimize_function(self) -> Result[BestOptimization, str]: # noqa: PLR0911 else: # Mark optimization success since no PR will be created mark_optimization_success( - trace_id=self.function_trace_id, - is_optimization_found=best_optimization is not None + trace_id=self.function_trace_id, is_optimization_found=best_optimization is not None ) self.log_successful_optimization(explanation, generated_tests, exp_type) From 53b091848bfe923163b2c2e4114728fa2dff41da Mon Sep 17 00:00:00 2001 From: HeshamHM28 Date: Tue, 10 Jun 2025 16:43:04 +0300 Subject: [PATCH 5/5] add try/catch --- codeflash/api/aiservice.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/codeflash/api/aiservice.py b/codeflash/api/aiservice.py index dc60e3452..81ab84d42 100644 --- a/codeflash/api/aiservice.py +++ b/codeflash/api/aiservice.py @@ -98,7 +98,12 @@ def optimize_python_code( # noqa: D417 """ start_time = time.perf_counter() - git_repo_owner, git_repo_name = get_repo_owner_and_name() + try: + git_repo_owner, git_repo_name = get_repo_owner_and_name() + except Exception as e: + logger.warning(f"Could not determine repo owner and name: {e}") + git_repo_owner, git_repo_name = None, None + payload = { "source_code": source_code, "dependency_code": dependency_code,