Skip to content

Finish WorldModelRAG: Neo4j backend, 10k-doc demo, hybrid retrieval benchmark - #823

Merged
AmitoVrito merged 6 commits into
mainfrom
Abhay-Mmmm/v2.0-world-model-rag-live-causal-knowledge-graph
Jul 21, 2026
Merged

AmitoVrito merged 6 commits into
mainfrom
Abhay-Mmmm/v2.0-world-model-rag-live-causal-knowledge-graph

Conversation

@Abhay-Mmmm

Copy link
Copy Markdown
Collaborator

Summary

Closes the three remaining acceptance criteria on #735 (the core WorldModelRAG facade, extractors, resolver, hybrid retriever, bitemporal queries, causal linker, and in_memory/kuzu backends were already built and tested):

  • Neo4j (+ Memgraph) backend — Neo4jWorldGraphBackend(InMemoryWorldGraphBackend) mirrors the existing KuzuWorldGraphBackend write-through pattern (writes persisted to Neo4j, reads served from the in-memory mirror). Wired "neo4j"/"memgraph" into _make_graph_backend (both speak Bolt, same driver), added a .neo4j() classmethod, and exported it from the package __init__s. neo4j>=5.0 was already declared under the graph extra — no new dependency.
  • 10k-document demo — examples/world_model_corpus.py deterministically generates causal two-hop chains (Person → Product → Incident) split across separate documents, extracted via the existing zero-cost HeuristicWorldModelExtractor. examples/world_model_rag.py now ingests 10k real documents (runs in ~1s) with real HashingEmbeddings, and uses a real LLM when OPENAI_API_KEY/ANTHROPIC_API_KEY is set.
  • Hybrid-vs-vector-only benchmark — benchmarks/world_model_retrieval_bench.py compares HybridWorldModelRetriever against a true vector-only baseline on the synthetic chain question set: hybrid hit@5 = 99.4% vs vector-only = 0.0% at full 10k-doc / 500-question scale. benchmarks/test_world_model_retrieval_bench.py hard-enforces a ≥20 percentage-point hit-rate/MRR gap in CI via make bench.

Also closed a pre-existing gap: KuzuWorldGraphBackend had zero test coverage despite being fully implemented — added test_kuzu_backend_roundtrip.

Test plan

  • pytest tests/retrieval/test_world_model.py -v — 17 existing + 2 new tests pass, Neo4j/Kuzu roundtrip tests skip cleanly without Docker/testcontainers (same convention as the existing test_property_graph.py Neo4j test)
  • pytest tests/retrieval/ -v — full 224-test retrieval suite passes
  • Verified Neo4j + Kuzu roundtrip tests actually pass when neo4j/kuzu/Docker are available locally
  • python examples/world_model_rag.py — 10k-doc demo runs end-to-end in ~1.2s, correctly reaches causal chain targets via graph traversal
  • python benchmarks/world_model_retrieval_bench.py --n-docs 10000 — reproduces the ≥20% improvement claim at full scale
  • pytest benchmarks/test_world_model_retrieval_bench.py -c benchmarks/pytest.ini — CI-enforced regression check passes
  • ruff check clean on all changed/new files

🤖 Generated with Claude Code

Abhay-Mmmm and others added 2 commits July 18, 2026 17:20
…hybrid benchmark

Closes the three remaining acceptance criteria from issue #735: a real
Neo4j/Memgraph GraphBackend (mirrors KuzuWorldGraphBackend's write-through
pattern), a 10k-document end-to-end demo backed by a deterministic
synthetic causal-chain corpus, and a hybrid-vs-vector-only retrieval
benchmark showing hybrid beats vector-only hit-rate by >=20% (hard-enforced
in CI), reproducing >99% hit@5 vs 0% for vector-only at full scale.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
- Neo4jWorldGraphBackend.add_event no longer double-persists: the
  in-memory parent's add_event already calls the overridden
  upsert_entity/upsert_relation (which persist), so the extra
  _persist_entity/_persist_document calls were redundant network
  round trips.
- graph_backend="neo4j"/"memgraph" now honor NEO4J_URI/NEO4J_USERNAME/
  NEO4J_PASSWORD env vars instead of a hardcoded bolt://localhost:7687
  with default credentials that silently targets the wrong endpoint
  outside local dev.
- Added WorldModelRAG.close() forwarding to the graph backend (e.g.
  releases the Neo4j driver), using the same getattr-callable pattern
  as delete_by_metadata.
- benchmarks/world_model_retrieval_bench.py now runs the hybrid and
  vector-only retrieval calls concurrently via asyncio.gather instead
  of sequential awaits.
- Added a regression test guarding that both "neo4j" and "memgraph"
  route to Neo4jWorldGraphBackend (previously only exercised via
  Neo4jWorldGraphBackend's own testcontainers test, not through
  WorldModelRAG's string-based wiring).
- Updated docs/world-model-rag.md, which still claimed neo4j/memgraph/
  kuzu all "raise clear runtime errors" -- stale since kuzu already
  worked and now neo4j/memgraph do too.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@Abhay-Mmmm
Abhay-Mmmm marked this pull request as draft July 18, 2026 13:34
@Abhay-Mmmm

Copy link
Copy Markdown
Collaborator Author

WIP

Abhay-Mmmm and others added 3 commits July 18, 2026 22:14
…tegies

Only the default "hybrid" (RRF) strategy had test coverage; add a test
asserting graph_first/vector_first actually change result ordering.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
…x dropped resolver

- ci.yml never ran benchmarks/test_world_model_retrieval_bench.py: the
  hard-enforced hybrid-vs-vector-only benchmark test only lived in
  benchmarks.yml, which triggers on tag push/nightly/manual dispatch, not
  pull_request. Add a dedicated CI job so the gate actually blocks PRs.
- _make_graph_backend's "kuzu" branch dropped the caller's entity_resolver
  (pre-existing, sitting right next to the correctly-wired neo4j/memgraph
  branch this PR added).
- Delete Neo4jWorldGraphBackend.add_event: a no-op override identical to
  the inherited behavior via MRO (InMemoryWorldGraphBackend.add_event already
  dispatches through the overridden upsert_entity/upsert_relation).
- Close leaked Neo4j driver handles in
  test_neo4j_and_memgraph_strings_route_to_neo4j_backend.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
…memory mirror

Neo4jWorldGraphBackend.query_subgraph/to_mermaid previously read from the
inherited in-memory dict, so picking Neo4j bought neither durability nor
multi-process reads. Port the bounded-hop traversal pattern from
Neo4jPropertyGraphBackend.traverse() so reads hit Neo4j directly; writes
still go through the in-memory mirror first for seed/name-index lookup.
Kuzu is left as-is per maintainer discussion on issue #735.
@Abhay-Mmmm Abhay-Mmmm linked an issue Jul 19, 2026 that may be closed by this pull request
12 tasks
@Abhay-Mmmm
Abhay-Mmmm marked this pull request as ready for review July 19, 2026 06:35

@AmitoVrito AmitoVrito left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Review — solves #735, with one follow-up filed

Reviewed the diff against the issue's acceptance criteria and the surrounding code.

Verdict: approving. All four acceptance criteria are satisfied:

  • ✅ 10k-doc end-to-end demo — deterministic, runs fully offline (HeuristicWorldModelExtractor + HashingEmbeddings).
  • ✅ Hybrid beats vector-only by ≥20% — CI-enforced via test_world_model_retrieval_bench.py. The synthetic corpus isolates graph-only-reachable tail docs, so it's a structural gap (fair, though best-case — the win is demonstrated with the regex extractor, not the LLM pipeline).
  • ✅ Backends — in_memory + kuzu (now with roundtrip coverage) + new Neo4jWorldGraphBackend. Note the Neo4j testcontainers test skips without Docker, so CI coverage of the live Cypher path is effectively nil.
  • ✅ Time-travel as_of — pre-existing on main, confirmed working for in-memory.

Nice touches: real fakes (no MagicMock), deterministic seeds, a hard CI regression gate, and backfilled Kuzu coverage.

One correctness issue, tracked as a follow-up (not blocking merge): Neo4jWorldGraphBackend.query_subgraph filters min_confidence/as_of only on the edge result, not on the node/document path traversal (WORLD_RELATES*0..hops has no WHERE). In-memory prunes traversal by edge validity (world_model.py:631), so Neo4j will return extra nodes/documents for time-travel and confidence-filtered queries — a behavioral divergence between backends. Filed separately; fix + regression test incoming.

Merging now; the Neo4j parity fix follows in its own PR.

@AmitoVrito
AmitoVrito merged commit 45eb6cb into main Jul 21, 2026
6 checks passed
AmitoVrito added a commit that referenced this pull request Jul 21, 2026
…ce/as_of (#826)

Neo4jWorldGraphBackend.query_subgraph applied min_confidence/as_of only to the
returned edges, so nodes — and their source documents — reachable solely through
low-confidence or time-expired edges leaked into results, diverging from
InMemoryWorldGraphBackend on confidence-filtered and time-travel (as_of) queries.

Prune the node/document traversal with the same per-path validity predicate the
in-memory BFS uses, and record relation-provenance documents against both edge
endpoints (MENTIONS) so document parity holds too. Verified for parity against a
live Neo4j via testcontainers; regression test fails on the pre-fix code.

Also documents the WorldModelRAG Neo4j/Memgraph backend (#823) in the changelog.

Closes #826.

Co-authored-by: Abhay Krishna <[email protected]>
@AmitoVrito

Copy link
Copy Markdown
Collaborator

Thanks @Abhay-Mmmm, merged. Solid work on the Neo4j backend and the benchmark gate. 🙌

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.

v2.0: World-Model RAG — live causal knowledge graph over user data

2 participants