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>
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.
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.
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.
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.
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.
Move code_context_extractor.py and unused_definition_remover.py from
codeflash/context/ to codeflash/languages/python/context/ and update
all import sites.
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.
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>
5 scenarios testing: sequential debugging, Result type + effort config,
test patterns, domain type conventions, and deduplication/repair mechanics.
Also adds tessl-labs/tessl-skill-eval-scenarios dev dependency.
- Add trigger hints and code snippets to both skills
- Add checkpoints after each step
- Extract module reference and troubleshooting into linked files
- Bump codeflash-skills tile to 0.2.0
os.path.relpath returns backslashes on Windows. The backslash-to-slash
conversion happened after the ./ / ../ prefix check, so the check
failed and prepended ./ producing ./../src/... paths. Use
Path.as_posix() instead of manual string replacement.
The formatter correctly removed the unused re-exports from
parse_test_output.py. Update the test to import directly from
codeflash.languages.javascript.parse.