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): review the whole PR twice, then only what each push changed (#116968)

Julian Beze53abe561013
diff --git a/products/review_hog/ARCHITECTURE.md b/products/review_hog/ARCHITECTURE.md
index 2a1c32f7fab3..5e5aad94fd13 100644
--- a/products/review_hog/ARCHITECTURE.md
+++ b/products/review_hog/ARCHITECTURE.md
@@ -314,9 +314,12 @@ the prompt tells the session to read those files at the head. There is no team s
DevEx-owned prompts.
Each session persists as one `perspective_result` under a reserved pass (`SINGLE_AGENT_PASS_NUMBER` 2000, lenses 2001
-and 2002) and its part. On a follow-up turn, a P3 finding more than `FLASH_FOLLOW_UP_CHANGE_MARGIN_LINES` (3)
-lines from any code that changed since `completed_head_sha` drops as `old_code` first (`ChangedSinceReview`). The two
-heads' PR snapshots are compared by line content per file, so lines a base merge only moved stay old. P0 to P2
+and 2002) and its part. On a follow-up turn, a finding more than `FLASH_FOLLOW_UP_CHANGE_MARGIN_LINES` (3) lines
+from any code that changed since `completed_head_sha` drops as `old_code` first (`ChangedSinceReview`). An automatic turn
+after run `FLASH_WHOLE_PR_TURNS` (2) drops every P2 and P3 finding there, so a fix only draws findings about the fix:
+one run finds only part of what is there, so the first two runs review the whole pull request, and later ones review
+the push. Earlier runs and requested turns drop only P3 findings there. The
+two heads' PR snapshots are compared by line content per file, so lines a base merge only moved stay old. Must-fix
findings, files whose patch GitHub left out, a first review, and a re-run at the reviewed head skip the check. Step 7 then runs as two dedup calls in parallel (`dedupe_flash_findings`,
`tools/single_agent_review.py`), both one-shot OpenAI calls on `FLASH_DEDUP_MODEL` (`gpt-6-luna` @ medium,
`run_oneshot_openai_review`): the main findings against earlier turns, and the lens findings against the same plus
@@ -336,7 +339,9 @@ repeat, and findings of this turn never drop each other. The turn logs it, marks
`compose_flash_findings` then ranks the findings highest priority first, a reported P0 before a P1 of the same stored
priority, and the main session first on ties, before anything persists. Every must-fix (P0/P1) finding is kept outside the cap, up to `FLASH_MUST_FIX_CAP_MULTIPLIER` (2)
times the cap, and P2 and then P3 findings fill the slots left under the cap, `flash_max_findings(parts)` (4 plus 2 per
-lens part past the first, up to 10). No validator runs, so dedup writes an
+lens part past the first, up to 10). The first review (run 1) gets `FLASH_FIRST_REVIEW_CAP_MULTIPLIER` (2) times that
+cap: a cut finding is recorded only as a `dropped_finding`, not as a finding later turns dedup against, so a later turn
+would find it again and post it on its own. No validator runs, so dedup writes an
accept-as-found verdict per survivor. P0/P1 store as `must_fix`, P2 as `should_fix`, P3 as `consider`, and the
finding's `reported_priority` keeps the reviewer's P0-P3, so P0 and P1 stay apart for analysis. One exception: a dedup survivor takes a more severe duplicate's level when both fold into the same stored priority (a P0 merged into a P1); the `dropped_finding` record keeps the duplicate's own level. Findings publish
inline. An
diff --git a/products/review_hog/backend/reviewer/constants.py b/products/review_hog/backend/reviewer/constants.py
index d8db7b70edba..5354b5117a53 100644
--- a/products/review_hog/backend/reviewer/constants.py
+++ b/products/review_hog/backend/reviewer/constants.py
@@ -172,6 +172,14 @@ FLASH_MUST_FIX_CAP_MULTIPLIER = 2
FLASH_FOLLOW_UP_CHANGE_MARGIN_LINES = 3
+# The first review keeps more of what it finds. A finding the cap cuts is recorded only as a dropped finding, so
+# a later turn finds it again and posts it on its own, one push at a time.
+FLASH_FIRST_REVIEW_CAP_MULTIPLIER = 2
+# Turns up to this run review the whole pull request: one run finds only part of what is there, so a second pass
+# is worth it. A later automatic follow-up only comments on what its push changed, plus must-fix anywhere.
+FLASH_WHOLE_PR_TURNS = 2
+
+
def flash_max_findings(lens_part_count: int) -> int:
extra_parts = max(lens_part_count - 1, 0)
return min(FLASH_MAX_FINDINGS_BASE + FLASH_MAX_FINDINGS_PER_EXTRA_PART * extra_parts, FLASH_MAX_FINDINGS_CEILING)
diff --git a/products/review_hog/backend/reviewer/fingerprint.py b/products/review_hog/backend/reviewer/fingerprint.py
index c00d87a3a259..ef4aec25167a 100644
--- a/products/review_hog/backend/reviewer/fingerprint.py
+++ b/products/review_hog/backend/reviewer/fingerprint.py
@@ -30,6 +30,7 @@ from products.review_hog.backend.reviewer.constants import (
DEDUP_RUNTIME_ADAPTER,
FLASH_DEDUP_MODEL,
FLASH_DEDUP_REASONING_EFFORT,
+ FLASH_FIRST_REVIEW_CAP_MULTIPLIER,
FLASH_FOLLOW_UP_CHANGE_MARGIN_LINES,
FLASH_LENS_CHUNK_MAX_LINES,
FLASH_LENS_MAX_CHUNKS,
@@ -39,6 +40,7 @@ from products.review_hog.backend.reviewer.constants import (
FLASH_MAX_FINDINGS_PER_EXTRA_PART,
FLASH_MUST_FIX_CAP_MULTIPLIER,
FLASH_PROMPT_DIFF_MAX_CHARS,
+ FLASH_WHOLE_PR_TURNS,
ONESHOT_MODEL,
ONESHOT_REASONING_EFFORT,
ReviewArm,
@@ -263,6 +265,8 @@ class TurnFingerprint:
"max_findings_per_extra_part": FLASH_MAX_FINDINGS_PER_EXTRA_PART,
"max_findings_ceiling": FLASH_MAX_FINDINGS_CEILING,
"must_fix_cap_multiplier": FLASH_MUST_FIX_CAP_MULTIPLIER,
+ "first_review_cap_multiplier": FLASH_FIRST_REVIEW_CAP_MULTIPLIER,
+ "whole_pr_turns": FLASH_WHOLE_PR_TURNS,
"follow_up_change_margin_lines": FLASH_FOLLOW_UP_CHANGE_MARGIN_LINES,
"lens_chunk_max_lines": FLASH_LENS_CHUNK_MAX_LINES,
"lens_max_chunks": FLASH_LENS_MAX_CHUNKS,
diff --git a/products/review_hog/backend/reviewer/tests/test_single_agent_review.py b/products/review_hog/backend/reviewer/tests/test_single_agent_review.py
index 38c5f3327df3..9bdc0338acc1 100644
--- a/products/review_hog/backend/reviewer/tests/test_single_agent_review.py
+++ b/products/review_hog/backend/reviewer/tests/test_single_agent_review.py
@@ -319,6 +319,7 @@ class TestDedupeFlashFindings:
*,
prior_findings: list[ReviewIssueFinding] | None = None,
changed_since: ChangedSinceReview | None = None,
+ changed_code_only: bool = False,
) -> FlashSelection:
with patch(f"{_DEDUP_MODULE}.run_oneshot_openai_review", mock_llm):
return await dedupe_flash_findings(
@@ -331,6 +332,7 @@ class TestDedupeFlashFindings:
repository="o/r",
lens_part_count=1,
changed_since=changed_since,
+ changed_code_only=changed_code_only,
)
@pytest.mark.asyncio
@@ -595,6 +597,45 @@ class TestDedupeFlashFindings:
assert [kept.id for kept in selection.kept] == (["2000-1-1"] if posts else [])
assert [drop.disposition for drop in selection.dropped] == ([] if posts else ["old_code"])
+ @pytest.mark.asyncio
+ @pytest.mark.parametrize(
+ "line,priority,changed_code_only,posts",
+ [
+ pytest.param(30, IssuePriority.SHOULD_FIX, True, False, id="late_automatic_p2_on_unchanged_code"),
+ pytest.param(30, IssuePriority.SHOULD_FIX, False, True, id="whole_pr_p2_on_unchanged_code"),
+ pytest.param(11, IssuePriority.SHOULD_FIX, True, True, id="late_automatic_p2_on_the_fix"),
+ pytest.param(30, IssuePriority.MUST_FIX, True, True, id="late_automatic_p1_on_unchanged_code"),
+ ],
+ )
+ async def test_a_late_automatic_follow_up_only_comments_on_what_the_push_changed(
+ self, pr_metadata: PRMetadata, line: int, priority: IssuePriority, changed_code_only: bool, posts: bool
+ ) -> None:
+ # Each fix drew a new P2 on code it never touched, and an open finding keeps stamphog from approving,
+ # so the author's agent fixed and pushed again, round after round.
+ earlier = [_patched("a.py", ("addition", 10, ["x = 1"]))]
+ current = [_patched("a.py", ("addition", 10, ["x = 1", "y = 2"]))]
+ issue = _issue("2000-1-1", priority).model_copy(update={"file": "a.py", "lines": [LineRange(start=line)]})
+
+ selection = await self._dedupe(
+ pr_metadata,
+ [issue],
+ _flash_dedup(),
+ changed_since=ChangedSinceReview.between(earlier, current),
+ changed_code_only=changed_code_only,
+ )
+
+ assert [drop.disposition for drop in selection.dropped] == ([] if posts else ["old_code"])
+
+
+def test_a_first_review_keeps_twice_the_cap() -> None:
+ # A finding the cap cuts is not stored, so a later turn would find it again and post it on its own.
+ main = [_issue(f"m{n}", IssuePriority.SHOULD_FIX) for n in range(1, 11)]
+
+ first = compose_flash_findings(main, [], lens_part_count=1, first_review=True)
+ follow_up = compose_flash_findings(main, [], lens_part_count=1)
+
+ assert (len(first.kept), first.cap, len(follow_up.kept)) == (8, 8, 4)
+
def test_turn_stats_count_every_candidate_once_per_session() -> None:
# The completed event's funnel only reads true when every finding that entered dedup is either
diff --git a/products/review_hog/backend/reviewer/tools/single_agent_review.py b/products/review_hog/backend/reviewer/tools/single_agent_review.py
index a2021b10ff8e..c3910ba82d44 100644
--- a/products/review_hog/backend/reviewer/tools/single_agent_review.py
+++ b/products/review_hog/backend/reviewer/tools/single_agent_review.py
@@ -22,6 +22,7 @@ from posthog.dataclasses import frozen
from products.review_hog.backend.reviewer.artefact_content import ReviewIssueFinding, ValidationVerdict
from products.review_hog.backend.reviewer.constants import (
+ FLASH_FIRST_REVIEW_CAP_MULTIPLIER,
FLASH_FOLLOW_UP_CHANGE_MARGIN_LINES,
FLASH_LENSES,
FLASH_MUST_FIX_CAP_MULTIPLIER,
@@ -282,6 +283,7 @@ def compose_flash_findings(
lens_part_count: int,
group_levels: Mapping[str, int] | None = None,
budget_left: int | None = None,
+ first_review: bool = False,
) -> FlashSelection:
"""The findings a Flash turn keeps, highest priority first, P0 before P1, and the main review first on ties.
@@ -291,12 +293,13 @@ def compose_flash_findings(
`budget_left` is how many more findings the pull request may post (`finding_budget.py`), None when no
budget applies. Must-fix findings count toward it but always post. A finding that fits under the cap
- but not under the budget drops as `budget`, the rest as `cap`.
+ but not under the budget drops as `budget`, the rest as `cap`. A first review gets
+ `FLASH_FIRST_REVIEW_CAP_MULTIPLIER` times the cap.
The caller persists only the kept findings. A persisted finding that never posts counts as already
raised, so every later turn would keep it off the PR too.
"""
- cap = flash_max_findings(lens_part_count)
+ cap = flash_max_findings(lens_part_count) * (FLASH_FIRST_REVIEW_CAP_MULTIPLIER if first_review else 1)
ranked = _flash_order(main, lens, group_levels)
must_fix = [issue for issue in ranked if issue.priority == IssuePriority.MUST_FIX]
must_fix = must_fix[: FLASH_MUST_FIX_CAP_MULTIPLIER * cap]
@@ -602,15 +605,20 @@ class ChangedSinceReview:
)
-def _old_code_findings(issues: list[Issue], changed_since: ChangedSinceReview | None) -> list[Issue]:
- """A follow-up turn's P3 findings on code that did not change since the last reviewed head.
+def _old_code_findings(
+ issues: list[Issue], changed_since: ChangedSinceReview | None, *, changed_code_only: bool
+) -> list[Issue]:
+ """A follow-up turn's findings on code that did not change since the last reviewed head.
- A P2 or higher on old code still goes through dedup: the earlier review may have missed it, and dedup
- against earlier turns' findings already removes one that a turn posted before.
+ With `changed_code_only` the turn only comments on what the push changed, so a fix does not draw a new
+ opinion on code earlier turns already covered. It still posts a must-fix anywhere. Otherwise only P3
+ findings drop there: a P2 still goes through dedup, because the earlier review may have missed it, and
+ dedup against earlier turns' findings already removes one that a turn posted before.
"""
if changed_since is None:
return []
- return [issue for issue in issues if issue.priority == IssuePriority.CONSIDER and not changed_since.touches(issue)]
+ dropped = {IssuePriority.SHOULD_FIX, IssuePriority.CONSIDER} if changed_code_only else {IssuePriority.CONSIDER}
+ return [issue for issue in issues if issue.priority in dropped and not changed_since.touches(issue)]
async def dedupe_flash_findings(
@@ -627,6 +635,8 @@ async def dedupe_flash_findings(
fall_back_on_any_error: bool = False,
changed_since: ChangedSinceReview | None = None,
budget_left: int | None = None,
+ changed_code_only: bool = False,
+ first_review: bool = False,
) -> FlashSelection:
"""Deduplicate a single-agent turn's main and lens findings, then keep the few it posts.
@@ -637,9 +647,10 @@ async def dedupe_flash_findings(
it repeats survives (`_resolve_duplicates`). A finding that survives takes the priority of the most
severe duplicate removed in its favor. `fall_back_on_any_error` lets a dedup
call fall back to the positional pre-filter on any failure, for the activity's last attempt.
- On a follow-up turn, `changed_since` drops the P3 findings on unchanged code before dedup.
+ On a follow-up turn, `changed_since` drops findings on unchanged code before dedup: every finding below
+ must-fix with `changed_code_only`, otherwise only P3 findings.
"""
- old_code = _old_code_findings(issues, changed_since)
+ old_code = _old_code_findings(issues, changed_since, changed_code_only=changed_code_only)
old_code_ids = {issue.id for issue in old_code}
reviewed = [issue for issue in issues if issue.id not in old_code_ids]
main = [issue for issue in reviewed if issue.source_perspective == SINGLE_AGENT_SOURCE]
@@ -680,7 +691,12 @@ async def dedupe_flash_findings(
kept_lens = [issue for issue in lens if issue.id not in removed_ids]
group_levels = _raise_survivors([*kept_main, *kept_lens], [removal.duplicate for removal in held])
composed = compose_flash_findings(
- kept_main, kept_lens, lens_part_count=lens_part_count, group_levels=group_levels, budget_left=budget_left
+ kept_main,
+ kept_lens,
+ lens_part_count=lens_part_count,
+ group_levels=group_levels,
+ budget_left=budget_left,
+ first_review=first_review,
)
turn_issues = {issue.id: issue for issue in issues}
dedup_drops = [
diff --git a/products/review_hog/backend/temporal/activities.py b/products/review_hog/backend/temporal/activities.py
index 7749a504348d..c22d753f019e 100644
--- a/products/review_hog/backend/temporal/activities.py
+++ b/products/review_hog/backend/temporal/activities.py
@@ -54,6 +54,7 @@ from products.review_hog.backend.reviewer.constants import (
CHUNKING_RUNTIME_ADAPTER,
COORDINATOR_MAX_SUBAGENTS,
FLASH_LENSES,
+ FLASH_WHOLE_PR_TURNS,
NON_RETRYABLE_UNIT_FAILURE_CATEGORIES,
REVIEW_MODE_FLASH,
REVIEW_MODE_FULL,
@@ -1798,6 +1799,9 @@ async def dedup_activity(input: SandboxStageInput) -> DedupResult:
fall_back_on_any_error=_is_final_attempt(),
changed_since=changed_since,
budget_left=budget_left,
+ # Requested reviews and the first whole-PR turns still look at code the push did not change.
+ changed_code_only=input.trigger_source == TRIGGER_AUTOMATIC and input.run_index > FLASH_WHOLE_PR_TURNS,
+ first_review=input.run_index == 1,
)
survivors = flash_selection.kept
flash_stats = flash_turn_stats(issues, flash_selection, reviewable_lines=lens_plan.reviewable_lines)
diff --git a/products/review_hog/backend/tests/test_review_activity.py b/products/review_hog/backend/tests/test_review_activity.py
index 770f3371d225..6e19a38cd795 100644
--- a/products/review_hog/backend/tests/test_review_activity.py
+++ b/products/review_hog/backend/tests/test_review_activity.py
@@ -748,18 +748,21 @@ async def test_full_dedup_lists_other_reviewers_comments_when_the_app_login_is_n
@pytest.mark.django_db
@pytest.mark.parametrize(
- "trigger_source,expected_budget_left",
+ "trigger_source,run_index,expected_budget_left,expected_changed_code_only",
[
- pytest.param("automatic", 2, id="automatic_turn_is_held_to_the_budget"),
- pytest.param("comment", None, id="requested_review_is_not"),
- pytest.param(None, None, id="payload_from_before_the_field"),
+ pytest.param("automatic", 1, 2, False, id="first_review"),
+ pytest.param("automatic", 2, 2, False, id="second_automatic_turn_reviews_the_whole_pr"),
+ pytest.param("automatic", 3, 2, True, id="later_automatic_turn_reviews_the_push"),
+ pytest.param("comment", 3, None, False, id="requested_review"),
+ pytest.param(None, 3, None, False, id="payload_from_before_the_field"),
],
)
-async def test_only_an_automatic_standard_turn_is_held_to_the_finding_budget(
- trigger_source: str | None, expected_budget_left: int | None
+async def test_a_standard_turn_scope_follows_its_trigger_and_run(
+ trigger_source: str | None, run_index: int, expected_budget_left: int | None, expected_changed_code_only: bool
) -> None:
- # Automatic reviews are the safeguard that stops at must-fix findings once the budget is spent. A review
- # someone asks for must surface everything the turn keeps.
+ # Automatic reviews are the safeguard that stops at must-fix findings once the budget is spent, and after
+ # the first whole-PR turns they only comment on what a push changed. A review someone asks for surfaces
+ # everything the turn keeps, across the pull request.
dedupe = AsyncMock(return_value=FlashSelection(kept=[], dropped=[], cap=4, lens_part_count=1))
with (
patch(f"{_MODULE}.ReviewActivityHeartbeater"),
@@ -776,10 +779,16 @@ async def test_only_an_automatic_standard_turn_is_held_to_the_finding_budget(
patch(f"{_MODULE}._refresh_status_comment", AsyncMock()),
):
await ActivityEnvironment().run(
- dedup_activity, SandboxStageInput(**_single_agent_stage(trigger_source=trigger_source))
+ dedup_activity,
+ SandboxStageInput(**_single_agent_stage(trigger_source=trigger_source, run_index=run_index)),
)
- assert dedupe.call_args.kwargs["budget_left"] == expected_budget_left
+ kwargs = dedupe.call_args.kwargs
+ assert (kwargs["budget_left"], kwargs["changed_code_only"], kwargs["first_review"]) == (
+ expected_budget_left,
+ expected_changed_code_only,
+ run_index == 1,
+ )
@pytest.mark.parametrize(