Skip to content

Fix: Skip nested functions in JavaScript/TypeScript discovery#1968

Merged
Saga4 merged 6 commits intomainfrom
fix/skip-nested-functions-js
Apr 2, 2026
Merged

Fix: Skip nested functions in JavaScript/TypeScript discovery#1968
Saga4 merged 6 commits intomainfrom
fix/skip-nested-functions-js

Conversation

@mohammedahmed18
Copy link
Copy Markdown
Contributor

Bug Description

Nested functions (functions defined inside other functions) were being discovered and attempted to be optimized, causing 140+ optimization runs to fail with errors like:

Invalid source code: Undefined variable(s): base, streamFn, record, writer

Root Cause

The discover_functions method in codeflash/languages/javascript/support.py was allowing nested functions to be marked for optimization. When the extraction logic ran, it only captured the nested function body without the parent scope variables, causing semantic validation failures.

Example:

export function wrapStreamFn(streamFn) {
    const base = { id: 1 };
    
    const wrapped = (model, context, options) => {  // ← This was being optimized
        return streamFn(model, context, options);   // ← But streamFn is undefined!
    };
    
    return wrapped;
}

The wrapped function depends on streamFn and base from the parent scope, but the extractor only captured the nested function itself.

Fix

Added explicit check to skip functions with parent_function set during discovery:

# Skip nested functions (functions defined inside other functions)
if func.parent_function:
    logger.debug(f"Skipping nested function: {func.name} (parent: {func.parent_function})")
    continue

Nested functions are now filtered out at discovery time before extraction is attempted.

Testing

Added tests/languages/javascript/test_discover_functions.py with 4 test cases:

  1. test_discovers_top_level_function - Ensures normal functions still work
  2. test_skips_nested_functions_in_closures - Main bug fix verification
  3. test_discovers_class_methods - Ensures class methods still work (different from nested)
  4. test_skips_nested_functions_with_multiple_levels - Handles deeply nested functions

All tests pass. Linter (prek) passes.

Impact

  • Resolves 140+ trace failures with undefined variable errors
  • Functions like wrapStreamFn.wrapped, createAnthropicPayloadLogger.wrapStreamFn.wrapped will no longer be attempted
  • Only top-level functions and class methods will be optimized

Affected Traces

This fix resolves errors in 140+ trace IDs including:

  • 02a59310-bb18-47e4-87cb-1e5144ce2d8c (wrapStreamFn.wrapped)
  • And many others with nested function patterns

Note

This is paired with PR #2519 (aiservice import validation fix). Together they resolve the most common optimization failures in the openclaw run.

Codeflash Bot and others added 2 commits April 2, 2026 08:33
…onments

- Add -w flag for pnpm workspace roots to avoid ERR_PNPM_ADDING_TO_ROOT
- Use local package path (/opt/codeflash/packages/codeflash) in dev mode
- Improve error logging to show actual stderr at ERROR level instead of WARNING
- Add unit tests for workspace detection and local package usage

Fixes 9/13 optimization failures caused by 'Cannot find package codeflash'
Trace IDs affected: 08d594a2, 1722cff7, 23480bf7, 3074f19b, 6043236e,
                     b883f1bd, d01b03ce, e56507a4, f8f54e06
Bug: Nested functions were being discovered and attempted to be optimized,
but the extraction logic only captured the nested function body without
parent scope variables, causing validation errors like:
  'Undefined variable(s): base, streamFn, record, writer'

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

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

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

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

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

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@claude
Copy link
Copy Markdown
Contributor

claude bot commented Apr 2, 2026

Claude finished @mohammedahmed18's task in 1m 57s —— View job


PR Review Summary

Prek Checks

All ruff checks pass. No auto-fixable issues. Mypy has 31 pre-existing errors in support.py — none introduced by this PR.

Code Review

Dead code at support.py:179–180

# Line 166–168: all functions with parent_function are already skipped
if func.parent_function:
    logger.debug(...)
    continue

# ...lines 175–178...

# This block can never execute — func.parent_function is always falsy here
if func.parent_function:                                           # line 179
    parents.append(FunctionParent(name=func.parent_function, type="FunctionDef"))  # line 180

The continue on line 168 means any function with parent_function set never reaches line 179. This dead code was load-bearing before — it used to add nested function context to the parents list for functions that passed the old export filter. Now it's unreachable and should be removed. Fix this →

The fix logic itself (adding if func.parent_function: continue and removing the not func.parent_function exception in the export filter) is correct.

Duplicate Detection

No duplicates detected. Python's discover_functions doesn't track parent_function at all, so there's no parallel logic to align.

Test Coverage

All 4 new tests pass. Coverage is appropriate for a SMALL PR — the key happy path, the bug scenario, class methods, and multi-level nesting are all covered.


Last updated: 2026-04-02

@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


Codeflash Bot seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@Saga4 Saga4 merged commit b8c31b3 into main Apr 2, 2026
19 of 27 checks passed
@Saga4 Saga4 deleted the fix/skip-nested-functions-js branch April 2, 2026 16:21
mohammedahmed18 added a commit that referenced this pull request Apr 2, 2026
…havior

PR #1968 changed JS to skip nested functions like Python, but the parity
test still expected JS to discover both outer and inner.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants