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): log draft status on review and push gate events (#116792)

Julian Bezc8a7c5ce5527
diff --git a/products/review_hog/ARCHITECTURE.md b/products/review_hog/ARCHITECTURE.md
index 93260104a351..a291da48a674 100644
--- a/products/review_hog/ARCHITECTURE.md
+++ b/products/review_hog/ARCHITECTURE.md
@@ -494,7 +494,7 @@ pr_metadata.head_branch` is threaded (as explicit kwargs, alongside `team_id` /
The workflow captures one **`reviewhog_review_started`** product-analytics event per turn that passed every
gate (`track_review_started_activity`) and, after the publish stage, one **`reviewhog_review_completed`** per
finalized turn (published or stored), carrying repository / PR / trigger / finding-count / PR-size properties
- (`track_review_completed_activity`); a dead turn gets `reviewhog_review_failed` instead. All three, and the
+ (`track_review_completed_activity`). Both carry `pr_draft` from the turn's PR snapshot, null when the snapshot is unavailable; a dead turn gets `reviewhog_review_failed` instead. All three, and the
per-finding `reviewhog_finding_outcome`, carry routing properties from `reviewer/telemetry.py`: the tier,
reviewer configuration, validator and resolver pins, and review mode when known.
Flash turn events name the Flash arm in both seats.
@@ -941,7 +941,7 @@ An automatic follow-up turn (one with an `automatic_reviewed_head_sha`) first ru
Its rules, in order: `no_new_commits` (the PR gained no commit and its full diff is unchanged; a force-push that drops commits runs the review), `reviewhog_commits_only` (the ReviewHog app authored every new commit, such as resolution fixes; this includes the PostHog app's commits on self-driving PRs, which share the app identity), `merge_only` (the PR's full diff against its base is unchanged, as after a base merge or a rebase), `docs_only` (the new own commits touch only docs, lockfiles, snapshots, images, or generated files), and `system_one_below_threshold` (System One rates the own code patches below `SYSTEM_ONE_SKIP_BELOW`).
Each rule has a `SKIP_*` switch. `no_new_commits`, `reviewhog_commits_only`, and `merge_only` skip; `docs_only` and System One only record a shadow decision until production data confirms them.
A skipped turn posts no status comment and leaves `automatic_reviewed_head_sha` in place, so the next push is judged against the last reviewed head.
-Any gate failure reviews the push. Each decision emits `reviewhog_push_gate_decided` with `skipped`, `would_skip`, the reason, the System One probability and model, and `own_code_lines`, the lines the new own commits change in code files (empty when GitHub could not list every file of a commit), so skip rules that weigh push size can be calibrated offline.
+Any gate failure reviews the push. Each decision emits `reviewhog_push_gate_decided` with `skipped`, `would_skip`, the reason, the System One probability and model, `own_code_lines`, the lines the new own commits change in code files (empty when GitHub could not list every file of a commit), and `pr_draft` from the head's PR snapshot (null when it is unavailable), so skip rules that weigh push size can be calibrated offline.
The first automatic review and every label, UI, inbox, and manual trigger never reach the gate.
Reviews start only from a trigger (a push, the label, the UI, the inbox, or a manual run) and run right away.
ReviewHog never schedules a review for later, for example on a timer after a skipped push: nobody waits for a review that arrives at an unknown time, and a late review can land while the PR sits in a merge queue or under a stack.
diff --git a/products/review_hog/backend/temporal/activities.py b/products/review_hog/backend/temporal/activities.py
index 618e0afabe6e..469005c32d2c 100644
--- a/products/review_hog/backend/temporal/activities.py
+++ b/products/review_hog/backend/temporal/activities.py
@@ -2144,6 +2144,11 @@ def _pr_size_properties(snapshot: "PRSnapshotArtefact | None") -> dict[str, int
}
+def _pr_draft(snapshot: "PRSnapshotArtefact | None") -> bool | None:
+ """Whether the PR was a draft when the turn fetched it; None when the turn's snapshot is unavailable."""
+ return snapshot.pr_metadata.draft if snapshot is not None else None
+
+
def _flash_event_properties(turn: FlashTurnStats | None, sessions: FlashSessionStats | None) -> dict[str, object]:
"""A single-agent turn's finding funnel and session health; empty for a pipeline turn.
@@ -2208,6 +2213,7 @@ def _track_review_started(input: TrackReviewStartedInput) -> None:
"review_design_reason": input.review_design_reason or None,
"flash_coordinator": input.coordinator,
**_pr_size_properties(snapshot),
+ "pr_draft": _pr_draft(snapshot),
},
groups=groups(team=report.team),
send_feature_flags=True,
@@ -2237,7 +2243,9 @@ async def track_review_started_activity(input: TrackReviewStartedInput) -> None:
await database_sync_to_async(_track_review_started_safe, thread_sensitive=False)(input)
-def _track_push_gate_decided(input: GatePushInput, decision: PushGateDecision) -> None:
+def _track_push_gate_decided(
+ input: GatePushInput, decision: PushGateDecision, current: "PRSnapshotArtefact | None"
+) -> None:
report = ReviewReport.objects.for_team(input.team_id).select_related("acting_user", "team").get(id=input.report_id)
posthoganalytics.capture(
distinct_id=_review_event_identity(report),
@@ -2260,6 +2268,7 @@ def _track_push_gate_decided(input: GatePushInput, decision: PushGateDecision) -
"system_one_skip_below": SYSTEM_ONE_SKIP_BELOW,
"own_commits": decision.own_commits,
"own_code_lines": decision.own_code_lines,
+ "pr_draft": _pr_draft(current),
},
groups=groups(team=report.team),
)
@@ -2281,7 +2290,7 @@ def _gate_push(input: GatePushInput) -> PushGateDecision:
if decision.skip:
ReviewReport.objects.for_team(input.team_id).filter(id=input.report_id).update(status=ReviewReport.Status.IDLE)
try:
- _track_push_gate_decided(input, decision)
+ _track_push_gate_decided(input, decision, current)
except Exception:
logger.exception("Failed to capture reviewhog_push_gate_decided for report %s; continuing", input.report_id)
return decision
@@ -2342,6 +2351,7 @@ def _track_review_completed(input: TrackReviewCompletedInput) -> None:
review_design=input.review_design,
),
**_pr_size_properties(snapshot),
+ "pr_draft": _pr_draft(snapshot),
"duration_seconds": duration_seconds,
# No marker means a turn started before the marker shipped, or a failed marker: the version is unknown.
"reviewhog_version": input.marker.version if input.marker is not None else None,
diff --git a/products/review_hog/backend/tests/test_review_completed_tracking.py b/products/review_hog/backend/tests/test_review_completed_tracking.py
index f1f1def792bb..ea26ff4ea09f 100644
--- a/products/review_hog/backend/tests/test_review_completed_tracking.py
+++ b/products/review_hog/backend/tests/test_review_completed_tracking.py
@@ -47,12 +47,12 @@ from products.signals.backend.enums import ReportPriority
_PR_URL = "https://github.com/o/r/pull/7"
-def _pr_metadata() -> PRMetadata:
+def _pr_metadata(*, draft: bool = False) -> PRMetadata:
return PRMetadata(
number=7,
title="t",
state="open",
- draft=False,
+ draft=draft,
created_at="",
updated_at="",
author="octocat",
@@ -135,7 +135,8 @@ class TestTrackReviewCompleted(BaseTest):
team_id=self.team.id,
report_id=report_id,
head_sha="sha1",
- pr_metadata=_pr_metadata(),
+ # The report row says not a draft, so the draft property must come from the turn's snapshot.
+ pr_metadata=_pr_metadata(draft=True),
pr_comments=[],
# One reviewable file: raw PR additions (120) vs reviewable additions (80) must diverge
# so the assertions below can't pass with the two properties swapped.
@@ -198,6 +199,7 @@ class TestTrackReviewCompleted(BaseTest):
assert props["pr_changed_files"] == 7
assert props["pr_commits"] == 3
assert props["pr_reviewable_additions"] == 80
+ assert props["pr_draft"] is True
assert 90 <= props["duration_seconds"] < 600
assert props["reviewhog_version"] == "reviewhog-full-9-9"
assert props["reviewhog_fingerprint"] == "abc1234"
@@ -255,6 +257,8 @@ class TestTrackReviewCompleted(BaseTest):
props = capture.call_args.kwargs["properties"]
assert props["pr_additions"] is None
assert props["pr_reviewable_additions"] is None
+ # Unknown is not "not a draft": a False here would count the turn as a ready PR.
+ assert props["pr_draft"] is None
assert props["findings_total"] == 0
# A turn without a marker ran on an unknown release; the current deploy's version would mislabel it.
assert props["reviewhog_version"] is None
@@ -312,12 +316,12 @@ class TestTrackReviewCompleted(BaseTest):
# A skipped turn ends before any stage that returns the report to rest, and a skipped turn
# keeps the next turn's run_index, so a run_index-keyed event would merge two skipped pushes.
report_id = self._review_report()
- for head_sha in ("sha1", "sha2"):
+ for head_sha, draft in (("sha1", True), ("sha2", False)):
persist_pr_snapshot(
team_id=self.team.id,
report_id=report_id,
head_sha=head_sha,
- pr_metadata=_pr_metadata(),
+ pr_metadata=_pr_metadata(draft=draft),
pr_comments=[],
pr_files=[],
)
@@ -364,6 +368,8 @@ class TestTrackReviewCompleted(BaseTest):
14,
)
assert first.kwargs["uuid"] != second.kwargs["uuid"]
+ # Each push reports the draft state of its own head.
+ assert (props["pr_draft"], second.kwargs["properties"]["pr_draft"]) == (True, False)
def test_capture_failure_is_swallowed(self) -> None:
# Telemetry must never fail a review — losing this guard would fail review turns on any
@@ -452,6 +458,9 @@ class TestTrackReviewCompleted(BaseTest):
assert completed.kwargs["properties"]["turn_trigger_source"] == "label"
assert failed.kwargs["properties"]["turn_trigger_source"] == "inbox"
assert started.kwargs["properties"]["turn_trigger_source"] == "inbox"
+ # No snapshot for this head, so the draft state is unknown on both events that carry it.
+ assert completed.kwargs["properties"]["pr_draft"] is None
+ assert started.kwargs["properties"]["pr_draft"] is None
# Three events per turn, three uuid namespaces: a shared one would dedupe a start or a
# failure against the completion of the same turn.
assert len({call.kwargs["uuid"] for call in (completed, failed, started)}) == 3