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.

fix(events): ignore tied owners at the ownership margin (#3870)

Emre K.0535736cff22
diff --git a/packages/core/src/repowise/core/events/diff.py b/packages/core/src/repowise/core/events/diff.py
index 49e5d7aaf..8efd079c7 100644
--- a/packages/core/src/repowise/core/events/diff.py
+++ b/packages/core/src/repowise/core/events/diff.py
@@ -79,7 +79,12 @@ def _ownership(before: Mapping[str, Any], after: Mapping[str, Any]) -> Iterator[
if min(a.get("owned_files", 0), b.get("owned_files", 0)) < MIN_OWNED_FILES:
return
old, new = set(a.get("main_owners") or ()), set(b.get("main_owners") or ())
- if old and new and len(old & new) / len(old | new) < OWNERSHIP_SHIFT_JACCARD:
+ # New snapshots identify the whole tie at the cutoff. Remove those
+ # identities from both comparisons, including a legacy snapshot without
+ # the tie fact; its alphabetically selected member is not a real shift.
+ tied = set(a.get("margin_tied_owners") or ()) | set(b.get("margin_tied_owners") or ())
+ stable_old, stable_new = old - tied, new - tied
+ if stable_old and stable_new and len(stable_old & stable_new) / len(stable_old | stable_new) < OWNERSHIP_SHIFT_JACCARD:
yield "ownership_shift", "observed", {
"basis": "observed",
"main_owners_before": len(old),
diff --git a/packages/core/src/repowise/core/events/snapshot.py b/packages/core/src/repowise/core/events/snapshot.py
index 87e8c115a..8a29043a5 100644
--- a/packages/core/src/repowise/core/events/snapshot.py
+++ b/packages/core/src/repowise/core/events/snapshot.py
@@ -305,17 +305,26 @@ def _ownership(
owned = sum(owners.values())
main: list[str] = []
held = 0
- for person, count in sorted(owners.items(), key=lambda kv: (-kv[1], kv[0])):
+ ranked = sorted(owners.items(), key=lambda kv: (-kv[1], kv[0]))
+ for person, count in ranked:
if held * 2 > owned:
break
main.append(person)
held += count
+ # Keep the deterministic main set for activity checks. A tie split by the
+ # majority cutoff carries no ownership signal in the diff, however.
+ margin = (
+ [person for person, count in ranked if count == owners[main[-1]]]
+ if main and len(main) < len(ranked) and ranked[len(main)][1] == owners[main[-1]]
+ else []
+ )
recent_authors = {resolve(c["author_name"], c["author_email"]) for c in commits}
return {
"owned_files": owned,
"owner_count": len(owners),
# Without a key there is nothing safe to compare, so no set is kept.
"main_owners": sorted(_keyed(key, k) for k in main) if key else None,
+ "margin_tied_owners": sorted(_keyed(key, k) for k in margin) if key else None,
"active_main_owners": sum(1 for k in main if k in recent_authors),
"recent_commits": len({c["sha"] for c in commits}),
"recent_capped": capped,
diff --git a/tests/unit/core/test_events.py b/tests/unit/core/test_events.py
index 69d08d18a..3d37226bc 100644
--- a/tests/unit/core/test_events.py
+++ b/tests/unit/core/test_events.py
@@ -105,6 +105,57 @@ def test_observed_owner_turnover_needs_enough_owned_files():
assert _kinds(_facts(), _facts(ownership=grew)) == []
+def _owned_by(counts):
+ paths, rows = [], {}
+ for person, count in counts.items():
+ for _ in range(count):
+ path = f"src/file{len(paths)}.py"
+ paths.append(path)
+ rows[path] = {"primary_owner_name": person, "primary_owner_email": None}
+
+ class Resolve:
+ def __call__(self, name, email):
+ return name
+
+ def is_bot(self, person):
+ return False
+
+ return snapshot_mod._ownership(paths, rows, [], Resolve(), b"k" * 32, False)
+
+
+def test_alphabetical_churn_at_ownership_margin_is_not_a_shift():
+ singles = dict.fromkeys("bcdefghij", 1)
+ before = _owned_by({"alice": 9, **singles})
+ after = _owned_by({"alice": 9, **singles, "aaron": 1})
+ assert before["main_owners"] != after["main_owners"]
+ assert _kinds(_facts(ownership=before), _facts(ownership=after)) == []
+
+
+def test_old_snapshot_does_not_report_a_new_tie_as_a_handover():
+ singles = dict.fromkeys("bcdefghij", 1)
+ before = _owned_by({"alice": 9, **singles})
+ before.pop("margin_tied_owners") # Persisted before the margin fact existed.
+ after = _owned_by({"alice": 9, **singles, "aaron": 1})
+ assert _kinds(_facts(ownership=before), _facts(ownership=after)) == []
+
+
+def test_real_owner_handover_survives_a_tied_margin():
+ singles = dict.fromkeys("bcdefghij", 1)
+ before = _owned_by({"alice": 9, **singles})
+ after = _owned_by({"zoe": 9, **singles})
+ assert _kinds(_facts(ownership=before), _facts(ownership=after)) == [
+ ("ownership_shift", "observed")
+ ]
+
+
+def test_non_tied_multi_file_owner_handover_still_fires():
+ before = _owned_by({"alice": 9, "bob": 3, **dict.fromkeys("cdefghi", 1)})
+ after = _owned_by({"alice": 9, "zoe": 3, **dict.fromkeys("cdefghi", 1)})
+ assert _kinds(_facts(ownership=before), _facts(ownership=after)) == [
+ ("ownership_shift", "observed")
+ ]
+
+
def test_owner_inactive_only_while_others_still_change_it():
gone = {"active_main_owners": 0}
assert _kinds(_facts(), _facts(ownership=gone)) == [("owner_inactive", "")]