Commit graph

6481 commits

Author SHA1 Message Date
Kevin Turcios
3ad21016fc refactor: deduplicate format_code and validate_syntax between JS and TS support
Use self.default_file_extension in format_code and a treesitter_language
property in validate_syntax so TypeScriptSupport inherits both methods
from JavaScriptSupport instead of duplicating them.
2026-03-02 07:33:45 -05:00
Kevin Turcios
67f789ce67 refactor: deduplicate language metadata and helper function creation
Extract add_language_metadata() in AiServiceClient to consolidate 3
identical payload setup blocks. Extract helper_from_func() closure in
import_resolver to consolidate duplicate HelperFunction creation.
2026-03-02 07:20:27 -05:00
Kevin Turcios
415ad33bdc fix: add missing protocol methods to PythonSupport
Protocol methods with default bodies on a Protocol class are not
inherited by implementations. Add explicit detect_module_system and
adjust_test_config_for_discovery to PythonSupport.
2026-03-02 07:09:32 -05:00
Kevin Turcios
38444462f3 docs: document LanguageSupport protocol methods in architecture.md 2026-03-02 07:07:47 -05:00
Kevin Turcios
8ca716258f refactor: convert remaining language guards to protocol dispatch and add Java scaffolding
Add JAVA enum value, is_java() helper, registry import guard, and Java
test frameworks (junit5/junit4/testng) as zero-behavior-change scaffolding.

Add 6 new LanguageSupport protocol methods/properties
(default_language_version, valid_test_frameworks,
test_result_serialization_format, load_coverage,
adjust_test_config_for_discovery, detect_module_system,
process_generated_test_strings) and implement them in PythonSupport and
JavaScriptSupport.

Replace 12 is_python()/is_javascript() guards across aiservice.py (5),
parse_test_output.py (4), verifier.py (2), and discover_unit_tests.py (1)
with protocol dispatch, moving ~40 lines of JS instrumentation logic from
verifier.py into JavaScriptSupport.

Also add imported_type_skeletons field to CodeContext and
JAVA_TESTCASE_TIMEOUT constant for upcoming Java support.
2026-03-02 07:04:36 -05:00
Kevin Turcios
2e4127a3b6
Merge pull request #1708 from codeflash-ai/extract-js-optimizer
refactor: extract JavaScript Optimizer subclass
2026-03-02 11:44:11 +00:00
Kevin Turcios
341c622d40 docs: update rules and architecture for new language structure
- Add JS optimizer, normalizer, and support files to architecture tree
- Update key entry points table for per-function optimization and test execution
- Add protocol dispatch preference to language-patterns rules
- Fix stale context path in optimization-patterns
- Add explicit utf-8 encoding rule scoped to new/changed code
2026-03-02 06:10:12 -05:00
Kevin Turcios
04a94f2b03 test: update tests for refactored language support
- Update discover_functions calls to new (source, file_path) signature
- Use language-specific FunctionOptimizer subclasses in tests
- Add explicit utf-8 encoding to read_text()/write_text() for Windows
- Fix pytest fixture in TestTsJestSkipsConversion (was __init__)
- Update nonexistent file tests for source-based discover_functions
- Remove unused imports
2026-03-02 06:09:06 -05:00
Kevin Turcios
3ebaf1066a refactor: extract JavaScriptFunctionOptimizer and eliminate language guards
- Extract JavaScriptFunctionOptimizer subclass with JS-specific overrides
  for code context, candidate comparison, line profiling, and code replacement
- Replace is_python()/is_javascript() guards with LanguageSupport protocol
  dispatch across optimizer, verification, and utility layers
- Move generate_concolic_tests into LanguageSupport (default no-op, Python
  overrides with CrossHair)
- Collapse normalizer infrastructure: delete CodeNormalizer ABC and registry,
  move implementations into language directories
- Unify discover_functions to source-based signature (source, file_path)
- Flatten function discovery indirection, making git-diff discovery work
  for JS/TS
- Extract language-agnostic code replacer to languages/code_replacer.py
- Deduplicate error constants and function grouping logic into shared locations
- Restore pytest_cmd support in PythonSupport test runners
- Add empty-code guard in language-agnostic code replacer
- Fix tmpdir_path cleanup, self.args crash, UnboundLocalError in finally block
2026-03-02 06:08:22 -05:00
Kevin Turcios
8a75fe1377
Merge pull request #1699 from codeflash-ai/extract-python-optimizer
refactor: extract PythonFunctionOptimizer subclass
2026-03-02 08:18:15 +00:00
claude[bot]
4ccbffe4f3 fix: combine conditions in line_profiler_step to avoid returning empty dict for all TestResults
The return statement was outside the inner if-not block, causing all TestResults
(even populated ones) to return an empty dict.

Co-authored-by: Kevin Turcios <KRRT7@users.noreply.github.com>
2026-03-02 07:36:55 +00:00
claude[bot]
192cf0f8c8 style: fix unused loop variable in function_optimizer.py
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-02 07:19:31 +00:00
Kevin Turcios
b6af185998 fix: split discovery and instrumentation log messages for E2E harnesses
Log "Discovered N existing unit test files" after counting tests, and
"Instrumented N existing unit test files" after injecting profiling.
Python E2E harness matches "Discovered", JS harness matches "Instrumented".
2026-03-02 02:16:50 -05:00
claude[bot]
6f2939aa02 fix: restore 'Discovered' log message for E2E test compatibility
The refactoring changed the log message from 'Discovered' to 'Instrumented',
breaking the E2E test regex in end_to_end_test_utilities.py:225.

Co-authored-by: Kevin Turcios <KRRT7@users.noreply.github.com>
2026-03-02 06:27:56 +00:00
claude[bot]
1f914c58e3 docs: update architecture.md with new Python optimizer files
Add languages/python/function_optimizer.py and languages/python/optimizer.py
to the directory tree and Key Entry Points table.

Co-authored-by: Kevin Turcios <KRRT7@users.noreply.github.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-02 05:44:02 +00:00
Kevin Turcios
6a916ac83f fix: address review feedback on PythonFunctionOptimizer extraction
- Add clarifying comment on shared replace_function_definitions_in_module import
- Remove misleading alias in test_unused_helper_revert.py, use PythonFunctionOptimizer directly
- Align base line_profiler_step return type to dict[str, Any]
- Fix latent bug: handle non-empty TestResults in line_profiler_step
2026-03-01 23:51:43 -05:00
Kevin Turcios
2b40e4bd48
Update codeflash/optimization/optimizer.py
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
2026-03-01 23:36:00 -05:00
Kevin Turcios
a55841b978 fix: use PythonFunctionOptimizer in tests that depend on Python-specific hooks 2026-03-01 23:22:19 -05:00
Kevin Turcios
96c1459ca4 chore: add TODO for get_code_optimization_context extraction, update follow_up.md 2026-03-01 23:22:15 -05:00
Kevin Turcios
a3d81ba95d refactor: remove redundant AST pre-resolution from create_function_optimizer
The factory method was resolving the function AST from original_module_ast
before constructing PythonFunctionOptimizer, duplicating the _resolve_function_ast
hook. Now the hook handles resolution and the factory checks the result post-construction.
2026-03-01 23:10:57 -05:00
Kevin Turcios
a2e3a0a84d refactor: extract line_profiler_step and parse_line_profile_test_results into PythonFunctionOptimizer 2026-03-01 23:02:59 -05:00
Kevin Turcios
c8b8074e48 refactor: extract should_skip_sqlite_cleanup hook into PythonFunctionOptimizer 2026-03-01 22:59:49 -05:00
Kevin Turcios
2457da1932 refactor: extract compare_candidate_results hook into PythonFunctionOptimizer 2026-03-01 22:57:18 -05:00
Kevin Turcios
74d2efb08b refactor: remove is_python() guards from code-restore sites in run_optimized_candidate 2026-03-01 22:52:00 -05:00
Kevin Turcios
f5715e76d3 refactor: extract collect_async_metrics hook into PythonFunctionOptimizer 2026-03-01 22:52:00 -05:00
Kevin Turcios
39067436b4 refactor: extract should_check_coverage hook into PythonFunctionOptimizer
Replace 2 is_python() guard sites around coverage_critic() calls with a
should_check_coverage() hook, defaulting to False in the base class and
True in PythonFunctionOptimizer.
2026-03-01 22:51:36 -05:00
claude[bot]
d93506ce86 fix: resolve mypy type errors in new Python optimizer files 2026-03-02 03:09:55 +00:00
Kevin Turcios
f96e5187c7 refactor: remove redundant instrument_existing_tests override
PythonSupport.instrument_existing_test() already delegates to
inject_profiling_into_existing_test(), so the base class handles
Python correctly through the language_support abstraction.
2026-03-01 22:05:40 -05:00
Kevin Turcios
f6062505ab refactor: extract instrument_async_for_mode and instrument_capture hooks
Move async decorator instrumentation (4 sites) and codeflash capture
instrumentation (2 sites) into overridable hooks. Base class no-ops;
PythonFunctionOptimizer provides the implementations.
2026-03-01 21:55:16 -05:00
Kevin Turcios
524d1be743 refactor: extract instrument_existing_tests into PythonFunctionOptimizer
Move the Python-specific test instrumentation branch (~80 lines using
inject_profiling_into_existing_test) to the subclass. Base class keeps
the JS/TS branch using language_support.instrument_existing_test as the
default.
2026-03-01 21:49:13 -05:00
claude[bot]
717c10d273 fix: resolve mypy type error in resolve_python_function_ast 2026-03-02 02:33:24 +00:00
Kevin Turcios
64de536471 fix: restore base replace_function_and_helpers, fix test imports, move ast to TYPE_CHECKING
- Base class keeps the language-routing replacement logic (used by both
  Python and JS); Python subclass adds unused-helper revert on top via super()
- Tests that exercise Python-specific replace+revert use PythonFunctionOptimizer
- Move `ast` to TYPE_CHECKING in optimizer.py (fixes prek)
2026-03-01 21:30:19 -05:00
Kevin Turcios
c5cdefe5ab refactor: extract PythonFunctionOptimizer subclass from FunctionOptimizer
Move 6 Python-specific methods into PythonFunctionOptimizer in
languages/python/function_optimizer.py. Base class gets no-op defaults;
Optimizer.create_function_optimizer dispatches to the subclass when
is_python().
2026-03-01 21:02:46 -05:00
Kevin Turcios
c7f225de7f refactor: use resolve_python_function_ast in FunctionOptimizer.__init__
Consolidate Python AST resolution through the shared helper instead of
importing get_first_top_level_function_or_method_ast directly.
2026-03-01 20:45:33 -05:00
Kevin Turcios
1ad876fd3d refactor: extract Python-specific optimizer logic to languages/python/optimizer.py
Move AST parsing, code normalization, and imported module validation out of
the language-agnostic Optimizer class into dedicated functions.
2026-03-01 20:38:17 -05:00
Saurabh Misra
86202d40e5
Merge pull request #1690 from codeflash-ai/fix/comparator-itertools-count
fix: handle itertools types in comparator with Python 3.9-3.14 support
2026-02-27 15:21:59 -08:00
Saurabh Misra
4843748acb
Merge pull request #1666 from codeflash-ai/feat/subagent-ide-diff
feat: surface subagent optimization diffs in IDE's native diff view
2026-02-27 15:20:13 -08:00
Kevin Turcios
cbc66e28ad
Merge pull request #1689 from codeflash-ai/consolidate-python-discovery
refactor: consolidate Python function discovery to CST path only
2026-02-27 20:39:16 +00:00
aseembits93
3a33fe43a4 fix: support Python 3.10-3.14 in comparator itertools tests
Handle itertools.cycle on Python 3.14 where __reduce__ was removed by
falling back to element-by-element sampling. Add version guards for
pairwise (3.10+) and batched (3.12+) tests.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-28 00:05:36 +05:30
aseembits93
eeda6c2d32 fix: handle all remaining itertools types in comparator
Add a catch-all handler for itertools iterators (chain, islice, product,
permutations, combinations, starmap, accumulate, compress, dropwhile,
takewhile, filterfalse, zip_longest, groupby, pairwise, batched, tee).
Uses module check (type.__module__ == "itertools") so it automatically
covers any itertools type without version-specific enumeration. groupby
gets special handling to also materialize its group iterators.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-27 23:14:59 +05:30
Kevin Turcios
2b2caf4995 fix: resolve mypy errors in discovery and support files
- Change FunctionVisitor.file_path from str to Path
- Unify dict keys to Path across discovery functions (get_all_files_and_functions,
  get_functions_within_lines, get_functions_within_git_diff, etc.)
- Remove redundant isinstance check in discover_functions
- Add assert for found_function narrowing after exit_with_message
- Fix closest_matching_file_function_name return type narrowing
2026-02-27 12:38:50 -05:00
aseembits93
456a18837b fix: handle itertools.repeat and itertools.cycle in comparator
itertools.repeat uses repr() comparison (same approach as count).
itertools.cycle uses __reduce__() to extract internal state (saved items,
remaining items, and first-pass flag) since repr() only shows a memory
address. The __reduce__ approach is deprecated in 3.14 but is the only
way to access cycle state without consuming elements.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-27 23:06:46 +05:30
aseembits93
12e8f7009c fix: handle itertools.count in comparator
The comparator had no handler for itertools.count (an infinite iterator),
causing it to fall through all type checks and return False even for
equal objects. Use repr() comparison which reliably reflects internal
state and avoids the __reduce__ deprecation coming in Python 3.14.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-27 23:01:06 +05:30
Kevin Turcios
91cf6ea2aa fix: update tests for consolidated CST discovery behavior
Nested functions are now skipped by FunctionVisitor, and
discover_functions no longer swallows parse/IO errors — callers
handle them. Update test expectations accordingly.
2026-02-27 12:08:42 -05:00
Kevin Turcios
47f420214e refactor: consolidate Python function discovery to CST path only
Remove the AST-based discovery path for Python, routing all languages
through the unified CST-based `_find_all_functions_via_language_support`.
Delete dead code: `find_functions_with_return_statement`,
`_find_all_functions_in_python_file`, `function_has_return_statement`,
`function_is_a_property`, and associated constants. Fix FunctionVisitor
to skip nested functions and exclude @property/@cached_property, and let
parse errors propagate for correct empty-dict behavior on invalid files.
2026-02-27 11:55:45 -05:00
Kevin Turcios
50a2538464
Merge pull request #1686 from codeflash-ai/trace_ops
perf: optimize tracer hot path with string-based path ops and caching
2026-02-27 15:14:42 +00:00
Kevin Turcios
94755489e7 should be tests 2026-02-27 09:23:11 -05:00
Kevin Turcios
e614ce8381 commit 2026-02-27 09:10:11 -05:00
Kevin Turcios
a80bb5b2c4 perf: use string-based path ops in tracer hot path, defer Path construction
Keep file_name as a plain str throughout the per-call path. Path is only
constructed once per unique function in the first-time-seen branch where
filter_files_optimized and FunctionModules need it.
2026-02-27 09:05:02 -05:00
Kevin Turcios
a4340b0bc6 perf: cache path resolution and validity checks in tracer hot path
Replace per-call Path.is_relative_to() and Path.exists() (stat syscall)
with a single cached lookup per unique co_filename. Use os.path.realpath
and str.startswith instead of pathlib to avoid object allocation on the
hot path. Profile showed these pathlib ops at ~15% self-time.
2026-02-27 09:05:02 -05:00