Skip to content

Commit 38b69ca

Browse files
authored
Merge branch 'main' into can-specify-multiple-replay-tests
2 parents 9529eb4 + 6e2d21f commit 38b69ca

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

codeflash/api/aiservice.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from codeflash.cli_cmds.console import console, logger
1313
from codeflash.code_utils.env_utils import get_codeflash_api_key
14+
from codeflash.code_utils.git_utils import get_last_commit_author_if_pr_exists, get_repo_owner_and_name
1415
from codeflash.models.models import OptimizedCandidate
1516
from codeflash.telemetry.posthog_cf import ph
1617
from codeflash.version import __version__ as codeflash_version
@@ -97,6 +98,12 @@ def optimize_python_code( # noqa: D417
9798
9899
"""
99100
start_time = time.perf_counter()
101+
try:
102+
git_repo_owner, git_repo_name = get_repo_owner_and_name()
103+
except Exception as e:
104+
logger.warning(f"Could not determine repo owner and name: {e}")
105+
git_repo_owner, git_repo_name = None, None
106+
100107
payload = {
101108
"source_code": source_code,
102109
"dependency_code": dependency_code,
@@ -105,6 +112,9 @@ def optimize_python_code( # noqa: D417
105112
"python_version": platform.python_version(),
106113
"experiment_metadata": experiment_metadata,
107114
"codeflash_version": codeflash_version,
115+
"current_username": get_last_commit_author_if_pr_exists(None),
116+
"repo_owner": git_repo_owner,
117+
"repo_name": git_repo_name,
108118
}
109119

110120
logger.info("Generating optimized candidates…")

codeflash/api/cfapi.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,14 @@ def add_code_context_hash(code_context_hash: str) -> None:
224224
"POST",
225225
{"owner": owner, "repo": repo, "pr_number": pr_number, "code_hash": code_context_hash},
226226
)
227+
228+
229+
def mark_optimization_success(trace_id: str, *, is_optimization_found: bool) -> Response:
230+
"""Mark an optimization event as success or not.
231+
232+
:param trace_id: The unique identifier for the optimization event.
233+
:param is_optimization_found: Boolean indicating whether the optimization was found.
234+
:return: The response object from the API.
235+
"""
236+
payload = {"trace_id": trace_id, "is_optimization_found": is_optimization_found}
237+
return make_cfapi_request(endpoint="/mark-as-success", method="POST", payload=payload)

codeflash/code_utils/git_utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import os
34
import shutil
45
import subprocess
56
import sys
@@ -176,3 +177,20 @@ def remove_git_worktrees(worktree_root: Path | None, worktrees: list[Path]) -> N
176177
logger.warning(f"Error removing worktrees: {e}")
177178
if worktree_root:
178179
shutil.rmtree(worktree_root)
180+
181+
182+
def get_last_commit_author_if_pr_exists(repo: Repo | None = None) -> str | None:
183+
"""Return the author's name of the last commit in the current branch if PR_NUMBER is set.
184+
185+
Otherwise, return None.
186+
"""
187+
if "PR_NUMBER" not in os.environ:
188+
return None
189+
try:
190+
repository: Repo = repo if repo else git.Repo(search_parent_directories=True)
191+
last_commit = repository.head.commit
192+
except Exception:
193+
logger.exception("Failed to get last commit author.")
194+
return None
195+
else:
196+
return last_commit.author.name

codeflash/optimization/function_optimizer.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from rich.tree import Tree
2020

2121
from codeflash.api.aiservice import AiServiceClient, LocalAiServiceClient
22-
from codeflash.api.cfapi import add_code_context_hash
22+
from codeflash.api.cfapi import add_code_context_hash, mark_optimization_success
2323
from codeflash.benchmarking.utils import process_benchmark_data
2424
from codeflash.cli_cmds.console import code_print, console, logger, progress_bar
2525
from codeflash.code_utils import env_utils
@@ -390,6 +390,11 @@ def optimize_function(self) -> Result[BestOptimization, str]: # noqa: PLR0911
390390
original_helper_code,
391391
self.function_to_optimize.file_path,
392392
)
393+
else:
394+
# Mark optimization success since no PR will be created
395+
mark_optimization_success(
396+
trace_id=self.function_trace_id, is_optimization_found=best_optimization is not None
397+
)
393398
self.log_successful_optimization(explanation, generated_tests, exp_type)
394399

395400
# Add function to code context hash if in gh actions

0 commit comments

Comments
 (0)