:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
"""Resolve a merged PR to the digest audiences it belongs to.
Ownership decides who hears about a merge:
- ``owned``: one audience for each team that owns a file the PR changed. The review already
resolved that ownership against the checkout (see models.PullRequestAudience).
- ``repo_declared``: one audience for a repo that declared its own digest channel (see
logic/digest_config.py). It carries every merge in that repo to one place, whoever owns what.
A standalone repo needs this.
The author's own team is deliberately not an audience. A team hears about code it owns, not about
everywhere its members touched. A merge that no team owns, in a repo that declares nothing,
therefore reaches nobody: that is an ownership gap, and ``hogli owners:unowned`` is where it gets
fixed.
Digest grouping and channel routing key off the opaque audience string alone (see
logic/channel_resolution.py).
"""
from __future__ import annotations
import re
from dataclasses import field
from typing import TYPE_CHECKING, Any
import structlog
from posthog.dataclasses import frozen
from ..facade.enums import AudienceReason
from .digest_config import load_repo_digest_config
if TYPE_CHECKING:
from ..models import StamphogRepoConfig
logger = structlog.get_logger(__name__)
# The engine reports owning teams as GitHub handles; audience keys are bare slugs, which is what
# channel resolution looks up in a repository's teams: registry.
_TEAM_HANDLE_PREFIX = "@"
# Namespace for the audience a repository gets by declaring its own digest channel, kept distinct
# from the team slugs that share the key space. Read back by logic/channel_resolution.py, which
# routes it straight to the declared channel.
REPO_AUDIENCE_PREFIX = "repo:"
# Ownership is resolved from owners.yaml in the PR-HEAD checkout — unlike .stamphog/*, those files
# are not replaced with default-branch copies, so an owner string is attacker-controlled. A slug is
# only ever a GitHub team name, so anything else is rejected rather than sanitized. This is what
# keeps a crafted owner out of the reserved "repo:" namespace, which skips the shared-channel guard
# on the grounds that a repo maintainer chose that channel for their own repository.
_TEAM_SLUG_RE = re.compile(r"^[A-Za-z0-9._-]+$")
@frozen
class _OwnerTeam:
"""One team's stake in a PR: its slug, a capped sample of its changed paths, and how many
source files it owns in the PR."""
slug: str
files: list[str]
file_count: int
@frozen
class ResolvedAudience:
key: str
reason: AudienceReason
# Capped sample of this team's changed paths; empty unless reason is OWNED.
owned_files: list[str] = field(default_factory=list)
# How many files this team owns in the PR, uncapped, and counting only what a person edited.
owned_file_count: int = 0
def team_slug_from_handle(handle: str) -> str:
"""The bare slug in a GitHub team handle: `@PostHog/team-devex` becomes `team-devex`.
An audience key is this slug, and the digest matches the reviewer's per-team clauses back to
one by reading their handles the same way (see logic/digest.py), so the two agree by
construction rather than by two copies of the rule.
"""
return handle.partition("/")[2]
def _repository_audience_key(repo_config: StamphogRepoConfig) -> str:
return f"{REPO_AUDIENCE_PREFIX}{repo_config.repository}"
def _owner_teams(gate_result: dict[str, Any] | None) -> list[_OwnerTeam]:
"""Every team owning a changed file, with its path sample and true file count.
The review walked the real checkout to get this, which the digest cannot do later. Anything
unexpected in the blob resolves to "no owners" rather than raising: a merge must still be
captured when the ownership section is missing or an older engine never wrote it.
The count is the team's source files: the files a build step wrote are subtracted from it, so a
team that owns one real file plus a regenerated one reads as owning one. Both the graze rule in
logic/digest.py and the prompt read that count, and a build artifact inflating it hid a sweep
from both. A team left with no source file is dropped here rather than passed to the digest
prompt, which carried the same rule in words and did not hold to it. The engine counts the
generated files per team (gates.detect_ownership), so a run recorded before it did that carries
no count, subtracts nothing, and drops nobody.
"""
ownership = ((gate_result or {}).get("classification") or {}).get("ownership") or {}
teams = ownership.get("teams")
if not isinstance(teams, list):
return []
files_by_team = ownership.get("team_files")
if not isinstance(files_by_team, dict):
files_by_team = {}
counts_by_team = ownership.get("team_file_counts")
if not isinstance(counts_by_team, dict):
counts_by_team = {}
generated_by_team = ownership.get("team_generated_file_counts")
if not isinstance(generated_by_team, dict):
generated_by_team = {}
owners = {}
for team in teams:
if not isinstance(team, str) or not team.startswith(_TEAM_HANDLE_PREFIX):
continue
# A handle without an organization is not a team, and the slug regex below rejects it.
slug = team_slug_from_handle(team)
if not _TEAM_SLUG_RE.match(slug):
logger.warning("stamphog_owner_team_slug_rejected", team=team)
continue
paths = files_by_team.get(team)
sample = [p for p in paths if isinstance(p, str)] if isinstance(paths, list) else []
count = counts_by_team.get(team)
# Fall back to the sample size when the count is missing or nonsense; never below it.
file_count = max(count, len(sample)) if isinstance(count, int) else len(sample)
generated_count = generated_by_team.get(team)
# Clamped at zero: file_count falls back to the capped sample size when the count is
# missing, and the generated tally is uncapped, so it can legitimately exceed it.
source_count = max(file_count - generated_count, 0) if isinstance(generated_count, int) else file_count
if file_count > 0 and source_count == 0:
logger.info("stamphog_owner_team_generated_only", team=team, file_count=file_count)
continue
owners[slug] = (sample, source_count)
return [_OwnerTeam(slug=slug, files=sample, file_count=count) for slug, (sample, count) in sorted(owners.items())]
def resolve_audiences(
repo_config: StamphogRepoConfig,
gate_result: dict[str, Any] | None = None,
) -> list[ResolvedAudience]:
"""Every audience a merged PR belongs to. Empty when nobody owns it and no repo claimed it.
Membership is deliberately generous: a team owning a single changed file still gets an
audience. Whether the PR is worth mentioning to that team is a later, per-digest judgment.
A repo-declared audience sits alongside the owning teams rather than replacing them, so a
shared repo still tells each team about its own area while a standalone repo gets the single
feed it asked for.
"""
audiences = []
digest_config = load_repo_digest_config(repo_config)
if digest_config is not None and digest_config.channel:
audiences.append(
ResolvedAudience(key=_repository_audience_key(repo_config), reason=AudienceReason.REPO_DECLARED)
)
for owner in _owner_teams(gate_result):
audiences.append(
ResolvedAudience(
key=owner.slug,
reason=AudienceReason.OWNED,
owned_files=owner.files,
owned_file_count=owner.file_count,
)
)
return audiences