: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
"""The inbox trigger: auto-review self-driving (Signals) implementations when they produce a PR.
A `post_save` receiver on `tasks.TaskRun`, connected through the tasks facade hook (tasks never imports
review_hog, keeping every product edge in the existing direction). The trigger is the save that
records a review target on the run's `output` — NOT run completion: on the tasks architecture a
successful run deliberately never leaves `in_progress` (it stays followable and the PR follow-up
loop babysits the PR; see `execute_sandbox/workflow.py::_maybe_record_terminal_status`), so a
completion-gated trigger would never fire. The review is gated by the **report's assignees**: the
users the Inbox "For you" filter and its Slack notifications resolve from the report's latest
`suggested_reviewers` artefact. The acting reviewer — whose `review_inbox_prs` gates the review
and whose perspectives / validator / urgency threshold drive it — is the task's own user when they
are among the assigned reviewers (whoever clicked "Create PR", or the auto-start assignee), else
the first assigned reviewer that resolves to an org member. `Task.created_by` alone carries no
assignment meaning (background signals tasks are created as the GitHub-integration creator), which
is why it only counts when it maps into the assigned set.
The assigned reviewers carry a second, independent toggle: `stamphog_review_inbox_prs` sends the PR
to hosted Stamphog for an approve-first review with a real GitHub approval, through the stamphog
facade (`queue_inbox_pr_review`). Any assigned reviewer's opt-in is enough, unlike the
acting-reviewer gate above: stamphog reads no per-user options, so narrowing it to one reviewer
would only drop reviews the other assignees asked for. That call queues the first review only;
later pushes are re-reviewed by stamphog's own webhook path, which re-checks the same toggle
through the resolver registered in `connect()` (stamphog cannot import review_hog back without a
dependency cycle).
The review target is `output.pr_url`, written by the agent server when it observes the agent open
the PR, or by the GitHub-webhook backstop (`tasks/webhooks.py`). A pushed branch without a PR
(`output.head_branch`, synced at the end of every agent turn) is not a target: a review of a branch
whose PR never opens is spend with no reader, and the PR save re-fires this receiver, so waiting
for the PR costs only the head start. The workflow and client keep their branch-target support for
callers that know what they are doing; this receiver just never uses it.
The `TaskRun.branch` FIELD is never used as a target: auto-start seeds it with the BASE branch and
the agent server later overwrites it with the work branch, so its meaning depends on the path taken.
"""
import logging
from typing import Any
from django.db import transaction
from products.review_hog.backend.models import ReviewUserSettings
from products.review_hog.backend.preferences import ReviewPreferences
from products.signals.backend.facade.report_artefacts import persisted_report_priority
from products.signals.backend.facade.taxonomy import ReportPriority
from products.stamphog.backend.facade.finished_review_hooks import (
FinishedReviewRun,
register_finished_review_hook as register_stamphog_finished_review_hook,
)
from products.stamphog.backend.facade.inbox_hooks import register_inbox_acting_reviewer_resolver
from products.stamphog.backend.facade.review_evidence import (
ReviewEvidence,
ReviewEvidenceState,
register_review_evidence_provider,
)
from products.tasks.backend.facade.task_run_signals import connect_task_run_post_save
# This module loads during django.setup() (AppConfig.ready() wires the receiver), and
# posthog/test/repo_invariants/test_startup_import_budget.py forbids temporalio/modal/openai/anthropic at setup —
# the temporal client and the stamphog task module reach all four, so those two imports stay
# function-local in _start_review / _start_stamphog_review per the budget test's own prescription.
logger = logging.getLogger(__name__)
# The name stamphog shows for these reviews, the same name the reviews carry on GitHub.
REVIEWER_NAME = "PostHog Review"
def connect() -> None:
"""Wire the TaskRun-save receiver and the stamphog hooks; called once from `AppConfig.ready()`."""
connect_task_run_post_save(handle_task_run_saved, dispatch_uid="review_hog_task_run_completed")
# Stamphog re-checks this toggle before re-reviewing a self-driving PR. It gets the resolver
# through a hook because importing review_hog from stamphog would be a dependency cycle.
register_inbox_acting_reviewer_resolver(resolve_stamphog_acting_reviewer)
# Stamphog waits for a pending review of the head it judges and counts a completed one as
# assurance. The same dependency cycle rules out a direct import.
register_review_evidence_provider(review_evidence_for_stamphog)
# Stamphog asks for a review of a self-driving PR it refused or escalated. The same dependency
# cycle rules out a direct import.
register_stamphog_finished_review_hook(queue_stamphog_handoff)
def queue_stamphog_handoff(finished: FinishedReviewRun) -> None:
"""Queue the review Stamphog asks for, off Stamphog's activity."""
if not finished.request_external_review:
return
from products.review_hog.backend.tasks import process_stamphog_handoff # noqa: PLC0415
process_stamphog_handoff.delay(
repository=finished.repository,
pr_number=finished.pr_number,
head_sha=finished.head_sha,
author_login=finished.author_login,
head_branch=finished.head_branch,
)
def review_evidence_for_stamphog(repository: str, pr_number: int, head_sha: str) -> ReviewEvidence | None:
"""Map review_hog's own head status onto stamphog's evidence contract."""
# The lookup reads the review pipeline's modules, which reach openai, anthropic and temporalio.
from products.review_hog.backend.facade.review_status import review_status_for_head # noqa: PLC0415
status = review_status_for_head(repository, pr_number, head_sha)
if status is None:
return None
return ReviewEvidence(
state=ReviewEvidenceState(status.state.value),
reviewer=REVIEWER_NAME,
review_kind=status.review_kind,
findings_count=status.findings_count,
published=status.published,
head_sha=status.head_sha,
base_ref=status.base_branch,
base_sha=status.base_sha,
skipped_paths=status.skipped_paths,
)
def handle_task_run_saved(sender: type, instance: Any, created: bool, **kwargs: Any) -> None:
"""Start an inbox review when a signals-origin implementation run records its PR.
Fires on every TaskRun save (a hot model), so the checks run cheapest-first and the whole body is
exception-proof — this executes inside tasks' save path and must never raise into it. Repeat
saves with an unchanged target re-fire it deliberately: the deterministic workflow id +
USE_EXISTING collapse duplicates while a review runs, and a same-head re-trigger costs one fetch
(early-exit).
"""
try:
if created:
# Runs are created before the agent does anything; targets arrive via later saves.
return
update_fields = kwargs.get("update_fields")
if update_fields is not None and "output" not in update_fields:
# Declared-fields saves that don't touch `output` (follow-up state persistence,
# status flips) can't carry a new review target — drop them without a DB hit.
return
if instance.status in (instance.Status.FAILED, instance.Status.CANCELLED):
return
output = instance.output if isinstance(instance.output, dict) else {}
pr_url = output.get("pr_url") or None
if pr_url is None:
return
task = instance.task # first DB hit
signal_report_id = task.signal_report_source_id
if signal_report_id is None:
return
if task.deleted:
# A soft-deleted task's run can still save output. Its PR is disowned work, and the
# webhook leg already drops deleted tasks (find_signal_implementation_run), so the
# first review must gate the same way instead of minting the approval the delete disavowed.
return
# Only the self-driving implementation run reviews. Report research and repo selection share
# both signal_report_id and internal=True with it, so only ai_stage tells them apart.
if (instance.state or {}).get("ai_stage") != "implementation":
return
repository = (task.repository or "").strip() or None
resolved = resolve_assigned_reviewers(instance.team_id, signal_report_id)
if not resolved:
return
settings_by_user = ReviewUserSettings.load_preferences_many(instance.team_id, resolved)
acting_user_id = pick_reviewer(resolved, task.created_by_id)
stamphog_user_id = _pick_stamphog_reviewer(resolved, settings_by_user, task.created_by_id)
# robust=True on both: the two dispatches are independent but share one commit-hook queue, so
# a failure in one must not cancel the other. Django logs the failing hook and runs the rest.
if settings_by_user[acting_user_id].review_inbox_prs:
# The priority as it stood when the implementation task was created. The agent can append
# judgments through the artefact API, so a later one may be its own vote on its review.
signal_priority = persisted_report_priority(
team_id=instance.team_id, report_id=str(signal_report_id), before=task.created_at
)
transaction.on_commit(
lambda: _start_review(
pr_url=pr_url,
team_id=instance.team_id,
user_id=acting_user_id,
signal_report_id=str(signal_report_id),
signal_priority=signal_priority,
repository=repository,
),
robust=True,
)
if stamphog_user_id is not None and repository is not None:
# Queuing a Celery task keeps this save path fast.
stamphog_pr_url, stamphog_repository = pr_url, repository
transaction.on_commit(
lambda: _start_stamphog_review(
pr_url=stamphog_pr_url,
repository=stamphog_repository,
team_id=instance.team_id,
acting_user_id=stamphog_user_id,
signal_report_id=str(signal_report_id),
task_run_id=str(instance.id),
),
robust=True,
)
except Exception:
logger.exception("review_hog_inbox_trigger_failed")
def resolve_stamphog_acting_reviewer(team_id: int, signal_report_id: str, preferred_user_id: int | None) -> int | None:
"""A reviewer whose stamphog inbox toggle is currently on, else None.
Registered with stamphog's inbox hook registry (see `connect()`). Stamphog calls it before
re-reviewing a self-driving PR on a later push and again when the queued first review executes,
so turning the toggle off mid-PR stops further stamphog runs. It has to gate the same way the
initial trigger does: if one path fires on "any assigned reviewer opted in" and the other only
on the canonical reviewer, a push would treat the PR as opted out and retract a standing
approval while somebody is still opted in. ``preferred_user_id`` (the task's creator, or the
reviewer a queued job was attributed to) wins while still opted in, keeping attribution stable.
"""
resolved = resolve_assigned_reviewers(team_id, signal_report_id)
if not resolved:
return None
settings_by_user = ReviewUserSettings.load_preferences_many(team_id, resolved)
return _pick_stamphog_reviewer(resolved, settings_by_user, preferred_user_id)
def _pick_stamphog_reviewer(
resolved: list[int], settings_by_user: dict[int, ReviewPreferences], preferred_user_id: int | None
) -> int | None:
"""The user id to attribute a stamphog inbox review to, or None when nobody is opted in.
Any assigned reviewer's opt-in is enough, unlike the ReviewHog review which gates on the
canonical reviewer's: stamphog reads no per-user options, so the id is provenance only and
narrowing to one reviewer would drop reviews the other assignees asked for. Among the opted-in
reviewers the pick follows the same preferred-else-first rule, so the same user is attributed
on the first review and on later re-reviews. Either way there is one review per head: this
returns a single id however many are opted in, and stamphog dedupes on (pull request, head sha).
"""
opted_in = [user_id for user_id in resolved if settings_by_user[user_id].stamphog_review_inbox_prs]
return pick_reviewer(opted_in, preferred_user_id) if opted_in else None
def pick_reviewer(resolved: list[int], task_created_by_id: int | None) -> int:
"""The canonical reviewer out of a non-empty resolved list: the task's own user, else the first.
The task's user (`created_by`: the auto-start assignee, or whoever clicked "Create PR") wins when
they are among the resolved reviewers, so someone who asked for the implementation gets their own
rules applied to its review. Otherwise the first resolved reviewer is canonical (maintainer
decisions, 2026-07-02/03): a background task whose creator carries no assignment meaning, or a
non-assigned clicker, follows the primary assignee's rules.
"""
if task_created_by_id is not None and task_created_by_id in resolved:
return task_created_by_id
return resolved[0]
def resolve_assigned_reviewers(team_id: int, signal_report_id: Any) -> list[int]:
"""The user ids of the report's assigned reviewers, in assignment order (no opt-in toggles checked here).
Assignment means the report's latest `suggested_reviewers` artefact, the same set the Inbox "For
you" filter matches and Slack notifications fan out to, with logins resolved to org members the
same way those surfaces resolve them. Callers decide which of them gates what: ReviewHog runs
under the canonical reviewer (`pick_reviewer`, whose perspectives, blind spots, validator, and
urgency threshold drive the review), stamphog under any who opted in (`_pick_stamphog_reviewer`).
Empty when the report has no reviewers or none resolve to an org member.
"""
# Function-local: pulls the signals contracts module (pydantic dataclasses), which the
# startup-import-budget test forbids at django.setup() — see the module-top comment.
from products.signals.backend.facade.report_reviewers import ( # noqa: PLC0415
org_user_ids_by_github_login,
suggested_reviewer_logins,
)
logins = suggested_reviewer_logins(team_id=team_id, report_id=str(signal_report_id))
if not logins:
return []
user_id_by_login = org_user_ids_by_github_login(team_id=team_id, github_logins=logins)
return [user_id_by_login[login] for login in logins if login in user_id_by_login]
def _start_stamphog_review(
*, pr_url: str, repository: str, team_id: int, acting_user_id: int, signal_report_id: str, task_run_id: str
) -> None:
"""Fire-and-forget the hosted Stamphog review; the broker being down must never surface into the saver.
The queued task checks the repo config (a synced, enabled StamphogRepoConfig for the PR's
repository), so teams without the Stamphog App get a silent no-op there and this stays a plain
toggle-gated dispatch. `repository` is the task's own repo, which the queued task requires the
PR to be in; `output.pr_url` is writable through the task-run API, so the PR alone isn't trusted.
"""
# Function-local: pulls the stamphog task module (temporalio via its workflow client), which
# the startup-import-budget test forbids at django.setup() — see the module-top comment.
from products.stamphog.backend.facade.tasks import queue_inbox_pr_review # noqa: PLC0415
try:
queue_inbox_pr_review(
team_id=team_id,
pr_url=pr_url,
repository=repository,
acting_user_id=acting_user_id,
signal_report_id=signal_report_id,
task_run_id=task_run_id,
)
logger.info("review_hog_stamphog_inbox_review_queued: pr %s for signal report %s", pr_url, signal_report_id)
except Exception:
logger.exception("review_hog_stamphog_inbox_review_queue_failed")
def _start_review(
*,
pr_url: str,
team_id: int,
user_id: int,
signal_report_id: str,
signal_priority: ReportPriority | None,
repository: str | None,
) -> None:
"""Fire-and-forget the review workflow; Temporal being down must never surface into the saver.
Binds the PR to the task's own repository: `output.pr_url` is written by whoever controls the
run, the sandbox agent included, so an unbound URL could aim the team's GitHub credential at any
PR the installation reaches (the stamphog leg applies the same rule). Then honors the busy-guard,
the same refusal the trigger/resolve endpoints apply, so an inbox review never starts while this
PR's resolution run is still committing to the branch.
"""
# Function-local: importing the temporal client executes the review_hog temporal package, whose
# activity registration reaches temporalio, modal, openai, and anthropic — all four forbidden at
# django.setup() by the startup-import-budget test. See the module-top comment.
from products.review_hog.backend.review_request_rules import trigger_review_mode # noqa: PLC0415
from products.review_hog.backend.reviewer.tools.github_meta import PRParser # noqa: PLC0415
from products.review_hog.backend.temporal.client import start_review_pr_workflow, workflow_running # noqa: PLC0415
from products.review_hog.backend.temporal.types import TRIGGER_INBOX, resolve_pr_workflow_id # noqa: PLC0415
try:
pr_info = PRParser().parse_github_pr_url(pr_url)
pr_repository = f"{pr_info['owner']}/{pr_info['repo']}"
# GitHub slugs are case-insensitive; the task row lowercases its own.
if repository is None or pr_repository.lower() != repository.lower():
logger.warning(
"review_hog_inbox_review_skipped_repository_mismatch: %s is not in task repository %s (signal report %s)",
pr_url,
repository,
signal_report_id,
)
return
# Busy-guard (CONTEXT.md): a published inbox review chains its own resolution, which
# commits to the branch for minutes — starting a fresh review meanwhile races those
# pushes and re-reviews threads mid-settlement. The chained hand-off starts resolution
# as a child workflow, never through this path, so it stays exempt.
if workflow_running(
resolve_pr_workflow_id(
team_id=team_id,
owner=str(pr_info["owner"]),
repo=str(pr_info["repo"]),
pr_number=int(pr_info["pr_number"]),
)
):
logger.info(
"review_hog_inbox_review_skipped_busy: resolution still running for %s (signal report %s)",
pr_url,
signal_report_id,
)
return
workflow_id = start_review_pr_workflow(
team_id=team_id,
user_id=user_id,
publish=True,
acting_user_id=user_id,
trigger_source=TRIGGER_INBOX,
review_mode=trigger_review_mode(team_id, pr_repository, int(pr_info["pr_number"])),
signal_report_id=signal_report_id,
signal_priority=signal_priority,
pr_url=pr_url,
)
logger.info("review_hog_inbox_review_started: workflow %s for signal report %s", workflow_id, signal_report_id)
except Exception:
logger.exception("review_hog_inbox_review_start_failed")