Skip to content

Native graph engine: exact hop-count betweenness at every size (#3038) - #3048

Merged
squid-protocol merged 3 commits into
mainfrom
fix/3038-exact-betweenness
Sep 15, 2026
Merged

squid-protocol merged 3 commits into
mainfrom
fix/3038-exact-betweenness

Conversation

@squid-protocol

Copy link
Copy Markdown
Owner

Closes #3038. Part of #3033 (the native graph engine). Stacked: #3044 (#3037) → #3046 (#3035) → the #3036 PR → 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

graph_engine.betweenness_centrality(index, budget) computes exact, unweighted Brandes betweenness on the CSR index.

Bit-identical to nx.betweenness_centrality(G), the tailored oracle (exact, unweighted, normalized, directed), not merely equal at 6 dp. It mirrors networkx's Brandes pass step for step:

  • BFS in adjacency order, with predecessors in discovery order
  • dependencies accumulated in reverse BFS order, sources in node order
  • then the directed 1 / ((N-1)(N-2)) rescale

A new test asserts == on 25 seeded graphs. The prototype matched 300/300, plus the crucible graph.

Other details:

  • A file with no outgoing import starts no path, so it is skipped as a source.
  • The run is gated by a deterministic WorkBudget (PATH_METRICS_WORK_BUDGET, counting edge scans). Past the budget, betweenness is None, never 0.0.
  • NetworkRiskSensor._betweenness() runs it in both modes, replacing nx.betweenness_centrality(G, k=100, weight="weight", seed=42). The networkx builder's centrality try block is gone.

After this PR, modularity (#3039) is the only metric networkx still computes.

⚠️ Definition change (#3033's tailoring decisions): temporal-crucible / snapshot history

before after
sources all files up to 500 files; 100 sampled (seed 42) above every file with an outgoing import, at every size
path length edge weight read as distance, so an entity import (a stronger coupling) counted as a longer path import hops

On language-crucible, the sample found 11 files with nonzero betweenness against 41 exact. It read 14 of the exact top-20 choke points as 0: the top-20 lists overlap on only 6 of 20.

betweenness_score values from before and after are not comparable above 500 files. Below 500, they differ only where an entity import changed a shortest path. docs/zero_dependency_mode.md records this under "Older snapshots".

Measured on language-crucible v1.2.0 (V=2,817, E=1,354; warm, graph_parity.py --db)

time
native exact 1.95 ms
networkx exact (the tailored oracle) 1,674 ms
networkx k=100 sampled, weighted (what the engine ran) 72 ms

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. Each mode has exactly 1 difference, and it is the intended one: systemic_bottlenecks/cascading_state_mutation, the betweenness × state-mutation ranking.

rank before, full precision (sampled, weighted) after, both modes (exact, hops)
1 python/twisted/http.py python/fastapi/fastapi/routing.py
2 python/twisted/defer.py python/twisted/http.py
3 lua/freebsd-src/color.lua dockerfile/moby/builder/dockerfile/copy.go
4 abap/abapGit/zabapgit.prog.abap lua/cosmopolitan/all.lua
5 abap/abapGit/zcl_abapgit_ajson.clas.abap abap/abapGit/zabapgit.prog.abap
  • In zero-dependency mode the ranking was [], because betweenness was never computed there. It is now the same list as full precision.
  • Per-file betweenness_score does not appear in the audit JSON, so no other field moved. There is no coordinate ripple: betweenness feeds no mass.
  • Both fixtures are re-blessed in the second commit.

Verification

  • pytest passes on the 10 affected suites, including test_supply_chain_firewall.py (460 passed), run as plain pytest from outside the repo.
  • New tests:
    • bit-identity with networkx exact
    • the work budget raises
    • the sensor never calls nx.betweenness_centrality, and both modes agree
    • past the budget, betweenness is None
    • zero-dependency betweenness is computed
  • audit_check.py: all clear.
  • Consumer comments updated (signal_processor, supply_chain_firewall). Docs updated: zero_dependency_mode.md (table, summary, heading, the rankings and insights that now work without networkx, the definition-change section, older snapshots), the 02-16 wiki (including the stale "k=50 sampling" tradeoff), README, CLAUDE.md, the console banner and the zero-dependency warning.

🤖 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 14, 2026
Comment thread tests/tools/graph_parity.py Fixed
@github-actions

Copy link
Copy Markdown
Contributor

🐦‍⬛ Muninn Security Scan

✅ No security issues found.

🐦‍⬛ Powered by Muninn · Skald Lab

squid-protocol and others added 3 commits September 14, 2026 19:20
- graph_engine.betweenness_centrality(): exact unweighted Brandes on the CSR
  index. It mirrors networkx step for step (BFS adjacency order, predecessor
  order, reverse-BFS accumulation, sources in node order, the directed
  1/((N-1)(N-2)) rescale), so it is bit-identical to
  nx.betweenness_centrality(G). Sources with no outgoing import are skipped,
  and the run is gated by a WorkBudget (PATH_METRICS_WORK_BUDGET).
- This replaces nx.betweenness_centrality(G, k=100, weight="weight", seed=42)
  per #3033's tailoring decisions. Above 500 files that call SAMPLED 100
  sources, and it read weight as distance. On language-crucible the sample
  found 11 nonzero files vs 41 exact and read 14 of the exact top-20 choke
  points as 0.
- Runs in BOTH modes via NetworkRiskSensor._betweenness, so zero-dependency
  mode gains betweenness, the "cascading state mutation" ranking and the
  "Cognitive Choke Point" insight. Modularity is now the only networkx
  metric left.
- language-crucible: 1.95 ms vs 1,674 ms for networkx's exact call (and
  72 ms for the sampled call it replaces).
- Registered as tailored in tests/tools/graph_parity.py. The docs note the
  definition change for snapshot history.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Uncapped golden_diff scope: exactly 1 difference per mode --
systemic_bottlenecks/cascading_state_mutation, the betweenness x
state-mutation ranking: re-ranked in full precision (sampled weighted ->
exact hops) and [] -> populated in zero-dependency mode (identical lists).

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Signed-off-by: Joe Esquibel <[email protected]>
@squid-protocol
squid-protocol force-pushed the fix/3038-exact-betweenness branch from 0fd7a8e to 71da0ae Compare September 15, 2026 00:05
@squid-protocol
squid-protocol merged commit 78b78df into main Sep 15, 2026
29 checks passed
@squid-protocol
squid-protocol deleted the fix/3038-exact-betweenness branch September 15, 2026 00:18
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 betweenness (hop-count BFS Brandes, every repo size)

2 participants