Skip to content

: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.

feat(review-hog): start Standard reviews from the label and the Inbox (#115917)

Julian Bez52d6357f57fb
diff --git a/products/review_hog/backend/label_reviews.py b/products/review_hog/backend/label_reviews.py
index ee9b9846e0ae..e11948b72436 100644
--- a/products/review_hog/backend/label_reviews.py
+++ b/products/review_hog/backend/label_reviews.py
@@ -1,5 +1,5 @@
"""The label trigger: the `posthog-review` label (or the legacy `reviewhog` label) on a pull request starts a
-Full review.
+Standard review, or a Deep one once the pull request has a published Deep review.
The PostHog GitHub App's own `labeled` delivery starts it, so the trigger works in every repository the
app is installed on and a project owns (`ownership.py`). One label add is one review: pushes do not
@@ -20,12 +20,16 @@ from prometheus_client import Counter
from posthog.dataclasses import frozen
from posthog.egress.limiter.policies import Priority
from posthog.models.integration import GitHubIntegration, Integration
+from posthog.models.team import Team
from posthog.otel_metrics import OtelInstrumentFactory
+from posthog.permissions import posthog_feature_flag_enabled
from products.review_hog.backend.automatic_reviews import connector_user_id
from products.review_hog.backend.ownership import RepositoryOwnership, RepositoryRef
from products.review_hog.backend.pr_owner import PullRequestOwnerResolver
from products.review_hog.backend.pull_request_events import REVIEW_LABEL, REVIEW_LABELS
+from products.review_hog.backend.review_request_rules import trigger_review_mode
+from products.review_hog.backend.reviewer.constants import REVIEW_MODE_FLASH, REVIEW_MODE_FULL
from products.review_hog.backend.reviewer.persistence import lift_review_tier_for_joined_trigger
from products.review_hog.backend.reviewer.tools.github_client import (
GitHubAPIError,
@@ -45,6 +49,31 @@ BOT_LABEL_COMMENT = (
"so the label was removed. Add the label yourself to start a review."
)
+# The rollout flag of `@posthog` pull request comments (products/github_commands). Read here by key,
+# because github_commands already depends on this product.
+COMMENT_COMMANDS_FLAG = "github-commands"
+
+
+def comment_commands_enabled(team_id: int) -> bool:
+ """Whether `@posthog` comments run for this project's organization, so the review may point to them."""
+ organization_id = Team.objects.values_list("organization_id", flat=True).filter(id=team_id).first()
+ if organization_id is None:
+ return False
+ return posthog_feature_flag_enabled(
+ COMMENT_COMMANDS_FLAG, f"review-hog-team-{team_id}", organization_id=organization_id, team_id=team_id
+ )
+
+
+def label_commands_hint(review_mode: str) -> str:
+ """The note a label-started review ends with: which review the label ran, and the commands for more."""
+ if review_mode == REVIEW_MODE_FLASH:
+ return (
+ f"This Standard review started from the `{REVIEW_LABEL}` label. Comment `@posthog review --deep` "
+ "for a Deep review, or `@posthog help` to see all commands."
+ )
+ return f"This Deep review started from the `{REVIEW_LABEL}` label. Comment `@posthog help` to see all commands."
+
+
LabelReviewOutcome = Literal[
"repository_not_added",
"installation_mismatch",
@@ -181,13 +210,17 @@ class LabelReview:
joins_running_review = workflow_running(
review_pr_workflow_id(team_id=team_id, owner=github_owner, repo=github_repo, pr_number=self.pr_number)
)
+ review_mode = trigger_review_mode(team_id, self.repository, self.pr_number)
start_review_pr_workflow(
pr_url=f"https://github.com/{self.repository}/pull/{self.pr_number}",
team_id=team_id,
user_id=run_user_id,
publish=True,
trigger_source=TRIGGER_LABEL,
+ review_mode=review_mode,
)
- if joins_running_review:
+ # Standard is excluded for the same reason as on the comment path: the lift rewrites the
+ # persisted arm, so the cheaper review must not raise what later turns cost.
+ if joins_running_review and review_mode == REVIEW_MODE_FULL:
lift_review_tier_for_joined_trigger(team_id=team_id, repository=self.repository, pr_number=self.pr_number)
_observe("started")
diff --git a/products/review_hog/backend/receivers.py b/products/review_hog/backend/receivers.py
index 3b640d4ec611..daa2b837320a 100644
--- a/products/review_hog/backend/receivers.py
+++ b/products/review_hog/backend/receivers.py
@@ -319,6 +319,7 @@ def _start_review(
# 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
@@ -359,6 +360,7 @@ def _start_review(
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,
diff --git a/products/review_hog/backend/review_request_rules.py b/products/review_hog/backend/review_request_rules.py
index 9de46ec03a07..81fcca7dc926 100644
--- a/products/review_hog/backend/review_request_rules.py
+++ b/products/review_hog/backend/review_request_rules.py
@@ -44,6 +44,17 @@ def full_review_published(report: ReviewReport | None) -> bool:
return report is not None and REVIEW_MODE_FULL in published_heads_by_mode(report)
+def trigger_review_mode(team_id: int, repository: str, pr_number: int) -> str:
+ """The mode a label or Inbox trigger starts: Standard, or Deep once the pull request has a Deep review.
+
+ A Standard request after a published Deep review is dropped, so without the Deep case the trigger
+ would do nothing on such a pull request.
+ """
+ # Repository casing can differ per trigger, like the report lookup in `request_pr_review`.
+ report = ReviewReport.objects.for_team(team_id).filter(repository__iexact=repository, pr_number=pr_number).first()
+ return REVIEW_MODE_FULL if full_review_published(report) else REVIEW_MODE_FLASH
+
+
def flash_refusal(report: ReviewReport | None, review_mode: str) -> ReviewRequestRefusal | None:
if review_mode == REVIEW_MODE_FLASH and full_review_published(report):
return ReviewRequestRefusal.FLASH_AFTER_FULL
diff --git a/products/review_hog/backend/reviewer/tests/test_publish_review.py b/products/review_hog/backend/reviewer/tests/test_publish_review.py
index 8e9680b5748c..d0e6039c8a27 100644
--- a/products/review_hog/backend/reviewer/tests/test_publish_review.py
+++ b/products/review_hog/backend/reviewer/tests/test_publish_review.py
@@ -322,18 +322,30 @@ class TestPublishReviewGate:
[
# A valid finding on an off-diff line resolves zero inline comments, but the review (its body
# carries it in the "Other findings" section) must still post, not be silently dropped.
- ("all_off_diff", [240], 0, "full", None),
- ("mixed", [1, 240], 1, "full", None),
- ("all_inline", [1], 1, "full", ""),
+ ("all_off_diff", [240], 0, "full", "", None),
+ ("mixed", [1, 240], 1, "full", "", None),
+ ("all_inline", [1], 1, "full", "", ""),
# Standard has no status comment, so its review body always says which turn it is.
(
"standard_all_inline",
[1],
1,
"flash",
+ "",
"**PostHog Review · Standard · reviewed `sha`**: 1 Should fix.\n\n",
),
- ("standard_mixed", [1, 240], 1, "flash", None),
+ ("standard_mixed", [1, 240], 1, "flash", "", None),
+ # The footer must survive the inline-only body, which replaces the full body on GitHub.
+ ("footer_all_inline", [1], 1, "full", "Comment `@posthog help`.", "Comment `@posthog help`.\n\n"),
+ (
+ "standard_footer_all_inline",
+ [1],
+ 1,
+ "flash",
+ "Comment `@posthog help`.",
+ "**PostHog Review · Standard · reviewed `sha`**: 1 Should fix.\n\nComment `@posthog help`.\n\n",
+ ),
+ ("footer_mixed", [1, 240], 1, "full", "Comment `@posthog help`.", None),
]
)
@patch(_POST)
@@ -345,6 +357,7 @@ class TestPublishReviewGate:
finding_lines: list[int],
expected_comments: int,
review_mode: str,
+ footer: str,
inline_prefix: str | None,
mock_report_cls: MagicMock,
mock_load: MagicMock,
@@ -377,13 +390,14 @@ class TestPublishReviewGate:
head_sha="sha",
published_priorities=_SHOULD_FIX_PUBLISHED,
review_mode=review_mode,
+ footer=footer,
)
assert outcome.posted is True
mock_post.assert_called_once()
assert len(mock_post.call_args.args[4]) == expected_comments
marker = mock_post.call_args.kwargs["marker"]
- assert marker in mock_post.call_args.args[3]
+ assert mock_post.call_args.args[3].endswith(f"{footer}\n\n{marker}" if footer else marker)
expected_inline_body = None if inline_prefix is None else f"{inline_prefix}{marker}"
assert mock_post.call_args.kwargs["inline_body"] == expected_inline_body
# A 👍 from an earlier clean turn must not sit next to these findings.
diff --git a/products/review_hog/backend/reviewer/tools/publish_review.py b/products/review_hog/backend/reviewer/tools/publish_review.py
index 444c2c94d16f..9b211a3ddcb6 100644
--- a/products/review_hog/backend/reviewer/tools/publish_review.py
+++ b/products/review_hog/backend/reviewer/tools/publish_review.py
@@ -89,6 +89,7 @@ def publish_persisted_review(
urgency_threshold: IssuePriority,
installation_id: str | None = None,
review_mode: str = REVIEW_MODE_FULL,
+ footer: str = "",
) -> PublishOutcome:
"""Publish an already-computed review for `report_id` at `head_sha`, idempotently.
@@ -123,6 +124,7 @@ def publish_persisted_review(
published_priorities=published_priorities_for(urgency_threshold),
installation_id=installation_id,
review_mode=review_mode,
+ footer=footer,
)
if outcome.posted:
if report.outcomes_emitted_at is not None:
@@ -197,6 +199,7 @@ def publish_review(
published_priorities: set[IssuePriority],
installation_id: str | None = None,
review_mode: str = REVIEW_MODE_FULL,
+ footer: str = "",
) -> PublishOutcome:
"""Publish the review to GitHub: the stored body plus inline comments from the durable rows.
@@ -214,7 +217,9 @@ def publish_review(
report = ReviewReport.objects.for_team(team_id).get(id=report_id)
marker = _review_marker(report_id, head_sha, review_mode)
- body = f"{report.report_markdown}\n\n{marker}"
+ # `footer` closes the review whichever body posts, so the inline-only body keeps it too.
+ footer_block = f"{footer}\n\n" if footer else ""
+ body = f"{report.report_markdown}\n\n{footer_block}{marker}"
valid_findings = load_valid_findings(team_id=team_id, report_id=report_id, run_index=run_index)
diff_lines = build_diff_line_map(pr_files)
@@ -243,10 +248,10 @@ def publish_review(
# a one-line summary, because it has no status comment to connect the comments to the turn.
inline_body = None
if len(comments) == len(publishable):
- inline_body = marker
+ inline_body = f"{footer_block}{marker}"
if review_mode == REVIEW_MODE_FLASH:
summary = render_review_summary(review_mode, head_sha, Counter(publishable))
- inline_body = f"{summary}\n\n{marker}"
+ inline_body = f"{summary}\n\n{footer_block}{marker}"
logger.info(f"Review: {len(body)} chars body, {len(comments)} inline comments")
review_url = _post_github_review(
diff --git a/products/review_hog/backend/temporal/activities.py b/products/review_hog/backend/temporal/activities.py
index 6d2f414bb77b..97b26666f227 100644
--- a/products/review_hog/backend/temporal/activities.py
+++ b/products/review_hog/backend/temporal/activities.py
@@ -36,6 +36,7 @@ from posthog.temporal.common.utils import close_db_connections
from products.review_hog.backend.app_identity import posthog_app_login
from products.review_hog.backend.automatic_reviews import automatic_flash_allowed, observe_dispatch
+from products.review_hog.backend.label_reviews import comment_commands_enabled, label_commands_hint
from products.review_hog.backend.models import ReviewProjectSettings, ReviewReport, ReviewUserSettings
from products.review_hog.backend.pr_owner import PullRequestOwnerResolver
from products.review_hog.backend.preferences import ReviewPreferences
@@ -2024,6 +2025,9 @@ async def build_body_activity(input: BuildBodyInput) -> None:
def _publish(input: PublishInput) -> PublishResult:
token, installation_id = _installation_auth(input.team_id, f"{input.owner}/{input.repo}")
+ footer = ""
+ if input.trigger_source == TRIGGER_LABEL and comment_commands_enabled(input.team_id):
+ footer = label_commands_hint(input.review_mode)
outcome = publish_persisted_review(
team_id=input.team_id,
report_id=input.report_id,
@@ -2036,6 +2040,7 @@ def _publish(input: PublishInput) -> PublishResult:
urgency_threshold=IssuePriority(input.urgency_threshold),
installation_id=installation_id,
review_mode=input.review_mode,
+ footer=footer,
)
if input.trigger_source == TRIGGER_AUTOMATIC:
ReviewReport.objects.for_team(input.team_id).filter(id=input.report_id).update(
diff --git a/products/review_hog/backend/tests/test_automatic_reviews.py b/products/review_hog/backend/tests/test_automatic_reviews.py
index 2c53da996595..b87af44d19ee 100644
--- a/products/review_hog/backend/tests/test_automatic_reviews.py
+++ b/products/review_hog/backend/tests/test_automatic_reviews.py
@@ -508,8 +508,24 @@ class TestLabelReviewTask(BaseTest):
user_id=self.user.id if run_as == "author" else self.connector.id,
publish=True,
trigger_source="label",
+ review_mode="flash",
)
+ def test_a_label_after_a_published_deep_review_runs_deep(self) -> None:
+ # A Standard request after a published Deep review is dropped, so the label would do nothing.
+ ReviewReport.objects.for_team(self.team.id).create(
+ team=self.team,
+ repository="posthog/posthog",
+ pr_number=42,
+ head_branch="feature",
+ base_branch="master",
+ published_heads_by_mode={"full": _HEAD_SHA},
+ )
+
+ self._run(_label_payload())
+
+ assert self.start.call_args.kwargs["review_mode"] == "full"
+
@parameterized.expand(
[
("another_bot", "posthog-review", "renovate[bot]", False),
diff --git a/products/review_hog/backend/tests/test_inbox_trigger.py b/products/review_hog/backend/tests/test_inbox_trigger.py
index f42a8562bd09..907180c5ebf8 100644
--- a/products/review_hog/backend/tests/test_inbox_trigger.py
+++ b/products/review_hog/backend/tests/test_inbox_trigger.py
@@ -157,6 +157,7 @@ class TestInboxTrigger(BaseTest):
publish=True,
acting_user_id=self.alice.id,
trigger_source="inbox",
+ review_mode="flash",
signal_report_id=str(task.signal_report_id),
signal_priority=None,
pr_url=_PR_URL,
diff --git a/products/review_hog/backend/tests/test_review_activity.py b/products/review_hog/backend/tests/test_review_activity.py
index 83519ff77401..7aa077dcb823 100644
--- a/products/review_hog/backend/tests/test_review_activity.py
+++ b/products/review_hog/backend/tests/test_review_activity.py
@@ -38,15 +38,18 @@ from products.review_hog.backend.reviewer.models.split_pr_into_chunks import Chu
from products.review_hog.backend.reviewer.review_design import REVIEW_DESIGN_PIPELINE, REVIEW_DESIGN_SINGLE_AGENT
from products.review_hog.backend.reviewer.tools.coordinator_review import CoordinatorResult, CoordinatorReview
from products.review_hog.backend.reviewer.tools.issue_deduplicator import AlreadyRaised, DedupOutcome, Duplicate
+from products.review_hog.backend.reviewer.tools.publish_review import PublishOutcome
from products.review_hog.backend.reviewer.tools.select_perspectives import PerspectiveSelectionDTO
from products.review_hog.backend.temporal.activities import (
CoordinatorReviewInput,
LensReviewInput,
LoadedPerspectiveDTO,
+ PublishInput,
ReviewChunkInput,
SandboxStageInput,
SelectPerspectivesInput,
_current_pr_comments,
+ _publish,
coordinator_review_activity,
dedup_activity,
lens_review_activity,
@@ -739,3 +742,40 @@ async def test_full_dedup_lists_other_reviewers_comments_when_the_app_login_is_n
)
assert result.raised_elsewhere == [AlreadyRaised(title="Already said", level="P2", comment_id=2, commenter="alice")]
+
+
+ "trigger_source,commands_on,expected_hint",
+ [
+ pytest.param("label", True, "@posthog review --deep", id="label_with_commands"),
+ pytest.param("label", False, None, id="label_without_commands"),
+ pytest.param("automatic", True, None, id="other_trigger"),
+ ],
+)
+def test_a_label_review_points_to_the_comment_commands(
+ trigger_source: str, commands_on: bool, expected_hint: str | None
+) -> None:
+ # The hint advertises `@posthog` commands, so it must not appear where they do not run or where nobody used the label.
+ publish = MagicMock(return_value=PublishOutcome(posted=True))
+ with (
+ patch(f"{_MODULE}._installation_auth", return_value=("token", "1")),
+ patch(f"{_MODULE}.comment_commands_enabled", return_value=commands_on),
+ patch(f"{_MODULE}.publish_persisted_review", publish),
+ patch(f"{_MODULE}.ReviewReport"),
+ ):
+ _publish(
+ PublishInput(
+ team_id=1,
+ report_id="rep-1",
+ head_sha="sha1",
+ run_index=1,
+ owner="o",
+ repo="r",
+ pr_number=1,
+ review_mode=REVIEW_MODE_FLASH,
+ trigger_source=trigger_source,
+ )
+ )
+
+ footer = publish.call_args.kwargs["footer"]
+ assert (expected_hint in footer) if expected_hint else footer == ""