: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): log the push size on push gate decisions (#116001)
Julian Bezd1aff26bcbd5
diff --git a/products/review_hog/ARCHITECTURE.md b/products/review_hog/ARCHITECTURE.mdindex 49f6a8b636b5..7441dc20b86b 100644--- a/products/review_hog/ARCHITECTURE.md+++ b/products/review_hog/ARCHITECTURE.md@@ -936,8 +936,11 @@ An automatic follow-up turn (one with an `automatic_reviewed_head_sha`) first ruIts 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, and the System One probability and model.+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.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.+A change that the gate skips gets reviewed at the next trigger, or not at all.**Review surfaces (Code review scene).** The reviews API's `scope=mine` ("For you") matches reports where theviewer is the **acting user OR the PR's author** — `author_login` compared case-insensitively against thediff --git a/products/review_hog/backend/reviewer/push_gate.py b/products/review_hog/backend/reviewer/push_gate.pyindex 0fee79038829..2616ecc119da 100644--- a/products/review_hog/backend/reviewer/push_gate.py+++ b/products/review_hog/backend/reviewer/push_gate.py@@ -111,6 +111,8 @@ class PushGateDecision:probability: float | None = Nonemodel: str | None = Noneown_commits: int | None = None+ # Lines the new own commits change in code files, summed per commit. Set only when the gate read every file.+ own_code_lines: int | None = None@frozen@@ -120,6 +122,13 @@ class _ChangedFile:patch: str | None+@frozen+class _CommitFiles:+ files: list[_ChangedFile]+ # GitHub lists at most one page of files per commit, so a full page may hide more files.+ complete: bool++@frozenclass _ChangedLine:type: str@@ -149,7 +158,12 @@ def _rule_enabled(reason: SkipReason) -> bool:def _matched(- reason: SkipReason, *, own_commits: int | None = None, probability: float | None = None, model: str | None = None+ reason: SkipReason,+ *,+ own_commits: int | None = None,+ probability: float | None = None,+ model: str | None = None,+ own_code_lines: int | None = None,) -> PushGateDecision:return PushGateDecision(skip=_rule_enabled(reason),@@ -158,11 +172,17 @@ def _matched(probability=probability,model=model,own_commits=own_commits,+ own_code_lines=own_code_lines,)def _runs(- reason: RunReason, *, own_commits: int | None = None, probability: float | None = None, model: str | None = None+ reason: RunReason,+ *,+ own_commits: int | None = None,+ probability: float | None = None,+ model: str | None = None,+ own_code_lines: int | None = None,) -> PushGateDecision:return PushGateDecision(skip=False,@@ -171,6 +191,7 @@ def _runs(probability=probability,model=model,own_commits=own_commits,+ own_code_lines=own_code_lines,)@@ -253,21 +274,25 @@ class PushGate:reviewhog_shas={commit["sha"] for commit in commits if _is_own_commit(commit, app_login)},)- def _commit_files(self, shas: list[str]) -> list[_ChangedFile] | None:+ def _commit_files(self, shas: list[str]) -> _CommitFiles | None:files: list[_ChangedFile] = []+ complete = Truefor sha in shas:commit = self._get(f"/commits/{sha}", endpoint="/commits/{ref}")if commit is None:return None- files.extend(_changed_files(commit))- return files+ commit_files = _changed_files(commit)+ complete = complete and len(commit_files) < GITHUB_COMPARE_FILES_CAP+ files.extend(commit_files)+ return _CommitFiles(files=files, complete=complete)- def _ask_system_one(self, code_files: list[_ChangedFile], own_commits: int) -> PushGateDecision:+ def _ask_system_one(self, code_files: list[_ChangedFile], own_commits: int, lines_known: bool) -> PushGateDecision:+ lines = sum(file.changes for file in code_files) if lines_known else Noneif any(file.patch is None for file in code_files):- return _runs("interdiff_too_large", own_commits=own_commits)+ return _runs("interdiff_too_large", own_commits=own_commits, own_code_lines=lines)state = "\n\n".join(f"--- {file.filename}\n{file.patch}" for file in code_files)if len(state) > MAX_STATE_CHARS:- return _runs("interdiff_too_large", own_commits=own_commits)+ return _runs("interdiff_too_large", own_commits=own_commits, own_code_lines=lines)try:client = build_system_one_client(model=PUSH_GATE_MODEL,@@ -278,19 +303,24 @@ class PushGate:result = client.decide(state=state, questions={_QUESTION_ID: _ALTERS_QUESTION})except (SystemOneNotConfigured, SystemOneRequestFailed) as error:logger.warning("Push gate could not reach System One: %s", type(error).__name__)- return _runs("system_one_unavailable", own_commits=own_commits)+ return _runs("system_one_unavailable", own_commits=own_commits, own_code_lines=lines)answer = result.answers[_QUESTION_ID]if not isinstance(answer, NoulAnswer):- return _runs("system_one_unavailable", own_commits=own_commits)+ return _runs("system_one_unavailable", own_commits=own_commits, own_code_lines=lines)if answer.probability < SYSTEM_ONE_SKIP_BELOW:return _matched("system_one_below_threshold",own_commits=own_commits,probability=answer.probability,model=result.model,+ own_code_lines=lines,)return _runs(- "system_one_above_threshold", own_commits=own_commits, probability=answer.probability, model=result.model+ "system_one_above_threshold",+ own_commits=own_commits,+ probability=answer.probability,+ model=result.model,+ own_code_lines=lines,)def decide(self, *, previous_head_sha: str, head_sha: str, base_branch: str) -> PushGateDecision:@@ -321,7 +351,7 @@ class PushGate:own_files = self._commit_files(own_shas)if own_files is None:return _runs("compare_unavailable", own_commits=len(own_shas))- code_files = [file for file in own_files if not _NON_CODE_FILE.search(file.filename)]+ code_files = [file for file in own_files.files if not _NON_CODE_FILE.search(file.filename)]if not code_files:- return _matched("docs_only", own_commits=len(own_shas))- return self._ask_system_one(code_files, own_commits=len(own_shas))+ return _matched("docs_only", own_commits=len(own_shas), own_code_lines=0 if own_files.complete else None)+ return self._ask_system_one(code_files, own_commits=len(own_shas), lines_known=own_files.complete)diff --git a/products/review_hog/backend/temporal/activities.py b/products/review_hog/backend/temporal/activities.pyindex 7d4da4d820ab..f1fd23c3b85a 100644--- a/products/review_hog/backend/temporal/activities.py+++ b/products/review_hog/backend/temporal/activities.py@@ -2256,6 +2256,7 @@ def _track_push_gate_decided(input: GatePushInput, decision: PushGateDecision) -"system_one_model": decision.model,"system_one_skip_below": SYSTEM_ONE_SKIP_BELOW,"own_commits": decision.own_commits,+ "own_code_lines": decision.own_code_lines,},groups=groups(team=report.team),)diff --git a/products/review_hog/backend/tests/test_push_gate.py b/products/review_hog/backend/tests/test_push_gate.pyindex 826bd146f5ce..72e2fcb3dfd8 100644--- a/products/review_hog/backend/tests/test_push_gate.py+++ b/products/review_hog/backend/tests/test_push_gate.py@@ -183,12 +183,25 @@ def test_push_gate_asks_system_one_about_own_code_commits(probability,PUSH_GATE_MODEL,)+ # The README's lines stay out of the push size, like they stay out of the state.+ assert decision.own_code_lines == 2call = system_one.decide.call_args.kwargs# The threshold was calibrated on exactly this state: own code patches, docs dropped.assert call["state"] == f"--- posthog/billing.py\n{_BILLING_FIX}"assert "round" not in str(call["questions"]["alters"].instructions)+def test_push_gate_logs_no_push_size_when_a_commit_lists_a_full_page_of_files() -> None:+ full_page = [_file(f"posthog/module_{n}.py") for n in range(300)]+ push = _push(+ new_commits=[_commit("a"), _commit("c")],+ pr_files=[_file("posthog/billing.py", _BILLING_FIX)],+ own_commit_files=full_page,+ )+ decision = _decide(push, _system_one(0.8))+ assert (decision.reason, decision.own_code_lines) == ("system_one_above_threshold", None)++def test_push_gate_reviews_a_push_that_mixes_reviewhog_and_author_commits() -> None:push = _push(new_commits=[_commit("a"), _own_commit("r"), _commit("c")],diff --git a/products/review_hog/backend/tests/test_review_completed_tracking.py b/products/review_hog/backend/tests/test_review_completed_tracking.pyindex decdfbbdd735..f1f1def792bb 100644--- a/products/review_hog/backend/tests/test_review_completed_tracking.py+++ b/products/review_hog/backend/tests/test_review_completed_tracking.py@@ -328,6 +328,7 @@ class TestTrackReviewCompleted(BaseTest):probability=0.03,model="jevk5",own_commits=2,+ own_code_lines=14,)with (@@ -356,10 +357,11 @@ class TestTrackReviewCompleted(BaseTest):first, second = capture.call_args_listassert first.kwargs["event"] == "reviewhog_push_gate_decided"props = first.kwargs["properties"]- assert (props["skipped"], props["reason"], props["system_one_probability"]) == (+ assert (props["skipped"], props["reason"], props["system_one_probability"], props["own_code_lines"]) == (True,"system_one_below_threshold",0.03,+ 14,)assert first.kwargs["uuid"] != second.kwargs["uuid"]