# Who Knows About X?

- **Command:** `lore experts <path-or-topic>`
- **Confidence:** 92%
- **Tier:** 1
- **Status:** proposed
- **Effort:** medium — two query paths (file-based, topic-based)

## What

Given a file path, find people who have authored MRs touching that file, left
DiffNotes on that file, or discussed issues referencing that file. Given a topic
string, use search to find relevant entities then extract the active participants.

## Why

"Who should I ask about the auth module?" is one of the most common questions in
large teams. This answers it empirically from actual contribution and review data.
No guessing, no out-of-date wiki pages.

## Data Required

All exists today:
- `mr_file_changes` (new_path, merge_request_id) — who changed the file
- `notes` (position_new_path, author_username) — who reviewed the file
- `merge_requests` (author_username) — MR authorship
- `documents` + search pipeline — for topic-based queries
- `discussions` + `notes` — for participant extraction

## Implementation Sketch

### Path Mode: `lore experts src/auth/`

```
1. Query mr_file_changes WHERE new_path LIKE 'src/auth/%'
2. Join merge_requests to get author_username for each MR
3. Query notes WHERE position_new_path LIKE 'src/auth/%'
4. Collect all usernames with activity counts
5. Rank by: MR authorship (weight 3) + DiffNote authorship (weight 2) + discussion participation (weight 1)
6. Apply recency decay (recent activity weighted higher)
```

### Topic Mode: `lore experts "authentication timeout"`

```
1. Run existing hybrid search for the topic
2. Collect top N document results
3. For each document, extract author_username
4. For each document's entity, query discussions and collect note authors
5. Rank by frequency and recency
```

### SQL (Path Mode)

```sql
-- Authors who changed files matching pattern
SELECT mr.author_username, COUNT(*) as changes, MAX(mr.merged_at) as last_active
FROM mr_file_changes mfc
JOIN merge_requests mr ON mfc.merge_request_id = mr.id
WHERE mfc.new_path LIKE ?1
  AND mr.state = 'merged'
GROUP BY mr.author_username
ORDER BY changes DESC;

-- Reviewers who commented on files matching pattern
SELECT n.author_username, COUNT(*) as reviews, MAX(n.created_at) as last_active
FROM notes n
WHERE n.position_new_path LIKE ?1
  AND n.note_type = 'DiffNote'
  AND n.is_system = 0
GROUP BY n.author_username
ORDER BY reviews DESC;
```

## Human Output Format

```
Experts for: src/auth/

  alice     12 changes, 8 reviews   (last active 3d ago)   [top contributor]
  bob        3 changes, 15 reviews  (last active 1d ago)   [top reviewer]
  charlie    5 changes, 2 reviews   (last active 2w ago)
  dave       1 change,  0 reviews   (last active 3mo ago)  [stale]
```

## Robot Mode Output

```json
{
  "ok": true,
  "data": {
    "query": "src/auth/",
    "query_type": "path",
    "experts": [
      {
        "username": "alice",
        "changes": 12,
        "reviews": 8,
        "discussions": 3,
        "score": 62,
        "last_active": "2025-01-25T10:00:00Z",
        "role": "top_contributor"
      }
    ]
  }
}
```

## Downsides

- Historical data may be stale (people leave teams, change roles)
- Path mode requires `mr_file_changes` to be populated (Gate 4 ingestion)
- Topic mode quality depends on search quality
- Doesn't account for org chart / actual ownership

## Extensions

- `lore experts --since 90d` — recency filter
- `lore experts --min-activity 3` — noise filter
- Combine with `lore silos` to highlight when an expert is the ONLY expert
