Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

feat(identity): merge author identities by union-find with guardrails (#2684)

Swati Ahujacff3d39771b4
diff --git a/packages/core/src/repowise/core/analysis/health/rows.py b/packages/core/src/repowise/core/analysis/health/rows.py
index 8180a273a..68e57e60b 100644
--- a/packages/core/src/repowise/core/analysis/health/rows.py
+++ b/packages/core/src/repowise/core/analysis/health/rows.py
@@ -8,12 +8,13 @@ these adapters so no consumer has to know which.
from __future__ import annotations
import json
+from collections.abc import Mapping
from typing import Any
def field(row: Any, name: str, default: Any = None) -> Any:
- """Read one attribute from a dataclass, an ORM row, or a dict."""
- if isinstance(row, dict):
+ """Read one attribute from a dataclass, an ORM row, or a mapping."""
+ if isinstance(row, Mapping):
return row.get(name, default)
return getattr(row, name, default)
diff --git a/packages/core/src/repowise/core/analysis/owners.py b/packages/core/src/repowise/core/analysis/owners.py
index 2d7e38730..7cb9de314 100644
--- a/packages/core/src/repowise/core/analysis/owners.py
+++ b/packages/core/src/repowise/core/analysis/owners.py
@@ -27,6 +27,7 @@ from repowise.core.analysis.health.aggregation import module_label
from repowise.core.analysis.health.rows import field as row_field
from repowise.core.analysis.health.rows import json_field
from repowise.core.author_identity import (
+ IdentityResolver,
build_identity_resolver,
canonicalize_author_email,
)
@@ -110,6 +111,30 @@ class OwnerAccumulator:
ACTIVITY_WINDOW_DAYS = 90
+def people_resolver(
+ git_rows: Iterable[Any] = (), commit_rows: Iterable[Any] = ()
+) -> IdentityResolver:
+ """The one identity resolver every people-shaped figure goes through.
+
+ Built from every (name, email) the rows carry: each file's primary owner
+ and ``top_authors_json`` entries, and each commit's author. A commit row's
+ optional ``co_authors`` (``Co-authored-by`` trailer pairs) is evidence only
+ and never merges anyone (see :func:`build_identity_resolver`). Owners,
+ the truck factor, ownership rollups and the whole-history contributor count
+ all key on it, so one person is one bucket everywhere.
+ """
+ pairs: list[tuple[str | None, str | None]] = []
+ evidence: list[tuple[str | None, str | None]] = []
+ for m in git_rows:
+ pairs.append((row_field(m, "primary_owner_name"), row_field(m, "primary_owner_email")))
+ for a in json_field(m, "top_authors_json", []):
+ pairs.append((a.get("name"), a.get("email")))
+ for c in commit_rows:
+ pairs.append((row_field(c, "author_name"), row_field(c, "author_email")))
+ evidence.extend(row_field(c, "co_authors") or ())
+ return build_identity_resolver(pairs, evidence)
+
+
def aggregate_owners(
git_rows: Iterable[Any],
dead_rows: Iterable[Any],
@@ -130,16 +155,7 @@ def aggregate_owners(
accs: dict[str, OwnerAccumulator] = {}
module_totals: dict[str, int] = defaultdict(int)
- # The resolver folds noreply variants and same-name real+noreply emails to
- # one bucket, so it needs every (name, email) pair before the main walk.
- pairs: list[tuple[str | None, str | None]] = []
- for m in rows:
- pairs.append((row_field(m, "primary_owner_name"), row_field(m, "primary_owner_email")))
- for a in json_field(m, "top_authors_json", []):
- pairs.append((a.get("name"), a.get("email")))
- for c in commits:
- pairs.append((row_field(c, "author_name"), row_field(c, "author_email")))
- resolve = build_identity_resolver(pairs)
+ resolve = people_resolver(rows, commits)
def _ensure(name: str, email: str | None) -> OwnerAccumulator:
k = resolve(name, email)
@@ -147,11 +163,8 @@ def aggregate_owners(
return OwnerAccumulator(key="", name=name or "(unknown)", email=email)
acc = accs.get(k)
if acc is None:
- acc = OwnerAccumulator(key=k, name=name or (email or ""), email=email)
+ acc = OwnerAccumulator(key=k, name=resolve.display_name(k), email=email)
accs[k] = acc
- # Promote a richer display name if we learn one later.
- if name and (not acc.name or acc.name == acc.email):
- acc.name = name
if email and not acc.email:
acc.email = email
return acc
@@ -265,7 +278,7 @@ def aggregate_owners(
def _apply_commit_activity(
accs: dict[str, OwnerAccumulator],
- resolve: Any,
+ resolve: IdentityResolver,
commits: list[Any],
total_commits: int | None,
) -> None:
@@ -336,5 +349,6 @@ __all__ = [
"as_utc",
"module_share",
"owner_key",
+ "people_resolver",
"silo_modules",
]
diff --git a/packages/core/src/repowise/core/author_identity.py b/packages/core/src/repowise/core/author_identity.py
index 8f2a0a51f..a7bfde92c 100644
--- a/packages/core/src/repowise/core/author_identity.py
+++ b/packages/core/src/repowise/core/author_identity.py
@@ -8,18 +8,21 @@ vary between commits, so the same person fans out into several contributor
buckets whenever identity is keyed on the raw email.
:func:`canonicalize_author_email` folds every noreply variant for a login onto
-one stable key so those buckets collapse. It is deliberately the *simple*
-version: it only unifies the noreply forms of a single login. The GitHub
-*system* author ``[email protected]`` (stamped on some merge commits) is left
-untouched on purpose — it stays its own bucket and is never merged into a human
-contributor. Bridging a person's noreply identity to their *real* email when the
-two share only a display name is the thorough version, out of scope here.
+one stable key so those buckets collapse. The GitHub *system* author
+``[email protected]`` (stamped on some merge commits) is left untouched on
+purpose — it stays its own bucket and is never merged into a human contributor.
+
+:func:`build_identity_resolver` goes further across a whole repo's authors,
+merging emails into people by union-find over a few evidence edges (see its
+docstring). False merges are worse than splits, so every edge beyond an exact
+email match is guarded and can be rolled back.
"""
from __future__ import annotations
import re
-from collections.abc import Callable, Iterable
+from collections import Counter, defaultdict
+from collections.abc import Iterable
_NOREPLY_DOMAIN = "@users.noreply.github.com"
@@ -31,6 +34,37 @@ _GH_NOREPLY_RE = re.compile(
re.IGNORECASE,
)
+# Automation, excluded from people counts. Either an explicit bot marker or a
+# service name matched in full, so "Netlify Johnson" stays a person.
+_BOT_NAME_RE = re.compile(
+ r"(\[bot\]"
+ r"|^bot$"
+ r"|[-_ ]bot$"
+ r"|^(dependabot|renovate(bot)?|greenkeeper|snyk([-_ ]bot)?|imgbot|"
+ r"github[-_ ]?actions|semantic[-_ ]release|allcontributors|codecov|mergify|"
+ r"pre[-_ ]commit[-_ ]ci|netlify|vercel)$)",
+ re.IGNORECASE,
+)
+_BOT_EMAIL_RE = re.compile(
+ r"(\[bot\]@|@bots\.noreply\.github\.com|^(actions@github\.com|"
+ r"noreply@github\.com)$)",
+ re.IGNORECASE,
+)
+
+# A noreply login this generic names a machine or a shared account, not a
+# person, so it never bridges to a real email by display name. Human first
+# names never belong here: a short or common login is handled by the length
+# floor and the per-login email cap instead.
+_SYSTEM_LOGINS = frozenset({"root", "admin", "user", "ubuntu", "runner", "git", "dev", "test"})
+_MIN_BRIDGE_LOGIN_LEN = 4
+# A login whose name matches more real emails than this is a common word, not
+# one person's handle.
+_MAX_EMAILS_PER_LOGIN = 3
+
+# Host labels that only exist on a local network or a single machine
+_LOCAL_HOST_LABELS = frozenset({"local", "localdomain", "localhost", "lan", "home", "internal"})
+
def canonicalize_author_email(email: str | None) -> str | None:
"""Return a stable identity email, folding GitHub noreply variants.
@@ -64,46 +98,217 @@ def author_identity_key(author_name: str | None, author_email: str | None) -> st
return (canonical or author_name or "").strip().lower()
-def _is_github_noreply(canonical_email: str | None) -> bool:
- """True for a ``[email protected]`` identity (not the system
- ``[email protected]`` author, which lives on a different domain)."""
- return bool(canonical_email) and canonical_email.endswith(_NOREPLY_DOMAIN)
+def is_bot(name: str | None, email: str | None) -> bool:
+ """True when this author is automation (CI or a coding agent's own identity).
+
+ Identity only: an agent-*assisted* commit still has a human author.
+ """
+ # Imported here: the provenance module loads the whole git indexer package.
+ from repowise.core.ingestion.git_indexer.agent_provenance import agent_from_identity
+
+ if agent_from_identity(name, email):
+ return True
+ if name and _BOT_NAME_RE.search(name):
+ return True
+ return bool(email and _BOT_EMAIL_RE.search(email))
+
+
+def _is_machine_local(canonical_email: str) -> bool:
+ """An address git made up from a hostname: ``(none)``, ``localhost``, a
+ ``.local`` host, or a domain with no dot (so no public TLD)."""
+ domain = canonical_email.rpartition("@")[2].strip("()")
+ if not domain or domain == "none" or "." not in domain:
+ return True
+ return any(label in _LOCAL_HOST_LABELS for label in domain.split("."))
+
+
+def _clean_name(name: str | None) -> str:
+ return " ".join((name or "").split())
+
+
+def _node(name: str | None, email: str | None) -> str:
+ """An observation's node: its canonical email, else ``name:<name>``."""
+ canon = canonicalize_author_email(email)
+ if canon:
+ return canon.strip().lower()
+ if name and name.strip():
+ return f"name:{name.strip()}"
+ return ""
+
+
+class _UnionFind:
+ def __init__(self, items: Iterable[str]) -> None:
+ self.parent = {x: x for x in items}
+
+ def find(self, x: str) -> str:
+ while self.parent[x] != x:
+ self.parent[x] = self.parent[self.parent[x]]
+ x = self.parent[x]
+ return x
+
+ def union(self, a: str, b: str) -> None:
+ self.parent[self.find(a)] = self.find(b)
+
+ def groups(self) -> list[list[str]]:
+ out: dict[str, list[str]] = defaultdict(list)
+ for x in self.parent:
+ out[self.find(x)].append(x)
+ return list(out.values())
+
+
+class IdentityResolver:
+ """``resolve(name, email) -> identity key`` over one repo's authors.
+
+ Keys match ``owner_key``'s shape: a lower-cased email (the identity's most
+ used real email), or ``name:<name>`` when no email is known. An author the
+ resolver was not built from resolves to its own canonical email.
+ """
+
+ def __init__(self, key_of: dict[str, str], names: dict[str, Counter[str]], bots: set[str]):
+ self._key_of = key_of
+ self._names = names
+ self._bots = bots
+
+ def __call__(self, name: str | None, email: str | None) -> str:
+ node = _node(name, email)
+ return self._key_of.get(node, node)
+
+ def display_name(self, key: str) -> str:
+ """The identity's most frequent name, preferring names of 2+ words."""
+ names = self._names.get(key)
+ if not names:
+ return key.removeprefix("name:")
+ pool = Counter({n: c for n, c in names.items() if len(n.split()) >= 2}) or names
+ # Ties break alphabetically so the name is stable across runs.
+ return min(pool, key=lambda n: (-pool[n], n))
+
+ def is_bot(self, key: str) -> bool:
+ return key in self._bots
+
+ def people(self) -> set[str]:
+ """Every human identity the resolver was built from."""
+ return set(self._names) - self._bots
def build_identity_resolver(
pairs: Iterable[tuple[str | None, str | None]],
-) -> Callable[[str | None, str | None], str]:
- """Return ``resolve(name, email) -> identity key`` for a whole repo's authors.
-
- Canonicalizes each email (folding GitHub noreply *variants* of one login),
- then does the same-display-name merge the simple dedup calls for: a person
- who shows up both with a real email and with a ``[email protected]``
- email under the **same display name** collapses to one identity. Only the
- noreply side is redirected, and only when that name maps to exactly **one**
- real email (unambiguous) — so two distinct real emails under one name, or a
- name<->login bridge across *different* names, are left split on purpose.
-
- Keys match ``owner_key``'s shape: a lower-cased email, or ``name:<name>``
- when no email is known.
+ evidence: Iterable[tuple[str | None, str | None]] = (),
+) -> IdentityResolver:
+ """Merge a repo's author ``(name, email)`` observations into people.
+
+ Pass one pair per observation (a commit, or a file's author entry): the
+ counts pick each identity's key and display name. Nodes are canonical
+ emails, which already folds the exact edges:
+
+ - **E1** the same email under any name or casing is one node;
+ - **E2** every noreply variant of one login is one node.
+
+ Union-find then adds the guarded edges:
+
+ - **E3** a noreply login joins the real email of the same display name,
+ when that name has 2+ words and exactly one real email;
+ - **E4** a noreply login ``L`` joins every real email whose display name is
+ ``L`` (case-insensitive), when ``L`` has 4+ characters, is not a system
+ account, and names at most 3 real emails;
+ - **E5** a machine-local email (``*.local``, ``localhost``, ``(none)``, no
+ public TLD) joins the single real email of the same 2+ word name.
+
+ Guardrails, since a false merge is worse than a split: the same display
+ name alone never joins two real emails; a node whose names point E3/E5 at
+ two different real emails takes neither; bots take no E3-E5 edge; and an
+ identity that would hold two different noreply logins has its E3-E5 edges
+ rolled back. (Numeric ids are not compared on their own: E2 already folds
+ one login's re-issued ids, and two logins are caught by login.)
+
+ ``evidence`` pairs (``Co-authored-by`` trailers) never create a node or an
+ edge: they only add their name to the display-name tally of an identity
+ whose email the author pairs already know.
"""
- real_by_name: dict[str, set[str]] = {}
+ names: dict[str, Counter[str]] = defaultdict(Counter)
+ bot_votes: Counter[str] = Counter()
+ bot_cache: dict[tuple[str | None, str | None], bool] = {}
+ logins: dict[str, str] = {}
for name, email in pairs:
- canon = canonicalize_author_email(email)
- if canon and not _is_github_noreply(canon) and name and name.strip():
- real_by_name.setdefault(name.strip().lower(), set()).add(canon)
- # Fold only when a display name resolves to a single real email.
- fold = {n: next(iter(s)) for n, s in real_by_name.items() if len(s) == 1}
-
- def resolve(name: str | None, email: str | None) -> str:
- canon = canonicalize_author_email(email)
- if canon:
- if _is_github_noreply(canon) and name and name.strip():
- real = fold.get(name.strip().lower())
- if real:
- return real
- return canon.strip().lower()
- if name and name.strip():
- return f"name:{name.strip()}"
- return ""
-
- return resolve
+ node = _node(name, email)
+ if not node:
+ continue
+ names[node][_clean_name(name) or node] += 1
+ if (name, email) not in bot_cache:
+ bot_cache[(name, email)] = is_bot(name, email)
+ bot_votes[node] += 1 if bot_cache[(name, email)] else -1
+ m = _GH_NOREPLY_RE.match((email or "").strip().lower())
+ if m:
+ logins[node] = m.group("login")
+ # A node is automation when most of its observations say so.
+ bots = {n for n, v in bot_votes.items() if v > 0}
+
+ def kind(node: str) -> str:
+ if node.startswith("name:"):
+ return "name"
+ if node in logins:
+ return "noreply"
+ return "local" if _is_machine_local(node) else "real"
+
+ kinds = {n: kind(n) for n in names}
+ real_by_name: dict[str, set[str]] = defaultdict(set)
+ for node, counter in names.items():
+ if kinds[node] == "real" and node not in bots:
+ for n in counter:
+ real_by_name[n.lower()].add(node)
+
+ soft: list[tuple[str, str]] = []
+ for node, counter in names.items():
+ if node in bots or kinds[node] not in ("noreply", "local"):
+ continue
+ # E3 (noreply) and E5 (machine-local): the one real email of that
+ # name. A one-word name ("Kirill", "Samuel") is shared by too many
+ # people to vouch for anyone; a handle-like one is E4's job.
+ by_name: set[str] = set()
+ for n in counter:
+ if len(n.split()) < 2:
+ continue
+ reals = real_by_name.get(n.lower(), set())
+ if len(reals) == 1:
+ by_name |= reals
+ # A shared alias seen under two people's names would bridge them both.
+ if len(by_name) == 1:
+ soft.append((node, next(iter(by_name))))
+ # E4: the login is the display name of a few real emails.
+ login = logins.get(node, "")
+ reals = real_by_name.get(login, set())
+ if (
+ len(login) >= _MIN_BRIDGE_LOGIN_LEN
+ and login not in _SYSTEM_LOGINS
+ and len(reals) <= _MAX_EMAILS_PER_LOGIN
+ ):
+ soft.extend((node, r) for r in reals)
+
+ def merge(edges: list[tuple[str, str]]) -> _UnionFind:
+ uf = _UnionFind(names)
+ for a, b in edges:
+ uf.union(a, b)
+ return uf
+
+ uf = merge(soft)
+ conflicted = set()
+ for nodes in uf.groups():
+ if len({logins[n] for n in nodes if n in logins}) > 1:
+ conflicted.add(uf.find(nodes[0]))
+ if conflicted:
+ uf = merge([(a, b) for a, b in soft if uf.find(a) not in conflicted])
+
+ rank = {"real": 0, "noreply": 1, "local": 2, "name": 3}
+ key_of: dict[str, str] = {}
+ merged: dict[str, Counter[str]] = {}
+ for nodes in uf.groups():
+ key = min(nodes, key=lambda n: (rank[kinds[n]], -names[n].total(), n))
+ merged[key] = sum((names[n] for n in nodes), Counter())
+ key_of.update(dict.fromkeys(nodes, key))
+
+ for name, email in evidence:
+ key = key_of.get(_node(None, email))
+ if key and _clean_name(name):
+ merged[key][_clean_name(name)] += 1
+
+ # Bots never merge, so a bot's identity is its own node.
+ return IdentityResolver(key_of, merged, bots)
diff --git a/packages/core/src/repowise/core/ingestion/git_indexer/records.py b/packages/core/src/repowise/core/ingestion/git_indexer/records.py
index ff5c1f4a3..c39ac01ba 100644
--- a/packages/core/src/repowise/core/ingestion/git_indexer/records.py
+++ b/packages/core/src/repowise/core/ingestion/git_indexer/records.py
@@ -504,6 +504,38 @@ def _lifetime_churn(
return _walk_churn(repo, rev)
+# ``Name <email>`` inside a ``Co-authored-by`` trailer value.
+_TRAILER_PERSON_RE = re.compile(r"^\s*(?P<name>[^<]*?)\s*<(?P<email>[^>]*)>\s*$")
+
+
+def count_people(repo: Any, rev: str) -> int:
+ """Humans who authored a non-merge commit reachable from *rev*.
+
+ One ``git log --no-merges`` pass reads each commit's mailmap-applied author
+ and its ``Co-authored-by`` trailers, and the shared people resolver folds
+ one person's several names and emails into one (trailers are evidence
+ only). Automation is not counted.
+ """
+ from repowise.core.analysis.owners import people_resolver
+
+ out = repo.git.log(
+ "--no-merges",
+ "--format=%aN%x00%aE%x00%(trailers:key=Co-authored-by,valueonly,separator=%x1f)",
+ rev,
+ )
+ rows = []
+ for line in out.splitlines():
+ name, _, rest = line.partition("\0")
+ email, _, trailers = rest.partition("\0")
+ co_authors = [
+ (m["name"], m["email"])
+ for value in trailers.split("\x1f")
+ if (m := _TRAILER_PERSON_RE.match(value))
+ ]
+ rows.append({"author_name": name, "author_email": email, "co_authors": co_authors})
+ return len(people_resolver(commit_rows=rows).people())
+
+
def capture_repo_totals(repo: Any, prior: RepoTotals | None = None) -> RepoTotals:
"""Whole-history stats for *repo* via a handful of cheap git calls.
@@ -527,9 +559,8 @@ def capture_repo_totals(repo: Any, prior: RepoTotals | None = None) -> RepoTotal
- the root commit(s) — earliest committed date (project age), the founding
author's name, and that commit's subject. Multiple roots (merged
histories) use the earliest root.
- - ``git shortlog -sn`` — one line per mailmap-folded author, so its line
- count is the true all-time contributor count. It is passed a revision
- (rather than left to read stdin, which would block).
+ - one ``git log --no-merges`` over author names and emails — the
+ all-time count of people (see :func:`count_people`).
Lifetime churn is the one exception: it walks the history, so it is capped
(see :func:`_lifetime_churn`).
@@ -570,11 +601,8 @@ def capture_repo_totals(repo: Any, prior: RepoTotals | None = None) -> RepoTotal
except Exception:
pass
- try:
- out = repo.git.shortlog("-sn", rev)
- totals.total_contributor_count = sum(1 for line in out.splitlines() if line.strip())
- except Exception:
- pass
+ with contextlib.suppress(Exception):
+ totals.total_contributor_count = count_people(repo, rev)
totals.total_lines_added, totals.total_lines_deleted = _lifetime_churn(
repo, totals.total_commit_count, rev, head_sha, prior
diff --git a/packages/core/src/repowise/core/stats_highlights.py b/packages/core/src/repowise/core/stats_highlights.py
index 40ecae644..384d12737 100644
--- a/packages/core/src/repowise/core/stats_highlights.py
+++ b/packages/core/src/repowise/core/stats_highlights.py
@@ -17,14 +17,12 @@ ISO strings (artifacts); :func:`parse_dt` normalises all three to aware UTC.
from __future__ import annotations
-import re
from collections import Counter
from collections.abc import Callable, Iterable, Mapping
from datetime import UTC, datetime, timedelta
from itertools import pairwise
from typing import Any
-from repowise.core.author_identity import build_identity_resolver
from repowise.core.test_paths import is_test_related_path
Row = Mapping[str, Any]
@@ -51,41 +49,9 @@ _LOCAL_TIME_COVERAGE = 0.99
# A peak hour drawn from fewer commits than this is noise, not a habit.
_CHRONOTYPE_MIN_COMMITS = 10
-# Automation, excluded from the people-shaped sections. Either an explicit bot
-# marker or a service name matched in full, so "Netlify Johnson" stays a person.
-_BOT_NAME_RE = re.compile(
- r"(\[bot\]"
- r"|^bot$"
- r"|[-_ ]bot$"
- r"|^(dependabot|renovate(bot)?|greenkeeper|snyk([-_ ]bot)?|imgbot|"
- r"github[-_ ]?actions|semantic[-_ ]release|allcontributors|codecov|mergify|"
- r"pre[-_ ]commit[-_ ]ci|netlify|vercel)$)",
- re.IGNORECASE,
-)
-_BOT_EMAIL_RE = re.compile(
- r"(\[bot\]@|@bots\.noreply\.github\.com|^(actions@github\.com|"
- r"noreply@github\.com)$)",
- re.IGNORECASE,
-)
-
_FUNCTION_KINDS = frozenset({"function", "method"})
-def is_bot(name: str | None, email: str | None) -> bool:
- """True when this author is automation (CI or a coding agent's own identity).
-
- Identity only: an agent-*assisted* commit still has a human author.
- """
- # Imported here: the provenance module loads the whole git indexer package.
- from repowise.core.ingestion.git_indexer.agent_provenance import agent_from_identity
-
- if agent_from_identity(name, email):
- return True
- if name and _BOT_NAME_RE.search(name):
- return True
- return bool(email and _BOT_EMAIL_RE.search(email))
-
-
def size_class(total_nloc: int) -> dict[str, Any]:
for ceiling, name, blurb in _SIZE_CLASSES:
if total_nloc < ceiling:
@@ -404,9 +370,10 @@ def build_commit_pass(commits: Iterable[Row], repo_totals: Row | None = None) ->
"""
totals = repo_totals or {}
rows = list(commits)
- resolve = build_identity_resolver([(r.get("author_name"), r.get("author_email")) for r in rows])
+ from repowise.core.analysis.owners import people_resolver
+
+ resolve = people_resolver(commit_rows=rows)
- contributors: set[str] = set()
display_name: dict[str, str] = {}
humans: set[str] = set()
arrival: dict[str, datetime] = {}
@@ -418,9 +385,8 @@ def build_commit_pass(commits: Iterable[Row], repo_totals: Row | None = None) ->
name, email = row.get("author_name"), row.get("author_email")
key = resolve(name, email) if (name or email) else None
if key:
- contributors.add(key)
- display_name.setdefault(key, name or key)
- if not is_bot(name, email):
+ display_name.setdefault(key, resolve.display_name(key))
+ if not resolve.is_bot(key):
humans.add(key)
awards.add(row)
moment = parse_dt(row.get("committed_at"))
@@ -451,7 +417,7 @@ def build_commit_pass(commits: Iterable[Row], repo_totals: Row | None = None) ->
key=lambda a: a["first_commit_at"] or "",
)
return {
- "origin": _origin(totals, first_at, max(commit_times, default=None), len(rows), len(contributors)),
+ "origin": _origin(totals, first_at, max(commit_times, default=None), len(rows), len(humans)),
"rhythm": rhythm,
"chronotypes": (
_chronotypes(cal["hours"], cal["weekdays"], display_name) if local_mode else []
@@ -554,17 +520,28 @@ def code_half_life(git_metadata: Iterable[Row], last_at: Any) -> int | None:
# ---------------------------------------------------------------------------
-def build_people(git_metadata: Iterable[Row]) -> dict[str, Any]:
- """Owner count, single-owner files, directory silos and the truck factor."""
+def build_people(git_metadata: Iterable[Row], commits: Iterable[Row] = ()) -> dict[str, Any]:
+ """Owner count, single-owner files, directory silos and the truck factor.
+
+ Owners are people, not display names: each file's primary owner goes
+ through :func:`people_resolver`, so one person under two names or emails
+ is one owner, and automation owns nothing here.
+ """
from repowise.core.analysis.health.aggregation import module_label
+ from repowise.core.analysis.owners import people_resolver
+ rows = list(git_metadata)
+ resolve = people_resolver(rows, commits)
owners: Counter[str] = Counter()
single_owner_files = 0
module_owner_files: dict[str, Counter[str]] = {}
module_file_totals: Counter[str] = Counter()
- for m in git_metadata:
- owner = m.get("primary_owner_name")
+ for m in rows:
+ name = m.get("primary_owner_name")
+ owner = resolve(name, m.get("primary_owner_email")) if name else None
+ if owner and resolve.is_bot(owner):
+ owner = None
if _int(m.get("bus_factor")) == 1:
single_owner_files += 1
module = module_label(m.get("file_path"))
diff --git a/packages/server/src/repowise/server/routers/git.py b/packages/server/src/repowise/server/routers/git.py
index 82f2f14ee..708a50ef3 100644
--- a/packages/server/src/repowise/server/routers/git.py
+++ b/packages/server/src/repowise/server/routers/git.py
@@ -27,6 +27,7 @@ from repowise.core.analysis.change_risk import (
score_change,
scores_excluding,
)
+from repowise.core.analysis.owners import people_resolver
from repowise.core.analysis.risk_semantics import change_risk_authority
from repowise.core.co_change import MIN_CO_CHANGE_SUPPORT, parse_partners
from repowise.core.ingestion.git_indexer._constants import (
@@ -647,15 +648,18 @@ async def get_ownership(
for m in all_meta:
modules.setdefault(top_level_module(m.file_path), []).append(m)
+ resolve = people_resolver(all_meta)
entries = []
for module_path, files in sorted(modules.items()):
- owners: dict[str, int] = {}
- for f in files:
- if f.primary_owner_name:
- owners[f.primary_owner_name] = owners.get(f.primary_owner_name, 0) + 1
+ owners: Counter[str] = Counter(
+ resolve(f.primary_owner_name, f.primary_owner_email)
+ for f in files
+ if f.primary_owner_name
+ )
if owners:
- top_owner = max(owners, key=owners.get) # type: ignore[arg-type]
- owner_pct = owners[top_owner] / len(files)
+ top_key = max(owners, key=owners.__getitem__)
+ top_owner = resolve.display_name(top_key)
+ owner_pct = owners[top_key] / len(files)
else:
top_owner = None
owner_pct = 0.0
@@ -841,13 +845,19 @@ async def get_git_summary(
sum(m.churn_percentile for m in all_meta) / len(all_meta) * 100.0 if all_meta else 0.0
)
- owners: dict[str, int] = {}
- for m in all_meta:
- if m.primary_owner_name:
- owners[m.primary_owner_name] = owners.get(m.primary_owner_name, 0) + 1
+ # One person is one owner, however many names and emails they commit under.
+ resolve = people_resolver(all_meta)
+ owners = Counter(
+ resolve(m.primary_owner_name, m.primary_owner_email)
+ for m in all_meta
+ if m.primary_owner_name
+ )
total = len(all_meta) or 1
top_owners = sorted(
- [{"name": k, "file_count": v, "pct": v / total} for k, v in owners.items()],
+ [
+ {"name": resolve.display_name(k), "file_count": v, "pct": v / total}
+ for k, v in owners.items()
+ ],
key=lambda x: x["file_count"],
reverse=True,
)[:top_owners_limit]
diff --git a/packages/server/src/repowise/server/routers/stats.py b/packages/server/src/repowise/server/routers/stats.py
index 2e8034958..7e9d580f9 100644
--- a/packages/server/src/repowise/server/routers/stats.py
+++ b/packages/server/src/repowise/server/routers/stats.py
@@ -196,6 +196,7 @@ async def stats_highlights(
select(
GitMetadata.file_path,
GitMetadata.primary_owner_name,
+ GitMetadata.primary_owner_email,
GitMetadata.bus_factor,
GitMetadata.commit_count_total,
GitMetadata.commit_count_capped,
@@ -235,7 +236,7 @@ async def stats_highlights(
rhythm = activity["rhythm"]
rhythm["code_half_life_days"] = sh.code_half_life(all_meta, activity["origin"]["last_commit_at"])
people = {
- **sh.build_people(all_meta),
+ **sh.build_people(all_meta, commits),
"contributor_count": activity["origin"]["contributor_count"],
"chronotypes": activity["chronotypes"],
"arrivals": activity["arrivals"],
diff --git a/tests/unit/ingestion/test_author_identity.py b/tests/unit/ingestion/test_author_identity.py
index 86e5df579..fea06c032 100644
--- a/tests/unit/ingestion/test_author_identity.py
+++ b/tests/unit/ingestion/test_author_identity.py
@@ -8,11 +8,20 @@ per-file author-email selection in ``index_file``.
from __future__ import annotations
+import os
+import subprocess
+from pathlib import Path
+
+import git
+import pytest
+
from repowise.core.ingestion.git_indexer import (
build_identity_resolver,
canonicalize_author_email,
)
from repowise.core.ingestion.git_indexer.identity import author_identity_key
+from repowise.core.ingestion.git_indexer.records import capture_repo_totals, count_people
+from repowise.core.stats_highlights import build_people
from repowise.server.services.owner_profile import owner_key
@@ -120,3 +129,281 @@ class TestIdentityResolver:
resolve("jdoe", "[email protected]")
)
+
+
+# ---------------------------------------------------------------------------
+# Union-find merge (E3-E5) and its guardrails
+# ---------------------------------------------------------------------------
+
+_NR = "@users.noreply.github.com"
+
+
+def _groups(pairs, evidence=()):
+ """The resolver's partition of the given emails, as a set of frozensets."""
+ resolve = build_identity_resolver(pairs, evidence)
+ out: dict[str, set[str]] = {}
+ for name, email in pairs:
+ out.setdefault(resolve(name, email), set()).add(email.lower())
+ return {frozenset(g) for g in out.values()}, resolve
+
+
+MERGE_CASES = [
+ pytest.param(
+ # A handle used as the name on several real emails, and a full name on
+ # the login's noreply address: one person (E4 + E2).
+ [
+ ("Maya Rossi", f"mrossi{_NR}"),
+ ("mrossi", f"12381+mrossi{_NR}"),
+ ("mrossi", "[email protected]"),
+ ("mrossi", "[email protected]"),
+ ],
+ [{f"mrossi{_NR}", f"12381+mrossi{_NR}", "[email protected]", "[email protected]"}],
+ "Maya Rossi",
+ id="login-named-real-emails",
+ ),
+ pytest.param(
+ [
+ ("Omar Haddad", f"5358+ohdev{_NR}"),
+ ("ohdev", f"5358+ohdev{_NR}"),
+ ("ohdev", "[email protected]"),
+ ],
+ [{f"5358+ohdev{_NR}", "[email protected]"}],
+ "Omar Haddad",
+ id="noreply-full-name-plus-handle-email",
+ ),
+ pytest.param(
+ # Machine-local addresses fold into the one real email of that name (E5).
+ [
+ ("Lena Park", "[email protected]"),
+ ("Lena Park", "[email protected]"),
+ ("Lena Park", "[email protected]"),
+ ("Lena Park", "lena@(none)"),
+ ],
+ [
+ {
+ "lena@(none)",
+ }
+ ],
+ "Lena Park",
+ id="machine-local-emails",
+ ),
+ pytest.param(
+ # Same full name on noreply + one real email (E3), a local email (E5).
+ [
+ ("Tomas Berg", f"144+tberg{_NR}"),
+ ("Tomas Berg", "[email protected]"),
+ ("Tomas Berg", "[email protected]"),
+ ],
+ [{f"144+tberg{_NR}", "[email protected]", "[email protected]"}],
+ "Tomas Berg",
+ id="full-name-noreply-and-local",
+ ),
+ pytest.param(
+ # Two different people called Alex keep their own identities, and so
+ # do two real emails under one full name: a name alone never merges.
+ [
+ ("Alex", "[email protected]"),
+ ("Alex", "[email protected]"),
+ ("Alex Kim", "[email protected]"),
+ ("Alex Kim", "[email protected]"),
+ ],
+ None,
+ id="two-alexes-stay-apart",
+ ),
+ pytest.param(
+ # A one-word name is too common to bridge a noreply login (E3/E5 need
+ # 2+ words); the login is not that email's name either (no E4).
+ [
+ ("Kirill", f"80+kbast{_NR}"),
+ ("Kirill", "[email protected]"),
+ ("Kirill", "[email protected]"),
+ ],
+ [{f"80+kbast{_NR}"}, {"[email protected]"}, {"[email protected]"}],
+ None,
+ id="one-word-name-does-not-merge",
+ ),
+ pytest.param(
+ # Generic and short logins never bridge by name.
+ [
+ ("admin", f"9+admin{_NR}"),
+ ("admin", "[email protected]"),
+ ("ubuntu", f"ubuntu{_NR}"),
+ ("ubuntu", "[email protected]"),
+ ("jo", f"3+jo{_NR}"),
+ ("jo", "[email protected]"),
+ ],
+ [
+ {f"9+admin{_NR}"},
+ {f"ubuntu{_NR}"},
+ {f"3+jo{_NR}"},
+ ],
+ None,
+ id="generic-login-collision",
+ ),
+ pytest.param(
+ # A login that is the name of more than 3 real emails is a common word.
+ [
+ ("mike", f"7+mike{_NR}"),
+ ("mike", "[email protected]"),
+ ("mike", "[email protected]"),
+ ("mike", "[email protected]"),
+ ("mike", "[email protected]"),
+ ],
+ None,
+ id="login-naming-too-many-emails",
+ ),
+ pytest.param(
+ # Two different logins would land in one identity: roll the soft edges
+ # back rather than guess which login is this person's.
+ [
+ ("Jane Doe", f"11+janed{_NR}"),
+ ("Jane Doe", f"22+jdoe99{_NR}"),
+ ("Jane Doe", "[email protected]"),
+ ],
+ [{f"11+janed{_NR}"}, {f"22+jdoe99{_NR}"}, {"[email protected]"}],
+ None,
+ id="two-login-conflict-rolls-back",
+ ),
+ pytest.param(
+ # Same conflict reached through E4: two handles name one real email.
+ [
+ ("Pat Quinn", f"31+pquinn{_NR}"),
+ ("Patrick", f"32+patq{_NR}"),
+ ("pquinn", "[email protected]"),
+ ("patq", "[email protected]"),
+ ],
+ [{f"31+pquinn{_NR}"}, {f"32+patq{_NR}"}, {"[email protected]"}],
+ None,
+ id="two-login-conflict-via-handles",
+ ),
+ pytest.param(
+ # One shared alias committed under two people's names must not bridge
+ # them: it matches a different real email per name, so it takes neither.
+ [
+ ("Jane Doe", f"41+shared{_NR}"),
+ ("John Smith", f"41+shared{_NR}"),
+ ("Jane Doe", "[email protected]"),
+ ("John Smith", "[email protected]"),
+ ("Jane Doe", "[email protected]"),
+ ("John Smith", "[email protected]"),
+ ],
+ [{f"41+shared{_NR}"}, {"[email protected]"}, {"[email protected]"}, {"[email protected]"}],
+ None,
+ id="shared-alias-two-names-stays-apart",
+ ),
+]
+
+
[email protected](("pairs", "expected", "display"), MERGE_CASES)
+def test_identity_merge_table(pairs, expected, display) -> None:
+ groups, resolve = _groups(pairs)
+ assert groups == {frozenset(g) for g in expected}
+ if display is not None:
+ (key,) = {resolve(n, e) for n, e in pairs}
+ assert resolve.display_name(key) == display
+
+
+def test_bots_never_merge_and_are_not_people() -> None:
+ pairs = [
+ ("dependabot[bot]", f"49699333+dependabot[bot]{_NR}"),
+ ("dependabot[bot]", f"dependabot[bot]{_NR}"),
+ ("github-actions", "[email protected]"),
+ ("Dana White", "[email protected]"),
+ ]
+ resolve = build_identity_resolver(pairs)
+ bot = resolve("dependabot[bot]", f"49699333+dependabot[bot]{_NR}")
+ assert resolve.is_bot(bot)
+ assert resolve.is_bot(resolve("github-actions", "[email protected]"))
+ assert resolve.people() == {"[email protected]"}
+
+
+def test_co_author_trailer_alone_never_merges() -> None:
+ # Only a trailer names the real email with the handle, so E4 has no author
+ # row to stand on and the two stay apart. The trailer's name still counts
+ # for the display name of the identity it points at.
+ pairs = [("Ana Lima", "[email protected]"), ("ana-l", f"7+ana-l{_NR}")]
+ groups, resolve = _groups(pairs, evidence=[("ana-l", "[email protected]"), ("Ana M. Lima", "[email protected]")])
+ assert groups == {frozenset({"[email protected]"}), frozenset({f"7+ana-l{_NR}"})}
+ assert resolve.display_name("[email protected]") == "Ana Lima"
+
+
+def test_display_name_prefers_a_full_name() -> None:
+ resolve = build_identity_resolver([("mrossi", "[email protected]")] * 5 + [("Maya Rossi", "[email protected]")])
+ assert resolve.display_name("[email protected]") == "Maya Rossi"
+
+
+# ---------------------------------------------------------------------------
+# Whole-repo: one git log pass, trailers, and the truck factor
+# ---------------------------------------------------------------------------
+
+
+def _git_commit(repo: Path, author: str, message: str) -> None:
+ (repo / "f.txt").write_text(f"{author}\n{message}\n")
+ subprocess.run(["git", "add", "f.txt"], cwd=repo, check=True)
+ env = {**os.environ, "GIT_COMMITTER_NAME": "Committer", "GIT_COMMITTER_EMAIL": "[email protected]"}
+ subprocess.run(
+ ["git", "commit", "-q", f"--author={author}", "-m", message],
+ cwd=repo,
+ check=True,
+ env=env,
+ )
+
+
+def test_contributor_count_from_one_log_pass(tmp_path: Path) -> None:
+ subprocess.run(["git", "init", "-q"], cwd=tmp_path, check=True)
+ authors = [
+ f"Maya Rossi <12381+mrossi{_NR}>",
+ "mrossi <[email protected]>",
+ "Lena Park <[email protected]>",
+ "Lena Park <[email protected]>",
+ "Alex <[email protected]>",
+ "Alex <[email protected]>",
+ f"dependabot[bot] <49699333+dependabot[bot]{_NR}>",
+ "Ana Lima <[email protected]>",
+ ]
+ for n, author in enumerate(authors):
+ _git_commit(tmp_path, author, f"change {n}")
+ # The trailer pairs the handle with Ana's real email; it must not merge.
+ _git_commit(tmp_path, f"ana-l <7+ana-l{_NR}>", "pair work\n\nCo-authored-by: ana-l <[email protected]>")
+
+ repo = git.Repo(tmp_path)
+ shortlog = repo.git.shortlog("-sn", "HEAD").splitlines()
+ # shortlog groups by name: it splits Maya and merges the two Alexes.
+ assert len(shortlog) == 7
+ # Maya, Lena, two Alexes, Ana, ana-l; the bot is not a person.
+ assert count_people(repo, "HEAD") == 6
+ assert capture_repo_totals(repo).total_contributor_count == 6
+
+
+def test_truck_factor_counts_people_not_names() -> None:
+ def meta(path: str, name: str, email: str) -> dict:
+ return {
+ "file_path": path,
+ "primary_owner_name": name,
+ "primary_owner_email": email,
+ "bus_factor": 1,
+ }
+
+ rows = (
+ [meta(f"a/{i}.py", "Maya Rossi", f"12381+mrossi{_NR}") for i in range(4)]
+ + [meta(f"b/{i}.py", "Omar Haddad", "[email protected]") for i in range(3)]
+ + [meta(f"c/{i}.py", "mrossi", "[email protected]") for i in range(3)]
+ + [meta(f"d/{i}.py", "Lena Park", "[email protected]") for i in range(2)]
+ + [meta(f"e/{i}.py", "Ivan Petrov", "[email protected]") for i in range(2)]
+ )
+ # Keyed by name it takes 3 owners (4 + 3 + 3) to pass half of 14 files;
+ # as people Maya owns 7 and Omar 3, so 2 do.
+ assert build_people(rows)["truck_factor"] == 2
+ bots = [meta(f"z/{i}.py", "dependabot[bot]", f"1+dependabot[bot]{_NR}") for i in range(9)]
+ people = build_people(rows + bots)
+ # Automation owns nothing: the bot's 9 files neither count nor move the factor.
+ assert people["owner_count"] == 4
+ assert people["truck_factor"] == 2
diff --git a/tests/unit/server/golden/ownership_rollups.json b/tests/unit/server/golden/ownership_rollups.json
index 4c79fcbdc..ce9e6d185 100644
--- a/tests/unit/server/golden/ownership_rollups.json
+++ b/tests/unit/server/golden/ownership_rollups.json
@@ -16,7 +16,7 @@
},
{
- "name": "Bob",
+ "name": "Bob Stone",
"email": "[email protected]",
"files_owned": 2,
"hotspots_owned": 1,
@@ -62,7 +62,7 @@
},
{
- "name": "Bob",
+ "name": "Bob Stone",
"email": "[email protected]",
"files_owned": 2,
"hotspots_owned": 1,
@@ -153,7 +153,7 @@
"files_touched_total": 3,
"co_authors": [
{
- "name": "Bob",
+ "name": "Bob Stone",
"email": "[email protected]",
"shared_files": 3,
"co_change_strength": 1.0
@@ -182,7 +182,7 @@
},
- "name": "Bob",
+ "name": "Bob Stone",
"email": "[email protected]",
"files_owned": 2,
"hotspots_owned": 1,
@@ -317,7 +317,7 @@
"co_change_strength": 0.5
},
{
- "name": "Bob",
+ "name": "Bob Stone",
"email": "[email protected]",
"shared_files": 1,
"co_change_strength": 0.5
@@ -343,7 +343,7 @@
"avg_churn_percentile": 54.99999999999999,
"median_bus_factor": 1.0,
"min_bus_factor": 1,
- "primary_owner": "Bob",
+ "primary_owner": "Bob Stone",
"primary_owner_pct": 0.3333333333333333,
"is_silo": false,
"decision_count": 2,
@@ -400,7 +400,7 @@
"avg_churn_percentile": 54.99999999999999,
"median_bus_factor": 1.0,
"min_bus_factor": 1,
- "primary_owner": "Bob",
+ "primary_owner": "Bob Stone",
"primary_owner_pct": 0.3333333333333333,
"is_silo": false,
"decision_count": 2,
@@ -408,7 +408,7 @@
"health_score": 56.75,
"owners": [
{
- "name": "Bob",
+ "name": "Bob Stone",
"email": "[email protected]",
"file_count": 1,
"pct": 0.3333333333333333
@@ -468,7 +468,7 @@
"pct": 0.5
},
{
- "name": "Bob",
+ "name": "Bob Stone",
"email": "[email protected]",
"file_count": 1,
"pct": 0.5
@@ -515,7 +515,7 @@
"avg_churn_percentile": 60.0,
"median_bus_factor": 1.0,
"min_bus_factor": 1,
- "primary_owner": "Bob",
+ "primary_owner": "Bob Stone",
"primary_owner_pct": 0.9,
"is_silo": true,
"decision_count": 0,
@@ -523,7 +523,7 @@
"health_score": 36.0,
"owners": [
{
- "name": "Bob",
+ "name": "Bob Stone",
"email": "[email protected]",
"file_count": 1,
"pct": 0.9
@@ -551,7 +551,7 @@
"avg_churn_percentile": 54.99999999999999,
"median_bus_factor": 1.0,
"min_bus_factor": 1,
- "primary_owner": "Bob",
+ "primary_owner": "Bob Stone",
"primary_owner_pct": 0.3333333333333333,
"is_silo": false,
"decision_count": 2,
@@ -559,7 +559,7 @@
"health_score": 56.75,
"owners": [
{
- "name": "Bob",
+ "name": "Bob Stone",
"email": "[email protected]",
"file_count": 1,
"pct": 0.3333333333333333
@@ -603,7 +603,7 @@
],
"suggestions": [
{
- "name": "Bob",
+ "name": "Bob Stone",
"email": "[email protected]",
"score": 1.99,
"recent_commits": 8,
@@ -637,7 +637,7 @@
]
},
{
- "name": "Bob",
+ "name": "Bob Stone",
"email": "[email protected]",
"score": 0.7133,
"recent_commits": 5,
diff --git a/tests/unit/server/test_ownership_rollups_golden.py b/tests/unit/server/test_ownership_rollups_golden.py
index a38e60a1c..7761a30a5 100644
--- a/tests/unit/server/test_ownership_rollups_golden.py
+++ b/tests/unit/server/test_ownership_rollups_golden.py
@@ -24,8 +24,8 @@ from tests.unit.server.conftest import create_test_repo
GOLDEN = Path(__file__).parent / "golden" / "ownership_rollups.json"
_ALICE = {"name": "Alice", "email": "[email protected]"}
-_BOB = {"name": "Bob", "email": "[email protected]"}
-_BOB_NOREPLY = {"name": "Bob", "email": "[email protected]"}
+_BOB = {"name": "Bob Stone", "email": "[email protected]"}
+_BOB_NOREPLY = {"name": "Bob Stone", "email": "[email protected]"}
_CAROL = {"name": "Carol", "email": None}
@@ -67,7 +67,7 @@ _GIT = {
),
),
"src/api.py": dict(
- primary_owner_name="Bob",
+ primary_owner_name="Bob Stone",
primary_owner_email="[email protected]",
primary_owner_commit_pct=0.9,
top_authors_json=_authors((_BOB_NOREPLY, 18, 1_785_000_000), (_ALICE, 2, None)),
@@ -95,7 +95,7 @@ _GIT = {
bus_factor=1,
),
"lib/util.py": dict(
- primary_owner_name="Bob",
+ primary_owner_name="Bob Stone",
primary_owner_email="[email protected]",
primary_owner_commit_pct=0.6,
top_authors_json=_authors((_BOB, 6, None), (_ALICE, 4, 1_770_000_000)),
diff --git a/tests/unit/server/test_stats_bot_filter.py b/tests/unit/server/test_stats_bot_filter.py
index 7493ec69f..ecb75e29a 100644
--- a/tests/unit/server/test_stats_bot_filter.py
+++ b/tests/unit/server/test_stats_bot_filter.py
@@ -13,7 +13,7 @@ from __future__ import annotations
import pytest
-from repowise.core.stats_highlights import is_bot as _is_bot
+from repowise.core.author_identity import is_bot as _is_bot
AGENTS = [
# Coding agents, recognised via the ingestion layer's provenance registry.