Commit graph

1502 commits

Author SHA1 Message Date
Aseem Saxena
019f220c11
cleaning up 2026-02-02 09:45:48 -08:00
Aseem Saxena
3276f9542e
Update django/aiservice/code_repair/code_repair_context.py
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
2026-02-02 09:44:36 -08:00
aseembits93
c2cd6e5e72 minor fix 2026-02-02 09:37:34 -08:00
aseembits93
2e523313b5 prek fixes 2026-02-02 09:25:44 -08:00
Aseem Saxena
1ffdee3000
Fix check for empty test source code section
Ensure sections[diff.test_src_code] is not None before assignment.
2026-02-02 09:24:28 -08:00
Aseem Saxena
a39e155a84
bug: mismatch in cli and internal schema for code repair
Change test_src_code to allow None type
2026-02-02 09:08:17 -08:00
Sarthak Agarwal
b48a8d9a43
Add vitest support in backend (#2363) 2026-02-02 20:51:52 +05:30
Sarthak Agarwal
cbfebf8ee4 fix(js-testgen): escape curly braces in prompt template
The JavaScript test generation prompt contained `{fn}` as part of
example code showing import syntax. However, Python's `.format()`
method interprets this as a placeholder and tries to substitute it,
causing a KeyError.

Fixed by escaping the curly braces as `{{fn}}` so they render as
literal `{fn}` in the final prompt.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-01 03:50:05 +05:30
Saurabh Misra
70360436bd fix: strip file extensions from JS/TS import paths in generated tests
LLMs often add .js extensions to TypeScript import paths (e.g.,
`import { func } from '../module.js'`), but TypeScript/Jest module
resolution doesn't require explicit extensions. This causes
"Cannot find module" errors.

This change adds `strip_js_extensions()` function that removes
.js/.ts/.tsx/.jsx/.mjs/.mts extensions from relative import paths
in generated tests. The function handles:
- ES module imports: import { x } from '../path.js'
- CommonJS requires: require('../path.js')
- Jest mocks: jest.mock('../path.js'), jest.doMock(), etc.

External package imports (lodash, react, etc.) are preserved.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-31 04:22:07 +00:00
Saurabh Misra
b801254d13 fix: strengthen import path extension guidance in prompts
Add more explicit instructions to prevent LLMs from adding .js/.ts
extensions to import paths. The previous guidance was being ignored
by some models.

- Add dedicated "CRITICAL: IMPORT PATH RULES" section with examples
- Show both WRONG and CORRECT patterns explicitly
- Remind to copy the provided import statement exactly

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-31 02:35:21 +00:00
Saurabh Misra
d59c48426e fix: merge prompt extension fixes and LLM client improvements
- Cherry-pick: Remove .js extension guidance from prompts (from fix/js-import-extension-prompt)
- Add get_llm_client() to create fresh clients per request (fixes event loop issues)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-31 02:32:58 +00:00
Saurabh Misra
8461f71668 fix: use JavaScript identifier regex instead of Python isidentifier()
Python's str.isidentifier() validates Python identifiers, not JavaScript
identifiers. This caused valid JS identifiers like '$handler' to be
rejected (since $ is not valid in Python identifiers).

Changed to use a regex pattern that matches JavaScript identifier rules:
- Can start with letter, underscore, or $
- Can contain letters, digits, underscores, or $

Added tests for $ identifiers to ensure they are correctly handled.
2026-01-31 02:21:02 +00:00
Kevin Turcios
a394db3382 formatting 2026-01-30 20:00:11 -05:00
Saurabh Misra
addbaad370
Merge branch 'main' into fix/class-method-import-syntax 2026-01-30 16:36:03 -08:00
Saurabh Misra
09e6a1710f Address review: add validation for edge cases in import generation
- Add _is_valid_js_identifier() to check for reserved words (module, exports, prototype, etc.)
- Only use class import pattern for single-dot names where class name is valid identifier
- Fall back to module import for:
  - Multiple dots (e.g., Constructor.prototype.method)
  - Reserved words (e.g., module.exports)
- Add comprehensive tests for edge cases
2026-01-31 00:35:10 +00:00
Saurabh Misra
b2fb58eba6 Fix invalid JavaScript import syntax for class methods
When generating test imports for class methods like `Validator.validateRequest`,
the previous code produced invalid JavaScript:
  const { Validator.validateRequest } = require('../middlewares/Validator');

This is invalid because dots are not allowed in destructuring patterns.

The fix:
- Add _generate_import_statement() function to detect class methods (names with dots)
- For class methods: generate `const ClassName = require('...')`
- For simple functions: keep destructuring `const { funcName } = require('...')`
- Update prompt templates to use {import_statement} placeholder

Includes unit tests for the new import generation logic.
2026-01-31 00:35:10 +00:00
Saurabh Misra
289827e5cb
Merge pull request #2337 from codeflash-ai/fix/improve-typescript-validation-error-messages
fix: improve TypeScript/JavaScript validation error messages
2026-01-30 16:03:02 -08:00
Saurabh Misra
d255a29203
Update django/aiservice/aiservice/validators/javascript_validator.py
Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com>
2026-01-30 16:00:05 -08:00
Saurabh Misra
8800614d1c Add unit tests for TypeScript/JavaScript validator error reporting
Tests for:
- Error location reporting with line numbers and code snippets
- Markdown code block parsing with various scenarios
- Multiple code blocks with mixed valid/invalid content
- Real-world TypeScript patterns (async, try-catch, template literals)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-30 23:53:26 +00:00
Saurabh Misra
07ae9db684 fix: improve TypeScript/JavaScript validation error messages
Add better error diagnostics for TypeScript/JavaScript syntax validation:

- Add line numbers and code snippets to error messages
- Log warnings when markdown parsing finds no code blocks
- Show the actual problematic code in error logs
- Help debug "Invalid syntax" errors by showing exact location

This helps diagnose issues where the API rejects code that tree-sitter
parses correctly on the client side by providing more context in the
error messages and logs.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-30 23:47:39 +00:00
aseembits93
af2935f4f2 0-index finally 2026-01-30 14:42:28 -08:00
Kevin Turcios
c1a25b33e5
Merge branch 'main' into ranker-multidim-scoring 2026-01-30 22:16:08 +00:00
ali
99a7a32b32
safer caching 2026-01-30 19:52:18 +02:00
ali
879aa93967
fix validating js/ts code with markdown syntax 2026-01-30 19:44:31 +02:00
Kevin Turcios
0f5d578d37
Merge branch 'main' into ranker-multidim-scoring 2026-01-29 22:45:18 +00:00
HeshamHM28
c24f350719
Fix Prevent log code for paid org in the optimization feature "AI service " (#2325)
Fixes Cf-1038
2026-01-29 19:28:30 +00:00
Kevin Turcios
04197195e8
Store instrumented performance tests in feature logging (#2330)
## Summary
- Add `instrumented_perf_test` field to `OptimizationFeatures` model
- Update `log_features` function to accept and store performance
instrumented tests

---------

Co-authored-by: Sarthak Agarwal <sarthak.saga@gmail.com>
2026-01-29 03:09:47 -05:00
aseembits93
f1b6fbf737 adding back the instructions 2026-01-28 16:13:28 -08:00
aseembits93
7386dd20b5 1-indexed ranking everywhere 2026-01-28 16:02:24 -08:00
aseembits93
71d397753d Merge remote-tracking branch 'origin/main' into ranker-multidim-scoring 2026-01-28 15:36:30 -08:00
aseembits93
215e6ad390 fixed merge issues 2026-01-28 15:33:11 -08:00
ali
c19d9f4450
fix unit tests 2026-01-28 23:20:48 +02:00
ali
f0480fac39
use treesitter for validating js & ts code syntax 2026-01-28 23:15:30 +02:00
ali
db3f269b37
linting and formatting 2026-01-28 22:41:27 +02:00
ali
ec97ebd4e7
more cleanup 2026-01-28 22:23:54 +02:00
ali
31091350c9
cleanup 2026-01-28 22:19:40 +02:00
ali
dcac02b3f2
abstraction 2026-01-28 21:53:51 +02:00
Kevin Turcios
5ab169076c
Merge branch 'main' into multi-language 2026-01-28 08:19:52 -05:00
Kevin Turcios
8a66c78220
refactor: consolidate CST utilities and simplify add_missing_imports (#2324)
## Summary
- Consolidate shared CST utilities into `aiservice/common/cst_utils.py`
- Simplify `add_missing_imports` by removing redundant abstractions
- Require CST modules instead of strings in postprocessing pipeline
2026-01-28 08:05:45 -05:00
ali
d7e7125220
Merge branch 'multi-language' of github.com:codeflash-ai/codeflash-internal into multi-language 2026-01-28 13:41:55 +02:00
ali
557fb11939
remove jest globals check (client handles it now) 2026-01-28 13:41:06 +02:00
misrasaurabh1
b0a1d6c09f Remove instrumentation of js tests from aiservice and into client 2026-01-27 16:10:35 -08:00
ali
39de6a3bce
Merge branch 'multi-language' of github.com:codeflash-ai/codeflash-internal into multi-language 2026-01-27 23:30:11 +02:00
ali
72cb589948
some instrumentation fixes 2026-01-27 23:30:03 +02:00
Aseem Saxena
7f8ce66c6a multi dimensional scoring and structured json parsing for ranker 2026-01-27 12:24:16 -08:00
codeflash-ai[bot]
238de2b9c0
️ Speed up function _safe_replace_function_calls by 101% in PR #2247 (multi-language) (#2306)
## ️ This pull request contains optimizations for PR #2247
If you approve this dependent PR, these changes will be merged into the
original PR branch `multi-language`.
>This PR will be automatically closed if the original PR is merged.
----
#### 📄 101% (1.01x) speedup for ***`_safe_replace_function_calls` in
`django/aiservice/testgen/instrumentation/javascript/instrument_javascript.py`***

⏱️ Runtime : **`4.63 milliseconds`** **→** **`2.30 milliseconds`** (best
of `5` runs)

#### 📝 Explanation and details


This optimization achieves a **101% speedup** (reducing runtime from
4.63ms to 2.30ms) through three key performance improvements:

**1. Pre-compiled regex pattern (primary speedup driver)**
The original code calls `re.match(pattern, remaining)` inside the main
loop, which recompiles the pattern on every iteration. The line profiler
shows this operation took 3.92 seconds (33.1% of total time). The
optimized version compiles the pattern once upfront (`pattern_re =
re.compile(pattern)`) and reuses it via `pattern_re.match(source, i)`,
reducing this to 1.10 seconds (10% of total time) - a **72% reduction**
in pattern matching overhead.

**2. Eliminated substring creation for pattern matching**
The original creates a new substring `remaining = source[i:]` on every
loop iteration (5.68e8 ns, 4.8% of time). The optimized version uses
`pattern_re.match(source, i)` which matches directly from position `i`
without creating intermediate strings. This saves both allocation and
copying costs, particularly impactful given the loop runs 1.3-1.8
million times per execution.

**3. Fast-forward through comments using `str.find()`**
- **Single-line comments**: Changed from character-by-character loop
(`while i < length and source[i] != "\n"`) to `source.find("\n", i)`,
eliminating repeated indexing operations
- **Multi-line comments**: Changed from character-by-character scanning
to `source.find("*/", i)`, directly jumping to comment end

**Test case analysis:**
The optimization particularly excels on:
- **Large-scale scenarios**: The 500-iteration test shows 125% speedup
(1.24ms → 549μs), demonstrating excellent scalability
- **Comment-heavy code**: Tests with comments show 17-65% speedups as
`str.find()` efficiently skips entire comment blocks
- **Method call filtering**: Tests with `.foo()` patterns show 53-75%
speedups from reduced pattern matching overhead

All 45 test cases pass with improved or comparable performance,
validating that the optimization preserves correctness while delivering
consistent runtime improvements across diverse JavaScript code patterns.



 **Correctness verification report:**

| Test                        | Status            |
| --------------------------- | ----------------- |
| ⚙️ Existing Unit Tests | 🔘 **None Found** |
| 🌀 Generated Regression Tests |  **59 Passed** |
|  Replay Tests | 🔘 **None Found** |
| 🔎 Concolic Coverage Tests | 🔘 **None Found** |
|📊 Tests Coverage       | 100.0% |
<details>
<summary>🌀 Click to see Generated Regression Tests</summary>

```python
from __future__ import annotations

# imports
import re
from collections.abc import Callable

import pytest  # used for our unit tests

from testgen.instrumentation.javascript.instrument_javascript import (
    _safe_replace_function_calls,
)

# unit tests


# Helper replacement function used in many tests: wraps the matched call
def _wrap_with_capture(match: re.Match[str]) -> str:
    # return a wrapper that includes the exact matched text
    return f"codeflash.capture({match.group(0)})"


def test_basic_single_replacement():
    # Basic scenario: a single top-level function call should be replaced.
    src = "var x = foo(1);"
    pattern = r"foo\([^\)]*\)"
    codeflash_output = _safe_replace_function_calls(
        src, "foo", _wrap_with_capture, pattern
    )
    res = codeflash_output  # 12.5μs -> 9.73μs (28.9% faster)


def test_string_literals_not_replaced():
    # Strings containing the function call should be left alone.
    src = "var s = 'foo(1)'; foo(2);"
    pattern = r"foo\([^\)]*\)"
    codeflash_output = _safe_replace_function_calls(
        src, "foo", _wrap_with_capture, pattern
    )
    res = codeflash_output  # 14.5μs -> 10.5μs (38.5% faster)


def test_double_quoted_and_escaped_strings():
    # Double-quoted strings with escaped quotes should be preserved.
    src = 'var s = "he said \\"foo(3)\\""; foo(4);'
    pattern = r"foo\([^\)]*\)"
    codeflash_output = _safe_replace_function_calls(
        src, "foo", _wrap_with_capture, pattern
    )
    res = codeflash_output  # 15.7μs -> 11.5μs (36.0% faster)


def test_template_literals_with_expressions_skipped():
    # Template literals should preserve content and skip replacement inside ${...}.
    src = "`start ${ { nested: foo(5) } } end` foo(6);"
    pattern = r"foo\([^\)]*\)"
    codeflash_output = _safe_replace_function_calls(
        src, "foo", _wrap_with_capture, pattern
    )
    res = codeflash_output  # 12.8μs -> 12.0μs (6.23% faster)


def test_single_line_comment_skipped():
    # Calls inside single-line comments should not be replaced.
    src = " // foo(7)\nfoo(8);"
    pattern = r"foo\([^\)]*\)"
    codeflash_output = _safe_replace_function_calls(
        src, "foo", _wrap_with_capture, pattern
    )
    res = codeflash_output  # 9.40μs -> 8.02μs (17.1% faster)


def test_multi_line_comment_skipped_and_unclosed_comment_handled():
    # Multi-line comments containing calls should be preserved.
    src = "/* comment foo(9) */ foo(10);"
    pattern = r"foo\([^\)]*\)"
    codeflash_output = _safe_replace_function_calls(
        src, "foo", _wrap_with_capture, pattern
    )
    res = codeflash_output  # 9.87μs -> 8.17μs (20.9% faster)

    # Unclosed multi-line comment at EOF should not hang or replace inside it
    src_unclosed = "/* unclosed foo(11)"
    codeflash_output = _safe_replace_function_calls(
        src_unclosed, "foo", _wrap_with_capture, pattern
    )
    res_unclosed = codeflash_output  # 3.26μs -> 1.97μs (65.2% faster)


def test_method_call_dot_not_replaced():
    # Calls that are methods (preceded by a dot) should not be replaced.
    src = "obj.foo(12); foo(13);"
    pattern = r"foo\([^\)]*\)"
    codeflash_output = _safe_replace_function_calls(
        src, "foo", _wrap_with_capture, pattern
    )
    res = codeflash_output  # 14.8μs -> 9.49μs (55.6% faster)


def test_already_wrapped_skipped():
    # If 'codeflash.capture' immediately precedes the call start index, replacement should be skipped.
    # This tests the specific check in the function that avoids double-wrapping.
    src = "codeflash.capturefoo(14);"
    pattern = r"foo\([^\)]*\)"
    codeflash_output = _safe_replace_function_calls(
        src, "foo", _wrap_with_capture, pattern
    )
    res = codeflash_output  # 19.9μs -> 11.3μs (75.4% faster)


def test_template_literal_with_escaped_backticks_and_expressions():
    # Template literal containing escaped backticks and ${...} should be handled correctly.
    src = r"`backtick \` and ${ { foo: foo(15) } } end` foo(16);"
    pattern = r"foo\([^\)]*\)"
    codeflash_output = _safe_replace_function_calls(
        src, "foo", _wrap_with_capture, pattern
    )
    res = codeflash_output  # 14.2μs -> 12.8μs (11.1% faster)


def test_overlapping_names_handling():
    # When function name appears adjacent to other letters like 'foofoo(17)',
    # ensure inner call is still found at the correct position.
    src = "foofoo(17); foo(18);"
    pattern = r"foo\([^\)]*\)"
    codeflash_output = _safe_replace_function_calls(
        src, "foo", _wrap_with_capture, pattern
    )
    res = codeflash_output  # 11.3μs -> 7.92μs (43.2% faster)


def test_escaped_characters_in_regular_strings_do_not_break_scanning():
    # Strings that include escaped backslashes and escaped quotes should not break the scanner.
    src = '"line1\\nfoo(19)\\nline2" foo(20);'
    pattern = r"foo\([^\)]*\)"
    codeflash_output = _safe_replace_function_calls(
        src, "foo", _wrap_with_capture, pattern
    )
    res = codeflash_output  # 10.9μs -> 9.15μs (19.1% faster)


def test_large_scale_multiple_replacements():
    # Large-scale test: many occurrences (but below 1000) to assess scalability.
    count = 500  # keep below 1000 as per instructions
    src_parts = [f"foo({i});" for i in range(count)]
    src = " ".join(src_parts)
    pattern = r"foo\([^\)]*\)"
    codeflash_output = _safe_replace_function_calls(
        src, "foo", _wrap_with_capture, pattern
    )
    res = codeflash_output  # 1.24ms -> 549μs (125% faster)
    # Ensure that every occurrence was wrapped exactly once.
    wrapped_count = res.count("codeflash.capture(")


def test_no_false_positive_when_preceded_by_other_chars():
    # If the character before the function is not a dot but some other char, replacement should proceed.
    src = "afoo(21); .foo(22);"
    pattern = r"foo\([^\)]*\)"
    codeflash_output = _safe_replace_function_calls(
        src, "foo", _wrap_with_capture, pattern
    )
    res = codeflash_output  # 13.3μs -> 8.96μs (48.5% faster)


def test_pattern_that_matches_functions_with_spaces():
    # Pattern that allows spaces before the opening parenthesis.
    src = "foo (23); foo(24);"
    pattern = r"foo\s*\([^\)]*\)"
    codeflash_output = _safe_replace_function_calls(
        src, "foo", _wrap_with_capture, pattern
    )
    res = codeflash_output  # 10.1μs -> 7.71μs (30.6% faster)


# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

```

```python
import re
from collections.abc import Callable

# imports
import pytest

from testgen.instrumentation.javascript.instrument_javascript import (
    _safe_replace_function_calls,
)


class TestBasicFunctionality:
    """Test cases for basic functionality of _safe_replace_function_calls."""

    def test_simple_function_call_replacement(self):
        """Test replacing a simple function call with a basic pattern."""
        source = "foo()"
        pattern = r"foo\(\)"
        replace_func = lambda m: "bar()"
        codeflash_output = _safe_replace_function_calls(
            source, "foo", replace_func, pattern
        )
        result = codeflash_output  # 5.38μs -> 5.00μs (7.58% faster)

    def test_function_call_with_arguments(self):
        """Test replacing function calls that have arguments."""
        source = "foo(x, y)"
        pattern = r"foo\([^)]*\)"
        replace_func = lambda m: "bar(x, y)"
        codeflash_output = _safe_replace_function_calls(
            source, "foo", replace_func, pattern
        )
        result = codeflash_output  # 5.25μs -> 4.74μs (10.7% faster)

    def test_multiple_function_calls(self):
        """Test replacing multiple occurrences of the same function."""
        source = "foo(); foo(); foo();"
        pattern = r"foo\(\)"
        call_count = [0]

        def replace_func(m):
            call_count[0] += 1
            return f"replaced_{call_count[0]}()"

        codeflash_output = _safe_replace_function_calls(
            source, "foo", replace_func, pattern
        )
        result = codeflash_output  # 12.2μs -> 8.70μs (40.3% faster)

    def test_empty_source(self):
        """Test with an empty source string."""
        source = ""
        pattern = r"foo\(\)"
        replace_func = lambda m: "bar()"
        codeflash_output = _safe_replace_function_calls(
            source, "foo", replace_func, pattern
        )
        result = codeflash_output  # 853ns -> 2.04μs (58.1% slower)

    def test_no_matches(self):
        """Test when no function calls match the pattern."""
        source = "let x = 5; y = 10;"
        pattern = r"foo\(\)"
        replace_func = lambda m: "bar()"
        codeflash_output = _safe_replace_function_calls(
            source, "foo", replace_func, pattern
        )
        result = codeflash_output  # 14.3μs -> 7.73μs (84.8% faster)

    def test_function_call_in_middle_of_code(self):
        """Test replacing function call in the middle of other code."""
        source = "let result = foo(); console.log(result);"
        pattern = r"foo\(\)"
        replace_func = lambda m: "bar()"
        codeflash_output = _safe_replace_function_calls(
            source, "foo", replace_func, pattern
        )
        result = codeflash_output  # 24.9μs -> 12.9μs (92.5% faster)

    def test_preserve_whitespace(self):
        """Test that whitespace around function calls is preserved."""
        source = "foo(  )  "
        pattern = r"foo\(\s*\)"
        replace_func = lambda m: "bar()"
        codeflash_output = _safe_replace_function_calls(
            source, "foo", replace_func, pattern
        )
        result = codeflash_output  # 6.61μs -> 5.85μs (12.8% faster)

    def test_function_call_in_single_quoted_string(self):
        """Test that function calls inside single-quoted strings are NOT replaced."""
        source = "'foo()' bar()"
        pattern = r"foo\(\)|bar\(\)"
        replace_func = lambda m: "replaced()"
        codeflash_output = _safe_replace_function_calls(
            source, "foo", replace_func, pattern
        )
        result = codeflash_output  # 7.49μs -> 7.27μs (3.07% faster)

    def test_function_call_in_double_quoted_string(self):
        """Test that function calls inside double-quoted strings are NOT replaced."""
        source = '"foo()" bar()'
        pattern = r"foo\(\)|bar\(\)"
        replace_func = lambda m: "replaced()"
        codeflash_output = _safe_replace_function_calls(
            source, "foo", replace_func, pattern
        )
        result = codeflash_output  # 7.22μs -> 6.47μs (11.5% faster)

    def test_function_call_in_single_line_comment(self):
        """Test that function calls in single-line comments are NOT replaced."""
        source = "// foo() should not be replaced\nbar()"
        pattern = r"foo\(\)|bar\(\)"
        replace_func = lambda m: "replaced()"
        codeflash_output = _safe_replace_function_calls(
            source, "foo", replace_func, pattern
        )
        result = codeflash_output  # 9.27μs -> 6.86μs (35.0% faster)

    def test_function_call_in_multiline_comment(self):
        """Test that function calls in multi-line comments are NOT replaced."""
        source = "/* foo() should not be replaced */ bar()"
        pattern = r"foo\(\)|bar\(\)"
        replace_func = lambda m: "replaced()"
        codeflash_output = _safe_replace_function_calls(
            source, "foo", replace_func, pattern
        )
        result = codeflash_output  # 10.00μs -> 7.47μs (33.7% faster)


class TestEdgeCases:
    """Test cases for edge cases and unusual conditions."""

    def test_escaped_quote_in_string(self):
        """Test handling of escaped quotes inside strings."""
        source = r"'foo\'s bar' foo()"
        pattern = r"foo\(\)"
        replace_func = lambda m: "replaced()"
        codeflash_output = _safe_replace_function_calls(
            source, "foo", replace_func, pattern
        )
        result = codeflash_output  # 7.99μs -> 7.41μs (7.81% faster)

    def test_escaped_backslash_in_string(self):
        """Test handling of escaped backslashes in strings."""
        source = r"'path\\to\\file' foo()"
        pattern = r"foo\(\)"
        replace_func = lambda m: "replaced()"
        codeflash_output = _safe_replace_function_calls(
            source, "foo", replace_func, pattern
        )
        result = codeflash_output  # 8.41μs -> 7.76μs (8.36% faster)

    def test_method_call_not_replaced(self):
        """Test that method calls (preceded by dot) are NOT replaced."""
        source = "obj.foo() + foo()"
        pattern = r"foo\(\)"
        replace_func = lambda m: "bar()"
        codeflash_output = _safe_replace_function_calls(
            source, "foo", replace_func, pattern
        )
        result = codeflash_output  # 12.9μs -> 8.37μs (53.6% faster)

    def test_template_literal_simple(self):
        """Test handling of simple template literals."""
        source = "`foo()` bar()"
        pattern = r"foo\(\)|bar\(\)"
        replace_func = lambda m: "replaced()"
        codeflash_output = _safe_replace_function_calls(
            source, "foo", replace_func, pattern
        )
        result = codeflash_output  # 7.53μs -> 6.86μs (9.73% faster)

    def test_template_literal_with_expression(self):
        """Test handling of template literals with embedded expressions."""
        source = "`Value: ${foo()}` bar()"
        pattern = r"foo\(\)|bar\(\)"
        replace_func = lambda m: "replaced()"
        codeflash_output = _safe_replace_function_calls(
            source, "foo", replace_func, pattern
        )
        result = codeflash_output  # 9.50μs -> 9.01μs (5.51% faster)

    def test_template_literal_with_nested_braces(self):
        """Test handling of template literals with nested braces in expressions."""
        source = "`${obj.map(x => ({value: x}))}` foo()"
        pattern = r"foo\(\)"
        replace_func = lambda m: "replaced()"
        codeflash_output = _safe_replace_function_calls(
            source, "foo", replace_func, pattern
        )
        result = codeflash_output  # 10.5μs -> 10.2μs (2.83% faster)

    def test_codeflash_capture_already_wrapped(self):
        """Test that already wrapped codeflash.capture calls are not double-wrapped."""
        source = "codeflash.capture(foo())"
        pattern = r"foo\(\)"
        replace_func = lambda m: "bar()"
        codeflash_output = _safe_replace_function_calls(
            source, "foo", replace_func, pattern
        )
        result = codeflash_output  # 16.9μs -> 10.5μs (60.3% faster)

    def test_nested_quotes_in_string(self):
        """Test handling of different quote types inside strings."""
        source = """'string with "quotes"' foo()"""
        pattern = r"foo\(\)"
        replace_func = lambda m: "replaced()"
        codeflash_output = _safe_replace_function_calls(
            source, "foo", replace_func, pattern
        )
        result = codeflash_output  # 8.46μs -> 8.08μs (4.66% faster)

    def test_multiline_comment_not_closed(self):
        """Test handling of multi-line comment that extends to end of source."""
        source = "foo() /* comment starts here"
        pattern = r"foo\(\)"
        replace_func = lambda m: "replaced()"
        codeflash_output = _safe_replace_function_calls(
            source, "foo", replace_func, pattern
        )
        result = codeflash_output  # 9.20μs -> 6.59μs (39.5% faster)

    def test_function_call_at_end_of_source(self):
        """Test function call replacement at the very end of source."""
        source = "foo()"
        pattern = r"foo\(\)"
        replace_func = lambda m: "bar()"
        codeflash_output = _safe_replace_function_calls(
            source, "foo", replace_func, pattern
        )
        result = codeflash_output  # 4.28μs -> 4.24μs (0.730% faster)

    def test_adjacent_strings(self):
        """Test handling of adjacent string literals."""
        source = "'foo()' \"foo()\" foo()"
        pattern = r"foo\(\)"
        replace_func = lambda m: "replaced()"
        codeflash_output = _safe_replace_function_calls(
            source, "foo", replace_func, pattern
        )
        result = codeflash_output  # 9.13μs -> 8.05μs (13.4% faster)

    def test_comment_after_function_call(self):
        """Test function call followed by a comment."""
        source = "foo() // this is foo\nbar()"
        pattern = r"foo\(\)|bar\(\)"
        replace_func = lambda m: "replaced()"
        codeflash_output = _safe_replace_function_calls(
            source, "foo", replace_func, pattern
        )
        result = codeflash_output  # 10.5μs -> 8.23μs (27.1% faster)

    def test_escaped_character_in_comment(self):
        """Test comment handling with special characters."""
        source = "foo() // comment with \\ backslash\nbar()"
        pattern = r"foo\(\)|bar\(\)"
        replace_func = lambda m: "replaced()"
        codeflash_output = _safe_replace_function_calls(
            source, "foo", replace_func, pattern
        )
        result = codeflash_output  # 11.4μs -> 8.42μs (35.6% faster)

    def test_regex_special_chars_in_string(self):
        """Test strings containing regex special characters."""
        source = r"'[a-z].*+?' foo()"
        pattern = r"foo\(\)"
        replace_func = lambda m: "replaced()"
        codeflash_output = _safe_replace_function_calls(
            source, "foo", replace_func, pattern
        )
        result = codeflash_output  # 7.29μs -> 6.70μs (8.80% faster)

    def test_single_char_string(self):
        """Test handling of single character strings."""
        source = "'' 'a' foo()"
        pattern = r"foo\(\)"
        replace_func = lambda m: "replaced()"
        codeflash_output = _safe_replace_function_calls(
            source, "foo", replace_func, pattern
        )
        result = codeflash_output  # 7.62μs -> 6.79μs (12.2% faster)

    def test_consecutive_escaped_quotes(self):
        """Test multiple consecutive escaped quotes."""
        source = r"'\"\"' foo()"
        pattern = r"foo\(\)"
        replace_func = lambda m: "replaced()"
        codeflash_output = _safe_replace_function_calls(
            source, "foo", replace_func, pattern
        )
        result = codeflash_output  # 6.92μs -> 6.09μs (13.5% faster)

    def test_template_literal_escaped_backtick(self):
        """Test template literal with escaped backtick."""
        source = r"`escaped \` backtick` foo()"
        pattern = r"foo\(\)"
        replace_func = lambda m: "replaced()"
        codeflash_output = _safe_replace_function_calls(
            source, "foo", replace_func, pattern
        )
        result = codeflash_output  # 9.10μs -> 8.80μs (3.43% faster)

```

</details>


To edit these changes `git checkout
codeflash/optimize-pr2247-2026-01-25T08.38.25` and push.


[![Codeflash](https://img.shields.io/badge/Optimized%20with-Codeflash-yellow?style=flat&color=%23ffc428&logo=data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNDgwIiBoZWlnaHQ9ImF1dG8iIHZpZXdCb3g9IjAgMCA0ODAgMjgwIiBmaWxsPSJub25lIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPgo8cGF0aCBmaWxsLXJ1bGU9ImV2ZW5vZGQiIGNsaXAtcnVsZT0iZXZlbm9kZCIgZD0iTTI4Ni43IDAuMzc4NDE4SDIwMS43NTFMNTAuOTAxIDE0OC45MTFIMTM1Ljg1MUwwLjk2MDkzOCAyODEuOTk5SDk1LjQzNTJMMjgyLjMyNCA4OS45NjE2SDE5Ni4zNDVMMjg2LjcgMC4zNzg0MThaIiBmaWxsPSIjRkZDMDQzIi8+CjxwYXRoIGZpbGwtcnVsZT0iZXZlbm9kZCIgY2xpcC1ydWxlPSJldmVub2RkIiBkPSJNMzExLjYwNyAwLjM3ODkwNkwyNTguNTc4IDU0Ljk1MjZIMzc5LjU2N0w0MzIuMzM5IDAuMzc4OTA2SDMxMS42MDdaIiBmaWxsPSIjMEIwQTBBIi8+CjxwYXRoIGZpbGwtcnVsZT0iZXZlbm9kZCIgY2xpcC1ydWxlPSJldmVub2RkIiBkPSJNMzA5LjU0NyA4OS45NjAxTDI1Ni41MTggMTQ0LjI3NkgzNzcuNTA2TDQzMC4wMjEgODkuNzAyNkgzMDkuNTQ3Vjg5Ljk2MDFaIiBmaWxsPSIjMEIwQTBBIi8+CjxwYXRoIGZpbGwtcnVsZT0iZXZlbm9kZCIgY2xpcC1ydWxlPSJldmVub2RkIiBkPSJNMjQyLjg3MyAxNjQuNjZMMTg5Ljg0NCAyMTkuMjM0SDMxMC44MzNMMzYzLjM0NyAxNjQuNjZIMjQyLjg3M1oiIGZpbGw9IiMwQjBBMEEiLz4KPC9zdmc+Cg==)](https://codeflash.ai)
![Static
Badge](https://img.shields.io/badge/🎯_Optimization_Quality-high-green)

Co-authored-by: codeflash-ai[bot] <148906541+codeflash-ai[bot]@users.noreply.github.com>
Co-authored-by: Kevin Turcios <106575910+KRRT7@users.noreply.github.com>
2026-01-27 00:28:03 -05:00
codeflash-ai[bot]
64b0bb74e0
️ Speed up function _has_test_functions by 18% in PR #2247 (multi-language) (#2308)
## ️ This pull request contains optimizations for PR #2247
If you approve this dependent PR, these changes will be merged into the
original PR branch `multi-language`.
>This PR will be automatically closed if the original PR is merged.
----
#### 📄 18% (0.18x) speedup for ***`_has_test_functions` in
`django/aiservice/testgen/testgen_javascript.py`***

⏱️ Runtime : **`740 microseconds`** **→** **`627 microseconds`** (best
of `76` runs)

#### 📝 Explanation and details


The optimized code achieves an **18% runtime improvement** by
eliminating repeated regex compilation overhead.

**Key optimization:**
- **Precompiled regex pattern**: The pattern
`r"(?:test|it)\s*\(\s*['\"]"` is compiled once at module load time into
`_TEST_FUNC_RE`, rather than being recompiled on every function call. In
Python, `re.search()` with a raw string pattern incurs compilation cost
each time it's invoked.

**Performance breakdown from line profiler:**
- Original: 2.70ms spent in `re.search(test_pattern, code)` (96.5% of
total time)
- Optimized: 862μs for the direct pattern search (100% of total time,
but 3.1x faster overall)
- The pattern string assignment overhead (97μs in original) is
eliminated entirely

**Why this matters for the workload:**
Based on `function_references`, this function is called from
`parse_and_validate_js_output()` during LLM response validation. This is
a **hot path** operation that executes on every test generation request.
The validation flow checks multiple conditions including syntax
validation before checking for test functions, meaning this function
runs repeatedly during normal operations.

**Test case performance:**
- **Small inputs** (single test functions): 50-80% faster (e.g., 2.80μs
→ 1.83μs)
- **Empty/minimal strings**: 130-140% faster (e.g., 1.80μs → 750ns) 
- **Large inputs** (500-1000 lines): 1-8% faster depending on match
location
- **Early matches** benefit most since regex short-circuits on first
match

The optimization is most effective when processing typical-sized
JavaScript test code (dozens to hundreds of lines), which aligns with
the common use case of validating LLM-generated test functions.



 **Correctness verification report:**

| Test                        | Status            |
| --------------------------- | ----------------- |
| ⚙️ Existing Unit Tests | 🔘 **None Found** |
| 🌀 Generated Regression Tests |  **102 Passed** |
|  Replay Tests | 🔘 **None Found** |
| 🔎 Concolic Coverage Tests | 🔘 **None Found** |
|📊 Tests Coverage       | 100.0% |
<details>
<summary>🌀 Click to see Generated Regression Tests</summary>

```python
from __future__ import annotations

import re

# imports
import pytest  # used for our unit tests

from testgen.testgen_javascript import _has_test_functions


def test_basic_test_call_double_quotes():
    # Basic: a standard Jest test call using double quotes should be detected.
    code = 'test("my test name", () => { expect(true).toBe(true); });'
    codeflash_output = _has_test_functions(code)  # 2.83μs -> 1.78μs (59.3% faster)


def test_basic_it_call_single_quotes():
    # Basic: a standard it() call using single quotes should be detected.
    code = "it('does something', function() { /* ... */ });"
    codeflash_output = _has_test_functions(code)  # 2.80μs -> 1.83μs (53.0% faster)


def test_whitespace_and_newlines_between_name_and_paren():
    # Edge: whitespace/newlines between the function name and '(' and between '(' and the quote
    # The regex allows arbitrary whitespace, so this should still match.
    code = "it   \n (\n  'handles newlines'\n )"
    codeflash_output = _has_test_functions(code)  # 2.90μs -> 1.89μs (53.2% faster)


def test_empty_string_returns_false():
    # Edge: empty input must return False (no tests found).
    code = ""
    codeflash_output = _has_test_functions(code)  # 1.80μs -> 750ns (139% faster)


def test_uppercase_function_name_not_matched():
    # Edge: the regex is case-sensitive; 'Test' should NOT match.
    code = "Test('capitalized should not match', () => {});"
    codeflash_output = _has_test_functions(code)  # 3.20μs -> 2.23μs (43.7% faster)


def test_backtick_template_not_matched():
    # Edge: template literals use backticks; pattern looks only for single/double quotes.
    code = "test(`template literal name`, () => {});"
    codeflash_output = _has_test_functions(code)  # 3.34μs -> 2.27μs (47.0% faster)


def test_numeric_first_arg_not_matched():
    # Edge: if the first argument is not a quoted string (e.g., a number), pattern should not match.
    code = "test(123, () => {});"
    codeflash_output = _has_test_functions(code)  # 3.01μs -> 1.86μs (61.7% faster)


def test_test_call_inside_comment_still_matches():
    # Important behavioral note: the function does not ignore comments.
    # A 'test(' occurrence inside a JS comment still matches because the function only does regex search.
    code = "// test('in a single-line comment')\n/* test(\"in block comment\") */"
    # Both comment forms contain test('...') / test("...") which the regex will find.
    codeflash_output = _has_test_functions(code)  # 2.96μs -> 1.84μs (61.1% faster)


def test_substring_in_identifier_matches():
    # The regex is permissive and will match occurrences where 'test' or 'it' appear as suffixes
    # of other identifiers (e.g., 'latesttest(' or 'split('). This test documents that behavior.
    code_latest = "function latesttest(){}\nlatesttest('x')"
    code_split = "const arr = ['a']; arr.split('a');"
    # Both contain the substring "test('..." or "it('...", so they should be considered matches by the implementation.
    codeflash_output = _has_test_functions(
        code_latest
    )  # 3.77μs -> 2.59μs (45.5% faster)
    codeflash_output = _has_test_functions(code_split)  # 1.16μs -> 793ns (46.7% faster)


def test_comment_between_paren_blocks_prevents_match():
    # If there is a non-whitespace token (like a block comment) between '(' and the starting quote,
    # the current regex will not match because it expects only whitespace between '(' and the quote.
    code = "test(/* important note */ 'name in comment')"
    codeflash_output = _has_test_functions(code)  # 3.31μs -> 2.30μs (43.5% faster)


def test_multiple_test_and_it_occurrences():
    # A file with multiple matches should still return True (boolean).
    code = """
    describe('suite', () => {
      it('first case', () => {});
      // some other code
      test("second case", () => {});
    });
    """
    codeflash_output = _has_test_functions(code)  # 3.34μs -> 2.16μs (54.9% faster)


def test_large_scale_no_match_performance():
    # Large-scale: many lines without any test/it(...) occurrences should return False.
    # Keep size under 1000 to respect constraints. We use 900 repeated lines.
    repeated = "const filler = 0;\n" * 900  # 900 lines of filler
    codeflash_output = _has_test_functions(repeated)  # 76.6μs -> 75.5μs (1.47% faster)


def test_large_scale_match_near_end():
    # Large-scale: many lines of filler followed by a single test at the end should return True.
    # This ensures the search scans through large input and finds a late occurrence.
    repeated = "const filler = 0;\n" * 900  # 900 lines of filler
    code = repeated + "  // real test follows\n  test('final case', () => {});"
    codeflash_output = _has_test_functions(code)  # 77.5μs -> 76.3μs (1.59% faster)


def test_it_with_newline_between_name_and_paren():
    # Verify that a newline immediately after 'it' and before '(' is allowed by the regex (\s* covers newline).
    code = "it\n('newline-allowed')"
    codeflash_output = _has_test_functions(code)  # 3.22μs -> 1.94μs (66.0% faster)


def test_quoted_string_with_escaped_quotes_still_matches():
    # Even if the string contains escaped quotes, the regex only checks the opening quote, so it should match.
    code = r'test("contains an escaped quote: \" here", () => {});'
    codeflash_output = _has_test_functions(code)  # 2.90μs -> 1.83μs (58.2% faster)


# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

```

```python
import re

import pytest

from testgen.testgen_javascript import _has_test_functions


def test_simple_test_function_with_single_quotes():
    """Test detection of test() function with single quotes."""
    code = "test('should work', () => {})"
    codeflash_output = _has_test_functions(code)  # 3.78μs -> 2.29μs (65.1% faster)


def test_simple_it_function_with_single_quotes():
    """Test detection of it() function with single quotes."""
    code = "it('should work', () => {})"
    codeflash_output = _has_test_functions(code)  # 3.10μs -> 1.99μs (56.0% faster)


def test_simple_test_function_with_double_quotes():
    """Test detection of test() function with double quotes."""
    code = 'test("should work", () => {})'
    codeflash_output = _has_test_functions(code)  # 3.07μs -> 1.89μs (62.8% faster)


def test_simple_it_function_with_double_quotes():
    """Test detection of it() function with double quotes."""
    code = 'it("should work", () => {})'
    codeflash_output = _has_test_functions(code)  # 3.13μs -> 1.86μs (68.2% faster)


def test_no_test_functions():
    """Test code without any test functions returns False."""
    code = "function myFunction() { return 42; }"
    codeflash_output = _has_test_functions(code)  # 3.08μs -> 1.85μs (66.5% faster)


def test_test_function_with_single_whitespace():
    """Test detection with single space between function name and parenthesis."""
    code = "test ('my test', () => {})"
    codeflash_output = _has_test_functions(code)  # 3.20μs -> 1.96μs (63.0% faster)


def test_it_function_with_single_whitespace():
    """Test detection with single space between function name and parenthesis."""
    code = "it ('my test', () => {})"
    codeflash_output = _has_test_functions(code)  # 2.98μs -> 1.88μs (58.5% faster)


def test_test_function_with_multiple_whitespaces():
    """Test detection with multiple spaces between function name and parenthesis."""
    code = "test   ('my test', () => {})"
    codeflash_output = _has_test_functions(code)  # 3.04μs -> 1.95μs (55.5% faster)


def test_it_function_with_multiple_whitespaces():
    """Test detection with multiple spaces between function name and parenthesis."""
    code = "it   ('my test', () => {})"
    codeflash_output = _has_test_functions(code)  # 3.06μs -> 1.89μs (62.1% faster)


def test_test_function_with_tab_character():
    """Test detection with tab character between function name and parenthesis."""
    code = "test\t('my test', () => {})"
    codeflash_output = _has_test_functions(code)  # 3.05μs -> 1.93μs (57.8% faster)


def test_it_function_with_tab_character():
    """Test detection with tab character between function name and parenthesis."""
    code = "it\t('my test', () => {})"
    codeflash_output = _has_test_functions(code)  # 2.99μs -> 1.85μs (61.6% faster)


def test_test_function_with_newline():
    """Test detection with newline between function name and parenthesis."""
    code = "test\n('my test', () => {})"
    codeflash_output = _has_test_functions(code)  # 3.02μs -> 1.93μs (56.5% faster)


def test_it_function_with_newline():
    """Test detection with newline between function name and parenthesis."""
    code = "it\n('my test', () => {})"
    codeflash_output = _has_test_functions(code)  # 2.95μs -> 1.90μs (55.4% faster)


def test_multiple_test_functions():
    """Test detection with multiple test functions in the same code."""
    code = """
    test('first test', () => {});
    it('second test', () => {});
    """
    codeflash_output = _has_test_functions(code)  # 3.12μs -> 1.92μs (62.3% faster)


def test_test_function_in_multiline_code():
    """Test detection of test function within multiline code."""
    code = """
    const helper = () => {};
    test('actual test', () => {});
    const another = () => {};
    """
    codeflash_output = _has_test_functions(code)  # 3.19μs -> 2.12μs (50.1% faster)


def test_it_function_in_multiline_code():
    """Test detection of it function within multiline code."""
    code = """
    const helper = () => {};
    it('actual test', () => {});
    const another = () => {};
    """
    codeflash_output = _has_test_functions(code)  # 3.46μs -> 2.23μs (55.1% faster)


def test_test_word_in_comment_not_matched():
    """Test that test() in comments is still detected by regex (no comment parsing)."""
    code = "// test('in comment', () => {})"
    # Note: The function uses regex without comment awareness, so it will match
    codeflash_output = _has_test_functions(code)  # 3.10μs -> 1.94μs (60.2% faster)


def test_test_word_in_string_variable():
    """Test that test word in string variable doesn't match pattern."""
    code = 'const description = "this is a test of something";'
    codeflash_output = _has_test_functions(code)  # 3.56μs -> 2.29μs (55.3% faster)


def test_test_as_variable_name_not_matched():
    """Test that 'test' as variable name doesn't match without parenthesis."""
    code = "const test = 5;"
    codeflash_output = _has_test_functions(code)  # 3.11μs -> 2.03μs (53.1% faster)


def test_testing_as_word_not_matched():
    """Test that 'testing' word doesn't match."""
    code = "const testing = 'some value';"
    codeflash_output = _has_test_functions(code)  # 3.17μs -> 2.04μs (55.4% faster)


def test_it_as_pronoun_not_matched():
    """Test that 'it' as pronoun doesn't match without proper pattern."""
    code = "// it is a good day"
    codeflash_output = _has_test_functions(code)  # 3.15μs -> 1.98μs (59.1% faster)


def test_it_as_variable_not_matched():
    """Test that 'it' as variable name doesn't match without parenthesis."""
    code = "const it = 5;"
    codeflash_output = _has_test_functions(code)  # 3.13μs -> 1.92μs (63.7% faster)


def test_empty_string():
    """Test with empty string input."""
    codeflash_output = _has_test_functions("")  # 1.74μs -> 757ns (129% faster)


def test_only_whitespace():
    """Test with only whitespace."""
    codeflash_output = _has_test_functions("   \n\t  ")  # 2.05μs -> 890ns (131% faster)


def test_test_function_with_special_test_name():
    """Test detection with special characters in test name."""
    code = "test('test-name_123!@#', () => {})"
    codeflash_output = _has_test_functions(code)  # 2.95μs -> 1.90μs (55.1% faster)


def test_it_function_with_special_test_name():
    """Test detection with special characters in test name."""
    code = "it('it-name_123!@#', () => {})"
    codeflash_output = _has_test_functions(code)  # 3.04μs -> 1.88μs (62.0% faster)


def test_test_function_with_empty_string_name():
    """Test detection with empty string as test name."""
    code = "test('', () => {})"
    codeflash_output = _has_test_functions(code)  # 2.90μs -> 1.84μs (58.0% faster)


def test_it_function_with_empty_string_name():
    """Test detection with empty string as test name."""
    code = "it('', () => {})"
    codeflash_output = _has_test_functions(code)  # 2.97μs -> 1.86μs (59.7% faster)


def test_test_with_carriage_return():
    """Test detection with carriage return character."""
    code = "test\r('my test', () => {})"
    codeflash_output = _has_test_functions(code)  # 3.02μs -> 1.89μs (60.2% faster)


def test_it_with_carriage_return():
    """Test detection with carriage return character."""
    code = "it\r('my test', () => {})"
    codeflash_output = _has_test_functions(code)  # 3.12μs -> 1.91μs (63.8% faster)


def test_test_with_form_feed():
    """Test detection with form feed character."""
    code = "test\f('my test', () => {})"
    codeflash_output = _has_test_functions(code)  # 2.98μs -> 1.85μs (60.7% faster)


def test_it_with_form_feed():
    """Test detection with form feed character."""
    code = "it\f('my test', () => {})"
    codeflash_output = _has_test_functions(code)  # 3.13μs -> 1.91μs (64.4% faster)


def test_test_with_vertical_tab():
    """Test detection with vertical tab character."""
    code = "test\v('my test', () => {})"
    codeflash_output = _has_test_functions(code)  # 3.08μs -> 1.87μs (65.0% faster)


def test_it_with_vertical_tab():
    """Test detection with vertical tab character."""
    code = "it\v('my test', () => {})"
    codeflash_output = _has_test_functions(code)  # 3.13μs -> 1.83μs (71.4% faster)


def test_test_with_non_breaking_space():
    """Test that non-breaking space might not work depending on whitespace regex."""
    code = "test\u00a0('my test', () => {})"
    # Non-breaking space might not be treated as \s in regex
    codeflash_output = _has_test_functions(code)
    result = codeflash_output  # 3.25μs -> 2.07μs (56.6% faster)


def test_test_with_zero_width_space():
    """Test with zero-width space."""
    code = "test\u200b('my test', () => {})"
    codeflash_output = _has_test_functions(code)
    result = codeflash_output  # 3.82μs -> 2.74μs (39.4% faster)


def test_only_test_keyword():
    """Test with only the word 'test' without parenthesis."""
    code = "test"
    codeflash_output = _has_test_functions(code)  # 2.85μs -> 1.83μs (55.3% faster)


def test_only_it_keyword():
    """Test with only the word 'it' without parenthesis."""
    code = "it"
    codeflash_output = _has_test_functions(code)  # 1.89μs -> 793ns (138% faster)


def test_test_with_parenthesis_but_no_quote():
    """Test function call without string argument."""
    code = "test(variable)"
    codeflash_output = _has_test_functions(code)  # 3.39μs -> 2.12μs (59.8% faster)


def test_it_with_parenthesis_but_no_quote():
    """Test it function call without string argument."""
    code = "it(variable)"
    codeflash_output = _has_test_functions(code)  # 3.24μs -> 2.07μs (56.5% faster)


def test_test_followed_by_string_literal_without_parenthesis():
    """Test with string literal but missing parenthesis."""
    code = "test 'string'"
    codeflash_output = _has_test_functions(code)  # 3.17μs -> 1.93μs (64.5% faster)


def test_it_followed_by_string_literal_without_parenthesis():
    """Test with string literal but missing parenthesis."""
    code = "it 'string'"
    codeflash_output = _has_test_functions(code)  # 3.24μs -> 2.02μs (59.9% faster)


def test_test_with_backtick_quotes():
    """Test with backtick quotes (template literals)."""
    code = "test(`my test`, () => {})"
    codeflash_output = _has_test_functions(code)  # 3.49μs -> 2.34μs (49.5% faster)


def test_it_with_backtick_quotes():
    """Test it with backtick quotes (template literals)."""
    code = "it(`my test`, () => {})"
    codeflash_output = _has_test_functions(code)  # 3.38μs -> 2.23μs (51.4% faster)


def test_describe_function_not_matched():
    """Test that describe() function is not matched."""
    code = "describe('suite', () => {})"
    codeflash_output = _has_test_functions(code)  # 3.13μs -> 1.94μs (61.2% faster)


def test_beforeEach_function_not_matched():
    """Test that beforeEach() function is not matched."""
    code = "beforeEach(() => {})"
    codeflash_output = _has_test_functions(code)  # 1.98μs -> 901ns (119% faster)


def test_afterEach_function_not_matched():
    """Test that afterEach() function is not matched."""
    code = "afterEach(() => {})"
    codeflash_output = _has_test_functions(code)  # 2.68μs -> 1.50μs (78.6% faster)


def test_test_method_on_object():
    """Test with test as method call on object."""
    code = "obj.test('my test', () => {})"
    codeflash_output = _has_test_functions(code)  # 3.12μs -> 1.94μs (61.4% faster)


def test_it_method_on_object():
    """Test with it as method call on object."""
    code = "obj.it('my test', () => {})"
    codeflash_output = _has_test_functions(code)  # 3.10μs -> 2.00μs (55.0% faster)


def test_test_substring_in_longer_identifier():
    """Test when test is part of longer identifier."""
    code = "mytest('my test', () => {})"
    codeflash_output = _has_test_functions(code)  # 3.00μs -> 1.90μs (58.0% faster)


def test_it_substring_in_longer_identifier():
    """Test when it is part of longer identifier."""
    code = "unit('my test', () => {})"
    codeflash_output = _has_test_functions(code)  # 3.07μs -> 1.94μs (58.2% faster)


def test_test_with_unicode_test_name():
    """Test detection with unicode characters in test name."""
    code = "test('\u4e2d\u6587\u6d4b\u8bd5', () => {})"
    codeflash_output = _has_test_functions(code)  # 3.57μs -> 2.31μs (54.4% faster)


def test_it_with_unicode_test_name():
    """Test detection with unicode characters in test name."""
    code = "it('\u4e2d\u6587\u6d4b\u8bd5', () => {})"
    codeflash_output = _has_test_functions(code)  # 3.09μs -> 2.08μs (48.8% faster)


def test_test_with_emoji():
    """Test detection with emoji in test name."""
    code = "test('\u263a emoji test', () => {})"
    codeflash_output = _has_test_functions(code)  # 3.14μs -> 2.02μs (55.2% faster)


def test_it_with_emoji():
    """Test detection with emoji in test name."""
    code = "it('\u263a emoji test', () => {})"
    codeflash_output = _has_test_functions(code)  # 3.09μs -> 2.00μs (54.0% faster)


def test_very_long_test_name():
    """Test detection with very long test name."""
    long_name = "a" * 5000
    code = f"test('{long_name}', () => {{}})"
    codeflash_output = _has_test_functions(code)  # 3.00μs -> 1.77μs (69.5% faster)


def test_very_long_code_without_tests():
    """Test with very long code but no test functions."""
    code = "const x = 1;\n" * 500
    codeflash_output = _has_test_functions(code)  # 29.7μs -> 28.5μs (4.29% faster)


def test_test_with_escaped_quote():
    """Test with escaped quote in test name."""
    code = "test('test\\'s name', () => {})"
    codeflash_output = _has_test_functions(code)  # 3.15μs -> 2.03μs (55.2% faster)


def test_it_with_escaped_quote():
    """Test with escaped quote in test name."""
    code = "it('it\\'s name', () => {})"
    codeflash_output = _has_test_functions(code)  # 2.97μs -> 1.87μs (59.0% faster)


def test_test_with_double_quote_in_single_quote():
    """Test with double quote inside single quoted test name."""
    code = "test('has \"double\" quotes', () => {})"
    codeflash_output = _has_test_functions(code)  # 2.93μs -> 1.82μs (60.7% faster)


def test_it_with_double_quote_in_single_quote():
    """Test with double quote inside single quoted test name."""
    code = "it('has \"double\" quotes', () => {})"
    codeflash_output = _has_test_functions(code)  # 2.97μs -> 1.83μs (62.6% faster)


def test_test_with_single_quote_in_double_quote():
    """Test with single quote inside double quoted test name."""
    code = "test(\"has 'single' quotes\", () => {})"
    codeflash_output = _has_test_functions(code)  # 3.00μs -> 1.90μs (57.8% faster)


def test_it_with_single_quote_in_double_quote():
    """Test with single quote inside double quoted test name."""
    code = "it(\"has 'single' quotes\", () => {})"
    codeflash_output = _has_test_functions(code)  # 2.97μs -> 1.78μs (66.7% faster)


def test_test_case_sensitive():
    """Test that TEST (uppercase) is not matched."""
    code = "TEST('my test', () => {})"
    codeflash_output = _has_test_functions(code)  # 3.03μs -> 1.81μs (67.1% faster)


def test_it_case_sensitive():
    """Test that IT (uppercase) is not matched."""
    code = "IT('my test', () => {})"
    codeflash_output = _has_test_functions(code)  # 2.81μs -> 1.84μs (52.8% faster)


def test_test_with_mixed_case():
    """Test that TeSt (mixed case) is not matched."""
    code = "TeSt('my test', () => {})"
    codeflash_output = _has_test_functions(code)  # 3.05μs -> 1.85μs (65.2% faster)


def test_it_with_mixed_case():
    """Test that It (mixed case) is not matched."""
    code = "It('my test', () => {})"
    codeflash_output = _has_test_functions(code)  # 2.94μs -> 1.91μs (54.3% faster)


def test_code_with_many_non_test_functions():
    """Test performance with many non-test functions."""
    # Build code with 500 non-test function definitions
    code_lines = [f"function func{i}() {{ return {i}; }}" for i in range(500)]
    code = "\n".join(code_lines)
    codeflash_output = _has_test_functions(code)  # 77.0μs -> 75.9μs (1.52% faster)


def test_code_with_many_functions_and_one_test():
    """Test detection of single test among many non-test functions."""
    # Build code with 500 non-test functions and 1 test function
    code_lines = [f"function func{i}() {{ return {i}; }}" for i in range(250)]
    code_lines.append("test('the actual test', () => {})")
    code_lines.extend(
        [f"function func{i}() {{ return {i}; }}" for i in range(250, 500)]
    )
    code = "\n".join(code_lines)
    codeflash_output = _has_test_functions(code)  # 40.3μs -> 38.9μs (3.41% faster)


def test_code_with_many_test_functions():
    """Test detection with many test functions."""
    # Build code with 100 test functions
    code_lines = [f"test('test {i}', () => {{}})" for i in range(100)]
    code = "\n".join(code_lines)
    codeflash_output = _has_test_functions(code)  # 2.94μs -> 1.74μs (69.4% faster)


def test_code_with_many_it_functions():
    """Test detection with many it functions."""
    # Build code with 100 it functions
    code_lines = [f"it('test {i}', () => {{}})" for i in range(100)]
    code = "\n".join(code_lines)
    codeflash_output = _has_test_functions(code)  # 2.91μs -> 1.61μs (80.4% faster)


def test_code_with_alternating_test_and_it_functions():
    """Test detection with alternating test and it functions."""
    # Build code with 100 alternating test and it functions
    code_lines = []
    for i in range(50):
        code_lines.append(f"test('test {i}', () => {{}})")
        code_lines.append(f"it('it {i}', () => {{}})")
    code = "\n".join(code_lines)
    codeflash_output = _has_test_functions(code)  # 2.93μs -> 1.64μs (78.7% faster)


def test_code_with_many_non_matching_similar_patterns():
    """Test performance with many similar but non-matching patterns."""
    # Build code with 500 similar patterns that don't match
    code_lines = [f"test{i}('name', () => {{}})" for i in range(500)]
    code = "\n".join(code_lines)
    codeflash_output = _has_test_functions(code)  # 64.3μs -> 62.9μs (2.27% faster)


def test_large_code_with_test_at_end():
    """Test detection when test function is at end of large code."""
    # Build code with 500 lines and test at the end
    code_lines = [f"const var{i} = {i};" for i in range(500)]
    code_lines.append("test('test at end', () => {})")
    code = "\n".join(code_lines)
    codeflash_output = _has_test_functions(code)  # 41.4μs -> 40.3μs (2.65% faster)


def test_large_code_with_it_at_end():
    """Test detection when it function is at end of large code."""
    # Build code with 500 lines and it at the end
    code_lines = [f"const var{i} = {i};" for i in range(500)]
    code_lines.append("it('it at end', () => {})")
    code = "\n".join(code_lines)
    codeflash_output = _has_test_functions(code)  # 41.5μs -> 40.3μs (3.00% faster)


def test_large_code_with_test_at_beginning():
    """Test detection when test function is at beginning of large code."""
    # Build code with test at beginning and 500 lines after
    code_lines = ["test('test at beginning', () => {})"]
    code_lines.extend([f"const var{i} = {i};" for i in range(500)])
    code = "\n".join(code_lines)
    codeflash_output = _has_test_functions(code)  # 3.09μs -> 1.90μs (62.2% faster)


def test_large_code_with_it_at_beginning():
    """Test detection when it function is at beginning of large code."""
    # Build code with it at beginning and 500 lines after
    code_lines = ["it('it at beginning', () => {})"]
    code_lines.extend([f"const var{i} = {i};" for i in range(500)])
    code = "\n".join(code_lines)
    codeflash_output = _has_test_functions(code)  # 3.02μs -> 1.81μs (67.1% faster)


def test_code_with_multiple_tests_scattered():
    """Test detection with multiple test functions scattered throughout large code."""
    # Build code with 20 test functions scattered among 480 non-test lines
    code_lines = []
    for i in range(500):
        if i % 25 == 0:
            code_lines.append(f"test('scattered test {i}', () => {{}})")
        else:
            code_lines.append(f"const var{i} = {i};")
    code = "\n".join(code_lines)
    codeflash_output = _has_test_functions(code)  # 2.86μs -> 1.79μs (59.8% faster)


def test_code_with_very_large_test_name():
    """Test performance with very long test name."""
    # Create a test with name of 10000 characters
    long_name = "x" * 10000
    code = f"test('{long_name}', () => {{}})"
    codeflash_output = _has_test_functions(code)  # 2.99μs -> 1.84μs (62.1% faster)


def test_code_with_deeply_nested_structures():
    """Test detection in deeply nested code structures."""
    # Build nested structure with test at bottom
    code = "const nested = { level1: { level2: { level3: { level4: { " * 50
    code += "test('nested test', () => {})"
    code += " } } } } };" * 50
    codeflash_output = _has_test_functions(code)  # 14.6μs -> 13.5μs (8.56% faster)


def test_code_with_many_whitespace_variations():
    """Test detection with many different whitespace patterns."""
    code_lines = []
    for i in range(100):
        if i % 4 == 0:
            code_lines.append(f"test('test {i}', () => {{}})")
        elif i % 4 == 1:
            code_lines.append(f"test ('test {i}', () => {{}})")
        elif i % 4 == 2:
            code_lines.append(f"test  ('test {i}', () => {{}})")
        else:
            code_lines.append(f"test\t('test {i}', () => {{}})")
    code = "\n".join(code_lines)
    codeflash_output = _has_test_functions(code)  # 2.92μs -> 1.71μs (70.7% faster)


def test_code_return_type_is_boolean():
    """Test that return value is always boolean regardless of input size."""
    # Various test inputs
    test_inputs = [
        "",
        "test",
        "test('name', () => {})",
        "const x = 1;" * 100,
        "test('name', () => {})" + "const x = 1;" * 100,
    ]
    for test_input in test_inputs:
        codeflash_output = _has_test_functions(test_input)
        result = codeflash_output  # 11.9μs -> 9.34μs (27.0% faster)


# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

```

</details>


To edit these changes `git checkout
codeflash/optimize-pr2247-2026-01-25T08.57.25` and push.


[![Codeflash](https://img.shields.io/badge/Optimized%20with-Codeflash-yellow?style=flat&color=%23ffc428&logo=data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iNDgwIiBoZWlnaHQ9ImF1dG8iIHZpZXdCb3g9IjAgMCA0ODAgMjgwIiBmaWxsPSJub25lIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPgo8cGF0aCBmaWxsLXJ1bGU9ImV2ZW5vZGQiIGNsaXAtcnVsZT0iZXZlbm9kZCIgZD0iTTI4Ni43IDAuMzc4NDE4SDIwMS43NTFMNTAuOTAxIDE0OC45MTFIMTM1Ljg1MUwwLjk2MDkzOCAyODEuOTk5SDk1LjQzNTJMMjgyLjMyNCA4OS45NjE2SDE5Ni4zNDVMMjg2LjcgMC4zNzg0MThaIiBmaWxsPSIjRkZDMDQzIi8+CjxwYXRoIGZpbGwtcnVsZT0iZXZlbm9kZCIgY2xpcC1ydWxlPSJldmVub2RkIiBkPSJNMzExLjYwNyAwLjM3ODkwNkwyNTguNTc4IDU0Ljk1MjZIMzc5LjU2N0w0MzIuMzM5IDAuMzc4OTA2SDMxMS42MDdaIiBmaWxsPSIjMEIwQTBBIi8+CjxwYXRoIGZpbGwtcnVsZT0iZXZlbm9kZCIgY2xpcC1ydWxlPSJldmVub2RkIiBkPSJNMzA5LjU0NyA4OS45NjAxTDI1Ni41MTggMTQ0LjI3NkgzNzcuNTA2TDQzMC4wMjEgODkuNzAyNkgzMDkuNTQ3Vjg5Ljk2MDFaIiBmaWxsPSIjMEIwQTBBIi8+CjxwYXRoIGZpbGwtcnVsZT0iZXZlbm9kZCIgY2xpcC1ydWxlPSJldmVub2RkIiBkPSJNMjQyLjg3MyAxNjQuNjZMMTg5Ljg0NCAyMTkuMjM0SDMxMC44MzNMMzYzLjM0NyAxNjQuNjZIMjQyLjg3M1oiIGZpbGw9IiMwQjBBMEEiLz4KPC9zdmc+Cg==)](https://codeflash.ai)
![Static
Badge](https://img.shields.io/badge/🎯_Optimization_Quality-high-green)

Co-authored-by: codeflash-ai[bot] <148906541+codeflash-ai[bot]@users.noreply.github.com>
Co-authored-by: Kevin Turcios <106575910+KRRT7@users.noreply.github.com>
2026-01-27 00:14:02 -05:00
Kevin Turcios
bf1d214ece format 2026-01-27 00:00:49 -05:00
Kevin Turcios
764a3f8899
Merge branch 'main' into multi-language 2026-01-26 23:59:03 -05:00
Kevin Turcios
0444e32f77
fix: CST tree handling and testgen pipeline improvements (#2310)
## Summary
- Fix CST tree corruption issues that caused 'NoneType' object has no
attribute 'visit' errors
- Consolidate testgen postprocessing into a single pipeline with
tuple-based pattern
- Improve markdown code extraction to prefer filepath-annotated blocks
- Add diagnostic context to optimization failure logs

## Changes
- Handle empty `SimpleStatementLine` and `StatementHandler` body to
prevent malformed CST
- Add trace_id logging to optimization and import failure paths
- Refactor testgen postprocessing into consolidated pipeline
- Fix code extraction for LLM responses with multiple code blocks

## Test plan
- [x] Added integration tests for full testgen pipeline
- [x] Added tests for markdown extraction with filepath preference
- [x] Existing tests pass

---------

Co-authored-by: codeflash-ai[bot] <148906541+codeflash-ai[bot]@users.noreply.github.com>
2026-01-26 23:57:55 -05:00
ali
580da4cc8e
fix unit tests 2026-01-26 19:57:38 +02:00
ali
e559b6b9b4
handle repair multi-language 2026-01-26 16:41:58 +02:00
ali
7a8661ae31
fix line profiler optimizations 2026-01-26 15:19:24 +02:00
Kevin Turcios
80be7d2dae fix: resolve type errors and linting issues
- Add language parameter to split_markdown_code and group_code for JS/TS support
- Fix callable type annotation in instrument_javascript.py
- Update testgen_javascript.py to use ChatCompletionMessageParam types
- Add None checks before parse_python_version calls
- Add missing None assertions in test files
- Apply ruff auto-fixes for formatting and unused imports
2026-01-25 01:17:44 -05:00
Kevin Turcios
549f99eb97 formatting 2026-01-25 01:04:50 -05:00
Kevin Turcios
f04ac67728 merge: resolve conflicts with main
- Accept consolidated markdown utilities from common module
- Use wrap_code_in_markdown with language parameter for language support
- Remove duplicate split_markdown_code implementation
- Add validation for python_version before parsing
2026-01-25 01:00:56 -05:00
Kevin Turcios
d091cd17fe
refactor: consolidate shared utilities (#2289)
- Consolidate shared utilities into `aiservice/common/`
- Remove profanity filtering from optimization pipeline
- Add unit tests for common module
2026-01-25 00:49:48 -05:00
Kevin Turcios
df9bef20c4
refactor: remove profanity filtering from optimization pipeline (#2288)
## Summary
- Removes `profanity_regex` and `profanity_words` from
`postprocess_constants.py`
- Removes `remove_profanity_from_explanation` from the optimization
pipeline
- Removes associated test
2026-01-24 23:46:03 -05:00
Kevin Turcios
3a304aa00f
Merge branch 'main' into multi-language 2026-01-24 17:42:16 -05:00
Kevin Turcios
7aa4da74f8
feat: improve testgen import resolution and forward reference handling (#2276)
## Summary
- Add forward reference detection and automatic fix with `from
__future__ import annotations`
- Handle aliased imports and chained calls in test instrumentation
- Fix import resolution from correct module in multi-context testgen
- Allow ellipsis in Protocol/abstract method bodies
- Add dataclass constructor notes for LLM about required/positional
arguments
- Add logging to silent exception handlers

## Test plan
- [x] Unit tests added for forward reference detection
- [x] Unit tests added for dataclass constructor notes
- [x] Unit tests added for ellipsis handling in AST
- [x] Unit tests added for chained call instrumentation
- [x] Unit tests extended for add_missing_imports

---------

Co-authored-by: codeflash-ai[bot] <148906541+codeflash-ai[bot]@users.noreply.github.com>
2026-01-24 17:39:38 -05:00
Aseem Saxena
b62c9dceab api veersion 2026-01-23 08:55:05 -08:00
Kevin Turcios
f534926059
more context for asyncs (#2273)
# Pull Request Checklist

## Description
- [ ] **Description of PR**: Clear and concise description of what this
PR accomplishes
- [ ] **Breaking Changes**: Document any breaking changes (if
applicable)
- [ ] **Related Issues**: Link to any related issues or tickets

## Testing
- [ ] **Test cases Attached**: All relevant test cases have been
added/updated
- [ ] **Manual Testing**: Manual testing completed for the changes

## Monitoring & Debugging
- [ ] **Logging in place**: Appropriate logging has been added for
debugging user issues
- [ ] **Sentry will be able to catch errors**: Error handling ensures
Sentry can capture and report errors
- [ ] **Avoid Dev based/Prisma logging**: No development-only or
Prisma-specific logging in production code

## Configuration
- [ ] **Env variables newly added**: Any new environment variables are
documented in .env.example file or mentioned in description
---

## Additional Notes
<!-- Add any additional context, screenshots, or notes for reviewers
here -->
2026-01-23 05:25:24 -05:00
Kevin Turcios
8c75f238ed
Fix async crashes (#2270)
# Pull Request Checklist

## Description
- [ ] **Description of PR**: Clear and concise description of what this
PR accomplishes
- [ ] **Breaking Changes**: Document any breaking changes (if
applicable)
- [ ] **Related Issues**: Link to any related issues or tickets

## Testing
- [ ] **Test cases Attached**: All relevant test cases have been
added/updated
- [ ] **Manual Testing**: Manual testing completed for the changes

## Monitoring & Debugging
- [ ] **Logging in place**: Appropriate logging has been added for
debugging user issues
- [ ] **Sentry will be able to catch errors**: Error handling ensures
Sentry can capture and report errors
- [ ] **Avoid Dev based/Prisma logging**: No development-only or
Prisma-specific logging in production code

## Configuration
- [ ] **Env variables newly added**: Any new environment variables are
documented in .env.example file or mentioned in description
---

## Additional Notes
<!-- Add any additional context, screenshots, or notes for reviewers
here -->
2026-01-23 00:51:35 -05:00
Kevin Turcios
2d8da15932
Acceptance reason in explanation for async (#2207)
# Pull Request Checklist

## Description
- [ ] **Description of PR**: Clear and concise description of what this
PR accomplishes
- [ ] **Breaking Changes**: Document any breaking changes (if
applicable)
- [ ] **Related Issues**: Link to any related issues or tickets

## Testing
- [ ] **Test cases Attached**: All relevant test cases have been
added/updated
- [ ] **Manual Testing**: Manual testing completed for the changes

## Monitoring & Debugging
- [ ] **Logging in place**: Appropriate logging has been added for
debugging user issues
- [ ] **Sentry will be able to catch errors**: Error handling ensures
Sentry can capture and report errors
- [ ] **Avoid Dev based/Prisma logging**: No development-only or
Prisma-specific logging in production code

## Configuration
- [ ] **Env variables newly added**: Any new environment variables are
documented in .env.example file or mentioned in description
---

## Additional Notes
<!-- Add any additional context, screenshots, or notes for reviewers
here -->
2026-01-22 22:28:44 -05:00
Sarthak Agarwal
b095f5d9b8
Merge branch 'main' into multi-language 2026-01-23 03:34:13 +05:30
ali
7f795cf941
remove capture perf looped 2026-01-21 21:49:42 +02:00
Sarthak Agarwal
47ecce6e4f fix refinment add language param 2026-01-21 22:09:58 +05:30
Kevin Turcios
6f6b5e47e6
replace_definition_with_import as early as possible (#2264)
# Pull Request Checklist

## Description
- [ ] **Description of PR**: Clear and concise description of what this
PR accomplishes
- [ ] **Breaking Changes**: Document any breaking changes (if
applicable)
- [ ] **Related Issues**: Link to any related issues or tickets

## Testing
- [ ] **Test cases Attached**: All relevant test cases have been
added/updated
- [ ] **Manual Testing**: Manual testing completed for the changes

## Monitoring & Debugging
- [ ] **Logging in place**: Appropriate logging has been added for
debugging user issues
- [ ] **Sentry will be able to catch errors**: Error handling ensures
Sentry can capture and report errors
- [ ] **Avoid Dev based/Prisma logging**: No development-only or
Prisma-specific logging in production code

## Configuration
- [ ] **Env variables newly added**: Any new environment variables are
documented in .env.example file or mentioned in description
---

## Additional Notes
<!-- Add any additional context, screenshots, or notes for reviewers
here -->
2026-01-20 17:42:29 -05:00
ali
3722c3db22
multi-file context support for js/ts 2026-01-20 19:34:11 +02:00
Sarthak Agarwal
56304b5669 line_profiling_changes 2026-01-20 03:58:35 +05:30
Sarthak Agarwal
229f3c8f1a add ts verification for candidate 2026-01-19 21:44:13 +05:30
Kevin Turcios
e793993d12
Merge branch 'main' into cf-1014 2026-01-19 10:58:43 -05:00
Kevin Turcios
3a79f03f0a
fix: improve test generation parsing and validation (#2259)
## Summary
- Remove unicode quote sanitization from test code validation
- Rely on individual test validation to filter out tests with syntax
errors (including unicode characters)

## Test plan
- [x] Existing tests pass
- [x] Tests with unicode quote syntax errors are correctly filtered out
during individual validation
2026-01-19 10:23:26 -05:00
Sarthak Agarwal
de7dffbb11 add debug logs 2026-01-19 18:53:13 +05:30
Kevin Turcios
48b667062b fix: exclude loop variables from test import fallback
Make get_referenced_names_from_source scope-aware by reusing
UndefinedNameCollector, preventing invalid imports like `i` and `v`
from loop variables in AI-generated tests.
2026-01-18 04:53:34 -05:00
Kevin Turcios
53baee3994
test: add normalize_code helper and update test assertions (#2255)
## Summary
- Add `normalize_code` helper in `tests/conftest.py` for comparing code
while ignoring quote style differences
- Update test assertions to use `normalize_code()` wrapper
- Add unit tests for comprehension instrumentation cases
2026-01-18 04:03:21 -05:00
Kevin Turcios
fd16c8b889
Typeagent fixes (#2256)
# Pull Request Checklist

## Description
- [ ] **Description of PR**: Clear and concise description of what this
PR accomplishes
- [ ] **Breaking Changes**: Document any breaking changes (if
applicable)
- [ ] **Related Issues**: Link to any related issues or tickets

## Testing
- [ ] **Test cases Attached**: All relevant test cases have been
added/updated
- [ ] **Manual Testing**: Manual testing completed for the changes

## Monitoring & Debugging
- [ ] **Logging in place**: Appropriate logging has been added for
debugging user issues
- [ ] **Sentry will be able to catch errors**: Error handling ensures
Sentry can capture and report errors
- [ ] **Avoid Dev based/Prisma logging**: No development-only or
Prisma-specific logging in production code

## Configuration
- [ ] **Env variables newly added**: Any new environment variables are
documented in .env.example file or mentioned in description
---

## Additional Notes
<!-- Add any additional context, screenshots, or notes for reviewers
here -->
2026-01-18 03:58:51 -05:00
Kevin Turcios
caf48ac5c3
fix: skip instrumentation for function calls inside comprehensions (#2254)
## Summary
- Skip instrumentation for target function calls inside list/set/dict
comprehensions and generator expressions
- Fixes NameError when AI-generated tests use comprehensions like
`[func(x) for x in items]`

---------

Co-authored-by: codeflash-ai[bot] <148906541+codeflash-ai[bot]@users.noreply.github.com>
2026-01-18 03:58:14 -05:00
Kevin Turcios
b64776e284
use AsyncAzureOpenAI (#2253)
# Pull Request Checklist

## Description
- [ ] **Description of PR**: Clear and concise description of what this
PR accomplishes
- [ ] **Breaking Changes**: Document any breaking changes (if
applicable)
- [ ] **Related Issues**: Link to any related issues or tickets

## Testing
- [ ] **Test cases Attached**: All relevant test cases have been
added/updated
- [ ] **Manual Testing**: Manual testing completed for the changes

## Monitoring & Debugging
- [ ] **Logging in place**: Appropriate logging has been added for
debugging user issues
- [ ] **Sentry will be able to catch errors**: Error handling ensures
Sentry can capture and report errors
- [ ] **Avoid Dev based/Prisma logging**: No development-only or
Prisma-specific logging in production code

## Configuration
- [ ] **Env variables newly added**: Any new environment variables are
documented in .env.example file or mentioned in description
---

## Additional Notes
<!-- Add any additional context, screenshots, or notes for reviewers
here -->
2026-01-17 22:44:12 -05:00
Aseem Saxena
14d64da083
Additional Instructions for writing tests compatible with JIT compiled code (#2204)
Plan is to have it behind a jit flag (False by default, passed from cli)
until we have tested it thoroughly.
2026-01-16 10:07:49 -08:00
misrasaurabh1
d8095c7424 Make it better and work again e2e 2026-01-16 10:03:44 -08:00
misrasaurabh1
4570e219a6 fixes and refactor 2026-01-16 00:44:11 -08:00
aseembits93
1a29879603 fix 2026-01-15 21:41:09 -08:00
misrasaurabh1
eaed8a5749 jest looper 2026-01-15 21:40:57 -08:00
misrasaurabh1
4f053eae49 First e2e working version js optimizer!! 2026-01-15 20:40:03 -08:00
misrasaurabh1
0ae49fdd87 checkpoint 2026-01-15 17:31:41 -08:00
misrasaurabh1
0f2dc957d8 checkpoint 2026-01-15 17:00:01 -08:00
misrasaurabh1
1c80984933 checkpoint 2026-01-15 15:57:46 -08:00
misrasaurabh1
6f6bceb233 checkpoint 2026-01-15 15:24:07 -08:00
aseembits93
51ef6aa86b newlines 2026-01-15 12:36:45 -08:00
Aseem Saxena
be392e7808
newline 2026-01-15 12:35:26 -08:00
aseembits93
bb7b898ee3 ready to review 2026-01-15 12:33:10 -08:00
misrasaurabh1
87e09c222a wip instrumentation and execution 2026-01-15 12:13:36 -08:00
Aseem Saxena
bb7b15b27c
Merge branch 'main' into testgen-jit 2026-01-15 11:24:27 -08:00
aseembits93
fa0689c1a6 cleaning up 2026-01-15 10:52:11 -08:00
misrasaurabh1
dc36bf5c57 wip 2026-01-14 23:58:35 -08:00
misrasaurabh1
1a63515f47 Initial js support in aiservice 2026-01-14 22:15:27 -08:00
aseembits93
9c4c7126a4 cleaning up 2026-01-14 19:01:43 -08:00
aseembits93
9d9bf41858 cleaning up 2026-01-14 18:53:04 -08:00
Kevin Turcios
69b55cd319
fix: preserve nested functions in test instrumentation (#2246)
# Pull Request Checklist

## Description
- [ ] **Description of PR**: Clear and concise description of what this
PR accomplishes
- [ ] **Breaking Changes**: Document any breaking changes (if
applicable)
- [ ] **Related Issues**: Link to any related issues or tickets

## Testing
- [ ] **Test cases Attached**: All relevant test cases have been
added/updated
- [ ] **Manual Testing**: Manual testing completed for the changes

## Monitoring & Debugging
- [ ] **Logging in place**: Appropriate logging has been added for
debugging user issues
- [ ] **Sentry will be able to catch errors**: Error handling ensures
Sentry can capture and report errors
- [ ] **Avoid Dev based/Prisma logging**: No development-only or
Prisma-specific logging in production code

## Configuration
- [ ] **Env variables newly added**: Any new environment variables are
documented in .env.example file or mentioned in description
---

## Additional Notes
<!-- Add any additional context, screenshots, or notes for reviewers
here -->
2026-01-14 21:19:24 -05:00
Kevin Turcios
dd0354883f
Handle optional filename in response (#2245)
# Pull Request Checklist

## Description
- [ ] **Description of PR**: Clear and concise description of what this
PR accomplishes
- [ ] **Breaking Changes**: Document any breaking changes (if
applicable)
- [ ] **Related Issues**: Link to any related issues or tickets

## Testing
- [ ] **Test cases Attached**: All relevant test cases have been
added/updated
- [ ] **Manual Testing**: Manual testing completed for the changes

## Monitoring & Debugging
- [ ] **Logging in place**: Appropriate logging has been added for
debugging user issues
- [ ] **Sentry will be able to catch errors**: Error handling ensures
Sentry can capture and report errors
- [ ] **Avoid Dev based/Prisma logging**: No development-only or
Prisma-specific logging in production code

## Configuration
- [ ] **Env variables newly added**: Any new environment variables are
documented in .env.example file or mentioned in description
---

## Additional Notes
<!-- Add any additional context, screenshots, or notes for reviewers
here -->
2026-01-14 20:49:09 -05:00
Kevin Turcios
ca62d68955
fix: prompt adjustment for testgen (#2242)
## Summary
- Preserve nested functions in test instrumentation (skip instrumenting
calls inside nested function definitions)
- Prevent LLM from embedding markdown code fences in generated tests
(updated system prompts)
- Handle optional filename in python code blocks (e.g.,
`\`\`\`python:filename.py`)

## Test plan
- [x] Unit tests added for nested function preservation
- [x] Unit test for optional filename parsing
- [ ] Manual testing with generated tests
2026-01-14 20:27:43 -05:00
Kevin Turcios
286e76a9f0
fix: Improve symbol resolution for test generation (#2241)
## Summary
- Include private symbols when resolving undefined names in generated
tests
- Resolve symbols imported by source module in generated tests  
- Add fallback for symbols referenced but not defined in source snippet
- Ensure LocalDefinitionRemover only removes top-level definitions
(preserves nested classes/functions)
- Performance optimization for `get_symbols_from_source_code` function

## Test plan
- [x] Unit tests added for all new functionality
- [ ] Manual testing with generated tests

---------

Co-authored-by: codeflash-ai[bot] <148906541+codeflash-ai[bot]@users.noreply.github.com>
2026-01-14 20:16:51 -05:00
Aseem Saxena
930382731d
Merge branch 'main' into testgen-jit 2026-01-14 12:03:08 -08:00
Aseem Saxena
521900af83 bug fix and latest instrumentaiton 2026-01-13 02:04:15 +00:00
Kevin Turcios
aa544dc74a
log out trace_id for debugging (#2235)
# Pull Request Checklist

## Description
- [ ] **Description of PR**: Clear and concise description of what this
PR accomplishes
- [ ] **Breaking Changes**: Document any breaking changes (if
applicable)
- [ ] **Related Issues**: Link to any related issues or tickets

## Testing
- [ ] **Test cases Attached**: All relevant test cases have been
added/updated
- [ ] **Manual Testing**: Manual testing completed for the changes

## Monitoring & Debugging
- [ ] **Logging in place**: Appropriate logging has been added for
debugging user issues
- [ ] **Sentry will be able to catch errors**: Error handling ensures
Sentry can capture and report errors
- [ ] **Avoid Dev based/Prisma logging**: No development-only or
Prisma-specific logging in production code

## Configuration
- [ ] **Env variables newly added**: Any new environment variables are
documented in .env.example file or mentioned in description
---

## Additional Notes
<!-- Add any additional context, screenshots, or notes for reviewers
here -->
2026-01-12 20:34:36 -05:00
Kevin Turcios
d20c49ada1
CF-991 improve exception reporting for the aiservice (#2223)
# Pull Request Checklist

## Description
- [ ] **Description of PR**: Clear and concise description of what this
PR accomplishes
- [ ] **Breaking Changes**: Document any breaking changes (if
applicable)
- [ ] **Related Issues**: Link to any related issues or tickets

## Testing
- [ ] **Test cases Attached**: All relevant test cases have been
added/updated
- [ ] **Manual Testing**: Manual testing completed for the changes

## Monitoring & Debugging
- [ ] **Logging in place**: Appropriate logging has been added for
debugging user issues
- [ ] **Sentry will be able to catch errors**: Error handling ensures
Sentry can capture and report errors
- [ ] **Avoid Dev based/Prisma logging**: No development-only or
Prisma-specific logging in production code

## Configuration
- [ ] **Env variables newly added**: Any new environment variables are
documented in .env.example file or mentioned in description
---

## Additional Notes
<!-- Add any additional context, screenshots, or notes for reviewers
here -->

---------

Co-authored-by: codeflash-ai[bot] <148906541+codeflash-ai[bot]@users.noreply.github.com>
2026-01-12 20:18:00 -05:00
aseembits93
7101d9c449 no need to normalize code 2026-01-12 15:59:43 -08:00
aseembits93
c58618beb5 fix for 3.12.3 2026-01-12 15:34:36 -08:00
aseembits93
4ff64cc7e8 existing tests should be fixed now 2026-01-12 15:17:26 -08:00
aseembits93
a6032c2359 need to fix existing tests 2026-01-12 14:44:10 -08:00
aseembits93
bc6a767e41 unrelated ty check fixes 2026-01-12 12:50:56 -08:00
aseembits93
0534e3d3b5 consistent quotes 2026-01-12 12:32:35 -08:00
aseembits93
80fda18800 fix tests 2026-01-12 12:15:16 -08:00
Kevin Turcios
f2f4ea88fa
Merge branch 'main' into gpu-sync-instrumentation 2026-01-10 02:56:27 -05:00
Kevin Turcios
1e1820d27a
feat: add ty type checker to pre-k hooks (#2213)
Adds a local pre-commit hook for ty type checker since there's no
official pre-commit hook available yet (tracked in astral-sh/ty#269).
2026-01-09 22:36:53 -05:00
Aseem Saxena
c2782f3d2a handle submodule imports 2026-01-09 18:46:15 -08:00
Aseem Saxena
e5ad4efd0a double quote to single 2026-01-09 15:45:19 -08:00
Aseem Saxena
282ed2bea0 double quote to single 2026-01-09 15:42:27 -08:00
Aseem Saxena
e8cc522296 prek fix 2026-01-09 15:37:57 -08:00
Aseem Saxena
5379db9c4a
Merge branch 'main' into gpu-sync-instrumentation 2026-01-09 15:32:39 -08:00
Aseem Saxena
2348566f6b working the same as cli with more tests 2026-01-09 15:22:57 -08:00
Aseem Saxena
75bd21d0c2 working the same as cli 2026-01-09 14:48:01 -08:00
Sarthak Agarwal
7bef00afa3
delete the package causing issue (#2220)
Co-authored-by: Kevin Turcios <106575910+KRRT7@users.noreply.github.com>
2026-01-10 01:05:06 +05:30
Sarthak Agarwal
50fbd275d5
add backward compatibility (#2217)
# Pull Request Checklist

## Description
- [ ] **Description of PR**: Clear and concise description of what this
PR accomplishes
- [ ] **Breaking Changes**: Document any breaking changes (if
applicable)
- [ ] **Related Issues**: Link to any related issues or tickets

## Testing
- [ ] **Test cases Attached**: All relevant test cases have been
added/updated
- [ ] **Manual Testing**: Manual testing completed for the changes

## Monitoring & Debugging
- [ ] **Logging in place**: Appropriate logging has been added for
debugging user issues
- [ ] **Sentry will be able to catch errors**: Error handling ensures
Sentry can capture and report errors
- [ ] **Avoid Dev based/Prisma logging**: No development-only or
Prisma-specific logging in production code

## Configuration
- [ ] **Env variables newly added**: Any new environment variables are
documented in .env.example file or mentioned in description
---

## Additional Notes
<!-- Add any additional context, screenshots, or notes for reviewers
here -->
2026-01-10 00:31:11 +05:30
Kevin Turcios
93519bbcbe
Merge branch 'main' into update-prompts-mini-5 2026-01-09 01:56:37 -05:00
Kevin Turcios
8afe66c376
Merge branch 'main' into gpt-5-mini 2026-01-09 01:37:19 -05:00
Kevin Turcios
fe039a1502 fix prod crashes 2026-01-09 00:49:55 -05:00
Kevin Turcios
3c8408d73b
Merge branch 'gpt-5-mini' into update-prompts-mini-5 2026-01-09 00:35:39 -05:00
Kevin Turcios
2f7a400b73
Merge branch 'main' into gpt-5-mini 2026-01-09 00:32:11 -05:00
Sarthak Agarwal
8fbe21bffa
fix review prompt v1 (#2124)
Co-authored-by: Kevin Turcios <106575910+KRRT7@users.noreply.github.com>
2026-01-09 10:56:12 +05:30
Kevin Turcios
3449bba20d gpt-5-mini
gpt-5-mini is misinterpreting the template format in execute_user_prompt.md. When it sees:

  # function to test
  {function_code}

  It's outputting a nested code block with markdown path syntax (python:unstructured/staging/label_box.py) instead of the actual function code inline. It never reaches the # unit tests section where it should generate def test_* functions.
2026-01-09 00:10:12 -05:00
Kevin Turcios
c3385c79ba double max_tokens for now 2026-01-09 00:10:12 -05:00
Kevin Turcios
f393c8ce9f refresh here 2026-01-09 00:07:16 -05:00
Kevin Turcios
084af4f34d fix: restore inline prompts in ranker.py
The previous commit deleted the .md files but didn't revert the code
that loads from them, causing FileNotFoundError on server startup.
2026-01-09 00:07:15 -05:00
Kevin Turcios
d3f785156f revert changes to prompts 2026-01-09 00:07:15 -05:00
Kevin Turcios
fe0eb264a6 readjust prompt 2026-01-09 00:07:15 -05:00
Kevin Turcios
64970afae8 strenghten prompt 2026-01-09 00:07:14 -05:00
Kevin Turcios
a906d651cd use speedups 2026-01-09 00:07:14 -05:00
Kevin Turcios
00bb9756f4 re-adjust 2026-01-09 00:07:13 -05:00
Kevin Turcios
33939c62c3 move to their own md 2026-01-09 00:07:13 -05:00
Kevin Turcios
32daebf832 update prompts 2026-01-09 00:07:13 -05:00
Kevin Turcios
4f27a23623 remove temperature 2026-01-09 00:07:13 -05:00
Kevin Turcios
99bfbd980d use the centralized config 2026-01-09 00:07:12 -05:00
Kevin Turcios
1d1ec35cc3 typed dict runtime eval 2026-01-09 00:05:30 -05:00
Kevin Turcios
13af3b09a7 prek 2026-01-09 00:05:29 -05:00
Kevin Turcios
db926e785a cleanup calculate_llm_cost 2026-01-09 00:05:29 -05:00
Kevin Turcios
2d35bac4eb use GPT-5-Mini 2026-01-09 00:05:29 -05:00
Kevin Turcios
1de222fcf2 add OpenAI_GPT_5_Mini and update pricing 2026-01-09 00:05:28 -05:00
Kevin Turcios
cf6898eacf use haiku 2026-01-08 23:29:09 -05:00
Kevin Turcios
8c8d9f2c73
Merge branch 'main' into fix/add-missing-imports-postprocessing 2026-01-08 14:39:56 -05:00
Aseem Saxena
29ec4fa6a5
Merge branch 'main' into gpu-sync-instrumentation 2026-01-07 18:38:03 -08:00
HeshamHM28
50c2a61b85 Fix: Simplify _increment_llm_cost function by removing unnecessary line breaks 2026-01-08 03:24:16 +02:00
Aseem Saxena
0777cdfecc instructions to generate jit compiled code in another PR 2026-01-07 17:23:31 -08:00
HeshamHM28
5f21f8fae0 Fix: Refactor llm_cost increment logic into a separate function 2026-01-08 03:19:22 +02:00
Aseem Saxena
5e3bdccd8d testgen instructions in another commit 2026-01-07 16:58:52 -08:00
Aseem Saxena
85b308a363 cleaning up 2026-01-07 16:44:44 -08:00
HeshamHM28
96aa1305f0 Fix: Update defaults in get_or_create_optimization_event for repository_id and current_username 2026-01-08 02:41:07 +02:00
Aseem Saxena
8700af0140
Merge branch 'main' into testgen-jit 2026-01-07 16:20:10 -08:00
Aseem Saxena
bd08e1b792 simpler jax instrumentation 2026-01-07 16:16:26 -08:00
Aseem Saxena
9fb2f72a41 instrumentation for generated tests 2026-01-07 15:13:08 -08:00
Kevin Turcios
b834e7d332
Merge branch 'main' into fix/add-missing-imports-postprocessing 2026-01-07 17:30:33 -05:00
HeshamHM28
60a3c0694c
[FEAT] Privacy Mode (#2092)
Fixes CF-690

  Pull Request Checklist

  Description

  - Description of PR:

  Privacy Mode Feature for Pro Users

This PR introduces a Privacy Mode feature that allows paid users
(Pro/Enterprise) to control how their code is stored during the
optimization review process.

  Key Changes:

  1. Privacy Mode Toggle in Sidebar
    - Added a new toggle in the dashboard sidebar for Privacy Mode
- Only available for Pro/Enterprise users (disabled with upgrade prompt
for free users)
- Persists user preference in database with localStorage fallback for
fast UI
  2. Storage Strategy Based on Privacy Setting
- Privacy Mode ON: Code is stored exclusively in GitHub branches (never
cached in database)
- Privacy Mode OFF: Code is temporarily cached in database for faster
loading, cleaned up when PR is created
  3. Database Changes
    - Added privacy_mode boolean field to users table (default: false)
    - Added staging_storage_type field to track storage method
  4. API Updates
- cf-api now checks user's privacy mode when determining storage
strategy
- StagingStorageStrategyFactory considers privacy mode alongside tier
eligibility
- Added getUserPrivacyMode, setUserPrivacyMode, and isUserPaid functions


  User Experience

  - Free users see the toggle disabled with "Upgrade to Pro" messaging
  - Tooltip explains the trade-off: privacy vs. loading speed
- Toggle state syncs between localStorage (for instant UI) and database
(for persistence)

  Storage Flow

  User Request → Check Privacy Mode
├─ Privacy ON + Paid + Valid Repo → Git Branch Storage (GitHub only)
      └─ Privacy OFF or Free → Plain Text Storage (Database cache)

---------

Co-authored-by: ali <mohammed18200118@gmail.com>
Co-authored-by: Mohamed Ashraf <ashraf@codeflash.ai>
2026-01-07 12:23:45 -08:00
Kevin Turcios
795ccab1be fix: add prompt instructions to avoid Mock objects and incorrect constructors
- Do not use Mock objects for domain classes (pickling issues)
- Use correct constructor signatures from context
- Use concrete subclasses when base class is abstract
2026-01-07 14:44:11 -05:00
codeflash-ai[bot]
207f2847c6
Optimize UndefinedNameCollector.get_undefined_names
The optimization eliminates a significant performance bottleneck by moving the computation of `set(dir(builtins))` from runtime to module load time.

**What changed:**
- Introduced a module-level constant `_BUILTIN_NAMES = frozenset(dir(builtins))` 
- Replaced the repeated `builtin_names = set(dir(builtins))` call with direct reference to `_BUILTIN_NAMES`
- Used `frozenset` instead of `set` for the constant (immutable and slightly more efficient for lookups)

**Why this is faster:**
The original code calls `dir(builtins)` and creates a new set from it on every invocation of `get_undefined_names()`. The line profiler shows this single line consumed 73.3% of the function's execution time (1.68ms out of 2.29ms total). 

Python's `dir(builtins)` returns a list of ~150 builtin names, and converting this to a set requires:
1. Calling `dir()` to introspect the builtins module
2. Iterating through the list 
3. Allocating a new set and hashing each name

Since builtin names are constant for a Python process, this work is entirely redundant. By computing it once at module import time, each call to `get_undefined_names()` saves ~21.8μs per invocation (from the line profiler: 21799.7ns per hit).

**Performance impact:**
- **184% speedup** overall (1.09ms → 383μs)
- The optimized version shows 100% of time now spent on the actual set operations (name filtering), not builtin lookup
- Test results show 1350-2450% improvement for simple cases with few names, and 22-126% improvement even for large-scale tests with 1000+ names
- The optimization is particularly effective when `get_undefined_names()` is called repeatedly (as shown by the 77 hits in the profiler and multiple test invocations)

**Test case effectiveness:**
- Most effective for tests with repeated calls to `get_undefined_names()` on the same or different collector instances
- Benefits all test patterns since the builtin check happens on every call regardless of the number of user-defined names
- Even large-scale tests with 1000+ names show 22-35% improvements because the builtin lookup overhead is eliminated
2026-01-07 19:31:38 +00:00
Kevin Turcios
68a51bfcc3 fix: remove local class redefinitions and replace with imports
Instead of just adding missing imports, now also:
1. Detects classes/functions defined locally that exist in source module
2. Removes those local redefinitions
3. Adds imports for them

This ensures tests use the real classes from the source module.
2026-01-07 14:18:34 -05:00
Kevin Turcios
86f68b6ac7 fix: add postprocessing to fix missing imports in generated tests
When the LLM generates test code, it sometimes redefines classes locally
(like Element, ChunkingOptions) but forgets to import or define others
(like PreChunk), causing NameError at runtime.

This adds a postprocessing step that:
1. Detects undefined names in the generated test code
2. Checks if those names are defined in the source module
3. Adds the missing imports automatically

This is more reliable than relying on prompt instructions which the LLM
sometimes ignores.
2026-01-07 14:12:04 -05:00
Kevin Turcios
0d13635e4f not just for claude code 2026-01-06 22:21:32 -05:00
Kevin Turcios
883038bffe reference ty 2026-01-06 22:18:04 -05:00
Kevin Turcios
b10b0295fe Update AGENTS.md 2026-01-06 22:16:20 -05:00
Kevin Turcios
ff1f302e40 Create AGENTS.md 2026-01-06 22:13:44 -05:00
Kevin Turcios
c9df33526f
Merge branch 'main' into remove-print-messages 2026-01-06 18:49:26 -05:00
Kevin Turcios
9ea1afbdbf
Merge branch 'main' into remove-print-messages 2026-01-06 18:24:22 -05:00
Aseem Saxena
7feb6a19e8
Merge branch 'main' into ranker-discourage-type 2026-01-06 15:16:20 -08:00
Kevin Turcios
7f3d204ab6
Merge branch 'main' into ranker-discourage-type 2026-01-06 17:49:50 -05:00
Kevin Turcios
977f5a4348
Merge branch 'main' into unstructured-fixes 2026-01-06 17:47:55 -05:00
mashraf-222
669a1fe028
Merge branch 'main' into disable-telemetry-flag-dev 2026-01-06 21:08:36 +02:00
Kevin Turcios
5abb597996
Merge branch 'main' into unstructured-fixes 2026-01-06 13:40:51 -05:00
Kevin Turcios
7b2922807b
Merge branch 'main' into unstructured-fixes 2026-01-06 12:08:31 -05:00
mashraf-222
d0d2fb6fc6
Merge branch 'main' into disable-telemetry-flag-dev 2026-01-06 18:01:31 +02:00
ali
6060fa6701
ensures llm generates the testgen code as is a markdown code 2026-01-06 12:57:29 +02:00
ali
997f450794
default values for candidates number 2026-01-06 03:18:00 +02:00
ali
75ee24ebbf
control num of llm calls for optimize and optimize-lp 2026-01-06 02:10:51 +02:00
mashraf-222
206469f3f6
Merge branch 'main' into disable-telemetry-flag-dev 2026-01-05 19:25:10 +02:00
Aseem Saxena
c5f859fc27 device specific tests 2026-01-02 13:30:32 -08:00
Aseem Saxena
805219691d
typo 2026-01-02 12:21:39 -08:00
Aseem Saxena
0bb3c2522c
grammar 2026-01-02 10:23:09 -08:00
Aseem Saxena
3478043dad
typo 2026-01-02 10:20:40 -08:00
Aseem Saxena
8dadc322f8
Update ranking optimization guidelines in ranker.py
avoid isinstance to type conversions
2026-01-02 10:19:27 -08:00
Sarthak Agarwal
23784bdf05 Discourage the isinstance to type conversion 2026-01-02 23:01:22 +05:30
mashraf-222
59354251aa
Merge branch 'main' into disable-telemetry-flag-dev 2026-01-02 15:14:23 +02:00
Kevin Turcios
798523bc29 Add logging.exception() to API error handlers for better debugging
Replace logger.error() and debug_log_sensitive_data() with
logging.exception() in exception handlers to ensure full stack traces
are logged to console/logs, not just to Sentry.

Affected endpoints:
- /optimize: Added exception logging
- /rank: Added logging import and exception logging
- /explanations: Added logging import and exception logging
- /workflow-gen: Changed logger.error to logger.exception
2026-01-01 17:47:12 -05:00
Kevin Turcios
2095c31195 Remove print_messages debug output from testgen
Remove the print_messages function and its usage since observability
tooling is now in place. This eliminates verbose system prompt output
during test generation in non-production environments.
2026-01-01 17:42:52 -05:00
Kevin Turcios
0c81863fff
Merge branch 'main' into unstructured-fixes 2026-01-01 13:31:05 -05:00
Kevin Turcios
0b308e4cd8
Merge branch 'main' into ty-follow-up 2026-01-01 13:29:07 -05:00
Kevin Turcios
b357f9541c fix: remove unreachable code in _handle_assign method 2026-01-01 13:07:27 -05:00
ali
2c5f031145
correct type for request in adaptive api 2026-01-01 20:01:28 +02:00
ali
13cedf8f5b
Merge branch 'main' of github.com:codeflash-ai/codeflash-internal into exp/adaptive-optimization 2026-01-01 20:00:00 +02:00
ali
8cd1ecb670
ruff format 2026-01-01 17:08:52 +02:00
Kevin Turcios
441a15f137 fix: instruct LLM to import classes instead of mocking them
Generated tests were failing isinstance() checks because LLM created
mock class definitions instead of importing real ones.

Added prompt instruction to import classes from their actual modules
when file paths are shown in the context.
2026-01-01 02:03:12 -05:00