symforge 11.18.1

SymForge — in-memory code intelligence MCP server for Claude Code, Codex, and Gemini CLI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
use super::*;
use crate::domain::{SourceVersion, WorkingTreeState};

fn temporal_request(commit: &str) -> GitTemporalComputationRequest {
    GitTemporalComputationRequest {
        fence: GitTemporalPublicationFence {
            project_generation: 7,
            content_generation: 11,
            source: None,
            source_version: Some(SourceVersion {
                branch: Some("main".to_string()),
                commit: Some(commit.to_string()),
                working_tree: WorkingTreeState::Clean,
            }),
        },
        repo_root: PathBuf::from("repo"),
    }
}

#[test]
fn temporal_queue_keeps_one_running_and_coalesces_one_latest_request() {
    let mut queue = GitTemporalJobQueue::default();
    let first = temporal_request("a");
    let superseded = temporal_request("b");
    let latest = temporal_request("c");

    assert_eq!(queue.enqueue(first.clone()), Some(first.clone()));
    assert_eq!(queue.enqueue(first.clone()), None);
    assert_eq!(queue.enqueue(superseded), None);
    assert_eq!(queue.enqueue(latest.clone()), None);
    assert_eq!(queue.finish(&first), Some(latest.clone()));
    assert_eq!(queue.finish(&latest), None);
}

// ── Rendering helpers ───────────────────────────────────────────────

#[test]
fn test_churn_bar_zero() {
    assert_eq!(churn_bar(0.0), "░░░░░░░░░░");
}

#[test]
fn test_churn_bar_half() {
    assert_eq!(churn_bar(0.5), "█████░░░░░");
}

#[test]
fn test_churn_bar_full() {
    assert_eq!(churn_bar(1.0), "██████████");
}

#[test]
fn test_churn_bar_clamps_above_one() {
    assert_eq!(churn_bar(1.5), "██████████");
}

#[test]
fn test_churn_bar_clamps_negative() {
    assert_eq!(churn_bar(-0.2), "░░░░░░░░░░");
}

#[test]
fn test_churn_label_frozen() {
    assert_eq!(churn_label(0.1), "frozen");
}

#[test]
fn test_churn_label_cool() {
    assert_eq!(churn_label(0.3), "cool");
}

#[test]
fn test_churn_label_warm() {
    assert_eq!(churn_label(0.5), "warm");
}

#[test]
fn test_churn_label_hot() {
    assert_eq!(churn_label(0.7), "hot");
}

#[test]
fn test_churn_label_critical() {
    assert_eq!(churn_label(0.9), "critical");
}

#[test]
fn test_relative_time_today() {
    assert_eq!(relative_time(0.3), "today");
}

#[test]
fn test_relative_time_days() {
    assert_eq!(relative_time(3.0), "3d ago");
}

#[test]
fn test_relative_time_weeks() {
    assert_eq!(relative_time(14.0), "2w ago");
}

#[test]
fn test_relative_time_months() {
    assert_eq!(relative_time(60.0), "2mo ago");
}

#[test]
fn test_relative_time_negative() {
    assert_eq!(relative_time(-1.0), "just now");
}

// ── Truncation ──────────────────────────────────────────────────────

#[test]
fn test_truncate_message_short() {
    assert_eq!(truncate_message("hello", 72), "hello");
}

#[test]
fn test_truncate_message_exact_boundary() {
    let msg = "a".repeat(72);
    assert_eq!(truncate_message(&msg, 72), msg);
}

#[test]
fn test_truncate_message_long() {
    let msg = "a".repeat(100);
    let result = truncate_message(&msg, 72);
    assert!(result.ends_with("..."));
    assert_eq!(result.len(), 72);
}

// ── Numstat parsing ─────────────────────────────────────────────────

#[test]
fn test_parse_numstat_line_normal() {
    assert_eq!(
        parse_numstat_line("3\t1\tsrc/foo.rs"),
        Some("src/foo.rs".to_string())
    );
}

#[test]
fn test_parse_numstat_line_binary() {
    assert_eq!(parse_numstat_line("-\t-\timage.png"), None);
}

#[test]
fn test_parse_numstat_line_empty_path() {
    assert_eq!(parse_numstat_line("3\t1\t"), None);
}

#[test]
fn test_parse_numstat_line_not_numstat() {
    assert_eq!(parse_numstat_line("some random line"), None);
}

#[test]
fn test_parse_numstat_line_zero_changes() {
    assert_eq!(
        parse_numstat_line("0\t0\tsrc/lib.rs"),
        Some("src/lib.rs".to_string())
    );
}

// ── Git log parsing ─────────────────────────────────────────────────

fn sample_git_log() -> String {
    // Simulate two commits: one 2 days ago, one 10 days ago.
    let now_unix = 1_741_520_000_u64;
    let ts_2d = now_unix - (2 * 86400);
    let ts_10d = now_unix - (10 * 86400);
    format!(
        "{delim}\n\
             H:abc1234\n\
             U:{ts_2d}\n\
             D:2026-03-07T10:00:00+00:00\n\
             A:Alice\n\
             M:fix: resolve parsing bug\n\
             \n\
             3\t1\tsrc/protocol/tools.rs\n\
             5\t2\tsrc/protocol/format.rs\n\
             {delim}\n\
             H:def5678\n\
             U:{ts_10d}\n\
             D:2026-02-27T10:00:00+00:00\n\
             A:Bob\n\
             M:feat: add search feature\n\
             \n\
             10\t0\tsrc/protocol/tools.rs\n\
             20\t5\tsrc/live_index/query.rs\n",
        delim = COMMIT_DELIMITER,
    )
}

#[test]
fn test_parse_git_log_extracts_commits() {
    let log = sample_git_log();
    let now_unix = 1_741_520_000_u64;
    let commits = parse_git_log(&log, now_unix);
    assert_eq!(commits.len(), 2);
    assert_eq!(commits[0].hash, "abc1234");
    assert_eq!(commits[0].author, "Alice");
    assert_eq!(commits[0].files.len(), 2);
    assert_eq!(commits[1].hash, "def5678");
    assert_eq!(commits[1].author, "Bob");
    assert_eq!(commits[1].files.len(), 2);
}

#[test]
fn test_parse_git_log_computes_days_ago() {
    let log = sample_git_log();
    let now_unix = 1_741_520_000_u64;
    let commits = parse_git_log(&log, now_unix);
    assert!((commits[0].days_ago - 2.0).abs() < 0.01);
    assert!((commits[1].days_ago - 10.0).abs() < 0.01);
}

#[test]
fn test_parse_git_log_empty_input() {
    let commits = parse_git_log("", 1_741_520_000);
    assert!(commits.is_empty());
}

#[test]
fn test_parse_git_log_skips_commit_with_no_files() {
    let log = format!(
        "{delim}\nH:aaa1111\nU:1741520000\nD:2026-03-09\nA:Eve\nM:empty commit\n",
        delim = COMMIT_DELIMITER,
    );
    let commits = parse_git_log(&log, 1_741_520_000);
    assert!(commits.is_empty());
}

// ── Churn score computation ─────────────────────────────────────────

#[test]
fn test_churn_score_recent_beats_old() {
    // Build a minimal log: file_a changed today, file_b changed 60 days ago.
    let now_unix = 1_741_520_000_u64;
    let ts_today = now_unix;
    let ts_old = now_unix - (60 * 86400);
    let log = format!(
        "{delim}\nH:aaa\nU:{ts_today}\nD:2026-03-09\nA:X\nM:today\n\n1\t0\tfile_a.rs\n\
             {delim}\nH:bbb\nU:{ts_old}\nD:2026-01-08\nA:Y\nM:old\n\n1\t0\tfile_b.rs\n",
        delim = COMMIT_DELIMITER,
    );
    let index = GitTemporalIndex::compute_from_log(&log, now_unix);
    let a = index.files.get("file_a.rs").unwrap();
    let b = index.files.get("file_b.rs").unwrap();
    assert!(
        a.churn_score > b.churn_score,
        "recent file ({}) should have higher churn than old file ({})",
        a.churn_score,
        b.churn_score
    );
}

#[test]
fn test_churn_score_multiple_commits_beats_single() {
    let now_unix = 1_741_520_000_u64;
    let ts1 = now_unix - 86400;
    let ts2 = now_unix - (3 * 86400);
    let ts3 = now_unix - (5 * 86400);
    let log = format!(
        "{delim}\nH:a1\nU:{ts1}\nD:d1\nA:X\nM:m1\n\n1\t0\thot.rs\n\
             {delim}\nH:a2\nU:{ts2}\nD:d2\nA:X\nM:m2\n\n1\t0\thot.rs\n\
             {delim}\nH:a3\nU:{ts3}\nD:d3\nA:X\nM:m3\n\n1\t0\thot.rs\n1\t0\tcold.rs\n",
        delim = COMMIT_DELIMITER,
    );
    let index = GitTemporalIndex::compute_from_log(&log, now_unix);
    let hot = index.files.get("hot.rs").unwrap();
    let cold = index.files.get("cold.rs").unwrap();
    assert!(hot.churn_score > cold.churn_score);
}

// ── Co-change / Jaccard ─────────────────────────────────────────────

#[test]
fn test_co_change_jaccard_basic() {
    // Two commits: both touch A+B. One extra commit touches only A.
    // shared=2, union = 3+2-2 = 3, jaccard = 2/3 ≈ 0.667
    let now_unix = 1_741_520_000_u64;
    let ts1 = now_unix - 86400;
    let ts2 = now_unix - 2 * 86400;
    let ts3 = now_unix - 3 * 86400;
    let log = format!(
        "{d}\nH:c1\nU:{ts1}\nD:d\nA:X\nM:m\n\n1\t0\ta.rs\n1\t0\tb.rs\n\
             {d}\nH:c2\nU:{ts2}\nD:d\nA:X\nM:m\n\n1\t0\ta.rs\n1\t0\tb.rs\n\
             {d}\nH:c3\nU:{ts3}\nD:d\nA:X\nM:m\n\n1\t0\ta.rs\n",
        d = COMMIT_DELIMITER,
    );
    let index = GitTemporalIndex::compute_from_log(&log, now_unix);
    let a = index.files.get("a.rs").unwrap();
    assert_eq!(a.co_changes.len(), 1);
    assert_eq!(a.co_changes[0].path, "b.rs");
    assert_eq!(a.co_changes[0].shared_commits, 2);
    // Jaccard: 2 / (3 + 2 - 2) = 2/3
    assert!((a.co_changes[0].coupling_score - 0.6667).abs() < 0.01);
}

#[test]
fn test_co_change_mega_commit_excluded() {
    // One commit touches 60 files — should be excluded from co-change.
    let now_unix = 1_741_520_000_u64;
    let ts = now_unix - 86400;
    let mut files_section = String::new();
    for i in 0..60 {
        files_section.push_str(&format!("1\t0\tfile_{i}.rs\n"));
    }
    // Add a second commit that only touches file_0 and file_1 to have data.
    let ts2 = now_unix - 2 * 86400;
    let log = format!(
        "{d}\nH:mega\nU:{ts}\nD:d\nA:X\nM:mega commit\n\n{files}\
             {d}\nH:small\nU:{ts2}\nD:d\nA:X\nM:small\n\n1\t0\tfile_0.rs\n1\t0\tfile_1.rs\n",
        d = COMMIT_DELIMITER,
        files = files_section,
    );
    let index = GitTemporalIndex::compute_from_log(&log, now_unix);
    // file_0 should only see file_1 as co-change from the small commit,
    // not all 59 other files from the mega commit.
    let f0 = index.files.get("file_0.rs").unwrap();
    // Only 1 shared commit (the small one), which is below MIN_SHARED_COMMITS (2).
    // So no co-changes should appear.
    assert!(f0.co_changes.is_empty());
}

#[test]
fn test_co_change_below_min_shared_excluded() {
    // Two files share only 1 commit — below MIN_SHARED_COMMITS threshold.
    let now_unix = 1_741_520_000_u64;
    let ts = now_unix - 86400;
    let log = format!(
        "{d}\nH:c1\nU:{ts}\nD:d\nA:X\nM:m\n\n1\t0\ta.rs\n1\t0\tb.rs\n",
        d = COMMIT_DELIMITER,
    );
    let index = GitTemporalIndex::compute_from_log(&log, now_unix);
    let a = index.files.get("a.rs").unwrap();
    assert!(a.co_changes.is_empty());
    assert_eq!(a.weak_co_changes.len(), 1);
    assert_eq!(a.weak_co_changes[0].path, "b.rs");
    assert_eq!(a.weak_co_changes[0].shared_commits, 1);
}

#[test]
fn test_co_change_low_jaccard_kept_as_weak_candidate() {
    let now_unix = 1_741_520_000_u64;
    let mut log = String::new();
    for i in 0..2 {
        let ts = now_unix - (i as u64 + 1) * 86400;
        log.push_str(&format!(
            "{d}\nH:shared{i}\nU:{ts}\nD:d\nA:X\nM:shared\n\n1\t0\ta.rs\n1\t0\tb.rs\n",
            d = COMMIT_DELIMITER,
        ));
    }
    for i in 0..8 {
        let ts = now_unix - (i as u64 + 10) * 86400;
        log.push_str(&format!(
            "{d}\nH:a{i}\nU:{ts}\nD:d\nA:X\nM:a-only\n\n1\t0\ta.rs\n",
            d = COMMIT_DELIMITER,
        ));
    }
    for i in 0..8 {
        let ts = now_unix - (i as u64 + 30) * 86400;
        log.push_str(&format!(
            "{d}\nH:b{i}\nU:{ts}\nD:d\nA:X\nM:b-only\n\n1\t0\tb.rs\n",
            d = COMMIT_DELIMITER,
        ));
    }
    let index = GitTemporalIndex::compute_from_log(&log, now_unix);
    let a = index.files.get("a.rs").unwrap();
    assert!(
        a.co_changes.is_empty(),
        "jaccard should stay below strong threshold"
    );
    assert_eq!(a.weak_co_changes.len(), 1);
    assert_eq!(a.weak_co_changes[0].path, "b.rs");
    assert_eq!(a.weak_co_changes[0].shared_commits, 2);
}

// ── Contributors ────────────────────────────────────────────────────

#[test]
fn test_contributors_sorted_by_percentage() {
    let now_unix = 1_741_520_000_u64;
    let mut log = String::new();
    // Alice: 3 commits, Bob: 1 commit to same file.
    for (i, author) in ["Alice", "Alice", "Alice", "Bob"].iter().enumerate() {
        let ts = now_unix - (i as u64 + 1) * 86400;
        log.push_str(&format!(
            "{d}\nH:c{i}\nU:{ts}\nD:d\nA:{author}\nM:m{i}\n\n1\t0\tshared.rs\n",
            d = COMMIT_DELIMITER,
        ));
    }
    let index = GitTemporalIndex::compute_from_log(&log, now_unix);
    let h = index.files.get("shared.rs").unwrap();
    assert_eq!(h.contributors.len(), 2);
    assert_eq!(h.contributors[0].author, "Alice");
    assert_eq!(h.contributors[0].commit_count, 3);
    assert!((h.contributors[0].percentage - 75.0).abs() < 0.1);
    assert_eq!(h.contributors[1].author, "Bob");
}

// ── Last commit ─────────────────────────────────────────────────────

#[test]
fn test_last_commit_is_most_recent() {
    let now_unix = 1_741_520_000_u64;
    let ts_recent = now_unix - 86400;
    let ts_old = now_unix - 30 * 86400;
    let log = format!(
        "{d}\nH:old111\nU:{ts_old}\nD:old-date\nA:Old\nM:old commit\n\n1\t0\tf.rs\n\
             {d}\nH:new222\nU:{ts_recent}\nD:new-date\nA:New\nM:new commit\n\n1\t0\tf.rs\n",
        d = COMMIT_DELIMITER,
    );
    let index = GitTemporalIndex::compute_from_log(&log, now_unix);
    let h = index.files.get("f.rs").unwrap();
    assert_eq!(h.last_commit.hash, "new222");
    assert_eq!(h.last_commit.author, "New");
}

// ── Hotspots ────────────────────────────────────────────────────────

#[test]
fn test_hotspots_sorted_descending() {
    let now_unix = 1_741_520_000_u64;
    let mut log = String::new();
    // hot.rs: 5 recent commits. cold.rs: 1 old commit.
    for i in 0..5 {
        let ts = now_unix - (i + 1) * 86400;
        log.push_str(&format!(
            "{d}\nH:h{i}\nU:{ts}\nD:d\nA:X\nM:m\n\n1\t0\thot.rs\n",
            d = COMMIT_DELIMITER,
        ));
    }
    let ts_old = now_unix - 80 * 86400;
    log.push_str(&format!(
        "{d}\nH:c0\nU:{ts_old}\nD:d\nA:Y\nM:m\n\n1\t0\tcold.rs\n",
        d = COMMIT_DELIMITER,
    ));
    let index = GitTemporalIndex::compute_from_log(&log, now_unix);
    assert!(!index.stats.hotspots.is_empty());
    assert_eq!(index.stats.hotspots[0].0, "hot.rs");
    assert!(index.stats.hotspots[0].1 > index.stats.hotspots.last().unwrap().1);
}

// ── Pending / unavailable states ────────────────────────────────────

#[test]
fn test_pending_state() {
    let idx = GitTemporalIndex::pending();
    assert_eq!(idx.state, GitTemporalState::Pending);
    assert!(idx.files.is_empty());
}

#[test]
fn test_unavailable_state() {
    let idx = GitTemporalIndex::unavailable("no git".to_string());
    assert_eq!(
        idx.state,
        GitTemporalState::Unavailable("no git".to_string())
    );
}

// ── Path normalization ──────────────────────────────────────────────

#[test]
fn test_normalize_git_path_backslash() {
    assert_eq!(normalize_git_path("src\\foo\\bar.rs"), "src/foo/bar.rs");
}

#[test]
fn test_normalize_git_path_forward_slash_unchanged() {
    assert_eq!(normalize_git_path("src/foo/bar.rs"), "src/foo/bar.rs");
}

// ── Producer parity: MCP open vs the embedded source-root open ───────

/// One commit over top-level files only; `None` removes a file.
fn fixture_commit(
    repo: &git2::Repository,
    parents: &[git2::Oid],
    files: &[(&str, Option<&str>)],
    hours_ago: i64,
    message: &str,
) -> git2::Oid {
    let parent_commits: Vec<git2::Commit<'_>> = parents
        .iter()
        .map(|oid| repo.find_commit(*oid).unwrap())
        .collect();
    let base = parent_commits.first().map(|commit| commit.tree().unwrap());
    let mut builder = repo.treebuilder(base.as_ref()).unwrap();
    for (name, content) in files {
        match content {
            Some(content) => {
                let blob = repo.blob(content.as_bytes()).unwrap();
                builder.insert(name, blob, 0o100644).unwrap();
            }
            None => builder.remove(name).unwrap(),
        }
    }
    let tree = repo.find_tree(builder.write().unwrap()).unwrap();
    let now = SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap()
        .as_secs() as i64;
    let signature = git2::Signature::new(
        "Fixture",
        "fixture@example.invalid",
        &git2::Time::new(now - hours_ago * 3600, 0),
    )
    .unwrap();
    let parent_refs: Vec<&git2::Commit<'_>> = parent_commits.iter().collect();
    repo.commit(None, &signature, &signature, message, &tree, &parent_refs)
        .unwrap()
}

/// Six commits: a root, two coupled edits, a side branch, a rename
/// (`a.rs` to `e.rs`) and a merge of the side branch.
fn producer_fixture() -> tempfile::TempDir {
    let dir = tempfile::tempdir().unwrap();
    let repo = git2::Repository::init(dir.path()).unwrap();
    let root = fixture_commit(
        &repo,
        &[],
        &[
            ("a.rs", Some("a0")),
            ("b.rs", Some("b0")),
            ("c.rs", Some("c0")),
        ],
        10,
        "root",
    );
    let second = fixture_commit(
        &repo,
        &[root],
        &[("a.rs", Some("a1")), ("b.rs", Some("b1"))],
        9,
        "couple a and b",
    );
    let third = fixture_commit(
        &repo,
        &[second],
        &[("a.rs", Some("a2")), ("b.rs", Some("b2"))],
        8,
        "couple a and b again",
    );
    let side = fixture_commit(
        &repo,
        &[second],
        &[("c.rs", Some("c1")), ("d.rs", Some("d0"))],
        7,
        "side branch",
    );
    let renamed = fixture_commit(
        &repo,
        &[third],
        &[("a.rs", None), ("e.rs", Some("a2"))],
        6,
        "rename a to e",
    );
    let merge = fixture_commit(
        &repo,
        &[renamed, side],
        &[("c.rs", Some("c1")), ("d.rs", Some("d0"))],
        5,
        "merge side",
    );
    repo.reference("refs/heads/fixture", merge, true, "fixture")
        .unwrap();
    repo.set_head("refs/heads/fixture").unwrap();
    dir
}

type ProducerRow = (String, String, String, String, Vec<String>);

fn producer_rows(commits: &[ParsedCommit]) -> Vec<ProducerRow> {
    commits
        .iter()
        .map(|commit| {
            let mut files = commit.files.clone();
            files.sort();
            (
                commit.hash.clone(),
                commit.timestamp.clone(),
                commit.author.clone(),
                commit.message.clone(),
                files,
            )
        })
        .collect()
}

type CoChangeRow = (
    String,
    Vec<(String, u32, u32)>,
    Vec<(String, u32, u32)>,
    u32,
);

fn co_change_rows(index: &GitTemporalIndex) -> Vec<CoChangeRow> {
    let entries = |list: &[CoChangeEntry]| {
        list.iter()
            .map(|entry| {
                (
                    entry.path.clone(),
                    entry.shared_commits,
                    entry.coupling_score.to_bits(),
                )
            })
            .collect::<Vec<_>>()
    };
    let mut rows: Vec<CoChangeRow> = index
        .files
        .iter()
        .map(|(path, history)| {
            (
                path.clone(),
                entries(&history.co_changes),
                entries(&history.weak_co_changes),
                history.commit_count,
            )
        })
        .collect();
    rows.sort();
    rows
}

#[test]
fn mcp_and_embedded_producers_walk_identical_history_and_aggregate_identically() {
    let dir = producer_fixture();

    // MCP producer: `compute` opens with `GitRepo::open` (discovery).
    let mcp = load_commits(dir.path()).unwrap();
    // Embedded producer: the source-root open with a live stop poll.
    let repo = crate::git::GitRepo::open_worktree_root(dir.path()).unwrap();
    let embedded = load_commits_from(&repo, &mut || false).unwrap();
    assert_eq!(producer_rows(&mcp), producer_rows(&embedded));

    let rows = producer_rows(&mcp);
    let messages: Vec<&str> = rows.iter().map(|row| row.3.as_str()).collect();
    assert_eq!(
        messages,
        [
            "merge side",
            "rename a to e",
            "side branch",
            "couple a and b again",
            "couple a and b",
            "root"
        ]
    );
    // Merge: diffed against its first parent only. Rename: no similarity
    // detection, so both the removed and the added path are listed.
    assert_eq!(rows[0].4, ["c.rs", "d.rs"]);
    assert_eq!(rows[1].4, ["a.rs", "e.rs"]);

    let mcp_index = GitTemporalIndex::compute(dir.path());
    let embedded_index = GitTemporalIndex::compute_from_repo(&repo, &mut || false);
    assert_eq!(mcp_index.state, GitTemporalState::Ready);
    assert_eq!(embedded_index.state, GitTemporalState::Ready);
    assert_eq!(co_change_rows(&mcp_index), co_change_rows(&embedded_index));
    let b = &mcp_index.files["b.rs"];
    assert_eq!(b.co_changes[0].path, "a.rs");
    assert_eq!(b.co_changes[0].shared_commits, 3);

    // The embedded stop abandons the walk instead of reporting partial history.
    let stopped = GitTemporalIndex::compute_from_repo(&repo, &mut || true);
    assert!(matches!(stopped.state, GitTemporalState::Unavailable(_)));
}