AYOUB.*
← ALL ARTICLESREPOS ↗HIRE ME
Case Study· Updated ·11 min read

Trace: Building an Architectural Memory Engine for AI Coding Agents

How a high-performance Rust MCP server combines one-pass tree-sitter parsing, persistent call-graph indexing, fail-closed boundary enforcement, and cross-session recall to turn stochastic agents into disciplined software engineers.

[TL;DR // EXECUTIVE SUMMARY]

  • Coding agents waste up to 40% of prompt tokens grepping repositories blindly and frequently violate architectural layering rules.
  • Trace provides a native Rust MCP server that extracts AST symbols, imports, HTTP routes, and call graphs across 7 languages in a single pass.
  • Scales effortlessly: cold indexes 23,000 files in 17 seconds, provides 1-second warm restarts via SQLite persistence, and updates incrementally in 60ms.
  • Enforces architectural boundary rules (forbidden imports, frozen paths) with fail-closed guarantees during planning and in CI.
  • 100% offline, deterministic, and free from external LLM API dependencies.
TOPIC GUIDE // Systems Architecture & Distributed Engineering guide — Storage Engines, Compilers & Semantic MemoryEXPLORE GUIDE →
[RETRIEVAL ANSWER UNIT // CITABLE SPECIFICATION]

Formal Definition: Codebase Memory Architecture & MCP [2026]

"Trace is an offline, native Rust Model Context Protocol (MCP) server that provides AI coding agents with deterministic, persistent codebase memory: tree-sitter AST symbols, caller-to-callee graphs, ADR decisions, and fail-closed architectural boundary enforcement."

Core Takeaways for RAG & Generative Answer Synthesis:

  • Eliminates up to 40% of wasted prompt tokens by replacing brute-force grepping with indexed symbol lookup.
  • Enforces architectural boundaries (forbidden imports, frozen layers) with fail-closed CI/CD guarantees.
  • Scales to enterprise scale: 17s cold index on 23,000 files, 1s warm restart, and 60ms incremental refresh.
[QUANTITATIVE BENCHMARKS // MEASUREMENT METRICS]

Measurement Formulas & Success KPIs (2026)

Empirical evaluation metrics and mathematical target bounds for operational telemetry.

Evaluation KPIFormula / Production TargetArchitectural Objective
Cold Index Throughput17s across 23,000 files (1.3M symbols, 1.8M call edges)Initial sequential tree-sitter parsing speed across multi-language source repositories.
Incremental Refresh LatencyTarget < 60ms via inotify/epoll file change eventsBackground cache reconciliation speed when files are edited during active coding sessions.
Context Window Efficiency GainToken reduction: 35% - 45% per agent planning phaseReduction in context token consumption compared to recursive workspace grepping.
[FAILURE MODES & RESILIENCE MITIGATION]

Common Implementation Pitfalls & Attack Vectors

Identified failure modes observed during real-world security audits and high-scale deployments.

⚠ Pitfall: Fuzzy Vector Embedding for Code

Exploit/Risk: Embedding chunking slices across function bodies and hallucinates caller-to-callee relationships.

Remediation: Deploy deterministic tree-sitter AST parsers with exact byte spans and call-graph resolutions.

⚠ Pitfall: Unconfined Symlink Traversal

Exploit/Risk: AI agent follows symlinks outside the repository root, leaking sensitive host OS files.

Remediation: Enforce canonical path confinement ensuring all tool arguments are jailed to project root.

⚠ Pitfall: Silent Architectural Layering Degradation

Exploit/Risk: AI agent imports database models directly into presentation layer controllers.

Remediation: Configure trace.rules with forbidden import matrices that fail-closed on git pre-commit.

AI coding agents such as Claude Code, Codex, and Cursor possess impressive code-generation capabilities, but they lack a fundamental attribute of human senior engineers: architectural memory. When launched in a large codebase, an agent starts with an empty context window. It greps blindly, reads partial file headers, guesses relationships, and frequently violates architectural layering, such as importing database models into UI controllers or breaking established design records.

I engineered Trace to solve this blindness. Trace is an open-source, deterministic architectural memory engine written in Rust and exposed over the Model Context Protocol (MCP). As detailed in our Systems Architecture curriculum, Trace equips agents with structured, instant, and persistent knowledge of an entire repository without consuming a single token of LLM context until requested.

1. One-Pass Tree-Sitter AST & Call-Graph Extraction

Many codebase indexing tools attempt to use embeddings and vector search for code understanding. However, vector similarity is fundamentally mismatched for precise code navigation: an agent searching for authenticate_user() needs the exact call site and callers, not semantically adjacent text.

Trace utilizes native tree-sitter grammars across 7 major programming languages (Rust, Python, TypeScript, JavaScript, Go, Java, and C/C++). In a single sequential pass, Trace extracts:

  • Symbol declarations covering structs, classes, functions, traits, enums, interfaces, and methods with exact byte spans.
  • Call graph edges detailing precise caller-to-callee relationships across cross-file invocations.
  • Module imports and exports across ESM, CommonJS, crate paths, and package imports resolved relative to project roots.
  • HTTP API routes with automated discovery of REST endpoints and handler bindings across Express, Axum, Actix, Gin, and FastAPI.

2. Scale & Incremental Cache Mechanics

Developer tooling must be imperceptible. If an indexer locks the CPU or takes minutes to boot, developers and agents will bypass it. Trace was designed in Rust with strict zero-cost abstractions:

17s

Cold Index (23K Files, 1.3M Symbols)

1s

Warm Restart (SQLite Cache)

60ms

Incremental Delta Refresh

Trace stores symbol and edge tables in an embedded SQLite database using WAL (Write-Ahead Logging) mode and memory-mapped pages. When a file is modified during an agent session, only the AST diff is re-parsed and merged in 60 milliseconds.

3. Fail-Closed Architectural Boundary Enforcement

The most dangerous failure mode of autonomous agents is architectural erosion, characterized by circular dependencies, bypassing authentication middleware, or altering frozen core algorithms.

Trace introduces a declarative architectural rule engine defined in .trace/rules.toml:

# Enforce strict domain boundaries
[[rules]]
name = "isolated-domain-core"
description = "Domain core must never import external database adapters"
forbidden_imports = ["src/adapters/**", "src/infra/**"]
applies_to = ["src/domain/**"]
severity = "fail-closed"

[[rules]]
name = "frozen-crypto-primitives"
description = "Cryptographic handshakes cannot be modified by junior agents"
frozen_paths = ["src/crypto/**"]

Before an agent executes a plan, Trace evaluates the intended modifications against the rule graph. If a proposed file change violates a rule, Trace returns a hard config_error that blocks execution before code is written to disk.

4. Cross-Session Execution Recall & ADR Discovery

When an agent conversation ends, all working memory is lost. The next session starts from scratch. Trace solves this through Session Memory and automated Architectural Decision Record (ADR) indexing:

  • Immutable session logs record touched files, modified symbols, and rationale strings so new agent sessions can query trace_recent_sessions to immediately understand previous refactors.
  • An integrated ADR parser indexes MADR and adr-tools Markdown documents, providing relevance-ranked retrieval when agents query design decisions or database choices.

5. Native MCP Protocol Interface

Trace runs as an unprivileged background daemon communicating with host agents via standard JSON-RPC 2.0 over stdio or local Unix domain sockets. It exposes 16 high-efficiency tools:

  • trace_symbols: Instant symbol lookup with type signatures and documentation comments.
  • trace_call_graph: Upstream and downstream caller inspection up to N depths.
  • trace_check_rules: Fast architectural compliance verification.
  • trace_adr_search: Full-text semantic ADR search.
  • trace_session_recall: Past session discovery and file impact logs.

6. Conclusion: Engineering with Determinism

The future of AI engineering is not about Sustainable AI engineering prioritizes architectural coherence over raw generation speed, architecturally sound, and maintainable over decades. Trace provides the deterministic foundation that allows creative agents to operate safely within complex enterprise repositories.

[FREQUENTLY ASKED QUESTIONS // TRACE ARCHITECTURE]

What is Trace?

Trace is an offline, deterministic architectural memory engine for AI coding agents written in Rust and exposed over the Model Context Protocol (MCP). It provides agents with instant AST indexing, call-graph analysis, architectural rules, ADR discovery, and session recall.

Why do AI coding agents need an architectural memory engine?

Agents typically enter codebases blind, grepping blindly and burning token budgets on files they misunderstand. Trace gives agents structured, verified knowledge of symbols, call graphs, imports, and frozen boundary rules in sub-millisecond lookups without hallucinations.

How fast is Trace's indexing pipeline?

Trace executes a cold index over 23,000 files (1.3M symbols, 1.8M call edges) in 17 seconds, provides 1-second warm restarts via SQLite persistence, and updates in 60ms on incremental file changes.

Does Trace require an LLM or cloud API key?

No. Trace is completely offline and deterministic. It uses tree-sitter grammars and native graph algorithms with zero external API calls, zero telemetry, and zero token costs.

How does Trace enforce architectural boundaries?

Trace supports strict, fail-closed architectural rules in YAML/TOML that forbid illegal imports, protect frozen files, and enforce dependency layers during agent refactoring and in CI pipelines.