Agent Engineering / Context & Memory
Use a code index to gather context
- MUST locate code through an index (symbol lookup, references, call graph, semantic search) before opening files, when an index is available.
- 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.
- 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.
- MUST re-index, or confirm the index is current, after a branch switch, pull, rebase or large edit.
- 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.
- 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.
- SHOULD keep the index local and out of version control, and exclude secrets, vendored code, build output and generated files from it.
- SHOULD record in the plan or handoff which queries found the relevant code, so a reviewer or the next agent can repeat them.
- MAY have an orchestrator share one index across sub-agents (for example, a single MCP server) instead of each agent re-reading the repository.
When to apply
Section titled “When to apply”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.
Procedure
Section titled “Procedure”- Check the index. Confirm one exists and is current for this branch; re-index if not.
- Find the entry point. Use semantic search with the task’s words, or symbol lookup if you know the name.
- Expand the context deliberately. Query callers, callees and implementations of that symbol. Stop when you can explain how the change propagates.
- Read the spans. Open only the line ranges the queries returned.
- Verify. Confirm the code at those lines matches what the index reported before you edit.
- Fall back for non-symbols. Use ripgrep for strings, config keys and comments.
Examples
Section titled “Examples”| 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" |
Tool options
Section titled “Tool options”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:
- One repo, local, minimal footprint: codebase-memory-mcp (single binary) or Codanna (adds semantic search). Recommended default.
- Agents that refactor and edit at symbol level: Serena, on top of language servers you already run.
- Many repos on one machine: Gortex.
- Organisation-wide or very large estates: Sourcegraph MCP.
- 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).
Anti-patterns
Section titled “Anti-patterns”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
.envfiles and secrets.
Checklist
Section titled “Checklist”- 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