Skip to content
Merged
Show file tree
Hide file tree
Changes from 21 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
18 changes: 7 additions & 11 deletions codeflash/api/aiservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,8 @@ def get_optimization_impact(
replay_tests: str,
root_dir: Path,
concolic_tests: str, # noqa: ARG002
) -> str:
calling_fn_details: str,
) -> tuple[str, str]:
"""Compute the optimization impact of current Pull Request.
Args:
Expand All @@ -558,6 +559,7 @@ def get_optimization_impact(
replay_tests: str -> replay test table
root_dir: Path -> path of git directory
concolic_tests: str -> concolic_tests (not used)
calling_fn_details: str -> filenames and definitions of functions which call the function_to_optimize
Returns:
-------
Expand All @@ -577,13 +579,6 @@ 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 All @@ -598,25 +593,26 @@ def get_optimization_impact(
"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),
"calling_fn_details": calling_fn_details,
}
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 ""
return ("", str(e))

if response.status_code == 200:
return cast("str", response.json()["impact"])
return (cast("str", response.json()["impact"]), cast("str", response.json()["impact_explanation"]))
try:
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 ("", error)


class LocalAiServiceClient(AiServiceClient):
Expand Down
4 changes: 4 additions & 0 deletions codeflash/api/cfapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ def create_pr(
coverage_message: str,
replay_tests: str = "",
concolic_tests: str = "",
optimization_impact: str = "",
) -> Response:
"""Create a pull request, targeting the specified branch. (usually 'main').

Expand All @@ -197,6 +198,7 @@ def create_pr(
"coverage_message": coverage_message,
"replayTests": replay_tests,
"concolicTests": concolic_tests,
"optimizationImpact": optimization_impact,
}
return make_cfapi_request(endpoint="/create-pr", method="POST", payload=payload)

Expand All @@ -212,6 +214,7 @@ def create_staging(
replay_tests: str,
concolic_tests: str,
root_dir: Path,
optimization_impact: str = "",
) -> Response:
"""Create a staging pull request, targeting the specified branch. (usually 'staging').

Expand Down Expand Up @@ -252,6 +255,7 @@ def create_staging(
"coverage_message": coverage_message,
"replayTests": replay_tests,
"concolicTests": concolic_tests,
"optimizationImpact": optimization_impact,
}

return make_cfapi_request(endpoint="/create-staging", method="POST", payload=payload)
Expand Down
Loading
Loading