Skip to content

Fix nested functions being silently dropped from extraction (#1041) - #1045

Merged
squid-protocol merged 4 commits into
mainfrom
fix/1041-nested-functions
Aug 5, 2026
Merged

squid-protocol merged 4 commits into
mainfrom
fix/1041-nested-functions

Conversation

@squid-protocol

Copy link
Copy Markdown
Owner

Summary

  • Mode B (_slice_by_braces) and Mode C (_slice_by_indentation) in gitgalaxy/core/detector.py both had an identical guard -- if start_idx < last_end_idx: continue -- that skipped any func_start match whose start fell before the previously accepted match's end. A nested/inner function declaration necessarily starts before its enclosing function's end, so this guard silently dropped every nested function instead of ever making it its own FunctionNode (undercounting function_count, and merging the nested function's complexity into its parent's aggregate metrics instead of reporting it separately).
  • The guard was unnecessary: each match already resolves its own end independently from its OWN start position (brace-depth tracking via _find_balanced_end for Mode B, dedent tracking for Mode C) -- so once a nested match is allowed to be processed at all, it already gets a correctly bounded, correctly nested scope with no further changes needed. Removing the guard (and the now-dead last_end_idx bookkeeping) is the whole fix.
  • Mode A (labels/assembly) never had this guard and is unaffected, per the issue.

Verification

  • Repro'd the bug pre-fix (Python def-in-def and a brace-style nested function both collapsed to 1 extracted function instead of 2), confirmed fixed post-fix, including a sibling-nested-functions case (two inner defs at the same nesting depth under one outer function).
  • Added regression tests: test_detector_nested_function_is_counted_as_own_node_braces and test_detector_nested_function_is_counted_as_own_node_indentation in tests/core_engine/test_detector.py.
  • Full local suite: 5435 passed, 2 skipped, 1 deselected, 11 xfailed, 3 xpassed.
  • tests/tools/audit_check.py --regenerate: ruff baseline updated for 2 pure line-shift findings from the removed dead code (no new findings); mypy and dead-key audits clean.

Note for reviewers

This is a parsing-logic change to detector.py, so per the repo's Differential Scan protocol it should be checked against tests/golden_master_audit.json / tests/golden_master_zero_dep_audit.json by CI's crucible-audit check against the ~80-repo corpus. I don't have LANGUAGE_CRUCIBLE_PATH set up locally, so I did not run crucible_check.py -- deferring to CI here per the repo's cost-conscious-verification convention. Function counts are expected to increase for any corpus file with nested/inner functions (that's the intended fix), so if crucible-audit flags a diff, it should be reviewed and the golden master re-blessed via update_golden_master.py rather than treated as a regression by default -- happy to do that once CI reports the actual diff.

Test plan

  • pytest tests/core_engine/test_detector.py -q -- 82 passed
  • pytest tests/ -q -- full suite green
  • tests/tools/audit_check.py -- clean after baseline regen
  • CI crucible-audit -- pending, may require golden master re-bless (see note above)

🤖 Generated with Claude Code

Mode B (_slice_by_braces) and Mode C (_slice_by_indentation) both skipped
any func_start match whose start fell before the previously accepted
match's end ("if start_idx < last_end_idx: continue"). A nested/inner
function declaration necessarily starts before its enclosing function's
end, so this guard silently dropped every nested function instead of
counting it as its own FunctionNode -- undercounting function_count and
merging the nested function's complexity into its parent's aggregate
metrics instead of reporting it separately.

The guard turns out to be unnecessary: each match already resolves its
own end independently (brace-depth tracking via _find_balanced_end for
Mode B, dedent tracking for Mode C), from its OWN start position -- so a
nested match already gets a correctly bounded, correctly nested scope on
its own once it's allowed to be processed at all. Removing the guard (and
the now-dead last_end_idx bookkeeping) is enough; no stack/depth tracker
needed on top of what the per-match boundary resolvers already do.

Mode A (labels/assembly) never had this guard and is unaffected.

Adds regression tests for both brace-style and indentation-style nested
functions, verifying the outer and inner functions are both extracted as
distinct FunctionNodes with independently correct scopes.

Closes #1041.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

🐦‍⬛ Muninn Security Scan

✅ No security issues found.

🐦‍⬛ Powered by Muninn · Skald Lab

#1041)

Regenerated tests/golden_master_audit.json and
tests/golden_master_zero_dep_audit.json via
tests/tools/crucible_check.py --update against the language-crucible
corpus, per the Differential Scan protocol.

The prior commit (nested functions no longer silently dropped from
extraction) intentionally changes structural output for any corpus file
containing nested/inner functions or function expressions -- function
counts, per-function complexity attribution, and every metric downstream
of them (risk exposures, cluster assignments, cumulative mass/impact)
shift accordingly across roughly 8,840 leaf values in each fixture. Spot-
checked the diff for the failure modes that would actually indicate a
bug rather than the intended fix: no tracebacks/exceptions, no crashes,
and every composition/impact delta samples in the expected direction
(more structure now counted, not less -- e.g. perl/mojo's cluster
membership and total_mass both increase). Both full-precision and
zero-dependency crucible_check.py runs are clean against the
regenerated fixtures.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@squid-protocol

Copy link
Copy Markdown
Owner Author

Re-blessed the golden master fixtures against the language-crucible corpus (crucible_check.py --update, both full-precision and zero-dependency modes) -- see the follow-up commit for the full rationale. `crucible_check.py` now passes clean in both modes locally; pushing to let CI's `crucible-audit` confirm.

squid-protocol and others added 2 commits August 4, 2026 20:59
…ions

# Conflicts:
#	tests/golden_master_audit.json
#	tests/golden_master_zero_dep_audit.json
#	tests/ruff_audit_baseline.json
#1040 (nested class boundaries) merged to main after this branch's own
golden master re-bless for #1041 (nested functions), so the merge above
combined both fixes but carried a stale golden master that only reflected
#1041 in isolation. Regenerated both fixtures via
tests/tools/crucible_check.py --update against the language-crucible
corpus so they reflect #1040 and #1041 together.

The bulk of this delta is attributable to #1040 alone (already reviewed
and merged separately) -- e.g. cpp/godot/object.h's bogus 464-line
"initialize_class" function (the exact case called out in #1040's own PR
description) drops out of the function list here because the class-scope
fix now correctly consumes it as part of Object's class body instead of
mis-parsing it as a giant standalone function. crucible_check.py passes
clean in both full-precision and zero-dependency modes against the
regenerated fixtures, with no tracebacks/exceptions in the diff.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@squid-protocol
squid-protocol merged commit b60bbed into main Aug 5, 2026
39 of 41 checks passed
@squid-protocol
squid-protocol deleted the fix/1041-nested-functions branch August 5, 2026 01:21
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.

1 participant