Skip to content

Scope dampener correlation to real function boundaries (#346 phase 1) - #349

Merged
squid-protocol merged 1 commit into
mainfrom
feature/346-satellite-scoped-dampener-correlation
Jul 24, 2026
Merged

squid-protocol merged 1 commit into
mainfrom
feature/346-satellite-scoped-dampener-correlation

Conversation

@squid-protocol

Copy link
Copy Markdown
Owner

Summary

Fixes #346 (phase 1 -- see the issue for the split; phase 2, persisting a satellite-aware ledger for post-hoc tools and scoping the amplifier pairs, is tracked separately in #348).

  • detector.py's three dampener-pair correlations -- "2. The Silencer Region" (safety mitigating high_risk_execution), the sync_locks half of "3. The Race Condition Radar", and "5. The Memory Leak / UAF Tracker" (cleanup mitigating memory_alloc) -- all used a flat character-radius proximity check as their only notion of "nearby". A safety/cleanup call in a completely different function could silently cancel real risk in this one, as long as it fell within the radius (500/300/800 chars respectively) -- a false negative, not just noise, since these blocks reduce reported risk rather than add to it.
  • None of the six correlate() calls in coding_analysis() had access to real function boundaries, because coding_analysis() (where they ran) executed before _function_slice() (which computes satellite/function ranges) in splice().
  • Fixed by:
    • Extracting _correlate_signals into gitgalaxy/core/spatial_correlation.py as a shared, reusable primitive (StructuralExtractor._correlate_signals is now a thin delegating wrapper, kept for existing callers).
    • Adding a scope-aware correlate_scoped(): a target/dampener pair must fall within the same satellite range to correlate; a target outside every known satellite range falls back to the pre-scoping flat behavior, so nothing that correlated before this change stops correlating -- module-level/unsliced code is unaffected.
    • Relocating the three dampener blocks out of coding_analysis() into apply_dampener_correlations(), called from _function_slice()'s own per-segment loop right after that segment's satellites are computed -- the one place spatial_map and real satellite ranges already coexist, with no new cross-referencing needed.
    • Adding start_idx/end_idx to each satellite (FunctionNode), computed but previously discarded in _calculate_block_metrics.
    • Removing _function_slice()'s early MAX_SATELLITES break: that cap bounds stored function metadata, not which code gets correlated -- letting it silently skip correlating the remainder of an unusually large file would have been a regression this fix specifically exists to prevent. Only matters for files with 250+ functions.
  • The three amplifier-only pairs (RCE/IO taint corroboration, active secret-leak amplification, and the concurrency-amplifying half of the Race Condition Radar) are intentionally left on the flat radius -- a false positive there costs one extra review item, an accepted tradeoff, not the false-negative risk this phase targets.

Test plan

  • Added tests/core_engine/test_spatial_correlation.py (12 tests) covering the new primitives in isolation: baseline correlate_signals parity, filter_positions_in_range, correlate_scoped (cross-function rejection, same-function acceptance, module-level fallback, mixed covered/uncovered), and apply_dampener_correlations for all three relocated blocks.
  • Added test_detector_dampeners_do_not_cross_function_boundaries and test_detector_dampeners_still_apply_within_same_function in test_detector.py, exercising the fix through the real StructuralExtractor.splice() path. Verified the cross-function test fails on the pre-fix code (0 == 1, proving the false-negative) and passes after.
  • python -m pytest tests/core_engine/ tests/security_auditing/ -q -- 433 passed, 1 xfailed (pre-existing, unrelated).
  • python -m pytest tests/ -q -- full suite, 826 passed, 1 xfailed.

🤖 Generated with Claude Code

detector.py's three dampener-pair correlations -- "2. The Silencer
Region" (safety mitigating high_risk_execution), the sync_locks half of
"3. The Race Condition Radar", and "5. The Memory Leak / UAF Tracker"
(cleanup mitigating memory_alloc) -- all used a flat character-radius
proximity check as their only notion of "nearby". A safety/cleanup call
in a completely different function could silently cancel real risk in
this one, as long as it fell within the (500/300/800-char) radius --
a false negative, not just noise, since these blocks reduce reported
risk rather than add to it.

None of the six correlate() calls in coding_analysis() had access to
real function boundaries, because coding_analysis() (where they ran)
executed before _function_slice() (which computes satellite/function
ranges) in splice(). Fixed by:

- Extracting _correlate_signals into gitgalaxy/core/spatial_correlation.py
  as a shared, reusable primitive (StructuralExtractor._correlate_signals
  is now a thin delegating wrapper, kept for existing callers).
- Adding a scope-aware correlate_scoped(): a target/dampener pair must
  fall within the same satellite range to correlate; a target outside
  every known satellite range falls back to the pre-scoping flat
  behavior, so nothing that correlated before this change stops
  correlating -- module-level/unsliced code is unaffected.
- Relocating the three dampener blocks out of coding_analysis() into
  apply_dampener_correlations(), called from _function_slice()'s own
  per-segment loop, right after that segment's satellites are computed
  -- the one place spatial_map and real satellite ranges already
  coexist, with no new cross-referencing needed.
- Adding start_idx/end_idx to each satellite (FunctionNode), computed
  but previously discarded in _calculate_block_metrics.
- Removing _function_slice()'s early MAX_SATELLITES break: that cap
  bounds stored function *metadata*, not which code gets correlated --
  letting it silently skip correlating the remainder of an unusually
  large file would have been a regression this fix specifically exists
  to prevent. Only matters for files with 250+ functions.

The three amplifier-only pairs (RCE/IO taint corroboration, active
secret-leak amplification, and the concurrency-amplifying half of the
Race Condition Radar) are intentionally left on the flat radius --
a false positive there costs one extra review item, an accepted
tradeoff, not the false-negative risk this phase targets. Persisting a
satellite-aware ledger for post-hoc tools (#105/#106) and scoping the
amplifier pairs is tracked separately in #348.

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

Copy link
Copy Markdown
Contributor

🐦‍⬛ Muninn Security Scan

✅ No security issues found.

🐦‍⬛ Powered by Muninn · Skald Lab

@squid-protocol
squid-protocol merged commit 93ede71 into main Jul 24, 2026
22 checks passed
@squid-protocol
squid-protocol deleted the feature/346-satellite-scoped-dampener-correlation branch July 24, 2026 02:19
squid-protocol added a commit that referenced this pull request Jul 26, 2026
…alc_cog_load) (#506)

Found via CodeQL's py/redundant-comparison + py/unreachable-statement
alerts (#347, #349): `if safe_loc < 15: ... return 5.0, total_density`
returns unconditionally for any 1-14 LOC file, so the later
`if safe_loc <= 2 and total_density == 0: return 0.0, total_density`
could never execute -- safe_loc is always >= 15 by the time execution
reaches it. Every tiny file, including a provably empty stub (e.g. a
near-blank __init__.py), was forced to a cognitive_load score of 5.0.

The dead code itself documents the original intent: a truly empty
<=2 LOC file shouldn't get even the small-file floor. Moved that exact
check inside the reachable `< 15` branch rather than inventing new
behavior, and removed the now-fully-dead duplicate below it.

The carve-out only fires for tier1 languages (irc == 0) since irc
feeds into total_density too -- tier2/3 files always carry a nonzero
floor from irc alone. Verified against the language-crucible corpus in
both zero-dependency and full-precision mode: exactly one file
(rust/tokio/fuzz.rs, tier1) changed from a phantom 5.0% to a correct
0.0%, rippling into its directory/global averages. Regenerated both
golden masters and added two unit tests (empty tiny file -> true 0.0,
small file with real signal -> unchanged 5.0 floor).

Full suite: 869 passed, 1 pre-existing xfail. Baseline regenerated for
line-shift from removing the dead duplicate (no new findings).

Co-authored-by: Joe Esquibel <[email protected]>
Co-authored-by: Claude Sonnet 5 <[email protected]>
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.

[FEATURE/CORE-ENGINE] Extract a shared, scope-aware spatial correlation primitive and make threat_locations the persisted ledger

1 participant