Skip to content

calls_out: calls inside a nested named function belong to that function only (C8, #3642) - #3671

Merged
squid-protocol merged 6 commits into
mainfrom
claude/sleepy-gates-xyog68
Sep 25, 2026
Merged

squid-protocol merged 6 commits into
mainfrom
claude/sleepy-gates-xyog68

Conversation

@squid-protocol

@squid-protocol squid-protocol commented Sep 25, 2026 •

Copy link
Copy Markdown
Owner

Summary

Fixes #3642 (part of #3641). Contract C8 (docs/calls_out_rule_contract.md) says a call inside a nested named function belongs to that inner function only. A call inside an anonymous one (lambda, arrow callback, closure) stays with the enclosing named function.

The bug: the engine scanned each unit's whole text block for calls. So every call inside a nested unit the slicer also emits was listed on the enclosing unit too. #3360's C5 fix removed only the nested unit's header (def inner(), not its body.

The fix (gitgalaxy/core/detector.py):

  • _calculate_block_metrics queues each unit it scanned with the C-style/ruby calls_out family (the qualified patterns).
  • After slicing, _function_slice calls _drop_nested_unit_calls for every queued unit that has named units strictly inside its [start_idx, end_idx) span. It rescans the unit's original block with those spans blanked out (newlines kept), using the same literal shield and calls_out pattern.
    • A callee stays only if the rescan still finds it.
    • calls_out_qualifiers is rebuilt from the rescan.
  • It rescans rather than mapping offsets because _apply_literal_shield changes lengths (a comment becomes ''). Offsets taken on the shielded text don't line up with source spans, and the first cut of this PR dropped the wrong calls because of that.
  • Synthetic buckets (Anonymous_Block, __global_context__, …) are not named units, so their bodies are never blanked out of the unit around them.
  • The C5 pass still runs afterwards, unchanged.
  • If a unit's block can't be found verbatim at its span (a slicer that rebuilds its block), the unit is left as it was.
  • Families without positional matches (CALL verb, command position, lisp, language-specific rules) are untouched. None of the issue's languages use them.

Type of change

  • Bug fix
  • Parsing or engine logic (gitgalaxy/core/detector.py)

Measured: call_graph_accuracy.py on language-crucible v1.5.0

language precision before → after recall before → after FP FN
python 96.3% → 99.6% 99.2% → 99.2% 424 → 42 92 → 92
javascript 87.6% → 97.3% 97.6% → 97.5% 327 → 63 56 → 59
typescript 94.0% → 99.6% 95.2% → 95.1% 384 → 23 307 → 310
csharp 93.9% → 99.2% 97.6% → 97.6% 166 → 22 62 → 62
java 99.1% → 100.0% 98.2% → 98.2% 17 → 0 35 → 35
rust 87.4% → 89.8% 90.3% → 90.3% 935 → 734 696 → 697
go, c, cpp, php, ruby unchanged unchanged

The fp:inner-named-* shapes from the issue fall like this:

shape before → after
python function_definition 392 → 10
rust function_item 166 → 9
csharp local_function_statement 143 → 0
java method_declaration 17 → 0
TS method_definition 184 → 3
TS arrow_function 142 → 14
JS function_declaration 131 → 11
JS function_expression 127 → 4

The remainder are cases where the engine and tree-sitter nest units differently.

Recall cost: 7 true positives (JS 3, TS 3, Rust 1). All are fn:engine-attributes-to-another-function: the engine emits a nested unit that tree-sitter treats as anonymous, so the call now sits on the engine's inner unit, not the function tree-sitter expects. These are leads for the ledger, not verified verdicts.

tests/call_graph_accuracy_baseline.json is regenerated. docs/self_scan/graph_comparison_ledger.json has the new shapes merged in with --ledger. Some shapes now read still_reproduces: false, including validated ones. No verdict was hand-edited.

Golden masters

Both fixtures were re-blessed with crucible_check.py --update --yes (--mode zero, then --mode full). Each moves the same 4 values, all idiom-wrapper call-site counts. Those counts sum over each function's calls_out_to, so a call inside nested units was counted once per enclosing unit:

  • cpython ceval.c debug_prints wrapped sites 52 → 13. The C slicer nests 29–34 units inside three over-long units that span preprocessor blocks; before this PR each of those also carried the inner units' calls.
  • gnucobol numeric.c memory_alloc (cob_gmp_free) 9 → 7.
  • redis printf.lua debug_prints (printf) 192 → 153.

The session's network policy blocks openaipublic.blob.core.windows.net, so the full-precision run loaded tiktoken's cl100k_base from a local cache. That file was rebuilt from the js-tiktoken npm package; its SHA-256 matches tiktoken's expected hash, which tiktoken re-checks on load.

Test changes

  • New tests/extraction/languages/test_calls_out_nested_body_3642.py:
    • a nested function's calls are not the outer function's, and the nested function keeps its own (Python, embedded Python, JS, TS, Rust, C# local function);
    • a callee called both outside and inside the nested function stays on the outer one;
    • qualifiers come only from the outer function's own calls;
    • a lambda's calls stay on the outer function;
    • with two levels of nesting, each call goes to the innermost function.
  • test_calls_out_nested_decl_3360.py::test_default_argument_call_on_a_nested_header_is_still_a_call: def inner(x=make()) now puts make on inner, which matches the tree-sitter side of call_graph_accuracy.py. Needs a decision: Python evaluates that default when the outer function runs, so if C8 should carve out parameter lists, that's a small follow-up.
  • test_calls_out_keywords_3359.py typescript case: go() sat inside const f = async () => …, a named arrow that C8 gives to f. It's now an anonymous p.then(async () => …) callback, so the case still checks that async ( is not a call.

Verification

$ pytest tests/                                                   -- 11335 passed, 334 skipped, 0 failed
$ pytest tests/extraction/languages/ -k calls_out  (final head)   -- 200 passed
$ PATH=.venv/bin:$PATH python tests/tools/audit_check.py          -- all clear (ruff, mypy, dead-key, ast-accuracy)
$ python tests/tools/call_graph_accuracy.py --buckets 3           -- before/after table above
$ python tests/tools/call_graph_accuracy.py --regenerate --ledger -- re-run after merging main
$ python tests/tools/crucible_check.py --mode zero --update --yes -- 4 differences, blessed
$ python tests/tools/crucible_check.py --mode full --update --yes -- same 4 differences, blessed

main is merged in (#3669's ledger auto-update conflicted: took main's side and re-merged this branch's shapes).

🤖 Generated with Claude Code

https://claude.ai/code/session_01GCpfMCUMpco98QGbsuv2sr

…t's (C8, #3642)

The detector scanned each unit's whole block for calls, so every call inside a
nested named unit the slicer also emitted was listed on the enclosing unit too
(#3360's C5 fix dropped only the nested header). Each C-style/ruby call site's
offset is now kept per slice, and _drop_nested_unit_calls blanks every named
unit nested inside a unit's span before keeping a callee; qualifiers are rebuilt
from the surviving sites. Synthetic buckets are not named units, so anonymous
bodies' calls stay with the enclosing unit (C8's second bullet).

Golden masters not yet re-blessed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01GCpfMCUMpco98QGbsuv2sr
@github-actions

Copy link
Copy Markdown
Contributor

🐦‍⬛ Muninn Security Scan

✅ No security issues found.

🐦‍⬛ Powered by Muninn · Skald Lab

…3642)

_apply_literal_shield changes lengths (a comment becomes ''), so call offsets
taken on the shielded block did not line up with the nested units' spans and
the first cut dropped the wrong calls (python recall 99.2 -> 98.2 on the
crucible). A unit with named units nested inside it is now rescanned with
them blanked out of its original block, newlines kept, through the same
shield and calls_out pattern.

Regenerates the call_graph_accuracy baseline and merges the shapes into
graph_comparison_ledger.json: python precision 96.3 -> 99.6, javascript
87.6 -> 97.3, typescript 94.0 -> 99.6, csharp 93.9 -> 99.2, java 99.1 -> 100,
rust 87.4 -> 89.8.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01GCpfMCUMpco98QGbsuv2sr
Four values move, all idiom-wrapper call-site counts, which are summed over
functions' calls_out_to: a call inside a unit the slicer nests in another was
counted once per enclosing unit. cpython ceval.c debug_prints 52 -> 13 (the C
slicer nests 29-34 units inside three over-long preprocessor-spanning units),
gnucobol numeric.c memory_alloc 9 -> 7, redis printf.lua printf 192 -> 153.

test_calls_out_keywords_3359's typescript case wrote go() inside a named
arrow (const f = async () => ...), which C8 gives to f; the case now uses an
anonymous callback so it still tests that async( is not a call.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01GCpfMCUMpco98QGbsuv2sr
@squid-protocol squid-protocol changed the title calls_out: a nested named unit's calls are its own, not the outer uni… calls_out: calls inside a nested named function belong to that function only (C8, #3642) Sep 25, 2026
graph_comparison_ledger.json conflicted with main's auto-update (#3669): took
main's side and re-merged this branch's shapes with call_graph_accuracy.py
--regenerate --ledger (baseline unchanged).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01GCpfMCUMpco98QGbsuv2sr
The same four idiom-wrapper call-site counts the zero-dependency fixture
moved (cpython ceval.c debug_prints 52 -> 13, gnucobol numeric.c
memory_alloc 9 -> 7, redis printf.lua printf 192 -> 153); nothing else.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01GCpfMCUMpco98QGbsuv2sr
@squid-protocol
squid-protocol merged commit 043d9f9 into main Sep 25, 2026
34 checks passed
@squid-protocol
squid-protocol deleted the claude/sleepy-gates-xyog68 branch September 25, 2026 20:05
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.

calls_out: a function's calls include every nested named function's calls (C8)

2 participants