Skip to content

Commit 26da88f

Browse files
committed
fix precommit later
1 parent cc1af4e commit 26da88f

File tree

9 files changed

+39
-94
lines changed

9 files changed

+39
-94
lines changed

codeflash/api/aiservice.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ def generate_regression_tests( # noqa: D417
532532
ph("cli-testgen-error-response", {"response_status_code": response.status_code, "error": response.text})
533533
return None
534534

535-
def get_optimization_impact(
535+
def get_optimization_review(
536536
self,
537537
original_code: dict[Path, str],
538538
new_code: dict[Path, str],
@@ -546,7 +546,7 @@ def get_optimization_impact(
546546
concolic_tests: str, # noqa: ARG002
547547
calling_fn_details: str,
548548
) -> tuple[str, str]:
549-
"""Compute the optimization impact of current Pull Request.
549+
"""Compute the optimization review of current Pull Request.
550550
551551
Args:
552552
original_code: dict -> data structure mapping file paths to function definition for original code
@@ -563,7 +563,7 @@ def get_optimization_impact(
563563
564564
Returns:
565565
-------
566-
- 'high' or 'low' optimization impact
566+
- 'high', 'medium' or 'low' optimization review
567567
568568
"""
569569
diff_str = "\n".join(
@@ -579,7 +579,7 @@ def get_optimization_impact(
579579
]
580580
)
581581
code_diff = f"```diff\n{diff_str}\n```"
582-
logger.info("!lsp|Computing Optimization Impact…")
582+
logger.info("!lsp|Computing Optimization Review…")
583583
payload = {
584584
"code_diff": code_diff,
585585
"explanation": explanation.raw_explanation_message,
@@ -597,19 +597,19 @@ def get_optimization_impact(
597597
}
598598
console.rule()
599599
try:
600-
response = self.make_ai_service_request("/optimization_impact", payload=payload, timeout=600)
600+
response = self.make_ai_service_request("/optimization_review", payload=payload, timeout=600)
601601
except requests.exceptions.RequestException as e:
602602
logger.exception(f"Error generating optimization refinements: {e}")
603603
ph("cli-optimize-error-caught", {"error": str(e)})
604604
return ("", str(e))
605605

606606
if response.status_code == 200:
607-
return (cast("str", response.json()["impact"]), cast("str", response.json()["impact_explanation"]))
607+
return (cast("str", response.json()["review"]), cast("str", response.json()["review_explanation"]))
608608
try:
609609
error = cast("str", response.json()["error"])
610610
except Exception:
611611
error = response.text
612-
logger.error(f"Error generating impact candidates: {response.status_code} - {error}")
612+
logger.error(f"Error generating optimization review: {response.status_code} - {error}")
613613
ph("cli-optimize-error-response", {"response_status_code": response.status_code, "error": error})
614614
console.rule()
615615
return ("", error)

codeflash/api/cfapi.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ def suggest_changes(
130130
coverage_message: str,
131131
replay_tests: str = "",
132132
concolic_tests: str = "",
133-
optimization_impact: str = "",
133+
optimization_review: str = "",
134134
) -> Response:
135135
"""Suggest changes to a pull request.
136136
@@ -156,7 +156,7 @@ def suggest_changes(
156156
"coverage_message": coverage_message,
157157
"replayTests": replay_tests,
158158
"concolicTests": concolic_tests,
159-
"optimizationImpact": optimization_impact,
159+
"optimizationImpact": optimization_review, # impact keyword left for legacy reasons, touches js/ts code
160160
}
161161
return make_cfapi_request(endpoint="/suggest-pr-changes", method="POST", payload=payload)
162162

@@ -173,7 +173,7 @@ def create_pr(
173173
coverage_message: str,
174174
replay_tests: str = "",
175175
concolic_tests: str = "",
176-
optimization_impact: str = "",
176+
optimization_review: str = "",
177177
) -> Response:
178178
"""Create a pull request, targeting the specified branch. (usually 'main').
179179
@@ -198,7 +198,7 @@ def create_pr(
198198
"coverage_message": coverage_message,
199199
"replayTests": replay_tests,
200200
"concolicTests": concolic_tests,
201-
"optimizationImpact": optimization_impact,
201+
"optimizationImpact": optimization_review, # Impact keyword left for legacy reasons, it touches js/ts codebase
202202
}
203203
return make_cfapi_request(endpoint="/create-pr", method="POST", payload=payload)
204204

@@ -214,7 +214,7 @@ def create_staging(
214214
replay_tests: str,
215215
concolic_tests: str,
216216
root_dir: Path,
217-
optimization_impact: str = "",
217+
optimization_review: str = "",
218218
) -> Response:
219219
"""Create a staging pull request, targeting the specified branch. (usually 'staging').
220220
@@ -255,7 +255,7 @@ def create_staging(
255255
"coverage_message": coverage_message,
256256
"replayTests": replay_tests,
257257
"concolicTests": concolic_tests,
258-
"optimizationImpact": optimization_impact,
258+
"optimizationImpact": optimization_review, # Impact keyword left for legacy reasons, it touches js/ts codebase
259259
}
260260

261261
return make_cfapi_request(endpoint="/create-staging", method="POST", payload=payload)

codeflash/code_utils/code_extractor.py

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

33
import ast
4-
import time
54
from dataclasses import dataclass
65
from itertools import chain
76
from pathlib import Path
@@ -14,7 +13,7 @@
1413
from libcst.helpers import calculate_module_and_package
1514

1615
from codeflash.cli_cmds.console import logger
17-
from codeflash.code_utils.config_consts import MAX_CONTEXT_LEN_IMPACT, TIME_LIMIT_FOR_OPT_IMPACT
16+
from codeflash.code_utils.config_consts import MAX_CONTEXT_LEN_REVIEW
1817
from codeflash.models.models import CodePosition, FunctionParent
1918

2019
if TYPE_CHECKING:
@@ -42,7 +41,7 @@ def visit_FunctionDef(self, node: cst.FunctionDef) -> Optional[bool]: # noqa: A
4241
def leave_FunctionDef(self, original_node: cst.FunctionDef) -> None: # noqa: ARG002
4342
self.scope_depth -= 1
4443

45-
def visit_ClassDef(self, node: cst.ClassDef) -> Optional[bool]:
44+
def visit_ClassDef(self, node: cst.ClassDef) -> Optional[bool]: # noqa: ARG002
4645
self.scope_depth += 1
4746
return True
4847

@@ -86,7 +85,7 @@ def __init__(self, new_assignments: dict[str, cst.Assign], new_assignment_order:
8685
def visit_FunctionDef(self, node: cst.FunctionDef) -> None: # noqa: ARG002
8786
self.scope_depth += 1
8887

89-
def leave_FunctionDef(self, original_node: cst.FunctionDef, updated_node: cst.FunctionDef) -> cst.FunctionDef:
88+
def leave_FunctionDef(self, original_node: cst.FunctionDef, updated_node: cst.FunctionDef) -> cst.FunctionDef: # noqa: ARG002
9089
self.scope_depth -= 1
9190
return updated_node
9291

@@ -100,7 +99,7 @@ def leave_ClassDef(self, original_node: cst.ClassDef, updated_node: cst.ClassDef
10099
def visit_If(self, node: cst.If) -> None: # noqa: ARG002
101100
self.if_else_depth += 1
102101

103-
def leave_If(self, original_node: cst.If, updated_node: cst.If) -> cst.If:
102+
def leave_If(self, original_node: cst.If, updated_node: cst.If) -> cst.If: # noqa: ARG002
104103
self.if_else_depth -= 1
105104
return updated_node
106105

@@ -148,7 +147,7 @@ def _find_insertion_index(self, updated_node: cst.Module) -> int:
148147

149148
return insert_index
150149

151-
def leave_Module(self, original_node: cst.Module, updated_node: cst.Module) -> cst.Module:
150+
def leave_Module(self, original_node: cst.Module, updated_node: cst.Module) -> cst.Module: # noqa: ARG002
152151
# Add any new assignments that weren't in the original file
153152
new_statements = list(updated_node.body)
154153

@@ -192,20 +191,20 @@ def __init__(self) -> None:
192191
self.global_statements = []
193192
self.in_function_or_class = False
194193

195-
def visit_ClassDef(self, node: cst.ClassDef) -> bool:
194+
def visit_ClassDef(self, node: cst.ClassDef) -> bool: # noqa: ARG002
196195
# Don't visit inside classes
197196
self.in_function_or_class = True
198197
return False
199198

200-
def leave_ClassDef(self, original_node: cst.ClassDef) -> None:
199+
def leave_ClassDef(self, original_node: cst.ClassDef) -> None: # noqa: ARG002
201200
self.in_function_or_class = False
202201

203-
def visit_FunctionDef(self, node: cst.FunctionDef) -> bool:
202+
def visit_FunctionDef(self, node: cst.FunctionDef) -> bool: # noqa: ARG002
204203
# Don't visit inside functions
205204
self.in_function_or_class = True
206205
return False
207206

208-
def leave_FunctionDef(self, original_node: cst.FunctionDef) -> None:
207+
def leave_FunctionDef(self, original_node: cst.FunctionDef) -> None: # noqa: ARG002
209208
self.in_function_or_class = False
210209

211210
def visit_SimpleStatementLine(self, node: cst.SimpleStatementLine) -> None:
@@ -286,16 +285,16 @@ def visit_Module(self, node: cst.Module) -> None:
286285
self.depth = 0
287286
self._collect_imports_from_block(node)
288287

289-
def visit_FunctionDef(self, node: cst.FunctionDef) -> None:
288+
def visit_FunctionDef(self, node: cst.FunctionDef) -> None: # noqa: ARG002
290289
self.depth += 1
291290

292-
def leave_FunctionDef(self, node: cst.FunctionDef) -> None:
291+
def leave_FunctionDef(self, node: cst.FunctionDef) -> None: # noqa: ARG002
293292
self.depth -= 1
294293

295-
def visit_ClassDef(self, node: cst.ClassDef) -> None:
294+
def visit_ClassDef(self, node: cst.ClassDef) -> None: # noqa: ARG002
296295
self.depth += 1
297296

298-
def leave_ClassDef(self, node: cst.ClassDef) -> None:
297+
def leave_ClassDef(self, node: cst.ClassDef) -> None: # noqa: ARG002
299298
self.depth -= 1
300299

301300
def visit_If(self, node: cst.If) -> None:
@@ -329,7 +328,7 @@ def leave_SimpleStatementLine(
329328

330329
return cst.Module(body=[updated_node])
331330

332-
def leave_Module(self, original_node: cst.Module, updated_node: cst.Module) -> cst.Module:
331+
def leave_Module(self, original_node: cst.Module, updated_node: cst.Module) -> cst.Module: # noqa: ARG002
333332
# If there were no imports, add at the beginning of the module
334333
if self.last_import_line == 0 and not self.inserted:
335334
updated_body = list(updated_node.body)
@@ -1058,13 +1057,10 @@ def find_function_calls(source_code: str, target_function_name: str, target_file
10581057
def find_occurances(
10591058
qualified_name: str, file_path: str, fn_matches: list[Path], project_root: Path, tests_root: Path
10601059
) -> list[str]: # max chars for context
1061-
start_time = time.time()
10621060
context_len = 0
10631061
fn_call_context = ""
10641062
for cur_file in fn_matches:
1065-
if time.time() - start_time > TIME_LIMIT_FOR_OPT_IMPACT:
1066-
break
1067-
if context_len > MAX_CONTEXT_LEN_IMPACT:
1063+
if context_len > MAX_CONTEXT_LEN_REVIEW:
10681064
break
10691065
cur_file_path = Path(cur_file)
10701066
# exclude references in tests
@@ -1151,7 +1147,7 @@ def get_fn_references_jedi(
11511147
return []
11521148

11531149

1154-
def get_opt_impact_metrics(
1150+
def get_opt_review_metrics(
11551151
source_code: str, file_path: Path, qualified_name: str, project_root: Path, tests_root: Path
11561152
) -> str:
11571153
try:

codeflash/code_utils/config_consts.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,4 @@
3535
N_TESTS_TO_GENERATE_EFFECTIVE = N_TESTS_TO_GENERATE_LSP if _IS_LSP_ENABLED else N_TESTS_TO_GENERATE
3636
TOTAL_LOOPING_TIME_EFFECTIVE = TOTAL_LOOPING_TIME_LSP if _IS_LSP_ENABLED else TOTAL_LOOPING_TIME
3737

38-
MAX_CONTEXT_LEN_IMPACT = 1000
39-
TIME_LIMIT_FOR_OPT_IMPACT = 10 # in sec
38+
MAX_CONTEXT_LEN_REVIEW = 1000

codeflash/models/models.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,6 @@
3131
from codeflash.verification.comparator import comparator
3232

3333

34-
@dataclass
35-
class ImpactMetrics:
36-
cyclomatic_complexity: Optional[int] = None
37-
cyclomatic_complexity_rating: Optional[str] = None
38-
calling_fns: Optional[str] = None
39-
40-
4134
@dataclass(frozen=True)
4235
class AIServiceRefinerRequest:
4336
optimization_id: str

codeflash/optimization/function_optimizer.py

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from codeflash.benchmarking.utils import process_benchmark_data
2424
from codeflash.cli_cmds.console import code_print, console, logger, lsp_log, progress_bar
2525
from codeflash.code_utils import env_utils
26-
from codeflash.code_utils.code_extractor import get_opt_impact_metrics
26+
from codeflash.code_utils.code_extractor import get_opt_review_metrics
2727
from codeflash.code_utils.code_replacer import (
2828
add_custom_marker_to_all_tests,
2929
modify_autouse_fixture,
@@ -1461,36 +1461,21 @@ def process_review(
14611461

14621462
if raise_pr or staging_review:
14631463
data["root_dir"] = git_root_dir()
1464-
calling_fn_details = get_opt_impact_metrics(
1464+
calling_fn_details = get_opt_review_metrics(
14651465
self.function_to_optimize_source_code,
14661466
self.function_to_optimize.file_path,
14671467
self.function_to_optimize.qualified_name,
14681468
self.project_root,
14691469
self.test_cfg.tests_root,
14701470
)
1471-
opt_impact_response = ""
1471+
opt_review_response = ""
14721472
try:
1473-
opt_impact_response = self.aiservice_client.get_optimization_impact(
1473+
opt_review_response = self.aiservice_client.get_optimization_review(
14741474
**data, calling_fn_details=calling_fn_details
14751475
)
14761476
except Exception as e:
1477-
logger.debug(f"optimization impact response failed, investigate {e}")
1478-
data["optimization_impact"] = opt_impact_response[0]
1479-
new_explanation_with_opt_explanation = Explanation(
1480-
raw_explanation_message=f"Impact: {opt_impact_response[0]}\n Impact_explanation: {opt_impact_response[1]} END OF IMPACT EXPLANATION\nCALLING CONTEXT \n{calling_fn_details}\nEND OF CALLING CONTEXT\n"
1481-
+ new_explanation.raw_explanation_message,
1482-
winning_behavior_test_results=explanation.winning_behavior_test_results,
1483-
winning_benchmarking_test_results=explanation.winning_benchmarking_test_results,
1484-
original_runtime_ns=explanation.original_runtime_ns,
1485-
best_runtime_ns=explanation.best_runtime_ns,
1486-
function_name=explanation.function_name,
1487-
file_path=explanation.file_path,
1488-
benchmark_details=explanation.benchmark_details,
1489-
original_async_throughput=explanation.original_async_throughput,
1490-
best_async_throughput=explanation.best_async_throughput,
1491-
)
1492-
best_optimization.explanation_v2 = new_explanation_with_opt_explanation.explanation_message()
1493-
data["explanation"] = new_explanation_with_opt_explanation
1477+
logger.debug(f"optimization review response failed, investigate {e}")
1478+
data["optimization_review_response"] = opt_review_response[0]
14941479
if raise_pr and not staging_review:
14951480
data["git_remote"] = self.args.git_remote
14961481
check_create_pr(**data)

codeflash/result/create_pr.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def check_create_pr(
185185
concolic_tests: str,
186186
root_dir: Path,
187187
git_remote: Optional[str] = None,
188-
optimization_impact: str = "",
188+
optimization_review: str = "",
189189
) -> None:
190190
pr_number: Optional[int] = env_utils.get_pr_number()
191191
git_repo = git.Repo(search_parent_directories=True)
@@ -227,7 +227,7 @@ def check_create_pr(
227227
coverage_message=coverage_message,
228228
replay_tests=replay_tests,
229229
concolic_tests=concolic_tests,
230-
optimization_impact=optimization_impact,
230+
optimization_review=optimization_review,
231231
)
232232
if response.ok:
233233
logger.info(f"Suggestions were successfully made to PR #{pr_number}")
@@ -277,7 +277,7 @@ def check_create_pr(
277277
coverage_message=coverage_message,
278278
replay_tests=replay_tests,
279279
concolic_tests=concolic_tests,
280-
optimization_impact=optimization_impact,
280+
optimization_review=optimization_review,
281281
)
282282
if response.ok:
283283
pr_id = response.text

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ dependencies = [
4444
"pygls>=1.3.1",
4545
"codeflash-benchmark",
4646
"filelock",
47-
"radon", #for code complexity metrics
4847
]
4948

5049
[project.urls]

0 commit comments

Comments
 (0)