Skip to content

Native graph engine: closeness + average path length, native in both modes (#3037) - #3044

Closed
squid-protocol wants to merge 3 commits into
mainfrom
fix/3037-closeness-path-length
Closed

squid-protocol wants to merge 3 commits into
mainfrom
fix/3037-closeness-path-length

Conversation

@squid-protocol

Copy link
Copy Markdown
Owner

Closes #3037. Part of #3033 (the native graph engine). Builds on #3042 (#3034, merged): it uses that PR's GraphIndex and WorkBudget.

What changed

graph_engine.closeness_and_path_length(index, budget) runs one breadth-first search per node over the reversed CSR index. That single pass produces both hop-count metrics:

  • Closeness (per file): strict parity with nx.closeness_centrality(G). That is directed, incoming distance, in Wasserman-Faust form. Registered in the parity harness as strict.
  • Average path length (repo): redefined per the tailoring decisions on Native graph engine: replace networkx with a stdlib-only implementation (zero-dependency, exact parity, faster) #3033.
    • Now: the mean hop count over every ordered pair (A, B) where A transitively imports B, across all files. It is None when no file imports another.
    • Before: the mean shortest path in the largest undirected component. That ignored import direction and covered 22.5% of the crucible's files.
    • It is registered in the harness as tailored. The oracle is per-source nx.single_source_shortest_path_length.
  • Cutoffs → a work budget. The V>1500 (closeness) and V>5000 (path length) node-count cutoffs are gone. PATH_METRICS_WORK_BUDGET is 50M incoming-edge scans, about 3 s of pure-Python search in the worst case. It counts work, never time, so the golden masters can't flake. Past the budget, both metrics are None, never 0.0.
  • Both modes. Like PageRank (Zero-dependency mode: networkx metrics that were never computed read as 0.0 outside the DB (false 'Containment' insight, 0.0 macro table, HITL never fires) #3027), this runs in the networkx and zero-dependency builders alike, from the same index. Zero-dependency mode therefore gains closeness, the "fragile dependency chain" ranking, and path length.

Edge weight plays no part in either metric: it stays a PageRank strength. networkx's weighted paths would treat it as a distance, which is backwards (#3033).

Measured on language-crucible v1.2.0 (V=2,817, E=1,354; warm, best of 5, tests/tools/graph_parity.py --db)

metric parity native before
closeness strict, max |diff| 0.0 1.72 ms 12.87 ms (nx), and skipped at this size
avg path length tailored, max |diff| 0.0 1.61 ms about 1.2 s (nx, undirected LCC)

Both metrics come out of one pass: 1.7 ms in total.

⚠️ Definition change: temporal-crucible / snapshot history

repo_data.network_avg_path_length keeps its column, but its meaning changes with this PR. Values from before and after are not comparable. On language-crucible it was 7.0723 and is now 1.358. docs/zero_dependency_mode.md records this under "Older snapshots", and the brief's §3.5 interpretation text now describes the new definition. Closeness was NULL in zero-dependency mode and above 1,500 files before this PR, so older snapshots show NULL where newer ones have values.

Golden masters: re-blessed after an uncapped, scoped diff

Scoped with golden_diff.deep_compare over a fresh scan of each mode, using GITGALAXY_DISABLE_GIT_HISTORY=1 and the same flags as test_golden_crucible.py.

  • full precision: 2 differences, both intended
    • network_macro/avg_path_length: 7.0723 → 1.358 (the redefinition)
    • systemic_bottlenecks/fragile_dependency_chain: [] → the top closeness × error-risk files. Closeness used to be skipped above 1,500 files, which covers the whole crucible.
  • zero dependency: 2 differences, both intended
    • avg_path_length: None → 1.358
    • the same fragile_dependency_chain ranking as full precision
  • Closeness is identical in the two modes for all 2,817 files: 0 mismatches between the two scans' DBs. Before this PR, all 2,817 were NULL in both modes; now 300 are non-zero.
  • No coordinate or other metric moved in either mode. Closeness feeds no mass or risk formula.
  • The only commit on main since the fixtures were last blessed is Native graph engine: shared CSR graph index + parity/benchmark harness (#3034) #3042, which moved neither fixture. So every difference above belongs to this PR.

Verification

  • The parity harness passes on 25 seeded random graphs for each metric, and on the real crucible graph (CLI above).
  • New sensor tests:
    • a 1,501-file chain in both modes: closeness and path length are computed (formerly skipped) and equal the closed forms. The long chain also shows the search is iterative.
    • the work budget exhausted → None, not 0.0
    • a graph with no imports → path length None
  • Updated tests: zero-dependency closeness equals full precision. A networkx centrality failure or macro-block failure no longer takes down closeness or path length, since both are computed outside those blocks.
  • pytest passes on the 9 affected suites (308 passed), run as plain pytest from outside the repo.
  • audit_check.py: all clear.

🤖 Generated with Claude Code

squid-protocol and others added 2 commits September 14, 2026 18:21
…modes (#3037)

- graph_engine.closeness_and_path_length(): one BFS per node over the
  reversed CSR index yields both hop-count metrics.
  - closeness: strict parity with nx.closeness_centrality (Wasserman-Faust)
  - average path length: REDEFINED per #3033's tailoring decisions as the
    mean hops over every directed reachable pair (A transitively imports B)
    across all files, instead of the largest undirected component
  - None when no file imports another
- The V>1500 (closeness) and V>5000 (path length) node-count cutoffs are
  replaced by a deterministic WorkBudget: PATH_METRICS_WORK_BUDGET = 50M
  incoming-edge scans, past which both metrics are None, never 0.0.
- Runs in BOTH modes like PageRank, so zero-dependency mode now gets
  closeness, the "fragile dependency chain" ranking and path length.
- language-crucible (V=2,817): both metrics in 1.7 ms. Before this, closeness
  took 12.9 ms (nx) and was skipped at this size, and the undirected path
  length took about 1.2 s. Path length reads 1.358 (was 7.0723).
- Docs: zero_dependency_mode.md, the 02-16 wiki page, README, CLAUDE.md,
  the console banner and the LLM brief's section 3.5 text.

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

Scoped with an uncapped golden_diff over fresh scans of both modes: exactly
two differences per mode -- network_macro/avg_path_length (7.0723 / None ->
1.358) and systemic_bottlenecks/fragile_dependency_chain ([] -> populated,
closeness now computed at the crucible's 2,817 files).

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@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
@github-actions

Copy link
Copy Markdown
Contributor

🐦‍⬛ Muninn Security Scan

✅ No security issues found.

🐦‍⬛ Powered by Muninn · Skald Lab

Comment thread tests/tools/graph_parity.py Fixed
…ality

CodeQL (py/unnecessary-lambda) on #3044: `oracle=lambda graph:
nx.closeness_centrality(graph)` is a bare wrapper -- use the callable
directly. Behaviour-identical (the harness calls oracle(graph)). The
pagerank oracle keeps its lambda: it binds weight="weight", so it is not
a bare wrapper.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Claude-Session: https://claude.ai/code/session_014gkhy3FWC4ovUWkMDonGMr
@squid-protocol

Copy link
Copy Markdown
Owner Author

Closing: every change in this PR is already on main. #3046 was stacked on this branch, so its squash merge (492b029) carried #3037's commits too, including the f63649d lambda cleanup. Nothing is left to merge here.

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: closeness at every size + directed average path length

2 participants