Skip to content

feat(graph): GET/POST /graph/structural-stats — full-graph Cat 5/8 (gated) - #211

Merged
jphein merged 1 commit into
mainfrom
feat/graph-structural-stats
May 31, 2026
Merged

feat(graph): GET/POST /graph/structural-stats — full-graph Cat 5/8 (gated)#211
jphein merged 1 commit into
mainfrom
feat/graph-structural-stats

Conversation

@jphein

@jphein jphein commented May 31, 2026

Copy link
Copy Markdown
Collaborator

What

Adds a gated daemon endpoint that computes the exact full-graph SME Cat 5 (connectivity / isolates) and Cat 8 (modularity) stats over the whole RELATION knowledge graph — replacing the biased, sample-dependent numbers SME's --real-kg adapter currently derives from a capped /graph?limit=N slice.

Why (green-Somnia's catch)

SME's --real-kg path reads a /graph?limit=N sample whose absolute Cat 5/8 numbers drift with the limit and don't equal the full graph — the other relation fraction alone moves 47% (5k) → 58% (10k) vs 55% full. Cat 4 is unaffected (it already uses the exact full-graph GROUP BY), but Cat 5/8 needed a true full-graph computation. The verdicts (well-connected, hierarchical-PASS) are robust across samples; the precise absolute figures were not publishable from a sample.

How

A row-returning MATCH ()-[r:RELATION]->() OOMs AGE (#160 — agtype edge-object materialization), so this reads the endpoint integer pairs (start_id, end_id) directly off mempalace_kg."RELATION" (no agtype — the kg_reader.read_kg_postgres pattern) and computes:

  • Cat 5 connectivity via a dependency-free union-find (exact, ~O(E·α)): component count, largest-component size + fraction, isolate count (entities with zero RELATION degree, counted against the full Entity universe), size histogram.
  • Cat 8 modularity via networkx Louvain over the largest WCC. networkx is a soft dep (added to requirements.txt with the established rationale, imported only by this route); if absent → modularity: null + a note, connectivity unaffected.

⚠️ Heavy + gated

The compute is a GB-RAM / minutes spike over ~1.9M edges, so it is never run inline on demand:

  • POST /graph/structural-stats triggers it once; GET returns the cached payload (404 until first computed — a plain read never computes).
  • POST is write-auth gated (header key, not the viz cookie) and refused 409 while a bench is active (.bench-active.lock), run under the read semaphore + off the event loop, result cached.
  • 503 under chroma / unreachable AGE.

The one-time production run is intended off-peak, surfaced for scheduling — not inline on the box serving the live palace.

Tests

33 green (1 modularity test skips without networkx), ruff clean. Validated against synthetic graphs with known answers (two disjoint triangles, isolates-vs-full-universe, clique-bridge-clique modularity, empty graph, None-endpoints) + route gating/caching (GET-404-before-compute, POST-409-under-bench, compute+cache+serve, 503-on-None, modularity-flag threading). No prod compute was run — the real 1.9M-edge population waits for an off-peak window.

SME consumer side (get_structural_stats() adapter method + cat5/cat8 wiring) lands in a separate SME PR.

Refs #147.

🤖 Generated with Claude Code

…ated)

green-Somnia's catch: SME's --real-kg adapter reads a SAMPLED /graph?limit=N
slice whose Cat 5 (connectivity/isolates) and Cat 8 (modularity) absolute
numbers DRIFT with the limit and ≠ the full graph (the `other` fraction alone
moves 47%→58% across sample sizes). Cat 4 is safe (it uses the exact full-graph
GROUP BY aggregation), but Cat 5/8 needed an exact full-graph computation.

A row-returning `MATCH ()-[r:RELATION]->()` OOMs AGE (#160 — agtype edge-object
materialization), so this reads the endpoint INTEGER pairs (start_id, end_id)
directly off the `mempalace_kg."RELATION"` label table (no agtype) — the
kg_reader.read_kg_postgres pattern — and computes:
  * Cat 5 connectivity via a dependency-free union-find (exact, ~O(E·α)):
    component_count, largest_component_size + fraction, isolate_count (entities
    with zero RELATION degree, counted against the full Entity universe),
    component_size_histogram.
  * Cat 8 modularity via networkx Louvain over the largest WCC. networkx is a
    soft dep (added to requirements with the established "degraded-state worse
    than disk" rationale, imported only by this route); absent → modularity
    None + a note, connectivity unaffected.

⚠️ HEAVY + GATED — the compute is a GB-RAM / minutes spike over ~1.9M edges, so
it is NEVER run inline on demand:
  * POST /graph/structural-stats triggers it once; GET returns the cached
    payload (404 until first computed — a plain read never computes).
  * POST is write-auth gated (header key, not the viz cookie) and REFUSED 409
    while a bench is active (.bench-active.lock), run under the read semaphore
    + off the event loop, result cached.
  * 503 under chroma / unreachable AGE.

The one-time production run is intended off-peak (not inline on the box serving
the live palace). Validated against synthetic graphs with known WCC/modularity
(two-triangles, isolates-vs-full-universe, clique-bridge-clique, empty,
None-endpoints) + route gating/caching tests — NO prod compute run here.

33 tests green (1 modularity test skips without networkx), ruff clean.
SME consumer (get_structural_stats() + cat5/cat8) lands separately.

Refs #147.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings May 31, 2026 02:02
@gemini-code-assist

Copy link
Copy Markdown

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

@jphein
jphein merged commit c7bc865 into main May 31, 2026
1 check failed
@jphein
jphein deleted the feat/graph-structural-stats branch May 31, 2026 02:03

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

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.

2 participants