Plan: Real GraphRAG Retrieval (Lexical + Citation Expansion)¶
- Spec: spec.md (028-real-graphrag-retrieval)
- Approach: build the tool as a standalone, fully unit-testable object first (fake Neo4j session — no DB needed), then wire it as the workflow default. Read-only against the existing schema, so there is no migration and no data risk.
Steps¶
- Create the
tools/package AGENTS.md documents but which never existed:agents_runtime/tools/{__init__,graph_rag}.py. - Cypher, schema-accurate — verified against the real node shapes
before writing:
:Casehasid/title/court/decided_at/source/ source_url/confidence(nojurisdictionproperty → filter by lowercasecourtprefix, the conventionlist_under_cited_jurisdictionsalready uses);:StatuteSectioncarriesjurisdiction_code. Every statementLIMIT-bounded, parameter-bound, andarchived_at IS NULL. GraphRAGRetriever:_lexical_cases→case_text_fulltext, withescape_luceneon the query and a substring-scan fallback on any index error._lexical_statutes→statute_section_text_fulltext(skipped, not fatal, when unavailable).- Expansion →
EXPAND_CITES_CYPHERover the lexical seed ids. _run_loggedwraps everysession.runwithprovenance.log_graph_query.- Dedup by node id;
last_coveragepublished per run. - Top-level
except→[]so a graph fault degrades to the refusal path rather than raising into the API. RetrievalCoverage.note()composes the reviewer-facing caveat: empty-corpus wording, result mix, jurisdictions represented, and an explicit absent-jurisdiction warning.- Wire into
LegalWorkflow:_build_default_retriever()replaces the[]stub; the retriever is built in__init__(no connection opened) so_retrievecan readlast_coverageand return it — returning is what merges into LangGraph state; mutating the input state would not.LegalWorkflowStategainsretrieval_coverage;RetrievalStatsgains the four new fields;invokemerges them with a sensible default note. - Attribution in the prompt:
_analyzerenderstitle — court (date)per passage and instructs the model to name the issuing authority and to refuse rather than answer from general knowledge. - Typing:
__call__takesMapping[str, Any](a supertype of theLegalWorkflowStateTypedDict) so it satisfies theRetrievercallable type under parameter contravariance.
Test strategy¶
tests/agents/tools/test_graph_rag.py with a FakeSession routing Cypher by distinctive substring (the two fulltext statements share the db.index.fulltext.queryNodes prefix, so routing keys on the RETURN projection instead): lexical+expansion, index-missing fallback, absent jurisdiction, total failure, no-queries-no-DB-calls, provenance logging, and escape_lucene behavior. Existing workflow tests must stay green unchanged (they inject their own retriever, so the default swap is invisible to them).
Gates: make test-backend (full suite, DBs up), ruff/black, mypy --strict on the touched modules, make lint-specs.
Rollback¶
Revert the commit; the workflow falls back to the stub. No migrations, no schema changes, no data writes (the retriever is read-only).