Skip to content

Native graph engine: exact security_auditor reach counts, no 500 cap (#3040) - #3052

Merged
squid-protocol merged 2 commits into
mainfrom
fix/3040-exact-reach-counts
Sep 15, 2026
Merged

squid-protocol merged 2 commits into
mainfrom
fix/3040-exact-reach-counts

Conversation

@squid-protocol

Copy link
Copy Markdown
Owner

Closes #3040. Part of #3033 (the native graph engine). Stacked: #3047 (#3036) → #3048 (#3038) → #3049 (#3039) → this PR. The branch carries their commits until they merge. Only the last commit, plus its re-bless, is this PR's own.

What changed

security_auditor._resolve_dependency_graph now has one native implementation for both modes. It computes total_upstream, the files a file depends on (directly or transitively), and total_downstream, the files that depend on it. It replaces two implementations that disagreed:

networkx path pure-Python fallback now
definition min(len(nx.descendants), 500) deque BFS len(nx.descendants) exactly
a file on a dependency cycle not counted counted as its own dependency (+1) not counted
cap 500 500, but checked once per popped node, so it could overshoot none: exact (#3033's tailoring decisions)

graph_engine.reach_counts(index, budget) does the work. It condenses strongly connected components, then builds each component's reachable node set as a Python-int bitset:

  • descendants: the OR of its successor components' members and reach, built in Tarjan's completion order
  • ancestors: the same over predecessors, in reverse order
  • A node's count is its component's reach plus the rest of its own component.

strongly_connected_components() is the iterative Tarjan that nodes_in_cycles (#3035) already ran, now shared. Its components are numbered in reverse topological order of the condensation.

Memory guard: a Python int is as long as its highest set bit, so on a huge, deeply layered graph the bitsets can total O(N²) bits. The old 500 cap existed to prevent exactly that blow-up. Each OR now charges the deterministic WorkBudget (PATH_METRICS_WORK_BUDGET) in 64-bit words. Past the budget, the totals and ratios are None, never a placeholder 0 (#3027). The audit JSON then shows null, and the ML feature matrix maps None to NaN, which XGBoost treats as a missing value.

security_auditor no longer imports networkx.

Parity and speed

result
seeded random graphs exact 300/300 (prototype) plus 25 per direction in pytest (descendants/ancestors registered as tailored in graph_parity.py)
a 600-file chain (the old cap clipped it at 500) equal to len(nx.descendants/ancestors) for every file (new test)
a 5,000-file cycle every file reaches 4,999, not 5,000
language-crucible graph exact; 4.3 ms vs 7.0 ms per direction

Golden masters

I diffed fresh scans of each mode against the committed fixtures with an uncapped golden_diff.deep_compare. The scans used GITGALAXY_DISABLE_GIT_HISTORY=1 and the same flags as test_golden_crucible.py.

  • Full precision: 0 differences. The cap never bound on crucible (max 40 upstream, 65 downstream), and the native count equals len(nx.descendants/ancestors).
  • Zero dependency: 20 differences, all intended. They are 10 files × {Total Upstream, Total Downstream}, and every one drops by exactly 1, to the value full precision already reports. The 10 files are the ones on import cycles, which the old BFS fallback counted as their own dependency:
    • tcl/macports_registry/{portimage,registry}.tcl
    • zig/tigerbeetle/{config,constants}.zig
    • zig/zig/{Compilation,InternPool,Type,Zcu}.zig
    • zig/zls/{DocumentStore,analysis}.zig

The zero-dependency fixture is re-blessed in the second commit.

Verification

  • pytest passes on the affected suites: graph engine, security auditor, the sensor tests, state rehydrator, signal processor, and the LLM/record/audit recorders (536 passed).
  • New tests:
    • a cycle counts exactly 1 up and 1 down (the old fallback said 2)
    • the 600-file chain with no cap
    • past the budget → None
    • reach counts are iterative on a 5,000-file cycle and chain, and the budget raises
  • audit_check.py: all clear. The ruff baseline is regenerated for one pure line-shift (RUF046, same finding, moved).
  • Docs updated: zero_dependency_mode.md (the reach row is now identical in both modes; older-snapshots note) and the 02-20 wiki.

🤖 Generated with Claude Code

@squid-protocol squid-protocol added enhancement New feature, sensor, or structural signature metrics Heuristics, risk exposures, and topological math updates core-engine Modifications to the central physics and parsing engine labels Sep 15, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🐦‍⬛ Muninn Security Scan

✅ No security issues found.

🐦‍⬛ Powered by Muninn · Skald Lab

@squid-protocol
squid-protocol force-pushed the fix/3040-exact-reach-counts branch from 352d315 to fe7e87a Compare September 15, 2026 00:19
squid-protocol and others added 2 commits September 14, 2026 20:25
…3040)

- graph_engine.strongly_connected_components(): the iterative Tarjan
  nodes_in_cycles already ran, now shared. Components are numbered in
  completion order, i.e. reverse topological order of the condensation.
- graph_engine.reach_counts(): exact descendants/ancestors per node,
  computed by SCC condensation + Python-int reachability bitsets ORed along
  the condensation. Equal to len(nx.descendants)/len(nx.ancestors): a node
  is never its own dependency, not even on a cycle. Each OR charges a
  WorkBudget in 64-bit words, so a huge layered graph fails soft instead
  of exhausting memory.
- security_auditor._resolve_dependency_graph: one native implementation in
  both modes, replacing min(len(nx.descendants), 500) and the deque-BFS
  fallback. The fallback counted a cycle file as its own dependency, and
  its cap check could overshoot. Past the budget, totals and ratios are
  None; the ML feature matrix maps None to NaN (XGBoost's missing value).
- networkx is no longer imported by security_auditor.
- language-crucible: exact; 4.3 ms vs 7 ms per direction.
- ruff baseline regenerated for one pure line-shift (RUF046, unchanged).

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…ounts)

Uncapped golden_diff scope: full precision 0 differences (crucible_check
--update: no drift -- the cap never bound on crucible). Zero dependency
exactly 20: 10 files x {Total Upstream, Total Downstream}, each dropping
by exactly 1 to the full-precision value -- the files on import cycles,
which the old BFS fallback counted as their own dependency.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@squid-protocol
squid-protocol force-pushed the fix/3040-exact-reach-counts branch from fe7e87a to cb5cb2d Compare September 15, 2026 00:25
@squid-protocol
squid-protocol merged commit 76779f0 into main Sep 15, 2026
29 checks passed
@squid-protocol
squid-protocol deleted the fix/3040-exact-reach-counts branch September 15, 2026 00:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core-engine Modifications to the central physics and parsing engine enhancement New feature, sensor, or structural signature metrics Heuristics, risk exposures, and topological math updates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Native graph engine: exact security_auditor reach counts (no 500 cap)

1 participant