Impact Graph¶
The Impact Graph tracks relationships between specifications, code files, and tests.
Import¶
ImpactGraph¶
Constructor¶
Methods¶
add_node¶
Add a node to the graph.
Example¶
graph = ImpactGraph()
# Add spec node
graph.add_node(
id="auth-req-001",
node_type=NodeType.SPEC,
metadata={"path": "auth/requirements.md"}
)
# Add code node
graph.add_node(
id="src/auth/service.py",
node_type=NodeType.CODE,
)
# Add test node
graph.add_node(
id="tests/test_auth.py",
node_type=NodeType.TEST,
)
add_edge¶
Add an edge between nodes.
def add_edge(
from_id: str,
to_id: str,
edge_type: EdgeType,
metadata: dict | None = None,
) -> Edge
Example¶
# Spec implemented by code
graph.add_edge(
from_id="auth-req-001",
to_id="src/auth/service.py",
edge_type=EdgeType.IMPLEMENTED_BY,
)
# Code tested by test
graph.add_edge(
from_id="src/auth/service.py",
to_id="tests/test_auth.py",
edge_type=EdgeType.TESTED_BY,
)
get_impacted¶
Get nodes impacted by changes.
def get_impacted(
node_ids: list[str],
depth: int = 2,
direction: Direction = Direction.BOTH,
) -> list[Node]
Parameters¶
| Parameter | Type | Description | Default |
|---|---|---|---|
node_ids |
list[str] |
Starting nodes | required |
depth |
int |
Traversal depth | 2 |
direction |
Direction |
Traversal direction | BOTH |
Example¶
# Get specs impacted by code change
impacted = graph.get_impacted(
node_ids=["src/auth/service.py"],
depth=2,
)
for node in impacted:
print(f"{node.node_type}: {node.id}")
get_tests_for_files¶
Get tests that cover given files.
Example¶
tests = graph.get_tests_for_files(["src/auth/service.py"])
# ["tests/test_auth.py::test_login", "tests/test_auth.py::test_logout"]
get_specs_for_files¶
Get specs related to given files.
Example¶
get_neighbors¶
Get neighboring nodes.
def get_neighbors(
node_id: str,
edge_type: EdgeType | None = None,
direction: Direction = Direction.BOTH,
) -> list[Node]
Example¶
# Get all neighbors
neighbors = graph.get_neighbors("auth-req-001")
# Get only implemented_by neighbors
code_files = graph.get_neighbors(
"auth-req-001",
edge_type=EdgeType.IMPLEMENTED_BY,
direction=Direction.OUTGOING,
)
to_dict¶
Export graph as dictionary.
Example¶
to_mermaid¶
Export graph as Mermaid diagram.
Example¶
Output:
graph TD
auth-req-001[auth/requirements.md]
src/auth/service.py[service.py]
auth-req-001 -->|implemented_by| src/auth/service.py
to_dot¶
Export graph as DOT format.
ImpactBuilder¶
Build impact graphs from specifications and code.
from specmem.impact import ImpactBuilder
builder = ImpactBuilder()
# Add specs
for spec in specs:
builder.add_spec(spec)
# Analyze code relationships
builder.analyze_code_directory("src/")
# Analyze test relationships
builder.analyze_test_directory("tests/")
# Build the graph
graph = builder.build()
Methods¶
add_spec¶
analyze_code_directory¶
analyze_test_directory¶
build¶
Types¶
NodeType¶
EdgeType¶
class EdgeType(Enum):
IMPLEMENTS = "implements"
IMPLEMENTED_BY = "implemented_by"
DEPENDS_ON = "depends_on"
TESTED_BY = "tested_by"
COVERS = "covers"
REFINES = "refines"
CONFLICTS_WITH = "conflicts_with"