Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
100 commits
Select commit Hold shift + click to select a range
ce79864
move optimizer cleanup into server and run optimization in thread
mohammedahmed18 Aug 22, 2025
0afb67c
for safety
mohammedahmed18 Aug 22, 2025
85ccaaa
Merge branch 'main' into lsp/threaded-optimizer-cleanup
KRRT7 Aug 22, 2025
e7de51a
prevent duplicate global assignments when reverting helpers
mohammedahmed18 Aug 23, 2025
117e682
test: simplify
mohammedahmed18 Aug 23, 2025
c5fbe09
Merge branch 'main' into lsp/threaded-optimizer-cleanup
mohammedahmed18 Aug 24, 2025
9c8256a
prevent duplicates for new global statements
mohammedahmed18 Aug 24, 2025
674e69e
Merge pull request #676 from codeflash-ai/lsp/threaded-optimizer-cleanup
mohammedahmed18 Aug 25, 2025
55cb7f4
send the file paths with the functions in the current diff
mohammedahmed18 Aug 25, 2025
28f50cc
Merge branch 'main' of github.com:codeflash-ai/codeflash into fix/dup…
mohammedahmed18 Aug 25, 2025
8107bce
⚡️ Speed up function `add_global_assignments` by 18% in PR #683 (`fix…
codeflash-ai[bot] Aug 25, 2025
9c05130
Merge branch 'main' into standalone-fto-async
KRRT7 Aug 25, 2025
b18d2c9
better name
mohammedahmed18 Aug 26, 2025
90ebac3
revert comment
mohammedahmed18 Aug 26, 2025
936fa0a
wip
misrasaurabh1 Aug 28, 2025
0e7834e
Merge branch 'main' into parallel-pytest-tracing
aseembits93 Aug 28, 2025
78519ee
bugfix
aseembits93 Aug 29, 2025
6446662
cleanup
mohammedahmed18 Aug 30, 2025
c1f75ad
Merge pull request #686 from codeflash-ai/codeflash/optimize-pr683-20…
mohammedahmed18 Aug 30, 2025
fdaf6c0
Merge pull request #688 from codeflash-ai/lsp/send-filenames-with-fun…
mohammedahmed18 Sep 1, 2025
95a149b
Merge branch 'main' into standalone-fto-async
KRRT7 Sep 2, 2025
a59b9ed
[LSP] Get new/modified functions inside a git commit (#694)
mohammedahmed18 Sep 2, 2025
2a1096b
[Worktree] Persist optimization patches metadata (#690)
mohammedahmed18 Sep 2, 2025
d4788b9
debug measure time
aseembits93 Sep 2, 2025
48e367e
should work, will write some tests
aseembits93 Sep 3, 2025
d0195a9
basic tests
aseembits93 Sep 3, 2025
b7a52bc
tests for all collections objects
aseembits93 Sep 3, 2025
8753e54
Merge pull request #705 from codeflash-ai/deque-comparator
aseembits93 Sep 3, 2025
0dc325a
sets instead of lists
aseembits93 Sep 3, 2025
97f658c
fix: regex for api key shell export
mohammedahmed18 Sep 4, 2025
02ae60b
Merge pull request #715 from codeflash-ai/fix/allow-no-quotes-in-api-…
mohammedahmed18 Sep 4, 2025
b205516
ranker wip
aseembits93 Sep 5, 2025
8c36180
todo logging message, db logging
aseembits93 Sep 5, 2025
3cb5a2d
Merge remote-tracking branch 'origin/main' into ranker
aseembits93 Sep 5, 2025
9238fe5
int to float
aseembits93 Sep 5, 2025
bf066a9
minor bugfix
aseembits93 Sep 5, 2025
060a78c
Merge branch 'main' into parallel-pytest-tracing
aseembits93 Sep 6, 2025
dec3c1a
bugfix
aseembits93 Sep 6, 2025
0fe5e5f
Merge branch 'main' into fix/duplicate-global-assignments-when-revert…
mohammedahmed18 Sep 6, 2025
f085fe1
Merge pull request #683 from codeflash-ai/fix/duplicate-global-assign…
mohammedahmed18 Sep 7, 2025
a1ecf06
use the git root dir when raising the PR
mohammedahmed18 Sep 9, 2025
e55a543
fix for non-git repo
mohammedahmed18 Sep 9, 2025
8ce8b7a
filelock should be installed when not using `uv sync`
aseembits93 Sep 10, 2025
95b6bb6
update uv lock file
aseembits93 Sep 10, 2025
922f714
Merge pull request #723 from codeflash-ai/fix/project-root-path
misrasaurabh1 Sep 11, 2025
aab93ef
Merge branch 'main' into parallel-pytest-tracing
misrasaurabh1 Sep 11, 2025
258b523
Merge pull request #693 from codeflash-ai/parallel-pytest-tracing
misrasaurabh1 Sep 11, 2025
2225f58
Merge pull request #726 from codeflash-ai/filelock-install
misrasaurabh1 Sep 11, 2025
b902d62
release/v0.16.7
misrasaurabh1 Sep 11, 2025
774b340
pre-commit
KRRT7 Sep 11, 2025
3dcf7a3
Merge pull request #727 from codeflash-ai/release/v0.16.7
misrasaurabh1 Sep 11, 2025
4bcd81c
Warning Message for users using old versions (#701)
Saga4 Sep 11, 2025
95114bd
Merge branch 'main' into ranker
aseembits93 Sep 11, 2025
6752e0f
precommit fix
aseembits93 Sep 11, 2025
3ba531e
fix tests
aseembits93 Sep 11, 2025
8de932e
code review changes
aseembits93 Sep 12, 2025
bdd1461
potential bug
aseembits93 Sep 12, 2025
2802ae6
Merge pull request #717 from codeflash-ai/ranker
misrasaurabh1 Sep 12, 2025
95a38d3
deduplicate optimizations better
misrasaurabh1 Sep 13, 2025
a831ee3
fix a bug
misrasaurabh1 Sep 13, 2025
47f4d76
Update codeflash/code_utils/deduplicate_code.py
misrasaurabh1 Sep 13, 2025
6754b1f
optimize performance
misrasaurabh1 Sep 14, 2025
2792c67
Merge remote-tracking branch 'origin/deduplicate-better' into dedupli…
misrasaurabh1 Sep 14, 2025
9ac5d34
Merge pull request #733 from codeflash-ai/deduplicate-better
misrasaurabh1 Sep 14, 2025
1a5f103
fix overlappings args in codeflash wrap
aseembits93 Sep 16, 2025
5891dfa
linting fixes
aseembits93 Sep 16, 2025
978924d
Apply suggestion from @codeflash-ai[bot]
KRRT7 Sep 16, 2025
161f8cf
lint fix
aseembits93 Sep 16, 2025
b6d57ee
Merge pull request #737 from codeflash-ai/fix-overlapping-args-codefl…
aseembits93 Sep 18, 2025
87b86c9
rename warp.md to agents.md and improve it
KRRT7 Sep 18, 2025
9fdf45f
Merge pull request #741 from codeflash-ai/migrate-to-agents.md
KRRT7 Sep 18, 2025
48e88b7
Add env variable to shell config if present only in env variable (#740)
Saga4 Sep 18, 2025
b19c0b3
Ensure cleanup after every candidate testing
mohammedahmed18 Sep 18, 2025
4856cce
Merge pull request #742 from codeflash-ai/fix/reset-code-after-every-…
mohammedahmed18 Sep 18, 2025
d3e6427
[LSP] stderr verbose logs for the thought process (#718)
mohammedahmed18 Sep 18, 2025
e84e57a
[LSP] search for the nearest pyproject.toml file in the init feature …
mohammedahmed18 Sep 22, 2025
078b5c0
LSP reduce no of candidates (#729)
Saga4 Sep 22, 2025
cdd16bb
[fix] candidate inline and remove functions for config (#746)
Saga4 Sep 23, 2025
51ceda7
Update test_runner.py
KRRT7 Sep 23, 2025
5a425c3
Update test_runner.py
KRRT7 Sep 23, 2025
4c8b76f
Update test_runner.py
KRRT7 Sep 23, 2025
443cb4d
Merge pull request #747 from codeflash-ai/fix_looping_issue
KRRT7 Sep 23, 2025
32e85ee
release/v0.17.0 (#756)
misrasaurabh1 Sep 23, 2025
bebe856
attrs
KRRT7 Sep 24, 2025
febe0ec
remove unused import
KRRT7 Sep 24, 2025
5dffd2a
Merge pull request #757 from codeflash-ai/attrs-support
KRRT7 Sep 24, 2025
647531f
Update tracing_new_process.py
KRRT7 Sep 25, 2025
444ff12
code review
KRRT7 Sep 25, 2025
36e3868
linter
KRRT7 Sep 25, 2025
f296a0f
Merge pull request #761 from codeflash-ai/prevent-deadlocks-pytest
KRRT7 Sep 25, 2025
9d57cea
respect parents when searching for function in revert helpers
mohammedahmed18 Sep 25, 2025
27419be
new line
mohammedahmed18 Sep 25, 2025
5cd9024
async function
mohammedahmed18 Sep 25, 2025
1feedb6
Optimize find_target_node
codeflash-ai[bot] Sep 25, 2025
76123d0
Merge pull request #764 from codeflash-ai/codeflash/optimize-pr763-20…
mohammedahmed18 Sep 25, 2025
2d886e8
Merge pull request #763 from codeflash-ai/fix/correctly-find-funtion-…
mohammedahmed18 Sep 25, 2025
1e103bd
Merge pull request #687 from codeflash-ai/granular-async-instrumentation
KRRT7 Sep 26, 2025
f5c98e5
Resolve merge conflicts: consolidate async tests into single block an…
KRRT7 Sep 26, 2025
40c4108
oopsie mergie
KRRT7 Sep 26, 2025
4c4feef
Optimize CommentMapper.visit_FunctionDef
codeflash-ai[bot] Sep 26, 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,4 @@ fabric.properties

# Mac
.DS_Store
WARP.MD
318 changes: 318 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,318 @@
# CodeFlash AI Agent Instructions

This file provides comprehensive guidance to any coding agent (Warp, GitHub Copilot, Claude, Gemini, etc.) when working with the CodeFlash repository.

## Project Overview

CodeFlash is an AI-powered Python code optimizer that automatically improves code performance while maintaining correctness. It uses LLMs to analyze code, generate optimization ideas, validate correctness through comprehensive testing, benchmark performance improvements, and create merge-ready pull requests.

**Key Capabilities:**
- Optimize entire codebases with `codeflash --all`
- Optimize specific files or functions with targeted commands
- End-to-end workflow optimization with `codeflash optimize script.py`
- Automated GitHub Actions integration for CI/CD pipelines
- Comprehensive benchmarking and performance analysis
- Git worktree isolation for safe optimization

## Core Architecture

### Data Flow Pipeline
Discovery → Context → Optimization → Verification → Benchmarking → PR

1. **Discovery** (`codeflash/discovery/`) - Find optimizable functions via static analysis or execution tracing
2. **Context Extraction** (`codeflash/context/`) - Extract dependencies, imports, and related code
3. **Optimization** (`codeflash/optimization/`) - Generate optimized code via AI service calls
4. **Verification** (`codeflash/verification/`) - Run deterministic tests with custom pytest plugin
5. **Benchmarking** (`codeflash/benchmarking/`) - Performance measurement and comparison
6. **GitHub Integration** (`codeflash/github/`) - Automated PR creation with detailed analysis

### Key Components

**Main Entry Points:**
- `codeflash/main.py` - CLI entry point and main orchestration
- `codeflash/cli_cmds/cli.py` - Command-line argument parsing and validation

**Core Optimization Pipeline:**
- `codeflash/optimization/optimizer.py` - Main optimization orchestrator
- `codeflash/optimization/function_optimizer.py` - Individual function optimization
- `codeflash/tracing/` - Function call tracing and profiling

**Code Analysis & Manipulation:**
- `codeflash/code_utils/` - Code parsing, AST manipulation, static analysis
- `codeflash/context/` - Code context extraction and analysis
- `codeflash/verification/` - Code correctness verification through testing

**External Integrations:**
- `codeflash/api/aiservice.py` - LLM communication with rate limiting and retries
- `codeflash/github/` - GitHub integration for PR creation
- `codeflash/benchmarking/` - Performance benchmarking and measurement

**Supporting Systems:**
- `codeflash/models/models.py` - Pydantic models and type definitions
- `codeflash/telemetry/` - Usage analytics (PostHog) and error reporting (Sentry)
- `codeflash/ui/` - User interface components (Rich console output)
- `codeflash/lsp/` - Language Server Protocol support for IDE integration

### Key Optimization Workflows

**1. Full Codebase Optimization (`--all`)**
- Discovers all optimizable functions in the project
- Runs benchmarks if configured
- Optimizes functions in parallel
- Creates PRs for successful optimizations

**2. Targeted Optimization (`--file`, `--function`)**
- Focuses on specific files or functions
- Performs detailed analysis and context extraction
- Applies targeted optimizations

**3. Workflow Tracing (`optimize`)**
- Traces Python script execution
- Identifies performance bottlenecks
- Generates optimizations for traced functions
- Uses checkpoint system to resume interrupted runs

## Critical Development Patterns

### Package Management with uv (NOT pip)
```bash
# Always use uv, never pip
uv sync # Install dependencies
uv sync --group dev # Install dev dependencies
uv run pytest # Run commands
uv add package # Add new packages
uv build # Build package
```

### Code Manipulation with LibCST (NOT ast)
Always use `libcst` for code parsing/modification to preserve formatting:
```python
from libcst import parse_module, PartialPythonCodeGen
# Never use ast module for code transformations
```

### Testing with Deterministic Execution
Custom pytest plugin (`codeflash/verification/pytest_plugin.py`) ensures reproducible tests:
- Patches time, random, uuid for deterministic behavior
- Environment variables: `CODEFLASH_TEST_MODULE`, `CODEFLASH_TEST_CLASS`, `CODEFLASH_TEST_FUNCTION`
- Always use `uv run pytest`, never `python -m pytest`

### Git Worktree Isolation
Optimizations run in isolated git worktrees to avoid affecting main repo:
```python
from codeflash.code_utils.git_utils import create_detached_worktree, remove_worktree
# Pattern: create_detached_worktree() → optimize → create_diff_patch_from_worktree()
```

### Error Handling with Either Pattern
Use functional error handling instead of exceptions:
```python
from codeflash.either import is_successful, Either
result = aiservice_client.call_llm(...)
if is_successful(result):
optimized_code = result.value
else:
error = result.error
```

## Configuration

All configuration in `pyproject.toml` under `[tool.codeflash]`:
```toml
[tool.codeflash]
module-root = "codeflash" # Source code location
tests-root = "tests" # Test directory
benchmarks-root = "tests/benchmarks" # Benchmark tests
test-framework = "pytest" # Always pytest
formatter-cmds = [ # Auto-formatting commands
"uvx ruff check --exit-zero --fix $file",
"uvx ruff format $file",
]
```

## Development Commands

### Environment Setup
```bash
# Install dependencies (always use uv)
uv sync

# Install development dependencies
uv sync --group dev

# Install pre-commit hooks
uv run pre-commit install
```

### Code Quality & Linting
```bash
# Run linting and formatting with ruff (primary tool)
uv run ruff check --fix .
uv run ruff format .

# Type checking with mypy (strict mode)
uv run mypy .

# Clean Python cache files
uvx pyclean .
```

### Testing
```bash
# Run all tests
uv run pytest

# Run tests with coverage
uv run coverage run -m pytest tests/

# Run specific test file
uv run pytest tests/test_code_utils.py

# Run tests with verbose output
uv run pytest -v

# Run benchmarks
uv run pytest tests/benchmarks/

# Run end-to-end tests
uv run pytest tests/scripts/

# Run with specific markers
uv run pytest -m "not ci_skip"
```

### Running CodeFlash
```bash
# Initialize CodeFlash in a project
uv run codeflash init

# Optimize entire codebase
uv run codeflash --all

# Optimize specific file
uv run codeflash --file path/to/file.py

# Optimize specific function
uv run codeflash --file path/to/file.py --function function_name

# Trace and optimize a workflow
uv run codeflash optimize script.py

# Verify setup with test optimization
uv run codeflash --verify-setup

# Run with verbose logging
uv run codeflash --verbose --all

# Run with benchmarking enabled
uv run codeflash --benchmark --file target_file.py

# Use replay tests for debugging
uv run codeflash --replay-test tests/specific_test.py
```

## Development Guidelines

### Code Style
- Uses Ruff for linting and formatting (configured in pyproject.toml)
- Strict mypy type checking enabled
- Pre-commit hooks enforce code quality
- Line length: 120 characters
- Python 3.10+ syntax

### Testing Strategy
- Primary test framework: pytest
- Tests located in `tests/` directory
- End-to-end tests in `tests/scripts/`
- Benchmarks in `tests/benchmarks/`
- Extensive use of `@pytest.mark.parametrize`
- Shared fixtures in conftest.py
- Test isolation via custom pytest plugin

### Key Dependencies
- **Core**: `libcst`, `jedi`, `gitpython`, `pydantic`
- **Testing**: `pytest`, `coverage`, `crosshair-tool`
- **Performance**: `line_profiler`, `timeout-decorator`
- **UI**: `rich`, `inquirer`, `click`
- **AI**: Custom API client for LLM interactions

### Data Models & Types
- `codeflash/models/models.py` - Pydantic models for all data structures
- Extensive use of `@dataclass(frozen=True)` for immutable data
- Core types: `FunctionToOptimize`, `ValidCode`, `BenchmarkKey`

## AI Service Integration

### Rate Limiting & Retries
- Built-in rate limiting and exponential backoff
- Handle `Either` return types for error handling
- AI service endpoint: `codeflash/api/aiservice.py`

### Telemetry & Monitoring
- **Sentry**: Error tracking with `codeflash.telemetry.sentry`
- **PostHog**: Usage analytics with `codeflash.telemetry.posthog_cf`
- **Environment Variables**: `CODEFLASH_EXPERIMENT_ID` for testing modes

## Performance & Benchmarking

### Line Profiler Integration
- Uses `line_profiler` for detailed performance analysis
- Instruments functions with `@profile` decorator
- Generates before/after profiling reports
- Calculates precise speedup measurements

### Benchmark Test Framework
- Custom benchmarking in `tests/benchmarks/`
- Generates replay tests from execution traces
- Validates performance improvements statistically

## Debugging & Development

### Verbose Logging
```bash
uv run codeflash --verbose --file target_file.py
```

### Important Environment Variables
- `CODEFLASH_TEST_MODULE` - Current test module during verification
- `CODEFLASH_TEST_CLASS` - Current test class during verification
- `CODEFLASH_TEST_FUNCTION` - Current test function during verification
- `CODEFLASH_LOOP_INDEX` - Current iteration in pytest loops
- `CODEFLASH_EXPERIMENT_ID` - Enables local AI service for testing

### LSP Integration
Language Server Protocol support in `codeflash/lsp/` enables IDE integration during optimization.

### Common Debugging Patterns
1. Use verbose logging to trace optimization flow
2. Check git worktree operations for isolation issues
3. Verify deterministic test execution with environment variables
4. Use replay tests to debug specific optimization scenarios
5. Monitor AI service calls with rate limiting logs

## Best Practices

### Path Handling
- Always use absolute paths
- Handle encoding explicitly (UTF-8)
- Extensive path validation and cleanup utilities in `codeflash/code_utils/`

### Git Operations
- All optimizations run in isolated worktrees
- Never modify the main repository directly
- Use git utilities in `codeflash/code_utils/git_utils.py`

### Code Transformations
- Always use libcst, never ast module
- Preserve code formatting and comments
- Validate transformations with deterministic tests

### Error Handling
- Use Either pattern for functional error handling
- Log errors to Sentry for monitoring
- Provide clear user feedback via Rich console

### Performance Optimization
- Profile before and after changes
- Use benchmarks to validate improvements
- Generate detailed performance reports
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ How to use Codeflash -
- Automate optimizing all __future__ code you will write by installing Codeflash as a GitHub action.
- Optimize a Python workflow `python myscript.py` end-to-end by running `codeflash optimize myscript.py`

Codeflash is used by top engineering teams at [Pydantic](https://github.com/pydantic/pydantic/pulls?q=is%3Apr+author%3Amisrasaurabh1+is%3Amerged), [Langflow](https://github.com/langflow-ai/langflow/issues?q=state%3Aclosed%20is%3Apr%20author%3Amisrasaurabh1), [Roboflow](https://github.com/roboflow/inference/pulls?q=is%3Apr+is%3Amerged+codeflash+sort%3Acreated-asc), [Albumentations](https://github.com/albumentations-team/albumentations/issues?q=state%3Amerged%20is%3Apr%20author%3Akrrt7%20OR%20state%3Amerged%20is%3Apr%20author%3Aaseembits93%20) and many others to ship performant, expert level code.
Codeflash is used by top engineering teams at **Pydantic** [(PRs Merged)](https://github.com/pydantic/pydantic/pulls?q=is%3Apr+author%3Amisrasaurabh1+is%3Amerged), **Roboflow** [(PRs Merged 1](https://github.com/roboflow/inference/issues?q=state%3Aclosed%20is%3Apr%20author%3Amisrasaurabh1%20is%3Amerged), [PRs Merged 2)](https://github.com/roboflow/inference/issues?q=state%3Amerged%20is%3Apr%20author%3Acodeflash-ai%5Bbot%5D), **Unstructured** [(PRs Merged 1](https://github.com/Unstructured-IO/unstructured/pulls?q=is%3Apr+Explanation+and+details+in%3Abody+is%3Amerged), [PRs Merged 2)](https://github.com/Unstructured-IO/unstructured-ingest/pulls?q=is%3Apr+Explanation+and+details+in%3Abody+is%3Amerged), **Langflow** [(PRs Merged)](https://github.com/langflow-ai/langflow/issues?q=state%3Aclosed%20is%3Apr%20author%3Amisrasaurabh1) and many others to ship performant, expert level code.

Codeflash is great at optimizing AI Agents, Computer Vision algorithms, PyTorch code, numerical code, backend code or anything else you might write with Python.

Expand Down
Loading
Loading