Skip to content

refactor: prepare omni-java for main merge (batch 2)#1717

Closed
KRRT7 wants to merge 120 commits intoomni-javafrom
prepare-java-support-2
Closed

refactor: prepare omni-java for main merge (batch 2)#1717
KRRT7 wants to merge 120 commits intoomni-javafrom
prepare-java-support-2

Conversation

@KRRT7
Copy link
Collaborator

@KRRT7 KRRT7 commented Mar 2, 2026

Summary

Changes

  • verifier.py — remove inline JS instrumentation and module detection
  • test_runner.py — remove test runner functions (moved to language support)
  • code_context_extractor.py — remove get_code_optimization_context_for_language
  • code_replacer.py — remove replace_function_definitions_for_language and helpers
  • optimizer.py — remove JS methods, replace inline Python/JS logic with protocol dispatch
  • test_multi_file_code_replacement.py — update to PythonFunctionOptimizer

Still remaining

  • function_optimizer.py — base class extraction (the big one)

aseembits93 and others added 30 commits February 4, 2026 16:48
Remove safe_relative_to, resolve_classes_from_modules,
extract_classes_from_type_hint, resolve_transitive_type_deps,
extract_init_stub, _is_project_module_cached, is_project_path,
_is_project_module, extract_imports_for_class,
collect_names_from_annotation, is_dunder_method, _qualified_name,
and _validate_classdef. Inline trivial helpers into prune_cst and
clean up enrich_testgen_context and get_function_sources_from_jedi.
Remove corresponding tests.
Add enrichment step that parses FTO parameter type annotations, resolves
types via jedi (following re-exports), and extracts full __init__ source
to give the LLM constructor context for typed parameters.
Fix 10 failing tests: remove wrong assertions expecting import statements
inside extracted class code, use substring matching for UserDict class
signature, and rewrite click-dependent tests as project-local equivalents.
Add tests for resolve_instance_class_name, enhanced extract_init_stub_from_class,
and enrich_testgen_context instance resolution.
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
The optimized code achieves a **70% runtime speedup** (from 7.02ms to 4.13ms) through three key improvements:

## 1. **Faster Class Discovery via Deque-Based BFS (Primary Speedup)**
The original code uses `ast.walk()` which recursively traverses the entire AST tree even after finding the target class. The line profiler shows this taking 20.5ms (71% of time).

The optimized version replaces this with an explicit BFS using `collections.deque`, which stops immediately upon finding the target class. The profiler shows this reduces traversal time to 9.95ms - **cutting the search overhead by >50%**.

This is especially impactful when:
- The target class appears early in the module (eliminates unnecessary traversal)
- The module contains many classes (test shows 7-10% faster on modules with 100-1000 classes)
- The function is called frequently (shown by the 108% speedup on 1000 repeated calls)

## 2. **Explicit Loops Replace Generator Overhead**
The original code uses `any()` with a generator expression and `min()` with a generator to check decorators and find minimum line numbers. These create function call and generator overhead.

The optimized version uses explicit `for` loops with early breaks:
- Decorator checking: Directly iterates and breaks on first match
- Min line number: Uses explicit comparison instead of `min()` generator

The profiler shows decorator processing time reduced from ~1.4ms to ~0.3ms, and min line calculation from 69μs to 28μs.

## 3. **Conditional Flag Pattern for Relevance Checking**
Instead of evaluating both conditions in a compound expression, the optimized version uses an `is_relevant` flag with early exits, reducing redundant checks.

## Impact on Workloads
Based on `function_references`, this function is called from:
- `enrich_testgen_context`: Used in test generation workflows where it may process many classes
- Benchmark tests: Indicates this is in a performance-critical path

The optimization particularly benefits:
- **Large codebases**: 89-90% faster on classes with 100+ methods or 50+ properties
- **Repeated calls**: 108% faster when called 1000 times in sequence
- **Early matches**: Up to 88% faster when target class is found quickly
- **Deep nesting**: 57% faster for nested classes

The annotated tests show consistent 50-108% speedups across most scenarios, with minimal gains (6-10%) only when processing very large files where string slicing dominates runtime.
Add --agent CLI flag for AI agent integrations that skips all
interactive prompts. In agent mode, checkpoint resume is skipped
entirely so each run starts fresh. Also gates the existing checkpoint
prompt behind --yes.
In agent mode, disable all Rich output (panels, spinners, progress bars,
syntax highlighting) and use a plain StreamHandler for logging. Optimization
results with explanation and unified diff are written to stdout. A log
filter strips LSP prefixes and drops noisy test/file-path messages.
Also skip checkpoint creation and suppress Python warnings in agent mode.
…oncolic tests

- --agent now implies --no-pr and --worktree so source files stay clean
- Output uses structured XML (codeflash-optimization) with optimized-code
  for the consuming agent to apply via Edit/Write
- Skip concolic test generation in agent mode
- Skip patch file creation in worktree + agent mode
fix: resolve test file paths in discover_tests_pytest to fix path com…
The comparator did not recognize `types.UnionType` (Python 3.10+ `X | Y`
syntax), causing it to fall through to "Unknown comparator input type".
Conditionally include it in the equality-checked types tuple.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Rename the CLI flag, env var (CODEFLASH_SUBAGENT_MODE), helper
(is_subagent_mode), and related symbols to avoid confusion with
CodeFlash's own agent terminology.
Avoids computing the full summary (callee counts, string formatting)
only to discard it when running in subagent mode.
@codeflash-ai codeflash-ai deleted a comment from claude bot Mar 2, 2026
@codeflash-ai codeflash-ai deleted a comment from claude bot Mar 2, 2026
@codeflash-ai codeflash-ai deleted a comment from claude bot Mar 2, 2026
@codeflash-ai codeflash-ai deleted a comment from claude bot Mar 2, 2026
@codeflash-ai codeflash-ai deleted a comment from claude bot Mar 2, 2026
@codeflash-ai codeflash-ai deleted a comment from claude bot Mar 2, 2026
@codeflash-ai codeflash-ai deleted a comment from claude bot Mar 2, 2026
@codeflash-ai codeflash-ai deleted a comment from claude bot Mar 2, 2026
KRRT7 and others added 2 commits March 2, 2026 10:53
…owing

- Use XML structure instead of markdown for clearer step boundaries
- Resolve stale review threads via GraphQL instead of leaving them
- Positive framing instead of negation for instructions
- Replace aggressive language with calm direct instructions
- Add /simplify skill invocation for code quality pass
- Add verification checkpoint at the end
- Auto-close stale codeflash optimization PRs (age, conflicts, CI failures, deleted functions)
- Remove inline comment MCP tool, add Skill tool
fix: rewrite Claude Code PR review prompt
@KRRT7
Copy link
Collaborator Author

KRRT7 commented Mar 2, 2026

Duplicate Code Detected

This PR introduces several instances of duplicated code across language implementations:

1. _build_optimization_context method (HIGH)

Locations:

  • codeflash/languages/java/function_optimizer.py:58-136
  • codeflash/languages/javascript/function_optimizer.py:58-136

What's duplicated: The entire 79-line _build_optimization_context static method is byte-for-byte identical between JavaFunctionOptimizer and JavaScriptFunctionOptimizer.

Suggestion: Extract this method to the base FunctionOptimizer class in codeflash/optimization/function_optimizer.py. The logic is completely language-agnostic — it builds a CodeOptimizationContext from a CodeContext object by organizing helpers by file, computing token limits, and generating hashes.

2. line_profiler_step method (HIGH)

Locations:

  • codeflash/languages/java/function_optimizer.py:168-205
  • codeflash/languages/javascript/function_optimizer.py:169-206

What's duplicated: The 38-line line_profiler_step method is identical in both JavaFunctionOptimizer and JavaScriptFunctionOptimizer.

Suggestion: Move to the base FunctionOptimizer class. This method delegates to language-specific language_support methods, so it doesn't contain any Java/JS-specific logic.

3. parse_line_profile_test_results method (HIGH)

Locations:

  • codeflash/languages/java/function_optimizer.py:159-166
  • codeflash/languages/javascript/function_optimizer.py:160-167

What's duplicated: Identical 8-line method for parsing line profiler results.

Suggestion: Move to the base FunctionOptimizer class.

4. should_skip_sqlite_cleanup method (HIGH)

Locations:

  • codeflash/languages/java/function_optimizer.py:156-157
  • codeflash/languages/javascript/function_optimizer.py:157-158

What's duplicated: Identical 2-line method returning the same boolean logic.

Suggestion: Move to the base FunctionOptimizer class.

5. process_generated_test_strings method (HIGH)

Locations:

  • codeflash/languages/python/support.py:212-231
  • codeflash/languages/java/support.py:129-148

What's duplicated: The 20-line method is identical in PythonSupport and JavaSupport — both replace {codeflash_run_tmp_dir_client_side} placeholders in instrumented test sources.

Suggestion: Extract to a shared utility function in codeflash/languages/base.py or create a mixin class for LanguageSupport implementations. Note: JavaScriptSupport has a different implementation with additional JS-specific transformations, so it should remain separate.

Impact

These duplicates total ~150 lines of repeated code. Consolidating them will:

  • Reduce maintenance burden (bug fixes only need to be applied once)
  • Improve consistency across language implementations
  • Make the codebase easier to understand

cc @codeflash-ai/reviewers

@KRRT7 KRRT7 closed this Mar 2, 2026
@KRRT7 KRRT7 reopened this Mar 2, 2026
@codeflash-ai codeflash-ai deleted a comment from claude bot Mar 2, 2026
@codeflash-ai codeflash-ai deleted a comment from claude bot Mar 2, 2026
@codeflash-ai codeflash-ai deleted a comment from claude bot Mar 2, 2026
@codeflash-ai codeflash-ai deleted a comment from claude bot Mar 2, 2026
@codeflash-ai codeflash-ai deleted a comment from claude bot Mar 2, 2026
@codeflash-ai codeflash-ai deleted a comment from claude bot Mar 2, 2026
@codeflash-ai codeflash-ai deleted a comment from claude bot Mar 2, 2026
@codeflash-ai codeflash-ai deleted a comment from claude bot Mar 2, 2026
@codeflash-ai
Copy link
Contributor

codeflash-ai bot commented Mar 2, 2026

⚡️ Codeflash found optimizations for this PR

📄 14% (0.14x) speedup for get_optimized_code_for_module in codeflash/languages/code_replacer.py

⏱️ Runtime : 44.5 milliseconds 39.1 milliseconds (best of 23 runs)

A dependent PR with the suggested changes has been created. Please review:

If you approve, it will be merged into this PR (branch prepare-java-support-2).

Static Badge

@KRRT7 KRRT7 marked this pull request as ready for review March 2, 2026 19:07
@KRRT7 KRRT7 force-pushed the prepare-java-support-2 branch from 56ae338 to 7f96d61 Compare March 2, 2026 19:09
Full Java optimization pipeline: discovery, test discovery, context
extraction, instrumentation, test execution (Maven/direct JVM),
benchmarking, code replacement, coverage (JaCoCo), line profiling,
and result comparison (Kryo serialization).

Includes protocol-aligned JavaSupport, JavaFunctionOptimizer, and
merge fixes for omni-java → main integration.
@KRRT7 KRRT7 force-pushed the prepare-java-support-2 branch from 7f96d61 to 7b8169d Compare March 2, 2026 19:13
@KRRT7 KRRT7 closed this Mar 2, 2026
@KRRT7 KRRT7 deleted the prepare-java-support-2 branch March 3, 2026 00:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants