1. The Confidentiality Crisis & The Cloud LLM Trap

The rapid adoption of generative AI in legal practice has collided head-on with legal ethics. Litigators and corporate counsel are routinely enticed by commercial cloud platforms promising instant document summaries, deposition cross-examination, and brief drafting. However, routing sensitive client disclosures, internal work product, and discovery documents through multi-tenant cloud APIs exposes law firms to severe professional liability under California and federal rules:

Commercial cloud vendors attempt to soothe these concerns with business associate agreements (BAAs) and "zero-retention" marketing claims. Yet for high-stakes litigation, enterprise sovereignty means the data never traverses a public network interface in the first place. We built KruschLaw to prove that a modern, highly capable statutory intelligence platform can execute entirely inside a firm's private perimeter on self-hosted hardware.

2. Why Naive RAG Fails in Statutory Law

Most developer tutorials suggest that building a legal search engine is trivial: chunk a document into 500-token blocks, generate dense embeddings with OpenAI or HuggingFace, store them in a vector database, and query a language model with top-$k$ nearest neighbors.

In statutory and municipal law, this naive pipeline fails immediately. It introduces three critical failure modes:

A. The Semantic Neighbor Fallacy

Vector embeddings evaluate semantic similarity, not legal authority. In municipal law, a query about "tenant eviction protections for non-payment of rent during emergency declarations" will produce high cosine similarity matches across general lease termination provisions, commercial eviction rules, and residential noise complaints.

Cosine distance cannot distinguish between an advisory preamble and a mandatory operative mandate; it cannot determine whether an ordinance has been preempted by state statute; and it cannot recognize that a subsection is subject to a narrow jurisdictional exception appearing in an entirely separate title.

B. Arbitrary Token Slicing Destroys Statutory Hierarchy

Statutory codifications are structured hierarchically: Title → Chapter → Article → Section → Subsection → Paragraph. Naive token chunkers slice text at arbitrary boundaries, separating operational clauses from their parent section headers. An extracted snippet reading:

"(c) The civil penalty prescribed in subsection (a) shall not apply if the owner has filed an appeal within fourteen (14) calendar days."

is completely useless—and legally dangerous—if the chunking boundary severed the preceding text identifying which municipal code title, chapter, and underlying violation subsection (c) actually governs.

C. Fabricated Citations & Judicial Sanctions Risk

When language models are asked to draft legal briefs from loosely retrieved context, they exhibit an innate propensity to "smooth over" gaps by synthesizing statutory numbering. A model might generate an authoritative analysis citing Oakland Municipal Code § 8.24.050 for emergency rent relief, when that specific section actually governs solid waste disposal.

Submitting fabricated citations or confabulated judicial quotes to a court violates California Code of Civil Procedure § 128.7 and Federal Rule of Civil Procedure 11, subjecting trial counsel to severe court sanctions, fee-shifting penalties, and disciplinary referral.

3. System Architecture & Localhost Topology

KruschLaw is engineered as a zero-cloud-egress research platform. Its default networking topology binds strictly to loopback interfaces (127.0.0.1), preventing unauthorized broadcast across local area networks or third-party internet gateways:

┌────────────────────────────────────────────────────────────────────────┐ │ KRUSCHLAW SOVEREIGN RUNTIME ARCHITECTURE │ │ │ │ ┌──────────────────────────────────────────────────────────────┐ │ │ │ KruschLaw Web Interface │ │ │ │ (Streamlit UI / 127.0.0.1:8505) │ │ │ │ *Offline System Font Stack · Zero External CDNs* │ │ │ └──────────────────────────────┬───────────────────────────────┘ │ │ │ Local REST (CORS Restricted) │ │ ┌──────────────────────────────▼───────────────────────────────┐ │ │ │ KruschLaw API Backend │ │ │ │ (FastAPI / 127.0.0.1:8085) │ │ │ │ ├─ Lifespan Schemas & Health Network Diagnostics │ │ │ │ ├─ LOCUS-v1 Parquet Ingestion & Chunk Normalizer │ │ │ │ ├─ Citation & Verbatim Quote Grounding Scanner │ │ │ │ └─ Matter Lifecycle Management (Soft-Delete & Re-embed) │ │ │ └──────────────────────┬───────────────────────────────┬───────┘ │ │ │ │ │ │ SQL / pgvector │ │ Local HTTP │ │ 127.0.0.1:5435 │ │ :11434 │ │ ┌──────────────────────▼───────┐ ┌───────────────▼───────────┐ │ │ │ PostgreSQL 16 + pgvector │ │ Local Ollama Node │ │ │ │ ├─ tsvector Lexical GIN │ │ ├─ bge-large (1024-dim) │ │ │ │ ├─ pgvector HNSW Index │ │ └─ qwen2.5:14b / 7b (LLM) │ │ │ │ └─ Client Matter Catalog │ │ │ │ │ └──────────────────────────────┘ └───────────────────────────┘ │ └────────────────────────────────────────────────────────────────────────┘

The platform consists of four sovereign components:

  1. Streamlit Interface (:8505): Interactive litigation portal designed with an offline system font stack (no Google Fonts network requests), supporting live ordinance search, matter creation, batch ingestion, and Markdown brief export.
  2. FastAPI Control Plane (:8085): High-throughput asynchronous backend managing lifespan database migrations, Ollama batch embeddings, chunk normalization, and the citation grounding scanner.
  3. PostgreSQL 16 Database with pgvector (:5435): Persistent relational store hosting legal vector embeddings, full-text inverted indexes, and matter audit records.
  4. Local Inference Node (Ollama / :11434): Self-hosted runtime executing bge-large for dense semantic embedding and qwen2.5:14b (or 7b) for factual legal synthesis and issue-spotting.

4. Hybrid Retrieval via Reciprocal Rank Fusion (RRF)

To eliminate the "semantic neighbor" failure mode, KruschLaw pairs dense semantic vectors with cover-density full-text lexical ranking. Dense embeddings capture conceptual meaning (e.g., matching "unlawful detainer" to "eviction"), while sparse lexical queries capture exact statutory numbering (e.g., "§ 12.08.020"), named municipal codes, and statutory terms of art.

In PostgreSQL 16, KruschLaw executes this via a Common Table Expression (CTE) combining pgvector HNSW distance with tsvector cover-density ranking (ts_rank_cd):

WITH vec_matches AS (
    SELECT id, 
           (1 - (embedding <=> CAST(:vec AS vector))) AS cos_sim,
           ROW_NUMBER() OVER (ORDER BY embedding <=> CAST(:vec AS vector)) as v_rank
    FROM laws_vectors
    WHERE is_substantive = true AND embedding IS NOT NULL
    LIMIT 50
),
lex_matches AS (
    SELECT id, 
           ts_rank_cd(
               to_tsvector('english', coalesce(title, '') || ' ' || coalesce(section, '') || ' ' || coalesce(content, '')), 
               plainto_tsquery('english', :text_q)
           ) as l_score,
           ROW_NUMBER() OVER (
               ORDER BY ts_rank_cd(
                   to_tsvector('english', coalesce(title, '') || ' ' || coalesce(section, '') || ' ' || coalesce(content, '')), 
                   plainto_tsquery('english', :text_q)
               ) DESC
           ) as l_rank
    FROM laws_vectors
    WHERE is_substantive = true 
      AND to_tsvector('english', coalesce(title, '') || ' ' || coalesce(section, '') || ' ' || coalesce(content, '')) @@ plainto_tsquery('english', :text_q)
    LIMIT 50
)
SELECT l.id, l.jurisdiction, l.state, l.city, l.county, l.city_or_county, 
       l.topic, l.title, l.section, l.content, l.chunk_index,
       COALESCE(1.0 / (60 + v.v_rank), 0.0) + COALESCE(1.0 / (60 + lex.l_rank), 0.0) AS hybrid_score,
       COALESCE(v.cos_sim, 0.0) as similarity
FROM laws_vectors l
LEFT JOIN vec_matches v ON l.id = v.id
LEFT JOIN lex_matches lex ON l.id = lex.id
WHERE v.id IS NOT NULL OR lex.id IS NOT NULL
ORDER BY hybrid_score DESC
LIMIT :limit;

By fusing vector rank and lexical rank via Reciprocal Rank Fusion (with constant $k=60$), documents that score exceptionally well on exact section numbers or statutory phrases surface at the top of the context window even if their abstract embedding similarity is moderate.

5. Section-Aware Chunking & LOCUS-v1 Ingestion

To solve the boundary severance problem, KruschLaw introduces element-aware statutory chunking. When processing municipal codes or external datasets (such as the Hugging Face LocalLaws/LOCUS-v1 corpus), the ingestion engine preserves hierarchical context:

6. The Citation & Verbatim Quote Grounding Scanner

The most distinctive technical feature of KruschLaw is its post-generation Citation & Quote Grounding Scanner (implemented in src/backend/rag.py). Rather than hoping an LLM follows instructions not to hallucinate, KruschLaw parses the generated work product and verifies assertions against the exact retrieved authorities before presenting output to counsel:

def scan_and_flag_citations(generated_text: str, retrieved_laws: List[Dict]) -> Tuple[bool, List[str], str]:
    """
    Extract statutory citations and verbatim quotes from LLM output.
    Verify each against retrieved authorities. If ungrounded sections or
    misattributed quotes are detected, stamp an explicit hallucination advisory.
    """
    # 1. Extract cited section numbers (e.g. "Section 8.24.040", "§ 12.08.010")
    cite_pattern = re.compile(
        r'(?:section|sec\.|§)\s*([0-9]+(?:\.[0-9]+)*(?:-[a-z0-9]+)?)', 
        re.IGNORECASE
    )
    extracted_sections = set(cite_pattern.findall(generated_text))

    # 2. Extract verbatim quotes (20+ characters in quotation marks)
    quote_pattern = re.compile(r'["\u201c]([^"\u201d]{20,})["\u201d]')
    extracted_quotes = quote_pattern.findall(generated_text)

    # 3. Build ground-truth authority catalog
    valid_sections = set()
    authority_corpus = ""
    for law in retrieved_laws:
        sec = law.get("section", "").strip().lower()
        if sec:
            valid_sections.add(sec)
            valid_sections.update(re.findall(r'[0-9]+(?:\.[0-9]+)*(?:-[a-z0-9]+)?', sec))
        authority_corpus += " " + (law.get("content", "") + " " + law.get("title", "")).lower()

    # 4. Identify ungrounded citations and fabricated quotes
    ungrounded_cites = [c for c in extracted_sections if c.lower() not in valid_sections]
    ungrounded_quotes = [q for q in extracted_quotes if q.strip().lower() not in authority_corpus]

    if ungrounded_cites or ungrounded_quotes:
        advisory = (
            "> ⚠️ **Citation & Grounding Advisory (Potential Hallucination Risk)**:\n"
            "> The following citations or quotes were NOT verified against retrieved authorities:\n"
            + "\n".join(f"> - Section `{c}`" for c in ungrounded_cites)
            + "\n".join(f"> - *\"{q}\"*" for q in ungrounded_quotes) + "\n>\n"
            "> KruschLaw mandates that counsel independently verify these provisions prior to reliance."
        )
        return False, ungrounded_cites + ungrounded_quotes, advisory
        
    return True, [], "> 🛡️ **Citation Grounding Verified**: All statutory citations correspond directly to retrieved authorities."

If an open-weight reasoning model invents an ordinance number or paraphrases a quotation with altered wording, KruschLaw immediately stamps the brief with a prominent amber alert banner. Attorneys are not left guessing whether a citation is real—the software automatically highlights unverified assertions for manual inspection.

7. Hardware Profiles & Deployment Topology

KruschLaw is architected to operate across flexible hardware profiles, from single-workstation office deployments to dedicated rackmount servers:

8. Quickstart & Reproducibility

KruschLaw is fully containerized and operational via Docker Compose in four commands:

# 1. Clone the repository
git clone https://github.com/kruschdev/krusch-law.git
cd krusch-law

# 2. Configure environment (default: localhost-only bindings)
cp .env.example .env

# 3. Pull required local models in Ollama
ollama pull bge-large
ollama pull qwen2.5:14b

# 4. Launch the sovereign stack
docker compose up --build -d

Once launched, access the platform locally:

To seed the database with sample municipal ordinances (Oakland, San Francisco, and Los Angeles housing and noise codes), execute:

curl -X POST http://localhost:8085/api/ingest/mock

9. What We Are Building Next

KruschLaw v0.2.0 demonstrates that local open-weight models, coupled with hybrid PostgreSQL retrieval and automated citation scanners, can perform reliable preliminary statutory analysis without exposing client confidences to public clouds.

Our ongoing development focuses on three core extensions:

  1. Hierarchical Conflict Detection: Modeling municipal code preemption rules against California state statutes (e.g., Costa-Hawkins Rental Housing Act vs. local rent control).
  2. KruschNexus Integration: Connecting KruschLaw's statutory reasoning engine directly to KruschNexus's Poppler TSV bounding box extractor for side-by-side evidence verification against scanned discovery exhibits.
  3. Sub-15µs CPU Statutory Dispatch: Integrating the krusch cascade router to evaluate statutory regex patterns on CPU in microseconds before dispatching expensive neural GPU prefill.