symforge 11.22.0

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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
//! Git temporal intelligence — enriches the index with git history metadata.
//!
//! Computes per-file churn scores (exponential-decay weighted), ownership
//! distribution, co-change coupling (Jaccard coefficient), and repo-wide
//! hotspot summaries using libgit2 via [`crate::git::GitRepo`].
//!
//! Design principles:
//! - In-process git access: uses libgit2 (via git2 crate) — no child
//!   processes, no console windows, faster execution.
//! - Bounded: max 500 commits OR 90 days, whichever is smaller.
//! - Exponential decay: half-life of 14 days so recent activity dominates.
//! - Rank-normalized churn: percentile position across all tracked files
//!   (0.0 = coldest, 1.0 = hottest in repo) — meaningful regardless of
//!   absolute activity level.
//! - Jaccard co-change: `|A∩B| / |A∪B|` filters out high-frequency noise
//!   files (lock files, CI configs) that appear in many unrelated commits.
//! - Mega-commit filter: commits touching >50 files are excluded from
//!   co-change analysis to avoid pollution from bulk reformats/merges.

use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant, SystemTime};

use super::store::{GitTemporalPublicationFence, SharedIndex};

#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct GitTemporalComputationRequest {
    pub(crate) fence: GitTemporalPublicationFence,
    pub(crate) repo_root: PathBuf,
}

#[derive(Default)]
pub(crate) struct GitTemporalJobQueue {
    running: Option<GitTemporalComputationRequest>,
    pending_latest: Option<GitTemporalComputationRequest>,
}

impl GitTemporalJobQueue {
    fn enqueue(
        &mut self,
        request: GitTemporalComputationRequest,
    ) -> Option<GitTemporalComputationRequest> {
        if self.running.is_none() {
            self.running = Some(request.clone());
            return Some(request);
        }
        if self.running.as_ref() == Some(&request) || self.pending_latest.as_ref() == Some(&request)
        {
            return None;
        }
        self.pending_latest = Some(request);
        None
    }

    fn finish(
        &mut self,
        completed: &GitTemporalComputationRequest,
    ) -> Option<GitTemporalComputationRequest> {
        if self.running.as_ref() != Some(completed) {
            return None;
        }
        if let Some(next) = self.pending_latest.take() {
            self.running = Some(next.clone());
            Some(next)
        } else {
            self.running = None;
            None
        }
    }
}

// ── Background computation ──────────────────────────────────────────────

/// Spawn a background task that computes the git temporal index and swaps
/// it into the shared handle. Non-blocking — returns immediately.
///
/// Call after `LiveIndex::load()` or `SharedIndexHandle::reload()` completes.
///
/// V11 callbacks census (Feature 020 Slice 4, C3b): publication-fence-gated
/// DATA-PLANE enrichment — it computes git metadata and publishes it behind
/// the generation fence below; it admits no repository-source bytes and holds
/// no source-observation or publication authority of its own. C4's typed
/// roots gate its publication.
pub fn spawn_git_temporal_computation(index: SharedIndex, repo_root: PathBuf, expected_gen: u64) {
    // Guard: only spawn if a tokio runtime is available (not the case in some sync tests).
    if tokio::runtime::Handle::try_current().is_err() {
        return;
    }

    let fence = index.git_temporal_publication_fence();
    if fence.project_generation != expected_gen {
        tracing::trace!(
            expected_gen,
            current_gen = fence.project_generation,
            "stale git temporal computation request skipped"
        );
        return;
    }
    let published = index.published_generation();
    let signals_are_current = published.code_signals.computed_for_content_generation
        == fence.content_generation
        && fence.source_version.as_ref()
            == Some(&published.code_signals.computed_for_source_version)
        && matches!(
            published.code_signals.state,
            GitTemporalState::Ready | GitTemporalState::Unavailable(_)
        );
    if signals_are_current {
        return;
    }
    let request = GitTemporalComputationRequest { fence, repo_root };
    let Some(request) = index.git_temporal_jobs.lock().enqueue(request) else {
        return;
    };

    tokio::spawn(async move {
        let mut request = request;
        loop {
            // If data is already Ready, keep serving it while recomputing.
            let was_ready = index.git_temporal().state == GitTemporalState::Ready;
            if !was_ready
                && !index.update_git_temporal_at_fence(
                    GitTemporalIndex {
                        state: GitTemporalState::Computing,
                        ..GitTemporalIndex::pending()
                    },
                    &request.fence,
                )
            {
                tracing::trace!("stale git temporal computing-state publication skipped");
            }

            let repo_root = request.repo_root.clone();
            let result =
                tokio::task::spawn_blocking(move || GitTemporalIndex::compute(&repo_root)).await;

            match result {
                Ok(temporal) => {
                    let files = temporal.files.len();
                    let commits = temporal.stats.total_commits_analyzed;
                    let duration_ms = temporal.stats.compute_duration.as_millis() as u64;
                    if index.update_git_temporal_at_fence(temporal, &request.fence) {
                        tracing::info!(files, commits, duration_ms, "git temporal index computed");
                    } else {
                        tracing::trace!(
                            files,
                            commits,
                            duration_ms,
                            "stale git temporal publication skipped"
                        );
                    }
                }
                Err(error) => {
                    tracing::warn!("git temporal computation panicked: {error}");
                    if !was_ready {
                        let unavailable =
                            GitTemporalIndex::unavailable(format!("computation panicked: {error}"));
                        if !index.update_git_temporal_at_fence(unavailable, &request.fence) {
                            tracing::trace!("stale git temporal panic-state publication skipped");
                        }
                    }
                }
            }

            // A content/source-version change without another explicit request
            // still gets exactly one latest recomputation for the same source.
            let latest_fence = index.git_temporal_publication_fence();
            if latest_fence.project_generation == request.fence.project_generation
                && latest_fence.source == request.fence.source
                && latest_fence != request.fence
            {
                index
                    .git_temporal_jobs
                    .lock()
                    .enqueue(GitTemporalComputationRequest {
                        fence: latest_fence,
                        repo_root: request.repo_root.clone(),
                    });
            }

            let next = index.git_temporal_jobs.lock().finish(&request);
            let Some(next) = next else {
                break;
            };
            request = next;
        }
    });
}

// ── Configuration constants ─────────────────────────────────────────────

const MAX_COMMITS: u32 = 500;
const WINDOW_DAYS: u32 = 90;
/// Exponential decay half-life in days. A commit 14 days ago has half the
/// weight of one today; 28 days ago has a quarter, etc.
const HALF_LIFE_DAYS: f64 = 14.0;
/// Maximum co-changed files shown per file.
const CO_CHANGE_CAP_PER_FILE: usize = 5;
/// Top hotspots in the repo-wide stats.
const HOTSPOT_CAP: usize = 10;
/// Top coupled pairs in the repo-wide stats.
const COUPLED_PAIRS_CAP: usize = 10;
/// Minimum shared commits before a co-change pair is considered.
const MIN_SHARED_COMMITS: u32 = 2;
/// Minimum Jaccard coefficient to keep a co-change entry.
const MIN_JACCARD: f32 = 0.15;
/// Weak co-change candidates are surfaced only when strong matches are absent.
/// This keeps the main signal high-trust while still avoiding a blank wall for
/// files with some history but no qualifying strong pair.
const WEAK_MIN_JACCARD: f32 = 0.05;
/// Maximum low-confidence co-changed files shown per file.
const WEAK_CO_CHANGE_CAP_PER_FILE: usize = 3;
/// Maximum contributors shown per file.
const CONTRIBUTOR_CAP: usize = 5;
/// Commits touching more files than this are excluded from co-change
/// analysis (likely merges, formatting runs, bulk renames).
const MEGA_COMMIT_THRESHOLD: usize = 50;

// ── Public data types ───────────────────────────────────────────────────

/// Per-file temporal metadata derived from git history.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct GitFileHistory {
    /// Total commits touching this file within the analysis window.
    pub commit_count: u32,
    /// Recency-weighted churn score, rank-normalized to 0.0–1.0 across the
    /// entire repo. Uses exponential decay with a 14-day half-life so
    /// recent commits dominate. Rank-normalized means the hottest file in
    /// the repo is always ~1.0, the coldest ~0.0.
    pub churn_score: f32,
    /// Most recent commit touching this file.
    pub last_commit: CommitSummary,
    /// Ownership distribution — who actually maintains this file, sorted by
    /// commit share descending. Capped at top 5 contributors.
    pub contributors: Vec<ContributorShare>,
    /// Files that co-change with this one, ranked by Jaccard coupling
    /// strength. Capped at top 5.
    pub co_changes: Vec<CoChangeEntry>,
    /// Lower-confidence co-change candidates that missed the strong threshold
    /// but still have some evidence. Only shown when strong matches are absent.
    pub weak_co_changes: Vec<CoChangeEntry>,
}

/// Summary of a single git commit (cheap to clone, display-ready).
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct CommitSummary {
    /// Short hash (7 chars).
    pub hash: String,
    /// ISO 8601 author date for display.
    pub timestamp: String,
    /// Author name.
    pub author: String,
    /// First line of commit message, truncated to 72 chars.
    pub message_head: String,
    /// Days ago from the time of computation (for relative time display).
    pub days_ago: f64,
}

/// One contributor's share of a file's commit history.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ContributorShare {
    pub author: String,
    pub commit_count: u32,
    /// Percentage of total commits to this file (0.0–100.0).
    pub percentage: f32,
}

/// One co-change relationship for a file.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct CoChangeEntry {
    /// Path of the co-changed file.
    pub path: String,
    /// Jaccard coefficient: `|shared_commits| / |union_commits|`, 0.0–1.0.
    pub coupling_score: f32,
    /// Raw number of commits where both files changed together.
    pub shared_commits: u32,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum CoChangeStrength {
    Strong,
    Weak,
}

fn classify_co_change_pair(
    shared: u32,
    count_a: u32,
    count_b: u32,
) -> Option<(f32, CoChangeStrength)> {
    if shared == 0 {
        return None;
    }
    let union = count_a + count_b - shared;
    if union == 0 {
        return None;
    }
    let jaccard = shared as f32 / union as f32;
    if shared >= MIN_SHARED_COMMITS && jaccard >= MIN_JACCARD {
        Some((jaccard, CoChangeStrength::Strong))
    } else if jaccard >= WEAK_MIN_JACCARD {
        Some((jaccard, CoChangeStrength::Weak))
    } else {
        None
    }
}

fn sort_and_cap_co_changes(entries: &mut Vec<CoChangeEntry>, cap: usize) {
    entries.sort_by(|a, b| {
        b.coupling_score
            .partial_cmp(&a.coupling_score)
            .unwrap_or(std::cmp::Ordering::Equal)
            .then_with(|| b.shared_commits.cmp(&a.shared_commits))
            .then_with(|| a.path.cmp(&b.path))
    });
    entries.truncate(cap);
}

/// Repo-wide temporal summary for health reports.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct GitTemporalStats {
    /// Total commits analyzed in this computation.
    pub total_commits_analyzed: u32,
    /// Analysis window in days (from config, currently 90).
    pub analysis_window_days: u32,
    /// Top hotspot files by churn score.
    pub hotspots: Vec<(String, f32)>,
    /// Top coupled file pairs by Jaccard coefficient.
    pub most_coupled: Vec<(String, String, f32)>,
    /// Wall-clock time when computation completed.
    pub computed_at: SystemTime,
    /// Time spent computing the temporal index.
    pub compute_duration: Duration,
}

/// The full temporal index — a side-table that lives parallel to the
/// main `LiveIndex` on `SharedIndexHandle`.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct GitTemporalIndex {
    /// Per-file temporal metadata, keyed by relative path (forward-slash
    /// normalized, same key space as `LiveIndex::files`).
    pub files: HashMap<String, GitFileHistory>,
    /// Repo-wide summary statistics.
    pub stats: GitTemporalStats,
    /// Current state of the temporal index.
    pub state: GitTemporalState,
}

/// Lifecycle state of the temporal index.
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum GitTemporalState {
    /// Not yet computed (initial state).
    Pending,
    /// Background computation is in progress.
    Computing,
    /// Computation completed — data is available.
    Ready,
    /// Git is unavailable or the directory is not a git repo.
    Unavailable(String),
}

// ── Intermediate parsing types (private) ────────────────────────────────

#[derive(Debug)]
struct ParsedCommit {
    hash: String,
    timestamp: String,
    author: String,
    message: String,
    /// Days before computation time (0.0 = today).
    days_ago: f64,
    /// Relative file paths touched by this commit.
    files: Vec<String>,
}

// ── Rendering helpers (public) ──────────────────────────────────────────

/// Render a 10-character visual churn bar: `██████░░░░`
pub fn churn_bar(score: f32) -> String {
    let clamped = score.clamp(0.0, 1.0);
    let filled = (clamped * 10.0).round() as usize;
    let empty = 10_usize.saturating_sub(filled);
    format!("{}{}", "█".repeat(filled), "░".repeat(empty))
}

/// Human-readable churn label for a normalized score.
pub fn churn_label(score: f32) -> &'static str {
    if score >= 0.8 {
        "critical"
    } else if score >= 0.6 {
        "hot"
    } else if score >= 0.4 {
        "warm"
    } else if score >= 0.2 {
        "cool"
    } else {
        "frozen"
    }
}

/// Format days-ago as a compact relative time string: "3d ago", "2w ago", etc.
pub fn relative_time(days_ago: f64) -> String {
    if days_ago < 0.0 {
        return "just now".to_string();
    }
    if days_ago < 1.0 {
        return "today".to_string();
    }
    if days_ago < 7.0 {
        return format!("{}d ago", days_ago.round() as u32);
    }
    if days_ago < 30.0 {
        return format!("{}w ago", (days_ago / 7.0).round() as u32);
    }
    format!("{}mo ago", (days_ago / 30.0).round() as u32)
}

// ── Core implementation ─────────────────────────────────────────────────

impl GitTemporalIndex {
    /// Construct a pending (empty) temporal index.
    pub fn pending() -> Self {
        Self {
            files: HashMap::new(),
            stats: GitTemporalStats {
                total_commits_analyzed: 0,
                analysis_window_days: WINDOW_DAYS,
                hotspots: Vec::new(),
                most_coupled: Vec::new(),
                computed_at: SystemTime::now(),
                compute_duration: Duration::ZERO,
            },
            state: GitTemporalState::Pending,
        }
    }

    /// Construct an unavailable temporal index with a reason.
    pub fn unavailable(reason: String) -> Self {
        Self {
            state: GitTemporalState::Unavailable(reason),
            ..Self::pending()
        }
    }

    /// Compute the full temporal index from git history.
    ///
    /// Uses libgit2 via [`crate::git::GitRepo`] to walk the commit log
    /// and build per-file metrics, co-change relationships, and repo-wide
    /// stats. Designed to run on a blocking thread.
    pub fn compute(repo_root: &Path) -> Self {
        let start = Instant::now();
        match load_commits(repo_root) {
            Ok(commits) => Self::aggregate(commits, start),
            Err(reason) => Self::unavailable(reason),
        }
    }

    /// [`Self::compute`] over an already-open repository whose walk `stop`
    /// may abandon between commits. The embedded lanes use this with their
    /// operation budget; the producer and aggregator are the MCP ones.
    #[cfg(any(feature = "embed", test))]
    pub(crate) fn compute_from_repo(
        repo: &crate::git::GitRepo,
        stop: &mut dyn FnMut() -> bool,
    ) -> Self {
        let start = Instant::now();
        match load_commits_from(repo, stop) {
            Ok(commits) => Self::aggregate(commits, start),
            Err(reason) => Self::unavailable(reason),
        }
    }

    /// The aggregator shared by every producer: per-file churn, ownership,
    /// Jaccard co-change, and repo-wide stats from the walked commits.
    fn aggregate(commits: Vec<ParsedCommit>, start: Instant) -> Self {
        if commits.is_empty() {
            return Self {
                files: HashMap::new(),
                stats: GitTemporalStats {
                    total_commits_analyzed: 0,
                    analysis_window_days: WINDOW_DAYS,
                    hotspots: Vec::new(),
                    most_coupled: Vec::new(),
                    computed_at: SystemTime::now(),
                    compute_duration: start.elapsed(),
                },
                state: GitTemporalState::Ready,
            };
        }

        let total_commits = commits.len() as u32;
        let decay_lambda = (2.0_f64).ln() / HALF_LIFE_DAYS;

        // ── Phase 1: Per-file aggregation ───────────────────────────────

        // file -> list of commit indices (for co-change Jaccard denominators)
        let mut file_commit_indices: HashMap<String, Vec<usize>> = HashMap::new();
        // file -> author -> commit count
        let mut file_authors: HashMap<String, HashMap<String, u32>> = HashMap::new();
        // file -> index of most recent commit (smallest days_ago)
        let mut file_last_commit_idx: HashMap<String, usize> = HashMap::new();
        // file -> sum of decay-weighted commit scores
        let mut file_raw_churn: HashMap<String, f64> = HashMap::new();

        for (idx, commit) in commits.iter().enumerate() {
            let weight = (-decay_lambda * commit.days_ago).exp();

            // Dedup file paths per commit to avoid inflating Jaccard denominators
            // (renames or merges can list the same file twice in a single commit).
            let unique_files: HashSet<&String> = commit.files.iter().collect();
            for file_path in unique_files {
                file_commit_indices
                    .entry(file_path.clone())
                    .or_default()
                    .push(idx);

                *file_authors
                    .entry(file_path.clone())
                    .or_default()
                    .entry(commit.author.clone())
                    .or_insert(0) += 1;

                file_last_commit_idx
                    .entry(file_path.clone())
                    .and_modify(|existing| {
                        if commit.days_ago < commits[*existing].days_ago {
                            *existing = idx;
                        }
                    })
                    .or_insert(idx);

                *file_raw_churn.entry(file_path.clone()).or_insert(0.0) += weight;
            }
        }

        // ── Phase 2: Rank-normalize churn scores ────────────────────────

        let mut churn_entries: Vec<(String, f64)> = file_raw_churn.into_iter().collect();
        churn_entries.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));

        let file_count = churn_entries.len();
        let mut normalized_churn: HashMap<String, f32> = HashMap::with_capacity(file_count);
        for (rank, (path, _raw)) in churn_entries.iter().enumerate() {
            let score = if file_count <= 1 {
                if churn_entries[0].1 > 0.0 { 1.0 } else { 0.0 }
            } else {
                rank as f32 / (file_count - 1) as f32
            };
            normalized_churn.insert(path.clone(), score);
        }

        // ── Phase 3: Co-change matrix (Jaccard) ────────────────────────

        // Count how many commits each pair of files shares.
        let mut pair_counts: HashMap<(String, String), u32> = HashMap::new();

        for commit in &commits {
            let mut sorted_files: Vec<&str> = commit.files.iter().map(|s| s.as_str()).collect();
            sorted_files.sort_unstable();
            sorted_files.dedup();

            // Skip mega-commits (merges, bulk reformats, etc.)
            if sorted_files.len() > MEGA_COMMIT_THRESHOLD {
                continue;
            }

            for i in 0..sorted_files.len() {
                for j in (i + 1)..sorted_files.len() {
                    // Canonical ordering: alphabetically smaller path first.
                    let key = (sorted_files[i].to_string(), sorted_files[j].to_string());
                    *pair_counts.entry(key).or_insert(0) += 1;
                }
            }
        }

        // Compute Jaccard for each qualifying pair.
        let mut file_co_changes: HashMap<String, Vec<CoChangeEntry>> = HashMap::new();
        let mut weak_file_co_changes: HashMap<String, Vec<CoChangeEntry>> = HashMap::new();

        for ((file_a, file_b), shared) in &pair_counts {
            let count_a = file_commit_indices
                .get(file_a)
                .map(|v| v.len() as u32)
                .unwrap_or(0);
            let count_b = file_commit_indices
                .get(file_b)
                .map(|v| v.len() as u32)
                .unwrap_or(0);
            let Some((jaccard, strength)) = classify_co_change_pair(*shared, count_a, count_b)
            else {
                continue;
            };

            let target = match strength {
                CoChangeStrength::Strong => &mut file_co_changes,
                CoChangeStrength::Weak => &mut weak_file_co_changes,
            };

            // Bidirectional: A sees B, B sees A.
            target
                .entry(file_a.clone())
                .or_default()
                .push(CoChangeEntry {
                    path: file_b.clone(),
                    coupling_score: jaccard,
                    shared_commits: *shared,
                });
            target
                .entry(file_b.clone())
                .or_default()
                .push(CoChangeEntry {
                    path: file_a.clone(),
                    coupling_score: jaccard,
                    shared_commits: *shared,
                });
        }

        // Sort by coupling strength descending and cap per file.
        for entries in file_co_changes.values_mut() {
            sort_and_cap_co_changes(entries, CO_CHANGE_CAP_PER_FILE);
        }
        for entries in weak_file_co_changes.values_mut() {
            sort_and_cap_co_changes(entries, WEAK_CO_CHANGE_CAP_PER_FILE);
        }

        // ── Phase 4: Assemble GitFileHistory per file ───────────────────

        let mut files: HashMap<String, GitFileHistory> = HashMap::with_capacity(file_count);

        for (path, _commit_indices) in &file_commit_indices {
            let commit_count = _commit_indices.len() as u32;
            let churn_score = normalized_churn.get(path).copied().unwrap_or(0.0);

            // Last commit
            let last_idx = file_last_commit_idx.get(path).copied().unwrap_or(0);
            let last = &commits[last_idx];
            let last_commit = CommitSummary {
                hash: last.hash.clone(),
                timestamp: last.timestamp.clone(),
                author: last.author.clone(),
                message_head: truncate_message(&last.message, 72),
                days_ago: last.days_ago,
            };

            // Contributors
            let contributors = file_authors
                .get(path)
                .map(|authors| {
                    let total = authors.values().sum::<u32>() as f32;
                    let mut shares: Vec<ContributorShare> = authors
                        .iter()
                        .map(|(author, count)| ContributorShare {
                            author: author.clone(),
                            commit_count: *count,
                            percentage: (*count as f32 / total) * 100.0,
                        })
                        .collect();
                    shares.sort_by(|a, b| {
                        b.percentage
                            .partial_cmp(&a.percentage)
                            .unwrap_or(std::cmp::Ordering::Equal)
                    });
                    shares.truncate(CONTRIBUTOR_CAP);
                    shares
                })
                .unwrap_or_default();

            let co_changes = file_co_changes.remove(path).unwrap_or_default();
            let weak_co_changes = weak_file_co_changes.remove(path).unwrap_or_default();

            files.insert(
                path.clone(),
                GitFileHistory {
                    commit_count,
                    churn_score,
                    last_commit,
                    contributors,
                    co_changes,
                    weak_co_changes,
                },
            );
        }

        // ── Phase 5: Repo-wide stats ────────────────────────────────────

        let mut hotspots: Vec<(String, f32)> = files
            .iter()
            .map(|(path, h)| (path.clone(), h.churn_score))
            .collect();
        hotspots.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
        hotspots.truncate(HOTSPOT_CAP);

        let mut most_coupled: Vec<(String, String, f32)> = pair_counts
            .iter()
            .filter_map(|((a, b), shared)| {
                let count_a = file_commit_indices
                    .get(a)
                    .map(|v| v.len() as u32)
                    .unwrap_or(0);
                let count_b = file_commit_indices
                    .get(b)
                    .map(|v| v.len() as u32)
                    .unwrap_or(0);
                match classify_co_change_pair(*shared, count_a, count_b) {
                    Some((jaccard, CoChangeStrength::Strong)) => {
                        Some((a.clone(), b.clone(), jaccard))
                    }
                    _ => None,
                }
            })
            .collect();
        most_coupled.sort_by(|a, b| b.2.partial_cmp(&a.2).unwrap_or(std::cmp::Ordering::Equal));
        most_coupled.truncate(COUPLED_PAIRS_CAP);

        Self {
            files,
            stats: GitTemporalStats {
                total_commits_analyzed: total_commits,
                analysis_window_days: WINDOW_DAYS,
                hotspots,
                most_coupled,
                computed_at: SystemTime::now(),
                compute_duration: start.elapsed(),
            },
            state: GitTemporalState::Ready,
        }
    }
}

// ── Git log via libgit2 ─────────────────────────────────────────────────

/// Load commits from git history using libgit2 (no child processes).
fn load_commits(repo_root: &Path) -> Result<Vec<ParsedCommit>, String> {
    let repo = crate::git::GitRepo::open(repo_root)?;
    load_commits_from(&repo, &mut || false)
}

/// The history producer: the newest `MAX_COMMITS` commits within
/// `WINDOW_DAYS`, each diffed against its first parent.
fn load_commits_from(
    repo: &crate::git::GitRepo,
    stop: &mut dyn FnMut() -> bool,
) -> Result<Vec<ParsedCommit>, String> {
    let entries = repo.log_with_stats_until(MAX_COMMITS as usize, WINDOW_DAYS, stop)?;

    let now = SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs() as f64;

    Ok(entries
        .into_iter()
        .map(|e| {
            let days_ago = (now - e.unix_timestamp as f64) / 86400.0;
            ParsedCommit {
                hash: e.hash,
                timestamp: e.timestamp,
                author: e.author,
                message: e.message,
                days_ago: days_ago.max(0.0),
                files: e.files,
            }
        })
        .collect())
}

/// Truncate a message to `max_len` characters, appending "..." if truncated.
fn truncate_message(msg: &str, max_len: usize) -> String {
    if msg.chars().count() <= max_len {
        msg.to_string()
    } else {
        let truncated: String = msg.chars().take(max_len.saturating_sub(3)).collect();
        format!("{truncated}...")
    }
}

// ── Tests ───────────────────────────────────────────────────────────────

// ── Test-only parsing infrastructure ────────────────────────────────────
//
// These were the original CLI-based parsing functions. They are retained
// solely to support the existing unit tests that feed synthetic `git log`
// strings into `compute_from_log`. Compiled only in test builds.

#[cfg(test)]
const COMMIT_DELIMITER: &str = "SYMFORGE_GIT_TEMPORAL_DELIM";

#[cfg(test)]
fn parse_git_log(raw: &str, now_unix: u64) -> Vec<ParsedCommit> {
    let mut commits: Vec<ParsedCommit> = Vec::new();
    let mut current: Option<ParsedCommitBuilder> = None;

    for line in raw.lines() {
        let line = line.trim_end();

        if line == COMMIT_DELIMITER {
            if let Some(builder) = current.take()
                && let Some(commit) = builder.build()
            {
                commits.push(commit);
            }
            current = Some(ParsedCommitBuilder::new());
            continue;
        }

        let Some(builder) = current.as_mut() else {
            continue;
        };

        if let Some(rest) = line.strip_prefix("H:") {
            builder.hash = rest.to_string();
        } else if let Some(rest) = line.strip_prefix("U:") {
            if let Ok(unix_ts) = rest.parse::<u64>() {
                builder.unix_timestamp = Some(unix_ts);
                builder.days_ago = (now_unix as f64 - unix_ts as f64) / 86400.0;
            }
        } else if let Some(rest) = line.strip_prefix("D:") {
            builder.timestamp = rest.to_string();
        } else if let Some(rest) = line.strip_prefix("A:") {
            builder.author = rest.to_string();
        } else if let Some(rest) = line.strip_prefix("M:") {
            builder.message = rest.to_string();
        } else if !line.is_empty()
            && let Some(path) = parse_numstat_line(line)
        {
            builder.files.push(normalize_git_path(&path));
        }
    }

    if let Some(builder) = current.take()
        && let Some(commit) = builder.build()
    {
        commits.push(commit);
    }

    commits
}

#[cfg(test)]
fn parse_numstat_line(line: &str) -> Option<String> {
    let mut parts = line.splitn(3, '\t');
    let added = parts.next()?;
    let _removed = parts.next()?;
    let path = parts.next()?;

    if added == "-" {
        return None;
    }
    if !added.bytes().all(|b| b.is_ascii_digit()) {
        return None;
    }
    if path.is_empty() {
        return None;
    }

    Some(path.to_string())
}

#[cfg(test)]
fn normalize_git_path(path: &str) -> String {
    path.replace('\\', "/")
}

#[cfg(test)]
#[derive(Debug, Default)]
struct ParsedCommitBuilder {
    hash: String,
    timestamp: String,
    author: String,
    message: String,
    unix_timestamp: Option<u64>,
    days_ago: f64,
    files: Vec<String>,
}

#[cfg(test)]
impl ParsedCommitBuilder {
    fn new() -> Self {
        Self::default()
    }

    fn build(self) -> Option<ParsedCommit> {
        if self.hash.is_empty() || self.files.is_empty() {
            return None;
        }
        Some(ParsedCommit {
            hash: self.hash,
            timestamp: self.timestamp,
            author: self.author,
            message: self.message,
            days_ago: self.days_ago,
            files: self.files,
        })
    }
}

// ── Test-only helper (separate impl block to keep test infra out of prod) ──

#[cfg(test)]
impl GitTemporalIndex {
    /// Compute from a pre-built log string through the shared aggregator.
    fn compute_from_log(raw_log: &str, now_unix: u64) -> Self {
        let start = Instant::now();
        let commits = parse_git_log(raw_log, now_unix);
        Self::aggregate(commits, start)
    }
}

#[cfg(test)]
mod tests;