Skip to content
Agent Engineering / Context & Memory

Use a code index to gather context

  • BP-0010
  • v1.0.1
  • draft
  • guideline
  • role: implementer
  • role: reviewer
  • role: orchestrator
  • stage: plan
  • stage: build
  • stage: verify
  • updated 2026-11-24
  • reviewed 2026-11-24
  • next review 2027-05-24
  1. MUST locate code through an index (symbol lookup, references, call graph, semantic search) before opening files, when an index is available.
  2. MUST read only the spans the task needs: the symbol, its callers and callees, and their types. Do not read whole files or directories to “get a feel” for the code.
  3. MUST verify an index result against the current source before you edit or quote it. An index can be stale; the file on disk is the truth.
  4. MUST re-index, or confirm the index is current, after a branch switch, pull, rebase or large edit.
  5. SHOULD use reference and call-graph queries for impact analysis (“who calls X”, “what breaks if I change Y”) instead of text search, and list the affected symbols in the plan.
  6. SHOULD fall back to text search (ripgrep) when there is no index, or when the target is not a symbol: a string literal, config key, log message, comment or file in a language the index does not parse.
  7. SHOULD keep the index local and out of version control, and exclude secrets, vendored code, build output and generated files from it.
  8. SHOULD record in the plan or handoff which queries found the relevant code, so a reviewer or the next agent can repeat them.
  9. MAY have an orchestrator share one index across sub-agents (for example, a single MCP server) instead of each agent re-reading the repository.

Any task in an existing codebase: bug fixes, refactors, reviews, impact analysis, onboarding to an unfamiliar repo. The benefit grows with repository size. In a repo of a few files, reading them directly is fine.

  1. Check the index. Confirm one exists and is current for this branch; re-index if not.
  2. Find the entry point. Use semantic search with the task’s words, or symbol lookup if you know the name.
  3. Expand the context deliberately. Query callers, callees and implementations of that symbol. Stop when you can explain how the change propagates.
  4. Read the spans. Open only the line ranges the queries returned.
  5. Verify. Confirm the code at those lines matches what the index reported before you edit.
  6. Fall back for non-symbols. Use ripgrep for strings, config keys and comments.
Question Index query (example: Codanna CLI) Fallback
Where is parse_config defined? codanna mcp find_symbol name:"parse_config" rg -n "fn parse_config"
Who calls it? codanna mcp find_callers parse_config rg -n "parse_config\(" (misses indirect calls)
What breaks if I change it? codanna mcp analyze_impact symbol_name:"parse_config" Manual trace of references
Where do we validate tokens? codanna mcp semantic_search_with_context query:"validate auth token" rg -n -i "token" (noisy)
Where is MAX_RETRIES set in config? Not a symbol; skip the index rg -n "MAX_RETRIES"

The rules do not depend on the tool. These are options as of 2026-11-24; check each project’s current release, licence and telemetry settings before adopting it.

Tool Type Strengths Trade-offs
Codanna Local MCP server + one-shot CLI (Rust, tree-sitter) Symbol search, semantic search, call graphs, impact analysis; code stays on the machine Check language coverage for your stack
Gortex Local code-intelligence engine: graph, MCP server, CLI, web UI Multi-repository by default; large tool set (call chains, blast radius, dataflow, refactoring); telemetry off by default Large tool surface; enable only the tools agents need
codebase-memory-mcp Local MCP server, tree-sitter knowledge graph in SQLite Single static binary, no external services, persistent graph that survives session restarts; no built-in LLM Structural queries only (no embeddings-based semantic search)
Serena MCP toolkit over language servers (LSP) or a JetBrains plugin IDE-grade symbol lookup, references and symbol-level editing and refactoring across many languages Needs a working language server per language; runs on Python via uv
Sourcegraph MCP Hosted / self-hosted service Cross-repository search, navigation, history and Deep Search; OAuth 2.0 or access tokens Enterprise plan; code is indexed on a server, not the agent’s machine
Language server directly (LSP) Editor protocol Exact definitions and references; often already installed No semantic search or cross-language graph
universal-ctags Tag file Tiny, universal, offline Definitions only; no references or call graph
ripgrep / Zoekt Text search (Zoekt: indexed trigram search) Always works; strings, config keys, comments No semantics; misses indirect calls

Choosing:

  1. One repo, local, minimal footprint: codebase-memory-mcp (single binary) or Codanna (adds semantic search). Recommended default.
  2. Agents that refactor and edit at symbol level: Serena, on top of language servers you already run.
  3. Many repos on one machine: Gortex.
  4. Organisation-wide or very large estates: Sourcegraph MCP.
  5. Air-gapped, OT or restricted environments: a local, single-binary option with telemetry off, pinned to a reviewed release; treat it as third-party code under supply-chain controls (D.SEC.SUP).
  • cat-ing whole directories into context “to understand the codebase”.
  • Editing from an index snippet without opening the current file.
  • Trusting an index built on another branch.
  • Using text search for “who calls X” and missing calls through aliases, traits or interfaces.
  • Committing the index, or indexing .env files and secrets.
  • Index exists and is current for this branch
  • Context gathered by queries, not bulk reads
  • Index results checked against current source before editing
  • Impact (callers and implementations) listed in the plan
  • Queries recorded for the reviewer