Skip to content

fix(js): shadow names captured from enclosing scopes in tracked nested closures (#5217) - #5221

Closed
Sidreyas wants to merge 1 commit into
Graphify-Labs:v8from
Sidreyas:fix/js-enclosing-scope-shadow-5217
Closed

Sidreyas wants to merge 1 commit into
Graphify-Labs:v8from
Sidreyas:fix/js-enclosing-scope-shadow-5217

Conversation

@Sidreyas

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #5217. A name that a nested JS closure captures from an enclosing function (or enclosing block) was read as a free name and became an indirect_call to any same-named function in the corpus.

Cause. The #3124 branch in _js_extra_walk tracks the body of a closure passed in a call statement, under the file node. It fires for nested call statements as well as module-level ones ($ngConfirm({... action: function () {...}}) and setTimeout(function () {...}) in the issue). Once a body is tracked, walk_calls starts at that body instead of reaching it from the enclosing function. So the #2241 / #2822 folds that carry enclosing params and block bindings down never run for it. #4448 registered the closure's own params and vars for that walk (closure_locals_by_body). Bindings from the enclosing scopes were still missing. Case A in the issue works because save(value) has no closure argument, so its body is never tracked on its own.

Fix. A new helper, _js_enclosing_bound_names(closure, source), walks the ancestors from the closure up to program and collects what walk_calls would have folded on the way down:

  • each enclosing function's params and vars (_js_local_bound_names)
  • each enclosing statement_block / for_statement's let/const (_js_direct_lexical_names)
  • for…in/of loop bindings
  • catch (e) bindings

The #3124 branch unions this set into closure_locals_by_body with the closure's own bindings. Nothing else changes. Edge attribution stays the same: calls are still credited to the file node, as before. For a module-level call statement the ancestor chain has no scopes, so the set is empty and behaviour is unchanged.

Issue reproduction (app/controller.js + web/helpers.ts from the issue), edges into helpers.ts:

v8 this PR
contains (3) ✓ ✓
indirect_call controller.js → item() L9 present (false) gone
indirect_call controller.js → entry() L15 present (false) gone

Type of change

  • Bug fix

Verification & Invariants

Invariant: a name bound in any scope that lexically encloses a reference is never resolved as a corpus-wide by-name callback reference, whichever body the reference ends up walked from. Names that are not bound in an enclosing scope still resolve as before. Two guard tests check this: an unbound name in a nested callback, and a param of a sibling function that does not enclose the callback. Both resolve to value() before and after this change.

  • Read the CONTRIBUTING.md guide.
  • Reproduced the issue and identified the invariant.
  • Made the smallest fix necessary.
  • Added a regression test.
  • Kept the PR description synchronized with the final implementation.
  • Documented any limitations / unsupported cases explicitly.

New tests in tests/test_indirect_call_file_scope_callback_shadow.py (next to the #4448 ones):

test v8 this PR
test_param_captured_from_an_enclosing_function_is_not_a_reference (issue case B) FAIL pass
test_arrow_param_captured_two_levels_down_is_not_a_reference (issue case C) FAIL pass
test_enclosing_const_is_not_a_reference FAIL pass
test_enclosing_loop_binding_is_not_a_reference FAIL pass
test_enclosing_catch_binding_is_not_a_reference FAIL pass
test_unbound_name_in_a_nested_callback_still_resolves (guard) pass pass
test_a_binding_in_a_sibling_scope_does_not_shadow_a_nested_callback (guard) pass pass

Limitations (unchanged, out of scope):

  • The branch still credits the nested closure's calls to the file node, not to the enclosing named member ($scope.remove). This PR only removes the false references. It does not change attribution.
  • Enclosing function declarations (function item() {} nested in an outer function) are not added to the shadow set. They are real nodes, and the lexical-scope lookup in _emit_indirect_by_name already resolves them first.

How was this tested?

Windows 11, CPython 3.11, uv sync --all-extras.

python -m pytest tests/test_indirect_call_file_scope_callback_shadow.py -q    # 15 passed
# same file with the engine.py change stashed                                 # 5 failed, 10 passed
python -m pytest tests -q                                                       # 11092 passed, 262 skipped, 17 failed (all 17 also fail on unmodified v8, see below)
ruff check .                                                                    # All checks passed!
pyright graphify/extractors/engine.py                                          # 20 errors, identical count on v8 (pre-existing)

The 17 full-suite failures fail identically with the engine.py change stashed. They are environment-specific to this machine and unrelated to extraction:

  • Windows paths reaching bash (test_code_only_update_all_hosts[devin|aider], test_hooks::test_worktree_guard_survives_exported_cdpath)
  • getcwd / relative-root path tests (test_resolve_memoization, test_source_key_memoization, test_language_override_lifecycle)
  • test_skillgen audits, which compare against v8 history that is missing from my shallow clone

Graphify-specific checklist

  • I updated generated skill artifacts (uv run python -m tools.skillgen --bless) when changing their source fragments. (n/a, no fragments touched)
  • I confirmed that AST/structural extraction remains deterministic (no ambient state dependencies like ENV variables).
  • I reviewed changes for security implications (no unsafe interpolation into shell/Python).
  • I confirmed no API keys or local-only graph data are included.
  • (If applicable) I disclosed AI authorship in my commit messages.

🤖 Generated with Claude Code

…d closures (Graphify-Labs#5217)

The Graphify-Labs#3124 branch tracks the body of a closure passed in a call statement
under the file node, and it fires for call statements nested inside other
functions too. walk_calls then starts at that body, so the Graphify-Labs#2241/Graphify-Labs#2822
folds that carry enclosing params and block bindings down never reach it.
Graphify-Labs#4448 registered only the closure's own params/vars, so a name captured
from an enclosing function (`function (item) { confirm({ action:
function () { del(item) } }) }`) read as free and became an indirect_call
to any same-named function in the corpus.

Add _js_enclosing_bound_names, which collects enclosing function params and
vars, block let/const, for-in/of and catch bindings up to the program, and
union it into closure_locals_by_body for that branch. Attribution is
unchanged; module-level call statements have no enclosing scopes, so their
behaviour is unchanged too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Sidreyas
Sidreyas requested a review from safishamsi as a code owner November 15, 2026 11:23
Copilot AI balanced review requested due to automatic review settings November 15, 2026 11:23
@github-actions

Copy link
Copy Markdown

Thanks for the pull request, @Sidreyas. A maintainer will review it soon.

Want to talk it through while it is in review? Come join us on our Discord server. For longer-form discussion there is also GitHub Discussions.

A couple of things that speed up review: make sure the test suite passes on Python 3.10 and 3.13, and that the change keeps extraction deterministic.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@graphify-labs graphify-labs Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Graphify reviewed this change.

Worth a look — the grounded gate found no coupling regressions or blocking issues, but 1 advisory finding(s) below merit a look before merge.

Not checked: tests were not run; formal verification proved 0 of 1 changed function(s) (1 not verified).

Formal verification. PR-changed functions: 0/1 verified (0 proven, 0 may-equivalent, 0 distinguished) · 1 not verified (1 vacuous).

Not verified on this run: \_js\_extra\_walk (vacuous: never exercised).


Graphify review — findings

Fixes JS indirect-call resolution for closures passed in call statements nested inside another function: the closure body is now tracked on its own with the bindings of every enclosing function and block (params, vars, let/const, loop and catch bindings). A captured name like value in function (item) { confirm({ action: function () { del(item) } }) } no longer reads as a free reference and resolves to a same-named callable elsewhere in the corpus (#5217). _js_enclosing_bound_names walks the ancestor chain to program level and mirrors what walk_calls folds into extra_locals on the way down, and the for_in_statement case treats the loop's left side as bound, including undeclared assignment targets.

Worth a look

  • Undeclared for-in/of assignment targets are treated as enclosing locals — graphify/extractors/engine.py:3297 · Escalate · medium
    • NOT verified (no proof, no reproducing execution) — needs human review
Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 4212 functions depend on the 469 functions this change touches.

Health — this change adds coupling hotspots:

  • worse: _js_extra_walk() — 2 callers, 16 callees

Verification — 4212 functions in the blast radius were not formally verified this run (proofs are advisory here).

Gate & verification

graphify gate

PASS — objectively clean (no health regressions, tests not run — proofs not run this pass (advisory)). Grounded, not self-assessed.

Advisory (not blocking):

  • verification_scope: 2455 function(s) in the blast radius were not formally verified this run

Test selection

Test selection

52 of 575 test file(s) selected (9%) via static blast radius.

  • tests/test_4992_ts_import_types.py — impact
  • tests/test_astro_extraction.py — impact
  • tests/test_build.py — impact
  • tests/test_cjs_module_extension.py — impact
  • tests/test_cpp_alias_member_resolution.py — impact
  • tests/test_cpp_conditional_initializers.py — impact
  • tests/test_cpp_inherited_member_resolution.py — impact
  • tests/test_cpp_nested_and_cli.py — impact
  • tests/test_csharp_extension_calls.py — impact
  • tests/test_csharp_extension_metadata.py — impact
  • tests/test_csharp_extension_resolution.py — impact
  • tests/test_csharp_partial_classes.py — impact
  • tests/test_dart.py — impact
  • tests/test_dart_typedef.py — impact
  • tests/test_dotnet.py — impact
  • tests/test_ejs_extractor.py — impact
  • tests/test_ext_tagged_id_clash.py — impact
  • tests/test_extract.py — impact
  • tests/test_extract_php_closures.py — impact
  • tests/test_import_extension_resolution.py — impact
  • tests/test_indirect_call_block_scoped_shadow.py — impact
  • tests/test_indirect_call_file_scope_callback_shadow.py — impact, changed-test
  • tests/test_indirect_dispatch.py — impact
  • tests/test_indirect_dispatch_assign_return.py — impact
  • tests/test_indirect_dispatch_getattr.py — impact
  • tests/test_java_member_calls.py — impact
  • tests/test_js_exported_scalar_bindings.py — impact
  • tests/test_js_member_collisions.py — impact
  • tests/test_languages.py — impact
  • tests/test_multilang.py — impact
  • tests/test_php_unparsed_casts.py — impact
  • tests/test_python_inherited_method_calls.py — impact
  • tests/test_python_injected_factory_calls.py — impact
  • tests/test_python_property_reads.py — impact
  • tests/test_python_pytest_fixture_receivers.py — impact
  • tests/test_python_subscript_type_refs.py — impact
  • tests/test_python_underscore_resolution.py — impact
  • tests/test_rationale.py — impact
  • tests/test_ruby_resolution.py — impact
  • tests/test_scala_context_bounds.py — impact
  • tests/test_scala_extension_methods.py — impact
  • tests/test_scala_given_instances.py — impact
  • tests/test_scala_self_type.py — impact
  • tests/test_scala_top_level_binding.py — impact
  • tests/test_scala_type_definition.py — impact
  • tests/test_svelte_extraction.py — impact
  • tests/test_swift_computed_properties.py — impact
  • tests/test_swift_protocol_requirements.py — impact
  • tests/test_trailing_newline_not_a_syntax_error.py — impact
  • tests/test_ts_new_expression_calls.py — impact
  • … and 2 more

Selection is safe under the controlled-regression assumption; always-run tests + a periodic full run are the backstops. Advisory — it never changes the check verdict.

Formal verification

Could not verify: Could not verify \_js\_extra\_walk.

The verifier did not have enough to check \_js\_extra\_walk, so it is saying so rather than guessing. No false assurance is the whole point.

Guarantee: No guarantee either way, this is an honest abstention, not a pass.

Note: Reason: no capturable inputs from the test suite; property tier: not verifiable: all 264 sampled inputs raised on both versions — the function never executed, so 'no divergence' would be vacuous (mostly AttributeError — names the real obstacle, not a sampling gap)

· 1 more finding(s) on lines outside this diff (see the check run).

@safishamsi

Copy link
Copy Markdown
Member

Landed on v8 as ccb43e9 via authorship-preserving cherry-pick, released in graphify 0.9.110. Thanks @Sidreyas.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants