Critique/docs
Architecture

Memory & Context System

How Critique builds, stores, and retrieves deep repository knowledge across chat and automated PR reviews.

Critique is fundamentally repository-aware, and its understanding of a codebase is built on a multi-layered memory and context architecture. Rather than relying on simple character-count text chunking, the system isolates short-term contextual "state" from long-term "learned" repository memory.

1. Durable Context

Durable Context refers to semi-static, structured snapshots of the user's environment and runtime state. It grounds the AI before it executes actions.

When the system needs to understand the "now," it pulls a structured ContextDocument bundle containing:

  • Workspace Status: GitHub connection state, active installations, automation readiness.
  • Repository Summary: Visibility, default branch, recent PR statuses, and repository metadata.
  • Product Runtime: Information about the current review run or chat session.

This context is passed directly to the model as immediate, concrete background information, rather than being searched for.

2. Repository Memory

Unlike Durable Context, Repository Memory is the persistent, long-term storage layer designed to retain learnings, decisions, and evidence across isolated runs.

  • Data Models: Stored as AgentBoardEntry (structured board items visible to the user) and AgentMemoryChunk (text/context blocks).
  • Namespaces: Memory is tightly scoped to REPOSITORY:<id>, REVIEW_RUN:<id>, or CHAT_SESSION:<id>.
  • Content Types: Memories are categorized by their recordType (e.g., DECISION, VALIDATION_RESULT, SUMMARY, EVIDENCE, FACT). This categorization allows the AI to distinctly recall past architectural decisions or PR feedback rather than generic file contents.

3. Intelligent Chunking & Embeddings

Codebase indexing and memory indexing rely on an intelligent chunking pipeline backed by pgvector.

  • Code-Aware Splitting: Instead of blindly splitting files every 2,600 characters, Critique's chunker uses regex patterns (CODE_BOUNDARY_PATTERNS) to shift boundaries, ensuring chunks align with actual code structures like functions, classes, and exports.
  • Metadata Inference: The chunker extracts symbol hints (exports, types) and detects the "chunk role" (e.g., test, config, route, docs) based on the file path.
  • Embeddings: OpenRouter is used to generate 2560-dimensional semantic embeddings (defaulting to models like perplexity/pplx-embed-v1-4b). These are stored in PostgreSQL using the pgvector extension for hyper-fast vector similarity queries.

4. Hybrid Retrieval

When searchSharedMemory is invoked, Critique executes a hybrid retrieval process to query shared memory:

  1. Scope Resolution: Gathers active namespaces (the current chat, PR, repo, plus recent historical repos/runs).
  2. Lexical Search: Tokenizes the query, removing stop words, and searches the database for keyword matches in board entries and memory chunks.
  3. Semantic Search: Generates an embedding of the user's query and runs a cosine similarity search (<=>) against the pgvector indexed memory chunks.
  4. Ranking Algorithm: Results are fused and ranked using a composite score that combines:
    • Scope priority (current chat/repo > recent repos)
    • Record type weight (Decisions [5] > Validation/Summaries [4] > Evidence/Facts [3])
    • Freshness (time-decay modifier)
    • Lexical keyword occurrences & semantic similarity scores.

5. Learning Promotion

Promotion is the mechanism that transitions ephemeral findings from isolated automated runs into permanent REPOSITORY knowledge.

  • Review Runs: When a review finishes, the promoteReviewRunLearning function extracts the PR verdict, review summary, and top findings. It generates a rationale and writes it to the REPOSITORY namespace.
  • Remedy Runs: Upon a successful automated remediation, promoteSuccessfulRemedyOutcome extracts the final assistant output and attempt summaries.
  • Execution: During promotion, Critique creates both an AgentBoardEntry (as a DECISION and SUMMARY) and an AgentMemoryChunk to sync the textual learnings. This effectively upgrades run-specific outcomes into long-term repository knowledge, directly surfacing in future semantic searches.