Skip to content

Spatially verify Metaprogramming Hallucination Risk in Dev Agent Firewall (#106) - #360

Merged
squid-protocol merged 1 commit into
mainfrom
feature/106-metaprogramming-hallucination-risk
Jul 24, 2026
Merged

squid-protocol merged 1 commit into
mainfrom
feature/106-metaprogramming-hallucination-risk

Conversation

@squid-protocol

Copy link
Copy Markdown
Owner

Summary

Closes #106, and resolves #345. Landing order per the #102 epic (#344 -> #346 -> #105 -> #106): with #348's persisted threat_locations ledger and correlate_against_ledger() already proven out by #105, this is a second, even simpler consumer -- both signals it needs (reflection_metaprogramming and doc) are native detector.py rules, not security_lens-only, so there's no cross-worker merge dependency at all.

Replaces dev_agent_firewall.py's rule 3 (Metaprogramming Hallucination Risk), which was fully dead code per #345: it read file_data["telemetry"]["doc_density"], a key no producer anywhere ever wrote, so the check could never fire. Even a working version of the old check had two structural blind spots #106 called out: a global per-file doc-density average (a) misses undocumented metaprogramming buried in an otherwise well-documented file, and (b) false-positives on well-documented metaprogramming just because the rest of the file is sparse.

correlate_against_ledger() fixes both by proving proximity directly: a reflection_metaprogramming hit only flags hallucination_zone if no doc hit falls within the same function and max_distance=10 lines -- reusing the exact fixture value test_spatial_correlation.py already anticipated for this signal pair. Also drops the now-fully-unused signal_schema/hit_vector reads in evaluate_ecosystem, and exposes the raw count as guardrails["undocumented_metaprogramming"] per #106's proposed telemetry export.

Architectural integration, confirmed rather than assumed

Before landing, I traced the underlying data path end to end:

  • Satellite ranges are guaranteed non-overlapping by construction -- _slice_by_braces/_slice_by_indentation track last_end_idx and skip any func_start match nested inside the previous satellite, so correlate_scoped()'s "sorted, non-overlapping" assumption is structurally enforced, not just documented.
  • threat_locations/functions demonstrably survive from the per-file worker through _calculate_risk_exposures (Phase 3, spread via **meta) and build_dependency_graph (Phase 4, confirmed in-place mutation -- it already reads f.get("functions", []) itself) into the parsed_files list DevAgentFirewall consumes in Phase 5.

Test plan

  • test_dev_agent_firewall.py's hallucination-zone test rewritten to drive the ledger instead of the dead doc_density key.
  • Two new tests proving both of # [BUG/APPSEC] Spatially Verify Metaprogramming Hallucination Risk in Dev Agent Firewall #106's blind spots are fixed: locally-documented metaprogramming is spared (test_hallucination_zone_spares_locally_documented_metaprogramming), and a doc comment in a different function does not paper over undocumented metaprogramming in another (test_hallucination_zone_ignores_documentation_in_a_different_function).
  • python -m pytest tests/ -q -- full suite, 836 passed, 1 pre-existing xfail.

🤖 Generated with Claude Code

…wall (#106)

Landing order per the #102 epic (#344 -> #346 -> #105 -> #106): with
#348's persisted threat_locations ledger and correlate_against_ledger()
already proven out by #105, #106 is a second, even simpler consumer --
both signals it needs ("reflection_metaprogramming" and "doc") are
native detector.py rules, not security_lens-only, so no cross-worker
merge dependency exists at all.

Replaces dev_agent_firewall.py's rule 3 (Metaprogramming Hallucination
Risk), which was fully dead code per #345: it read
file_data["telemetry"]["doc_density"], a key no producer anywhere ever
wrote, so the check could never fire. Even a working version of the
old check had two structural blind spots described in #106: a global
per-file doc-density average (a) misses undocumented metaprogramming
buried in an otherwise well-documented file, and (b) false-positives
on well-documented metaprogramming just because the rest of the file
is sparse.

correlate_against_ledger() fixes both by proving proximity directly:
a "reflection_metaprogramming" hit only flags "hallucination_zone" if
no "doc" hit falls within the SAME function and max_distance=10 lines
-- reusing the exact fixture value test_spatial_correlation.py already
anticipated for this signal pair in #348's own test suite. Also drops
the now-fully-unused signal_schema/hit_vector reads in
evaluate_ecosystem, and exposes the raw count as
guardrails["undocumented_metaprogramming"] per #106's proposed
telemetry export.

Before landing, confirmed the underlying integration end to end rather
than assuming it: satellite ranges are guaranteed non-overlapping by
construction (_slice_by_braces/_slice_by_indentation track
last_end_idx and skip any nested match), and threat_locations/functions
demonstrably survive from the per-file worker through
_calculate_risk_exposures (Phase 3, spread via **meta) and
build_dependency_graph (Phase 4, confirmed in-place mutation by reading
f.get("functions", []) itself) into the parsed_files DevAgentFirewall
consumes in Phase 5.

Tests: test_dev_agent_firewall.py's existing hallucination-zone test
rewritten to drive the ledger instead of the dead doc_density key, plus
two new tests proving both of #106's blind spots are fixed (locally
documented metaprogramming is spared; a doc comment in a DIFFERENT
function does not paper over undocumented metaprogramming).

Full suite: 836 passed, 1 pre-existing xfail.

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 4cf3cd8 into main Jul 24, 2026
22 checks passed
@squid-protocol
squid-protocol deleted the feature/106-metaprogramming-hallucination-risk branch July 24, 2026 13:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant