From 71db7f67dd7b7ddc0044d37ebd87bd9170beac05 Mon Sep 17 00:00:00 2001 From: Alvin Ryanputra Date: Fri, 18 Apr 2025 11:34:45 -0400 Subject: [PATCH 01/13] revert fto change --- codeflash/discovery/functions_to_optimize.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/codeflash/discovery/functions_to_optimize.py b/codeflash/discovery/functions_to_optimize.py index 6e4f744d7..993c24eea 100644 --- a/codeflash/discovery/functions_to_optimize.py +++ b/codeflash/discovery/functions_to_optimize.py @@ -299,7 +299,7 @@ def get_all_replay_test_functions( if valid_function.qualified_name == function_name ] ) - if len(filtered_list): + if filtered_list: filtered_valid_functions[file_path] = filtered_list return filtered_valid_functions @@ -364,7 +364,7 @@ def visit_ClassDef(self, node: ast.ClassDef) -> None: ): self.is_staticmethod = True return - elif self.line_no: + else: # If we have line number info, check if class has a static method with the same line number # This way, if we don't have the class name, we can still find the static method for body_node in node.body: From 16c653f6a5461f9050f946acbc7722dc4665ca7d Mon Sep 17 00:00:00 2001 From: Alvin Ryanputra Date: Fri, 18 Apr 2025 11:36:35 -0400 Subject: [PATCH 02/13] revert fto change --- codeflash/discovery/functions_to_optimize.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/codeflash/discovery/functions_to_optimize.py b/codeflash/discovery/functions_to_optimize.py index 993c24eea..96f94b328 100644 --- a/codeflash/discovery/functions_to_optimize.py +++ b/codeflash/discovery/functions_to_optimize.py @@ -358,11 +358,11 @@ def visit_ClassDef(self, node: ast.ClassDef) -> None: for decorator in body_node.decorator_list ): self.is_classmethod = True - elif any( - isinstance(decorator, ast.Name) and decorator.id == "staticmethod" - for decorator in body_node.decorator_list - ): - self.is_staticmethod = True + # elif any( + # isinstance(decorator, ast.Name) and decorator.id == "staticmethod" + # for decorator in body_node.decorator_list + # ): + # self.is_staticmethod = True return else: # If we have line number info, check if class has a static method with the same line number From 68e5091b922f9165d9ddda72ee854ae96d2e669b Mon Sep 17 00:00:00 2001 From: Alvin Ryanputra Date: Fri, 18 Apr 2025 11:39:58 -0400 Subject: [PATCH 03/13] try without --benchmark --- .github/workflows/codeflash-optimize.yaml | 2 +- codeflash/discovery/functions_to_optimize.py | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/codeflash-optimize.yaml b/.github/workflows/codeflash-optimize.yaml index 357269116..6a08635bf 100644 --- a/.github/workflows/codeflash-optimize.yaml +++ b/.github/workflows/codeflash-optimize.yaml @@ -68,4 +68,4 @@ jobs: id: optimize_code run: | source .venv/bin/activate - poetry run codeflash --benchmark + poetry run codeflash diff --git a/codeflash/discovery/functions_to_optimize.py b/codeflash/discovery/functions_to_optimize.py index 96f94b328..8adfb4e00 100644 --- a/codeflash/discovery/functions_to_optimize.py +++ b/codeflash/discovery/functions_to_optimize.py @@ -358,13 +358,13 @@ def visit_ClassDef(self, node: ast.ClassDef) -> None: for decorator in body_node.decorator_list ): self.is_classmethod = True - # elif any( - # isinstance(decorator, ast.Name) and decorator.id == "staticmethod" - # for decorator in body_node.decorator_list - # ): - # self.is_staticmethod = True + elif any( + isinstance(decorator, ast.Name) and decorator.id == "staticmethod" + for decorator in body_node.decorator_list + ): + self.is_staticmethod = True return - else: + elif self.line_no: # If we have line number info, check if class has a static method with the same line number # This way, if we don't have the class name, we can still find the static method for body_node in node.body: From 92ecdc7f9f6a8ad46b801d839d899e4f571e0c0f Mon Sep 17 00:00:00 2001 From: Alvin Ryanputra Date: Fri, 18 Apr 2025 11:59:35 -0400 Subject: [PATCH 04/13] likely an issue with setup --- .github/workflows/codeflash-optimize.yaml | 2 +- codeflash/cli_cmds/cli.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/codeflash-optimize.yaml b/.github/workflows/codeflash-optimize.yaml index 6a08635bf..357269116 100644 --- a/.github/workflows/codeflash-optimize.yaml +++ b/.github/workflows/codeflash-optimize.yaml @@ -68,4 +68,4 @@ jobs: id: optimize_code run: | source .venv/bin/activate - poetry run codeflash + poetry run codeflash --benchmark diff --git a/codeflash/cli_cmds/cli.py b/codeflash/cli_cmds/cli.py index ed0dbd760..29fd5d7f1 100644 --- a/codeflash/cli_cmds/cli.py +++ b/codeflash/cli_cmds/cli.py @@ -167,6 +167,7 @@ def process_pyproject_config(args: Namespace) -> Namespace: # in this case, the ".." becomes outside project scope, causing issues with un-importable paths args.project_root = project_root_from_module_root(args.module_root, pyproject_file_path) args.tests_root = Path(args.tests_root).resolve() + args.benchmarks_root = Path(args.benchmarks_root).resolve() args.test_project_root = project_root_from_module_root(args.tests_root, pyproject_file_path) return handle_optimize_all_arg_parsing(args) From 233cf319745243571847fd2b89945f6ef5e09ad0 Mon Sep 17 00:00:00 2001 From: Alvin Ryanputra Date: Fri, 18 Apr 2025 13:35:04 -0400 Subject: [PATCH 05/13] attempt removing --benchmark from workflow --- .github/workflows/codeflash-optimize.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/codeflash-optimize.yaml b/.github/workflows/codeflash-optimize.yaml index 357269116..d5c817405 100644 --- a/.github/workflows/codeflash-optimize.yaml +++ b/.github/workflows/codeflash-optimize.yaml @@ -15,7 +15,7 @@ jobs: optimize: name: Optimize new Python code # Dynamically determine if environment is needed only when workflow files change and contributor is external - environment: ${{ (github.event_name == 'workflow_dispatch' || (contains(toJSON(github.event.pull_request.files.*.filename), '.github/workflows/') && github.event.pull_request.user.login != 'misrasaurabh1' && github.event.pull_request.user.login != 'KRRT7')) && 'external-trusted-contributors' || '' }} + environment: ${{ (github.event_name == 'workflow_dispatch' || (contains(toJSON(github.event.pull_request.files.*.filename), '.github/workflows/') && github.event.pull_request.user.login != 'misrasaurabh1' && github.event.pull_request.user.login != 'KRRT7' && github.event.pull_request.user.login != 'alvin-r')) && 'external-trusted-contributors' || '' }} if: ${{ github.actor != 'codeflash-ai[bot]' }} runs-on: ubuntu-latest env: @@ -40,7 +40,7 @@ jobs: echo "PR Author: $AUTHOR" # Allowlist check - if [[ "$AUTHOR" == "misrasaurabh1" || "$AUTHOR" == "KRRT7" ]]; then + if [[ "$AUTHOR" == "misrasaurabh1" || "$AUTHOR" == "KRRT7" || "$AUTHOR" == "alvin-r"]]; then echo "✅ Authorized user ($AUTHOR). Proceeding." elif [[ "${{ github.event.pull_request.state }}" == "open" ]]; then echo "✅ PR is open. Proceeding with appropriate protections." @@ -68,4 +68,4 @@ jobs: id: optimize_code run: | source .venv/bin/activate - poetry run codeflash --benchmark + poetry run codeflash From b89602fca8f3a8a8270b0fba957c4a971bfb615d Mon Sep 17 00:00:00 2001 From: Alvin Ryanputra Date: Fri, 18 Apr 2025 13:38:57 -0400 Subject: [PATCH 06/13] debug --- .github/workflows/codeflash-optimize.yaml | 6 +++--- codeflash/optimization/optimizer.py | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/codeflash-optimize.yaml b/.github/workflows/codeflash-optimize.yaml index d5c817405..357269116 100644 --- a/.github/workflows/codeflash-optimize.yaml +++ b/.github/workflows/codeflash-optimize.yaml @@ -15,7 +15,7 @@ jobs: optimize: name: Optimize new Python code # Dynamically determine if environment is needed only when workflow files change and contributor is external - environment: ${{ (github.event_name == 'workflow_dispatch' || (contains(toJSON(github.event.pull_request.files.*.filename), '.github/workflows/') && github.event.pull_request.user.login != 'misrasaurabh1' && github.event.pull_request.user.login != 'KRRT7' && github.event.pull_request.user.login != 'alvin-r')) && 'external-trusted-contributors' || '' }} + environment: ${{ (github.event_name == 'workflow_dispatch' || (contains(toJSON(github.event.pull_request.files.*.filename), '.github/workflows/') && github.event.pull_request.user.login != 'misrasaurabh1' && github.event.pull_request.user.login != 'KRRT7')) && 'external-trusted-contributors' || '' }} if: ${{ github.actor != 'codeflash-ai[bot]' }} runs-on: ubuntu-latest env: @@ -40,7 +40,7 @@ jobs: echo "PR Author: $AUTHOR" # Allowlist check - if [[ "$AUTHOR" == "misrasaurabh1" || "$AUTHOR" == "KRRT7" || "$AUTHOR" == "alvin-r"]]; then + if [[ "$AUTHOR" == "misrasaurabh1" || "$AUTHOR" == "KRRT7" ]]; then echo "✅ Authorized user ($AUTHOR). Proceeding." elif [[ "${{ github.event.pull_request.state }}" == "open" ]]; then echo "✅ PR is open. Proceeding with appropriate protections." @@ -68,4 +68,4 @@ jobs: id: optimize_code run: | source .venv/bin/activate - poetry run codeflash + poetry run codeflash --benchmark diff --git a/codeflash/optimization/optimizer.py b/codeflash/optimization/optimizer.py index 7f42b58c4..6fa925466 100644 --- a/codeflash/optimization/optimizer.py +++ b/codeflash/optimization/optimizer.py @@ -2,9 +2,9 @@ import ast import os -import time import shutil import tempfile +import time from collections import defaultdict from pathlib import Path from typing import TYPE_CHECKING @@ -83,7 +83,7 @@ def run(self) -> None: function_optimizer = None file_to_funcs_to_optimize: dict[Path, list[FunctionToOptimize]] num_optimizable_functions: int - + logger.info(self.args) # discover functions (file_to_funcs_to_optimize, num_optimizable_functions) = get_functions_to_optimize( optimize_all=self.args.all, From 80b1057bec18d83ec2eab9fca7d5e16312606081 Mon Sep 17 00:00:00 2001 From: Alvin Ryanputra Date: Fri, 18 Apr 2025 13:46:05 -0400 Subject: [PATCH 07/13] debugging --- codeflash/optimization/optimizer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codeflash/optimization/optimizer.py b/codeflash/optimization/optimizer.py index 6fa925466..bd6c4c9b1 100644 --- a/codeflash/optimization/optimizer.py +++ b/codeflash/optimization/optimizer.py @@ -76,7 +76,7 @@ def create_function_optimizer( def run(self) -> None: ph("cli-optimize-run-start") - logger.info("Running optimizer.") + logger.info("Fixing Running optimizer.") console.rule() if not env_utils.ensure_codeflash_api_key(): return From 9868c38c0beb86e1fd260ea8f27f872d00c029d3 Mon Sep 17 00:00:00 2001 From: Alvin Ryanputra Date: Fri, 18 Apr 2025 14:33:50 -0400 Subject: [PATCH 08/13] debugging --- .github/workflows/codeflash-optimize2.yaml | 52 ++++++++++++++++++++ codeflash/discovery/functions_to_optimize.py | 1 + 2 files changed, 53 insertions(+) create mode 100644 .github/workflows/codeflash-optimize2.yaml diff --git a/.github/workflows/codeflash-optimize2.yaml b/.github/workflows/codeflash-optimize2.yaml new file mode 100644 index 000000000..cb44721c3 --- /dev/null +++ b/.github/workflows/codeflash-optimize2.yaml @@ -0,0 +1,52 @@ +name: CodeFlash + +on: + pull_request_target: + paths: + - '**' # Trigger for all paths + + workflow_dispatch: + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + optimize: + name: Optimize new Python code + # Dynamically determine if environment is needed only when workflow files change and contributor is external + environment: ${{ (github.event_name == 'workflow_dispatch' || (contains(toJSON(github.event.pull_request.files.*.filename), '.github/workflows/') && github.event.pull_request.user.login != 'misrasaurabh1' && github.event.pull_request.user.login != 'KRRT7')) && 'external-trusted-contributors' || '' }} + if: ${{ github.actor != 'codeflash-ai[bot]' }} + runs-on: ubuntu-latest + env: + CODEFLASH_AIS_SERVER: prod + POSTHOG_API_KEY: ${{ secrets.POSTHOG_API_KEY }} + CODEFLASH_API_KEY: ${{ secrets.CODEFLASH_API_KEY }} + CODEFLASH_PR_NUMBER: ${{ github.event.number }} + COLUMNS: 110 + steps: + - name: 🛎️ Checkout + uses: actions/checkout@v4 + with: + ref: ${{ github.event.pull_request.head.ref }} + repository: ${{ github.event.pull_request.head.repo.full_name }} + fetch-depth: 0 + + + - name: 🐍 Set up Python 3.11 for CLI + uses: astral-sh/setup-uv@v5 + with: + python-version: 3.11.6 + + - name: 📦 Install dependencies (CLI) + run: | + uv tool install poetry + uv venv + source .venv/bin/activate + poetry install --with dev + + - name: ⚡️Codeflash Optimization + id: optimize_code + run: | + source .venv/bin/activate + poetry run codeflash --benchmark diff --git a/codeflash/discovery/functions_to_optimize.py b/codeflash/discovery/functions_to_optimize.py index 8adfb4e00..8ba2d0332 100644 --- a/codeflash/discovery/functions_to_optimize.py +++ b/codeflash/discovery/functions_to_optimize.py @@ -201,6 +201,7 @@ def get_functions_to_optimize( functions, test_cfg.tests_root, ignore_paths, project_root, module_root ) logger.info(f"Found {functions_count} function{'s' if functions_count > 1 else ''} to optimize") + logger.info(f"{filtered_modified_functions}") return filtered_modified_functions, functions_count From b194b0d42ca7cea7bbba7b26761f8012672901a0 Mon Sep 17 00:00:00 2001 From: Alvin Ryanputra Date: Fri, 18 Apr 2025 14:40:05 -0400 Subject: [PATCH 09/13] debugging --- .github/workflows/codeflash-optimize2.yaml | 52 ---------------------- 1 file changed, 52 deletions(-) delete mode 100644 .github/workflows/codeflash-optimize2.yaml diff --git a/.github/workflows/codeflash-optimize2.yaml b/.github/workflows/codeflash-optimize2.yaml deleted file mode 100644 index cb44721c3..000000000 --- a/.github/workflows/codeflash-optimize2.yaml +++ /dev/null @@ -1,52 +0,0 @@ -name: CodeFlash - -on: - pull_request_target: - paths: - - '**' # Trigger for all paths - - workflow_dispatch: - -concurrency: - group: ${{ github.workflow }}-${{ github.ref }} - cancel-in-progress: true - -jobs: - optimize: - name: Optimize new Python code - # Dynamically determine if environment is needed only when workflow files change and contributor is external - environment: ${{ (github.event_name == 'workflow_dispatch' || (contains(toJSON(github.event.pull_request.files.*.filename), '.github/workflows/') && github.event.pull_request.user.login != 'misrasaurabh1' && github.event.pull_request.user.login != 'KRRT7')) && 'external-trusted-contributors' || '' }} - if: ${{ github.actor != 'codeflash-ai[bot]' }} - runs-on: ubuntu-latest - env: - CODEFLASH_AIS_SERVER: prod - POSTHOG_API_KEY: ${{ secrets.POSTHOG_API_KEY }} - CODEFLASH_API_KEY: ${{ secrets.CODEFLASH_API_KEY }} - CODEFLASH_PR_NUMBER: ${{ github.event.number }} - COLUMNS: 110 - steps: - - name: 🛎️ Checkout - uses: actions/checkout@v4 - with: - ref: ${{ github.event.pull_request.head.ref }} - repository: ${{ github.event.pull_request.head.repo.full_name }} - fetch-depth: 0 - - - - name: 🐍 Set up Python 3.11 for CLI - uses: astral-sh/setup-uv@v5 - with: - python-version: 3.11.6 - - - name: 📦 Install dependencies (CLI) - run: | - uv tool install poetry - uv venv - source .venv/bin/activate - poetry install --with dev - - - name: ⚡️Codeflash Optimization - id: optimize_code - run: | - source .venv/bin/activate - poetry run codeflash --benchmark From 9fe888ceabb0ca92c9ef5f93b44086e6922cdbd4 Mon Sep 17 00:00:00 2001 From: Alvin Ryanputra Date: Fri, 18 Apr 2025 14:46:15 -0400 Subject: [PATCH 10/13] debugging --- codeflash/optimization/optimizer.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/codeflash/optimization/optimizer.py b/codeflash/optimization/optimizer.py index bd6c4c9b1..2f259118c 100644 --- a/codeflash/optimization/optimizer.py +++ b/codeflash/optimization/optimizer.py @@ -76,7 +76,7 @@ def create_function_optimizer( def run(self) -> None: ph("cli-optimize-run-start") - logger.info("Fixing Running optimizer.") + logger.info("Running optimizer.") console.rule() if not env_utils.ensure_codeflash_api_key(): return @@ -97,7 +97,7 @@ def run(self) -> None: ) function_benchmark_timings: dict[str, dict[BenchmarkKey, int]] = {} total_benchmark_timings: dict[BenchmarkKey, int] = {} - if self.args.benchmark: + if self.args.benchmark and num_optimizable_functions > 0: with progress_bar( f"Running benchmarks in {self.args.benchmarks_root}", transient=True, From 16b06766994f7f021cafc126dc1e582be5945df5 Mon Sep 17 00:00:00 2001 From: Alvin Ryanputra Date: Fri, 18 Apr 2025 14:56:21 -0400 Subject: [PATCH 11/13] fixed env for subprocess --- codeflash/benchmarking/trace_benchmarks.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/codeflash/benchmarking/trace_benchmarks.py b/codeflash/benchmarking/trace_benchmarks.py index 8f68030cb..8d14068e7 100644 --- a/codeflash/benchmarking/trace_benchmarks.py +++ b/codeflash/benchmarking/trace_benchmarks.py @@ -1,5 +1,6 @@ from __future__ import annotations +import os import re import subprocess from pathlib import Path @@ -9,6 +10,11 @@ def trace_benchmarks_pytest(benchmarks_root: Path, tests_root:Path, project_root: Path, trace_file: Path, timeout:int = 300) -> None: + benchmark_env = os.environ.copy() + if "PYTHONPATH" not in benchmark_env: + benchmark_env["PYTHONPATH"] = str(project_root) + else: + benchmark_env["PYTHONPATH"] += os.pathsep + str(project_root) result = subprocess.run( [ SAFE_SYS_EXECUTABLE, @@ -21,7 +27,7 @@ def trace_benchmarks_pytest(benchmarks_root: Path, tests_root:Path, project_root check=False, capture_output=True, text=True, - env={"PYTHONPATH": str(project_root)}, + env=benchmark_env, timeout=timeout, ) if result.returncode != 0: From ecfea1a6bf0083086e155a943b02d5d2053ba2eb Mon Sep 17 00:00:00 2001 From: Alvin Ryanputra Date: Fri, 18 Apr 2025 14:59:18 -0400 Subject: [PATCH 12/13] debug git diff lines --- codeflash/discovery/functions_to_optimize.py | 1 + 1 file changed, 1 insertion(+) diff --git a/codeflash/discovery/functions_to_optimize.py b/codeflash/discovery/functions_to_optimize.py index 8ba2d0332..227abf412 100644 --- a/codeflash/discovery/functions_to_optimize.py +++ b/codeflash/discovery/functions_to_optimize.py @@ -207,6 +207,7 @@ def get_functions_to_optimize( def get_functions_within_git_diff() -> dict[str, list[FunctionToOptimize]]: modified_lines: dict[str, list[int]] = get_git_diff(uncommitted_changes=False) + logger.info(f"modified lines: {modified_lines}") modified_functions: dict[str, list[FunctionToOptimize]] = {} for path_str in modified_lines: path = Path(path_str) From b12e423674e48cf0e7f03b518e3bb4638aedf969 Mon Sep 17 00:00:00 2001 From: Alvin Ryanputra Date: Fri, 18 Apr 2025 15:26:46 -0400 Subject: [PATCH 13/13] removed debuggin stuff --- codeflash/discovery/functions_to_optimize.py | 2 -- codeflash/optimization/optimizer.py | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/codeflash/discovery/functions_to_optimize.py b/codeflash/discovery/functions_to_optimize.py index 227abf412..8adfb4e00 100644 --- a/codeflash/discovery/functions_to_optimize.py +++ b/codeflash/discovery/functions_to_optimize.py @@ -201,13 +201,11 @@ def get_functions_to_optimize( functions, test_cfg.tests_root, ignore_paths, project_root, module_root ) logger.info(f"Found {functions_count} function{'s' if functions_count > 1 else ''} to optimize") - logger.info(f"{filtered_modified_functions}") return filtered_modified_functions, functions_count def get_functions_within_git_diff() -> dict[str, list[FunctionToOptimize]]: modified_lines: dict[str, list[int]] = get_git_diff(uncommitted_changes=False) - logger.info(f"modified lines: {modified_lines}") modified_functions: dict[str, list[FunctionToOptimize]] = {} for path_str in modified_lines: path = Path(path_str) diff --git a/codeflash/optimization/optimizer.py b/codeflash/optimization/optimizer.py index 2f259118c..946e3e822 100644 --- a/codeflash/optimization/optimizer.py +++ b/codeflash/optimization/optimizer.py @@ -83,7 +83,7 @@ def run(self) -> None: function_optimizer = None file_to_funcs_to_optimize: dict[Path, list[FunctionToOptimize]] num_optimizable_functions: int - logger.info(self.args) + # discover functions (file_to_funcs_to_optimize, num_optimizable_functions) = get_functions_to_optimize( optimize_all=self.args.all,