chore: sync main into omni-java (batch 3/4)#1558
Conversation
…atibility The loop-runner was loading jest-runner from codeflash's node_modules (v29) instead of the project's (v30), causing "runtime.enterTestCode is not a function" errors. This fix: - Adds recursive search to find jest-runner in any node_modules structure - Works with npm, yarn, and pnpm (including non-hoisted deps) - Prefers higher versions when multiple are found - Removes internal looping in capturePerf when using external loop-runner - Creates fresh TestRunner per batch to avoid Jest 30 state corruption Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
…codeflash into fix/jest30-pnpm-resolution
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
Consolidate three enricher functions (get_imported_class_definitions, get_external_base_class_inits, get_external_class_inits) into a single enrich_testgen_context that parses code context once. Extract shared helpers, unify prune_cst variants, deduplicate loop bodies, and remove dead UsedNameCollector class.
Move code_context_extractor.py and unused_definition_remover.py from codeflash/context/ to codeflash/languages/python/context/ and update all import sites.
Replace duplicate implementations in extract_code_context() and find_helper_functions() with calls to get_code_optimization_context() and get_function_sources_from_jedi() from the canonical context module.
Update stale context/ paths in mypy_allowlist.txt to match the languages/python/context/ move. Add assert to narrow BaseSuite to IndentedBlock in prune_cst for mypy.
Re-add graceful degradation when context exceeds token limits instead of raising ValueError immediately. Read-only context falls back to removing docstrings then removing entirely. Testgen context falls back to removing docstrings then removing enrichment before raising.
The optimization achieves a **68% runtime improvement** (23.5ms → 14.0ms) by replacing the expensive `ast.walk()` traversal with a targeted recursive collection strategy. **Key Performance Improvement:** The original code uses `ast.walk(tree)` which visits **every single node** in the AST tree (12,947 hits shown in line profiler), consuming 71.7% of total runtime. This includes unnecessary nodes like expressions, literals, and operators that can never contain `ImportFrom` statements. The optimized version implements a custom `collect_imports()` function that: 1. **Only traverses module body and control flow structures** where imports can legally appear (function/class definitions, if/while/for blocks, try/except) 2. **Skips irrelevant AST nodes** like expressions, literals, and operators entirely 3. **Recursively processes nested bodies** (body, orelse, finalbody, handlers) in a depth-first manner **Why This Works:** In Python, `from X import Y` statements can only appear: - At module level - Inside function/class definitions - Within control flow blocks (if/while/for/try) By checking `isinstance()` for only these container node types and recursively descending into their body attributes, we avoid traversing the entire AST subtree for each construct. This dramatically reduces the number of nodes visited while maintaining correctness. **Test Case Performance:** The optimization excels across all scales: - **Small imports** (single statements): 60-77% faster - **Large import lists** (100-500 items): 74-104% faster - **Many code blocks** (500-1000 lines): 70-77% faster - **Mixed code/imports** at scale: 70% faster The performance gain is particularly pronounced when the AST contains large amounts of non-import code (functions, classes, expressions), as shown by the `test_mixed_imports_and_code_large_scale` case improving from 9.31ms to 5.45ms (70.8% faster). **Impact on Workloads:** Given the function_references show this is used in code context extraction benchmarks, this optimization will significantly speed up any workflow that analyzes Python imports from large codebases or performs repeated import analysis during development workflows.
The optimized code achieves a **350% speedup** (2.36ms → 523μs) by replacing the generic `ast.walk()` traversal with a targeted stack-based iteration that only visits nodes where class definitions can appear. **Key Performance Improvement:** The original implementation uses `ast.walk(tree)`, which performs an exhaustive depth-first traversal of **every single node** in the AST—including expressions, literals, operators, and other leaf nodes that can never contain class definitions. For a typical Python module, this means checking thousands of irrelevant nodes. The optimized version uses a stack-based approach that only descends into structural nodes (ClassDef, FunctionDef, If, For, While, With, Try blocks) where classes can actually be defined. This dramatically reduces the number of nodes visited and `isinstance()` checks performed. **Why This Matters:** From the test results, we see consistent 200-700% speedups across all scenarios: - Empty modules: 579% faster (5.37μs → 791ns) - minimal traversal overhead - Simple cases: 200-400% faster - fewer nodes to check - Complex nested structures: 405% faster (37.2μs → 7.37μs) - targeted descent pays off - Large modules (500 classes): 280% faster (869μs → 228μs) - scales better - Mixed workloads: 558% faster (799μs → 121μs) - avoids non-class nodes **Impact on Workloads:** Based on the function references showing this is called from `build_testgen_context`, this optimization benefits test generation workflows that analyze Python code structure. Since class extraction is likely performed repeatedly during code analysis, the 4x speedup directly improves overall test generation throughput. The optimization is particularly effective for large codebases with many classes and complex nesting patterns, as demonstrated by the benchmark results.
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
…2026-02-16T20.53.40 ⚡️ Speed up function `collect_existing_class_names` by 351% in PR #1498 (`cf-simplify-context-extraction`)
The optimized collect_imports missed match/case statements where imports can legally appear. Add hasattr-guarded handling for ast.Match nodes. Co-authored-by: Kevin Turcios <KRRT7@users.noreply.github.com>
…2026-02-16T20.49.33 ⚡️ Speed up function `_parse_and_collect_imports` by 69% in PR #1498 (`cf-simplify-context-extraction`)
Extract shared helpers and remove dead code across the language support area: - Extract `is_assignment_used()` and move `recurse_sections` to unused_definition_remover.py, replacing duplicated logic in both context files - Extract `function_sources_to_helpers()` in support.py to unify identical HelperFunction construction - Remove dead `get_comment_prefix()` method from protocol and all implementations (comment_prefix property serves all callers)
…tion refactor: move context extraction to languages/python/context/
Replace deprecated license table format with modern license-files array
in both main package and codeflash-benchmark subpackage. This resolves
the setuptools deprecation warning about TOML table license format.
Changes:
- Use license-files = ["LICENSE"] instead of license = {text = "BSL-1.1"}
- Add LICENSE file to root directory
- Add LICENSE and README.md to codeflash-benchmark/
fix: update license format to use license-files
The cross-region inference profile for Claude Opus 4.6 on Bedrock is `us.anthropic.claude-opus-4-6-v1`, not `us.anthropic.claude-opus-4-6-v1:0`. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
fix: use correct Bedrock inference profile ID
…ternal-runner [FIX][JS] capturePerf shouldn't break when we have an external runner (batch size = 1)
…s_path_mismatch [FIX] path mismatch for behavior_file_path for original baseline, resulting into empty validated test source
Instead of injecting `from codeflash.code_utils.codeflash_wrap_decorator import ...` into instrumented source files, inject the decorator function definitions directly. This removes the hard dependency on the codeflash package being importable at runtime in the target environment, matching the pattern already used for sync instrumentation.
Replace inline code injection with a helper file approach that writes decorator implementations to a separate codeflash_async_wrapper.py file. This removes the codeflash package import dependency from instrumented source files while keeping line numbers stable (only 1 import + 1 decorator line added, same as before). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…e file Write all three async decorator implementations into one helper file to avoid overwrite issues when switching modes. Clean up the helper file in revert_code_and_helpers and early-exit paths so it doesn't persist in the user's project root after optimization.
…rget The e2e test expects codeflash to detect and fix the intentional use of blocking time.sleep() in an async function. Using asyncio.sleep() removes the optimization opportunity and causes the CI job to fail. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The optimized code removes `import time`, shifting all function lines up by 1. Update expected_lines from [10-20] to [9-19] to match. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
refactor: inline async decorators to remove codeflash import dependency
In --worktree mode, get_git_diff resolves file paths from cwd (the original repo), but module_root/project_root are mirrored to the worktree. This caused filter_functions to reject all diff-discovered functions as "outside module-root". Use the pre-mirror roots for filtering, then remap file paths to the worktree for downstream use.
git_root_dir() searches from CWD (original repo), but in worktree mode file paths have been remapped to the worktree. This caused relative_to() to raise ValueError when creating PRs. Search from module_root instead so root_dir is always in the same path space as the file paths.
…t-path Fix worktree mode filtering out all diff-discovered functions
chore: release v0.20.1
PR Review SummaryPrek ChecksStatus: Fixed and passing ✅ Fixed 8 linting issues and 6 formatting issues in commit
Mypy: 567 errors across checked files (325 in new Java module files, 242 in existing code). These are pre-existing type issues not introduced by this PR — the new Java module would benefit from stricter typing in a follow-up. Code ReviewNo critical issues found. ✅ This is a sync PR merging main into omni-java. Key changes reviewed:
Test CoveragePR branch: 3113 passed, 29 failed, 58 skipped | Main branch: 2356 passed, 8 failed, 56 skipped Modified Files (PR vs Main)
New Java Files
Coverage notes:
Last updated: 2026-02-20T01:50Z |
⚡️ Codeflash found optimizations for this PR📄 2,141% (21.41x) speedup for
|
Summary
Merges main up to
6020c4fa(PR #1533, cf-release-0.20.1) into omni-java.Included
languages/python/context/(refactor: move context extraction to languages/python/context/ #1498)Conflict resolutions
codeflash/code_utils/config_consts.py: Main bumpedOPTIMIZATION_CONTEXT_TOKEN_LIMITandTESTGEN_CONTEXT_TOKEN_LIMITfrom 16000 → 48000. Kept omni-java'sJAVA_TESTCASE_TIMEOUT = 120and Python timeout comment. Combined both.pyproject.toml: Main changedlicense = {text = "BSL-1.1"}→license-files = ["LICENSE"]. Kept omni-java'stree-sitter-java>=0.23.0dependency. Combined both.Remaining batches
6346c740(HEAD of main)