minutes-core 0.20.0

Core library for minutes — audio capture, transcription, and meeting memory
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
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
use crate::knowledge::slugify;
use crate::markdown::EntityRef;
use std::collections::{HashMap, HashSet};

/// Role and title descriptors that must not contaminate a person's canonical identity.
///
/// The LLM occasionally appends role context when extracting entities from transcripts
/// (e.g. "Junlei, tech lead" → should produce slug `junlei`, not `junlei-tech-lead`).
/// Listed longest-first so more specific phrases are stripped before shorter sub-phrases.
const ROLE_TITLE_SUFFIXES: &[&str] = &[
    "engineering manager",
    "technical lead",
    "engineering lead",
    "product manager",
    "project manager",
    "product lead",
    "design lead",
    "senior engineer",
    "lead engineer",
    "principal engineer",
    "backend engineer",
    "frontend engineer",
    "software engineer",
    "core team",
    "team member",
    "team lead",
    "tech lead",
];

#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct PersonIdentity {
    pub slug: String,
    pub name: String,
    pub aliases: Vec<String>,
}

#[derive(Clone, Debug)]
struct PersonCandidate {
    identity: PersonIdentity,
    alias_score: usize,
    support_score: usize,
}

#[derive(Clone, Debug, Default)]
pub(crate) struct PersonCanonicalizer {
    exact_matches: HashMap<String, Vec<usize>>,
    slug_matches: HashMap<String, Vec<usize>>,
    candidates: Vec<PersonCandidate>,
}

impl PersonCanonicalizer {
    pub(crate) fn new<I, S>(entities: &[EntityRef], context_names: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: AsRef<str>,
    {
        let mut canonicalizer = Self::default();

        for entity in entities {
            let Some(identity) = normalize_entity_identity(entity) else {
                continue;
            };

            let exact_keys = exact_keys_for_entity(entity);
            let slug_keys = slug_keys_for_entity(entity);

            let alias_score = exact_keys.len().max(slug_keys.len());
            let idx = canonicalizer.candidates.len();
            canonicalizer.candidates.push(PersonCandidate {
                identity,
                alias_score,
                support_score: 1,
            });

            for key in exact_keys {
                canonicalizer
                    .exact_matches
                    .entry(key)
                    .or_default()
                    .push(idx);
            }
            for key in slug_keys {
                canonicalizer.slug_matches.entry(key).or_default().push(idx);
            }
        }

        let context_values: Vec<String> = context_names
            .into_iter()
            .filter_map(|raw| normalize_raw_name(raw.as_ref()).map(|(_, name)| name.to_string()))
            .collect();

        for raw in context_values {
            let exact = canonicalizer.lookup_exact(&raw);
            if let Some(idx) = canonicalizer.pick_best_index(exact) {
                canonicalizer.candidates[idx].support_score += 1;
                continue;
            }

            let slug = slugify(&raw);
            if slug.is_empty() {
                continue;
            }

            if let Some(idx) = canonicalizer.pick_best_index(canonicalizer.lookup_slug(&slug)) {
                canonicalizer.candidates[idx].support_score += 1;
            }
        }

        canonicalizer
    }

    pub(crate) fn resolve(&self, raw: &str) -> Option<PersonIdentity> {
        let (_, trimmed) = normalize_raw_name(raw)?;

        if let Some(idx) = self.pick_best_index(self.lookup_exact(trimmed)) {
            return Some(self.candidates[idx].identity.clone());
        }

        let slug = slugify(trimmed);
        if slug.is_empty() {
            return None;
        }

        if let Some(idx) = self.pick_best_index(self.lookup_slug(&slug)) {
            return Some(self.candidates[idx].identity.clone());
        }

        Some(PersonIdentity {
            slug,
            name: trimmed.to_string(),
            aliases: vec![],
        })
    }

    pub(crate) fn resolve_entity(&self, entity: &EntityRef) -> Option<PersonIdentity> {
        if let Some(identity) = self.resolve(&entity.label) {
            return Some(identity);
        }
        if let Some(identity) = self.resolve(&entity.slug) {
            return Some(identity);
        }
        normalize_entity_identity(entity)
    }

    fn lookup_exact<'a>(&'a self, raw: &str) -> &'a [usize] {
        self.exact_matches
            .get(&raw.to_ascii_lowercase())
            .map(Vec::as_slice)
            .unwrap_or(&[])
    }

    fn lookup_slug<'a>(&'a self, slug: &str) -> &'a [usize] {
        self.slug_matches
            .get(slug)
            .map(Vec::as_slice)
            .unwrap_or(&[])
    }

    fn pick_best_index(&self, indices: &[usize]) -> Option<usize> {
        let mut best_idx: Option<usize> = None;
        let mut best_support = 0usize;
        let mut best_alias = 0usize;
        let mut ambiguous = false;

        for &idx in indices {
            let candidate = &self.candidates[idx];
            let support = candidate.support_score;
            let alias = candidate.alias_score;

            match best_idx {
                None => {
                    best_idx = Some(idx);
                    best_support = support;
                    best_alias = alias;
                }
                Some(_) if ambiguous => {
                    if support > best_support && alias > best_alias {
                        best_idx = Some(idx);
                        best_support = support;
                        best_alias = alias;
                        ambiguous = false;
                    }
                }
                Some(_) => {
                    if support > best_support || (support == best_support && alias > best_alias) {
                        best_idx = Some(idx);
                        best_support = support;
                        best_alias = alias;
                    } else if support == best_support && alias == best_alias {
                        ambiguous = true;
                    }
                }
            }
        }

        if ambiguous {
            None
        } else {
            best_idx
        }
    }
}

fn normalize_raw_name(raw: &str) -> Option<(&str, &str)> {
    let trimmed = raw.trim().trim_start_matches('@').trim();
    if trimmed.is_empty() {
        None
    } else {
        Some((raw, trimmed))
    }
}

/// Returns true if `fragment` (the text after a separator) is a known role or title.
///
/// Normalizes hyphens to spaces and strips leading articles ("the", "a", "an") before
/// matching, so "the core team" and "core-team" both match "core team".
fn trailing_fragment_is_role(fragment: &str) -> bool {
    let normalized: String = fragment
        .to_lowercase()
        .chars()
        .map(|c| if c == '-' { ' ' } else { c })
        .collect();
    let text = normalized
        .trim()
        .trim_start_matches("the ")
        .trim_start_matches("a ")
        .trim_start_matches("an ");
    ROLE_TITLE_SUFFIXES.iter().any(|&role| text.contains(role))
}

/// Strip a trailing role or title descriptor from a person name string.
///
/// Each structural separator (comma, parenthetical, dash, connectors) only fires when
/// the trailing fragment actually contains a [`ROLE_TITLE_SUFFIXES`] token, avoiding
/// false positives on nicknames (`"Robert (Bob) Smith"`), generational suffixes
/// (`"Sammy Davis, Jr."`), or names with connective words (`"Winnie the Pooh"`).
///
/// Examples:
/// - `"Junlei, tech lead"` → `"Junlei"`
/// - `"Junrei (core team)"` → `"Junrei"`
/// - `"Sam the tech lead"` → `"Sam"`
/// - `"Junrei from the core team"` → `"Junrei"`
/// - `"Alex — engineering lead"` → `"Alex"`
/// - `"Junlei Tech Lead"` → `"Junlei"`
/// - `"junlei-tech-lead"` → `"junlei"`
/// - `"Robert (Bob) Smith"` → `"Robert (Bob) Smith"` (unchanged)
/// - `"Sammy Davis, Jr."` → `"Sammy Davis, Jr."` (unchanged)
/// - `"Winnie the Pooh"` → `"Winnie the Pooh"` (unchanged)
/// - `"Dan Benamoz"` → `"Dan Benamoz"` (unchanged)
pub(crate) fn strip_role_suffix(name: &str) -> &str {
    // 1. Comma: "Name, role" — only when what follows is a known role.
    if let Some(pos) = name.find(", ") {
        if trailing_fragment_is_role(&name[pos + 2..]) {
            return name[..pos].trim();
        }
    }
    // 2. Parenthetical at end: "Name (role)" — the ends_with(')') guard excludes
    // "Name (nickname) Surname" patterns where the paren is not terminal.
    if name.ends_with(')') {
        if let Some(pos) = name.find(" (") {
            let inside = name[pos + 2..name.len() - 1].trim();
            if trailing_fragment_is_role(inside) {
                return name[..pos].trim();
            }
        }
    }
    // 3. Spaced dash variants: "Name — role", "Name – role", "Name - role"
    for sep in ["", "", " - "] {
        if let Some(pos) = name.find(sep) {
            if trailing_fragment_is_role(&name[pos + sep.len()..]) {
                return name[..pos].trim();
            }
        }
    }
    // 4. Connective words before a known role descriptor.
    for connector in [" from ", " the "] {
        if let Some(pos) = name.find(connector) {
            let before = name[..pos].trim();
            if !before.is_empty() && trailing_fragment_is_role(&name[pos + connector.len()..]) {
                return before;
            }
        }
    }
    // 5. Vocabulary-based suffix: "Junlei Tech Lead" or "junlei-tech-lead".
    // Normalize hyphens → spaces so slug-form and label-form are treated equally.
    let normalized: String = name
        .to_lowercase()
        .chars()
        .map(|c| if c == '-' { ' ' } else { c })
        .collect();
    for &role in ROLE_TITLE_SUFFIXES {
        if !normalized.ends_with(role) || normalized.len() <= role.len() {
            continue;
        }
        let split = normalized.len() - role.len();
        // Word boundary: the character before the role suffix must be whitespace.
        if normalized.as_bytes().get(split.saturating_sub(1)) != Some(&b' ') {
            continue;
        }
        // Guard: split must be a valid char boundary in the original string.
        // Since '-' and ' ' are both single ASCII bytes the lengths match, but
        // non-ASCII chars that change byte count on lowercasing would invalidate this.
        if !name.is_char_boundary(split) {
            continue;
        }
        let candidate = name[..split].trim_end_matches(|c: char| c == '-' || c.is_whitespace());
        if !candidate.is_empty() {
            return candidate;
        }
    }
    name
}

/// Curated, exact-match list of non-person "names" that must never become a person
/// entity. Deliberately NOT heuristic (no length or initials rules) so genuine short
/// names like "An", "Jo", "Hai" are never dropped — losing a real attendee is as
/// harmful as keeping a contaminated entry. Matched after normalization (lowercased,
/// hyphens -> spaces, whitespace collapsed) and after role/speaker stripping (#385).
const NON_PERSON_NAMES: &[&str] = &[
    "all",
    "none",
    "none identified",
    "not identified",
    "unknown",
    "unknown speaker",
    "unnamed",
    "unassigned",
    "everyone",
    "everybody",
    "anyone",
    "someone",
    "somebody",
    "nobody",
    "no one",
    "team",
    "the team",
    "team member",
    "team members",
    "group",
    "the group",
    "others",
    "attendee",
    "attendees",
    "participant",
    "participants",
    "speaker",
    "speakers",
    // Multi-word role/group phrases (never a real person name), plus a few
    // clearly-non-person single words. Single words that double as real
    // given names or surnames (dev, guest, host, owner, engineer, manager,
    // member, ...) are deliberately NOT listed — dropping a real attendee is
    // as harmful as keeping a contaminated entry (#385).
    "devops",
    "chatbot",
    "qa engineer",
    "chatbot owner",
    "chatbot owner developer",
    "new member",
    "new qa member",
    "additional devops team members",
];

/// Normalize a name for non-person matching: lowercase, hyphens -> spaces, collapse
/// whitespace.
fn normalize_for_match(name: &str) -> String {
    name.trim()
        .to_lowercase()
        .chars()
        .map(|c| if c == '-' { ' ' } else { c })
        .collect::<String>()
        .split_whitespace()
        .collect::<Vec<_>>()
        .join(" ")
}

/// True if `name` is plausibly a person, not a generic/group/role token.
///
/// Conservative exact-match denylist: only the explicitly listed non-person tokens
/// are rejected, so anything ambiguous (including short real names) is kept. Apply
/// after [`strip_role_suffix`] / [`strip_speaker_label`] so e.g. "Junlei Tech Lead"
/// has already become "Junlei" before this check.
/// Group/role marker tokens used only for the multi-word all-markers rule below.
/// Safe to be broad here (including name-colliding words like "dev"/"owner") because
/// the rule requires EVERY token to be a marker, and a real multi-word person name
/// always carries at least one non-marker token ("Mark Engineer", "An Le").
const GROUP_ROLE_TOKENS: &[&str] = &[
    "team",
    "teams",
    "group",
    "groups",
    "devops",
    "ops",
    "qa",
    "member",
    "members",
    "new",
    "additional",
    "the",
    "a",
    "an",
    "engineering",
    "engineer",
    "developer",
    "dev",
    "owner",
    "manager",
    "lead",
    "leads",
    "staff",
    "crew",
    "admin",
    "intern",
    "analyst",
    "designer",
    "consultant",
    "chatbot",
    "bot",
    "others",
    "attendees",
    "participants",
    "speaker",
    "speakers",
    "unknown",
    "unnamed",
    "unassigned",
];

pub(crate) fn is_plausible_person_name(name: &str) -> bool {
    let normalized = normalize_for_match(name);
    if normalized.is_empty() || NON_PERSON_NAMES.contains(&normalized.as_str()) {
        return false;
    }
    // Reject multi-word phrases that are entirely group/role markers ("qa team",
    // "devops team members"). Single tokens are governed only by the exact denylist
    // above, so real one-word names (Dev, Owner, An) survive (#385).
    let tokens: Vec<&str> = normalized.split(' ').collect();
    if tokens.len() >= 2 && tokens.iter().all(|t| GROUP_ROLE_TOKENS.contains(t)) {
        return false;
    }
    true
}

/// Strip a trailing diarization speaker label ("speaker 0", "-speaker-2") from a name,
/// in slug or label form, leaving the rest of the name intact (#385).
///
/// - `"Geert speaker 0"` -> `"Geert"`
/// - `"geert-speaker-0"` -> `"geert"`
/// - `"gert anne speaker 2"` -> `"gert anne"`
/// - `"speaker-1"` / `"speaker 0"` -> `""` (caller treats empty as non-person)
/// - `"Dan Benamoz"` / `"Catch 22"` -> unchanged (no trailing speaker token)
pub(crate) fn strip_speaker_label(name: &str) -> &str {
    // Scan the ORIGINAL bytes (not a lowercased/normalized copy) so byte offsets
    // can never drift on non-ASCII names (e.g. "İ" lowercasing to two chars). The
    // label tokens ("speaker", digits, "-"/" ") are all ASCII, so ASCII-only
    // comparison on the original is exact, and the cut index lands on a char
    // boundary because the byte before it is an ASCII separator (or string start).
    let b = name.as_bytes();
    let mut end = b.len();
    while end > 0 && b[end - 1].is_ascii_whitespace() {
        end -= 1;
    }
    // Require a trailing run of digits...
    let mut i = end;
    while i > 0 && b[i - 1].is_ascii_digit() {
        i -= 1;
    }
    if i == end {
        return name;
    }
    // ...preceded by optional spaces/hyphens and the word "speaker".
    let mut j = i;
    while j > 0 && (b[j - 1] == b' ' || b[j - 1] == b'-') {
        j -= 1;
    }
    const KW: &[u8] = b"speaker";
    if j < KW.len() || !b[j - KW.len()..j].eq_ignore_ascii_case(KW) {
        return name;
    }
    let start = j - KW.len();
    // "speaker" must be at a word boundary: string start or an ASCII space/hyphen.
    if start != 0 {
        let prev = b[start - 1];
        if prev != b' ' && prev != b'-' {
            return name;
        }
    }
    name[..start].trim_end_matches(|c: char| c == '-' || c.is_whitespace())
}

/// Strip both role/title suffixes and trailing speaker labels, to a fixpoint.
///
/// A single pass is order-sensitive ("Junlei Tech Lead speaker 0" needs the speaker
/// label removed before the role becomes trailing) and not idempotent for stacked
/// labels ("Geert speaker 0 speaker 1"). Iterating until stable handles both and
/// guarantees `strip_contamination(strip_contamination(x)) == strip_contamination(x)`.
pub(crate) fn strip_contamination(name: &str) -> &str {
    let mut cur = name.trim();
    loop {
        let next = strip_speaker_label(strip_role_suffix(cur)).trim();
        if next.len() == cur.len() {
            return next;
        }
        cur = next;
    }
}

fn normalize_entity_identity(entity: &EntityRef) -> Option<PersonIdentity> {
    // Role/title descriptors ("tech lead"), diarization speaker labels
    // ("speaker 0"), and generic/group tokens ("team", "qa engineer") must not
    // become or contaminate a person identity. Strip then gate before slugging (#385).
    let raw = if entity.label.trim().is_empty() {
        entity.slug.trim()
    } else {
        entity.label.trim()
    };
    let name = strip_contamination(raw).to_string();
    if !is_plausible_person_name(&name) {
        return None;
    }
    let slug = slugify(&name);
    if slug.is_empty() {
        return None;
    }

    Some(PersonIdentity {
        slug,
        name,
        aliases: unique_aliases(entity.aliases.iter().cloned()),
    })
}

fn exact_keys_for_entity(entity: &EntityRef) -> HashSet<String> {
    let mut keys = HashSet::new();

    for value in [entity.slug.trim(), entity.label.trim()]
        .into_iter()
        .chain(entity.aliases.iter().map(|a| a.trim()))
    {
        if value.is_empty() {
            continue;
        }
        keys.insert(value.to_ascii_lowercase());
        // Also index the stripped form so clean lookups ("Junlei") resolve to the entity
        // even when the stored label is "Junlei, tech lead".
        let stripped = strip_role_suffix(value);
        if stripped != value {
            keys.insert(stripped.to_ascii_lowercase());
        }
    }

    keys
}

fn slug_keys_for_entity(entity: &EntityRef) -> HashSet<String> {
    let mut keys = HashSet::new();

    for value in std::iter::once(entity.slug.as_str())
        .chain(std::iter::once(entity.label.as_str()))
        .chain(entity.aliases.iter().map(String::as_str))
    {
        let slug = slugify(value);
        if !slug.is_empty() {
            keys.insert(slug);
        }
        // Also index the role-stripped slug so clean-form lookups ("Junlei") still
        // resolve to the entity when the stored label is "Junlei Tech Lead".
        let stripped = strip_role_suffix(value.trim());
        if stripped != value.trim() {
            let stripped_slug = slugify(stripped);
            if !stripped_slug.is_empty() {
                keys.insert(stripped_slug);
            }
        }
    }

    keys
}

fn unique_aliases<I>(aliases: I) -> Vec<String>
where
    I: IntoIterator<Item = String>,
{
    let mut seen = HashSet::new();
    let mut out = Vec::new();
    for alias in aliases {
        let trimmed = alias.trim();
        if trimmed.is_empty() {
            continue;
        }

        let key = trimmed.to_ascii_lowercase();
        if seen.insert(key) {
            out.push(trimmed.to_string());
        }
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;

    fn dan_entities() -> Vec<EntityRef> {
        vec![EntityRef {
            slug: "dan-benamoz".into(),
            label: "Dan Benamoz".into(),
            aliases: vec!["Dan".into(), "dan".into()],
        }]
    }

    #[test]
    fn resolves_raw_name_through_alias_table() {
        let resolver = PersonCanonicalizer::new(&dan_entities(), ["Dan"]);
        let identity = resolver.resolve("Dan").expect("resolved identity");
        assert_eq!(identity.slug, "dan-benamoz");
        assert_eq!(identity.name, "Dan Benamoz");
    }

    #[test]
    fn falls_back_to_raw_slug_when_no_entity_matches() {
        let resolver = PersonCanonicalizer::new(&[], ["Dan"]);
        let identity = resolver.resolve("Dan").expect("fallback identity");
        assert_eq!(identity.slug, "dan");
        assert_eq!(identity.name, "Dan");
    }

    #[test]
    fn chooses_stronger_context_when_aliases_collide() {
        let resolver = PersonCanonicalizer::new(
            &[
                EntityRef {
                    slug: "dan-benamoz".into(),
                    label: "Dan Benamoz".into(),
                    aliases: vec!["Dan".into(), "DB".into(), "Daniel".into()],
                },
                EntityRef {
                    slug: "dan-smith".into(),
                    label: "Dan Smith".into(),
                    aliases: vec!["Dan".into()],
                },
            ],
            ["Dan", "Dan Benamoz", "DB"],
        );

        let identity = resolver.resolve("Dan").expect("collision resolution");
        assert_eq!(identity.slug, "dan-benamoz");
    }

    #[test]
    fn case_insensitive_matching_works() {
        let resolver = PersonCanonicalizer::new(&dan_entities(), ["DAN"]);
        let identity = resolver.resolve("DAN").expect("case-insensitive identity");
        assert_eq!(identity.slug, "dan-benamoz");
    }

    #[test]
    fn ambiguous_collision_without_stronger_signal_falls_back() {
        let resolver = PersonCanonicalizer::new(
            &[
                EntityRef {
                    slug: "dan-benamoz".into(),
                    label: "Dan Benamoz".into(),
                    aliases: vec!["Dan".into()],
                },
                EntityRef {
                    slug: "dan-smith".into(),
                    label: "Dan Smith".into(),
                    aliases: vec!["Dan".into()],
                },
            ],
            ["Dan"],
        );

        let identity = resolver.resolve("Dan").expect("ambiguous fallback");
        assert_eq!(identity.slug, "dan");
        assert_eq!(identity.name, "Dan");
    }

    fn candidate(alias_score: usize, support_score: usize) -> PersonCandidate {
        PersonCandidate {
            identity: PersonIdentity {
                slug: format!("candidate-{alias_score}-{support_score}"),
                name: format!("Candidate {alias_score}/{support_score}"),
                aliases: vec![],
            },
            alias_score,
            support_score,
        }
    }

    #[test]
    fn pick_best_index_keeps_ambiguity_latched_after_equal_top_tie() {
        let canonicalizer = PersonCanonicalizer {
            candidates: vec![candidate(2, 1), candidate(2, 1), candidate(3, 1)],
            ..Default::default()
        };

        assert_eq!(canonicalizer.pick_best_index(&[0, 1, 2]), None);
    }

    #[test]
    fn pick_best_index_returns_strictly_higher_scoring_candidate() {
        let canonicalizer = PersonCanonicalizer {
            candidates: vec![candidate(2, 1), candidate(2, 1), candidate(3, 2)],
            ..Default::default()
        };

        assert_eq!(canonicalizer.pick_best_index(&[0, 1, 2]), Some(2));
    }

    #[test]
    fn pick_best_index_returns_none_when_all_top_candidates_tie() {
        let canonicalizer = PersonCanonicalizer {
            candidates: vec![candidate(2, 1), candidate(2, 1), candidate(2, 1)],
            ..Default::default()
        };

        assert_eq!(canonicalizer.pick_best_index(&[0, 1, 2]), None);
    }

    // ── strip_role_suffix ────────────────────────────────────────

    #[test]
    fn strip_role_suffix_comma_separator() {
        assert_eq!(strip_role_suffix("Junlei, tech lead"), "Junlei");
        assert_eq!(strip_role_suffix("Junlei, the tech lead"), "Junlei");
    }

    #[test]
    fn strip_role_suffix_parenthetical() {
        assert_eq!(strip_role_suffix("Junrei (core team)"), "Junrei");
        assert_eq!(strip_role_suffix("Alex (engineering lead)"), "Alex");
    }

    #[test]
    fn strip_role_suffix_em_dash() {
        assert_eq!(strip_role_suffix("Alex — engineering lead"), "Alex");
        assert_eq!(strip_role_suffix("Sam – product manager"), "Sam");
    }

    #[test]
    fn strip_role_suffix_spaced_dash() {
        assert_eq!(strip_role_suffix("Alex - tech lead"), "Alex");
        assert_eq!(strip_role_suffix("Sam - product manager"), "Sam");
    }

    #[test]
    fn strip_role_suffix_from_connector() {
        assert_eq!(strip_role_suffix("Junrei from the core team"), "Junrei");
        // "engineering" alone is not a role token — must not strip.
        assert_eq!(
            strip_role_suffix("Sam from engineering"),
            "Sam from engineering"
        );
    }

    // ── false-positive guard tests (requested in silverstein/minutes#374) ─────

    #[test]
    fn strip_role_suffix_nickname_in_parens_left_intact() {
        // Parenthetical nickname with surname: "Robert (Bob) Smith" must not lose the surname.
        assert_eq!(
            strip_role_suffix("Robert (Bob) Smith"),
            "Robert (Bob) Smith"
        );
        assert_eq!(
            strip_role_suffix("Mike (Michael) Johnson"),
            "Mike (Michael) Johnson"
        );
    }

    #[test]
    fn strip_role_suffix_generational_suffix_left_intact() {
        // Generational and credential suffixes after a comma must not be stripped.
        assert_eq!(strip_role_suffix("Sammy Davis, Jr."), "Sammy Davis, Jr.");
        assert_eq!(strip_role_suffix("Jane Doe, PhD"), "Jane Doe, PhD");
    }

    #[test]
    fn strip_role_suffix_the_in_name_left_intact() {
        // "the" connector must only fire when a known role follows.
        assert_eq!(strip_role_suffix("Winnie the Pooh"), "Winnie the Pooh");
        assert_eq!(
            strip_role_suffix("Alexander the Great"),
            "Alexander the Great"
        );
    }

    #[test]
    fn strip_role_suffix_the_connector() {
        assert_eq!(strip_role_suffix("Sam the tech lead"), "Sam");
    }

    #[test]
    fn strip_role_suffix_vocabulary_label_form() {
        assert_eq!(strip_role_suffix("Junlei Tech Lead"), "Junlei");
        assert_eq!(strip_role_suffix("Junrei Core Team"), "Junrei");
        assert_eq!(strip_role_suffix("Alex Senior Engineer"), "Alex");
        assert_eq!(strip_role_suffix("Pat Engineering Manager"), "Pat");
    }

    #[test]
    fn strip_role_suffix_vocabulary_slug_form() {
        assert_eq!(strip_role_suffix("junlei-tech-lead"), "junlei");
        assert_eq!(strip_role_suffix("junrei-core-team"), "junrei");
        assert_eq!(strip_role_suffix("alex-senior-engineer"), "alex");
    }

    #[test]
    fn strip_role_suffix_clean_name_untouched() {
        assert_eq!(strip_role_suffix("Dan Benamoz"), "Dan Benamoz");
        assert_eq!(strip_role_suffix("Sarah Chen"), "Sarah Chen");
        assert_eq!(strip_role_suffix("Jordan Mills"), "Jordan Mills");
        assert_eq!(strip_role_suffix("dan-benamoz"), "dan-benamoz");
    }

    // ── speaker-label stripping (#385) ──────────────────────────

    #[test]
    fn strip_speaker_label_removes_trailing_label() {
        assert_eq!(strip_speaker_label("Geert speaker 0"), "Geert");
        assert_eq!(strip_speaker_label("geert-speaker-0"), "geert");
        assert_eq!(strip_speaker_label("Tanya Speaker 1"), "Tanya");
        assert_eq!(strip_speaker_label("gert anne speaker 2"), "gert anne");
    }

    #[test]
    fn strip_speaker_label_bare_label_becomes_empty() {
        assert_eq!(strip_speaker_label("speaker-1"), "");
        assert_eq!(strip_speaker_label("speaker 0"), "");
        assert_eq!(strip_speaker_label("Speaker 4"), "");
    }

    #[test]
    fn strip_speaker_label_leaves_clean_names_and_trailing_numbers() {
        assert_eq!(strip_speaker_label("Dan Benamoz"), "Dan Benamoz");
        assert_eq!(strip_speaker_label("dan-benamoz"), "dan-benamoz");
        // a trailing number that is not a speaker label is untouched
        assert_eq!(strip_speaker_label("Catch 22"), "Catch 22");
        assert_eq!(strip_speaker_label("Studio 54"), "Studio 54");
    }

    // ── non-person gate (#385) ──────────────────────────────────

    #[test]
    fn is_plausible_person_name_rejects_generic_and_role_tokens() {
        for bad in [
            "all",
            "team",
            "unassigned",
            "none-identified",
            "unknown",
            "unnamed",
            "speaker",
            "qa-engineer",
            "devops",
            "chatbot-owner",
            "chatbot-owner-developer",
            "new-qa-member",
            "additional-devops-team-members",
            "The Team",
            "PARTICIPANTS",
        ] {
            assert!(!is_plausible_person_name(bad), "should reject {bad:?}");
        }
    }

    #[test]
    fn is_plausible_person_name_keeps_real_short_and_compound_names() {
        // genuine short (often non-Western) names must never be dropped, and single
        // words that double as real given names / surnames must survive too (#385).
        for ok in [
            "An",
            "Jo",
            "Hai",
            "Tao",
            "Jia",
            "Geert",
            "Dan Benamoz",
            "Mark Engineer",
            "Dev",
            "Guest",
            "Host",
            "Owner",
            "Engineer",
            "Manager",
        ] {
            assert!(is_plausible_person_name(ok), "should keep {ok:?}");
        }
    }

    #[test]
    fn is_plausible_person_name_rejects_group_phrase_variants() {
        // multi-word phrases that are all group/role markers (#385 follow-on)
        for bad in [
            "qa team",
            "devops team",
            "devops team members",
            "new qa members",
            "additional devops team member",
            "the team",
        ] {
            assert!(!is_plausible_person_name(bad), "should reject {bad:?}");
        }
        // but real multi-word names with a genuine name token survive
        for ok in [
            "Mark Engineer",
            "Dev Patel",
            "An Le",
            "Dan Benamoz",
            "Owner Mcowner",
        ] {
            assert!(is_plausible_person_name(ok), "should keep {ok:?}");
        }
    }

    #[test]
    fn strip_contamination_does_not_eat_real_name_tokens() {
        // codex follow-up: confirm strip_role_suffix doesn't strip bare role-ish
        // surnames before the gate sees them.
        for name in ["Mark Engineer", "Dev", "Owner", "Manager", "Guest"] {
            assert_eq!(
                strip_contamination(name),
                name,
                "{name:?} must be untouched"
            );
        }
    }

    #[test]
    fn strip_speaker_label_preserves_non_ascii_prefix() {
        // byte-offset regression: lowercasing the Turkish dotted I changes byte length;
        // scanning the original must not slice mid-name (#385).
        assert_eq!(strip_speaker_label("İ Speaker 1"), "İ");
        assert_eq!(strip_speaker_label("İsmet speaker 1"), "İsmet");
        assert_eq!(strip_speaker_label("José speaker 2"), "José");
    }

    #[test]
    fn strip_contamination_handles_order_and_is_idempotent() {
        // speaker label trailing the role: must remove both regardless of order
        assert_eq!(strip_contamination("Junlei Tech Lead speaker 0"), "Junlei");
        assert_eq!(strip_contamination("Junlei, tech lead"), "Junlei");
        // stacked labels collapse fully
        assert_eq!(strip_contamination("Geert speaker 0 speaker 1"), "Geert");
        // idempotent
        for s in [
            "Junlei Tech Lead speaker 0",
            "Geert speaker 0 speaker 1",
            "Dan Benamoz",
        ] {
            let once = strip_contamination(s);
            assert_eq!(strip_contamination(once), once, "not idempotent for {s:?}");
        }
    }

    #[test]
    fn normalize_entity_identity_strips_speaker_label() {
        let entity = EntityRef {
            slug: "geert-speaker-0".into(),
            label: "Geert speaker 0".into(),
            aliases: vec![],
        };
        let identity = normalize_entity_identity(&entity).expect("should produce identity");
        assert_eq!(identity.slug, "geert");
    }

    #[test]
    fn normalize_entity_identity_rejects_non_person_tokens() {
        for bad in ["all", "team", "unassigned", "qa-engineer", "speaker-1"] {
            let entity = EntityRef {
                slug: bad.into(),
                label: String::new(),
                aliases: vec![],
            };
            assert!(
                normalize_entity_identity(&entity).is_none(),
                "should drop {bad:?}"
            );
        }
    }

    #[test]
    fn normalize_entity_identity_strips_comma_role_from_label() {
        let entity = EntityRef {
            slug: "junlei-tech-lead".into(),
            label: "Junlei, tech lead".into(),
            aliases: vec![],
        };
        let identity = normalize_entity_identity(&entity).expect("should produce identity");
        assert_eq!(identity.slug, "junlei");
        assert_eq!(identity.name, "Junlei");
    }

    #[test]
    fn normalize_entity_identity_strips_parenthetical_role() {
        let entity = EntityRef {
            slug: "junrei-core-team".into(),
            label: "Junrei (core team)".into(),
            aliases: vec![],
        };
        let identity = normalize_entity_identity(&entity).expect("should produce identity");
        assert_eq!(identity.slug, "junrei");
        assert_eq!(identity.name, "Junrei");
    }

    #[test]
    fn normalize_entity_identity_strips_vocab_suffix_from_label() {
        let entity = EntityRef {
            slug: "junlei-tech-lead".into(),
            label: "Junlei Tech Lead".into(),
            aliases: vec![],
        };
        let identity = normalize_entity_identity(&entity).expect("should produce identity");
        assert_eq!(identity.slug, "junlei");
        assert_eq!(identity.name, "Junlei");
    }

    #[test]
    fn normalize_entity_identity_strips_slug_when_label_is_empty() {
        let entity = EntityRef {
            slug: "junlei-tech-lead".into(),
            label: "".into(),
            aliases: vec![],
        };
        let identity = normalize_entity_identity(&entity).expect("should produce identity");
        assert_eq!(identity.slug, "junlei");
    }

    #[test]
    fn normalize_entity_identity_clean_entity_unchanged() {
        let entity = EntityRef {
            slug: "dan-benamoz".into(),
            label: "Dan Benamoz".into(),
            aliases: vec!["Dan".into()],
        };
        let identity = normalize_entity_identity(&entity).expect("should produce identity");
        assert_eq!(identity.slug, "dan-benamoz");
        assert_eq!(identity.name, "Dan Benamoz");
    }

    #[test]
    fn canonicalizer_resolves_contaminated_entity_to_clean_slug() {
        let entities = vec![
            EntityRef {
                slug: "junlei-tech-lead".into(),
                label: "Junlei, tech lead".into(),
                aliases: vec![],
            },
            EntityRef {
                slug: "junrei-core-team".into(),
                label: "Junrei (core team)".into(),
                aliases: vec![],
            },
        ];
        let resolver = PersonCanonicalizer::new(&entities, ["Junlei", "Junrei"]);

        let junlei = resolver.resolve("Junlei").expect("should resolve Junlei");
        assert_eq!(junlei.slug, "junlei", "role-stripped slug expected");

        let junrei = resolver.resolve("Junrei").expect("should resolve Junrei");
        assert_eq!(junrei.slug, "junrei", "role-stripped slug expected");

        // The contaminated form also resolves to the same slug
        let junlei_full = resolver
            .resolve("Junlei, tech lead")
            .expect("contaminated form should still resolve");
        assert_eq!(junlei_full.slug, "junlei");
    }
}