Dynamic Context Slicing
Relevant source files
The following files were used as context for generating this wiki page:
Dynamic Context Slicing is a core architectural feature of Arbor designed to extract token-bounded context from a Code Property Graph (CPG) for consumption by Large Language Models (LLMs). It enables the system to provide semantically relevant code snippets and structural metadata while strictly adhering to the token limits of various AI models.
The system performs bidirectional traversal around a target node to gather its "semantic neighborhood," including callers, callees, and related structural entities. By utilizing precise token counting (via tiktoken), the engine ensures that the most critical information—determined by factors like centrality, hop distance, and manual pinning—is prioritized within the available context window.
Sources: README.md:15-18, CHANGELOG.md:16-20, crates/arbor-graph/src/lib.rs:56
Architecture and Core Components
The slicing logic is implemented within the arbor-graph crate and exposed through the CLI and MCP layers. It transforms a target symbol's graph-native representation into an "Architectural Brief" suitable for AI reasoning.
Data Structures
The primary data structures involved in the slicing process are defined in the slice module.
| Structure | Description |
|---|---|
ContextSlice | The final result of a slicing operation, containing the target node, a collection of context nodes, token counts, and truncation metadata. |
ContextNode | Represents a node within the slice, including its metadata, hop depth from the target, estimated tokens, and pinning status. |
TruncationReason | Enum indicating why a slice ended: Complete (full depth reached), TokenLimit (out of tokens), or DepthLimit (max hops reached). |
Sources: crates/arbor-graph/src/lib.rs:56, crates/arbor-cli/src/commands.rs:693-705, CHANGELOG.md:17-18
The Slicing Workflow
The following diagram illustrates the flow from a user query or AI request to the generation of a context-aware architectural brief.
The slicing engine uses graph traversal to find the semantic neighborhood and applies token-based budget constraints at each step.
Sources: crates/arbor-cli/src/commands.rs:682-705, crates/arbor-mcp/src/lib.rs:271-331
Implementation Details
Context Retrieval and LLM Optimization
The engine replaces heuristic estimates with accurate token counting using tiktoken (specifically the cl100k_base encoding) to ensure LLM prompts fit exactly within their target budget.
Key functionalities include:
- Pinning Support: Developers or agents can "pin" critical nodes to ensure they are never truncated, regardless of the remaining token budget.
- Summary Generation: The
ContextSlicecan generate a summary string (Architectural Brief) that includes properties like type, file, impact level (centrality), and signatures for the target and its dependencies. - Bidirectional Search: The system looks both upstream (callers) and downstream (callees) to provide a complete picture of a symbol's usage.
Sources: README.md:15, CHANGELOG.md:16-20, crates/arbor-cli/src/commands.rs:710-730, crates/arbor-mcp/src/lib.rs:290-310
Integration Points
Dynamic Context Slicing is utilized across multiple Arbor interfaces:
CLI: arbor explain
The explain command invokes the slicing engine to retrieve context for a specific target.
--tokens N: Manually controls the context budget.--why: Traces the path taken through the graph to include specific context nodes.
Sources: crates/arbor-cli/src/commands.rs:669-682, CHANGELOG.md:30-33
MCP Tool: get_context
In the Model Context Protocol implementation, the get_context tool (also referred to as get_logic_path in some implementations) uses the slicing logic to provide AI agents with a structured overview of a symbol's semantic neighborhood.
The sequence demonstrates how an AI agent requests context, triggering a graph traversal and subsequent slicing before returning a formatted brief.
Sources: crates/arbor-mcp/src/lib.rs:175-185, crates/arbor-mcp/src/lib.rs:271-290
Architectural Brief Schema
When a slice is serialized (typically via MCP or arbor explain --json), it provides detailed metadata about the context nodes.
| Field | Description |
|---|---|
target | Metadata (name, kind, file) for the central symbol being analyzed. |
context_nodes | An array of nodes included in the slice, including their depth from target and token_estimate. |
total_tokens | The actual token count used by the slice. |
truncation_reason | Why the slicing stopped (e.g., TokenLimit). |
query_time_ms | Performance metric for the slicing operation. |
Sources: crates/arbor-cli/src/commands.rs:693-705, crates/arbor-mcp/src/lib.rs:188-210
Summary
Dynamic Context Slicing transforms raw graph data into prioritized, token-optimized context for AI agents. By combining graph centrality, bidirectional dependency traversal, and precise token counting, it ensures that LLMs receive the most relevant architectural information without exceeding context window constraints. This system is critical for the "Logic Forest" approach, providing deterministic and explainable code context rather than relying on probabilistic search methods.
Sources: README.md:65-75, CHANGELOG.md:16-20