: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
feat(review-hog): default acting-user fallback for labeled reviews (#71045)
Alex Lebedev064fd9f46277
diff --git a/products/review_hog/ARCHITECTURE.md b/products/review_hog/ARCHITECTURE.mdindex 4a96aad20281..0a41693c369a 100644--- a/products/review_hog/ARCHITECTURE.md+++ b/products/review_hog/ARCHITECTURE.md@@ -2690,17 +2690,20 @@ are enabled by the user via the toggle. A new canonical added later auto-enablesrow the user disabled is left alone (get_or_create no-op). Scouts call register-missing each coordinator tick(`config_registry.py`); we call it when resolving the acting user, before loading.-**PR-author → PostHog user (mandatory gate, no fallback).** The acting user whose perspectives apply = the **PR-author**, from `pr_metadata.author` (`tools/github_meta.py:244`), resolved via **`resolve_org_github_login_to_users(-team_id, [login]) -> {login: User}`** (`products/signals/backend/report_generation/resolve_reviewers.py:241`;-org-scoped, case-insensitive, backed by `User.get_github_login()` precedence UserIntegration-github →-UserSocialAuth-github → team-integration login, `posthog/models/user.py:438`; already used in prod). **CORRECTION-to earlier docs: this reverse-lookup DOES exist** (a prior research pass wrongly said it didn't). If the author does-**not** map to a PostHog org user → **do not review** (no fallback / no canonical default — we rely on-PostHog-stored skills, so the author must be a PostHog user with an org account). Gate **after fetch, before the-perspective fan-out** — resolution is needed for selection anyway, so no dead code, nothing wasted. `inputs.user_id`-today is the integration _creator_ (`trigger.py:54-57, 125`), NOT the PR author — keep it for sandbox/MCP auth, but-resolve the PR author separately for perspective selection.+**PR-author → PostHog user (label trigger falls back; other triggers don't).** The acting user whose perspectives+apply = the **PR author**, from `pr_metadata.author` (`tools/github_meta.py:244`), resolved via+**`resolve_org_github_login_to_users(team_id, [login]) -> {login: User}`**+(`products/signals/backend/report_generation/resolve_reviewers.py:241`; org-scoped, case-insensitive, backed by+`User.get_github_login()` precedence UserIntegration-github → UserSocialAuth-github → team-integration login,+`posthog/models/user.py:438`; already used in prod). **CORRECTION to earlier docs: this reverse-lookup DOES exist**+(a prior research pass wrongly said it didn't). When the author does **not** map to a PostHog org user, the **label+trigger** falls back to the **run user** (`inputs.user_id`, threaded into the resolve activity as `default_user_id`+— the same identity the sandboxes execute under, so acting and sandbox identity can never drift) — someone+explicitly asked for the review, so a borrowed user's perspectives beat no review. The+`review_labeled_prs` opt-out stays **author-only**: a fallback-resolved acting user never imports their personal+switch into someone else's PR (forced True in the resolve snapshot). Inbox/manual/branch triggers keep the+no-fallback contract → unmapped author = **no review**. Gate **after fetch, before the perspective fan-out**.+`inputs.user_id` is the sandbox/MCP identity (integration creator lineage), resolved separately from the acting user.**Loader change** — `load_perspectives_for_run(team_id, acting_user_id)` (`skill_loader.py:50`): after`register_missing_perspective_configs`, read the user's enabled setdiff --git a/products/review_hog/backend/temporal/activities.py b/products/review_hog/backend/temporal/activities.pyindex b58b6694330e..547dc1c0261f 100644--- a/products/review_hog/backend/temporal/activities.py+++ b/products/review_hog/backend/temporal/activities.py@@ -126,7 +126,7 @@ from products.review_hog.backend.reviewer.tools.split_pr_into_chunks import (plan_deterministic_chunks,reconcile_chunks,)-from products.review_hog.backend.temporal.types import TRIGGER_MANUAL+from products.review_hog.backend.temporal.types import TRIGGER_LABEL, TRIGGER_MANUALfrom products.signals.backend.artefact_attribution import ArtefactAttributionfrom products.signals.backend.artefact_schemas import CodeReview, CodeReviewCountsfrom products.signals.backend.models import SignalReport, SignalReportArtefact@@ -199,18 +199,27 @@ class ResolveActingUserInput:# When set, the resolved acting user is stamped onto this report (powers "your recent reviews").# Defaulted so payloads serialized before the field existed still deserialize.report_id: str | None = None+ # Which trigger runs: the label trigger falls back to `default_user_id` on an unmapped author —+ # the run user the trigger already resolved (`inputs.user_id`), so the acting identity can never+ # drift from the identity the sandboxes execute under. Defaulted for old in-flight payloads.+ trigger_source: str = TRIGGER_MANUAL+ default_user_id: int | None = None@dataclassclass ResolveActingUserResult:- # The user whose enabled perspectives drive this review; None when the PR author maps to no PostHog- # org user — we apply PostHog-stored skills, so the parent then skips the review.+ # The user whose enabled perspectives drive this review; None when nothing in the resolution+ # chain maps to a PostHog org user — the parent then skips the review.acting_user_id: int | None# The acting user's `ReviewUserSettings`, snapshotted here so mid-run edits don't flip gates# between stages. Defaults match the model's (and cover payloads serialized before these existed).+ # `review_labeled_prs` is the AUTHOR's opt-out: when the acting user is the default-user+ # fallback, it is forced True — a borrowed user's personal switch never governs someone else's PR.review_labeled_prs: bool = Trueurgency_threshold: str = IssuePriority.SHOULD_FIX.valuereview_inbox_prs: bool = False+ # Which chain link resolved: "author" | "default" | "override" (observability + tests).+ resolved_from: str = "author"@dataclass@@ -557,27 +566,48 @@ async def fetch_pr_data_activity(input: FetchPRDataInput) -> ReviewMeta:return await database_sync_to_async(_fetch_and_persist, thread_sensitive=False)(input)+def _login_to_user_id(team_id: int, login: str | None) -> int | None:+ if not login:+ return None+ matches = resolve_org_github_login_to_users(team_id, [login])+ user = matches.get(login.strip().lower())+ return user.id if user is not None else None++def _resolve_acting_user(- team_id: int, author_login: str, override_user_id: int | None, report_id: str | None = None+ team_id: int,+ author_login: str,+ override_user_id: int | None,+ report_id: str | None = None,+ trigger_source: str = TRIGGER_MANUAL,+ default_user_id: int | None = None,) -> ResolveActingUserResult:+ acting_user_id: int | Noneif override_user_id is not None:- acting_user_id: int | None = override_user_id+ acting_user_id, resolved_from = override_user_id, "override"else:- matches = resolve_org_github_login_to_users(team_id, [author_login])- user = matches.get(author_login.strip().lower()) if author_login else None- acting_user_id = user.id if user is not None else None+ acting_user_id, resolved_from = _login_to_user_id(team_id, author_login), "author"+ # Label-trigger fallback — someone explicitly asked for this review, so borrow the run user+ # the trigger already resolved. Other triggers keep the author-only contract and skip.+ if acting_user_id is None and trigger_source == TRIGGER_LABEL:+ acting_user_id, resolved_from = default_user_id, "default"if acting_user_id is None:return ResolveActingUserResult(acting_user_id=None)+ if resolved_from == "default":+ logger.info("PR author %r has no PostHog user; acting as the default user %s", author_login, acting_user_id)if report_id is not None:ReviewReport.objects.for_team(team_id).filter(id=report_id).update(acting_user_id=acting_user_id)settings = ReviewUserSettings.load(team_id, acting_user_id)return ResolveActingUserResult(acting_user_id=acting_user_id,- review_labeled_prs=settings.review_labeled_prs,+ # The labeled-PR opt-out protects authors ("don't review my PRs") — the borrowed default+ # user never imports their personal switch into someone else's PR.+ review_labeled_prs=settings.review_labeled_prs if resolved_from in ("author", "override") else True,# str() unwraps the TextChoices member an unsaved defaults instance carries — the payload# must hold the plain value.urgency_threshold=str(settings.urgency_threshold),review_inbox_prs=settings.review_inbox_prs,+ resolved_from=resolved_from,)@@ -588,12 +618,17 @@ async def resolve_acting_user_activity(input: ResolveActingUserInput) -> Resolve"""Resolve the user whose enabled perspectives drive this review, plus their settings snapshot.Production: the PR author, mapped GitHub-login → PostHog org user (`resolve_org_github_login_to_users`).- Returns None when the author isn't a PostHog org user — the parent then skips the review (no- fallback: we apply the author's PostHog-stored perspectives, so the author must be a PostHog user).+ The label trigger falls back to the default run user (someone explicitly asked for the review);+ other triggers return None on an unmapped author and the parent skips the review.The CLI/eval passes an explicit `override_user_id` to test a known user's perspectives on any PR."""return await database_sync_to_async(_resolve_acting_user, thread_sensitive=False)(- input.team_id, input.author_login, input.override_user_id, input.report_id+ input.team_id,+ input.author_login,+ input.override_user_id,+ input.report_id,+ input.trigger_source,+ input.default_user_id,)diff --git a/products/review_hog/backend/temporal/types.py b/products/review_hog/backend/temporal/types.pyindex db39c08da31a..3167ebe63a42 100644--- a/products/review_hog/backend/temporal/types.py+++ b/products/review_hog/backend/temporal/types.py@@ -29,8 +29,9 @@ class ReviewPRWorkflowInputs:no resolvable PR stores the review instead — the target's shape decides, not a mode flag.`acting_user_id` overrides whose perspectives run: the label trigger leaves it None (the workflow- resolves the PR author after fetch, skipping if not a PostHog user); the eval CLI and the inbox- trigger set it explicitly (the inbox PR author is a bot, so it can't be resolved from GitHub).+ resolves the PR author after fetch, falling back to the default run user when the author isn't a+ PostHog user); the eval CLI and the inbox trigger set it explicitly (the inbox PR author is a+ bot, so it can't be resolved from GitHub).`trigger_source` / `signal_report_id` default so in-flight payloads serialized before thesefields existed still deserialize.diff --git a/products/review_hog/backend/temporal/workflow.py b/products/review_hog/backend/temporal/workflow.pyindex be281e320aeb..e2d078953f64 100644--- a/products/review_hog/backend/temporal/workflow.py+++ b/products/review_hog/backend/temporal/workflow.py@@ -386,9 +386,10 @@ class ReviewPRWorkflow:)return report_id- # Resolve the acting user whose enabled perspectives apply (PR author, or the explicit- # override the CLI and inbox triggers set). Gate here, before any sandbox spend: a non-PostHog- # author has no perspectives, so skip the review.+ # Resolve the acting user whose enabled perspectives apply: the PR author, or the explicit+ # override the CLI and inbox triggers set. The label trigger falls back to the default run+ # user on an unmapped author (someone explicitly asked for this review); other triggers keep+ # the author-only contract, so an unmapped author still skips there. Gate before sandbox spend.acting = await workflow.execute_activity(resolve_acting_user_activity,ResolveActingUserInput(@@ -396,6 +397,8 @@ class ReviewPRWorkflow:author_login=meta.author_login,override_user_id=inputs.acting_user_id,report_id=report_id,+ trigger_source=inputs.trigger_source,+ default_user_id=inputs.user_id,),start_to_close_timeout=_QUICK_TIMEOUT,retry_policy=_RETRY,diff --git a/products/review_hog/backend/tests/test_acting_user.py b/products/review_hog/backend/tests/test_acting_user.pyindex 75b9bcd9ce9a..c96070183438 100644--- a/products/review_hog/backend/tests/test_acting_user.py+++ b/products/review_hog/backend/tests/test_acting_user.py@@ -5,6 +5,7 @@ from social_django.models import UserSocialAuthfrom products.review_hog.backend.models import ReviewReport, ReviewUserSettingsfrom products.review_hog.backend.temporal.activities import _resolve_acting_user+from products.review_hog.backend.temporal.types import TRIGGER_LABEL, TRIGGER_MANUAL_SELF = "SELF"@@ -56,6 +57,35 @@ class TestResolveActingUser(BaseTest):assert result.review_inbox_prs is Trueassert result.urgency_threshold == "must_fix"+ def test_label_trigger_falls_back_to_the_run_user(self) -> None:+ # Unmapped author on a labeled PR → the run user the trigger already resolved, passed+ # through as default_user_id so acting and sandbox identity can never drift.+ result = _resolve_acting_user(+ self.team.id, "ghost", None, trigger_source=TRIGGER_LABEL, default_user_id=self.user.id+ )+ assert (result.acting_user_id, result.resolved_from) == (self.user.id, "default")++ def test_non_label_triggers_keep_the_author_only_contract(self) -> None:+ result = _resolve_acting_user(+ self.team.id, "ghost", None, trigger_source=TRIGGER_MANUAL, default_user_id=self.user.id+ )+ assert result.acting_user_id is None++ def test_labeled_opt_out_protects_authors_but_never_travels_with_a_borrowed_user(self) -> None:+ # self.user is both the mapped author (octocat) and the run-user fallback.+ ReviewUserSettings.objects.for_team(self.team.id).create(+ team_id=self.team.id, user_id=self.user.id, review_labeled_prs=False+ )+ # Acting as the author: their own opt-out applies and the workflow will skip.+ as_author = _resolve_acting_user(self.team.id, "octocat", None, trigger_source=TRIGGER_LABEL)+ assert (as_author.resolved_from, as_author.review_labeled_prs) == ("author", False)+ # Acting as the borrowed run user on someone else's PR: the same row must NOT kill the review.+ as_default = _resolve_acting_user(+ self.team.id, "ghost", None, trigger_source=TRIGGER_LABEL, default_user_id=self.user.id+ )+ assert (as_default.acting_user_id, as_default.resolved_from) == (self.user.id, "default")+ assert as_default.review_labeled_prs is True+def test_resolve_stamps_the_acting_user_onto_the_report(self) -> None:# "Your recent reviews" filters on this stamp — if resolve stops writing it, the list goes empty.report = ReviewReport.objects.for_team(self.team.id).create(