RAG System & Review Memory
Relevant source files
The following files were used as context for generating this wiki page:
The Retrieval-Augmented Generation (RAG) system is the architectural core designed to enhance AI-driven code reviews with contextual depth beyond simple code diffs. It utilizes a multi-layered knowledge approach to provide the LLM with codebase-specific patterns, historical review decisions, and explicit developer-provided rules.
By integrating pgvector for semantic similarity search, the system identifies relevant code snippets and past review outcomes to generate more accurate, actionable, and consistent feedback. This "Review Memory" acts as a compounding moat, allowing the system to learn from developer interactions—such as accepting or dismissing findings—over time.
Sources: CLAUDE.md:14-18, packages/review/src/orchestrator.ts:125-155
Architecture Overview
The system is structured into four distinct knowledge layers that are retrieved in parallel during the review process. These layers ensure the LLM understands the specific context of the repository it is reviewing.
The Four Knowledge Layers
| Layer | Name | Purpose | Data Source |
|---|---|---|---|
| Layer 1 | Codebase Context | Find similar functions/classes to flag inconsistencies. | Indexed repository code chunks (AST-based). |
| Layer 2 | Review Memory | Reuse past review decisions and developer feedback. | ReviewMemory table & ReviewConversation. |
| Layer 3 | Rules & Standards | Enforce team-specific documentation and linting rules. | README.md, CONTRIBUTING.md, config files. |
| Layer 4 | Dependency Graph | Understand the "blast radius" of changes. | AST import graph (Callers/Callees). |
Sources: CLAUDE.md:25-78, packages/review/src/orchestrator.ts:134-152
Context Retrieval Flow
When a pull request is reviewed, the orchestrator embeds the diff patch and queries the vector database for relevant context across multiple layers.
Review Memory (Layer 2)
Review Memory is the mechanism by which the system persists AI review comments and tracks developer responses to build a long-term knowledge base.
Storage and Categorization
After a review is completed, a background job (StoreReviewMemory) embeds each generated comment. Comments are heuristically categorized into types such as SECURITY, PERFORMANCE, or STYLE to aid future retrieval.
function inferCategory(body: string): string {
const lower = body.toLowerCase();
if (/sql inject|xss|csrf|auth|token|secret|password|encrypt|sanitiz/.test(lower)) return 'SECURITY';
if (/perform|slow|n\+1|index|cache|latency|timeout|memory leak/.test(lower)) return 'PERFORMANCE';
if (/naming|style|format|lint|convention|indent|whitespace/.test(lower)) return 'STYLE';
return 'CORRECTNESS';
}
Sources: packages/review/src/memory-store.ts:14-22, packages/review/src/memory-store.ts:41-55
Learning from Feedback
The system processes developer comments on AI findings to update the memory's outcome. Intents are classified into ACCEPTED, DISMISSED, or QUESTION.
- ACCEPTED: Increases the weight of this pattern in future retrievals.
- DISMISSED: Prevents the system from flagging the same pattern again unless the severity is
CRITICAL. - QUESTION: Triggers an
explainCommentlogic where the AI defends or clarifies its finding.
Sources: packages/review/src/comment-processor.ts:55-85, packages/review/src/prompts.ts:18-20
Data Models
The system relies on PostgreSQL with the pgvector extension to store and query high-dimensional embeddings.
Core RAG Entities
| Entity | Description | Vector Table |
|---|---|---|
Chunk | Portions of the codebase (functions/classes) parsed via AST. | ChunkEmbedding |
ReviewMemory | Past AI review comments and their outcomes (Accepted/Dismissed). | ReviewMemoryEmbedding |
RepoKnowledge | Explicit facts extracted from developer comments (e.g., "we use soft deletes here"). | RepoKnowledgeEmbedding |
ReviewConversation | Records of developer-AI Q&A interactions regarding specific lines. | N/A |
Sources: packages/db/prisma/schema.prisma:455-525, packages/db/prisma/schema.prisma:544-590
Semantic Search Logic
Retrieval uses cosine distance (<=>) to find the nearest neighbors in the vector space. For ReviewMemory, results are additionally sorted to prioritize findings that were previously ACCEPTED by the team.
SELECT
rm."commentBody",
rme.embedding <=> ${vectorStr}::vector AS distance
FROM "ReviewMemory" rm
JOIN "ReviewMemoryEmbedding" rme ON rme."reviewMemoryId" = rm.id
WHERE rm."repositoryId" = ${repositoryId}
ORDER BY
distance ASC,
CASE rm.outcome WHEN 'ACCEPTED' THEN 0 WHEN 'PENDING' THEN 1 ELSE 2 END ASC
LIMIT ${topK}
Sources: packages/review/src/memory-search.ts:31-55
Prompt Construction
The retrieved context is injected into the LLM system prompt using structured templates. This transforms the AI from a generic reviewer into one that "knows" the specific repository's history and standards.
Context Injection Components
- Similar Patterns: Code snippets from the existing codebase (Layer 1).
- Past Decisions: Previous reviews with labels like
✅ accepted by teamor❌ dismissed by team(Layer 2). - Codebase Knowledge: Learned facts like "bookings use soft deletes" (Layer 2).
Sources: packages/review/src/prompts.ts:35-70, packages/review/src/prompts.ts:85-110
Summary
The RAG System and Review Memory enable the AI Code Reviewer to provide context-aware feedback by leveraging a repository's specific history and structure. By storing and retrieving data across four layers—Codebase, Memory, Rules, and Dependencies—the system reduces false positives (dismissed findings) and increases consistency with existing codebase patterns.