Commit graph

7714 commits

Author SHA1 Message Date
claude[bot]
4e52b0eec2 style: add check=False to subprocess.run and remove superfluous else branch
Co-authored-by: mohammed ahmed <undefined@users.noreply.github.com>
2026-04-06 12:05:35 +00:00
mohammed ahmed
6adea5ed0c fix: install @babel/preset-typescript for TypeScript transformation when only Babel is available
**Problem:**
When a project has @babel/core installed but no TypeScript transformer
(ts-jest, @swc/jest, etc.), Codeflash generated a Jest config with no
transform directive. Jest then defaulted to babel-jest (since @babel/core
was present), but without @babel/preset-typescript, Babel failed to
transform TypeScript files with error:

  SyntaxError: Support for the experimental syntax 'flow' isn't currently enabled

This affected all TypeScript projects using Babel without explicit TypeScript
transformer configuration.

**Solution:**
- Added _ensure_babel_preset_typescript() function that checks if the preset
  is available and installs it if needed
- Modified _detect_typescript_transformer() to use this fallback when @babel/core
  exists but no TypeScript transformer is configured
- The fix only applies when @babel/core is present, avoiding unnecessary installs

**Impact:**
- Fixes 4/7 (57%) of current optimization failures in budibase monorepo
- Systematic fix for all TypeScript projects with Babel but no TS transformer

**Testing:**
- Added 4 new unit tests in test_typescript_babel_fallback.py
- All 34 existing JavaScript test runner tests pass
- No linting/type errors

**Related Traces:**
- 26117bae-39bb-4f2f-9047-f2eb6594b7eb
- 5562089f-85e9-4a6d-b790-260bcd9316cb
- c2f741b0-7eaa-4c93-b839-3832c46a3a34
- ec5e20f3-31cc-4bb4-bef2-990ee509c2b1

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-04-06 12:02:40 +00:00
Kevin Turcios
8d51e2d310
Merge pull request #1985 from codeflash-ai/feat/compare-inject-flag
feat: add --inject flag to codeflash compare
2026-04-03 11:35:24 -05:00
Kevin Turcios
a5a475d0b4 address review: fix _shutil alias, warn on --inject with --script
- Replace `import shutil as _shutil` with plain `import shutil` to
  match the existing style in the same function
- Warn when --inject is used with --script mode (unsupported combo)
  instead of silently dropping the flag
2026-04-03 11:21:05 -05:00
Kevin Turcios
3444babf58 add --inject flag to codeflash compare
When benchmarking already-merged optimizations, the benchmark file
often doesn't exist at either the base or head ref. The --inject flag
copies specified files/directories from the working tree into both
worktrees before benchmark discovery and execution, eliminating the
need to cherry-pick benchmark commits onto temporary branches.

Usage:
  codeflash compare <base> <head> --inject tests/benchmarks/test_bench.py
2026-04-03 11:15:18 -05:00
Sarthak Agarwal
a756701bb2
Merge pull request #1974 from codeflash-ai/fix/false-positive-test-discovery
Fix false positive test discovery from substring matching
2026-04-03 18:26:19 +05:30
Kevin Turcios
accb245486
Merge pull request #1941 from codeflash-ai/cf-compare-copy-benchmarks
feat: enhance codeflash compare with memory profiling, script mode, and auto-calibration
2026-04-03 07:13:00 -05:00
ali
b77896d17f
fix: handle class methods, aliased imports, and namespace access in JS test discovery
The index-only approach missed tests for class methods (imported by class name),
aliased imports (only alias was tracked), and namespace imports (e.g. math.calculate).
Adds class_name→methods index, tracks both original+alias names, and extracts
namespace member access via regex.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-03 13:54:46 +02:00
ali
d8e758800f
Merge branch 'main' of github.com:codeflash-ai/codeflash into fix/false-positive-test-discovery 2026-04-03 13:32:34 +02:00
Sarthak Agarwal
30ea7015cc
Merge pull request #1977 from codeflash-ai/fix/js-discover-tests-performance
perf(js): optimize discover_tests from O(N×M) to O(N+M)
2026-04-03 16:56:28 +05:30
Sarthak Agarwal
61a539f3b5
Merge pull request #1978 from codeflash-ai/fix/jest-testmatch-testregex-conflict
fix(js): resolve Jest testMatch/testRegex configuration conflict
2026-04-03 16:48:56 +05:30
Sarthak Agarwal
3b7c61e9dd
Merge pull request #1971 from codeflash-ai/fix/vitest-coverage-excluded-files
fix: Improve error messaging for files excluded from Vitest coverage
2026-04-03 16:47:09 +05:30
claude[bot]
ab728f7452
Merge pull request #1975 from codeflash-ai/codeflash/optimize-pr1941-2026-04-02T18.50.56
️ Speed up function `validate_and_format_benchmark_table` by 45% in PR #1941 (`cf-compare-copy-benchmarks`)
2026-04-03 02:22:43 +00:00
ali
d464ad60c5
Merge branch 'main' of github.com:codeflash-ai/codeflash into fix/js-discover-tests-performance 2026-04-03 04:19:39 +02:00
ali
8e443e8f83
Merge branch 'main' of github.com:codeflash-ai/codeflash into fix/false-positive-test-discovery 2026-04-03 04:17:44 +02:00
ali
7777b24cc7
Merge branch 'main' of github.com:codeflash-ai/codeflash into fix/vitest-coverage-excluded-files 2026-04-03 03:05:36 +02:00
Codeflash Bot
b9d8efa0d1 fix(js): resolve Jest testMatch/testRegex configuration conflict
Fix Jest validation error that prevented all TypeScript/JavaScript optimizations
in projects using bundler moduleResolution.

## Problem

When optimizing JS/TS projects with bundler moduleResolution, Jest would fail with:

This blocked ALL optimizations for these projects since no tests could run.

## Root Cause

Two-stage Jest config generation caused conflicting options:

1. \_create_codeflash_jest_config() creates base config with `testRegex` (line 321)
2. \_create_runtime_jest_config() extends base config:
   ```javascript
   module.exports = {
     ...baseConfig,  // Spreads testRegex from base
     testMatch: [...],  // Adds testMatch - CONFLICT!
   }
   ```
3. Jest sees both options and rejects the configuration

## Solution

Explicitly clear `testRegex` when setting `testMatch` in runtime config:
```javascript
testMatch: ['**/*.test.ts', ...],
testRegex: undefined,  // Clear inherited testRegex
```

Jest config precedence allows explicit `undefined` to override inherited values.

## Testing

Before:
```
 Jest failed with returncode=1.
   Validation Error: Configuration options testMatch and testRegex cannot be used together.
```

After:
```
 Jest runs without configuration errors
 Config validation passes
```

Tested on n8n packages/workflow which uses bundler moduleResolution.

## Impact

- Fixes all JS/TS projects that use:
  - TypeScript with bundler moduleResolution (common in modern repos)
  - Projects where Codeflash detects the need for ESM compatibility config
- Allows optimization to proceed past configuration stage

## Files Changed

- `codeflash/languages/javascript/test_runner.py` - _create_runtime_jest_config()

Fixes issue where TypeScript projects with bundler moduleResolution
would immediately fail Jest validation, preventing any optimizations.
2026-04-02 20:02:30 +00:00
claude[bot]
867c3df583 style: replace unicode multiplication sign in comment with ASCII x
Co-authored-by: mohammed ahmed <undefined@users.noreply.github.com>
2026-04-02 19:59:21 +00:00
Codeflash Bot
0d0f722af4 perf(js): optimize discover_tests from O(N×M) to O(N+M)
Fix test discovery performance bottleneck that caused indefinite hangs on large codebases.

## Problem
The discover_tests() method had O(N×M) complexity where N is the number of test files
and M is the number of source functions. For large repos (e.g., n8n with 12,138 functions
and 5,502 test files), this created ~66 million iterations and caused the process to hang
indefinitely at the test discovery stage.

## Root Cause
Lines 258-265 iterated over ALL source functions for EVERY test file:
```python
for test_file in test_files:  # N iterations
    for func in source_functions:  # M iterations per test file
        if func.function_name in imported_names or func.function_name in source:
            # map test to function
```

Additionally, the `func.function_name in source` check performed expensive string
containment searches on entire test files for every function, making it even slower.

## Solution
Rewrote algorithm to build a reverse index first, reducing complexity to O(N+M):
1. Build function_name → qualified_name dict once (O(M))
2. For each test file, only check imported names against the index (O(N))

This reduces iterations from ~66 million to ~17,640 for large repos.

## Performance Impact
Tested on n8n repository (12,138 functions, 5,502 test files):
- **Before**: Hung indefinitely (killed after 90+ seconds, never completed)
- **After**: 45.2 seconds total
- **Improvement**: 3,700x complexity reduction

Also removed the fallback `func.function_name in source` check as it was:
- Extremely expensive (substring search in entire file)
- Prone to false positives (matches in comments/strings)
- Unnecessary (functions must be imported to be used)

## Testing
- Verified on n8n repo: discovers 149,378 tests in 45s (previously hung)
- Verified on smaller repos: still works correctly with negligible overhead

Fixes performance issue where Codeflash would appear to hang after function discovery
when run with --all on large JavaScript/TypeScript monorepos.
2026-04-02 19:56:27 +00:00
Sarthak Agarwal
b0c4e1f10e
Merge pull request #1976 from codeflash-ai/fix/nested-functions-parity-test
fix: update nested functions parity test
2026-04-03 01:11:35 +05:30
ali
834adc5a43
fix: update nested functions parity test to match new JS discovery behavior
PR #1968 changed JS to skip nested functions like Python, but the parity
test still expected JS to discover both outer and inner.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-02 21:02:23 +02:00
claude[bot]
cfcbae5854 style: remove duplicate comments introduced by optimization 2026-04-02 18:53:03 +00:00
Codeflash Bot
63c475af0e Fix AttributeError: use file_path not source_file_path
Bug #5 fix: The coverage exclusion error messages used
self.function_to_optimize.source_file_path but FunctionToOptimize
only has file_path attribute, not source_file_path. This caused
AttributeError when files were excluded from coverage.

Trace ID: 5c4a75fb-d8eb-4f75-9e57-893f0c44b9c7

Changes:
- Fixed lines 2797, 2803: source_file_path -> file_path
- Added regression test to verify correct attribute used

Testing:
- New test passes
- Linting passes

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-04-02 18:51:38 +00:00
codeflash-ai[bot]
4e07c98f69
Optimize validate_and_format_benchmark_table
The hot path shows `logger.debug` consuming 18.3% of original runtime despite appearing infrequently (141 hits), because formatting the f-string occurs unconditionally even when debug logging is disabled. Wrapping it with `logger.isEnabledFor(logging.DEBUG)` defers string construction until confirmed necessary, eliminating wasteful formatting. Replacing `lambda x: x[3]` with `operator.itemgetter(3)` in the sort key reduces per-comparison overhead from a Python function call to a C-level attribute access, and hoisting the division constant `1_000_000.0` outside the loop avoids repeated float literal construction. Line profiler confirms the sort line dropped from 568 µs to 197 µs (65% faster) and the debug call from 1102 µs to 124 µs (89% faster), yielding a 45% overall speedup with no correctness or metric trade-offs.
2026-04-02 18:51:00 +00:00
Codeflash Bot
29e42ae9cc Fix false positive test discovery from substring matching
Issue: Test discovery incorrectly matched test files with source functions
when the function name appeared anywhere in the test file, including in
mocks, comments, or unrelated code. This caused 'Failed to instrument test
file' errors.

Root cause: In javascript/support.py line 259, naive substring matching
(func.function_name in source) matched function names even when they were
only mentioned in mocks like:
  vi.mock('./file.js', () => ({ funcName: ... }))

Example: Function parseRestartRequestParams from restart-request.ts was
wrongly matched with update.test.ts because the test file mocked it.

Fix: Removed substring matching, now only matches explicitly imported
functions. This is more reliable and avoids false positives.

Trace ID: 0b575a96-62a8-4910-b163-1ad10e60ba79

Changes:
- Removed naive substring check in discover_tests()
- Only match functions that are explicitly imported
- Added regression tests (2 test cases)

Testing:
- All 70 JavaScript tests pass
- New tests verify fix works correctly
- Linting/type checks pass (uv run prek)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-04-02 18:46:48 +00:00
Codeflash Bot
e329d52c34 fix: Improve error messaging for files excluded from Vitest coverage
## Problem
When a file is excluded from coverage by vitest.config.ts (e.g., via
`coverage.exclude: ["src/agents/**"]`), Codeflash reports misleading
"Test coverage is 0.0%" messages even though tests run successfully.

This happens because:
- Vitest doesn't include excluded files in coverage-final.json
- Codeflash detects this (status = NOT_FOUND) but shows generic 0% message
- Users don't know the file is excluded from coverage collection

## Solution
Detect when coverage status is NOT_FOUND and provide a clear, actionable
error message explaining:
1. No coverage data was found for the file
2. It may be excluded by test framework configuration
3. Where to check (coverage.exclude in vitest.config.ts, etc.)

## Changes
- function_optimizer.py: Check CoverageStatus.NOT_FOUND before reporting 0%
- Added clear warning log and user-facing error message
- New test file: test_vitest_coverage_exclusions.py

## Testing
- All existing JavaScript tests pass
- New tests verify NOT_FOUND status is returned correctly
- Manual verification with openclaw logs (trace: 2a84fe6b-9871-4916-96da-bdd79bca508a)

Fixes #BUG-1 (from autoresearch:debug workflow)
Trace IDs affected: All 10 log files showing 0% coverage in /workspace/logs

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-04-02 17:44:46 +00:00
Sarthak Agarwal
f86fe2d4ed
Merge pull request #1969 from codeflash-ai/fix/vitest-coverage-thresholds
Fix: Disable Vitest coverage thresholds for Codeflash-generated tests
2026-04-02 22:19:27 +05:30
Kevin Turcios
6965e9871d feat: add --script mode to codeflash compare
Allows running arbitrary benchmark scripts on both git refs and
rendering a styled comparison table. Supports optional --memory
via memray wrapping. No codeflash config required for script mode.
2026-04-02 11:36:54 -05:00
Sarthak Agarwal
b8c31b3768
Merge pull request #1968 from codeflash-ai/fix/skip-nested-functions-js
Fix: Skip nested functions in JavaScript/TypeScript discovery
2026-04-02 21:51:13 +05:30
Kevin Turcios
ca198ce5ab fix: update test expectations for multi-round benchmark plugin
- test_trace_multithreaded_benchmark: SELECT DISTINCT collapses all 10
  threaded sorter calls to 1 row (identical metadata), change 10 → 1
- test_trace_benchmark_decorator: accept zero timing when func_time >
  total_time triggers the overflow guard in validate_and_format
2026-04-02 11:18:33 -05:00
Kevin Turcios
699b70a02a feat: support memory-only benchmarks without changed function detection
When --memory is used and no changed top-level functions are detected,
skip trace benchmarking but still run memray profiling. This fixes the
class method limitation where codeflash compare couldn't profile memory
for changes in class methods (which are excluded from @codeflash_trace
instrumentation due to pickle overhead).
2026-04-02 11:06:15 -05:00
ali
0ed45992db
revert unrelated changes 2026-04-02 17:59:02 +02:00
ali
dc13d3cf16
revert unrelated changes 2026-04-02 17:50:35 +02:00
ali
bd7185f121
Merge branch 'main' of github.com:codeflash-ai/codeflash into fix/skip-nested-functions-js 2026-04-02 17:47:48 +02:00
ali
432fad4711
revert unrelated changes 2026-04-02 17:46:51 +02:00
ali
aea95c2383
revert unrelated changes 2026-04-02 17:46:19 +02:00
Kevin Turcios
279a8fcb29 feat: add --memory flag to codeflash compare for peak memory profiling
Adds a second profiling phase using pytest-memray that runs after timing
benchmarks. Memory tables are suppressed when the delta is <1%.
2026-04-02 10:29:31 -05:00
Codeflash Bot
bca40c6e2c Fix: Disable Vitest coverage thresholds for Codeflash-generated tests
**Problem:**
When running Codeflash-generated tests with coverage enabled, Vitest would
fail with returncode=1 due to project-level coverage thresholds not being met.

Generated tests typically cover only a single function (~1-2% of codebase),
which fails projects with thresholds like 70% lines/functions configured in
their vitest.config.ts.

**Root Cause:**
In vitest_runner.py line 450, Codeflash was adding --coverage flag without
disabling the project's global coverage thresholds. This caused false failures
even when all tests passed successfully.

**Solution:**
Added coverage threshold override flags when coverage is enabled:
- --coverage.thresholds.lines=0
- --coverage.thresholds.functions=0
- --coverage.thresholds.statements=0
- --coverage.thresholds.branches=0

These flags disable project-level thresholds, allowing coverage collection
without failing the test run. Coverage data is still collected for analysis,
but thresholds no longer cause false failures.

**Testing:**
- Added comprehensive unit tests in test_vitest_coverage_thresholds.py
- All 40 existing vitest-related tests pass
- Verified with uv run prek (linter + type checker)

**Related Issues:**
Trace IDs affected: 05a626f3, 932e7799, a145328d, aa9bb63f, d669202e, e6de097a

Fixes 6 out of 10 optimization failures in openclaw project.
2026-04-02 14:08:37 +00:00
Kevin Turcios
74c29b20b1 fix: update tests for multi-round benchmark plugin
The benchmark plugin now runs multiple rounds with calibrated
iterations. Tests need SELECT DISTINCT for row counts and must
extract median_ns from BenchmarkStats before validation.
2026-04-02 07:24:55 -05:00
Codeflash Bot
8f1b9f2e99 Fix: Skip nested functions in JavaScript/TypeScript discovery
Bug: Nested functions were being discovered and attempted to be optimized,
but the extraction logic only captured the nested function body without
parent scope variables, causing validation errors like:
  'Undefined variable(s): base, streamFn, record, writer'

Root cause: The discover_functions method was allowing nested functions
(functions defined inside other functions) to be marked for optimization.
These nested functions depend on closure variables from their parent scope
and cannot be optimized in isolation.

Fix: Added explicit check to skip functions with parent_function set.
Nested functions are now filtered out during discovery phase.

Impact: Resolves 140+ trace failures with undefined variable errors.
Functions like 'wrapStreamFn.wrapped' will no longer be attempted.

Test: Added test_discover_functions.py with 4 test cases:
- test_discovers_top_level_function
- test_skips_nested_functions_in_closures (main bug fix test)
- test_discovers_class_methods (ensure methods still work)
- test_skips_nested_functions_with_multiple_levels

Affects trace IDs including: 02a59310-bb18-47e4-87cb-1e5144ce2d8c
and 140+ others with nested function extraction issues.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-04-02 11:57:41 +00:00
Sarthak Agarwal
fdf74e255b
Merge pull request #1948 from codeflash-ai/docs/codeflash-installation-for-js-and-ts
docs: Correct setup for js/ts projects
2026-04-02 14:10:34 +05:30
Sarthak Agarwal
857673d2c0
Merge pull request #1957 from codeflash-ai/fix/esm-relative-import-extensions
fix: Add .js extensions to relative imports in ESM TypeScript projects
2026-04-02 14:09:35 +05:30
Sarthak Agarwal
3f04b78f80
Merge pull request #1965 from codeflash-ai/fix/vitest-duplicate-imports
Fix: Prevent duplicate vitest/jest import declarations
2026-04-02 14:08:13 +05:30
Codeflash Bot
9a2fbc9c4c Fix: codeflash package installation for pnpm workspaces and dev environments
- Add -w flag for pnpm workspace roots to avoid ERR_PNPM_ADDING_TO_ROOT
- Use local package path (/opt/codeflash/packages/codeflash) in dev mode
- Improve error logging to show actual stderr at ERROR level instead of WARNING
- Add unit tests for workspace detection and local package usage

Fixes 9/13 optimization failures caused by 'Cannot find package codeflash'
Trace IDs affected: 08d594a2, 1722cff7, 23480bf7, 3074f19b, 6043236e,
                     b883f1bd, d01b03ce, e56507a4, f8f54e06
2026-04-02 08:33:01 +00:00
claude[bot]
52cf946ec0 fix: add missing return type annotations to test functions
Co-authored-by: mohammed ahmed <undefined@users.noreply.github.com>
2026-04-02 07:57:18 +00:00
Codeflash Bot
e3c667a0ca Fix: Prevent duplicate vitest/jest import declarations
## Problem
The inject_test_globals() function was adding duplicate framework imports,
causing parse errors like "Identifier 'describe' has already been declared".

This occurred when:
1. AI service generated tests WITH vitest imports
2. CLI called inject_test_globals() which added its own import
3. String-based duplicate check failed because identifiers had different order

Result: TWO import statements declaring the same identifiers → parse error.

## Solution
Replace string-based duplicate detection with regex-based detection that
catches ANY import from the framework, regardless of identifier order.

## Changes
- Added regex patterns for Vitest, Jest, and Mocha imports
- Modified inject_test_globals() to use regex search
- Added comprehensive tests in test_inject_test_globals_duplicate.py

## Impact
Fixes HIGH severity bug blocking test generation for all Vitest projects

## Trace IDs
- 03a5a9d9-8e56-47e8-9c5e-0160fb9a529a
- 0be70f8d-884e-45e4-8fa2-28ed40cdf068
- 29c6d314-8561-4bb4-9b77-00b3b83943f0
2026-04-02 07:51:54 +00:00
Saurabh Misra
2e6f0db3ce
Merge pull request #1963 from codeflash-ai/docs/java-docs-update
update init docs for java
2026-04-01 20:20:13 -07:00
misrasaurabh1
3bbdb26918 update init docs for java 2026-04-01 20:19:14 -07:00
Saurabh Misra
56d8579d70
Merge pull request #1961 from codeflash-ai/bump-version-0.20.5
chore: release v0.20.5
2026-04-01 20:01:55 -07:00
Saurabh Misra
ed6afc0370
Merge pull request #1960 from codeflash-ai/chore/bump-java-runtime-1.0.1
chore: bump codeflash-java-runtime to 1.0.1
2026-04-01 17:29:42 -07:00