← Blog

Architecture · Case Study  ยท  August 26, 2026

Why Grep Fails for AI Coding Agents: Building a Local AST Engine in Go

If you've used Claude Code, Cursor, Windsurf, or Google Antigravity on a real production repository, you've likely seen this pattern: the agent burns 6–10 turns searching for code, dumps thousands of irrelevant lines into the conversation, and occasionally breaks an upstream caller it never saw.

The Brute-Force Grep Bottleneck

When an AI coding agent explores a repository, its default fallback is raw grep or ripgrep. Grep is excellent for a human scanning a terminal — it is expensive for an LLM:

  • Noisy results. A search for a function like GenerateToken returns every mock, test fixture, and comment that mentions it — hundreds of lines the model has to read just to find the real declaration.
  • Compound re-sending. LLM API calls are stateless. Whatever gets pulled into the transcript on turn 2 is re-sent, token for token, on every subsequent turn. A single noisy grep early in a session gets paid for repeatedly.
  • Attention dilution. Dumping a 2,000-line file to find one method buries the relevant 40 lines inside 1,960 lines of noise, which is exactly the condition that produces hallucinated signatures and missed call sites.
Traditional grep flow                              codive flow
├─ grep -rn "GenerateToken" .                       ├─ find_symbol(query="GenerateToken")
│   380 lines of mocks/tests (noisy)                │   exact file:line + signature
├─ open auth_service.go in full                     ├─ get_file_skeleton(path=...)
│   2,100-line dump                                 │   ~120-token structural outline
├─ guess the edit, 3 callers break on import        ├─ blast_radius(symbol="GenerateToken")
│   retry loop, hallucinated signature               │   exact caller list + tests to run
└─ many turns, large re-sent context                └─ 1–2 targeted turns

The Local AST Solution

Instead of treating source as unstructured text, codive parses each file's Abstract Syntax Tree with a per-language extractor (Go's own go/parser, dedicated extractors for TypeScript/JavaScript, Python, Java/C#, and Rust). Every function, struct, interface, method, and annotation becomes a row in an embedded SQLite database at .codive/index.db, running in WAL (Write-Ahead Logging) mode so reads and writes don't block each other.

Your Repository
  └─ AST parser (per language) + file-content indexer
        └─ .codive/index.db  (SQLite, WAL mode)
              ├─ files    — path, language, size, mtime
              ├─ symbols  — kind, name, signature, line
              ├─ files_fts — FTS5 full-text index
              └─ decisions — recorded architectural memory
                    ├─ MCP server (JSON-RPC over stdio)
                    └─ CLI (codive search / symbol / blast / ...)

Because the index lives on disk as a plain SQLite file, every query — find_symbol, search_code, find_callers — is a B-tree or FTS5 lookup instead of a filesystem regex sweep, which is why typical latency stays under a few milliseconds even on large repositories.

What This Actually Changes for an Agent

The highest-leverage tool is pack_feature_context: instead of five sequential turns finding route handlers, models, and tests for a feature, one call bundles the relevant symbol matches, file skeletons, and test suites for a topic keyword. Paired with get_file_skeleton — which collapses function bodies to { /* L45-L92 */ } line markers while preserving every signature — an agent can understand a 1,200-line file's entire public surface for far less than a full read costs.

Why this matters beyond cost: it isn't only about token spend. A smaller, structured context is also a more accurate one — the model sees exact line numbers and signatures instead of reconstructing them from a wall of text, which is what actually reduces broken-caller regressions on refactors.

Where This Fits

codive doesn't replace your editor's search or a human doing a deliberate grep -R — it replaces the AI agent's default fallback to raw text search with a structured index built for exactly the kind of question an autonomous coding agent asks over and over: where is this defined, who calls it, what will break if I change it, what does this file's API surface look like. Those are exactly the four questions find_symbol, find_callers/find_references, blast_radius, and get_file_skeleton answer directly.