Skip to content

Commit 0650973

Browse files
committed
refactor: restructure CLAUDE.md for effective context usage
- Remove commands block from CLAUDE.md (standard tool usage Claude knows) - Remove dead @AGENTS.md reference - Add optimization pipeline overview with module pointers - Add domain glossary (optimization candidate, addressable time, candidate forest, replay test, tracer, worktree mode) - Extract mypy workflow to .claude/skills/fix-mypy.md (on-demand) - Create .claude/skills/fix-prek.md for prek workflow (on-demand) - Add key entry points table to architecture.md - Create path-scoped rules: optimization-patterns.md, language-patterns.md - Remove redundancy from source-code.md and across rules files - Move "never use pip" convention to code-style.md
1 parent 42a1150 commit 0650973

File tree

9 files changed

+85
-48
lines changed

9 files changed

+85
-48
lines changed

.claude/rules/architecture.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,17 @@ codeflash/
2626
├── result/ # Result types and handling
2727
└── version.py # Version information
2828
```
29+
30+
## Key Entry Points
31+
32+
| Task | Start here |
33+
|------|------------|
34+
| CLI arguments & commands | `cli_cmds/cli.py` |
35+
| Optimization orchestration | `optimization/optimizer.py``run()` |
36+
| Per-function optimization | `optimization/function_optimizer.py` |
37+
| Function discovery | `discovery/functions_to_optimize.py` |
38+
| Context extraction | `context/code_context_extractor.py` |
39+
| Test execution | `verification/test_runner.py`, `verification/pytest_plugin.py` |
40+
| Performance ranking | `benchmarking/function_ranker.py` |
41+
| Domain types | `models/models.py`, `models/function_types.py` |
42+
| Result handling | `either.py` (`Result`, `Success`, `Failure`, `is_successful`) |

.claude/rules/code-style.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
- **Line length**: 120 characters
44
- **Python**: 3.9+ syntax
5+
- **Package management**: Always use `uv`, never `pip`
56
- **Tooling**: Ruff for linting/formatting, mypy strict mode, prek for pre-commit checks
67
- **Comments**: Minimal - only explain "why", not "what"
78
- **Docstrings**: Do not add unless explicitly requested

.claude/rules/language-patterns.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
paths:
3+
- "codeflash/languages/**/*.py"
4+
---
5+
6+
# Language Support Patterns
7+
8+
- Current language is a module-level singleton in `languages/current.py` — use `set_current_language()` / `current_language()`, never pass language as a parameter through call chains
9+
- Use `get_language_support(identifier)` from `languages/registry.py` to get a `LanguageSupport` instance — never import language classes directly
10+
- New language support classes must use the `@register_language` decorator to register with the extension and language registries
11+
- `languages/__init__.py` uses `__getattr__` for lazy imports to avoid circular dependencies — follow this pattern when adding new exports
12+
- `is_javascript()` returns `True` for both JavaScript and TypeScript
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
paths:
3+
- "codeflash/optimization/**/*.py"
4+
- "codeflash/verification/**/*.py"
5+
- "codeflash/benchmarking/**/*.py"
6+
- "codeflash/context/**/*.py"
7+
---
8+
9+
# Optimization Pipeline Patterns
10+
11+
- All major operations return `Result[SuccessType, ErrorType]` — construct with `Success(value)` / `Failure(error)`, check with `is_successful()` before calling `unwrap()`
12+
- Code context has token limits (`OPTIMIZATION_CONTEXT_TOKEN_LIMIT`, `TESTGEN_CONTEXT_TOKEN_LIMIT` in `config_consts.py`) — exceeding them rejects the function
13+
- `read_writable_code` can span multiple files; `read_only_context_code` is reference-only
14+
- Code is serialized as markdown code blocks: ` ```language:filepath\ncode\n``` ` (see `CodeStringsMarkdown`)
15+
- Candidates form a forest (DAG): refinements/repairs reference `parent_id` on previous candidates
16+
- Test generation and optimization run concurrently — coordinate through `CandidateEvaluationContext`
17+
- Generated tests are instrumented with `codeflash_capture.py` to record return values and traces

.claude/rules/source-code.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,3 @@ paths:
66
# Source Code Rules
77

88
- Use `libcst` for code modification/transformation to preserve formatting. `ast` is acceptable for read-only analysis and parsing.
9-
- NEVER use leading underscores for function names (e.g., `_helper`). Python has no true private functions. Always use public names.
10-
- Any new feature or bug fix that can be tested automatically must have test cases.
11-
- If changes affect existing test expectations, update the tests accordingly. Tests must always pass after changes.

.claude/rules/testing.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ paths:
1313
- Use `.as_posix()` when converting resolved paths to strings (normalizes to forward slashes).
1414
- Any new feature or bug fix that can be tested automatically must have test cases.
1515
- If changes affect existing test expectations, update the tests accordingly. Tests must always pass after changes.
16+
- The pytest plugin patches `time`, `random`, `uuid`, and `datetime` for deterministic test execution — never assume real randomness or real time in verification tests.
17+
- `conftest.py` uses an autouse fixture that calls `reset_current_language()` — tests always start with Python as the default language.

.claude/skills/fix-mypy.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Fix mypy errors
2+
3+
When modifying code, fix any mypy type errors in the files you changed:
4+
5+
```bash
6+
uv run mypy --non-interactive --config-file pyproject.toml <changed_files>
7+
```
8+
9+
- Fix type annotation issues: missing return types, incorrect types, Optional/None unions, import errors for type hints
10+
- Do NOT add `# type: ignore` comments — always fix the root cause
11+
- Do NOT fix type errors that require logic changes, complex generic type rework, or anything that could change runtime behavior
12+
- Files in `mypy_allowlist.txt` are checked in CI — ensure they remain error-free

.claude/skills/fix-prek.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Fix prek failures
2+
3+
When prek (pre-commit) checks fail:
4+
5+
1. Run `uv run prek run` to see failures (local, checks staged files)
6+
2. In CI, the equivalent is `uv run prek run --from-ref origin/main`
7+
3. prek runs ruff format, ruff check, and mypy on changed files
8+
4. Fix issues in order: formatting → lint → type errors
9+
5. Re-run `uv run prek run` to verify all checks pass

CLAUDE.md

Lines changed: 18 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,35 @@
11
# CLAUDE.md
22

3-
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
4-
53
## Project Overview
64

75
CodeFlash is an AI-powered Python code optimizer that automatically improves code performance while maintaining correctness. It uses LLMs to generate optimization candidates, verifies correctness through test execution, and benchmarks performance improvements.
86

9-
## Common Commands
10-
11-
```bash
12-
# Package management (NEVER use pip)
13-
uv sync # Install dependencies
14-
uv sync --group dev # Install dev dependencies
15-
uv add <package> # Add a package
16-
17-
# Running tests
18-
uv run pytest tests/ # Run all tests
19-
uv run pytest tests/test_foo.py # Run specific test file
20-
uv run pytest tests/test_foo.py::test_bar -v # Run single test
21-
22-
# Type checking and linting
23-
uv run mypy codeflash/ # Type check
24-
uv run ruff check codeflash/ # Lint
25-
uv run ruff format codeflash/ # Format
26-
27-
# Linting (run before committing, checks staged files)
28-
uv run prek run
29-
30-
# Linting in CI (checks all files changed since main)
31-
uv run prek run --from-ref origin/main
7+
## Optimization Pipeline
328

33-
# Mypy type checking (run on changed files before committing)
34-
uv run mypy --non-interactive --config-file pyproject.toml <changed_files>
35-
36-
# Running the CLI
37-
uv run codeflash --help
38-
uv run codeflash init # Initialize in a project
39-
uv run codeflash --all # Optimize entire codebase
9+
```
10+
Discovery → Ranking → Context Extraction → Test Gen + Optimization → Baseline → Candidate Evaluation → PR
4011
```
4112

42-
## Mypy Type Checking
13+
1. **Discovery** (`discovery/`): Find optimizable functions across the codebase
14+
2. **Ranking** (`benchmarking/function_ranker.py`): Rank functions by addressable time using trace data
15+
3. **Context** (`context/`): Extract code dependencies (read-writable code + read-only imports)
16+
4. **Optimization** (`optimization/`, `api/`): Generate candidates via AI service, run in parallel with test generation
17+
5. **Verification** (`verification/`): Run candidates against tests, compare outputs via custom pytest plugin
18+
6. **Benchmarking** (`benchmarking/`): Measure performance, select best candidate by speedup
19+
7. **Result** (`result/`, `github/`): Create PR with winning optimization
4320

44-
When modifying code, fix any mypy type errors in the files you changed. Run mypy on changed files:
21+
## Domain Glossary
4522

46-
```bash
47-
uv run mypy --non-interactive --config-file pyproject.toml <changed_files>
48-
```
49-
50-
Rules:
51-
- Fix type annotation issues: missing return types, incorrect types, Optional/None unions, import errors for type hints
52-
- Do NOT add `# type: ignore` comments — always fix the root cause
53-
- Do NOT fix type errors that require logic changes, complex generic type rework, or anything that could change runtime behavior
54-
- Files in `mypy_allowlist.txt` are checked in CI — ensure they remain error-free
23+
- **Optimization candidate**: A generated code variant that might be faster (`OptimizedCandidate`)
24+
- **Function context**: All code needed for optimization — split into read-writable (modifiable) and read-only (reference)
25+
- **Addressable time**: Time a function spends that could be optimized (own time + callee time / call count)
26+
- **Candidate forest**: DAG of candidates where refinements/repairs build on previous candidates
27+
- **Replay test**: Test generated from recorded benchmark data to reproduce real workloads
28+
- **Tracer**: Profiling system that records function call trees and timings (`tracing/`, `tracer.py`)
29+
- **Worktree mode**: Git worktree-based parallel optimization (`--worktree` flag)
5530

5631
<!-- Section below is auto-generated by `tessl install` - do not edit manually -->
5732

5833
# Agent Rules <!-- tessl-managed -->
5934

6035
@.tessl/RULES.md follow the [instructions](.tessl/RULES.md)
61-
62-
@AGENTS.md

0 commit comments

Comments
 (0)