Repo-Level Dependency Graph Embeddings: Multi-Hop Code Navigation for Autonomous Agents

Repo-Level Dependency Graph Embeddings: Multi-Hop Code Navigation for Autonomous Agents

Standard vector search over flat code chunks fails when autonomous agents must navigate multi-file architectures. An edit to a base interface ripples through abstract factories, dependency injection containers, and remote RPC handlers located across dozens of modules. Repo-level dependency graph embeddings combine syntactic token semantics with call-graph and inheritance topology, enabling agents to execute multi-hop code navigation in sub-5ms latency.

1. Why Flat Text Embeddings Fail on Complex Multi-File Code Refactoring

Text embedding models (such as text-embedding-3 or standard bi-encoders) treat code as linear natural language text. When querying "How is user authentication validated?", standard cosine similarity retrieves the controller endpoint and perhaps the user entity model.

However, it fails completely to retrieve the distant middleware filter, the JWT token verification utility, or the database permission schema that the controller secretly depends upon. Because flat embeddings lack topological awareness of call graphs and inheritance hierarchies, the agent operates with partial context, leading to broken interfaces and runtime dependency injection failures.

Topological Blind Spot

Code semantics are defined by structural call hierarchies and dataflow dependencies, not surface lexical keyword proximity. Graph embeddings bridge the semantic gap between definition and execution.

2. Extracting Call and Inheritance Graphs via Language Server Protocol (LSP) and Tree-Sitter

High-precision code graphs are constructed by coupling Language Server Protocol (LSP) indexing with Tree-Sitter concrete syntax analysis. The indexer traverses the repository, emitting explicit nodes for every `Function`, `Class`, `Interface`, and `TypeAlias`.

Directed edges capture semantic relationships: `CALLS`, `INHERITS_FROM`, `IMPLEMENTS`, `IMPORTS`, and `INSTANTIATES`. This transforms a chaotic monorepo into an explicit, queryable property graph where every symbol has an unambiguous fully-qualified identifier.

Because modern polyglot monorepos mix TypeScript frontend services with Go backend APIs and Python data pipelines, tree-sitter grammars extract language-agnostic intermediate representations (IR). Cross-boundary foreign function calls and gRPC protocol buffer definitions are linked into unified graph edges, providing true end-to-end architectural visibility.

interface CodeNode {
  id: string; // e.g. "auth/service.ts:AuthService.verifyToken"
  kind: 'function' | 'class' | 'interface';
  file: string;
  byteRange: [number, number];
}

interface DependencyGraph {
  nodes: Map;
  edges: Array<{ source: string; target: string; relationship: 'CALLS' | 'IMPLEMENTS' | 'IMPORTS' }>;
}

export function extractMultiHopDependencies(
  graph: DependencyGraph,
  rootSymbol: string,
  maxHops: number = 2
): CodeNode[] {
  const visited = new Set();
  const queue: Array<{ id: string; depth: number }> = [{ id: rootSymbol, depth: 0 }];

  while (queue.length > 0) {
    const { id, depth } = queue.shift()!;
    if (visited.has(id) || depth > maxHops) continue;
    visited.add(id);

    const outgoing = graph.edges.filter(e => e.source === id);
    for (const edge of outgoing) {
      if (!visited.has(edge.target)) {
        queue.push({ id: edge.target, depth: depth + 1 });
      }
    }
  }

  return Array.from(visited).map(id => graph.nodes.get(id)!).filter(Boolean);
}

3. Graph Neural Network Embeddings: Fusing Lexical Tokens with Topological Graph Structure

To allow semantic vector search over graph topologies, Graph Convolutional Networks (GCNs) or Graph Attention Networks (GATs) fuse token representations with structural connectivity. A node's final embedding vector $\mathbf{z}_v$ aggregates both its internal code semantics and the representations of its neighboring caller and callee nodes: $\mathbf{h}_v^{(l+1)} = \sigma\left( W \cdot \text{AGGREGATE}(\{\mathbf{h}_u^{(l)} : u \in \mathcal{N}(v) \cup \{v\}\}) \right)$

This produces embedding vectors where two functions that perform similar architectural roles (e.g., repository data mappers in different domains) cluster together in vector space even if they share zero common vocabulary.

By applying multi-head graph attention mechanisms, the model dynamically assigns higher attention weights to critical architectural edges (such as abstract interface implementations) while down-weighting noisy boilerplate imports. This ensures that the learned embeddings encode high-order architectural design patterns.

4. Multi-Hop Graph Traversal: Answering 'Who Calls Function X Under Condition Y' in Sub-5ms

During agent problem-solving, graph embeddings enable lightning-fast multi-hop navigation. When an agent considers modifying a shared database migration, a single graph query traverses incoming `CALLS` and `IMPORTS` edges to retrieve all downstream dependent services in under 5ms.

This structured context is injected directly into the agent's prompt, presenting the exact blast radius of the proposed change and allowing the agent to update all dependent call sites in a single pass.

In contrast to exhaustive graph queries that flood context windows with thousands of irrelevant leaf nodes, graph neural ranking scores each candidate path by semantic task relevance. Only paths that exhibit high mutual information with the target issue description are surfaced to the LLM context.

5. Impact on SWE-bench: Success Rate Multipliers Across 500+ File Monorepo Challenges

Evaluating autonomous agents on large-scale repository benchmarks (such as SWE-bench Verified) demonstrates the necessity of graph-augmented retrieval. On repositories with more than 500 files, standard dense retrieval achieves a resolution rate of only 21.8%, with agents frequently failing to locate the root-cause file.

Integrating repo-level dependency graph embeddings increases the file localization accuracy from 44.2% to 92.6%, driving end-to-end bug resolution rates past 52.4% on complex enterprise codebases.

Furthermore, graph embeddings drastically reduce context window pollution: instead of injecting hundreds of irrelevant lines from adjacent utility files, agents receive precise subgraphs containing only the critical caller-callee chains required to verify and apply the bug patch.

Code Indexing Architecture Localization Accuracy (Top-5 Files) Multi-Hop Dependency Coverage Query Latency SWE-bench Success Rate
Flat BM25 Keyword Search 34.2% 12.0% (Direct matches only) 18 ms 18.4%
Dense Vector Chunks (bi-encoder) 44.2% 24.5% (Lexical overlap) 45 ms 21.8%
LSP Static Graph Only 78.4% 88.0% (Deterministic hops) 2 ms 38.2%
Hybrid Graph Neural Embeddings 92.6% 96.5% (Full topological recall) 4 ms 52.4%

Navigating modern monorepos requires understanding architectural connectivity rather than keyword proximity. Dependency graph embeddings bridge the gap between static language servers and semantic neural retrieval.

Empowering coding agents with topological multi-hop graph exploration delivers the contextual completeness required to resolve complex, enterprise-grade software engineering challenges.