Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
refactor(core): read author identity through the forges package (#3382)
Raghav Chamadiyacfbcc57429fb
diff --git a/packages/core/src/repowise/core/author_identity.py b/packages/core/src/repowise/core/author_identity.pyindex 8ca971636..f2b535519 100644--- a/packages/core/src/repowise/core/author_identity.py+++ b/packages/core/src/repowise/core/author_identity.py@@ -1,16 +1,14 @@"""Author-identity canonicalization shared across the ingestion + serving paths.-GitHub stamps commits made through its web UI, squash-merges and PR merges with-a synthetic *noreply* address instead of the author's real email:-``[email protected]`` (numeric-id prefixed) or the older-``[email protected]`` form. The numeric id and the exact shape-vary between commits, so the same person fans out into several contributor+Forges stamp web-UI commits and merges with a synthetic *noreply* address+instead of the author's real email (GitHub's ``[email protected]``+or the older ``login@...``), so one person fans out into several contributorbuckets 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. 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.+one stable key so those buckets collapse. Which addresses fold and which+authors are automation is read from ``repowise.core.forges``; system authors+such as ``[email protected]`` stay their own bucket, never merged into a person.: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@@ -20,40 +18,14 @@ email match is guarded and can be rolled back.from __future__ import annotations-import refrom collections import Counter, defaultdictfrom collections.abc import Iterablefrom functools import lru_cachefrom typing import Literal-IdentityKind = Literal["human", "agent", "bot"]+from repowise.core import forges-_NOREPLY_DOMAIN = "@users.noreply.github.com"--# ``[email protected]`` or the older ``login@...`` form.-# The numeric id and the prefix are optional; the login is everything before-# the ``@`` (minus the ``NNN+`` id). Case-insensitive to match git's casing.-_GH_NOREPLY_RE = re.compile(- r"^(?:\d+\+)?(?P<login>[^@\s+]+)@users\.noreply\.github\.com$",- 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,-)+IdentityKind = Literal["human", "agent", "bot"]# 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@@ -71,22 +43,22 @@ _LOCAL_HOST_LABELS = frozenset({"local", "localdomain", "localhost", "lan", "homdef canonicalize_author_email(email: str | None) -> str | None:- """Return a stable identity email, folding GitHub noreply variants.+ """Return a stable identity email, folding a forge's noreply variants.``[email protected]`` and ``[email protected]``- both collapse to ``[email protected]`` (lower-cased) so every- noreply variant of one login shares a key. Any other address — including the- ``[email protected]`` system author — is returned lower-cased and otherwise- unchanged. ``None``/empty passes through unchanged so callers can keep their- existing "no email → fall back to name" handling.+ both collapse to ``[email protected]``; any other address is+ returned lower-cased (see ``forges.canonical_email``). ``None``/empty passes+ through unchanged so callers can keep their existing "no email → fall back+ to name" handling."""if not email:return email- lowered = email.strip().lower()- m = _GH_NOREPLY_RE.match(lowered)- if m:- return f"{m.group('login')}{_NOREPLY_DOMAIN}"- return lowered+ return forges.canonical_email(email)+++def is_noreply(email: str | None) -> bool:+ """Whether *email* is a forge's synthetic noreply address for a login."""+ return forges.noreply_login(email) is not Nonedef author_identity_key(author_name: str | None, author_email: str | None) -> str:@@ -111,7 +83,7 @@ def _identity(name: str | None, email: str | None) -> tuple[IdentityKind, str |agent = agent_from_identity(name, email)if agent:return "agent", agent- if (name and _BOT_NAME_RE.search(name)) or (email and _BOT_EMAIL_RE.search(email)):+ if forges.is_bot(name, email):return "bot", Nonereturn "human", None@@ -247,9 +219,10 @@ def build_identity_resolver(Guardrails, since a false merge is worse than a split: the same displayname alone never joins two real emails; a node whose names point E3/E5 attwo 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.)+ identity that would hold two different noreply addresses has its E3-E5+ edges rolled back. (E2 already folds one GitHub login's re-issued ids, so+ two GitHub nodes are two logins; GitLab keeps the id, so the same login on+ two ids or hosts is two nodes, possibly two accounts.)``evidence`` pairs (``Co-authored-by`` trailers) never create a node or anedge: they only add their name to the display-name tally of an identity@@ -271,9 +244,9 @@ def build_identity_resolver(kind_votes[node][obs_kind] += 1if agent:agent_votes[node][agent] += 1- m = _GH_NOREPLY_RE.match((email or "").strip().lower())- if m:- logins[node] = m.group("login")+ noreply = forges.noreply_login(email)+ if noreply:+ logins[node] = noreply[1]# A node is automation when most of its observations say so (a tie stays# human); an agent/bot tie goes to the more specific agent.auto_kinds: dict[str, IdentityKind] = {}@@ -332,7 +305,7 @@ def build_identity_resolver(uf = merge(soft)conflicted = set()for nodes in uf.groups():- if len({logins[n] for n in nodes if n in logins}) > 1:+ if sum(n in logins for n in nodes) > 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])diff --git a/packages/core/src/repowise/core/ingestion/git_indexer/agent_provenance.py b/packages/core/src/repowise/core/ingestion/git_indexer/agent_provenance.pyindex ee8952792..b4a2b56b5 100644--- a/packages/core/src/repowise/core/ingestion/git_indexer/agent_provenance.py+++ b/packages/core/src/repowise/core/ingestion/git_indexer/agent_provenance.py@@ -71,6 +71,8 @@ from pathlib import Pathimport structlog+from repowise.core import forges+logger = structlog.get_logger(__name__)__all__ = [@@ -85,9 +87,12 @@ __all__ = [# ------------------------------------------------------------------ registry ---# Bot login (lowercase, without the ``[bot]`` suffix) → agent name, for-# ``<login>[bot]@users.noreply.github.com`` author/committer e-mails (GitHub's-# canonical bot identity, also re-injected on squash merges).+# GitHub bot login (lowercase, without the ``[bot]`` suffix) → agent name, for+# the GitHub noreply address of ``<login>[bot]`` (its canonical bot identity,+# also re-injected on squash merges). ``[bot]`` is optional: squash merges by+# the Copilot coding agent use ``<id>[email protected]``. Safe+# because each login is a unique, vendor-owned GitHub account no human can+# register; the same login on another forge is nobody's._BOT_LOGINS: dict[str, str] = {"copilot-swe-agent": "copilot","copilot": "copilot",@@ -126,7 +131,6 @@ _AGENT_ALIASES: list[tuple[str, str]] = [# Exact service e-mails → (agent, tier) for commit identity fields._SERVICE_EMAILS: dict[str, tuple[str, int]] = {"[email protected]": ("cursor", 1),- "devin-ai-integration[bot]@users.noreply.github.com": ("devin", 1),# Primarily Claude's co-author identity; as AUTHOR it is tier 1 like every# other service e-mail here."[email protected]": ("claude", 1),@@ -172,7 +176,6 @@ _COAUTHOR_PATTERNS: list[tuple[str, str]] = [r"^co-authored-by:\s*claude(?:\s+(?:opus|sonnet|haiku)[^<]*)?\s*<[^>]*@anthropic\.com>","claude",),- (r"^co-authored-by:\s*copilot\s*<\d+\+copilot@users\.noreply\.github\.com>", "copilot"),(r"^co-authored-by:\s*cursor(?:\s*agent)?\s*<(?:cursoragent@cursor\.com|[^>]*cursor[^>]*)>","cursor",@@ -559,16 +562,6 @@ class AgentProvenanceClassifier:extra_coauthor_patterns: list[tuple[str, str]] | None = None,) -> None:self._service_emails = {**_SERVICE_EMAILS, **(extra_service_emails or {})}- # ``[bot]`` is optional: GitHub squash-merges authored by the Copilot- # coding agent use ``<id>[email protected]`` (no- # suffix). Safe because each login is a unique, vendor-owned GitHub- # account — no human can register them.- self._bot_email_re = re.compile(- r"^(?:\d+\+)?("- + "|".join(re.escape(b) for b in _BOT_LOGINS)- + r")(?:\[bot\])?@users\.noreply\.github\.com$",- re.IGNORECASE,- )self._footers = [(re.compile(p, re.IGNORECASE | re.MULTILINE), agent)for p, agent in [*_FOOTER_PATTERNS, *(extra_footer_patterns or [])]@@ -585,9 +578,11 @@ class AgentProvenanceClassifier:hit = self._service_emails.get(e)if hit:return hit- m = self._bot_email_re.match(e)- if m:- return (_BOT_LOGINS[m.group(1).lower()], 1)+ noreply = forges.noreply_login(e)+ if noreply and noreply[0] is forges.ForgeKind.GITHUB:+ agent = _BOT_LOGINS.get(noreply[1].removesuffix("[bot]"))+ if agent:+ return (agent, 1)local, _, domain = e.partition("@")vendor = _VENDOR_DOMAIN_LOCALS.get(domain)if vendor and vendor[0].fullmatch(local):diff --git a/packages/core/src/repowise/core/ingestion/git_indexer/file_history.py b/packages/core/src/repowise/core/ingestion/git_indexer/file_history.pyindex 28f1f3334..0861bba97 100644--- a/packages/core/src/repowise/core/ingestion/git_indexer/file_history.py+++ b/packages/core/src/repowise/core/ingestion/git_indexer/file_history.py@@ -35,7 +35,7 @@ from .function_blame import (build_blame_index,ownership_from_blame,)-from .identity import canonicalize_author_email+from .identity import canonicalize_author_email, is_noreplyfrom .records import (_LOG_FORMAT,_RECORD_SEP,@@ -430,9 +430,6 @@ def _add_windows(meta["avg_commit_size"] = total_churn / c90 if c90 > 0 else 0.0-_NOREPLY_DOMAIN = "@users.noreply.github.com"--@dataclassclass _Authors:"""Per-author commit counts, one stable email each, and first/last commit times."""@@ -463,13 +460,11 @@ class _Authors:return authorsdef _prefer_email(self, name: str, email: str) -> None:- # One email per name: fold GitHub noreply variants and prefer a real- # address, so owner_profile (keyed on email) does not split a person.+ # One email per name: fold noreply variants and prefer a real address,+ # so owner_profile (keyed on email) does not split a person.canon = canonicalize_author_email(email) or emailexisting = self.emails.get(name)- if existing is None or (- existing.endswith(_NOREPLY_DOMAIN) and not canon.endswith(_NOREPLY_DOMAIN)- ):+ if existing is None or (is_noreply(existing) and not is_noreply(canon)):self.emails[name] = canondef entry(self, name: str, count: int) -> dict[str, Any]:diff --git a/packages/core/src/repowise/core/ingestion/git_indexer/identity.py b/packages/core/src/repowise/core/ingestion/git_indexer/identity.pyindex 35ba009a7..8674752b6 100644--- a/packages/core/src/repowise/core/ingestion/git_indexer/identity.py+++ b/packages/core/src/repowise/core/ingestion/git_indexer/identity.py@@ -7,6 +7,12 @@ from repowise.core.author_identity import (author_identity_key,build_identity_resolver,canonicalize_author_email,+ is_noreply,)-__all__ = ["author_identity_key", "build_identity_resolver", "canonicalize_author_email"]+__all__ = [+ "author_identity_key",+ "build_identity_resolver",+ "canonicalize_author_email",+ "is_noreply",+]diff --git a/tests/unit/ingestion/test_agent_provenance.py b/tests/unit/ingestion/test_agent_provenance.pyindex 22f073b35..e6ce04179 100644--- a/tests/unit/ingestion/test_agent_provenance.py+++ b/tests/unit/ingestion/test_agent_provenance.py@@ -79,6 +79,12 @@ def test_bot_author_email_is_tier1() -> None:assert prov.confidence == "high"+def test_agent_login_on_another_forge_is_a_person() -> None:+ """The agent logins are GitHub accounts; the same login on GitLab is nobody's."""+ prov = _classify(an="Copilot", ae="[email protected]")+ assert prov.agent is None++def test_service_email_author_is_tier1() -> None:prov = _classify(an="Cursor Agent", ae="[email protected]")assert (prov.agent, prov.autonomy_tier) == ("cursor", 1)diff --git a/tests/unit/ingestion/test_author_identity.py b/tests/unit/ingestion/test_author_identity.pyindex 10ff3a441..2a91239d0 100644--- a/tests/unit/ingestion/test_author_identity.py+++ b/tests/unit/ingestion/test_author_identity.py@@ -305,6 +305,37 @@ MERGE_CASES = [None,id="shared-alias-two-names-stays-apart",),+ pytest.param(+ # A GitLab noreply address joins its owner's real email by full name,+ # and is never folded into another account that reused the login.+ [+ ("Jane Doe", "[email protected]"),+ ("Jane Doe", "[email protected]"),+ ("Jim Doe", "[email protected]"),+ ],+ [+ {"[email protected]", "[email protected]"},+ {"[email protected]"},+ ],+ None,+ id="gitlab-noreply-joins-by-name",+ ),+ pytest.param(+ # One login on two GitLab ids or hosts may be two accounts: a shared+ # real email must not bridge them.+ [+ ("john", "[email protected]"),+ ("john", "[email protected]"),+ ("john", "[email protected]"),+ ],+ [+ {"[email protected]"},+ {"[email protected]"},+ {"[email protected]"},+ ],+ None,+ id="gitlab-same-login-two-ids-stays-apart",+ ),]@@ -341,6 +372,9 @@ KIND_CASES = [("github-actions", "[email protected]", "bot"),("renovate", "", "bot"),("Deploy Bot", "[email protected]", "bot"),+ ("deploy token", "[email protected]", "bot"),+ ("Project Collection Build Service (contoso)", "[email protected]", "bot"),+ ("bitbucket-pipelines", "[email protected]", "bot"),("Dana White", "[email protected]", "human"),# Near misses stay people: no loose substring or first-name rules.("Claudette Moreau", "[email protected]", "human"),