Plan: 006 — Knowledge Graph Explorer¶
Approach¶
Three layers, each hermetically testable:
- Cypher layer —
app/db/graph_queries.py. Three async functions that take anAsyncSessionand return typed dicts. Same shape asapp/db/citation_resolver.pysoFakeAsyncSessionfromtests/db/test_citation_resolver.pystyle works directly. - Read API —
app/api/graph.py. Three endpoints. A module-level_open_session()indirection keeps tests free of Neo4j: tests monkeypatch it to yield aFakeAsyncSession. Catches Neo4j-shaped exceptions and returnsavailable=false(NFR-DEGRADE-1). - UI —
app/graph/page.tsxserver shell +components/KnowledgeGraph.tsxclient component that composes a refactoredGraphViewer.tsx(now a true Cytoscape canvas, closing BL-20260430-05).
File Layout¶
backend/app/db/graph_queries.py (new, ~120 LOC)
backend/app/api/graph.py (new, ~140 LOC)
backend/app/main.py (edit — mount router)
backend/tests/db/test_graph_queries.py (new)
backend/tests/api/test_graph.py (new)
frontend/app/graph/page.tsx (new, server shell)
frontend/components/KnowledgeGraph.tsx (new, client)
frontend/components/GraphViewer.tsx (rewrite — real Cytoscape)
frontend/lib/api.ts (edit — graph fetchers + types)
frontend/__tests__/components/KnowledgeGraph.test.tsx (new, MSW-mocked)
Why These Choices¶
- Session-factory indirection: matches the pattern already used in spec
002's
densify_extraction. Tests stay docker-free; integration test is filed as a follow-up. - Graceful degradation over 503: a 5xx response would crash the
Knowledge Graph panel. Returning
{"available": false}lets the UI show a friendly banner and keeps the rest of the app responsive. - Cytoscape over React Flow: spec 001's data model + AGENTS.md both
call out Cytoscape; it's already in
package.json. Dynamic-import to avoid SSR pitfalls. - Refactor
GraphViewer.tsxrather than delete: keeps the existing import surface (used byKnowledgeGraphhere, and by future analysis views) and turns the open TODO into shipped code.
Cross-Cutting Concerns¶
- Provenance: every endpoint records via
provenance.log_graph_queryso the Agent Ops dashboard sees the hits. - Audit/HITL: read-only routes; no HITL needed.
- Tests: ≥ 75% backend (new modules ≥ 80%); ≥ 70% frontend.
Cytoscape is dynamically imported so vitest renders an empty canvas div — assertions target headings + element count via
data-testid.
Risk Mitigations¶
- R-1 (bundle size): dynamic
await import('cytoscape')inside the GraphViewer effect; Next.js code-splits it into a chunk only loaded on/graph. - R-2 (huge neighborhoods): the
neighborhoodCypher usesLIMIT $limiton both citation and cited-case branches; API capslimit ≤ 200.
Out-of-Scope (Tracked as Follow-ups)¶
- Path queries, court/date filters, authn/z, WebSocket push, integration test against a real Neo4j (mirrors BL-20260429-10 for resolver).