Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
ddf350d
add nuitka to depds
KRRT7 Jan 22, 2026
31cb011
add is_compiled_or_bundled_binary func
KRRT7 Jan 22, 2026
10f272f
fix: use Jedi InterpreterEnvironment for compiled binaries
KRRT7 Jan 22, 2026
c7dae3c
fix: find venv or system Python for compiled binaries
KRRT7 Jan 22, 2026
9c5268b
fix: move helper functions before Compat class
KRRT7 Jan 22, 2026
100345b
fix: use importlib.resources for pytest discovery script in compiled …
KRRT7 Jan 22, 2026
b4771c8
fix: embed pytest discovery script as string constant
KRRT7 Jan 22, 2026
5856841
fix: add comprehensive logging for Jedi operations in compiled binaries
KRRT7 Jan 22, 2026
f25a844
less scary
KRRT7 Jan 22, 2026
001e672
fix: find formatters in venv for compiled binaries
KRRT7 Jan 22, 2026
95781c7
add ty to dev deps
KRRT7 Jan 22, 2026
dc3700d
pyinstaller
KRRT7 Jan 22, 2026
be7b81c
fix: pytest plugin loading in PyInstaller binaries
KRRT7 Jan 22, 2026
d3e2ffc
fix: log subprocess output on test failures
KRRT7 Jan 22, 2026
a0b3f19
remove deps
KRRT7 Jan 22, 2026
7858f6c
formatting
KRRT7 Jan 22, 2026
78acb3e
allow mypy to seed
KRRT7 Jan 22, 2026
fcc3fef
no stubs for this
KRRT7 Jan 22, 2026
ef2295f
refactor compat.py
KRRT7 Jan 22, 2026
1a6b6dd
Merge branch 'main' into compile
KRRT7 Jan 22, 2026
7476a26
Merge branch 'main' into compile
KRRT7 Jan 22, 2026
79f7340
cleanup
KRRT7 Jan 22, 2026
ff40c23
let's test
KRRT7 Jan 22, 2026
90af2a9
let me test
KRRT7 Jan 22, 2026
68b7886
Update build-exe.yml
KRRT7 Jan 22, 2026
80c2596
there should be debugs
KRRT7 Jan 22, 2026
2b0828c
reorder
KRRT7 Jan 22, 2026
0a054a4
add to dev deps
KRRT7 Jan 22, 2026
7b0c8d9
Merge branch 'main' into compile
KRRT7 Jan 23, 2026
3b0db87
Merge branch 'main' into compile
KRRT7 Jan 23, 2026
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
42 changes: 42 additions & 0 deletions .github/workflows/build-exe.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Build Executable

on:
workflow_dispatch:
push:
branches:
- main
- compile
paths:
- 'codeflash/**'
- '.github/workflows/build-exe.yml'

jobs:
build:
runs-on: macos-latest

steps:
- name: Checkout
uses: actions/checkout@v5
with:
fetch-depth: 0

- name: Install uv
uses: astral-sh/setup-uv@v6

- name: Set up Python
run: uv python install 3.12

- name: Install dependencies
run: |
uv sync
uv add pyinstaller

- name: Build executable
run: uv run -m PyInstaller --onefile --name codeflash --copy-metadata readchar --collect-all=line_profiler codeflash/main.py

- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: codeflash-macos
path: dist/codeflash
retention-days: 30
3 changes: 2 additions & 1 deletion .github/workflows/mypy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ jobs:
- name: Install uv
uses: astral-sh/setup-uv@v6
with:
version: "0.5.30"
version: "latest"

- name: sync uv
run: |
uv venv --seed
uv sync


Expand Down
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,8 @@ WARP.MD
.mcp.json
.tessl/
tessl.json

# Nuitka build artifacts
main.bin
main.dist/
main.build/
4 changes: 2 additions & 2 deletions codeflash/benchmarking/trace_benchmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pathlib import Path

from codeflash.cli_cmds.console import logger
from codeflash.code_utils.compat import SAFE_SYS_EXECUTABLE
from codeflash.code_utils.compat import get_safe_sys_executable
from codeflash.code_utils.shell_utils import get_cross_platform_subprocess_run_args


Expand All @@ -23,7 +23,7 @@ def trace_benchmarks_pytest(
)
result = subprocess.run( # noqa: PLW1510
[
SAFE_SYS_EXECUTABLE,
get_safe_sys_executable(),
Path(__file__).parent / "pytest_new_process_trace_benchmarks.py",
benchmarks_root,
tests_root,
Expand Down
8 changes: 4 additions & 4 deletions codeflash/code_utils/checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from rich.prompt import Confirm

from codeflash.cli_cmds.console import console
from codeflash.code_utils.compat import codeflash_temp_dir
from codeflash.code_utils.compat import get_codeflash_temp_dir

if TYPE_CHECKING:
import argparse
Expand All @@ -19,7 +19,7 @@
class CodeflashRunCheckpoint:
def __init__(self, module_root: Path, checkpoint_dir: Path | None = None) -> None:
if checkpoint_dir is None:
checkpoint_dir = codeflash_temp_dir
checkpoint_dir = get_codeflash_temp_dir()
self.module_root = module_root
self.checkpoint_dir = Path(checkpoint_dir)
# Create a unique checkpoint file name
Expand Down Expand Up @@ -141,8 +141,8 @@ def get_all_historical_functions(module_root: Path, checkpoint_dir: Path) -> dic

def ask_should_use_checkpoint_get_functions(args: argparse.Namespace) -> Optional[dict[str, dict[str, str]]]:
previous_checkpoint_functions = None
if args.all and codeflash_temp_dir.is_dir():
previous_checkpoint_functions = get_all_historical_functions(args.module_root, codeflash_temp_dir)
if args.all and get_codeflash_temp_dir().is_dir():
previous_checkpoint_functions = get_all_historical_functions(args.module_root, get_codeflash_temp_dir())
if previous_checkpoint_functions and Confirm.ask(
"Previous Checkpoint detected from an incomplete optimization run, shall I continue the optimization from that point?",
default=True,
Expand Down
47 changes: 33 additions & 14 deletions codeflash/code_utils/code_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1129,18 +1129,30 @@ def find_specific_function_in_file(
Tuple of (line_number, column_offset) if found, None otherwise

"""
script = jedi.Script(code=source_code, path=filepath)
names = script.get_names(all_scopes=True, definitions=True)
for name in names:
if name.type == "function" and name.name == target_function:
# If class name specified, check parent
if target_class:
parent = name.parent()
if parent and parent.name == target_class and parent.type == "class":
return CodePosition(line_no=name.line, col_no=name.column)
else:
# Top-level function match
return CodePosition(line_no=name.line, col_no=name.column)
try:
from codeflash.code_utils.compat import get_jedi_environment

jedi_env = get_jedi_environment()
logger.debug(f"find_specific_function_in_file using Jedi environment: {jedi_env}")

script = jedi.Script(code=source_code, path=filepath, environment=jedi_env)
names = script.get_names(all_scopes=True, definitions=True)
for name in names:
try:
if name.type == "function" and name.name == target_function:
# If class name specified, check parent
if target_class:
parent = name.parent()
if parent and parent.name == target_class and parent.type == "class":
return CodePosition(line_no=name.line, col_no=name.column)
else:
# Top-level function match
return CodePosition(line_no=name.line, col_no=name.column)
except Exception as e:
logger.warning(f"Error accessing name.type in find_specific_function_in_file: {e}")
continue
except Exception as e:
logger.warning(f"Jedi operation failed in find_specific_function_in_file: {e}", exc_info=True)

return None # Function not found

Expand All @@ -1153,7 +1165,14 @@ def get_fn_references_jedi(
source_code, file_path, target_function, target_class
)
try:
script = jedi.Script(code=source_code, path=file_path, project=jedi.Project(path=project_root))
from codeflash.code_utils.compat import get_jedi_environment

jedi_env = get_jedi_environment()
logger.debug(f"get_fn_references_jedi using Jedi environment: {jedi_env}")

script = jedi.Script(
code=source_code, path=file_path, project=jedi.Project(path=project_root), environment=jedi_env
)
# Get references to the function
references = script.get_references(line=function_position.line_no, column=function_position.col_no)
# Collect unique file paths where references are found
Expand All @@ -1169,7 +1188,7 @@ def get_fn_references_jedi(
reference_files.add(ref_path)
return sorted(reference_files)
except Exception as e:
print(f"Error during Jedi analysis: {e}")
logger.warning(f"Error during Jedi analysis in get_fn_references_jedi: {e}", exc_info=True)
return []


Expand Down
143 changes: 116 additions & 27 deletions codeflash/code_utils/compat.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,136 @@
from __future__ import annotations

import os
import shutil
import sys
import tempfile
from functools import lru_cache
from pathlib import Path
from typing import TYPE_CHECKING

from platformdirs import user_config_dir

if TYPE_CHECKING:
codeflash_temp_dir: Path
codeflash_cache_dir: Path
codeflash_cache_db: Path
from jedi.api.environment import InterpreterEnvironment


LF: str = os.linesep
IS_POSIX: bool = os.name != "nt"


@lru_cache(maxsize=1)
def get_safe_sys_executable() -> str:
"""Get a safe Python executable path with forward slashes."""
return Path(_find_python_executable()).as_posix()


@lru_cache(maxsize=1)
def get_codeflash_cache_dir() -> Path:
"""Get the codeflash cache directory, creating it if necessary."""
return Path(user_config_dir(appname="codeflash", appauthor="codeflash-ai", ensure_exists=True))


@lru_cache(maxsize=1)
def get_codeflash_temp_dir() -> Path:
"""Get the codeflash temp directory, creating it if necessary."""
temp_dir = Path(tempfile.gettempdir()) / "codeflash"
temp_dir.mkdir(parents=True, exist_ok=True)
return temp_dir


@lru_cache(maxsize=1)
def get_codeflash_cache_db() -> Path:
"""Get the path to the codeflash cache database."""
return get_codeflash_cache_dir() / "codeflash_cache.db"


@lru_cache(maxsize=1)
def get_jedi_environment() -> InterpreterEnvironment | None:
"""Get the appropriate Jedi environment based on execution context.

Returns InterpreterEnvironment for compiled/bundled binaries to avoid
subprocess spawning issues. Returns None for normal Python execution.
"""
if not is_compiled_or_bundled_binary():
return None

try:
from jedi.api.environment import InterpreterEnvironment

from codeflash.cli_cmds.console import logger

logger.debug("Using Jedi InterpreterEnvironment for compiled/bundled binary")
return InterpreterEnvironment()
except Exception as e:
from codeflash.cli_cmds.console import logger

logger.warning(f"Could not create InterpreterEnvironment: {e}")
return None


@lru_cache(maxsize=1)
def is_compiled_or_bundled_binary() -> bool:
"""Check if running in a compiled/bundled binary."""
if getattr(sys, "frozen", False) or hasattr(sys, "_MEIPASS"):
return True

return "__compiled__" in globals()


def find_executable_in_venv(exe_names: list[str]) -> str | None:
"""Find an executable in venv directories.

Searches for venv in current directory and parent directories.
Returns the first matching executable found, or None if not found.

Args:
exe_names: List of possible executable names (e.g., ["python3", "python"])

Returns:
Path to executable if found, None otherwise

"""
if not is_compiled_or_bundled_binary():
return None

class Compat:
# os-independent newline
LF: str = os.linesep
current_dir = Path.cwd()
venv_names = [".venv", "venv"]

SAFE_SYS_EXECUTABLE: str = Path(sys.executable).as_posix()
# Walk up directory tree looking for venv
for parent in [current_dir, *current_dir.parents]:
for venv_name in venv_names:
venv_dir = parent / venv_name
if venv_dir.is_dir():
bin_dir = venv_dir / ("bin" if os.name != "nt" else "Scripts")
for exe_name in exe_names:
exe_path = bin_dir / exe_name
if exe_path.is_file():
return str(exe_path)

Comment on lines +100 to 109
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚡️Codeflash found 47% (0.47x) speedup for _find_python_executable in codeflash/code_utils/compat.py

⏱️ Runtime : 450 microseconds 305 microseconds (best of 1 runs)

📝 Explanation and details

The optimized code achieves a 47% speedup by replacing pathlib.Path operations with lower-level os.path functions in the find_executable_in_venv function.

Key Optimization

Path Object Creation → String Operations

The original code creates multiple Path objects during directory traversal:

  • parent / venv_name creates a new Path object
  • venv_dir / "bin" creates another Path object
  • bin_dir / exe_name creates yet another Path object
  • Each .is_dir() and .is_file() call involves Path object overhead

The optimized version uses os.path.join(), os.path.isdir(), and os.path.isfile() which operate directly on strings, avoiding the object allocation and method dispatch overhead of Path objects.

Why This Works

Looking at the line profiler data, the hottest lines in the original code are:

  • Path concatenations (parent / venv_name, bin_dir / exe_name): ~34% of total time
  • Directory/file checks (is_dir(), is_file()): ~34% of total time

By eliminating Path object creation in the loop (which runs ~112-159 times based on profiler hits), we reduce memory allocations and method call overhead significantly.

Iteration Strategy Change

The optimization also changes from for parent in [current_dir, *current_dir.parents] (which pre-allocates the entire parent list) to a while True loop with manual parent traversal using os.path.dirname(). This:

  • Avoids creating the full parents list upfront
  • Enables early exit once the venv is found (same behavior, but lighter memory footprint)

Test Case Performance

The optimization shows consistent gains across different scenarios:

  • Simple venv in cwd: 48% faster (50.1μs → 33.8μs)
  • Deep parent traversal: 72% faster (121μs → 70.7μs) - especially beneficial when venv is in ancestor directories
  • Fallback scenarios: 37-44% faster even when no venv is found

Impact on Workloads

Based on function_references, this function is called by get_safe_sys_executable(), which appears to be used during initialization or when determining the Python executable to use. While not in a tight loop, the ~47% speedup reduces startup/initialization overhead, particularly beneficial when:

  • Running in compiled/bundled binary mode where venv discovery happens
  • Deep directory structures require many parent traversals (test shows 72% speedup for deep hierarchies)

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 200 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
🌀 Click to see Generated Regression Tests
import os  # used to inspect/create venv-like dirs and to restore os.name if changed
import shutil  # used and monkeypatched for system python lookup behavior
import stat  # for setting executable bit if desired (not required by function)
import sys  # used for sys.executable and to simulate frozen attributes

# import the real module that contains the function under test
from codeflash.code_utils import compat

# We will test the real _find_python_executable function from the compat module.
# The function relies on is_compiled_or_bundled_binary (which is cached) and on
# find_executable_in_venv, shutil.which, os.name, and the current working directory.
# Tests will manipulate these dependencies through pytest fixtures (monkeypatch, tmp_path)
# and will ALWAYS call the real function under test (no mocking/stubbing of it).


# Helper to ensure the lru_cache on is_compiled_or_bundled_binary is cleared between tests.
def _clear_compiled_cache():
    # The function is decorated with lru_cache(maxsize=1); clear its cache before changing state.
    compat.is_compiled_or_bundled_binary.cache_clear()


def _ensure_no_compiled_flag(monkeypatch):
    """Ensure the compat module does not report compiled/bundled binary.
    This is done by removing any compiled indicators from runtime:
    - remove sys.frozen if present on compat check
    - remove sys._MEIPASS if present
    - remove __compiled__ from compat module globals if present
    Then clear the cached result.
    """
    # Remove attributes if they exist (monkeypatch will restore them at test end).
    monkeypatch.delattr(sys, "frozen", raising=False)
    monkeypatch.delattr(sys, "_MEIPASS", raising=False)
    # Remove module-level __compiled__ if present
    monkeypatch.delattr(compat, "__compiled__", raising=False)
    _clear_compiled_cache()


def _set_compiled_flag(monkeypatch):
    """Make compat.is_compiled_or_bundled_binary() return True by setting one of the indicators.
    We set sys._MEIPASS (non-raising so that attribute can be created).
    Then clear the cached result.
    """
    monkeypatch.setattr(sys, "_MEIPASS", "1", raising=False)
    _clear_compiled_cache()


def test_not_compiled_returns_sys_executable(monkeypatch, tmp_path):
    # Ensure no compiled indicator exists and cache is cleared.
    _ensure_no_compiled_flag(monkeypatch)

    # Change cwd to a temporary directory that does NOT contain a venv.
    monkeypatch.chdir(tmp_path)

    # Call the function; since not compiled it should immediately return sys.executable.
    codeflash_output = compat._find_python_executable()
    result = codeflash_output  # 4.60μs -> 4.60μs (0.022% faster)


def test_compiled_prefers_venv_in_cwd(monkeypatch, tmp_path):
    # Simulate compiled/bundled binary environment.
    _set_compiled_flag(monkeypatch)

    # Create a .venv directory in the current working directory with a bin/python3 file.
    venv_dir = tmp_path / ".venv" / "bin"
    venv_dir.mkdir(parents=True)
    python3_path = venv_dir / "python3"
    python3_path.write_text("#!/usr/bin/env python3\n")  # content doesn't matter
    # Ensure it's a file (is_file() should be True). Executable permission is not required by function.
    python3_path.chmod(python3_path.stat().st_mode | stat.S_IXUSR)

    # Set current working directory to the temp path where .venv lives.
    monkeypatch.chdir(tmp_path)

    # The function should detect the venv python3 and return its path as a string.
    codeflash_output = compat._find_python_executable()
    result = codeflash_output  # 50.1μs -> 33.8μs (48.1% faster)


def test_compiled_finds_venv_in_parent(monkeypatch, tmp_path):
    # Simulate compiled/bundled binary environment.
    _set_compiled_flag(monkeypatch)

    # Create a parent directory that holds "venv/bin/python3"
    parent = tmp_path / "ancestor"
    venv_dir = parent / "venv" / "bin"
    venv_dir.mkdir(parents=True)
    venv_python = venv_dir / "python3"
    venv_python.write_text("print('hello')")

    # Create a nested child path and set cwd to it so parent traversal is required.
    child = parent / "child" / "grandchild"
    child.mkdir(parents=True)

    monkeypatch.chdir(child)

    # The function should search up parents and find the venv/python3 in the ancestor.
    codeflash_output = compat._find_python_executable()
    result = codeflash_output  # 121μs -> 70.7μs (72.3% faster)


def test_compiled_venv_with_python_not_python3(monkeypatch, tmp_path):
    # Simulate compiled environment.
    _set_compiled_flag(monkeypatch)

    # Create a venv containing only 'python' (but not 'python3') to test fallback name order.
    venv_dir = tmp_path / "venv" / "bin"
    venv_dir.mkdir(parents=True)
    python_path = venv_dir / "python"
    python_path.write_text("# python fallback executable")

    # Set cwd to the directory containing the venv.
    monkeypatch.chdir(tmp_path)

    # Ensure that the result is the venv's 'python' (since 'python3' is absent but 'python' exists).
    codeflash_output = compat._find_python_executable()
    result = codeflash_output  # 76.0μs -> 55.8μs (36.1% faster)


def test_compiled_falls_back_to_shutil_which_sequence(monkeypatch, tmp_path):
    # Simulate compiled environment.
    _set_compiled_flag(monkeypatch)

    # Ensure no venv exists in cwd or parents.
    monkeypatch.chdir(tmp_path)

    # Monkeypatch shutil.which to behave differently for "python3" vs "python".
    def which_mock(name):
        # Simulate that "python3" is not present but "python" is present.
        if name == "python3":
            return None
        if name == "python":
            return "/usr/bin/fake-python"
        return None

    monkeypatch.setattr(shutil, "which", which_mock)

    # The function should call find_executable_in_venv (none found), then call shutil.which
    # first for "python3" (None) and then for "python" and return that value.
    codeflash_output = compat._find_python_executable()
    result = codeflash_output  # 101μs -> 70.8μs (44.1% faster)


def test_compiled_falls_back_to_sys_executable_when_no_system_python(monkeypatch, tmp_path):
    # Simulate compiled environment.
    _set_compiled_flag(monkeypatch)

    # No venv found (cwd is empty) and shutil.which will return None for all checks.
    monkeypatch.chdir(tmp_path)
    monkeypatch.setattr(shutil, "which", lambda name: None)

    # With no venv and no system python detected by shutil.which, function returns sys.executable.
    codeflash_output = compat._find_python_executable()
    result = codeflash_output  # 95.7μs -> 69.6μs (37.5% faster)


def test_windows_named_executable_and_scripts_dir(monkeypatch, tmp_path):
    # Simulate Windows by patching os.name to 'nt' for this test. Use raising=False to allow change.
    monkeypatch.setattr(os, "name", "nt", raising=False)

    # Simulate compiled environment.
    _set_compiled_flag(monkeypatch)

    # On Windows, the function looks for venv/Scripts/python.exe
    venv_scripts = tmp_path / "venv" / "Scripts"
    venv_scripts.mkdir(parents=True)
    win_python = venv_scripts / "python.exe"
    win_python.write_text("windows python exe content")
    # Mark as executable (not required by function, but realistic).
    win_python.chmod(win_python.stat().st_mode | stat.S_IXUSR)

    # Change cwd to tmp_path so the venv is visible.
    monkeypatch.chdir(tmp_path)

    # The function should detect the windows-style python executable and return it.
    codeflash_output = compat._find_python_executable()
    result = codeflash_output


def test_deep_parent_search_large_scale(monkeypatch, tmp_path):
    # Large scale scenario: create a deep directory tree and place venv near top to ensure traversal scales.
    # Depth chosen to be large but under 1000 as requested (we pick 150).
    depth = 150
    base = tmp_path
    current = base
    # Build a deep nested path
    for i in range(depth):
        current = current / f"level_{i}"
        current.mkdir()

    # Place venv in a higher-level ancestor (not immediate parent) to ensure many parent iterations.
    # Choose ancestor at level 10 so traversal will ascend many steps.
    ancestor = (
        base
        / "level_0"
        / "level_1"
        / "level_2"
        / "level_3"
        / "level_4"
        / "level_5"
        / "level_6"
        / "level_7"
        / "level_8"
        / "level_9"
    )
    venv_bin = ancestor / "venv" / "bin"
    venv_bin.mkdir(parents=True)
    deep_venv_python = venv_bin / "python3"
    deep_venv_python.write_text("deep venv python3")

    # Simulate compiled environment and set cwd to the deepest nested path.
    _set_compiled_flag(monkeypatch)
    monkeypatch.chdir(current)

    # The function should walk up the directory tree and find the venv placed in the ancestor.
    codeflash_output = compat._find_python_executable()
    result = codeflash_output


# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
from unittest.mock import patch

from codeflash.code_utils.compat import _find_python_executable


class TestBasicFunctionality:
    """Basic test cases for _find_python_executable under normal conditions."""

    def test_returns_string(self):
        """Verify that _find_python_executable returns a string."""
        codeflash_output = _find_python_executable()
        result = codeflash_output

    def test_returns_non_empty_string(self):
        """Verify that _find_python_executable returns a non-empty string."""
        codeflash_output = _find_python_executable()
        result = codeflash_output

    def test_normal_execution_returns_sys_executable(self):
        """When not compiled/bundled, should return sys.executable."""
        with patch("codeflash.code_utils.compat.is_compiled_or_bundled_binary", return_value=False):
            codeflash_output = _find_python_executable()
            result = codeflash_output

    def test_returns_path_like_string(self):
        """Verify that the returned string looks like a valid file path."""
        codeflash_output = _find_python_executable()
        result = codeflash_output

    def test_executable_contains_python_reference(self):
        """Verify that returned executable name references Python."""
        codeflash_output = _find_python_executable()
        result = codeflash_output
        result_lower = result.lower()


class TestEdgeCases:
    """Edge case test cases for _find_python_executable."""

    def test_when_sys_executable_is_sys_executable(self):
        """Verify behavior when sys.executable points to current interpreter."""
        codeflash_output = _find_python_executable()
        original_result = codeflash_output
        # Under normal conditions (not compiled), should match sys.executable
        with patch("codeflash.code_utils.compat.is_compiled_or_bundled_binary", return_value=False):
            codeflash_output = _find_python_executable()
            result = codeflash_output

    def test_compiled_binary_no_venv_no_system_python(self):
        """When compiled, no venv found, no system python found."""
        with patch("codeflash.code_utils.compat.is_compiled_or_bundled_binary", return_value=True):
            with patch("codeflash.code_utils.compat.find_executable_in_venv", return_value=None):
                with patch("shutil.which", return_value=None):
                    codeflash_output = _find_python_executable()
                    result = codeflash_output

    def test_compiled_binary_venv_found(self):
        """When compiled and venv python is found."""
        expected_venv_python = "/path/to/venv/bin/python3"
        with patch("codeflash.code_utils.compat.is_compiled_or_bundled_binary", return_value=True):
            with patch("codeflash.code_utils.compat.find_executable_in_venv", return_value=expected_venv_python):
                codeflash_output = _find_python_executable()
                result = codeflash_output

    def test_compiled_binary_venv_not_found_system_found(self):
        """When compiled, venv not found, system python found."""
        expected_system_python = "/usr/bin/python3"
        with patch("codeflash.code_utils.compat.is_compiled_or_bundled_binary", return_value=True):
            with patch("codeflash.code_utils.compat.find_executable_in_venv", return_value=None):
                with patch("shutil.which", return_value=expected_system_python):
                    codeflash_output = _find_python_executable()
                    result = codeflash_output

    def test_windows_python_executable(self):
        """Test behavior on Windows-like paths."""
        windows_python = "C:\\Python39\\python.exe"
        with patch("codeflash.code_utils.compat.is_compiled_or_bundled_binary", return_value=True):
            with patch("codeflash.code_utils.compat.find_executable_in_venv", return_value=windows_python):
                codeflash_output = _find_python_executable()
                result = codeflash_output

    def test_relative_path_python(self):
        """Test behavior with relative paths."""
        relative_python = "./venv/bin/python"
        with patch("codeflash.code_utils.compat.is_compiled_or_bundled_binary", return_value=True):
            with patch("codeflash.code_utils.compat.find_executable_in_venv", return_value=relative_python):
                codeflash_output = _find_python_executable()
                result = codeflash_output

    def test_venv_priority_over_system(self):
        """Verify venv python is prioritized over system python."""
        venv_python = "/home/user/.venv/bin/python"
        system_python = "/usr/bin/python3"

        with patch("codeflash.code_utils.compat.is_compiled_or_bundled_binary", return_value=True):
            with patch("codeflash.code_utils.compat.find_executable_in_venv", return_value=venv_python):
                with patch("shutil.which", return_value=system_python):
                    codeflash_output = _find_python_executable()
                    result = codeflash_output

    def test_empty_string_not_returned(self):
        """Verify that empty string is never returned."""
        codeflash_output = _find_python_executable()
        result = codeflash_output

    def test_none_not_returned(self):
        """Verify that None is never returned."""
        codeflash_output = _find_python_executable()
        result = codeflash_output

    def test_multiple_calls_consistent(self):
        """Verify that multiple calls return consistent results."""
        # Note: This may fail if system state changes, but should be consistent in unit test
        with patch("codeflash.code_utils.compat.is_compiled_or_bundled_binary", return_value=False):
            codeflash_output = _find_python_executable()
            result1 = codeflash_output
            codeflash_output = _find_python_executable()
            result2 = codeflash_output

    def test_python3_preferred_on_unix(self):
        """On Unix-like systems, python3 should be checked before python."""
        # This tests the order of names tried
        python3_path = "/usr/bin/python3"
        python_path = "/usr/bin/python"

        with patch("codeflash.code_utils.compat.is_compiled_or_bundled_binary", return_value=True):
            with patch("codeflash.code_utils.compat.find_executable_in_venv", return_value=None):
                # Simulate which() being called multiple times
                call_count = [0]

                def which_side_effect(name):
                    call_count[0] += 1
                    if name == "python3":
                        return python3_path
                    if name == "python":
                        return python_path
                    return None

                with patch("shutil.which", side_effect=which_side_effect), patch("os.name", "posix"):
                    codeflash_output = _find_python_executable()
                    result = codeflash_output


class TestLargeScale:
    """Large scale test cases for performance and scalability."""

    def test_repeated_calls_performance(self):
        """Test that repeated calls maintain performance (no degradation)."""
        import time

        with patch("codeflash.code_utils.compat.is_compiled_or_bundled_binary", return_value=False):
            # First call
            start_time = time.perf_counter()
            for _ in range(100):
                _find_python_executable()
            elapsed_time = time.perf_counter() - start_time

    def test_compiled_binary_multiple_calls_consistency(self):
        """Test that compiled binary mode works consistently across multiple calls."""
        with patch("codeflash.code_utils.compat.is_compiled_or_bundled_binary", return_value=True):
            with patch("codeflash.code_utils.compat.find_executable_in_venv", return_value="/venv/python"):
                results = []
                for _ in range(50):
                    codeflash_output = _find_python_executable()
                    result = codeflash_output
                    results.append(result)

    def test_large_number_of_different_paths(self):
        """Test behavior when system has many possible paths."""
        # Simulate finding different executables in rotation
        paths = [f"/usr/bin/python{i}" for i in range(100)]

        with patch("codeflash.code_utils.compat.is_compiled_or_bundled_binary", return_value=True):
            with patch("codeflash.code_utils.compat.find_executable_in_venv", return_value=None):
                for path in paths[:10]:  # Test with first 10 paths
                    with patch("shutil.which", return_value=path):
                        codeflash_output = _find_python_executable()
                        result = codeflash_output

    def test_path_string_length_handling(self):
        """Test handling of very long path strings."""
        long_path = "/very/long/path/" + "a" * 500 + "/python"

        with patch("codeflash.code_utils.compat.is_compiled_or_bundled_binary", return_value=True):
            with patch("codeflash.code_utils.compat.find_executable_in_venv", return_value=long_path):
                codeflash_output = _find_python_executable()
                result = codeflash_output

    def test_unicode_in_paths(self):
        """Test handling of Unicode characters in paths."""
        unicode_path = "/home/user/ПрОвЕрКа/python"

        with patch("codeflash.code_utils.compat.is_compiled_or_bundled_binary", return_value=True):
            with patch("codeflash.code_utils.compat.find_executable_in_venv", return_value=unicode_path):
                codeflash_output = _find_python_executable()
                result = codeflash_output

    def test_special_characters_in_paths(self):
        """Test handling of special characters in paths."""
        special_paths = [
            "/path/with spaces/python",
            "/path/with-dashes/python",
            "/path/with_underscores/python",
            "/path/with.dots/python",
        ]

        with patch("codeflash.code_utils.compat.is_compiled_or_bundled_binary", return_value=True):
            for special_path in special_paths:
                with patch("codeflash.code_utils.compat.find_executable_in_venv", return_value=special_path):
                    codeflash_output = _find_python_executable()
                    result = codeflash_output

    def test_sequential_fallback_logic(self):
        """Test that fallback logic is applied sequentially."""
        venv_python = "/venv/python"
        system_python = "/usr/bin/python"

        with patch("codeflash.code_utils.compat.is_compiled_or_bundled_binary", return_value=True):
            # Case 1: venv found first
            with patch("codeflash.code_utils.compat.find_executable_in_venv", return_value=venv_python):
                with patch("shutil.which", return_value=system_python):
                    codeflash_output = _find_python_executable()
                    result = codeflash_output

            # Case 2: venv not found, system found
            with patch("codeflash.code_utils.compat.find_executable_in_venv", return_value=None):
                with patch("shutil.which", return_value=system_python):
                    codeflash_output = _find_python_executable()
                    result = codeflash_output

            # Case 3: neither found
            with patch("codeflash.code_utils.compat.find_executable_in_venv", return_value=None):
                with patch("shutil.which", return_value=None):
                    codeflash_output = _find_python_executable()
                    result = codeflash_output


class TestIntegration:
    """Integration tests combining multiple aspects."""

    def test_actual_sys_executable_fallback_works(self):
        """Verify actual sys.executable is always a valid fallback."""
        # This is a real test without mocks
        codeflash_output = _find_python_executable()
        result = codeflash_output
        # When not compiled, should return sys.executable
        with patch("codeflash.code_utils.compat.is_compiled_or_bundled_binary", return_value=False):
            codeflash_output = _find_python_executable()
            result = codeflash_output

    def test_result_contains_executable_name(self):
        """Verify result contains some form of Python executable reference."""
        codeflash_output = _find_python_executable()
        result = codeflash_output
        result_lower = result.lower()

    def test_callable_without_arguments(self):
        """Verify function can be called without any arguments."""
        # Should not raise any exception
        codeflash_output = _find_python_executable()
        result = codeflash_output

    def test_return_type_consistency(self):
        """Verify return type is always string."""
        with patch("codeflash.code_utils.compat.is_compiled_or_bundled_binary", return_value=True):
            with patch("codeflash.code_utils.compat.find_executable_in_venv", return_value="/venv/python"):
                codeflash_output = _find_python_executable()
                result1 = codeflash_output

        with patch("codeflash.code_utils.compat.is_compiled_or_bundled_binary", return_value=False):
            codeflash_output = _find_python_executable()
            result2 = codeflash_output
from codeflash.code_utils.compat import _find_python_executable


def test__find_python_executable():
    _find_python_executable()

To test or edit this optimization locally git merge codeflash/optimize-pr1140-2026-01-22T23.56.38

Click to see suggested changes
Suggested change
for parent in [current_dir, *current_dir.parents]:
for venv_name in venv_names:
venv_dir = parent / venv_name
if venv_dir.is_dir():
bin_dir = venv_dir / ("bin" if os.name != "nt" else "Scripts")
for exe_name in exe_names:
exe_path = bin_dir / exe_name
if exe_path.is_file():
return str(exe_path)
parent_path = os.fspath(current_dir)
while True:
for venv_name in venv_names:
venv_dir = os.path.join(parent_path, venv_name)
if os.path.isdir(venv_dir):
bin_dir = os.path.join(venv_dir, ("bin" if os.name != "nt" else "Scripts"))
for exe_name in exe_names:
exe_path = os.path.join(bin_dir, exe_name)
if os.path.isfile(exe_path):
return exe_path
new_parent = os.path.dirname(parent_path)
if new_parent == parent_path:
break
parent_path = new_parent

Static Badge

IS_POSIX: bool = os.name != "nt"
return None

@property
def codeflash_cache_dir(self) -> Path:
return Path(user_config_dir(appname="codeflash", appauthor="codeflash-ai", ensure_exists=True))

@property
def codeflash_temp_dir(self) -> Path:
temp_dir = Path(tempfile.gettempdir()) / "codeflash"
if not temp_dir.exists():
temp_dir.mkdir(parents=True, exist_ok=True)
return temp_dir
def _find_python_executable() -> str:
"""Find the appropriate Python executable.

@property
def codeflash_cache_db(self) -> Path:
return self.codeflash_cache_dir / "codeflash_cache.db"
For compiled binaries, searches for venv in cwd/parent dirs, then falls back to system Python.
For normal execution, returns sys.executable.
"""
if not is_compiled_or_bundled_binary():
return sys.executable

python_names = ["python3", "python"] if os.name != "nt" else ["python.exe"]

_compat = Compat()
# Try venv first
venv_python = find_executable_in_venv(python_names)
if venv_python:
return venv_python

# Fall back to system Python
for python_name in python_names:
system_python = shutil.which(python_name)
if system_python:
return system_python

codeflash_temp_dir = _compat.codeflash_temp_dir
codeflash_cache_dir = _compat.codeflash_cache_dir
codeflash_cache_db = _compat.codeflash_cache_db
LF = _compat.LF
SAFE_SYS_EXECUTABLE = _compat.SAFE_SYS_EXECUTABLE
IS_POSIX = _compat.IS_POSIX
# Last resort: return sys.executable (even though it may not work)
return sys.executable
6 changes: 3 additions & 3 deletions codeflash/code_utils/concolic_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import sentry_sdk

from codeflash.code_utils.compat import SAFE_SYS_EXECUTABLE, codeflash_temp_dir
from codeflash.code_utils.compat import get_codeflash_temp_dir, get_safe_sys_executable

# Known CrossHair limitations that produce invalid Python syntax in generated tests:
# - "<locals>" - higher-order functions returning nested functions
Expand All @@ -26,12 +26,12 @@ def is_valid_concolic_test(test_code: str, project_root: Optional[str] = None) -
sentry_sdk.capture_message(f"CrossHair generated test with syntax error:\n{test_code}")
return False

temp_path = (codeflash_temp_dir / f"concolic_test_{uuid.uuid4().hex}.py").resolve()
temp_path = (get_codeflash_temp_dir() / f"concolic_test_{uuid.uuid4().hex}.py").resolve()
temp_path.write_text(test_code, encoding="utf-8")

try:
result = subprocess.run(
[SAFE_SYS_EXECUTABLE, "-m", "pytest", "--collect-only", "-q", temp_path.as_posix()],
[get_safe_sys_executable(), "-m", "pytest", "--collect-only", "-q", temp_path.as_posix()],
check=False,
capture_output=True,
text=True,
Expand Down
7 changes: 6 additions & 1 deletion codeflash/code_utils/env_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from codeflash.cli_cmds.console import logger
from codeflash.code_utils.code_utils import exit_with_message
from codeflash.code_utils.compat import find_executable_in_venv
from codeflash.code_utils.formatter import format_code
from codeflash.code_utils.shell_utils import read_api_key_from_shell_config, save_api_key_to_rc
from codeflash.lsp.helpers import is_LSP_enabled
Expand All @@ -28,7 +29,11 @@ def check_formatter_installed(formatter_cmds: list[str], exit_on_failure: bool =
exe_name = cmd_tokens[0]
command_str = " ".join(formatter_cmds).replace(" $file", "")

if shutil.which(exe_name) is None:
formatter_path = find_executable_in_venv([exe_name])
if formatter_path is None:
formatter_path = shutil.which(exe_name)

if formatter_path is None:
logger.error(
f"Could not find formatter: {command_str}\n"
f"Please install it or update 'formatter-cmds' in your codeflash configuration"
Expand Down
Loading
Loading