Skip to content

feat(js): add JavaScript function tracer with Babel instrumentation#1377

Open
KRRT7 wants to merge 10 commits intomainfrom
js-trace
Open

feat(js): add JavaScript function tracer with Babel instrumentation#1377
KRRT7 wants to merge 10 commits intomainfrom
js-trace

Conversation

@KRRT7
Copy link
Collaborator

@KRRT7 KRRT7 commented Feb 4, 2026

Summary

  • Add Babel-based function tracing for JavaScript/TypeScript projects
  • Captures function calls with nanosecond timing to SQLite database
  • Generates Jest/Vitest replay tests from traces
  • CLI integration with --language flag and auto-detection

Changes

  • JS Runtime: tracer.js, babel-tracer-plugin.js, trace-runner.js, replay.js
  • Python Integration: replay_test.py, tracer_runner.py, tracer.py
  • CLI: Language routing in main tracer.py
  • Refactor: Consolidated Jest/Vitest parsing into parse_test_output.py

Test plan

  • 21 unit and E2E tests in test_javascript_tracer.py
  • E2E test installs npm deps and runs full tracing pipeline
  • Added to js-tests.yml GitHub Actions workflow

KRRT7 added 4 commits February 4, 2026 05:04
Add Babel-based function tracing for JavaScript/TypeScript:
- tracer.js: Core tracer with SQLite storage and nanosecond timing
- babel-tracer-plugin.js: AST transformation to wrap functions
- trace-runner.js: Entry point for running traced code
- replay.js: Utilities for replay test generation
- Update package.json with exports and dependencies
Add Python modules for JavaScript tracing orchestration:
- replay_test.py: Generate Jest/Vitest replay tests from traces
- tracer_runner.py: Run trace-runner.js and detect test frameworks
- tracer.py: Refactored to use Babel-only approach, removed legacy
  source transformation code
Move Jest/Vitest JUnit XML parsing from separate module into the main
parse_test_output.py file to reduce module fragmentation. Remove
duplicate tests that were testing the now-deleted module.
Add language detection and routing to JavaScript tracer in the main
tracer CLI. Supports --language flag and auto-detection from file
extensions in project.
@codeflash-ai
Copy link
Contributor

codeflash-ai bot commented Feb 4, 2026

⚡️ Codeflash found optimizations for this PR

📄 18% (0.18x) speedup for JavaScriptTracer._get_function_alias in codeflash/languages/javascript/tracer.py

⏱️ Runtime : 294 microseconds 250 microseconds (best of 250 runs)

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

If you approve, it will be merged into this PR (branch js-trace).

Static Badge

logger.info("Running JavaScript tracer: %s", " ".join(cmd))

try:
process = subprocess.run(cmd, cwd=project_root, env=env, capture_output=False, check=False)
Copy link

@claude claude bot Feb 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Still not fixed: The subprocess still runs with capture_output=False, which makes debugging failures very difficult. When the tracer fails (non-zero exit code), there's no stdout/stderr available to diagnose the issue.

Suggested fix:

process = subprocess.run(cmd, cwd=project_root, env=env, capture_output=True, text=True, check=False)

if process.returncode != 0:
    result["error"] = f"Tracing failed with exit code {process.returncode}\nStdout: {process.stdout}\nStderr: {process.stderr}"

conn.close()
return functions

except Exception:
Copy link

@claude claude bot Feb 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Still not fixed: Silent exception handling at line 296. Catching all exceptions without logging makes debugging failures impossible. If database parsing fails, users won't know why.

Suggested fix:

except Exception as e:
    logger.exception(f"Failed to get traced functions from {trace_file}: {e}")
    return []

sys.exit(1)

console.print(f"[green]Trace saved to: {result['trace_file']}[/green]")

Copy link

@claude claude bot Feb 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Still present: The JavaScript tracer automatically triggers optimization without explicit user consent when not in trace-only mode (line 353). While this matches the intended workflow, consider adding a user confirmation or making this behavior more explicit in the CLI help.

Current behavior:

  • codeflash trace → automatically runs optimization after tracing
  • codeflash trace --trace-only → only traces, no optimization

This is acceptable if documented clearly.

from codeflash.languages.javascript.tracer import JavaScriptTracer


def node_available() -> bool:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Consider adding helper: The E2E tests check for Node.js availability, but there's no helper to install npm dependencies. Consider adding a fixture that checks if node_modules/codeflash exists and provides setup instructions if missing.

Add comprehensive test coverage for JavaScript tracing:
- Unit tests for trace parsing (function_calls and legacy schemas)
- Unit tests for replay test generation (Jest and Vitest)
- E2E test for full tracing pipeline with npm dependencies
- Framework detection tests (Jest, Vitest, package.json)
@claude
Copy link

claude bot commented Feb 4, 2026

PR Review Summary

✅ Prek Checks

All prek checks (ruff check, ruff format) passed successfully. No linting or formatting issues detected.

⚠️ Mypy Type Checks

Mypy checks revealed 52 type errors across 3 files. However, most errors appear to be pre-existing issues in the JavaScript support module, not introduced by this PR. Key issues include:

  • Missing type parameters for generic types (dict, list)
  • Union type handling issues (None checks)
  • Missing type annotations in parse_test_output.py

These type errors should be addressed in a follow-up PR focused on type safety improvements.

📊 Test Coverage Analysis

PR Branch Coverage (Changed Files):

File Statements Missed Coverage
codeflash/languages/javascript/support.py 916 240 74%
codeflash/languages/javascript/tracer.py 119 17 86%
codeflash/languages/javascript/tracer_runner.py 161 106 34% ⚠️
TOTAL 1,196 363 70%

Notes:

  • tracer.py has excellent coverage (86%)
  • ⚠️ tracer_runner.py has low coverage (34%) - needs more test cases for error paths and edge cases
  • support.py has acceptable coverage (74%) but could be improved
  • Note: codeflash/languages/javascript/parse.py was deleted (consolidated into parse_test_output.py)

Test Results:

  • 2,210 tests passed
  • 8 tests failed (unrelated to this PR - in test_tracer.py, appear to be pre-existing issues with Python tracer)
  • 42 tests skipped
  • Added 21 new JavaScript tracer tests in test_javascript_tracer.py

🔍 Code Review - Existing Issues Still Present

After reviewing the latest commits, the following issues from the previous review are still not addressed:

  1. tracer_runner.py:183 - ⚠️ capture_output=False makes debugging failures difficult

    • When tracer fails, no stdout/stderr is captured for diagnostics
    • Recommended: Use capture_output=True, text=True and log output on error
  2. replay_test.py:296 - ⚠️ Silent exception handling without logging

    • Catching all exceptions without logging makes debugging impossible
    • Recommended: Add logger.exception() to log the actual error
  3. tracer.py:353 - ℹ️ Auto-triggers optimization without explicit consent

    • This is acceptable behavior but should be clearly documented in CLI help
    • Current behavior: codeflash trace → auto-optimization; codeflash trace --trace-only → trace only
  4. test_javascript_tracer.py:28 - 💡 Consider adding helper for npm dependency setup

    • E2E tests check for Node.js but don't verify node_modules/codeflash exists
    • Recommended: Add fixture with setup instructions if dependencies missing

📝 Recommendations

  1. Priority: Fix tracer_runner.py capture_output issue - This affects debuggability significantly
  2. Priority: Add logging to exception handlers - Essential for production debugging
  3. Improve test coverage for tracer_runner.py - Currently at 34%, should target ≥70%
  4. Document auto-optimization behavior - Make it clear in --help output what happens after tracing
  5. Address mypy type errors - Schedule a follow-up PR to fix type annotations

✅ Overall Assessment

This is a solid PR that adds JavaScript tracing functionality with good test coverage overall. The main concerns are:

  • Low coverage in tracer_runner.py (34%)
  • Existing review comments not addressed (capture_output, exception logging)
  • Type annotation issues (can be addressed in follow-up)

The PR is functionally ready but would benefit from addressing the debuggability concerns before merging.


Last updated: 2026-02-04

@codeflash-ai
Copy link
Contributor

codeflash-ai bot commented Feb 4, 2026

⚡️ Codeflash found optimizations for this PR

📄 67% (0.67x) speedup for check_javascript_tracer_available in codeflash/languages/javascript/tracer_runner.py

⏱️ Runtime : 12.9 milliseconds 7.75 milliseconds (best of 124 runs)

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

If you approve, it will be merged into this PR (branch js-trace).

Static Badge

KRRT7 and others added 5 commits February 4, 2026 06:15
- Add explicit type annotations for json.load return values

- Annotate result dict with dict[str, Any]

- Add type hints for config parameters

- Fix pytest_split unpacking to satisfy type checker

- Add null check for outfile before passing to run_javascript_tracer_main

Co-Authored-By: Claude Sonnet 4.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

workflow-modified This PR modifies GitHub Actions workflows

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant