Commit graph

1611 commits

Author SHA1 Message Date
Sarthak Agarwal
5baccd4139
Merge pull request #1942 from codeflash-ai/fix/esm-module-path-extension
Fix ERR_MODULE_NOT_FOUND for TypeScript/ESM imports
2026-04-01 20:50:59 +05:30
Codeflash Bot
03df3da805 Fix ERR_MODULE_NOT_FOUND for TypeScript/ESM imports
## Problem
Codeflash was generating import statements without file extensions for
TypeScript and ESM projects, causing ERR_MODULE_NOT_FOUND errors when
Node.js tried to resolve the modules.

Example error from trace 08d0e99e-10e6-4ad2-981d-b907e3c068ea:
```
Error [ERR_MODULE_NOT_FOUND]: Cannot find module
'/workspace/target/packages/microservices/server/server-factory'
imported from .../test_create__unit_test_0.test.ts
```

The generated test had:
```typescript
import ServerFactory from '../../server/server-factory'
```

But Node.js ESM requires explicit file extensions.

## Root Cause
The get_module_path method in JavaScriptSupport was unconditionally
removing file extensions with .with_suffix(""), regardless of whether
the project used ESM or CommonJS module system.

## Solution
Modified get_module_path to:
1. Detect the module system using detect_module_system()
2. For ESM or TypeScript files: add .js extension (TypeScript convention)
3. For CommonJS: keep no extension (backward compatible)

TypeScript convention is to use .js extension in imports even when the
source file is .ts, as imports reference the compiled output.

## Testing
- Added two new test cases in TestGetModulePath class
- All 73 existing JavaScript support tests pass
- All 28 module system tests pass
- Lint and type checks pass

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-04-01 14:12:56 +00:00
HeshamHM28
20f76a30dd fix(test): move --no-pr before optimize subcommand in e2e tracer test
--no-pr is a top-level codeflash flag, not an optimize subcommand flag.
Placing it after optimize caused it to be passed to the JVM as an
unrecognized option.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-01 06:56:27 +00:00
Ubuntu
c9c5f9d035 Merge branch 'main' into java-config-redesign
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-01 06:49:21 +00:00
HeshamHM28
aeeca5c241 fix(java): find mvnw in parent dirs and respect --no-pr in tracer path
- Walk up parent directories when looking for mvnw wrapper, fixing
  multi-module projects where mvnw is in the root but optimizer runs
  from a submodule
- Respect user's --no-pr flag in Java tracer path instead of hardcoding
  no_pr=True, allowing PR creation from tracer-based optimizations
- Add --no-pr to e2e tracer test script

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-01 06:46:44 +00:00
Codeflash Bot
83bd04e0a9 Fix: Exclude property getters/setters from function discovery
**Problem:**
When functions are defined as property getters/setters (e.g., inside
Object.defineProperty with `get: function getrouter() {...}`), they were
being discovered as optimizable functions. This caused test generation
to fail because:

1. Tests tried to call the getter function directly (e.g., `obj.getrouter()`)
2. But getters are not accessible by function name - only via property access
3. This resulted in "TypeError: Cannot read properties of undefined (reading 'bind')"

**Root Cause:**
The tree-sitter function discovery in `treesitter.py` was finding all
function_expression nodes, including those used as property getters/setters.
These are not standalone functions and cannot be called directly.

**Solution:**
Added a check in `_walk_tree_for_functions` to exclude function_expression
nodes that are:
- Inside a `pair` node (key-value pair in object literal)
- Where the key is "get" or "set" (property accessor pattern)

**Affected Trace IDs (from logs):**
- 11b5475c-91d9-43b1-a317-33b7f4811c52 (init.getrouter)
- 78d8f1de-541a-4208-85c7-77194c70ec72 (createETagGenerator.generateETag)
- And others with similar patterns

**Testing:**
- Added comprehensive test suite in test_property_getter_exclusion.py
- Verified fix on actual Express.js application.js (getrouter no longer discovered)
- All existing JavaScript function discovery tests pass
- Linting (prek) passes

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-04-01 01:58:33 +00:00
HeshamHM28
944a6a972f
Merge pull request #1913 from codeflash-ai/cf-fix-multiline-instrumentation
fix: handle multi-line function calls in behavior instrumentation
2026-03-31 09:06:29 +02:00
ali
5b84811ff1
small fixes 2026-03-31 08:24:31 +02:00
ali
e1860a5637
feat: early exit on JS setup failures and introduce SetupError type
setup_test_config now returns bool so the optimizer aborts when JS
requirements (Node, npm, test framework) are missing instead of
silently continuing. Adds SetupError dataclass, fixes warning log to
print messages, and updates tests.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-31 08:04:00 +02:00
aseembits93
ec302cc596 fix: replace remaining uv tool install refs with uv pip install
Follow-up to #1909 — the npm package now installs into a dedicated venv
instead of using uv tool, but these references were missed.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-30 15:26:07 -07:00
HeshamHM28
5add169ba9
Merge branch 'main' into java-config-redesign 2026-03-27 14:10:00 +02:00
mashraf-222
928cbfbcb8
Merge pull request #1906 from codeflash-ai/cf-java-zero-config-strategy
[Feat] Java Auto Config
2026-03-27 12:16:10 +02:00
Kevin Turcios
66f1dab2ba Remove python_version from API payloads
Use language_version exclusively — the backend now resolves
python_version from language_version for backward compatibility.
2026-03-27 02:53:22 -05:00
HeshamHM28
94d9ff9d8b test: use full string equality for multi-line instrumentation tests
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-27 05:30:04 +00:00
HeshamHM28
e1478caaba fix: handle multi-line function calls in behavior instrumentation
wrap_target_calls_with_treesitter() operated line-by-line but tree-sitter
byte offsets span multiple lines. Multi-line calls like:
  func(arg1,
       arg2);
only had the first line replaced, leaving orphaned continuation lines
that caused compilation errors (80% of Spring AI functions skipped).

Rewrote to operate on the full body text with pre-computed character
offsets, processing calls back-to-front — same approach as
_add_timing_instrumentation which never had this bug.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-27 05:24:18 +00:00
Sarthak Agarwal
32575780bd
Merge pull request #1898 from codeflash-ai/cf-fix-skip-object-literal-methods
fix: skip object literal methods during JS/TS function discovery
2026-03-26 23:53:05 +05:30
Sarthak Agarwal
11632e75ae
Merge pull request #1901 from codeflash-ai/cf-fix-imports-inside-blocks
fix: convert import statements inside function bodies to require() calls
2026-03-26 23:51:26 +05:30
Ubuntu
c5b3687d52 refactor: remove zero-config logic from java-config-redesign branch
Zero-config Java support (auto-detection from build files, codeflash.toml
elimination) is handled separately in cf-java-zero-config-strategy. This
commit strips those changes, keeping only bug fixes:
- JFR parser, ReplayHelper, instrumentation, replay tests
- Multi-module test root resolution
- JUnit 4/5 test framework detection
- add_help=False for optimize subparser

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-26 07:39:29 +00:00
Ubuntu
d2e99e2f51 feat: add JaCoCo deps, fix checkstyle skip properties, and add auto-config integration tests
Add JaCoCo runtime and CLI dependencies to Gradle build. Split Maven validation
skip properties into true/false groups so failOnViolation flags are set to false
instead of true. Add Gradle wrapper and integration tests for Java auto-config.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-26 07:31:53 +00:00
HeshamHM28
b67bf0b6a8 fix tests 2026-03-26 08:42:00 +02:00
HeshamHM28
e7d07d073f Auto config 2026-03-26 06:38:33 +02:00
Sarthak Agarwal
d5f82bb48b
Merge pull request #1897 from codeflash-ai/cf-fix-tsx-syntax-validation
fix: use file-aware parser in validate_syntax for TSX support
2026-03-26 04:23:18 +05:30
mohammed ahmed
607200ab03
Merge pull request #1896 from codeflash-ai/cf-fix-formatter-temp-file-extension
fix: pass language to format_generated_code for correct temp file extension
2026-03-26 00:26:38 +02:00
mohammed ahmed
bd0e96f2c0
Merge pull request #1900 from codeflash-ai/cf-fix-monorepo-module-resolution
fix: add monorepo root node_modules to Jest moduleDirectories
2026-03-25 23:54:20 +02:00
ali
c9855e419b
fix: convert import statements inside function bodies to require() calls
AI-generated tests sometimes place `import X from 'Y'` inside jest.mock()
callbacks, describe() blocks, or other function bodies. This is invalid
JavaScript syntax (import must be top-level). Add a post-processing step
that converts indented import statements to equivalent require() calls.

Affects ~79/1026 Strapi log files showing "import/export cannot be used
outside of module code" SWC syntax errors.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-25 20:15:21 +02:00
ali
1a5216ed54
fix: add monorepo root node_modules to Jest moduleDirectories
In monorepo setups (e.g., Strapi), workspace packages are hoisted to the
root node_modules. When Jest runs from a subpackage, it can't resolve
these packages because its default moduleDirectories only includes the
local node_modules. This adds the monorepo root's node_modules to
moduleDirectories in the runtime Jest config.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-25 19:05:20 +02:00
ali
8b7ebee5fa
fix: skip object literal methods during JS/TS function discovery
Shorthand method definitions inside object literals (e.g., `{ encrypt(data) {} }`)
were being discovered as optimizable functions. Since bare method syntax is only
valid inside class bodies or object literals, extracting them as standalone code
produced syntactically invalid JavaScript, failing context extraction.

Now skip method_definition nodes whose parent is an `object` node, matching
the existing skip for arrow functions in `pair` nodes.

Trace IDs: 04f07244, 01ac202f, 024a3d42, 04da127a (~274 affected logs)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-25 18:44:08 +02:00
ali
9664307d59
fix: use file-aware parser in validate_syntax for TSX support
TypeScriptSupport.validate_syntax() always used the TYPESCRIPT parser,
which cannot parse JSX syntax. TSX files (.tsx) containing JSX were
incorrectly rejected as syntactically invalid, blocking optimization
of React components.

Added optional file_path parameter to validate_syntax across all
language support classes. When provided, the correct parser is selected
based on file extension (e.g., TSX parser for .tsx files).

Trace IDs: 00c25f79, 02697f98, fdfc6a8d (113 affected logs total)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-25 18:38:21 +02:00
ali
f95a06afe6
fix: pass language to format_generated_code for correct temp file extension
format_generated_code was called without the language parameter in
process_review(), defaulting to "python". This created temp files with
.py extension when formatting JS/TS code, causing prettier to fail.

Trace IDs: 11e9745d, 1578f081, 7e8abab2 (73 affected logs total)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-25 18:05:28 +02:00
claude[bot]
2857e84790 fix: remove invalid get_shell_rc_path patches from auth tests
Co-authored-by: Aseem Saxena <undefined@users.noreply.github.com>
2026-03-25 00:55:39 +00:00
aseembits93
9ce18aca33 feat: add auth login subcommand for standalone OAuth authentication
Adds `codeflash auth login` CLI subcommand that performs the full OAuth
PKCE flow, prints the authentication URL, and saves the API key.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-24 13:44:16 -07:00
Mohamed Ashraf
5942ae9e16 fix: prefer closer config file over parent Java build file in monorepos (TODO-37)
Java detection in parse_config_file() short-circuited before the existing
depth-comparison logic, so a parent pom.xml would override a closer
package.json or pyproject.toml. Now all config sources are detected first
and the closest one to CWD wins.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-24 16:50:18 +00:00
Mohamed Ashraf
3c63b60ae4 fix: preserve pom.xml formatting in config writer and align write/remove priority
Replace xml.etree.ElementTree with text-based regex manipulation in
_write_maven_properties() and _remove_java_build_config(). ElementTree
destroys XML comments, mangles namespace declarations (ns0: prefixes),
and reformats whitespace. The new approach reads/writes pom.xml as plain
text, only touching codeflash.* property lines.

Also extracts duplicated key_map to shared _MAVEN_KEY_MAP constant and
aligns remove priority to check pom.xml first (matching write order).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-24 16:30:22 +00:00
Kevin Turcios
c72945b045 feat: early dedup of optimization candidates before benchmark loop
Dedup candidates in CandidateProcessor when each batch arrives (initial,
line profiler, repair, refinement, adaptive) instead of only catching
duplicates one-by-one during the benchmark loop.

Changes:
- Add dedup_candidates() to CandidateProcessor with persistent
  seen_normalized set that tracks queued candidates across batches
- Simplify handle_duplicate_candidate/register_new_candidate to accept
  original_flat_code string instead of full CodeOptimizationContext
- 14 unit tests covering all dedup paths
2026-03-22 05:19:15 -05:00
misrasaurabh1
13dae81bd2 fix: increase JFR sampling frequency and make Workload exercise functions harder
- Set jdk.ExecutionSample#period=1ms (default was 10ms) so JFR captures
  samples from shorter-running programs
- Workload.main now runs 1000 rounds with larger inputs so JFR can
  capture method-level CPU samples (repeatString with O(n²) concat
  dominates ~75% of samples)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-19 23:50:22 -07:00
misrasaurabh1
be616d1d1f fix: flush e2e test output to CI logs in real-time
Use print(flush=True) instead of logging.info for subprocess output so
CI logs show progress in real-time instead of buffering until completion.
Also set PYTHONUNBUFFERED=1 for the subprocess.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-19 23:05:16 -07:00
misrasaurabh1
74cbe2aba6 fix: Windows compatibility for Java config detection tests
- Use Path comparisons instead of forward-slash substring matching
- Avoid parse_args() in test (reads stdin on Windows) — use Namespace directly

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-19 22:58:48 -07:00
misrasaurabh1
0fc5bba763 fix: e2e java tracer runs on all codeflash changes and validates replay tests + speedups
- Trigger on any codeflash/** or tests/** changes (not just java subset)
- Validate replay test files are discovered per-function
- Already validates: replay test generation, global discovery count,
  optimization success, and minimum speedup percentage

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-19 22:34:56 -07:00
misrasaurabh1
d441bb9761 fix: properly instrument replay tests instead of skipping them
The behavior instrumentation was producing malformed output for compact
@Test lines (annotation + method signature on same line, common in
replay tests). The method signature collection loop would skip past the
opening brace and consume subsequent methods' content.

Fix: detect when the @Test annotation line already contains { and treat
it as both annotation and method signature, avoiding the separate
signature search that was over-consuming lines.

Reverted the instrumentation skip for replay tests — they now get
properly instrumented for both behavior capture and performance timing.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-19 20:10:20 -07:00
misrasaurabh1
b0d4a5e8bf test: add tests for JFR parser, graceful timeout, and project root resolution
13 new tests covering:
- JFR class name normalization (/ to . conversion)
- Package-based sample filtering
- Addressable time calculation from JFR samples
- Method ranking order and format
- Graceful timeout (SIGTERM before SIGKILL)
- Multi-module project root detection (Path not str)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-19 19:43:31 -07:00
misrasaurabh1
721655fdd1 test: add tests for JUnit 4 support, overload handling, instrumentation skip
10 new tests covering:
- JUnit 5 replay test generation (imports, class visibility)
- JUnit 4 replay test generation (imports, public methods, @AfterClass)
- Overloaded method handling (no duplicate test method names)
- Instrumentation skip for replay tests (behavior + perf mode)
- Regular tests still get instrumented normally

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-19 19:39:25 -07:00
misrasaurabh1
3f95ff604a feat: eliminate codeflash.toml — auto-detect Java config from build files
Java projects no longer need a standalone config file. Codeflash reads
config from pom.xml <properties> or gradle.properties, and auto-detects
source/test roots from build tool conventions.

Changes:
- Add parse_java_project_config() to read codeflash.* properties from
  pom.xml and gradle.properties
- Add multi-module Maven scanning: parses each module's pom.xml for
  <sourceDirectory> and <testSourceDirectory>, picks module with most
  Java files as source root, identifies test modules by name
- Route Java projects through build-file detection in config_parser.py
  before falling back to pyproject.toml
- Detect Java language from pom.xml/build.gradle presence (no config needed)
- Fix project_root for multi-module projects (was resolving to sub-module)
- Fix JFR parser / separators (JVM uses com/example, normalized to com.example)
- Fix graceful timeout (SIGTERM before SIGKILL for JFR dump + shutdown hooks)
- Remove isRecording() check from TracingTransformer (was preventing class
  instrumentation for classes loaded during serialization)
- Delete all codeflash.toml files from fixtures and code_to_optimize
- Add 33 config detection tests
- Update docs for zero-config Java setup

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-19 19:11:06 -07:00
Saurabh Misra
59031a145e
Merge pull request #1874 from codeflash-ai/java-tracer
feat: Java tracing agent with end-to-end optimization pipeline
2026-03-19 15:12:11 -07:00
misrasaurabh1
3b396578d7 make workload purposefully terrible 2026-03-19 12:22:26 -07:00
Sarthak Agarwal
668c6d895d
Merge pull request #1878 from codeflash-ai/fix/bug_lang_detection
fix on singleton class returning python as current lang always
2026-03-20 00:16:42 +05:30
claude[bot]
0a71905be8 test: update git_diff tests to match new all-extensions behavior
get_git_diff now uses all registered extensions instead of filtering
by current language, so update tests to reflect the new behavior.

Co-authored-by: Sarthak Agarwal <undefined@users.noreply.github.com>
2026-03-19 18:07:19 +00:00
misrasaurabh1
c31f83726a fix: ensure src/test/java directory exists before config validation
Git doesn't track empty directories, so src/test/java must be created
before process_pyproject_config validates tests-root exists.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 23:19:44 -07:00
misrasaurabh1
0a83002555 fix: Java tracer e2e test script for CI compatibility
- Use `uv run -m codeflash.main` instead of direct file path
- Remove redundant --no-pr (already hardcoded in _run_java_tracer)
- Clean up leftover replay tests between retry attempts
- Add error logging for subprocess output

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-18 23:17:27 -07:00
misrasaurabh1
c699093a26 Initial e2e tracer implementation 2026-03-18 23:03:35 -07:00
Kevin Turcios
aae0171b1c Merge branch 'main' into fix/attrs-init-instrumentation 2026-03-18 18:14:33 -06:00
claude[bot]
4c9abfb2aa refactor: remove redundant try/finally; rely on conftest autouse fixture for language cleanup
The conftest.py autouse fixture already resets _current_language before/after
each test, making per-test try/finally cleanup unnecessary.

Co-authored-by: Kevin Turcios <KRRT7@users.noreply.github.com>
2026-03-18 18:23:49 +00:00
claude[bot]
0672e11342 fix: reset language singleton in test to prevent cross-test pollution
test_parse_line_profile_results_non_python_java_json set Language.JAVA
but never reset it, causing test_java_diff_ignored_when_language_is_python
to fail when tests ran in this order.

Co-authored-by: Kevin Turcios <KRRT7@users.noreply.github.com>
2026-03-18 17:34:03 +00:00
Kevin Turcios
115cdba481 fix: address review feedback for attrs init instrumentation
- Fix bug: skip attrs classes with init=False (no __init__ to patch)
- Deduplicate attrs namespace/name sets into shared constants
- Fix _get_attrs_config to resolve import aliases properly
- Add test for init=False case with exact expected output
2026-03-18 03:34:44 -06:00
Kevin Turcios
0cb8a08c7e feat: instrument attrs __init__ via module-level monkey-patch wrapper
Instead of skipping attrs classes entirely (previous approach), emit a
module-level patch block immediately after the class definition:

  _codeflash_orig_ClassName_init = ClassName.__init__
  def _codeflash_patched_ClassName_init(self, *args, **kwargs):
      return _codeflash_orig_ClassName_init(self, *args, **kwargs)
  ClassName.__init__ = codeflash_capture(...)(_codeflash_patched_ClassName_init)

This sidesteps the __class__ cell TypeError that attrs(slots=True) triggers
when a synthetic super().__init__() body is injected into the original class,
because the patched wrapper is a plain module-level function with no __class__
cell.

Changes:
- InitDecorator.__init__: add _attrs_classes_to_patch dict
- visit_ClassDef: for attrs classes, record (name -> decorator) instead of
  returning immediately; set inserted_decorator=True
- visit_Module: splice patch block statements after each attrs ClassDef
- _build_attrs_patch_block: new helper that builds the 3-statement AST block
- Tests: rename *_no_init_skipped -> *_patched_via_module_wrapper and update
  expected strings to assert the exact generated patch block

Co-Authored-By: Oz <oz-agent@warp.dev>
2026-03-18 01:42:29 -06:00
Kevin Turcios
dd5e347bbb fix: skip attrs classes in __init__ instrumentation; add attrs support to code_context_extractor
- instrument_codeflash_capture: detect @attrs.define / @attr.s / etc. in the
  'no explicit __init__' branch and return early, same as dataclass/NamedTuple.
  Prevents a TypeError caused by attrs(slots=True) creating a new class whose
  __class__ cell no longer matches the injected super().__init__ wrapper.

- code_context_extractor: add _get_attrs_config() helper; update
  _collect_synthetic_constructor_type_names, _build_synthetic_init_stub, and
  _extract_synthetic_init_parameters to handle attrs field conventions
  (factory= keyword, init=False, kw_only).

- tests: add 3 exact-output tests for instrumentation skip behaviour and
  3 exact-output tests for attrs stub generation.

Co-Authored-By: Oz <oz-agent@warp.dev>
2026-03-18 01:33:40 -06:00
HeshamHM28
2a35716864
Merge branch 'main' into feat/gradle-executor-from-java 2026-03-18 06:14:47 +02:00
HeshamHM28
ba36a1f7b2 fix jar file problems 2026-03-18 05:47:56 +02:00
HeshamHM28
6b2a852d79 fix running tests 2026-03-18 05:29:00 +02:00
HeshamHM28
cd0e19396c fix multi_root 2026-03-18 04:24:16 +02:00
Kevin Turcios
948bfedfa0
Merge pull request #1852 from codeflash-ai/cf-1846-port-perf-improvements
perf: cache jedi project, batch test cache writes, fix Windows relative_to bug
2026-03-17 18:54:31 -06:00
Kevin Turcios
bf9adf2673 fix: normalize module fallback formatting for import merge 2026-03-17 18:16:54 -06:00
Aseem Saxena
506eb44648
Merge pull request #1855 from codeflash-ai/fix/git-diff-multi-language-extension-filter
fix: git diff auto-detection filters by current language instead of hardcoding .py
2026-03-17 17:02:22 -07:00
Mohamed Ashraf
bc7a5bf4bb fix: output structured XML errors in subagent mode
When codeflash runs with --subagent (e.g., via the Claude Code plugin),
exit_with_message() now outputs <codeflash-error> XML to stdout instead
of Rich panel text. This lets the calling agent parse errors
programmatically rather than receiving unstructured text.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-17 18:48:17 +00:00
Mohamed Ashraf
87eedac002 fix: git diff auto-detection filters by current language instead of hardcoding .py
get_git_diff() hardcoded `.py` as the only valid file extension, causing
the auto-detect flow (no --file flag) to return 0 functions for Java,
JavaScript, and TypeScript projects. This broke the Claude Code plugin
integration where the hook runs `codeflash --subagent` without --file.

Now uses current_language_support().file_extensions to filter by the
active language's extensions dynamically.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-17 18:40:41 +00:00
mohammed ahmed
1e92f3d2ed
Merge pull request #1814 from codeflash-ai/fix/js-replacement-stale-line-numbers-after-global-declarations
fix: re-discover function position after add_global_declarations shifts line numbers
2026-03-17 17:17:51 +02:00
ali
3bbaf26008
test: add unit tests for node_modules symlink in JS worktree setup
Covers setup_test_config symlinking node_modules from original repo
to worktree, including edge cases (no worktree, missing node_modules,
already existing node_modules).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-17 16:54:34 +02:00
ali
1282a103cd
Merge branch 'main' of github.com:codeflash-ai/codeflash into fix/js-replacement-stale-line-numbers-after-global-declarations 2026-03-17 15:11:40 +02:00
claude[bot]
8dc6d9eeda fix: remove test for deleted create_pyproject_toml function
The function was removed in the dead code cleanup but the test file still
imported it and had a TestCreatePyprojectToml class, causing ImportError.

Co-authored-by: Kevin Turcios <undefined@users.noreply.github.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-17 07:48:19 +00:00
Kevin Turcios
a0a2a85020
Merge pull request #1660 from codeflash-ai/unstructured-inference
feat: improve function ranking with reference graph and test-based boosting
2026-03-16 23:05:28 -06:00
misrasaurabh1
6c3f626c9e fix: filter Java runtime annotations by class name and fix ordering
Runtime annotations in PR descriptions were broken in two ways:
1. add_runtime_comments() ignored class/method prefixes in keys, causing
   annotations from unrelated test classes to leak across files and sum
   incorrectly at the same line number. Now filters by class names found
   in each test source file.
2. Test functions were removed before annotations were added, shifting
   line numbers so annotations landed on wrong lines. Swapped ordering
   so annotations are applied first, then function removal carries them
   along correctly.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-16 17:51:57 -07:00
HeshamHM28
16b3c2f5e5 Merge branch 'main' into feat/gradle-executor-from-java 2026-03-16 23:45:14 +02:00
Kevin Turcios
2cafadb980 fix: deduplicate test count calls, guard None, and log effort escalation
Build test_count_cache once before ranking instead of calling
existing_unit_test_count O(2N) times. Guard for None function_to_tests
and add debug logging when effort is escalated from medium to high.
2026-03-16 14:41:55 -06:00
Kevin Turcios
5671562da2 perf: eliminate redundant CST parsing in get_code_optimization_context
Parse each file once instead of up to 16 times by:
- Making remove_unused_definitions_by_function_names accept/return cst.Module
- Making parse_code_and_prune_cst and add_needed_imports_from_module accept cst.Module
- Threading the parsed Module through process_file_context
- Adding extract_all_contexts_from_files that processes all 4 context types
  (READ_WRITABLE, READ_ONLY, HASHING, TESTGEN) in a single per-file pass
2026-03-16 10:11:58 -06:00
Kevin Turcios
282f2ba713 Improve testgen constructor context extraction 2026-03-16 00:47:17 -06:00
Kevin Turcios
cee12fe430 fix ranking boost ordering and statement helper extraction 2026-03-15 23:29:35 -06:00
Kevin Turcios
01847f9acc fix: auto-detect language_version in add_language_metadata when not provided
review_generated_tests and repair_generated_tests called add_language_metadata
without language_version, sending python_version=None to the API which rejects
with "Python version is required". Now falls back to current_language_support().
2026-03-14 19:16:01 -06:00
Kevin Turcios
a90cda2578 feat: boost ranking for tested functions and enable reference graph
- Add existing_unit_test_count() with parametrized test deduplication
- Stable-sort ranked functions so tested ones come first
- Enable reference graph resolver (was disabled) for non-CI runs
- Add per-function logging with ref count and test count
- Auto-upgrade top N functions to high effort when user hasn't set --effort
- Add CallGraph model with traversal (BFS, topological sort, subgraph)
- Add get_call_graph() to DependencyResolver protocol and ReferenceGraph
- Refactor get_callees() to delegate through get_call_graph()

CF-1660
2026-03-14 18:40:08 -06:00
Mohamed Ashraf
fa9d32f1c4 Merge branch 'main' into omni-java
Resolve 7 merge conflicts from main's modular refactoring + JS improvements:

- aiservice.py: combine multi-language metadata (omni-java) with main's structure
- cmd_init.py: adopt main's modular split (init_config, init_auth, github_workflow) + add Java import
- code_replacer.py: main's clean early-return style + omni-java's non-Python single-block fallback
- version.py, test_support_dispatch.py, test_javascript_test_runner.py: take main's versions
- uv.lock: regenerated

Port Java into main's modular structure:
- Fix init_java.py lazy imports to point to new modules (init_config, init_auth, github_workflow)
- Add Java workflow support to github_workflow.py (detection, template, customization)
- Fix broken Java imports (function_optimizer, line_profiler) after main's module moves

Add safety tests for merge-critical functions:
- test_add_language_metadata.py: 10 tests covering per-language payload correctness
- test_code_replacer_matching.py: 8 tests covering fallback chain

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-13 00:15:19 +00:00
Mohamed Ashraf
068c1a73d0 test: add unit tests for detect_project_language
Cover all detection paths: Java (pom.xml, build.gradle, build.gradle.kts),
TypeScript, JavaScript, Python, empty directory fallback, and priority
resolution when multiple build system markers coexist.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-12 04:01:49 +00:00
mohammed ahmed
d616501871
Merge pull request #1808 from codeflash-ai/fix/jest-runtime-config-for-external-tests
fix: use runtime Jest config for test files outside project root
2026-03-11 22:34:10 +02:00
ali
6a451ffe03
fix: re-discover function position after add_global_declarations shifts line numbers
When optimized JS/TS code introduces new global declarations (const, Set, etc.),
add_global_declarations inserts them into the original source, shifting the target
function's line numbers. The stale starting_line from function_to_optimize then
causes _replace_function_body to fail with "Could not find function X at line Y".

This affected ~666 function replacements in the Strapi optimization run, including
bytesToHumanReadable, isSelectable, getFileIconComponent, and many others.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-11 21:30:10 +02:00
Saurabh Misra
7f7591cf29
Merge pull request #1813 from codeflash-ai/fix/detect-deletion-only-diffs
fix: detect functions in deletion-only git diffs
2026-03-11 01:19:21 -04:00
aseembits93
b5a01457d2 test: add unit tests for deletion-only git diff detection
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-10 17:33:40 -07:00
Kevin Turcios
748094c7e0 Merge remote-tracking branch 'origin/main' into fix-dependabot-vulns 2026-03-10 16:54:29 -06:00
ali
eeeb6eebf8
test: update Jest roots tests to verify runtime config behavior
Update TestJestRootsConfiguration to match the new runtime config
approach: verify no --roots/config when tests are inside the project
root, and verify runtime config creation when tests are outside it.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-10 17:18:16 +02:00
HeshamHM28
1ec829daff 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
HeshamHM28
3aea9397fe Refactor Maven strategy for Codeflash integration
- Added functions to install the Codeflash runtime and manage its dependency in the Maven POM file.
- Implemented JaCoCo plugin addition and configuration for test coverage.
- Enhanced XML parsing for POM files to support both namespaced and non-namespaced formats.
- Updated test cases to reflect changes in dependency management and plugin configuration.
- Removed obsolete runtime jar finding logic and consolidated module detection for both Maven and Gradle projects.
- Improved input validation for test run commands.
2026-03-10 04:14:55 +02:00
HeshamHM28
f4a4ac633f Refactor Java test runner tests to use MavenStrategy
- Updated tests in `test_java_multimodule_deps_install.py` to utilize `MavenStrategy` for installing multi-module dependencies.
- Changed function calls from `ensure_multi_module_deps_installed` to `MavenStrategy.install_multi_module_deps`.
- Added a fixture for `MavenStrategy` to streamline test setup.
- Modified assertions and mock setups to align with the new strategy implementation.

- Refactored tests in `test_java_test_filter_validation.py` to replace `_run_maven_tests` with `MavenStrategy.run_tests_via_build_tool`.
- Adjusted test cases to ensure proper handling of empty and valid test filters.
- Updated mock setups for Maven executable and command execution to reflect changes in the strategy.
2026-03-09 23:30:18 +02:00
claude[bot]
dea671073e test: skip reporter junit xml test on Windows CI
Node.js subprocess pipe behavior causes the test to hang on Windows
(returncode=1 but stdout reader thread blocks beyond 10s timeout).

Co-authored-by: Aseem Saxena <aseembits93@users.noreply.github.com>
2026-03-09 19:10:57 +00:00
Sarthak Agarwal
222805730b
Merge pull request #1778 from codeflash-ai/fix/github_actions_init
[Fix] Github Actions init
2026-03-07 23:58:13 +05:30
Kevin Turcios
f43ee06859 refactor: restructure codebase for locality and faster CLI startup
Move files closer to their consumers:
- function_context.py merged into code_context_extractor.py
- FunctionOptimizer base class to languages/function_optimizer.py
- test_runner, instrument_codeflash_capture, parse_line_profile to languages/python/
- oauth_handler.py to cli_cmds/

Split cmd_init.py (1993 lines) into focused modules:
- init_config.py: config types, validation, writing, shared UI
- init_auth.py: API key management + GitHub app installation
- github_workflow.py: GitHub Actions workflow generation
- cmd_init.py: init orchestrator + Python setup (639 lines)

Defer heavy imports (cmd_init, posthog, sentry) from module-level to
usage sites, reducing CLI startup from ~600ms to ~250ms. Replace
set_defaults(func=) with direct args.command dispatch in main().
2026-03-07 08:21:27 -05:00
Kevin Turcios
2fec18c65b fix: build full dotted name in _expr_name for module-qualified decorators/bases
_expr_name now recurses into ast.Attribute to produce the full dotted
path (e.g. "dataclasses.dataclass", "typing.NamedTuple"). Callers use
.endswith() so both bare and module-qualified forms are matched. Adds
test for typing.NamedTuple base class.
2026-03-07 03:32:25 -05:00
Kevin Turcios
9fd5a3d93f fix: address PR review — recurse _expr_name for call-style decorators, guard empty-set superset
Add tests for remove_test_functions qualified name support and
module-qualified dataclass decorator handling.
2026-03-07 03:03:23 -05:00
Kevin Turcios
e33a7da615 Merge branch 'main' into testgen-review 2026-03-06 17:20:42 -05:00
Sarthak Agarwal
417623f930
Merge pull request #1780 from codeflash-ai/fix/normalizer
[Fix] Normalizer and expand its scope
2026-03-07 01:55:44 +05:30
Sarthak Agarwal
3367383824 fix failing unit tests with recent refactoring 2026-03-07 01:48:41 +05:30
Sarthak Agarwal
353feab063 [Fix] Normalizer and expand its scope 2026-03-06 21:31:24 +05:30
Sarthak Agarwal
123067571a add tests 2026-03-06 16:27:46 +05:30
Kevin Turcios
6473145616 fix: skip NamedTuple classes in __init__ instrumentation
NamedTuples have a synthesized __init__ that cannot be overwritten.
The instrumentation was missing a skip check (like the existing one
for dataclasses), causing "Cannot overwrite NamedTuple attribute
__init__" errors that crashed the test subprocess and produced 0%
coverage.

Also removes duplicate docstring in make_ai_service_request.
2026-03-06 02:05:09 -05:00
Kevin Turcios
9c1fb4a397 fix: use PythonFunctionOptimizer in test_mock_candidate_replacement
Base FunctionOptimizer.get_code_optimization_context() raises
NotImplementedError — tests need the Python-specific subclass.
2026-03-06 01:03:01 -05:00
Kevin Turcios
e01236eb29 Merge branch 'main' into testgen-review 2026-03-06 01:02:06 -05:00
Kevin Turcios
5d872e845d
Merge pull request #1650 from codeflash-ai/fix/unused-helper-attribute-refs
fix: detect attribute-referenced methods as used in unused helper detection
2026-03-05 23:55:33 +00:00
Mohamed Ashraf
50957395a9 feat: centralize JAR version, cache runtime setup, add pom.xml backup/restore
- Extract CODEFLASH_RUNTIME_VERSION and CODEFLASH_RUNTIME_JAR_NAME constants
  in build_tools.py, replacing 15+ hardcoded "1.0.0" references across
  test_runner.py, comparator.py, and line_profiler.py
- Cache _ensure_codeflash_runtime() results so it runs once per optimization
  instead of 3 times (behavioral, benchmarking, line profiling phases)
- Add backup_pom/restore_pom/restore_all_pom_backups to build_tools.py so
  pom.xml modifications (codeflash-runtime dependency, JaCoCo plugin) are
  always reverted after optimization completes, even on crashes
- Call restore_all_pom_backups() in function_optimizer.py's finally block

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-05 23:17:09 +00:00
Kevin Turcios
6330005999 test: update expected markdown ordering to match target-file-first change 2026-03-05 07:04:03 -05:00
claude[bot]
6ad7ea49f6 fix: add missing -> None return type annotations to test functions
Co-authored-by: Kevin Turcios <undefined@users.noreply.github.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-05 11:18:54 +00:00
Kevin Turcios
1bcea313a0 test: add unit tests for get_optimized_code_for_module fallback logic 2026-03-05 06:06:21 -05:00
Sarthak Agarwal
ae6b1a3b5f
Merge branch 'main' into fix/js-vitest-benchmarking-and-mocha-cjs 2026-03-04 16:49:43 +05:30
Sarthak Agarwal
f0e4e5326d max loop count in test support 2026-03-04 16:48:38 +05:30
Kevin Turcios
f9e7f2a82a fix: skip codeflash_capture instrumentation for dataclasses without explicit __init__
Dataclass __init__ is auto-generated at class creation time and not
present in the AST. The instrumentor was injecting a synthetic __init__
with super().__init__(*args, **kwargs) which calls object.__init__()
and fails because dataclass fields are passed as kwargs.

Now only skips when the class is a @dataclass AND has no explicit
__init__. Dataclasses with custom __init__ are still instrumented.
2026-03-04 04:47:30 -05:00
misrasaurabh1
df57235d25 test: use full string equality in Java runtime comments tests
Replace substring assertions (e.g. `"// 2.89ms ->" in lines[7]`) with
exact full-output comparisons for better regression detection.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-04 01:02:24 -08:00
misrasaurabh1
9367a86771 fix: remove dead Java path-fixing code from base FunctionOptimizer
The base class had duplicate _get_java_sources_root and _fix_java_test_paths
methods that were overridden by JavaFunctionOptimizer. The base class also
had an is_java() block in generate_and_instrument_tests that used undefined
variables (used_behavior_paths, is_java). Removed all dead code since
JavaFunctionOptimizer.fixup_generated_tests handles this properly.

Also updated JavaFunctionOptimizer._fix_java_test_paths to accept
display_source parameter and use whole-word rename for collision handling.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-04 01:02:24 -08:00
misrasaurabh1
6ec4db446c Merge remote-tracking branch 'origin/omni-java' into generated-tests-in-pr
# Conflicts:
#	codeflash/languages/java/instrumentation.py
#	codeflash/optimization/function_optimizer.py
#	codeflash/verification/verifier.py
#	codeflash/version.py
#	tests/test_languages/test_java/test_instrumentation.py
#	tests/test_languages/test_java/test_java_test_paths.py
2026-03-04 00:26:46 -08:00
claude[bot]
77b705d2c7 fix: use forward slashes in jest-reporter test paths for Windows compatibility
Windows backslashes in paths embedded into JavaScript strings are
interpreted as escape sequences by Node.js, corrupting the module path.
Use .as_posix() to emit forward slashes which Node accepts on all platforms.

Co-authored-by: Kevin Turcios <KRRT7@users.noreply.github.com>
2026-03-04 08:06:15 +00:00
claude[bot]
887e6384b6 fix: apply consistent conn safety pattern in trace benchmark tests
Initialize conn to None before try blocks and guard finally with
if conn is not None to prevent NameError if sqlite3.connect() raises.

Co-authored-by: Kevin Turcios <KRRT7@users.noreply.github.com>
2026-03-04 07:37:03 +00:00
Kevin Turcios
2faef8ade6 fix: update JS max_loops test assertion to match constant (1_000)
PR #1764 changed JS_BENCHMARKING_MAX_LOOPS from 100_000 to 1_000 but
the test was updated to assert 5_000 instead of 1_000.
2026-03-04 02:26:53 -05:00
claude[bot]
59ec38eec4 fix: resolve mypy type error for conn variable in test_trace_benchmarks
Initialize conn as Optional before try block to allow None assignment.

Co-authored-by: Kevin Turcios <undefined@users.noreply.github.com>
2026-03-04 07:15:22 +00:00
Kevin Turcios
17730663ec fix: close SQLite connections in finally blocks for Windows compatibility
Ensures SQLite connections are always closed before file cleanup to
prevent PermissionError on Windows where open handles block file deletion.
2026-03-04 02:12:18 -05:00
Kevin Turcios
eceac13fc3 Merge remote-tracking branch 'origin/main' into omni-java
# Conflicts:
#	.claude/rules/architecture.md
#	.claude/rules/code-style.md
#	.github/workflows/claude.yml
#	.github/workflows/duplicate-code-detector.yml
#	codeflash/api/aiservice.py
#	codeflash/cli_cmds/console.py
#	codeflash/cli_cmds/logging_config.py
#	codeflash/code_utils/deduplicate_code.py
#	codeflash/discovery/discover_unit_tests.py
#	codeflash/languages/base.py
#	codeflash/languages/code_replacer.py
#	codeflash/languages/javascript/mocha_runner.py
#	codeflash/languages/javascript/support.py
#	codeflash/languages/python/support.py
#	codeflash/optimization/function_optimizer.py
#	codeflash/verification/parse_test_output.py
#	codeflash/verification/verification_utils.py
#	codeflash/verification/verifier.py
#	packages/codeflash/package-lock.json
#	packages/codeflash/package.json
#	tests/languages/javascript/test_support_dispatch.py
#	tests/test_codeflash_capture.py
#	tests/test_languages/test_javascript_test_runner.py
#	tests/test_multi_file_code_replacement.py
2026-03-04 01:52:32 -05:00
Kevin Turcios
dbc04df9df Update test_support_dispatch.py 2026-03-04 01:32:35 -05:00
Kevin Turcios
2fb0145895 Merge remote-tracking branch 'origin/omni-java' into merge/misc-fixes-into-omni-java
# Conflicts:
#	codeflash/api/aiservice.py
#	codeflash/languages/base.py
#	codeflash/languages/java/support.py
#	codeflash/languages/javascript/support.py
#	codeflash/languages/python/support.py
#	codeflash/verification/verifier.py
2026-03-04 01:23:39 -05:00
HeshamHM28
8287f96f05
Merge pull request #1680 from codeflash-ai/feat/java/wire-language-version
feat: add language version support across multiple language implement…
2026-03-03 22:12:54 -08:00
Kevin Turcios
b43b37b6dd fix: update nested class replacement test to match PR #1726 design
Inner-class methods are intentionally skipped by Java discovery
(PR #1726) since instrumentation is name-only and not class-aware.
Update test to expect False from replacement.
2026-03-04 00:29:46 -05:00
Kevin Turcios
ca149fa2d0 fix: use relpath for main.py in E2E test utilities
Take omni-main-java's fix for E2E test runner path resolution —
uses os.path.relpath from __file__ instead of hardcoded relative path.
Also adds codeflash.toml detection for Java projects.
2026-03-04 00:19:02 -05:00
Sarthak Agarwal
12294cafb6 fix looping with JS/TS 2026-03-04 10:46:44 +05:30
Kevin Turcios
92ab600edc fix: resolve remaining test failures from main sync
- Update test_inject_profiling_used_frameworks, test_async_run_and_parse,
  test_pickle_patcher to use new inject_profiling_into_existing_test API
  (test_string param removed)
- Add parse_line_profile_results function to parse_line_profile_test_output
  module (imported by main's PythonFunctionOptimizer and test_instrument_tests)
2026-03-04 00:13:27 -05:00
Kevin Turcios
af7ce7fce2 fix: cherry-pick main improvements into omni-java branch
- Take main's JS improvements: Mocha CJS support, ESM/CJS handling,
  sanitize_mocha_imports, vitest benchmarking fixes
- Update instrument_existing_test API: remove test_string param, read from
  file internally (aligned across Python, JS, Java support classes)
- Take main's equivalence.py with pass_fail_only parameter
- Take main's models.py, critic.py, env_utils.py, replay_test.py fixes
- Take main's PythonFunctionOptimizer parse_line_profile fix
- Skip files where our branch has Java-specific additions main doesn't
  have (create_pr, discover_unit_tests, parse_test_output, optimizer,
  verification_utils, config_parser, cmd_init, detector, support.py
  protocol methods)
2026-03-03 23:59:26 -05:00
Kevin Turcios
bccc02aade merge: incorporate omni-main-java sync work
Merges the omni-main-java branch which synced main into omni-java,
including JavaFunctionOptimizer, removal of is_java()/is_python() guards,
protocol dispatch for parse_test_xml, and deletion of concolic_testing.py.
2026-03-03 23:42:39 -05:00
Kevin Turcios
c550bf1561 fix: restore QName test to match omni-java's enrich_testgen_context behavior
The cherry-picked test fix from main assumed stdlib classes are extracted,
but omni-java's implementation still skips them.
2026-03-03 22:27:44 -05:00
Kevin Turcios
a0249afc7e fix: use PythonFunctionOptimizer in tests that depend on Python-specific hooks 2026-03-03 22:22:14 -05:00
Kevin Turcios
c11f52321e fix: correct pre-existing test failures in test_code_context_extractor
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.
2026-03-03 22:22:00 -05:00
mashraf-222
82e4627b03
Merge pull request #1740 from codeflash-ai/fix/java-comparator-and-errorprone
fix: suppress Error Prone CheckReturnValue in instrumented tests and fix stale pom dependency
2026-03-04 05:21:24 +02:00
misrasaurabh1
26f2c502fb feat: add inline runtime comments and unique _cf_mod/_cf_cls markers for Java tests
- Use instrumented class name in _cf_mod/_cf_cls markers to disambiguate
  existing vs generated tests sharing the same original class name
- Encode line number in invocation IDs (L{line}_{counter}) for deterministic
  call-site identification in inline runtime comments
- Rewrite add_runtime_comments() to annotate each call line with inline
  performance data instead of a summary block at top
- Strip assertions before instrumenting so both modes share the same base source
- Update test expected strings for new marker format

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-03 18:58:20 -08:00
Kevin Turcios
ec73c2c692 revert: undo Vitest benchmarking and capture.js changes
Reverts ed2594bf which added --pool=forks to Vitest commands and
changed capture.js to use process.stdout.write and Vitest worker API.
These changes broke JS E2E tests (CJS, ESM, TS class) by altering
how all JS tests run, not just Vitest benchmarking.
2026-03-03 21:34:59 -05:00
Kevin Turcios
b2b8201541 fix: update test_get_helper_code expectation for blank line after imports 2026-03-03 21:10:35 -05:00
Kevin Turcios
fb6a4ab587 fix: update test expectations for extra blank line after imports
The CST comment position fix changed how blank lines are preserved
after import blocks, adding a PEP 8-compliant double blank line.
2026-03-03 20:58:32 -05:00
Sarthak Agarwal
f5d48841f0 fix mocha test runner 2026-03-03 20:50:50 -05:00
Sarthak Agarwal
4f2c65daec fix: strip CJS require('vitest') and require('@jest/globals') in Mocha tests
The AI backend generates vitest/jest-style imports for Mocha projects.
Our sanitize_mocha_imports() stripped ESM `import { ... } from 'vitest'`,
but process_generated_test_strings() runs BEFORE postprocessing and calls
ensure_module_system_compatibility() which converts these to CJS requires.
Result: `const { ... } = require('vitest')` survived sanitization.

Added regexes for the CJS variants of vitest and @jest/globals requires.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-03 20:49:04 -05:00
Sarthak Agarwal
74150c2a7f fix: resolve Vitest benchmarking showing wall-clock time instead of per-function timing
Root cause: Vitest performance tests reported "20.0 seconds over 1 loop"
(JUnit XML wall-clock fallback) instead of actual per-function nanosecond
timing. This was a chain of two issues:

1. **stdout interception**: Vitest's default `threads` pool intercepts
   process.stdout.write() and console.log(), preventing timing markers
   from flowing to the parent process. Fixed by adding `--pool=forks`
   to all Vitest commands and config files. The `forks` pool uses child
   processes where stdout flows directly to the parent.

2. **test name detection**: Even after markers flowed through (43,000+
   found in stdout), the parser couldn't match them to JUnit XML
   testcases because all markers had "unknown" as the test name. This
   happened because Vitest doesn't inject `beforeEach` as a global
   (unlike Jest), so capture.js's Jest-style hook to set
   `currentTestName` never fired.

   Fixed by adding Vitest-specific test name detection in capture.js:
   - Primary: `expect.getState().currentTestName` (full describe path)
   - Fallback: `__vitest_worker__.current.fullTestName`
   - Defense-in-depth: parser fallback matches "unknown" markers to
     the first testcase when no name match is found

Result: cheerio's `isHtml` went from "20.0s / 1 loop" to
"902μs / 20,853 loops" with proper speedup analysis.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-03 20:48:59 -05:00
Sarthak Agarwal
b39f2a2e9b fix: support Mocha CJS projects and sanitize incorrect framework imports
Three related fixes for Mocha test generation in CommonJS projects:

1. inject_test_globals() now accepts module_system param — emits
   `require('node:assert/strict')` for CJS instead of ESM import syntax
2. ensure_module_system_compatibility() now converts ESM→CJS even when
   the source has mixed imports (was skipping when both ESM and CJS were
   detected, leaving the ESM import from inject_test_globals unconverted)
3. New sanitize_mocha_imports() strips vitest/jest/@jest/globals imports
   that the AI sometimes generates for Mocha projects — Mocha provides
   describe/it/before*/after* as globals

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-03 20:48:53 -05:00
Sarthak Agarwal
15fcf4741d add mocha support 2026-03-03 20:48:39 -05:00
Sarthak Agarwal
e6b2f7975b feat: bundle JUnit XML reporter for Jest, replacing external jest-junit dependency
Ship a zero-dependency jest-reporter.js inside the codeflash runtime package
instead of requiring the external jest-junit npm package. This ensures the
reporter is always available when codeflash is installed, fixing Jest-based
projects (Strapi, Moleculer) that failed because jest-junit wasn't installed.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-03 20:48:34 -05:00
Kevin Turcios
8f0ff84ecf 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-03-03 20:42:33 -05:00
Sarthak Agarwal
cb1ea5adc3 fix: use pattern matching for collocated tests in monorepos
When testsRoot overlaps moduleRoot (common in JS/TS monorepos like Ghost
where both point to "src"), the directory-based filter incorrectly
excluded ALL source files. Switch to filename/directory pattern matching
(*.test.*, *.spec.*, __tests__/) when roots overlap, preserving the
existing directory-based filter for standard layouts.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-03 20:41:45 -05:00
Sarthak Agarwal
095b8b76d0 feat: add skip_confirm and skip_api_key params to JS init for non-interactive mode
Allow init_js_project(), should_modify_package_json_config(), and
collect_js_setup_info() to run without interactive prompts when
skip_confirm=True. Uses auto-detected defaults instead of prompting.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-03 20:40:54 -05:00
Sarthak Agarwal
a14ef40d15 fix: raise clear error for unsupported JS test frameworks instead of silent fallback
Add NotImplementedError guard in all 3 test dispatchers (behavioral,
benchmarking, line-profile) for frameworks other than jest and vitest.
Previously, mocha and other frameworks silently fell through to Jest,
causing confusing failures. Now users get a clear error message.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-03 20:40:49 -05:00
Sarthak Agarwal
800ebed837 feat: discover object methods exported via CJS module.exports = variable
Resolve `module.exports = varName` where varName is an object literal
containing methods. For patterns like `const utils = { match() {} };
module.exports = utils;`, the individual methods are now recognized as
exported. This fixes function discovery for CJS libraries like Moleculer.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-03 20:40:06 -05:00
Sarthak Agarwal
b3d4e225e5 feat: discover const arrow functions exported via named export clauses
Post-process find_functions() to mark functions as exported when they appear
in named export clauses like `export { joinBy }`. This fixes discovery for
TypeScript codebases (e.g., Strapi) that define const arrow functions and
export them via a separate export statement.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-03 20:40:01 -05:00
Kevin Turcios
a89dc4b09b test: add regression tests for comment position preservation 2026-03-03 20:36:56 -05:00
Kevin Turcios
b5fab57499 fix: preserve comment position by passing CST module directly to import adder
parse_code_and_prune_cst now returns cst.Module instead of str.
add_needed_imports_from_module accepts cst.Module | str, skipping re-parse
when a Module is passed. This eliminates the string round-trip that caused
comments to migrate from statement leading_lines to Module.header,
resulting in comments appearing above imports instead of at their
original position.
2026-03-03 20:36:52 -05:00
Kevin Turcios
4cf8d31deb perf: cache module scan in _clear_lru_caches and expand test coverage
Cache inspect.getmembers() results per module so repeated loop
iterations skip the expensive rescan. Add tests for get_runtime_from_stdout,
should_stop, _set_nodeid, _get_total_time, _timed_out, logreport, and
setup/teardown hooks.
2026-03-03 20:36:42 -05:00