feat: GraphRAG retrieval + context/citation engine (P13d-1)#144
Merged
Conversation
The pure-Dart retrieval core for the local "Ask your library" GraphRAG
chat (P13d): reuse the P10 semantic substrate (embed -> vector search)
plus a light graph re-rank to gather the most relevant library items for
a question, then assemble a bounded, cited context block + a
history-aware generation prompt the chat (d-2) will drive.
New lib/features/ai/data/:
- rag_context.dart (pure): RagSource/RagChatTurn/RagContext,
kRagSystemPrompt, buildSourceSnippet (aiSummary>description, capped),
selectRagSources (dedupe/cap), fitHistory (recent-turn char budget --
the tier-aware history-depth knob), buildRagPrompt (numbered cited
sources + optional bounded history).
- rag_availability.dart (pure): RagAvailability {unavailable,
retrievalOnly, full} + ragAvailability(...) -- the d-3 fallback gate.
- rag_retriever.dart: RagRetriever + provider -- embed query ->
vectorSearch -> relatedTo re-rank -> hydrate via MetadataRepository ->
cited RagContext; empty-sources when retrieval isn't ready.
Tests: prompt/snippet/fitHistory/selectRagSources, the ragAvailability
truth table, and the retriever with fake embedder + graph + seeded
in-memory metadata. No deps, no schema, no UI (it's d-2's engine).
Docs: P13-PLAN.md P13d card rewritten to the multi-turn decomposition
(d-1/d-2a/d-2b/d-3) + d-1 [~]; VERIFICATION.md P13d-1 note; BACKLOG.md
retrieval-only persistence, per-tier history budget, RAM co-residency.
https://claude.ai/code/session_013JoYmLCosYt5tQ8qwdbL1T
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
P13d-1 — Local GraphRAG "Ask your library" · retrieval engine
The first slice of the flagship P13d ("Ask your library"): the pure-Dart retrieval + context/citation engine the multi-turn chat (d-2) will drive. No UI, no schema, no deps — fully CI-verifiable.
What it does
Reuses the P10 semantic substrate (
embedderEngine.embed→GraphQueryService.vectorSearch) plus a lightrelatedTograph re-rank to gather the most relevant library items for a question, then assembles a bounded, cited context block + a history-aware generation prompt.New —
lib/features/ai/data/rag_context.dart(pure) —RagSource/RagChatTurn/RagContext,kRagSystemPrompt(answer ONLY from numbered sources, cite[n], say "I don't know", never invent),buildSourceSnippet(prefersaiSummaryover description, capped),selectRagSources(dedupe/cap),fitHistory(recent-turn char budget — the tier-aware history-depth knob),buildRagPrompt(numbered cited sources + optional bounded history).rag_availability.dart(pure) —RagAvailability {unavailable, retrievalOnly, full}+ragAvailability(...), the d-3 fallback gate.rag_retriever.dart—RagRetriever+ provider: embed query →vectorSearch→relatedTore-rank → hydrate viaMetadataRepository→ citedRagContext; returns empty-sources when retrieval isn't ready (no embedder / empty index) so the caller can fall back gracefully.Decomposition (revised — multi-turn chat)
The P13d card is rewritten to the maintainer-approved multi-turn target: persistent conversations on capable tiers (list / continue / rename / archive / delete), each turn re-retrieving fresh sources + a bounded history window whose depth scales with the device tier; retrieval-only fallback on low / ineligible tiers; entry from the Dashboard.
Tests
rag_context_test.dart(prompt numbering / question / history fold + budget truncation,fitHistory,selectRagSources,buildSourceSnippet),rag_availability_test.dart(truth table),rag_retriever_test.dart(fake embedder + graph + seeded in-memory metadata → ordered cited sources + grounded prompt; empty/unready → no sources).Docs
docs/design/P13-PLAN.md(P13d card → multi-turn decomposition; d-1[~]),docs/VERIFICATION.md(P13d-1 is the CI-covered engine; end-to-end ask flow verified at d-2),docs/BACKLOG.md(retrieval-only-answer persistence, per-tier history budget, RAM co-residency — carried to d-3).Verification
dart format·flutter analyze·flutter test— all green (839 tests). No APK check: d-1 ships no user-facing UI (it's d-2's engine).https://claude.ai/code/session_013JoYmLCosYt5tQ8qwdbL1T
Generated by Claude Code