Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
39c3c64
python side done, todo backend js/ts
aseembits93 Oct 13, 2025
567a441
Update cfapi.py
aseembits93 Oct 13, 2025
2cedeaf
Apply suggestion from @aseembits93
aseembits93 Oct 13, 2025
7bc22c7
Merge branch 'main' into opt-impact-aseem
aseembits93 Oct 15, 2025
7c6a41e
todos for next iteration
aseembits93 Oct 15, 2025
1d48849
Merge branch 'main' into opt-impact-aseem
aseembits93 Oct 15, 2025
83fabf5
wip
aseembits93 Oct 15, 2025
06ffc13
wip
aseembits93 Oct 15, 2025
9d69a58
DIRTY
aseembits93 Oct 16, 2025
2764aff
todo cleanup
aseembits93 Oct 16, 2025
e1e8efd
start cleaning up, write tests
aseembits93 Oct 17, 2025
fc5274d
start cleaning up, write tests
aseembits93 Oct 17, 2025
2570a2e
Apply suggestion from @aseembits93
aseembits93 Oct 17, 2025
ca313a1
update uv.lock later
aseembits93 Oct 17, 2025
33bcab6
merge conflicts
aseembits93 Oct 17, 2025
7c8e0f9
Merge branch 'main' into opt-impact-aseem-improvement
aseembits93 Oct 17, 2025
9cd4743
Apply suggestion from @aseembits93
aseembits93 Oct 17, 2025
651ded4
experiment
aseembits93 Oct 17, 2025
9fbb5fd
Merge branch 'main' into opt-impact-aseem-improvement
aseembits93 Oct 17, 2025
65bb2d2
Enhance explanation message with calling context details
aseembits93 Oct 17, 2025
cc1af4e
Merge branch 'main' into opt-impact-aseem-improvement
aseembits93 Oct 17, 2025
26da88f
fix precommit later
aseembits93 Oct 20, 2025
2c6abd3
precommit fix
aseembits93 Oct 20, 2025
b5b85be
Apply suggestion from @aseembits93
aseembits93 Oct 20, 2025
dd4577c
Apply suggestion from @aseembits93
aseembits93 Oct 20, 2025
c37505d
Apply suggestion from @aseembits93
aseembits93 Oct 20, 2025
bdd23cd
change function signature
aseembits93 Oct 20, 2025
0d43f6d
minor bug fix
aseembits93 Oct 20, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions codeflash/api/aiservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,13 @@ def get_optimization_impact(
]
)
code_diff = f"```diff\n{diff_str}\n```"
# TODO get complexity metrics and fn call heuristics -> constructing a complete static call graph can be expensive for really large repos
# grep function name in codebase -> ast parser to get no of calls and no of calls in loop -> radon lib to get complexity metrics -> send as additional context to the AI service
# metric 1 -> call count - how many times the function is called in the codebase
# metric 2 -> loop call count - how many times the function is called in a loop in the codebase
# metric 3 -> presence of decorators like @profile, @cache -> this means the owner of the repo cares about the performance of this function
# metric 4 -> cyclomatic complexity (https://en.wikipedia.org/wiki/Cyclomatic_complexity)
# metric 5 (for future) -> halstead complexity (https://en.wikipedia.org/wiki/Halstead_complexity_measures)
logger.info("!lsp|Computing Optimization Impact…")
payload = {
"code_diff": code_diff,
Expand Down
2 changes: 2 additions & 0 deletions codeflash/api/cfapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ def suggest_changes(
coverage_message: str,
replay_tests: str = "",
concolic_tests: str = "",
optimization_impact: str = "",
) -> Response:
"""Suggest changes to a pull request.

Expand All @@ -155,6 +156,7 @@ def suggest_changes(
"coverage_message": coverage_message,
"replayTests": replay_tests,
"concolicTests": concolic_tests,
"optimizationImpact": optimization_impact,
}
return make_cfapi_request(endpoint="/suggest-pr-changes", method="POST", payload=payload)

Expand Down
455 changes: 449 additions & 6 deletions codeflash/code_utils/code_extractor.py

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions codeflash/code_utils/config_consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@
N_CANDIDATES_LP_EFFECTIVE = min(N_CANDIDATES_LP_LSP if _IS_LSP_ENABLED else N_CANDIDATES_LP, MAX_N_CANDIDATES_LP)
N_TESTS_TO_GENERATE_EFFECTIVE = N_TESTS_TO_GENERATE_LSP if _IS_LSP_ENABLED else N_TESTS_TO_GENERATE
TOTAL_LOOPING_TIME_EFFECTIVE = TOTAL_LOOPING_TIME_LSP if _IS_LSP_ENABLED else TOTAL_LOOPING_TIME

MAX_CONTEXT_LEN_IMPACT = 1000
TIME_LIMIT_FOR_OPT_IMPACT = 10 # in sec
7 changes: 7 additions & 0 deletions codeflash/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@
from codeflash.verification.comparator import comparator


@dataclass
class ImpactMetrics:
cyclomatic_complexity: Optional[int] = None
cyclomatic_complexity_rating: Optional[str] = None
calling_fns: Optional[str] = None


@dataclass(frozen=True)
class AIServiceRefinerRequest:
optimization_id: str
Expand Down
22 changes: 14 additions & 8 deletions codeflash/optimization/function_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from codeflash.benchmarking.utils import process_benchmark_data
from codeflash.cli_cmds.console import code_print, console, logger, lsp_log, progress_bar
from codeflash.code_utils import env_utils
from codeflash.code_utils.code_extractor import get_opt_impact_metrics
from codeflash.code_utils.code_replacer import (
add_custom_marker_to_all_tests,
modify_autouse_fixture,
Expand Down Expand Up @@ -1461,14 +1462,19 @@ def process_review(

if raise_pr or staging_review:
data["root_dir"] = git_root_dir()
# 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}")
opt_impact_response = ""
try:
opt_impact_response = self.aiservice_client.get_optimization_impact(**data)
except Exception as e:
logger.debug(f"optimization impact response failed, investigate {e}")
data["optimization_impact"] = opt_impact_response
data["impact_metrics"] = get_opt_impact_metrics(
self.function_to_optimize_source_code,
self.function_to_optimize.file_path,
self.function_to_optimize.qualified_name,
self.project_root,
self.test_cfg.tests_root,
)
if raise_pr and not staging_review:
data["git_remote"] = self.args.git_remote
check_create_pr(**data)
Expand Down
2 changes: 2 additions & 0 deletions codeflash/result/create_pr.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ def check_create_pr(
concolic_tests: str,
root_dir: Path,
git_remote: Optional[str] = None,
optimization_impact: str = "",
) -> None:
pr_number: Optional[int] = env_utils.get_pr_number()
git_repo = git.Repo(search_parent_directories=True)
Expand Down Expand Up @@ -226,6 +227,7 @@ def check_create_pr(
coverage_message=coverage_message,
replay_tests=replay_tests,
concolic_tests=concolic_tests,
optimization_impact=optimization_impact,
)
if response.ok:
logger.info(f"Suggestions were successfully made to PR #{pr_number}")
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ dependencies = [
"pygls>=1.3.1",
"codeflash-benchmark",
"filelock",
"radon", #for code complexity metrics
]

[project.urls]
Expand Down
27 changes: 27 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.