Git Diff Impact Mapping and Risk Classification in Codebase Memory MCP

The git diff impact mapping system transforms modified files from a Git working tree into a ranked list of impacted symbols, using breadth-first search traversal and hop-distance metrics to classify propagation risk as CRITICAL, HIGH, MEDIUM, or LOW.

Codebase Memory MCP, maintained at DeusData/codebase-memory-mcp, provides an intelligent mechanism that converts raw Git diffs into actionable impact reports. By analyzing the call graph and symbol dependencies, the system quantifies how changes propagate through the codebase, enabling developers to assess the blast radius of modifications before they reach production.

How Git Diff Impact Mapping Works

The impact mapping process bridges version control changes with semantic code analysis through a multi-stage pipeline.

Change Detection and Symbol Extraction

The process begins when the detect_changes command scans the Git index and working tree to identify modified files. For each changed file, the system extracts every symbol—functions, types, constants, and definitions—that has been added, removed, or altered.

Once changed symbols are identified, the system executes a breadth-first search (BFS) starting from each modified symbol. This traversal follows the stored call-graph and definition graph to discover dependent symbols. During traversal, the engine calculates the hop distance for each visited node, representing how many edges separate a symbol from the original change.

According to the implementation in src/mcp/mcp.c (lines 2775–2777), the BFS collects fully-qualified symbol names and their corresponding hop distances, storing this data in the JSON field impacted_symbols when the risk_labels flag is enabled.

Risk Classification System

The system translates topological distance into actionable risk levels using a deterministic mapping function.

Hop Distance to Risk Level Mapping

The helper function cbm_hop_to_risk in src/store/store.c (line 2948) converts hop counts into four distinct risk categories:

  • 1 hop → CRITICAL (CBM_RISK_CRITICAL): Direct dependencies on changed code
  • 2 hops → HIGH (CBM_RISK_HIGH): Immediate neighbors of direct dependencies
  • 3 hops → MEDIUM (CBM_RISK_MEDIUM): Secondary propagation level
  • ≥4 hops or ≤0 → LOW (CBM_RISK_LOW): Deep propagation or calculation errors

Risk Label Generation

After classification, the function cbm_risk_label in src/store/store.c (line 2961) converts the internal risk enum into human-readable strings ("CRITICAL", "HIGH", "MEDIUM", "LOW"). These labels attach to each symbol entry in the final JSON output.

Implementation Details

The git diff impact mapping spans three core components:

  • src/mcp/mcp.c (lines 2775–2777): Contains the detect_changes implementation that orchestrates the BFS traversal and JSON construction
  • src/store/store.c (lines 2948, 2961): Implements cbm_hop_to_risk for distance-to-risk conversion and cbm_risk_label for string formatting
  • src/cli/cli.c (line 460): Exposes the --risk_labels flag in the command-line interface

Usage Examples

Command-Line Interface

Enable risk classification using the CLI flag described in src/cli/cli.c:

cbm detect_changes --risk_labels=true

C Integration

Programmatically invoke the impact mapping from C code:

#include <stdbool.h>

/* Initialize store context */
struct cbm_store *store = cbm_store_open("path/to/index");

/* Run detection with risk labeling enabled */
bool risk_labels = true;
detect_changes(store, risk_labels);

JSON Output Format

The system produces structured JSON containing the impacted_symbols array, as referenced in README.md (line 149):

{
  "impacted_symbols": [
    {
      "symbol": "my_project::utils::parse_input",
      "hop": 1,
      "risk": "CRITICAL"
    },
    {
      "symbol": "my_project::core::process",
      "hop": 3,
      "risk": "MEDIUM"
    },
    {
      "symbol": "my_project::api::handler",
      "hop": 4,
      "risk": "LOW"
    }
  ]
}

Summary

  • Git diff impact mapping analyzes modified files to identify semantically affected symbols through graph traversal
  • Breadth-first search calculates hop distances from changed symbols to their dependencies
  • Risk classification maps hop distances to four levels: CRITICAL (1 hop), HIGH (2 hops), MEDIUM (3 hops), and LOW (≥4 hops)
  • Implementation resides primarily in src/mcp/mcp.c for traversal logic and src/store/store.c for risk calculation
  • CLI integration supports --risk_labels=true to include risk ratings in JSON output

Frequently Asked Questions

How does the system determine which symbols are impacted by a Git diff?

The detect_changes command traverses the Git working tree to find modified files, then extracts changed symbols and executes a breadth-first search through the stored call-graph. Each visited symbol is recorded with its hop distance from the original change.

What is the relationship between hop distance and risk classification?

Hop distance directly determines risk level through the cbm_hop_to_risk function. Symbols 1 hop away classify as CRITICAL, 2 hops as HIGH, 3 hops as MEDIUM, and 4 or more hops as LOW, as implemented in src/store/store.c.

Where is the risk classification logic implemented?

The risk mapping logic lives in src/store/store.c at line 2948 (cbm_hop_to_risk), while the traversal engine resides in src/mcp/mcp.c at lines 2775–2777. The CLI interface exposing this functionality is defined in src/cli/cli.c at line 460.

How can I enable risk labels in the output?

Pass --risk_labels=true when invoking the detect_changes command, or set the equivalent boolean parameter to true when calling the C API function. This triggers the inclusion of risk classifications in the JSON output's impacted_symbols array.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →