Inspiration

India replaced the Indian Penal Code, 1860 (IPC) with the Bharatiya Nyaya Sanhita, 2023 (BNS), renumbering criminal offences, splitting and merging provisions, and shifting statutory cross-references across chapters. Advocates, law students, investigators, and legal researchers have to translate legacy IPC section citations into BNS equivalents while preserving punishments, statutory definitions, and Chapter III General Exceptions.

Flat keyword lookup or chunked vector RAG struggles with statutory law for two concrete reasons:

  1. Section-number collisions: Searching "302" in flat text mixes up legacy IPC 302 (Punishment for murder) with BNS 302 (Uttering words, etc., with deliberate intent to wound religious feelings of any person), whereas murder punishment corresponds to BNS 103.
  2. Multi-hop statutory dependencies: An offence definition like BNS 101 (Murder) connects to separate punishment provisions (BNS 103, BNS 104), related offences (BNS 100, BNS 109), doctrinal concepts (CONCEPT_INTENTION_KNOWLEDGE), Section 2 definitions, and explicit caveats (EX_101_1 through EX_101_5 and Chapter III General Exceptions ss. 14–44).

We built LegPort (legport.onrender.com) to model the BNS 2023 ↔ IPC 1860 statutory structure in a single portable RelateDB property graph running directly in the browser.


What it does

LegPort loads public/data/bns_legal_graph.rl (960 nodes, 854 typed relationships) inside @relate/relate-wasm and exposes it through an interactive legal workspace:

  • Orbital Graph Explorer: Renders all 956 legal nodes (358 :BnsSection, 542 :IpcSection, 35 :ExceptionNode, 13 :DefinitionNode, 8 :LegalConcept) using a chapter-ordered BNS ring, radially aligned IPC spokes, connectivity-scaled hub sizing, smart neighborhood auto-framing, inline BNS/IPC search, and a Live RelateDB Cypher Console with 1-click query presets.
  • Side-by-Side BNS ↔ IPC Comparison: Follows ANALOGOUS_TO_IPC relationships from the BNS–IPC Correspondence Table to inspect differences side-by-side.
  • Exceptions & Defences Explorer: Navigates EXCEPTION_TO edges across Chapter III General Exceptions (ss. 14–44) and section-level exceptions.
  • Persistent Collections & Research Notes: Writes :SavedCollection dossiers and :ResearchNote nodes directly via Cypher (CREATE persisted in browser OPFS), with single-file .rl import/export.
  • Cypher-Grounded Streaming Assistant: Supports Groq (openai/gpt-oss-120b, openai/gpt-oss-20b, qwen/qwen3.8-27b) and Gemini (gemini-3.8-flash & gemini-4-*). Each turn queries RelateDB via tool calls (search_graph_nodes, get_node_and_neighbors, get_ipc_comparison, run_cypher_query), streams reasoning and Cypher traces live, and hydrates cited provisions into an interactive subgraph preview.

Why RelateDB vs. Non-Graph Storage

Legal Query Challenge Non-Graph / Flat Storage LegPort + RelateDB Cypher
Querying "302" Text/keyword search conflates IPC 302 (murder) with BNS 302 (religious feelings). Typed edges separate BNS_103 -[:ANALOGOUS_TO_IPC]-> IPC_302 from BNS_302 -[:ANALOGOUS_TO_IPC]-> IPC_298.
Connected defences Isolated chunks omit separate punishment sections, definitions, or Chapter III exceptions. Cypher traverses PUNISHED_BY, EXCEPTION_TO, DEFINES, and EXPLAINED_BY deterministically over local WASM.
Deployment Requires hosted vector/database infrastructure and embedding pipelines. Zero database server: runs client-side in a Web Worker over one inspectable .rl file (2.45 MB).

How we built it

  1. Single-File Relate Store (public/data/bns_legal_graph.rl): Extracted the 237-page BNS 2023 source PDF (Index pp. 2–19, Corresponding Table pp. 20–73, Statutory Text pp. 74–237) into a single .rl Cypher script containing 960 nodes (358 :BnsSection, 542 :IpcSection, 35 :ExceptionNode, 13 :DefinitionNode, 8 :LegalConcept, 3 :SavedCollection, 1 :ResearchNote) and 854 relationships (528 ANALOGOUS_TO_IPC, 121 REFERS_TO, 80 EXPLAINED_BY, 34 INCLUDES, 29 EXCEPTION_TO, 19 RELATED_TO, 16 PUNISHED_BY, 14 IN_COLLECTION, 13 DEFINES).
  2. Embedded WASM + OPFS Storage: Runs @relate/[email protected] inside a dedicated Web Worker (src/db/relateWorker.ts) opening RelateDb.open('opfs://relatedb_bns_v2') with an in-memory WASM fallback when OPFS is unavailable.
  3. Frontend & Graph Visualization: Built with React 18 + TypeScript + Tailwind CSS + Cytoscape.js (bundled via Vite and deployed at legport.onrender.com), scaling each node's diameter relative to its visible graph degree: $$d(v) = \operatorname{round}!\left(30 + 64 \cdot \frac{\sqrt{\deg(v)}}{\sqrt{\max_{u} \deg(u)}}\right)$$
  4. Graph-Grounded AI Agent: Implemented SSE streaming (src/services/geminiGraphAgent.ts) with 429/503 automatic model fallback, structured bracketed citations + GRAPH_REFS: footer parsing, post-answer RelateDB edge hydration, and pruning of local keyword-prefetch guesses when an LLM answers.

Challenges we ran into

  • Avoiding a 956-node hairball: Standard circular/force layouts turned 528 BNS–IPC links into overlapping chords. We built computeOrbitalPositions() in CircularGraphCanvas.tsx to group BNS sections clockwise by chapter, position paired IPC sections radially outward at their BNS centroid angle, place Exceptions/Concepts/Definitions on inner rings, auto-frame the selected neighborhood, and switch labels by zoom level (zoom >= 0.32).
  • Model overload & inconsistent section citations: Cloud inference endpoints intermittently returned 503 high-demand errors, and models occasionally cited provisions in plain prose (103 BNS, 302 of the IPC, grouped section lists) instead of bracketed tags. We added model fallback cascades, a universal prose + GRAPH_REFS: parser with live Cypher hydration, and a direct RelateDB summary fallback if cloud models fail.
  • Preventing local prefetch noise in graph previews: Naive keyword pre-retrieval before an LLM call initially caused un-cited keyword candidates to linger in the graph preview. Separating keyword_prefetch from model_tool discovery phases let us prune locally guessed nodes whenever the LLM answers cleanly.

What we learned

  • Explicit graph structure prevents statutory ambiguity: Encoding legal mappings (ANALOGOUS_TO_IPC), caveats (EXCEPTION_TO), and sentencing links (PUNISHED_BY) in RelateDB resolves number collisions and cross-references before generation begins.
  • Browser WASM makes knowledge graphs instantly shareable: Packaging the entire database in one .rl file via @relate/relate-wasm lets anyone open the live app, inspect Cypher results, and export their updated graph with zero backend setup.

Acknowledgments

Thank you to Neo4j and the Relate team for organizing the "Context, Connected" Relate by Neo4j Hackathon and building @relate/relate-wasm, which made running a full legal Cypher graph inside the browser practical and lightweight.

Built With

Share this project:

Updates

Submission history