Repository navigation
Description
Summary
When a nested Python function passes a variable captured from its enclosing function (a free/closure variable) as a call argument, the callback-by-name resolver treats the identifier as a function reference. It then binds it corpus-wide to any same-named function, including a function nested inside another function in a different module. That nested function cannot be reached from the caller at all.
A plain parameter or local variable of the caller is correctly shadowed: no edge is emitted. Only the enclosing-scope (closure) case leaks.
Minimal reproduction
pkg/cli.py
def placeholder(graph):
def source(record):
return record.get("source_file", "")
return [n for n in graph if source(n)]pkg/walker.py
def read_text(node, source):
return source[node.start:node.end]
def extract_file(path):
source = path.read_bytes()
def walk(node):
return read_text(node, source) # `source` is bytes captured from extract_file
return walkfrom pathlib import Path
from graphify.extract import extract
root = Path("repro")
r = extract(sorted(root.rglob("*.py")), cache_root=root, root=root)Actual (calls / indirect_call edges):
placeholder() -- calls EXTRACTED -> pkg_cli_placeholder_source pkg/cli.py L4
extract_file() -- indirect_call INFERRED 0.85 -> pkg_walker_extract_file_walk pkg/walker.py L11
walk() -- calls EXTRACTED -> pkg_walker_read_text pkg/walker.py L9
walk() -- indirect_call INFERRED 0.85 -> pkg_cli_placeholder_source pkg/walker.py L9 <-- wrong
Expected: no edge from walk() to pkg_cli_placeholder_source. Here source is a bytes value bound in extract_file's scope. It is not a reference to cli.placeholder.<locals>.source, which is not visible from walker.py in the first place.
If source is instead a parameter of walk itself (def walk(node, source)), no false edge is produced. So parameters of the caller are shadowed, but names bound in enclosing function scopes are not.
Impact on a real corpus
Running /graphify on graphify's own graphify/ package (v0.9.124, commit d249de3) shows the problem at scale. The nested helper source() at cli.py:657 (inside _placeholder_stale_sources) becomes the #1 god node with 125 edges, 124 of them fabricated indirect_call INFERRED 0.85. They come from the language extractors, where source is the file-bytes variable captured by nested walk() / walk_calls() closures:
extractors/engine.py(51 edges),go.py(8),julia.py(7),r.py,rust.py,solidity.py(6 each),fortran.py,powershell.py(5 each), …- e.g.
extractors/engine.py:9574:config.import_handler(node, source, file_nid, ...), orextractors/bash.py:844:_read_text(c, source).
The side effects:
source()tops "God Nodes" andwalk()(v3: Codex and OpenClaw support #2) is inflated.- One Leiden community ("scoped walk / export macros") is held together mostly by the phantom hub.
- The report then suggests verifying "the 124 inferred relationships involving
source()".
Likely cause
The Python callback-by-name path (_emit_indirect_by_name → the cross-file resolver in extract.py, which emits "relation": "indirect_call" around L14700) resolves the bare identifier by label across the corpus. It only checks that the target is a callable, non-class definition with a source file. It does not check that:
- the identifier is a name bound in any enclosing function scope of the caller (assignment, parameter,
fortarget, etc.), which would shadow any module-level or cross-file definition (LEGB); or - the target is reachable by name from the caller's module. A function nested inside another function (
a.<locals>.b) can never be referenced by bare name from a different module.
Either check alone would remove all 124 false edges above.
Related
- fix(python): lexical-scope-aware indirect_call resolution #3607 (open PR) makes
indirect_callresolution lexical-scope-aware for names that resolve to a nested def in an enclosing scope. But it states that references with no enclosing-scope definition "still fall through exactly as before". In this case the enclosing-scope binding is a variable, not a def, so it appears to still reach the corpus-wide resolver. - JS: parameters of a file-scope anonymous callback are not shadowed, so a callback argument fabricates an indirect_call to any same-named function in the corpus #4448 is the same class of bug for JS (callback parameters not shadowed).
Environment
- graphifyy installed via
uv tool install graphifyy(Python 3.12, macOS) - Repo at
d249de3(2026-11-30),pyproject.tomlversion 0.9.124
Activity
github-actions commentedon Nov 30, 2026
Thanks for opening this issue, @md01750272449-oss. A maintainer will take a look soon.
If you would like to discuss it in real time, come say hi on our Discord server. For longer-form questions and ideas there is also GitHub Discussions.
To help us triage, please make sure the report includes what you expected, what actually happened, and the steps (and a small sample) to reproduce it.