open-kioku-git 2.3.0

Git repository helpers for Open Kioku indexing and analysis.
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
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
use chrono::{DateTime, Duration, Utc};
use globset::Glob;
use open_kioku_core::{
    Confidence, MemorySearchResult, Owner, OwnerSuggestion, OwnershipConfidenceBreakdown,
    OwnershipEvidence, OwnershipReport, OwnershipSourceType, PolicyComponentMatch, ProvenanceTouch,
};
use open_kioku_errors::Result;
use open_kioku_storage::HistoryStore;
use std::cmp::Ordering;
use std::collections::{BTreeMap, BTreeSet};
use std::fs;
use std::path::{Component, Path, PathBuf};

const HISTORY_LIMIT: usize = 100;
const STALE_AFTER_DAYS: i64 = 365;
const CODEOWNERS_SCORE: f32 = 0.70;
const MEMORY_ONLY_CAP: f32 = 0.45;

const CODEOWNERS_CANDIDATES: &[&str] = &[
    ".open-kioku/CODEOWNERS",
    ".github/CODEOWNERS",
    "CODEOWNERS",
    "docs/CODEOWNERS",
    "OWNERS",
];

pub struct OwnershipInput<'a> {
    pub repo: &'a Path,
    pub path: &'a Path,
    pub history: &'a dyn HistoryStore,
    pub memory_facts: &'a [MemorySearchResult],
    pub components: Vec<PolicyComponentMatch>,
}

pub fn ownership_for_path(input: OwnershipInput<'_>) -> Result<OwnershipReport> {
    let generated_at = Utc::now();
    let path = repo_relative_path(input.repo, input.path);
    let mut uncertainty = Vec::new();
    let mut owners = BTreeMap::<String, OwnerAggregate>::new();

    add_codeowners_evidence(
        input.repo,
        &path,
        generated_at,
        &mut owners,
        &mut uncertainty,
    );
    add_git_history_evidence(
        input.history,
        &path,
        generated_at,
        &mut owners,
        &mut uncertainty,
    );
    add_memory_evidence(
        input.memory_facts,
        generated_at,
        &mut owners,
        &mut uncertainty,
    );

    let suggestions = owner_suggestions(owners, &mut uncertainty);
    if suggestions.is_empty() {
        uncertainty.push(format!(
            "no owner suggestions found for `{}` from CODEOWNERS, git history, or repo memory",
            path.display()
        ));
    }
    if suggestions.iter().any(|suggestion| {
        suggestion
            .source_types
            .iter()
            .all(|source| *source == OwnershipSourceType::RepoMemory)
    }) {
        uncertainty.push(
            "memory-only ownership evidence is secondary and uncorroborated by CODEOWNERS or git history"
                .into(),
        );
    }

    Ok(OwnershipReport {
        path,
        components: input.components,
        generated_at,
        owners: suggestions,
        uncertainty,
    })
}

#[derive(Debug, Clone)]
struct CodeownersRule {
    file: PathBuf,
    line_number: usize,
    pattern: String,
    owners: Vec<Owner>,
}

#[derive(Debug, Clone)]
struct OwnerAggregate {
    owner: Owner,
    evidence: Vec<OwnershipEvidence>,
    codeowners: f32,
    git_history: f32,
    memory: f32,
}

impl OwnerAggregate {
    fn new(owner: Owner) -> Self {
        Self {
            owner,
            evidence: Vec::new(),
            codeowners: 0.0,
            git_history: 0.0,
            memory: 0.0,
        }
    }

    fn source_types(&self) -> Vec<OwnershipSourceType> {
        [
            OwnershipSourceType::Codeowners,
            OwnershipSourceType::GitHistory,
            OwnershipSourceType::RepoMemory,
        ]
        .into_iter()
        .filter(|source| {
            self.evidence
                .iter()
                .any(|evidence| evidence.source_type == *source)
        })
        .collect()
    }

    fn has_source(&self, source_type: OwnershipSourceType) -> bool {
        self.evidence
            .iter()
            .any(|evidence| evidence.source_type == source_type)
    }

    fn stale(&self) -> bool {
        !self.evidence.is_empty() && self.evidence.iter().all(|evidence| evidence.stale)
    }
}

fn add_codeowners_evidence(
    repo: &Path,
    path: &Path,
    generated_at: DateTime<Utc>,
    owners: &mut BTreeMap<String, OwnerAggregate>,
    uncertainty: &mut Vec<String>,
) {
    let rules = read_codeowners_rules(repo, uncertainty);
    if rules.is_empty() {
        uncertainty.push("no CODEOWNERS or owner config file was found".into());
        return;
    }

    let mut matched = None;
    for rule in &rules {
        match codeowners_pattern_matches(&rule.pattern, path) {
            Ok(true) => matched = Some(rule),
            Ok(false) => {}
            Err(err) => uncertainty.push(format!(
                "ignored invalid CODEOWNERS pattern `{}` in {}:{}: {err}",
                rule.pattern,
                rule.file.display(),
                rule.line_number
            )),
        }
    }

    let Some(rule) = matched else {
        uncertainty.push(format!(
            "CODEOWNERS files were present but no rule matched `{}`",
            path.display()
        ));
        return;
    };

    for owner in &rule.owners {
        let evidence = OwnershipEvidence {
            source_type: OwnershipSourceType::Codeowners,
            owner: owner.clone(),
            source: format!(
                "{}:{} `{}`",
                rule.file.display(),
                rule.line_number,
                rule.pattern
            ),
            message: format!(
                "CODEOWNERS rule `{}` matched `{}`",
                rule.pattern,
                path.display()
            ),
            confidence: Confidence::High,
            observed_at: Some(generated_at),
            stale: false,
        };
        add_evidence(
            owners,
            owner.clone(),
            evidence,
            OwnershipSourceType::Codeowners,
            CODEOWNERS_SCORE,
        );
    }
}

fn read_codeowners_rules(repo: &Path, uncertainty: &mut Vec<String>) -> Vec<CodeownersRule> {
    let mut rules = Vec::new();
    for candidate in CODEOWNERS_CANDIDATES {
        let path = repo.join(candidate);
        if !path.is_file() {
            continue;
        }
        let Ok(contents) = fs::read_to_string(&path) else {
            uncertainty.push(format!("could not read owner config `{}`", path.display()));
            continue;
        };
        for (index, raw_line) in contents.lines().enumerate() {
            let line_number = index + 1;
            let line = raw_line.trim();
            if line.is_empty() || line.starts_with('#') {
                continue;
            }
            let mut parts = line.split_whitespace();
            let Some(pattern) = parts.next() else {
                continue;
            };
            if pattern.starts_with('!') {
                uncertainty.push(format!(
                    "ignored unsupported negative CODEOWNERS pattern `{pattern}` in {}:{line_number}",
                    path.display()
                ));
                continue;
            }
            let mut rule_owners = Vec::new();
            for token in parts {
                if token.starts_with('#') {
                    break;
                }
                if let Some(owner) = owner_from_token(token) {
                    rule_owners.push(owner);
                }
            }
            if rule_owners.is_empty() {
                uncertainty.push(format!(
                    "ignored CODEOWNERS rule `{}` in {}:{} because it has no owner",
                    pattern,
                    path.display(),
                    line_number
                ));
                continue;
            }
            rules.push(CodeownersRule {
                file: PathBuf::from(candidate),
                line_number,
                pattern: pattern.to_string(),
                owners: rule_owners,
            });
        }
    }
    rules
}

fn codeowners_pattern_matches(pattern: &str, path: &Path) -> Result<bool> {
    let normalized_path = normalize_path_for_glob(path);
    for candidate in codeowners_globs(pattern) {
        let matcher = Glob::new(&candidate)
            .map_err(|err| open_kioku_errors::OkError::Config(err.to_string()))?
            .compile_matcher();
        if matcher.is_match(Path::new(&normalized_path)) {
            return Ok(true);
        }
    }
    Ok(false)
}

fn codeowners_globs(pattern: &str) -> Vec<String> {
    let anchored = pattern.starts_with('/');
    let directory = pattern.ends_with('/');
    let mut normalized = pattern
        .trim_start_matches('/')
        .trim_end_matches('/')
        .to_string();
    if normalized.is_empty() {
        normalized = "**".into();
    }
    if directory {
        normalized.push_str("/**");
    }

    let mut candidates = Vec::new();
    if anchored {
        candidates.push(normalized);
    } else {
        candidates.push(normalized.clone());
        candidates.push(format!("**/{normalized}"));
    }
    candidates.sort();
    candidates.dedup();
    candidates
}

fn add_git_history_evidence(
    history: &dyn HistoryStore,
    path: &Path,
    generated_at: DateTime<Utc>,
    owners: &mut BTreeMap<String, OwnerAggregate>,
    uncertainty: &mut Vec<String>,
) {
    let provenance = match history.provenance_for_path(path, HISTORY_LIMIT) {
        Ok(provenance) => provenance,
        Err(err) => {
            uncertainty.push(format!("git history ownership evidence unavailable: {err}"));
            return;
        }
    };
    uncertainty.extend(provenance.uncertainty.iter().cloned());
    if provenance.truncated {
        uncertainty.push(format!(
            "git history ownership evidence for `{}` is truncated at {HISTORY_LIMIT} touches",
            path.display()
        ));
    }

    let touches = unique_touches(&provenance.recent_touches);
    if touches.is_empty() {
        uncertainty.push(format!(
            "no git author touches were available for `{}`",
            path.display()
        ));
        return;
    }

    let total = touches.len() as f32;
    let mut by_owner = BTreeMap::<String, GitOwnerStats>::new();
    for touch in touches {
        let key = owner_key(&touch.commit.author);
        let entry = by_owner
            .entry(key)
            .or_insert_with(|| GitOwnerStats::new(touch.commit.author.clone()));
        entry.count += 1;
        entry.latest = entry.latest.max(Some(touch.commit.committed_at));
        entry.latest_commit = Some(touch.commit.id.0.clone());
        entry.latest_summary = Some(touch.commit.summary.clone());
    }

    for stats in by_owner.into_values() {
        let share = stats.count as f32 / total;
        let count_factor = 0.60 + ((stats.count as f32 / 3.0).min(1.0) * 0.40);
        let observed_at = stats.latest.unwrap_or(generated_at);
        let stale = is_stale(generated_at, observed_at);
        let freshness_multiplier = if stale { 0.55 } else { 1.0 };
        let git_score = ((0.30 + (0.32 * share)) * count_factor * freshness_multiplier).min(0.62);
        let message = format!(
            "{} authored {} of {} persisted touch(es) for `{}`; latest `{}`",
            stats.owner.name,
            stats.count,
            total as usize,
            path.display(),
            stats
                .latest_summary
                .as_deref()
                .unwrap_or("unknown commit summary")
        );
        let evidence = OwnershipEvidence {
            source_type: OwnershipSourceType::GitHistory,
            owner: stats.owner.clone(),
            source: format!(
                "git history:{}",
                stats.latest_commit.as_deref().unwrap_or("unknown")
            ),
            message,
            confidence: Confidence::from_score(git_score),
            observed_at: Some(observed_at),
            stale,
        };
        add_evidence(
            owners,
            stats.owner,
            evidence,
            OwnershipSourceType::GitHistory,
            git_score,
        );
    }
}

#[derive(Debug)]
struct GitOwnerStats {
    owner: Owner,
    count: usize,
    latest: Option<DateTime<Utc>>,
    latest_commit: Option<String>,
    latest_summary: Option<String>,
}

impl GitOwnerStats {
    fn new(owner: Owner) -> Self {
        Self {
            owner,
            count: 0,
            latest: None,
            latest_commit: None,
            latest_summary: None,
        }
    }
}

fn unique_touches(touches: &[ProvenanceTouch]) -> Vec<&ProvenanceTouch> {
    let mut seen = BTreeSet::new();
    let mut unique = Vec::new();
    for touch in touches {
        let key = format!(
            "{}:{}:{}",
            touch.commit.id.0,
            touch.path.display(),
            touch.qualified_name.as_deref().unwrap_or("<file>")
        );
        if seen.insert(key) {
            unique.push(touch);
        }
    }
    unique
}

fn add_memory_evidence(
    memory_facts: &[MemorySearchResult],
    generated_at: DateTime<Utc>,
    owners: &mut BTreeMap<String, OwnerAggregate>,
    uncertainty: &mut Vec<String>,
) {
    if memory_facts.is_empty() {
        uncertainty.push("no repo memory ownership facts matched this path".into());
        return;
    }

    let mut owner_hits = 0;
    for result in memory_facts {
        let fact_owners = memory_owner_tokens(&result.fact.text);
        if fact_owners.is_empty() {
            continue;
        }
        owner_hits += fact_owners.len();
        for owner in fact_owners {
            let stale = is_stale(generated_at, result.fact.created_at);
            let memory_score = ((0.08 + 0.10 * result.score.clamp(0.0, 1.0))
                * result.fact.confidence.score())
            .min(0.22);
            let evidence = OwnershipEvidence {
                source_type: OwnershipSourceType::RepoMemory,
                owner: owner.clone(),
                source: result.fact.id.0.clone(),
                message: format!(
                    "repo memory matched ownership terms: {}; source `{}`",
                    result.match_reason, result.fact.source
                ),
                confidence: Confidence::from_score(memory_score),
                observed_at: Some(result.fact.created_at),
                stale,
            };
            add_evidence(
                owners,
                owner,
                evidence,
                OwnershipSourceType::RepoMemory,
                memory_score,
            );
        }
    }

    if owner_hits == 0 {
        uncertainty.push(
            "repo memory matched this path but did not contain owner handles or email tokens"
                .into(),
        );
    }
}

fn add_evidence(
    owners: &mut BTreeMap<String, OwnerAggregate>,
    owner: Owner,
    evidence: OwnershipEvidence,
    source_type: OwnershipSourceType,
    score: f32,
) {
    let key = owner_key(&owner);
    let entry = owners
        .entry(key)
        .or_insert_with(|| OwnerAggregate::new(owner));
    match source_type {
        OwnershipSourceType::Codeowners => entry.codeowners = entry.codeowners.max(score),
        OwnershipSourceType::GitHistory => entry.git_history = entry.git_history.max(score),
        OwnershipSourceType::RepoMemory => entry.memory = (entry.memory + score).min(0.22),
    }
    entry.evidence.push(evidence);
}

fn owner_suggestions(
    owners: BTreeMap<String, OwnerAggregate>,
    uncertainty: &mut Vec<String>,
) -> Vec<OwnerSuggestion> {
    let mut drafts = owners
        .into_values()
        .map(|owner| {
            let freshness = if owner.evidence.iter().any(|evidence| !evidence.stale) {
                0.08
            } else {
                0.0
            };
            let mut raw_score =
                (owner.codeowners + owner.git_history + owner.memory + freshness).min(1.0);
            if !owner.has_source(OwnershipSourceType::Codeowners)
                && !owner.has_source(OwnershipSourceType::GitHistory)
            {
                raw_score = raw_score.min(MEMORY_ONLY_CAP);
            }
            SuggestionDraft {
                owner,
                freshness,
                raw_score,
                ambiguity_penalty: 0.0,
            }
        })
        .collect::<Vec<_>>();

    drafts.sort_by(compare_drafts);
    if let Some(top_score) = drafts.first().map(|draft| draft.raw_score) {
        let close_without_codeowners = drafts
            .iter()
            .filter(|draft| {
                !draft.owner.has_source(OwnershipSourceType::Codeowners)
                    && (top_score - draft.raw_score).abs() <= 0.08
            })
            .count();
        if close_without_codeowners > 1 {
            uncertainty.push(format!(
                "ownership is ambiguous across {close_without_codeowners} similarly scored non-CODEOWNERS owner candidates"
            ));
            for draft in &mut drafts {
                if !draft.owner.has_source(OwnershipSourceType::Codeowners)
                    && (top_score - draft.raw_score).abs() <= 0.08
                {
                    draft.ambiguity_penalty = 0.12;
                }
            }
        }
    }

    let mut suggestions = drafts
        .into_iter()
        .map(|draft| {
            let final_score = (draft.raw_score - draft.ambiguity_penalty).clamp(0.0, 1.0);
            let source_types = draft.owner.source_types();
            let stale = draft.owner.stale();
            let rationale = ownership_rationale(&source_types, stale);
            OwnerSuggestion {
                owner: draft.owner.owner,
                rationale,
                confidence: Confidence::from_score(final_score),
                score: final_score,
                source_types,
                stale,
                evidence: draft.owner.evidence,
                confidence_breakdown: OwnershipConfidenceBreakdown {
                    codeowners: draft.owner.codeowners,
                    git_history: draft.owner.git_history,
                    memory: draft.owner.memory,
                    freshness: draft.freshness,
                    ambiguity_penalty: draft.ambiguity_penalty,
                    final_score,
                },
            }
        })
        .collect::<Vec<_>>();
    suggestions.sort_by(compare_suggestions);
    suggestions
}

struct SuggestionDraft {
    owner: OwnerAggregate,
    freshness: f32,
    raw_score: f32,
    ambiguity_penalty: f32,
}

fn compare_drafts(left: &SuggestionDraft, right: &SuggestionDraft) -> Ordering {
    right
        .raw_score
        .partial_cmp(&left.raw_score)
        .unwrap_or(Ordering::Equal)
        .then_with(|| left.owner.owner.name.cmp(&right.owner.owner.name))
}

fn compare_suggestions(left: &OwnerSuggestion, right: &OwnerSuggestion) -> Ordering {
    right
        .score
        .partial_cmp(&left.score)
        .unwrap_or(Ordering::Equal)
        .then_with(|| left.owner.name.cmp(&right.owner.name))
}

fn ownership_rationale(source_types: &[OwnershipSourceType], stale: bool) -> String {
    let mut parts = Vec::new();
    if source_types.contains(&OwnershipSourceType::Codeowners) {
        parts.push("CODEOWNERS matched the queried path");
    }
    if source_types.contains(&OwnershipSourceType::GitHistory) {
        parts.push("local git history shows author touch evidence");
    }
    if source_types.contains(&OwnershipSourceType::RepoMemory) {
        parts.push("repo memory contributed secondary ownership evidence");
    }
    if stale {
        parts.push("all ownership evidence is stale");
    }
    if parts.is_empty() {
        "ownership evidence is unavailable".into()
    } else {
        parts.join("; ")
    }
}

fn memory_owner_tokens(text: &str) -> Vec<Owner> {
    let mut owners = Vec::new();
    let mut seen = BTreeSet::new();
    for token in text.split_whitespace() {
        if let Some(owner) = owner_from_token(token) {
            let key = owner_key(&owner);
            if seen.insert(key) {
                owners.push(owner);
            }
        }
    }
    owners
}

fn owner_from_token(token: &str) -> Option<Owner> {
    let cleaned = token.trim_matches(|ch: char| {
        matches!(
            ch,
            ',' | ';' | ':' | '.' | '(' | ')' | '[' | ']' | '{' | '}' | '<' | '>' | '"' | '\''
        )
    });
    if cleaned.len() < 2 {
        return None;
    }
    if cleaned.starts_with('@') && cleaned.len() > 1 {
        return Some(Owner {
            name: cleaned.to_string(),
            email: None,
        });
    }
    if looks_like_email(cleaned) {
        return Some(Owner {
            name: cleaned.to_string(),
            email: Some(cleaned.to_string()),
        });
    }
    None
}

fn looks_like_email(value: &str) -> bool {
    let Some((local, domain)) = value.split_once('@') else {
        return false;
    };
    !local.is_empty() && domain.contains('.') && !domain.starts_with('.') && !domain.ends_with('.')
}

fn owner_key(owner: &Owner) -> String {
    owner
        .email
        .as_deref()
        .unwrap_or(&owner.name)
        .trim_start_matches('@')
        .to_ascii_lowercase()
}

fn is_stale(generated_at: DateTime<Utc>, observed_at: DateTime<Utc>) -> bool {
    generated_at.signed_duration_since(observed_at) > Duration::days(STALE_AFTER_DAYS)
}

fn repo_relative_path(repo: &Path, path: &Path) -> PathBuf {
    if path.is_absolute() {
        let repo = repo.canonicalize().unwrap_or_else(|_| repo.to_path_buf());
        let path = path.canonicalize().unwrap_or_else(|_| path.to_path_buf());
        if let Ok(relative) = path.strip_prefix(repo) {
            return clean_relative_path(relative);
        }
        return clean_relative_path(&path);
    }
    clean_relative_path(path)
}

fn clean_relative_path(path: &Path) -> PathBuf {
    let mut cleaned = PathBuf::new();
    for component in path.components() {
        match component {
            Component::CurDir => {}
            Component::Normal(value) => cleaned.push(value),
            Component::ParentDir => cleaned.push(".."),
            Component::RootDir | Component::Prefix(_) => cleaned.push(component.as_os_str()),
        }
    }
    if cleaned.as_os_str().is_empty() {
        PathBuf::from(".")
    } else {
        cleaned
    }
}

fn normalize_path_for_glob(path: &Path) -> String {
    clean_relative_path(path)
        .to_string_lossy()
        .replace('\\', "/")
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::TimeZone;
    use open_kioku_core::{
        FileProvenance, GitChangeKind, GitCochangeEdge, GitCommitId, GitCommitRecord,
        HistorySnapshot, HistorySummary, MemoryFact, MemoryFactId, ProvenanceTouch,
    };
    use std::sync::Mutex;

    #[derive(Default)]
    struct StubHistoryStore {
        provenance: Mutex<Option<FileProvenance>>,
    }

    impl StubHistoryStore {
        fn with_provenance(provenance: FileProvenance) -> Self {
            Self {
                provenance: Mutex::new(Some(provenance)),
            }
        }
    }

    impl HistoryStore for StubHistoryStore {
        fn put_history_snapshot(&self, _snapshot: &HistorySnapshot) -> Result<()> {
            Ok(())
        }

        fn history_for_file(&self, path: &Path, _limit: usize) -> Result<HistorySummary> {
            Ok(HistorySummary::empty(path))
        }

        fn provenance_for_path(&self, path: &Path, _limit: usize) -> Result<FileProvenance> {
            Ok(self
                .provenance
                .lock()
                .unwrap()
                .clone()
                .unwrap_or_else(|| empty_provenance(path)))
        }

        fn cochange_neighbors(&self, _path: &Path, _limit: usize) -> Result<Vec<GitCochangeEdge>> {
            Ok(Vec::new())
        }

        fn recent_commits(&self, _limit: usize) -> Result<Vec<GitCommitRecord>> {
            Ok(Vec::new())
        }
    }

    #[test]
    fn codeowners_outranks_weak_memory_only_evidence() {
        let dir = tempfile::tempdir().unwrap();
        fs::create_dir_all(dir.path().join(".github")).unwrap();
        fs::write(
            dir.path().join(".github/CODEOWNERS"),
            "src/** @platform-team\n",
        )
        .unwrap();
        let history = StubHistoryStore::default();
        let memory = vec![memory_result(
            "src/a.rs owner @memory-team",
            Confidence::High,
        )];

        let report = ownership_for_path(OwnershipInput {
            repo: dir.path(),
            path: Path::new("src/a.rs"),
            history: &history,
            memory_facts: &memory,
            components: Vec::new(),
        })
        .unwrap();

        assert_eq!(report.owners[0].owner.name, "@platform-team");
        assert_eq!(report.owners[0].confidence, Confidence::High);
        assert!(report.owners[0]
            .source_types
            .contains(&OwnershipSourceType::Codeowners));
        let memory_owner = report
            .owners
            .iter()
            .find(|owner| owner.owner.name == "@memory-team")
            .unwrap();
        assert!(report.owners[0].score > memory_owner.score);
        assert_eq!(memory_owner.confidence, Confidence::Low);
    }

    #[test]
    fn source_mixing_raises_confidence_for_same_owner() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("CODEOWNERS"), "src/** dev@example.com\n").unwrap();
        let history = StubHistoryStore::with_provenance(FileProvenance {
            path: PathBuf::from("src/a.rs"),
            first_seen: None,
            last_touched: None,
            recent_touches: vec![touch("one", "Dev", "dev@example.com", 2026, 6, 1)],
            confidence: Confidence::High,
            truncated: false,
            uncertainty: Vec::new(),
        });
        let memory = vec![memory_result(
            "src/a.rs maintainer dev@example.com",
            Confidence::High,
        )];

        let report = ownership_for_path(OwnershipInput {
            repo: dir.path(),
            path: Path::new("src/a.rs"),
            history: &history,
            memory_facts: &memory,
            components: Vec::new(),
        })
        .unwrap();

        let owner = &report.owners[0];
        assert_eq!(owner.owner.email.as_deref(), Some("dev@example.com"));
        assert!(owner
            .source_types
            .contains(&OwnershipSourceType::Codeowners));
        assert!(owner
            .source_types
            .contains(&OwnershipSourceType::GitHistory));
        assert!(owner
            .source_types
            .contains(&OwnershipSourceType::RepoMemory));
        assert!(owner.score >= 0.95);
    }

    #[test]
    fn stale_ambiguous_git_history_is_not_authoritative() {
        let dir = tempfile::tempdir().unwrap();
        let history = StubHistoryStore::with_provenance(FileProvenance {
            path: PathBuf::from("src/a.rs"),
            first_seen: None,
            last_touched: None,
            recent_touches: vec![
                touch("one", "Old One", "one@example.com", 2020, 1, 1),
                touch("two", "Old Two", "two@example.com", 2020, 1, 2),
            ],
            confidence: Confidence::Medium,
            truncated: false,
            uncertainty: Vec::new(),
        });

        let report = ownership_for_path(OwnershipInput {
            repo: dir.path(),
            path: Path::new("src/a.rs"),
            history: &history,
            memory_facts: &[],
            components: Vec::new(),
        })
        .unwrap();

        assert_eq!(report.owners.len(), 2);
        assert!(report.owners.iter().all(|owner| owner.stale));
        assert!(report
            .owners
            .iter()
            .all(|owner| owner.confidence == Confidence::Low));
        assert!(report
            .uncertainty
            .iter()
            .any(|note| note.contains("ambiguous")));
    }

    #[test]
    fn missing_ownership_returns_uncertainty_without_fabricating_owner() {
        let dir = tempfile::tempdir().unwrap();
        let history = StubHistoryStore::default();

        let report = ownership_for_path(OwnershipInput {
            repo: dir.path(),
            path: Path::new("src/a.rs"),
            history: &history,
            memory_facts: &[],
            components: Vec::new(),
        })
        .unwrap();

        assert!(report.owners.is_empty());
        assert!(report
            .uncertainty
            .iter()
            .any(|note| note.contains("no owner suggestions")));
    }

    #[test]
    fn invalid_codeowners_pattern_is_reported_as_uncertainty() {
        let dir = tempfile::tempdir().unwrap();
        fs::write(dir.path().join("CODEOWNERS"), "[ @team\n").unwrap();
        let history = StubHistoryStore::default();

        let report = ownership_for_path(OwnershipInput {
            repo: dir.path(),
            path: Path::new("src/a.rs"),
            history: &history,
            memory_facts: &[],
            components: Vec::new(),
        })
        .unwrap();

        assert!(report
            .uncertainty
            .iter()
            .any(|note| note.contains("invalid CODEOWNERS pattern")));
    }

    fn empty_provenance(path: &Path) -> FileProvenance {
        FileProvenance {
            path: path.to_path_buf(),
            first_seen: None,
            last_touched: None,
            recent_touches: Vec::new(),
            confidence: Confidence::Low,
            truncated: false,
            uncertainty: Vec::new(),
        }
    }

    fn touch(
        id: &str,
        name: &str,
        email: &str,
        year: i32,
        month: u32,
        day: u32,
    ) -> ProvenanceTouch {
        let timestamp = Utc
            .with_ymd_and_hms(year, month, day, 12, 0, 0)
            .single()
            .unwrap();
        ProvenanceTouch {
            commit: GitCommitRecord {
                id: GitCommitId::new(id),
                parent_ids: Vec::new(),
                author: Owner {
                    name: name.into(),
                    email: Some(email.into()),
                },
                committer: None,
                authored_at: timestamp,
                committed_at: timestamp,
                summary: format!("commit {id}"),
                message: format!("commit {id}"),
                file_count: 1,
            },
            path: PathBuf::from("src/a.rs"),
            previous_path: None,
            symbol_id: None,
            qualified_name: None,
            change_kind: GitChangeKind::Modified,
            line_ranges: Vec::new(),
            confidence: Confidence::High,
            uncertainty: Vec::new(),
        }
    }

    fn memory_result(text: &str, confidence: Confidence) -> MemorySearchResult {
        MemorySearchResult {
            fact: MemoryFact {
                id: MemoryFactId::new(format!("memory:{}", text.len())),
                text: text.into(),
                source: "test".into(),
                confidence,
                entities: Vec::new(),
                created_at: Utc::now(),
            },
            score: 0.50,
            match_reason: "test memory match".into(),
            evidence: vec!["test".into()],
        }
    }
}