codeflash/tests
mohammed ahmed 4623659974 Fix: Add language check before calling adaptive_optimize for JS/TS
**Issue #8: CLI incorrectly calls adaptive_optimize for JavaScript/TypeScript**

**Problem:**
When a refined candidate (source=REFINE) succeeds for JS/TS, the next
iteration calls adaptive_optimize (Python-only endpoint) instead of
optimize_code_refinement (all languages). This results in "422 - Invalid
code generated" from the AI service because adaptive_optimize tries to
parse JS/TS code using libcst (Python AST parser).

**Root Cause:**
File: codeflash/languages/function_optimizer.py (line 1266)
The code checked if a REFINE candidate existed but did not check the
language before calling adaptive_optimize.

**Evidence:**
- Trace ID: 1417a6da-796c-4a38-8c44-00401dbab6c7
- Function: formatBytes (TypeScript)
- Error: "POST /ai/adaptive_optimize HTTP/1.1" 422 36
- AI service logs: "adaptive_optimize invalid code"

**Fix:**
Added language check at line 1266:
```python
if is_candidate_refined_before and self.function_to_optimize.language == "python":
    # Call adaptive_optimize (Python-only)
else:
    # Call optimize_code_refinement (all languages)
```

**Testing:**
- Added 4 regression tests in test_adaptive_optimize_language_bug.py
- All tests pass
- No linting errors from `uv run prek`

**Impact:**
- Fixes systematic bug affecting JS/TS optimizations with successful REFINE candidates
- Allows second refinement iteration to proceed for JS/TS
- Python behavior unchanged (still uses adaptive_optimize after REFINE)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-04-04 10:49:24 +00:00
..
benchmarks refactor: restructure codebase for locality and faster CLI startup 2026-03-07 08:21:27 -05:00
code_utils fix: replace remaining uv tool install refs with uv pip install 2026-03-30 15:26:07 -07:00
languages Merge branch 'main' of github.com:codeflash-ai/codeflash into fix/false-positive-test-discovery 2026-04-03 13:32:34 +02:00
scripts fix(test): move --no-pr before optimize subcommand in e2e tracer test 2026-04-01 06:56:27 +00:00
test_languages Fix: Add language check before calling adaptive_optimize for JS/TS 2026-04-04 10:49:24 +00:00
test_setup refactor: remove zero-config logic from java-config-redesign branch 2026-03-26 07:39:29 +00:00
__init__.py Move aiservice to django/aiservice 2024-01-02 16:55:24 -08:00
conftest.py format and lint all 2026-01-29 01:39:48 -08:00
mymodule.py format and lint all 2026-01-29 01:39:48 -08:00
test_add_language_metadata.py Remove python_version from API payloads 2026-03-27 02:53:22 -05:00
test_add_needed_imports_from_module.py fix: normalize module fallback formatting for import merge 2026-03-17 18:16:54 -06:00
test_add_runtime_comments.py merge to main 2026-02-21 01:49:31 +02:00
test_async_concurrency_decorator.py format and lint all 2026-01-29 01:39:48 -08:00
test_async_function_discovery.py format and lint all 2026-01-29 01:39:48 -08:00
test_async_run_and_parse_tests.py refactor: restructure codebase for locality and faster CLI startup 2026-03-07 08:21:27 -05:00
test_async_wrapper_sqlite_validation.py format and lint all 2026-01-29 01:39:48 -08:00
test_call_graph.py fix ranking boost ordering and statement helper extraction 2026-03-15 23:29:35 -06:00
test_cleanup_instrumented_files.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_cmd_auth.py fix: remove invalid get_shell_rc_path patches from auth tests 2026-03-25 00:55:39 +00:00
test_cmd_init.py refactor: restructure codebase for locality and faster CLI startup 2026-03-07 08:21:27 -05:00
test_code_context_extractor.py fix: skip attrs classes in __init__ instrumentation; add attrs support to code_context_extractor 2026-03-18 01:33:40 -06:00
test_code_deduplication.py [Fix] Normalizer and expand its scope 2026-03-06 21:31:24 +05:30
test_code_extractor_none_aliases_exact.py merge to main 2026-02-21 01:49:31 +02:00
test_code_replacement.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_code_replacer_matching.py Merge branch 'main' into omni-java 2026-03-13 00:15:19 +00:00
test_code_utils.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_codeflash_capture.py Merge branch 'main' into omni-java 2026-03-13 00:15:19 +00:00
test_codeflash_checkpoint.py format and lint all 2026-01-29 01:39:48 -08:00
test_codeflash_trace_decorator.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_comparator.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_compare.py feat: add --script mode to codeflash compare 2026-04-02 11:36:54 -05:00
test_critic.py format and lint all 2026-01-29 01:39:48 -08:00
test_early_dedup.py feat: early dedup of optimization candidates before benchmark loop 2026-03-22 05:19:15 -05:00
test_existing_tests_source_for.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_file_to_no_of_tests.py format and lint all 2026-01-29 01:39:48 -08:00
test_formatter.py fix: pass language to format_generated_code for correct temp file extension 2026-03-25 18:05:28 +02:00
test_function_dependencies.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_function_discovery.py merge: incorporate omni-main-java sync work 2026-03-03 23:42:39 -05:00
test_function_ranker.py format and lint all 2026-01-29 01:39:48 -08:00
test_get_code.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_get_helper_code.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_get_read_only_code.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_get_read_writable_code.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_get_testgen_code.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_git_utils.py test: update git_diff tests to match new all-extensions behavior 2026-03-19 18:07:19 +00:00
test_humanize_time.py format and lint all 2026-01-29 01:39:48 -08:00
test_init_javascript.py test: add unit tests for detect_project_language 2026-03-12 04:01:49 +00:00
test_inject_profiling_used_frameworks.py fix: resolve remaining test failures from main sync 2026-03-04 00:13:27 -05:00
test_instrument_all_and_run.py refactor: restructure codebase for locality and faster CLI startup 2026-03-07 08:21:27 -05:00
test_instrument_async_tests.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_instrument_codeflash_capture.py fix: address review feedback for attrs init instrumentation 2026-03-18 03:34:44 -06:00
test_instrument_codeflash_trace.py format and lint all 2026-01-29 01:39:48 -08:00
test_instrument_line_profiler.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_instrument_tests.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_instrumentation_run_results_aiservice.py refactor: restructure codebase for locality and faster CLI startup 2026-03-07 08:21:27 -05:00
test_is_numerical_code.py merge to main 2026-02-21 01:49:31 +02:00
test_java_assertion_removal.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_java_multimodule_deps_install.py refactor: replace find_gradle_executable and find_maven_executable with find_executable method in Maven and Gradle strategies 2026-03-10 08:27:10 +02:00
test_java_test_discovery.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_java_test_filter_validation.py refactor: replace find_gradle_executable and find_maven_executable with find_executable method in Maven and Gradle strategies 2026-03-10 08:27:10 +02:00
test_java_tests_project_rootdir.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_javascript_assertion_removal.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_javascript_function_discovery.py merge to main 2026-02-21 01:49:31 +02:00
test_lru_cache_clear.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_merge_test_results.py organized imports 2025-03-28 15:26:27 -07:00
test_merge_tests.py update the test-key format 2024-01-22 16:26:32 -08:00
test_mock_candidate_replacement.py fix failing unit tests with recent refactoring 2026-03-07 01:48:41 +05:30
test_multi_file_code_replacement.py test: sync dual-changed test files from main with omni-java fixes 2026-03-02 15:30:16 -05:00
test_parse_line_profile_test_output.py refactor: remove redundant try/finally; rely on conftest autouse fixture for language cleanup 2026-03-18 18:23:49 +00:00
test_parse_pytest_test_failures.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_parse_test_output_regex.py Merge remote-tracking branch 'origin/main' into omni-java 2026-03-04 01:52:32 -05:00
test_pickle_patcher.py fix: update tests for multi-round benchmark plugin 2026-04-02 07:24:55 -05:00
test_property_getter_exclusion.py Fix: Exclude property getters/setters from function discovery 2026-04-01 01:58:33 +00:00
test_pytest_plugin_deterministic_patches.py update tests 2025-10-28 23:05:26 -07:00
test_ranking_boost.py fix: deduplicate test count calls, guard None, and log effort escalation 2026-03-16 14:41:55 -06:00
test_reference_graph.py merge to main 2026-02-21 01:49:31 +02:00
test_remove_functions_from_generated_tests.py merge to main 2026-02-21 01:49:31 +02:00
test_remove_test_functions.py fix: address PR review — recurse _expr_name for call-style decorators, guard empty-set superset 2026-03-07 03:03:23 -05:00
test_remove_unused_definitions.py perf: eliminate redundant CST parsing in get_code_optimization_context 2026-03-16 10:11:58 -06:00
test_shell_utils.py format and lint all 2026-01-29 01:39:48 -08:00
test_static_analysis.py merge to main 2026-02-21 01:49:31 +02:00
test_test_runner.py test: sync test files from main (safe, main-only changes) 2026-03-02 15:25:50 -05:00
test_trace_benchmarks.py fix: update test expectations for multi-round benchmark plugin 2026-04-02 11:18:33 -05:00
test_tracer.py format and lint all 2026-01-29 01:39:48 -08:00
test_unit_test_discovery.py format and lint all 2026-01-29 01:39:48 -08:00
test_unused_helper_revert.py test: sync test files from main (safe, main-only changes) 2026-03-02 15:25:50 -05:00
test_validate_javascript_code.py merge to main 2026-02-21 01:49:31 +02:00
test_validate_python_code.py format and lint all 2026-01-29 01:39:48 -08:00
test_version_check.py format and lint all 2026-01-29 01:39:48 -08:00
test_worktree.py test: sync test files from main (safe, main-only changes) 2026-03-02 15:25:50 -05:00