# Cross-Project Impact Graph

- **Command:** `lore impact-graph [--format json|dot|mermaid]`
- **Confidence:** 75%
- **Tier:** 3
- **Status:** proposed
- **Effort:** medium — aggregation over entity_references, graph output formatting

## What

Aggregate `entity_references` by project pair to produce a weighted adjacency matrix
showing how projects reference each other. Output as JSON, DOT, or Mermaid for
visualization.

## Why

Makes invisible architectural coupling visible. "Backend and frontend repos have
47 cross-references this quarter" tells you about tight coupling that may need
architectural attention.

## Data Required

All exists today:
- `entity_references` (source/target entity IDs)
- `issues` / `merge_requests` (project_id for source/target)
- `projects` (path_with_namespace)

## Implementation Sketch

```sql
-- Project-to-project reference counts
WITH ref_projects AS (
    SELECT
        CASE er.source_entity_type
            WHEN 'issue' THEN i_src.project_id
            WHEN 'merge_request' THEN mr_src.project_id
        END as source_project_id,
        CASE er.target_entity_type
            WHEN 'issue' THEN i_tgt.project_id
            WHEN 'merge_request' THEN mr_tgt.project_id
        END as target_project_id,
        er.reference_type
    FROM entity_references er
    LEFT JOIN issues i_src ON er.source_entity_type = 'issue' AND er.source_entity_id = i_src.id
    LEFT JOIN merge_requests mr_src ON er.source_entity_type = 'merge_request' AND er.source_entity_id = mr_src.id
    LEFT JOIN issues i_tgt ON er.target_entity_type = 'issue' AND er.target_entity_id = i_tgt.id
    LEFT JOIN merge_requests mr_tgt ON er.target_entity_type = 'merge_request' AND er.target_entity_id = mr_tgt.id
    WHERE er.target_entity_id IS NOT NULL  -- resolved references only
)
SELECT
    p_src.path_with_namespace as source_project,
    p_tgt.path_with_namespace as target_project,
    er.reference_type,
    COUNT(*) as weight
FROM ref_projects rp
JOIN projects p_src ON rp.source_project_id = p_src.id
JOIN projects p_tgt ON rp.target_project_id = p_tgt.id
WHERE rp.source_project_id != rp.target_project_id  -- cross-project only
GROUP BY p_src.path_with_namespace, p_tgt.path_with_namespace, er.reference_type
ORDER BY weight DESC;
```

## Output Formats

### Mermaid
```mermaid
graph LR
    Backend -->|closes 23| Frontend
    Backend -->|mentioned 47| Infrastructure
    Frontend -->|mentioned 12| Backend
```

### DOT
```dot
digraph impact {
    "group/backend" -> "group/frontend" [label="closes: 23"];
    "group/backend" -> "group/infra" [label="mentioned: 47"];
}
```

## Downsides

- Requires multiple projects synced; limited value for single-project users
- "Mentioned" references are noisy (high volume, low signal)
- Doesn't capture coupling through shared libraries or APIs (code-level coupling)

## Extensions

- `lore impact-graph --since 90d` — time-scoped coupling analysis
- `lore impact-graph --type closes` — only meaningful reference types
- Include unresolved references to show dependencies on un-synced projects
- Coupling trend: is cross-project coupling increasing over time?
