src — context

Module: src-context Cohesion: 0.80 Members: 0

src — context

The src/context module is a foundational component responsible for providing the AI agent with a rich, structured, and up-to-date understanding of its operational environment, particularly the codebase it's working within. It encompasses mechanisms for loading initial configuration, mapping project structure, and performing advanced semantic search (Retrieval-Augmented Generation, RAG) over code.

This module aims to ensure the agent has access to relevant information at the right time, minimizing token usage while maximizing contextual accuracy.

Core Concepts

The src/context module is built around several key concepts:

  1. Bootstrap Context: Initial, static instructions and configurations loaded at session start.
  2. Codebase Mapping: A high-level, structural overview of the project's files, languages, and key symbols.
  3. Retrieval-Augmented Generation (RAG) for Code: A system for semantically searching and retrieving granular code snippets based on natural language queries. This involves:

Key Components

1. BootstrapLoader (src/context/bootstrap-loader.ts)

The BootstrapLoader is responsible for injecting initial context files into the agent's session. These files typically contain high-level instructions, agent identity, tool definitions, or project-specific guidelines.

2. CodebaseMapper (src/context/codebase-map.ts)

The CodebaseMapper provides a high-level, structural overview of the entire codebase. It's useful for understanding the project's layout, languages, and key components without needing deep semantic analysis of every line of code.

3. Codebase RAG System (src/context/codebase-rag/)

This sub-module is the heart of the agent's deep code understanding capabilities, enabling semantic search and retrieval-augmented generation.

3.1. Code Chunker (src/context/codebase-rag/chunker.ts)

The CodeChunker intelligently splits source code files into smaller, semantically meaningful units. This is crucial for RAG, as embeddings work best on focused pieces of information.

3.2. Embedding Providers (src/context/codebase-rag/embeddings.ts, src/context/codebase-rag/ollama-embeddings.ts)

Embedding providers convert text into numerical vectors (embeddings) that capture semantic meaning. These vectors are then used for similarity search.

3.3. Vector Store (src/context/codebase-rag/hnsw-store.ts)

The vector store is responsible for efficiently storing and querying the numerical embeddings.

3.4. CodebaseRAG (src/context/codebase-rag/codebase-rag.ts)

The CodebaseRAG class orchestrates the entire RAG process, from indexing a codebase to retrieving relevant information for a query.

How it Works: Execution Flow

The src/context module provides a layered approach to context management:

graph TD
    subgraph Agent Session
        A[AgentExecutor / PromptBuilder] --> B(BootstrapLoader.load)
        B --> C{Initial Context Files}
        C --> D[CodebaseMapper.buildMap]
        D --> E{High-Level Codebase Map}
        E --> F[CodebaseRAG.indexCodebase]
    end

    subgraph Codebase RAG Indexing
        F --> G[Find Files]
        G --> H{For Each File}
        H --> I[CodeChunker.chunkFile]
        I --> J{Code Chunks}
        J --> K[EmbeddingProvider.embedBatch]
        K --> L{Vector Embeddings}
        L --> M[VectorStore.addBatch]
        M --> N[Codebase RAG Index]
    end

    subgraph Codebase RAG Retrieval
        O[Agent Query] --> P[CodebaseRAG.retrieve]
        P --> Q{Query Embedding}
        Q --> R[VectorStore.search]
        R --> S{Candidate Chunks}
        S -- (Optional) --> T[CrossEncoderReranker.rerank]
        T --> U{Reranked Chunks}
        U --> V[Relevant Code Context]
    end

    style A fill:#f9f,stroke:#333,stroke-width:2px
    style O fill:#f9f,stroke:#333,stroke-width:2px
    style V fill:#f9f,stroke:#333,stroke-width:2px
    style N fill:#ccf,stroke:#333,stroke-width:2px

  1. Initial Context Loading: When an agent session starts, components like PromptBuilder or AgentExecutor call BootstrapLoader.load(cwd). This gathers static instruction files (e.g., BOOTSTRAP.md, AGENTS.md) and hierarchical context files, concatenating them into an initial system prompt or context block.

  1. Codebase Overview: The CodebaseMapper.buildMap() is invoked to create a high-level CodebaseMap. This provides a quick summary of the project's structure, languages, and file types, which can be used for general awareness or to inform other context strategies.

  1. Deep Code Understanding (RAG Indexing):

  1. Deep Code Understanding (RAG Retrieval):

Integration Points

The src/context module is deeply integrated throughout the agent's architecture: