Skip to content

Commit 5989b26

Browse files
committed
updates
1 parent dd8dceb commit 5989b26

File tree

3 files changed

+36
-32
lines changed

3 files changed

+36
-32
lines changed

codeflash/api/cfapi.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import json
44
import os
55
import sys
6+
import git
67
from functools import lru_cache
78
from pathlib import Path
89
from typing import TYPE_CHECKING, Any, Dict, Optional, Tuple, List
@@ -13,8 +14,8 @@
1314

1415
from codeflash.cli_cmds.console import console, logger
1516
from codeflash.code_utils.env_utils import ensure_codeflash_api_key, get_codeflash_api_key, get_pr_number
16-
from codeflash.code_utils.git_utils import get_repo_owner_and_name
1717
from codeflash.version import __version__
18+
from codeflash.code_utils.git_utils import get_repo_owner_and_name
1819

1920
if TYPE_CHECKING:
2021
from requests import Response
@@ -203,16 +204,27 @@ def is_function_being_optimized_again(owner: str, repo: str, pr_number: int, cod
203204
response.raise_for_status()
204205
return response.json()
205206

206-
def add_code_context_hash(owner: str, repo: str, pr_number: int, code_context_hash: str) -> Response:
207+
def add_code_context_hash( code_context_hash: str):
207208
"""Add code context to the DB cache"""
208-
response = make_cfapi_request(
209-
"/add-code-hash",
210-
"POST",
211-
{
212-
"owner": owner,
213-
"repo": repo,
214-
"pr_number": pr_number,
215-
"code_context_hash": code_context_hash
216-
}
217-
)
218-
return response
209+
pr_number = get_pr_number()
210+
if pr_number is None:
211+
return
212+
try:
213+
owner, repo = get_repo_owner_and_name()
214+
pr_number = get_pr_number()
215+
except git.exc.InvalidGitRepositoryError:
216+
return
217+
218+
219+
if owner and repo and pr_number is not None:
220+
make_cfapi_request(
221+
"/add-code-hash",
222+
"POST",
223+
{
224+
"owner": owner,
225+
"repo": repo,
226+
"pr_number": pr_number,
227+
"code_context_hash": code_context_hash
228+
}
229+
)
230+

codeflash/discovery/functions_to_optimize.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ def get_code_context_hash(self) -> str:
159159

160160
# Extract the function's code content
161161
lines = file_content.splitlines()
162+
print("starting and ending line ", self.starting_line, self.ending_line)
162163
if self.starting_line is not None and self.ending_line is not None:
163164
# Use line numbers if available (1-indexed to 0-indexed)
164165
function_content = "\n".join(lines[self.starting_line - 1 : self.ending_line])
@@ -460,7 +461,7 @@ def inspect_top_level_functions_or_methods(
460461
)
461462

462463

463-
def check_optimization_status(functions_by_file: dict[Path, list[FunctionToOptimize]]) -> list[tuple[str, str]]:
464+
def check_optimization_status(functions_by_file: dict[Path, list[FunctionToOptimize]], project_root_path: Path) -> set[tuple[str, str]]:
464465
"""Check which functions have already been optimized and filter them out.
465466
466467
This function calls the optimization API to:
@@ -497,22 +498,21 @@ def check_optimization_status(functions_by_file: dict[Path, list[FunctionToOptim
497498
for func in functions:
498499
func_hash = func.get_code_context_hash()
499500
# Use a unique path identifier that includes function info
500-
path_key = f"{file_path}:{func.qualified_name}"
501-
code_contexts[path_key] = func_hash
502-
path_to_function_map[path_key] = (file_path, func)
501+
code_contexts.append({"file_path": Path(file_path).relative_to(project_root_path),
502+
"function_name": func.qualified_name, "code_hash": func_hash})
503503

504504
if not code_contexts:
505-
return {}, 0
505+
return set(tuple())
506506

507507
try:
508508
result = is_function_being_optimized_again(owner, repo, pr_number, code_contexts)
509-
already_optimized_paths: list[tuple[str, str]] = result.get("already_optimized_paths", [])
510-
return already_optimized_paths
509+
already_optimized_paths: list[tuple[str, str]] = result.get("already_optimized_tuples", [])
510+
return set(( project_root_path / Path(path[0]), path[1]) for path in already_optimized_paths)
511511

512512
except Exception as e:
513513
logger.warning(f"Failed to check optimization status: {e}")
514514
# Return all functions if API call fails
515-
return []
515+
return set(tuple())
516516

517517

518518
def filter_functions(
@@ -625,6 +625,7 @@ def filter_functions(
625625
f"{already_optimized_count} already optimized function{'s' if already_optimized_count != 1 else ''}": already_optimized_count,
626626
f"{blocklist_funcs_removed_count} function{'s' if blocklist_funcs_removed_count != 1 else ''} as previously optimized": blocklist_funcs_removed_count,
627627
f"{previous_checkpoint_functions_removed_count} function{'s' if previous_checkpoint_functions_removed_count != 1 else ''} skipped from checkpoint": previous_checkpoint_functions_removed_count,
628+
f"{already_optimized_paths_removed_count} function{'s' if already_optimized_paths_removed_count != 1 else ''} as previously attempted optimization": already_optimized_paths_removed_count,
628629
}
629630
log_string = "\n".join([k for k, v in log_info.items() if v > 0])
630631
if log_string:

codeflash/optimization/function_optimizer.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -375,17 +375,8 @@ def optimize_function(self) -> Result[BestOptimization, str]: # noqa: PLR0911
375375
self.log_successful_optimization(explanation, generated_tests, exp_type)
376376

377377
# Add function to code context hash if in gh actions
378-
try:
379-
repository = git.Repo(Path.cwd(), search_parent_directories=True)
380-
owner, repo = get_repo_owner_and_name(repository)
381-
except git.exc.InvalidGitRepositoryError:
382-
logger.warning("No git repository found")
383-
owner, repo = None, None
384-
pr_number = get_pr_number()
385-
386-
if owner and repo and pr_number is not None:
387-
code_context_hash = self.function_to_optimize.get_code_context_hash()
388-
add_code_context_hash(owner, repo, pr_number, code_context_hash)
378+
379+
add_code_context_hash(self.function_to_optimize.get_code_context_hash())
389380

390381
if self.args.override_fixtures:
391382
restore_conftest(original_conftest_content)

0 commit comments

Comments
 (0)