: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): add per-PR splits for zero-change skips and delta follow-ups (#123523)
Julian Bez9783d120a38e
diff --git a/products/review_hog/ARCHITECTURE.md b/products/review_hog/ARCHITECTURE.mdindex cf65ec567f68..3e0b5d994456 100644--- a/products/review_hog/ARCHITECTURE.md+++ b/products/review_hog/ARCHITECTURE.md@@ -978,6 +978,10 @@ 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), `small_change` (the compare of the last reviewed head and the push head changes 1 to `SMALL_CHANGE_MAX_NET_CODE_LINES` code lines; a per-PR Redis counter allows `SMALL_CHANGE_MAX_DEFERRALS` deferrals in a row, then the next small push runs the review), `system_one_below_threshold` (System One rates the own code patches below `SYSTEM_ONE_SKIP_BELOW`), `draft` (the PR is a draft and no earlier rule skips the push), `late_follow_up` (the push would start review number `LATE_FOLLOW_UP_RUN_INDEX` or a later one), `small_pr` (the PR has fewer than `SMALL_PR_REVIEWABLE_LINES_BELOW` reviewable lines, the size the lens parts use), `quiet_small_push` (the last review kept no finding and the push changes at most `QUIET_PUSH_MAX_OWN_CODE_LINES` own code lines; skipped pushes add up until one crosses the limit), `next_push_likely` (the PR is not a draft, no earlier rule matched, and the chance of another automatic follow-up is at least `NEXT_PUSH_SKIP_AT`), and `throttled` (the PR spent its review budget).The `draft` rule reads the draft state from the turn's own PR snapshot: every turn fetches the PR before the gate and the snapshot load is latest-wins, so a PR marked ready at an unchanged head reads as ready. A draft skip reads the PR from GitHub again after the gate's other reads: a PR marked ready while the turn runs sends a ready request that the queue drops, because it asks for the head the running turn gates. If the PR is ready by then, the gate uses the decision of the rules before `draft`. A failed read reviews the push. Only a PR marked ready between that read and the end of the turn keeps its changes unreviewed until the next trigger. A draft decision keeps the System One answer and `own_code_lines` of the rules that ran before it.The `throttled` rule keeps a token bucket per PR in Redis (`PUSH_BUDGET`): it holds two reviews and gets one back every 30 minutes. Only a push that every earlier rule lets through takes a token, so a draft push or a merge from the base costs nothing. A push that finds the bucket empty waits for the next trigger like any other skip. A Redis failure reviews the push. A head pays once: a turn that runs again at the same head, as an activity retry or a retried `review-pr` child, does not take a second token. A retry after a denial asks the bucket again, so it cannot get past the budget. The first review and human triggers never reach the gate, so they neither take nor need a token.+Two per-PR splits run on top of these rules. Their flags are keyed like the coordinator flag, and a flag that is off puts every PR in the control arm.+`reviewhog-zero-change-skip` adds the `zero_change` rule after `quiet_small_push`: the gate compares the stored single-agent PR snapshots of the last automatically reviewed head and this head (`ChangedSinceReview`), and a PR whose reviewed files did not change skips in the treatment arm. `merge_only` already skips a push whose whole PR diff is unchanged (a base merge, a rebase, an amend), so this rule catches pushes that change only files the review leaves out, such as generated files and snapshots. The snapshots leave lockfiles out too, so a PR with a lockfile at either head never matches, and neither does a PR with a file whose patch GitHub left out (`PRChange.comparable`). The control arm reviews the push and records the match, so it is the audit. A missing snapshot never matches. A skipped push does not move the baseline, so the next comparison covers it.+`reviewhog-delta-band` lets a turn that passes every rule review only what changed: from run `DELTA_BAND_FIRST_RUN_INDEX` (3) on, with changed reviewable lines at most `DELTA_BAND_MAX_CHANGE_SHARE` (3%) of the PR's reviewable lines, no dependency file changed, and a comparison the snapshots show in full: no lockfile in the PR, no file without a patch, and no file that left the PR (`plan_delta_review`). The first follow-up always reviews the whole PR. A delta turn passes the baseline head to its sessions (`SandboxStageInput.delta_since_head_sha`), runs one session per lens, and each prompt shows only the changed files with the diff blocks around the changed lines (`delta_files`), plus the earlier findings, the full repository at the head, and the git command for the whole diff since the baseline, which also shows code the PR no longer changes. A gate failure, or a baseline snapshot that is gone when the prompt is built, gives the whole-PR prompt, also in each lens session.+The gate event carries `pr_changed_lines`, both arms, `coordinator`, `delta_eligible` and `delta_review`; every completed event carries `zero_change_skip_arm`, `delta_band_arm` and `delta_review`, so each split compares by arm over PR lifecycles. A coordinated PR always records the control arm of the delta split, so a delta comparison leaves out gate events with `coordinator` set.The `next_push_likely` chance comes from a table fitted offline on closed PRs and refitted by hand. Its key is the follow-up number, capped at 8, and the PR size band (additions plus deletions: up to 200, up to 1000, more). A size cell with fewer than 10 PRs falls back to the chance for the follow-up number alone. The follow-up number is `run_index - 1`, so it also counts label and UI turns, which the fit left out.Each rule has a `SKIP_*` switch. `no_new_commits`, `reviewhog_commits_only`, `merge_only`, `draft`, `late_follow_up`, `small_pr`, `quiet_small_push`, and `throttled` skip; the three value rules also take over a shadow match of an earlier rule, but never a gate failure, and they do not run on a retry of a turn that already finalized its head; `docs_only`, `small_change`, System One, and `next_push_likely` (`SKIP_NEXT_PUSH`) 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.diff --git a/products/review_hog/backend/reviewer/feature_flags.py b/products/review_hog/backend/reviewer/feature_flags.pyindex 27d349deba8e..dd5e6badf3f7 100644--- a/products/review_hog/backend/reviewer/feature_flags.py+++ b/products/review_hog/backend/reviewer/feature_flags.py@@ -36,21 +36,34 @@ def flash_pipeline_kill_switch_on(team_id: int) -> bool:STANDARD_COORDINATOR_FLAG = "reviewhog-standard-coordinator"+ZERO_CHANGE_SKIP_FLAG = "reviewhog-zero-change-skip"+DELTA_BAND_FLAG = "reviewhog-delta-band"-def standard_coordinator_on(repository: str, pr_number: int) -> bool:- """Whether this PR's Standard turns run their sessions as sub-agents of one coordinator session.+def _pull_request_flag_on(flag: str, repository: str, pr_number: int) -> bool:+ """Whether `flag` is on for this PR.Keyed by the PR, not the organization, so a percentage rollout splits the reviews of one organization- and every turn of a PR runs on the same design. Any evaluation failure reads as off.+ and every turn of a PR gets the same answer. Any evaluation failure reads as off."""try:- enabled = posthoganalytics.feature_enabled(- STANDARD_COORDINATOR_FLAG,- f"{repository}#{pr_number}",- send_feature_flag_events=False,- )+ enabled = posthoganalytics.feature_enabled(flag, f"{repository}#{pr_number}", send_feature_flag_events=False)except Exception:- logger.exception("Could not evaluate %s for %s#%s", STANDARD_COORDINATOR_FLAG, repository, pr_number)+ logger.exception("Could not evaluate %s for %s#%s", flag, repository, pr_number)return Falsereturn enabled is True+++def standard_coordinator_on(repository: str, pr_number: int) -> bool:+ """Whether this PR's Standard turns run their sessions as sub-agents of one coordinator session."""+ return _pull_request_flag_on(STANDARD_COORDINATOR_FLAG, repository, pr_number)+++def skips_zero_change_follow_ups(repository: str, pr_number: int) -> bool:+ """Whether this PR is in the treatment arm of the zero-change split: the gate skips a push whose PR diff did not change."""+ return _pull_request_flag_on(ZERO_CHANGE_SKIP_FLAG, repository, pr_number)+++def delta_reviews_low_risk_follow_ups(repository: str, pr_number: int) -> bool:+ """Whether this PR is in the treatment arm of the delta split: low-risk later follow-ups review only what changed."""+ return _pull_request_flag_on(DELTA_BAND_FLAG, repository, pr_number)diff --git a/products/review_hog/backend/reviewer/prompts/single_agent_review/prompt.jinja b/products/review_hog/backend/reviewer/prompts/single_agent_review/prompt.jinjaindex e5d45c16377b..4f67a61d515e 100644--- a/products/review_hog/backend/reviewer/prompts/single_agent_review/prompt.jinja+++ b/products/review_hog/backend/reviewer/prompts/single_agent_review/prompt.jinja@@ -19,6 +19,12 @@ Everything quoted from the pull request below (the title, the description, the d{{ FILE_LIST|safe }}Lock files, generated files, snapshots, and images are left out of this list and of the diff. Read them in the checkout when a finding depends on them.+{% if DELTA_SINCE %}++## Changes since the last review++Earlier reviews covered this pull request up to commit `{{ DELTA_SINCE }}`. This review covers only what changed since then: the file list and the diff below show only the changed files and the parts of the pull request's diff around the changes. Report problems in these changes or caused by them, and read any file in the repository to follow them. To see everything that changed since then, including code the pull request no longer changes, run `git fetch --depth 1 origin {{ DELTA_SINCE }}` and `git diff {{ DELTA_SINCE }} HEAD`.{% if BASE_SHA %} The whole pull request diff is available with git: run `git fetch --depth 1 origin {{ BASE_SHA }}` and `git diff {{ BASE_SHA }} HEAD`.{% endif %}+{% endif %}{% if SCOPE %}## Scopediff --git a/products/review_hog/backend/reviewer/push_gate.py b/products/review_hog/backend/reviewer/push_gate.pyindex 734a755eb2fe..4a527ffcb770 100644--- a/products/review_hog/backend/reviewer/push_gate.py+++ b/products/review_hog/backend/reviewer/push_gate.py@@ -45,6 +45,20 @@ The rules, in order. The first rule that matches gives the reason:the next turn that runs reviews everything since the last reviewed head. This caps bursts ofpushes, and it never schedules a review on its own.+13. `zero_change`: the reviewed files of the PR are the same as at the last automatically reviewed head. It compares+ the stored PR snapshots of both heads. Rule 3 already skips a push whose whole PR diff is unchanged, so this rule+ catches pushes that change only files the review leaves out, such as generated files and snapshots. The snapshots+ leave lockfiles out too, so a PR with a lockfile at either head never matches, and neither does a PR with a file+ whose patch GitHub left out (`PRChange.comparable`). A push after skipped pushes compares against the same+ baseline. It runs after rule 10 and before rule 11, and only a PR in the treatment arm of+ `reviewhog-zero-change-skip` skips. An unknown comparison (no snapshot at either head) never matches.++A turn that passes every rule can review only what changed (`plan_delta_review`): a follow-up from run+`DELTA_BAND_FIRST_RUN_INDEX` on, whose changed reviewable lines are at most `DELTA_BAND_MAX_CHANGE_SHARE` of the PR's+reviewable lines, with no dependency file changed and a comparison the snapshots show in full: no lockfile in the PR,+no file without a patch, and no file that left the PR. The first follow-up always reviews the whole PR. Only a PR in the treatment arm of `reviewhog-delta-band`+runs the delta review; the other arm records that the turn was eligible.+Each rule has its own switch. A rule that is switched off still reports the turn it would skip(`would_skip`), so production data can calibrate it before it skips anything. Every failure failsopen: the turn runs.@@ -69,8 +83,10 @@ from posthog.redis import get_clientfrom posthog.token_bucket import BucketUnavailable, Budget, consumefrom products.review_hog.backend.app_identity import posthog_app_login+from products.review_hog.backend.reviewer.artefact_content import PRSnapshotArtefactfrom products.review_hog.backend.reviewer.tools.github_client import GitHubAPIError, github_api_request, matches_app_botfrom products.review_hog.backend.reviewer.tools.github_meta import GITHUB_COMPARE_FILES_CAP, PRParser+from products.review_hog.backend.reviewer.tools.single_agent_review import ChangedSinceReviewlogger = logging.getLogger(__name__)@@ -110,6 +126,15 @@ SMALL_PR_REVIEWABLE_LINES_BELOW = 150QUIET_PUSH_MAX_OWN_CODE_LINES = 30# Picked by the replay in `review-eval/experiments/2026-10-14-review-throttle`.PUSH_BUDGET = Budget(burst=2, per_hour=2)+# Picked by the backtest in `review-eval/experiments/2026-10-26-economics-report` (calibrate67) and fixed for the experiment.+DELTA_BAND_FIRST_RUN_INDEX = 3+DELTA_BAND_MAX_CHANGE_SHARE = 0.03+# A change to one of these files changes what the code runs with, so it never counts as small or as no change. The+# lockfile endings match every lockfile the review snapshots leave out (`PRFilter`), so none of them changes unseen.+_DEPENDENCY_FILE = re.compile(+ r"(^|/)(package\.json|pyproject\.toml|requirements[^/]*\.txt|go\.mod|go\.sum|Cargo\.toml|Gemfile|composer\.json|"+ r"npm-shrinkwrap\.json)$|(\.lock|\.lockb|lock\.json|lock\.ya?ml)$"+)# Longer than a turn and its retry take, so a retried turn finds that its head paid already._PAID_HEAD_TTL_SECONDS = 6 * 3600@@ -220,6 +245,7 @@ SkipReason = Literal["quiet_small_push","next_push_likely","throttled",+ "zero_change",]RunReason = Literal["commits_removed",@@ -251,6 +277,42 @@ class PushGateDecision:# Inputs of rules 8 to 10. None when the gate could not read them.reviewable_lines: int | None = Noneprevious_findings: int | None = None+ # Lines of the PR's own diff that changed since the last automatically reviewed head, in every file. None when unknown.+ pr_changed_lines: int | None = None+ delta_eligible: bool = False+ delta_review: bool = False+++def is_dependency_file(path: str) -> bool:+ return _DEPENDENCY_FILE.search(path) is not None+++@frozen+class PRChange:+ """How the PR's diff changed since the last automatically reviewed head, as far as the stored snapshots show it."""++ # Diff lines added or removed since that head, in every file of the snapshots.+ changed_lines: int+ changed_reviewable_lines: int+ dependency_changed: bool+ # False when a change can hide from the snapshots, so neither split may act on the comparison.+ comparable: bool++ @classmethod+ def between(cls, earlier: PRSnapshotArtefact, current: PRSnapshotArtefact) -> "PRChange":+ change = ChangedSinceReview.between(earlier.pr_files, current.pr_files)+ changed = {filename for filename, count in change.changed_line_counts.items() if count}+ current_files = {pr_file.filename for pr_file in current.pr_files}+ return cls(+ changed_lines=sum(change.changed_line_counts.values()),+ changed_reviewable_lines=change.reviewable_line_count(),+ dependency_changed=any(is_dependency_file(filename) for filename in changed),+ # The snapshots leave lockfiles out, a file without a patch shows only its line counts, and a delta prompt+ # shows only the files still in the PR.+ comparable=not change.files_without_patch+ and changed <= current_files+ and not any(is_dependency_file(path) for path in [*earlier.skipped_paths, *current.skipped_paths]),+ )@frozen@@ -737,3 +799,39 @@ def apply_throttle(decision: PushGateDecision, *, report_id: str, head_sha: str)return decisionclient.delete(paid_key)return replace(decision, skip=_rule_enabled("throttled"), would_skip=True, reason="throttled")+++def apply_zero_change_rule(decision: PushGateDecision, *, change: PRChange | None, enabled: bool) -> PushGateDecision:+ """Rule 13, after rules 8 to 10. It takes over a shadow match. A missing or incomplete comparison runs the push."""+ decision = replace(decision, pr_changed_lines=change.changed_lines if change is not None else None)+ if decision.skip or decision.reason in _FAIL_OPEN_REASONS or change is None or not change.comparable:+ return decision+ if change.changed_lines != 0:+ return decision+ return replace(decision, skip=enabled, would_skip=True, reason="zero_change")+++def plan_delta_review(+ decision: PushGateDecision,+ *,+ run_index: int,+ change: PRChange | None,+ reviewable_lines: int | None,+ enabled: bool,+) -> PushGateDecision:+ """Whether a turn that runs reviews only what changed since the last automatically reviewed head.++ Anything unknown (no comparison, a change the snapshots cannot show, a gate that could not judge the push) keeps the+ full review.+ """+ eligible = (+ not decision.skip+ and decision.reason not in _FAIL_OPEN_REASONS+ and change is not None+ and change.comparable+ and not change.dependency_changed+ and run_index >= DELTA_BAND_FIRST_RUN_INDEX+ and reviewable_lines is not None+ and 0 < change.changed_reviewable_lines <= DELTA_BAND_MAX_CHANGE_SHARE * reviewable_lines+ )+ return replace(decision, delta_eligible=eligible, delta_review=eligible and enabled)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.pyindex 91c4d53b1695..797c6ec33f82 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@@ -26,6 +26,7 @@ from products.review_hog.backend.reviewer.tools.single_agent_review import (SingleAgentPrompt,compose_flash_findings,dedupe_flash_findings,+ delta_files,flash_turn_stats,)@@ -181,6 +182,56 @@ class TestSingleAgentPrompt:assert prompt.split(PROMPT_CACHE_MARKER)[1].lstrip().startswith("CORE RUBRIC")+ "delta_since,expect_section",+ [+ pytest.param("b" * 40, True, id="commit_sha"),+ pytest.param("main$(id)", False, id="not_a_commit_sha"),+ ],+)+def test_a_delta_prompt_shows_only_the_changed_blocks_and_names_the_baseline(+ pr_metadata: PRMetadata, delta_since: str, expect_section: bool+) -> None:+ def blocks(fix: str) -> list[PRFileUpdate]:+ return [+ PRFileUpdate(type="addition", new_start_line=10, new_end_line=10, code="limit = 10"),+ PRFileUpdate(type="context", new_start_line=11, new_end_line=13, code="a = 1\nb = 2\nc = 3"),+ PRFileUpdate(type="context", new_start_line=38, new_end_line=40, code="d = 4\ne = 5\nf = 6"),+ PRFileUpdate(type="deletion", old_start_line=41, old_end_line=41, code="old_total()"),+ PRFileUpdate(type="addition", new_start_line=41, new_end_line=41, code=fix),+ ]++ earlier = [+ PRFile(filename="a.py", status="modified", additions=2, deletions=1, changes=blocks("total()")),+ _file("b.py", "x = 1"),+ ]+ removed = PRFileUpdate(type="deletion", old_start_line=1, old_end_line=2, code="import os\nos.getcwd()")+ current = [+ PRFile(filename="a.py", status="modified", additions=2, deletions=1, changes=blocks("total(strict=True)")),+ _file("b.py", "x = 1"),+ # A push that deletes a whole file leaves only removals, with no head line to be near.+ PRFile(filename="c.py", status="removed", additions=0, deletions=2, changes=[removed]),+ ]++ shown = delta_files(current, ChangedSinceReview.between(earlier, current))+ prompt = SingleAgentPrompt(+ repository="o/r",+ pr_metadata=pr_metadata,+ pr_files=shown,+ prior_findings=[],+ guidelines="rubric",+ merge_base_sha=_MERGE_BASE,+ delta_since=delta_since,+ ).render()++ assert [f.filename for f in shown] == ["a.py", "c.py"]+ assert [u.code for u in shown[0].changes] == ["d = 4\ne = 5\nf = 6", "old_total()", "total(strict=True)"]+ assert shown[1].changes == [removed]+ assert ("## Changes since the last review" in prompt) is expect_section+ assert (f"git diff {delta_since} HEAD" in prompt) is expect_section+ assert "$(" not in prompt++class TestComposeFlashFindings:@pytest.mark.parametrize("main,lens,lens_part_count,expected_ids",diff --git a/products/review_hog/backend/reviewer/tools/single_agent_review.py b/products/review_hog/backend/reviewer/tools/single_agent_review.pyindex f892c5c0dc10..76477f45a40b 100644--- a/products/review_hog/backend/reviewer/tools/single_agent_review.py+++ b/products/review_hog/backend/reviewer/tools/single_agent_review.py@@ -34,7 +34,7 @@ from products.review_hog.backend.reviewer.constants import (priority_rank,)from products.review_hog.backend.reviewer.models import PROMPTS_DIR-from products.review_hog.backend.reviewer.models.github_meta import PRFile, PRMetadata+from products.review_hog.backend.reviewer.models.github_meta import PRFile, PRFileUpdate, PRMetadatafrom products.review_hog.backend.reviewer.models.issues_review import (DropDisposition,DroppedIssue,@@ -67,6 +67,10 @@ _LEADING_HTML_COMMENT = re.compile(r"\A\s*<!--.*?-->\s*", re.S)_COMMIT_SHA = re.compile(r"[0-9a-f]{40}|[0-9a-f]{64}")+def _commit_sha(value: str | None) -> str | None:+ return value if value is not None and _COMMIT_SHA.fullmatch(value) else None++def load_prompt_file(path: Path) -> str:"""A prompt file as the model receives it."""return _LEADING_HTML_COMMENT.sub("", path.read_text(), count=1).strip()@@ -100,8 +104,10 @@ class SingleAgentPrompt:merge_base_sha: str | None = None,scope_files: list[str] | None = None,for_lens: bool = False,+ delta_since: str | None = None,) -> None:self.repository = repository+ self.delta_since = delta_sinceself.guidelines = guidelinesself.pr_metadata = pr_metadataself.merge_base_sha = merge_base_sha@@ -174,8 +180,7 @@ class SingleAgentPrompt:def _base_sha(self) -> str | None:"""The merge base when it is a commit id, else None: the prompt then gives no git command."""- sha = self.merge_base_sha- return sha if sha is not None and _COMMIT_SHA.fullmatch(sha) else None+ return _commit_sha(self.merge_base_sha)def render(self) -> str:template, output_schema = load_template_and_schema(SINGLE_AGENT_PROMPT_DIR)@@ -190,6 +195,8 @@ class SingleAgentPrompt:REPOSITORY=self.repository,HEAD_SHA=self.pr_metadata.head_sha or self.pr_metadata.head_branch,BASE_SHA=self._base_sha(),+ # A commit id only, so no text from the repository reaches the session's shell.+ DELTA_SINCE=_commit_sha(self.delta_since),PR_TITLE=without_cache_marker(self.pr_metadata.title),PR_DESCRIPTION=without_cache_marker(self.pr_metadata.body.strip() or "(no description provided)"),FILE_LIST=without_cache_marker(self._file_list(not_shown)),@@ -609,6 +616,13 @@ def _count_delta(pr_file: PRFile | None, earlier: PRFile | None) -> int:return abs(additions - earlier_additions) + abs(deletions - earlier_deletions)+def _within_margin(start: int, end: int | None, lines: set[int]) -> bool:+ """Whether any of `lines` falls in `start`..`end`, widened by the follow-up change margin."""+ margin = FLASH_FOLLOW_UP_CHANGE_MARGIN_LINES+ # The range can come from the model, so it is compared, never expanded line by line.+ return any(start - margin <= line <= (end or start) + margin for line in lines)++@frozenclass ChangedSinceReview:"""The lines of the head that changed since an earlier reviewed head, per file."""@@ -646,13 +660,41 @@ class ChangedSinceReview:if not issue.lines or issue.file in self.files_without_patch:return Truechanged = self.lines.get(issue.file, set())- margin = FLASH_FOLLOW_UP_CHANGE_MARGIN_LINES- # The range comes from the model, so it is compared, never expanded line by line.- return any(- line_range.start - margin <= line <= (line_range.end or line_range.start) + margin- for line_range in issue.lines- for line in changed- )+ return any(_within_margin(line_range.start, line_range.end, changed) for line_range in issue.lines)+++def _near_changed(update: PRFileUpdate, changed: set[int]) -> bool:+ return update.new_start_line is not None and _within_margin(update.new_start_line, update.new_end_line, changed)+++def _borders_near(near: list[bool], index: int) -> bool:+ return (index > 0 and near[index - 1]) or (index + 1 < len(near) and near[index + 1])+++def delta_files(pr_files: list[PRFile], change: ChangedSinceReview) -> list[PRFile]:+ """The PR's files that changed since the earlier head, each with only the diff blocks around the changed lines.++ A removal block stays when it borders a kept block, so a removed guard shows next to the code it guarded. A file+ without a patch stays whole when its counts changed. Files the push did not touch drop out.+ """+ result: list[PRFile] = []+ for pr_file in pr_files:+ if pr_file.filename in change.files_without_patch:+ if change.changed_line_counts.get(pr_file.filename):+ result.append(pr_file)+ continue+ changed = change.lines.get(pr_file.filename)+ if not changed:+ continue+ near = [_near_changed(update, changed) for update in pr_file.changes]+ kept = [+ update+ for index, update in enumerate(pr_file.changes)+ if near[index] or (update.type == "deletion" and _borders_near(near, index))+ ]+ # A change with only removals, such as a deleted file, has no head line to be near, so the file stays whole.+ result.append(pr_file.model_copy(update={"changes": kept}) if kept else pr_file)+ return resultdef _old_code_findings(diff --git a/products/review_hog/backend/temporal/activities.py b/products/review_hog/backend/temporal/activities.pyindex 3ddf53766dd6..8b9a131c680b 100644--- a/products/review_hog/backend/temporal/activities.py+++ b/products/review_hog/backend/temporal/activities.py@@ -76,7 +76,12 @@ from products.review_hog.backend.reviewer.constants import (select_review_design,validation_arm_for_mode,)-from products.review_hog.backend.reviewer.feature_flags import flash_pipeline_kill_switch_on, standard_coordinator_on+from products.review_hog.backend.reviewer.feature_flags import (+ delta_reviews_low_risk_follow_ups,+ flash_pipeline_kill_switch_on,+ skips_zero_change_follow_ups,+ standard_coordinator_on,+)from products.review_hog.backend.reviewer.finding_budget import FindingBudget, ReviewRound, budget_armfrom products.review_hog.backend.reviewer.fingerprint import ReviewHogMarker, record_turn_markerfrom products.review_hog.backend.reviewer.lazy_seed import (@@ -129,12 +134,15 @@ from products.review_hog.backend.reviewer.progress import ()from products.review_hog.backend.reviewer.push_gate import (SYSTEM_ONE_SKIP_BELOW,+ PRChange,PushGate,PushGateDecision,apply_draft_rule,apply_follow_up_value_rules,apply_next_push_rule,apply_throttle,+ apply_zero_change_rule,+ plan_delta_review,)from products.review_hog.backend.reviewer.review_design import REVIEW_DESIGN_PIPELINE, REVIEW_DESIGN_SINGLE_AGENTfrom products.review_hog.backend.reviewer.review_state import review_already_published@@ -206,6 +214,7 @@ from products.review_hog.backend.reviewer.tools.single_agent_review import (FlashTurnStats,SingleAgentPrompt,dedupe_flash_findings,+ delta_files,flash_turn_stats,issues_from_review,lens_prompt_path,@@ -313,6 +322,10 @@ class ReviewMeta:# A single-agent turn that runs its sessions as sub-agents of one coordinator session. Recorded here# so a replay takes the same branch. Older histories decode as separate sessions.coordinator: bool = False+ # The PR's arms in the zero-change and delta splits. Recorded here so a replay takes the same branch.+ # Older histories decode as the control arms.+ zero_change_skip: bool = False+ delta_band: bool = False# None means no automatic review has run on the PR, so the push gate stays off for this turn.automatic_reviewed_head_sha: str | None = None# An automatic turn on a PR with the skip-agent-review label. Older histories decode as no skip.@@ -423,6 +436,8 @@ class SandboxStageInput:review_design: str = field(default=REVIEW_DESIGN_PIPELINE, kw_only=True)# Only an automatic turn is held to the finding budget. None on payloads from before the field.trigger_source: str | None = field(default=None, kw_only=True)+ # Set on a delta turn: the sessions see only what changed since this reviewed head.+ delta_since_head_sha: str | None = field(default=None, kw_only=True)@dataclass@@ -612,6 +627,9 @@ class TrackReviewCompletedInput:# Single-agent turns only.flash_turn: FlashTurnStats | None = Noneflash_sessions: FlashSessionStats | None = None+ zero_change_skip_arm: bool = False+ delta_band_arm: bool = False+ delta_review: bool = False@frozen@@ -673,6 +691,10 @@ class GatePushInput:head_sha: strrun_index: intreview_mode: str+ zero_change_skip: bool = False+ delta_band: bool = False+ # A coordinated PR always records the control arm of the delta split, so evaluations leave these PRs out.+ coordinator: bool = False@dataclass(frozen=False)@@ -910,6 +932,8 @@ def _fetch_and_persist(input: FetchPRDataInput) -> ReviewMeta:ReviewReport.objects.for_team(input.team_id).filter(id=report_id).update(status=ReviewReport.Status.IDLE if fetched.pr_metadata.state == "open" else ReviewReport.Status.CLOSED)+ coordinator = single_agent and pr_number is not None and standard_coordinator_on(input.repository, pr_number)+ separate_sessions = single_agent and not coordinatorreturn ReviewMeta(report_id=report_id,head_sha=head_sha,@@ -932,7 +956,12 @@ def _fetch_and_persist(input: FetchPRDataInput) -> ReviewMeta:review_design_reason=design_choice.reason,lens_chunk_count=len(lens_plan.chunks) if lens_plan is not None else 0,lens_chunks_capped=lens_plan is not None and lens_plan.capped,- coordinator=single_agent and pr_number is not None and standard_coordinator_on(input.repository, pr_number),+ coordinator=coordinator,+ zero_change_skip=pr_number is not None and skips_zero_change_follow_ups(input.repository, pr_number),+ # The delta review only exists for separate sessions, so a coordinated PR stays in the control arm.+ delta_band=separate_sessions+ and pr_number is not None+ and delta_reviews_low_risk_follow_ups(input.repository, pr_number),automatic_reviewed_head_sha=report.automatic_reviewed_head_sha,skip_agent_review=skip_agent_review,)@@ -1445,6 +1474,20 @@ async def review_chunk_activity(input: ReviewChunkInput) -> bool:# --- Single-agent review (the main Flash session and its lens sessions) ---------------------------+def _changed_since_head(+ team_id: int, report_id: str, head_sha: str | None, current_files: list[PRFile]+) -> ChangedSinceReview | None:+ """What changed since a reviewed head's single-agent snapshot, or None without a head or a snapshot (a full review)."""+ if head_sha is None:+ return None+ earlier = load_pr_snapshot(+ team_id=team_id, report_id=report_id, head_sha=head_sha, review_design=REVIEW_DESIGN_SINGLE_AGENT+ )+ if earlier is None:+ return None+ return ChangedSinceReview.between(earlier.pr_files, current_files)++def _prepare_single_agent_prompt(input: SandboxStageInput, chunk_id: int, for_lens: bool, guidelines: str) -> str:"""The prompt of the main session, or of one lens session on lens part `chunk_id`."""snapshot = load_pr_snapshot(@@ -1452,8 +1495,11 @@ def _prepare_single_agent_prompt(input: SandboxStageInput, chunk_id: int, for_le)if snapshot is None:raise ApplicationError("PR snapshot missing for the single-agent review", non_retryable=True)+ change = _changed_since_head(input.team_id, input.report_id, input.delta_since_head_sha, snapshot.pr_files)scope_files: list[str] | None = None- if for_lens:+ # The workflow starts one session per lens on a delta turn, so a delta turn whose baseline is gone reviews the whole+ # PR in that session instead of one lens part.+ if for_lens and input.delta_since_head_sha is None:chunks = plan_lens_chunks(snapshot.pr_files).chunksif not 1 <= chunk_id <= len(chunks):raise ApplicationError(f"Lens part {chunk_id} is not in the plan of {len(chunks)}", non_retryable=True)@@ -1462,7 +1508,8 @@ def _prepare_single_agent_prompt(input: SandboxStageInput, chunk_id: int, for_lerepository=input.repository,pr_metadata=snapshot.pr_metadata,merge_base_sha=snapshot.merge_base_sha,- pr_files=snapshot.pr_files,+ pr_files=delta_files(snapshot.pr_files, change) if change is not None else snapshot.pr_files,+ delta_since=input.delta_since_head_sha if change is not None else None,prior_findings=load_prior_findings(team_id=input.team_id, report_id=input.report_id, before_run_index=input.run_index),@@ -1717,12 +1764,7 @@ def _changed_since_last_review()if reviewed_head is None or reviewed_head == head_sha:return None- reviewed = load_pr_snapshot(- team_id=team_id, report_id=report_id, head_sha=reviewed_head, review_design=REVIEW_DESIGN_SINGLE_AGENT- )- if reviewed is None:- return None- return ChangedSinceReview.between(reviewed.pr_files, current_files)+ return _changed_since_head(team_id, report_id, reviewed_head, current_files)def _current_pr_comments(input: SandboxStageInput, snapshot: "PRSnapshotArtefact") -> list[PRComment]:@@ -2343,6 +2385,12 @@ def _track_push_gate_decided(input: GatePushInput, decision: PushGateDecision, d"next_push_probability": decision.next_push_probability,"reviewable_lines": decision.reviewable_lines,"previous_findings": decision.previous_findings,+ "pr_changed_lines": decision.pr_changed_lines,+ "zero_change_skip_arm": input.zero_change_skip,+ "delta_band_arm": input.delta_band,+ "coordinator": input.coordinator,+ "delta_eligible": decision.delta_eligible,+ "delta_review": decision.delta_review,},groups=groups(team=report.team),)@@ -2388,6 +2436,8 @@ def _gate_push(input: GatePushInput) -> PushGateDecision:current = load_pr_snapshot(team_id=input.team_id, report_id=input.report_id, head_sha=input.head_sha)draft = _pr_draft(current)gate: PushGate | None = None+ snapshot: PRSnapshotArtefact | None = None+ change: PRChange | None = Noneif current is None:decision = PushGateDecision(skip=False, would_skip=False, reason="snapshot_unavailable")else:@@ -2420,6 +2470,21 @@ def _gate_push(input: GatePushInput) -> PushGateDecision:reviewable_lines=plan_lens_chunks(current.pr_files).reviewable_lines if current.pr_files else None,previous_findings=_previous_turn_findings(input),)+ snapshot = load_pr_snapshot(+ team_id=input.team_id,+ report_id=input.report_id,+ head_sha=input.head_sha,+ review_design=REVIEW_DESIGN_SINGLE_AGENT,+ )+ earlier = load_pr_snapshot(+ team_id=input.team_id,+ report_id=input.report_id,+ head_sha=input.previous_head_sha,+ review_design=REVIEW_DESIGN_SINGLE_AGENT,+ )+ if snapshot is not None and earlier is not None:+ change = PRChange.between(earlier, snapshot)+ decision = apply_zero_change_rule(decision, change=change, enabled=input.zero_change_skip)if not draft:# The offline fit counted only automatic follow-ups. No turn stores its trigger, so this# count is the completed turns after the first review, which also includes label, UI,@@ -2434,6 +2499,13 @@ def _gate_push(input: GatePushInput) -> PushGateDecision:decision = gate.record_small_change_streak(decision, pr_number=current.pr_metadata.number, head_sha=input.head_sha)+ decision = plan_delta_review(+ decision,+ run_index=input.run_index,+ change=change,+ reviewable_lines=plan_lens_chunks(snapshot.pr_files).reviewable_lines if snapshot is not None else None,+ enabled=input.delta_band,+ )if decision.skip:ReviewReport.objects.for_team(input.team_id).filter(id=input.report_id).update(status=ReviewReport.Status.IDLE)try:@@ -2507,6 +2579,9 @@ def _track_review_completed(input: TrackReviewCompletedInput) -> None:"reviewhog_version": input.marker.version if input.marker is not None else None,"reviewhog_fingerprint": input.marker.fingerprint if input.marker is not None else None,**_flash_event_properties(input.flash_turn, input.flash_sessions),+ "zero_change_skip_arm": input.zero_change_skip_arm,+ "delta_band_arm": input.delta_band_arm,+ "delta_review": input.delta_review,},groups=groups(team=report.team),send_feature_flags=True,diff --git a/products/review_hog/backend/temporal/workflow.py b/products/review_hog/backend/temporal/workflow.pyindex 576a1e904706..fa9123d246e7 100644--- a/products/review_hog/backend/temporal/workflow.py+++ b/products/review_hog/backend/temporal/workflow.py@@ -41,6 +41,7 @@ from products.review_hog.backend.reviewer.constants import ()from products.review_hog.backend.reviewer.fingerprint import ReviewHogMarkerfrom products.review_hog.backend.reviewer.models.issues_review import IssuePriority+from products.review_hog.backend.reviewer.push_gate import PushGateDecisionfrom products.review_hog.backend.reviewer.review_design import REVIEW_DESIGN_PIPELINE, REVIEW_DESIGN_SINGLE_AGENTfrom products.review_hog.backend.reviewer.status_comment import FinalizeStatusCommentInputfrom products.review_hog.backend.reviewer.tools.select_perspectives import PerspectiveSelectionDTO, apply_selection@@ -591,6 +592,7 @@ class ReviewPRWorkflow:# Only an automatic follow-up is gated. The first automatic review of a PR and every human# trigger always run. The gate fails open: an activity failure reviews the push.+ gate: PushGateDecision | None = Noneif (workflow.patched("reviewhog-push-gate-2026-10")and inputs.trigger_source == TRIGGER_AUTOMATIC@@ -607,6 +609,9 @@ class ReviewPRWorkflow:head_sha=head_sha,run_index=meta.run_index,review_mode=inputs.review_mode,+ zero_change_skip=meta.zero_change_skip,+ delta_band=meta.delta_band,+ coordinator=meta.coordinator,),start_to_close_timeout=_QUICK_TIMEOUT,retry_policy=_RETRY,@@ -616,6 +621,12 @@ class ReviewPRWorkflow:return report_idexcept ActivityError:workflow.logger.warning("The push gate failed; reviewing the push")+ # A gate that failed or never ran leaves the whole PR to review. The patch keeps older histories on full turns.+ delta_since_head_sha = (+ meta.automatic_reviewed_head_sha+ if gate is not None and gate.delta_review and workflow.patched("reviewhog-p1-lite-delta-2026-10")+ else None+ )# The design comes off the recorded fetch result. The patch keeps a history that reached this# point before the single-agent design existed on the pipeline when it replays.@@ -731,6 +742,7 @@ class ReviewPRWorkflow:flash_reasoning_effort=acting.flash_reasoning_effort,review_design=review_design,trigger_source=inputs.trigger_source,+ delta_since_head_sha=delta_since_head_sha,)# The patch keeps a history from before the coordinator existed on separate sessions when it replays.@@ -741,7 +753,8 @@ class ReviewPRWorkflow:):flash_sessions = await self._review_with_coordinator(stage, meta.lens_chunk_count)elif review_design == REVIEW_DESIGN_SINGLE_AGENT:- flash_sessions = await self._review_with_single_agent(stage, meta.lens_chunk_count)+ lens_parts = 1 if delta_since_head_sha is not None else meta.lens_chunk_count+ flash_sessions = await self._review_with_single_agent(stage, lens_parts)else:await self._review_with_pipeline(stage, acting_user_id)@@ -904,6 +917,9 @@ class ReviewPRWorkflow:review_design=review_design,flash_turn=dedup.flash_stats if dedup is not None else None,flash_sessions=flash_sessions,+ zero_change_skip_arm=meta.zero_change_skip,+ delta_band_arm=meta.delta_band,+ delta_review=delta_since_head_sha is not None,),start_to_close_timeout=_QUICK_TIMEOUT,retry_policy=_RETRY,@@ -1058,6 +1074,7 @@ class ReviewPRWorkflow:review_mode=stage.review_mode,flash_reasoning_effort=stage.flash_reasoning_effort,review_design=stage.review_design,+ delta_since_head_sha=stage.delta_since_head_sha,lens=lens,chunk_id=chunk_id,),diff --git a/products/review_hog/backend/tests/test_feature_flags.py b/products/review_hog/backend/tests/test_feature_flags.pyindex 21c3f1f80abb..daeff9add8a8 100644--- a/products/review_hog/backend/tests/test_feature_flags.py+++ b/products/review_hog/backend/tests/test_feature_flags.py@@ -5,7 +5,15 @@ from django.test import SimpleTestCasefrom parameterized import parameterized-from products.review_hog.backend.reviewer.feature_flags import flash_pipeline_kill_switch_on, standard_coordinator_on+from products.review_hog.backend.reviewer.feature_flags import (+ DELTA_BAND_FLAG,+ STANDARD_COORDINATOR_FLAG,+ ZERO_CHANGE_SKIP_FLAG,+ delta_reviews_low_risk_follow_ups,+ flash_pipeline_kill_switch_on,+ skips_zero_change_follow_ups,+ standard_coordinator_on,+)_FEATURE_ENABLED = "products.review_hog.backend.reviewer.feature_flags.posthoganalytics.feature_enabled"@@ -26,17 +34,25 @@ class TestFlashPipelineKillSwitch(BaseTest):assert flash_pipeline_kill_switch_on(self.team.id) is expected-class TestStandardCoordinator(SimpleTestCase):+class TestPullRequestFlags(SimpleTestCase):@parameterized.expand([- ("flag_on", MagicMock(return_value=True), True),- ("flag_error", MagicMock(side_effect=RuntimeError("flag service down")), False),- ("flag_unknown", MagicMock(return_value=None), False),+ (f"{name}_{case}", check, flag, feature_enabled, expected)+ for name, check, flag in (+ ("coordinator", standard_coordinator_on, STANDARD_COORDINATOR_FLAG),+ ("zero_change_skip", skips_zero_change_follow_ups, ZERO_CHANGE_SKIP_FLAG),+ ("delta_band", delta_reviews_low_risk_follow_ups, DELTA_BAND_FLAG),+ )+ for case, feature_enabled, expected in (+ ("flag_on", MagicMock(return_value=True), True),+ ("flag_error", MagicMock(side_effect=RuntimeError("flag service down")), False),+ ("flag_unknown", MagicMock(return_value=None), False),+ )])- def test_coordinator_reads_the_flag_per_pull_request(- self, _name: str, feature_enabled: MagicMock, expected: bool+ def test_reads_its_flag_per_pull_request(+ self, _name: str, check, flag: str, feature_enabled: MagicMock, expected: bool) -> None:with patch(_FEATURE_ENABLED, feature_enabled):- assert standard_coordinator_on("posthog/posthog", 42) is expected- assert feature_enabled.call_args.args[1] == "posthog/posthog#42"+ assert check("posthog/posthog", 42) is expected+ assert feature_enabled.call_args.args[:2] == (flag, "posthog/posthog#42")diff --git a/products/review_hog/backend/tests/test_fetch_review_tier.py b/products/review_hog/backend/tests/test_fetch_review_tier.pyindex d24797e2246a..f2d6cf90fddc 100644--- a/products/review_hog/backend/tests/test_fetch_review_tier.py+++ b/products/review_hog/backend/tests/test_fetch_review_tier.py@@ -91,12 +91,15 @@ class TestFetchDecidesTheTier(BaseTest):@parameterized.expand([- ("flash", REVIEW_MODE_FLASH, [10], 1, True),- ("flash_two_parts", REVIEW_MODE_FLASH, [600, 600], 2, True),- ("full", REVIEW_MODE_FULL, [10], 0, False),+ ("flash", REVIEW_MODE_FLASH, [10], True, 1, True, False),+ ("flash_two_parts", REVIEW_MODE_FLASH, [600, 600], True, 2, True, False),+ ("flash_separate_sessions", REVIEW_MODE_FLASH, [10], False, 1, False, True),+ ("full", REVIEW_MODE_FULL, [10], True, 0, False, False),])- @patch(f"{_MODULE}.standard_coordinator_on", return_value=True)+ @patch(f"{_MODULE}.delta_reviews_low_risk_follow_ups", return_value=True)+ @patch(f"{_MODULE}.skips_zero_change_follow_ups", return_value=True)+ @patch(f"{_MODULE}.standard_coordinator_on")@patch(f"{_MODULE}.flash_pipeline_kill_switch_on", return_value=False)@patch(f"{_MODULE}._installation_auth", return_value=("tok", None))@patch(f"{_MODULE}.PRFetcher")@@ -105,17 +108,22 @@ class TestFetchDecidesTheTier(BaseTest):_name: str,review_mode: str,additions: list[int],+ coordinator_flag_on: bool,expected_parts: int,expected_coordinator: bool,+ expected_delta_arm: bool,mock_fetcher: MagicMock,_auth: MagicMock,_kill_switch: MagicMock,- _coordinator: MagicMock,+ coordinator_flag: MagicMock,+ _zero_change_flag: MagicMock,+ _delta_flag: MagicMock,) -> None:# The workflow fans the lens sessions out over this count, so a fetch that leaves it at the# default runs every Flash turn without its lenses and no error shows it. Only the# single-agent design fetches test and `.txt` files, so the design must be known first.# Its prompt reads a left-out diff against the merge base, so the snapshot must keep it.+ coordinator_flag.return_value = coordinator_flag_onpr_files = [PRFile(filename=f"d{index}/a.py", status="modified", additions=count, deletions=0)for index, count in enumerate(additions)@@ -138,6 +146,7 @@ class TestFetchDecidesTheTier(BaseTest):assert meta.lens_chunk_count == expected_partsassert meta.coordinator is expected_coordinator+ assert (meta.zero_change_skip, meta.delta_band) == (True, expected_delta_arm)single_agent = review_mode == REVIEW_MODE_FLASHassert mock_fetcher.return_value.fetch_pr_data.call_args.kwargs == {"review_tests_and_text": single_agent,diff --git a/products/review_hog/backend/tests/test_persistence.py b/products/review_hog/backend/tests/test_persistence.pyindex 248e4c6537c9..bf03919b475b 100644--- a/products/review_hog/backend/tests/test_persistence.py+++ b/products/review_hog/backend/tests/test_persistence.py@@ -1253,6 +1253,40 @@ class TestPRSnapshot(BaseTest):assert pipeline is not Noneassert [f.filename for f in pipeline.pr_files] == ["src/a.py"]+ def test_a_delta_lens_session_without_its_baseline_reviews_the_whole_pr(self) -> None:+ report_id = upsert_review_report(+ team_id=self.team.id, repository="o/r", pr_url="u", pr_metadata=_pr_metadata(head_sha="sha1")+ )+ persist_pr_snapshot(+ team_id=self.team.id,+ report_id=report_id,+ head_sha="sha1",+ pr_metadata=_pr_metadata(head_sha="sha1"),+ pr_comments=[],+ pr_files=[+ PRFile(filename="src/a.py", status="modified", additions=500, deletions=0),+ PRFile(filename="src/tests/test_a.py", status="modified", additions=500, deletions=0),+ ],+ review_design=REVIEW_DESIGN_SINGLE_AGENT,+ )+ delta_input = SandboxStageInput(+ team_id=self.team.id,+ user_id=self.user.id,+ report_id=report_id,+ head_sha="sha1",+ repository="o/r",+ branch="feat",+ run_index=3,+ review_mode=REVIEW_MODE_FLASH,+ review_design=REVIEW_DESIGN_SINGLE_AGENT,+ delta_since_head_sha="0" * 40,+ )++ prompt = _prepare_single_agent_prompt(delta_input, chunk_id=1, for_lens=True, guidelines="lens")++ assert "## Scope" not in prompt+ assert "## Changes since the last review" not in prompt+class TestPersistVerdict(BaseTest):def test_persist_verdict_pairs_with_its_finding_by_issue_key(self) -> None:diff --git a/products/review_hog/backend/tests/test_push_gate.py b/products/review_hog/backend/tests/test_push_gate.pyindex d2f6708f884e..7ec904e7c56d 100644--- a/products/review_hog/backend/tests/test_push_gate.py+++ b/products/review_hog/backend/tests/test_push_gate.py@@ -14,8 +14,11 @@ from posthog.llm.system_one import NoulAnswer, SystemOneNotConfigured, SystemOnefrom posthog.redis import TEST_clear_clients, get_clientfrom posthog.token_bucket import BucketUnavailable, TEST_reset_scripts+from products.review_hog.backend.reviewer.artefact_content import PRSnapshotArtefact+from products.review_hog.backend.reviewer.models.github_meta import PRFile, PRFileUpdate, PRMetadatafrom products.review_hog.backend.reviewer.push_gate import (PUSH_GATE_MODEL,+ PRChange,PushGate,PushGateDecision,_NextPushCell,@@ -23,7 +26,9 @@ from products.review_hog.backend.reviewer.push_gate import (apply_follow_up_value_rules,apply_next_push_rule,apply_throttle,+ apply_zero_change_rule,next_push_probability,+ plan_delta_review,)from products.review_hog.backend.reviewer.tools.github_client import GitHubAPIError@@ -728,3 +733,212 @@ def test_next_push_rule_keeps_the_value_rule_inputs() -> None:with patch(f"{_MODULE}.NEXT_PUSH_SKIP_AT", 0.0):result = apply_next_push_rule(decision, follow_up=4, pr_lines=400)assert (result.reason, result.reviewable_lines, result.previous_findings) == ("next_push_likely", 400, 2)+++def _change(**overrides: Any) -> PRChange:+ fields: dict[str, Any] = {+ "changed_lines": 12,+ "changed_reviewable_lines": 12,+ "dependency_changed": False,+ "comparable": True,+ **overrides,+ }+ return PRChange(**fields)+++ [+ (+ "no_change_in_the_treatment_arm_skips",+ _running(0),+ _change(changed_lines=0),+ True,+ (True, True, "zero_change"),+ ),+ (+ "no_change_in_the_control_arm_runs_and_records_it",+ _running(0),+ _change(changed_lines=0),+ False,+ (False, True, "zero_change"),+ ),+ (+ "a_changed_line_runs",+ _running(0),+ _change(changed_lines=1),+ True,+ (False, False, "system_one_above_threshold"),+ ),+ ("an_unknown_comparison_runs", _running(0), None, True, (False, False, "system_one_above_threshold")),+ (+ "a_change_the_snapshots_cannot_show_runs",+ _running(0),+ _change(changed_lines=0, comparable=False),+ True,+ (False, False, "system_one_above_threshold"),+ ),+ (+ "a_shadow_match_is_taken_over",+ PushGateDecision(skip=False, would_skip=True, reason="docs_only"),+ _change(changed_lines=0),+ True,+ (True, True, "zero_change"),+ ),+ (+ "a_gate_failure_keeps_running",+ PushGateDecision(skip=False, would_skip=False, reason="compare_unavailable"),+ _change(changed_lines=0),+ True,+ (False, False, "compare_unavailable"),+ ),+ (+ "an_earlier_skip_keeps_its_reason",+ PushGateDecision(skip=True, would_skip=True, reason="merge_only"),+ _change(changed_lines=0),+ True,+ (True, True, "merge_only"),+ ),+ ]+)+def test_zero_change_rule_skips_only_a_known_unchanged_pr_diff_in_the_treatment_arm(+ _name: str,+ decision: PushGateDecision,+ change: PRChange | None,+ enabled: bool,+ expected: tuple[bool, bool, str],+) -> None:+ result = apply_zero_change_rule(decision, change=change, enabled=enabled)+ assert (result.skip, result.would_skip, result.reason) == expected+++def _snapshot(pr_files: list[PRFile], skipped_paths: list[str]) -> PRSnapshotArtefact:+ metadata = PRMetadata(+ number=1,+ title="t",+ state="open",+ draft=False,+ created_at="2026-10-01T00:00:00Z",+ updated_at="2026-10-01T00:00:00Z",+ author="a",+ base_branch="master",+ head_branch="feat",+ commits=1,+ additions=1,+ deletions=0,+ changed_files=1,+ )+ return PRSnapshotArtefact(head_sha="h", pr_metadata=metadata, pr_files=pr_files, skipped_paths=skipped_paths)+++def _code(filename: str, code: str) -> PRFile:+ update = PRFileUpdate(type="addition", new_start_line=1, new_end_line=1, code=code)+ return PRFile(filename=filename, status="modified", additions=1, deletions=0, changes=[update])+++ [+ ("same_reviewed_files", [_code("a.py", "x = 1")], [], [_code("a.py", "x = 1")], [], (0, False, True)),+ (+ "a_lockfile_in_the_pr_is_unseen_pnpm_lock_yaml",+ [_code("a.py", "x = 1")],+ ["pnpm-lock.yaml"],+ [_code("a.py", "x = 1")],+ ["pnpm-lock.yaml"],+ (0, False, False),+ ),+ (+ "a_lockfile_in_the_pr_is_unseen_bun_lockb",+ [_code("a.py", "x = 1")],+ ["bun.lockb"],+ [_code("a.py", "x = 1")],+ ["bun.lockb"],+ (0, False, False),+ ),+ (+ "a_lockfile_in_the_pr_is_unseen_npm_shrinkwrap_json",+ [_code("a.py", "x = 1")],+ ["npm-shrinkwrap.json"],+ [_code("a.py", "x = 1")],+ ["npm-shrinkwrap.json"],+ (0, False, False),+ ),+ (+ "a_file_without_a_patch_is_unseen_even_with_the_same_counts",+ [PRFile(filename="big.py", status="modified", additions=5000, deletions=0)],+ [],+ [PRFile(filename="big.py", status="modified", additions=5000, deletions=0)],+ [],+ (0, False, False),+ ),+ (+ "a_file_that_left_the_pr_has_no_delta_diff",+ [_code("a.py", "x = 1"), _code("b.py", "guard()")],+ [],+ [_code("a.py", "x = 1")],+ [],+ (1, False, False),+ ),+ (+ "a_manifest_change_is_a_dependency_change",+ [_code("package.json", '"a": "1"')],+ [],+ [_code("package.json", '"a": "2"')],+ [],+ (2, True, True),+ ),+ ]+)+def test_pr_change_is_comparable_only_when_the_snapshots_show_every_change(+ _name: str,+ earlier_files: list[PRFile],+ earlier_skipped: list[str],+ current_files: list[PRFile],+ current_skipped: list[str],+ expected: tuple[int, bool, bool],+) -> None:+ change = PRChange.between(_snapshot(earlier_files, earlier_skipped), _snapshot(current_files, current_skipped))+ assert (change.changed_lines, change.dependency_changed, change.comparable) == expected+++ [+ ("small_later_push_in_the_treatment_arm", _running(5), {}, (True, True)),+ ("small_later_push_in_the_control_arm", _running(5), {"enabled": False}, (True, False)),+ ("first_follow_up_stays_whole", _running(5), {"run_index": 2}, (False, False)),+ ("over_the_share_stays_whole", _running(5), {"change": _change(changed_reviewable_lines=31)}, (False, False)),+ ("at_the_share_is_in", _running(5), {"change": _change(changed_reviewable_lines=30)}, (True, True)),+ (+ "no_reviewable_change_stays_whole",+ _running(5),+ {"change": _change(changed_reviewable_lines=0)},+ (False, False),+ ),+ ("a_dependency_change_stays_whole", _running(5), {"change": _change(dependency_changed=True)}, (False, False)),+ ("an_uncomparable_change_stays_whole", _running(5), {"change": _change(comparable=False)}, (False, False)),+ ("an_unknown_comparison_stays_whole", _running(5), {"change": None}, (False, False)),+ (+ "a_gate_failure_stays_whole",+ PushGateDecision(skip=False, would_skip=False, reason="compare_unavailable"),+ {},+ (False, False),+ ),+ (+ "a_skipped_push_reviews_nothing",+ PushGateDecision(skip=True, would_skip=True, reason="small_pr"),+ {},+ (False, False),+ ),+ ]+)+def test_delta_review_covers_only_small_comparable_later_pushes(+ _name: str, decision: PushGateDecision, overrides: dict[str, Any], expected: tuple[bool, bool]+) -> None:+ inputs: dict[str, Any] = {+ "run_index": 3,+ "change": _change(),+ "reviewable_lines": 1000,+ "enabled": True,+ **overrides,+ }+ result = plan_delta_review(decision, **inputs)+ assert (result.delta_eligible, result.delta_review) == expecteddiff --git a/products/review_hog/backend/tests/test_review_completed_tracking.py b/products/review_hog/backend/tests/test_review_completed_tracking.pyindex 894d8f095155..8f8bde66fd7c 100644--- a/products/review_hog/backend/tests/test_review_completed_tracking.py+++ b/products/review_hog/backend/tests/test_review_completed_tracking.py@@ -21,7 +21,7 @@ from products.review_hog.backend.reviewer.constants import ()from products.review_hog.backend.reviewer.finding_budget import budget_armfrom products.review_hog.backend.reviewer.fingerprint import ReviewHogMarker-from products.review_hog.backend.reviewer.models.github_meta import PRFile, PRMetadata+from products.review_hog.backend.reviewer.models.github_meta import PRFile, PRFileUpdate, PRMetadatafrom products.review_hog.backend.reviewer.models.issue_validation import IssueValidationfrom products.review_hog.backend.reviewer.models.issues_review import Issue, IssuePriority, LineRangefrom products.review_hog.backend.reviewer.persistence import (@@ -476,6 +476,101 @@ class TestTrackReviewCompleted(BaseTest):props = capture.call_args.kwargs["properties"]assert (result.skip, result.reason, props["previous_findings"], props["reviewable_lines"]) == expected+ @parameterized.expand(+ [+ ("unchanged_in_the_treatment_arm", "limit = 10", [], True, (True, "zero_change", 0, False)),+ ("unchanged_in_the_control_arm", "limit = 10", [], False, (False, "zero_change", 0, False)),+ # The snapshot leaves a lockfile out, so its change is unseen, and it changes what the code runs with.+ (+ "lockfile_in_the_pr",+ "limit = 10",+ ["pnpm-lock.yaml"],+ True,+ (False, "system_one_above_threshold", 0, False),+ ),+ ("small_code_change", "limit = 20", [], True, (False, "system_one_above_threshold", 2, True)),+ (+ "small_code_change_with_a_lockfile_in_the_pr",+ "limit = 20",+ ["pnpm-lock.yaml"],+ True,+ (False, "system_one_above_threshold", 2, False),+ ),+ ]+ )+ def test_the_gate_compares_the_stored_pr_diff_of_both_heads(+ self,+ _name: str,+ changed_code: str,+ skipped_paths: list[str],+ treatment: bool,+ expected: tuple[bool, str, int, bool],+ ) -> None:+ report_id = self._review_report()++ def code_file(code: str) -> PRFile:+ return PRFile(+ filename="a.py",+ status="modified",+ additions=900,+ deletions=0,+ changes=[+ PRFileUpdate(type="addition", new_start_line=1, new_end_line=1, code=code),+ PRFileUpdate(+ type="addition",+ new_start_line=2,+ new_end_line=900,+ code="\n".join(f"x{line} = {line}" for line in range(2, 901)),+ ),+ ],+ )++ for head_sha, code in (("sha0", "limit = 10"), ("sha1", changed_code)):+ persist_pr_snapshot(+ team_id=self.team.id,+ report_id=report_id,+ head_sha=head_sha,+ pr_metadata=_pr_metadata(),+ pr_comments=[],+ pr_files=[code_file(code)],+ review_design=REVIEW_DESIGN_SINGLE_AGENT,+ skipped_paths=skipped_paths,+ )+ decision = PushGateDecision(+ skip=False, would_skip=False, reason="system_one_above_threshold", own_code_lines=100+ )++ with (+ patch("products.review_hog.backend.temporal.activities._installation_auth", return_value=("tok", None)),+ patch("products.review_hog.backend.temporal.activities.PushGate") as gate,+ patch("products.review_hog.backend.temporal.activities.apply_throttle", side_effect=lambda d, **_: d),+ patch("products.review_hog.backend.temporal.activities.posthoganalytics.capture") as capture,+ ):+ gate.return_value.decide.return_value = decision+ gate.return_value.record_small_change_streak.side_effect = lambda decision, **_: decision+ result = _gate_push(+ GatePushInput(+ team_id=self.team.id,+ report_id=report_id,+ repository="o/r",+ previous_head_sha="sha0",+ head_sha="sha1",+ run_index=3,+ review_mode=REVIEW_MODE_FLASH,+ zero_change_skip=treatment,+ delta_band=treatment,+ coordinator=not treatment,+ )+ )++ props = capture.call_args.kwargs["properties"]+ assert (result.skip, result.reason, props["pr_changed_lines"], result.delta_review) == expected+ assert (props["zero_change_skip_arm"], props["delta_band_arm"], props["coordinator"]) == (+ treatment,+ treatment,+ not treatment,+ )+def _gate_draft_push(self, report_id: str, live_pr: dict[str, object] | Exception) -> tuple[PushGateDecision, Any]:live = MagicMock(side_effect=live_pr) if isinstance(live_pr, Exception) else MagicMock()live.return_value.json.return_value = live_prdiff --git a/products/review_hog/backend/tests/test_temporal_workflow.py b/products/review_hog/backend/tests/test_temporal_workflow.pyindex 59123aba6814..5eb2dfc24097 100644--- a/products/review_hog/backend/tests/test_temporal_workflow.py+++ b/products/review_hog/backend/tests/test_temporal_workflow.py@@ -161,6 +161,10 @@ async def _run_full_review_pr_workflow(fail_main_session: bool = False,coordinator: bool = False,coordinator_lens_failures: int = 0,+ run_index: int = 1,+ zero_change_skip: bool = False,+ delta_band: bool = False,+ gate_delta_review: bool = False,automatic_reviewed_head_sha: str | None = None,skip_agent_review: bool = False,gate_skips: bool = False,@@ -211,6 +215,8 @@ async def _run_full_review_pr_workflow(design_calls: dict[str, set[str]] = {}single_agent_calls: list[str] = []lens_calls: list[tuple[str, int]] = []+ main_delta_calls: list[str | None] = []+ lens_delta_calls: list[str | None] = []coordinator_calls: list[int] = []started_coordinator: list[bool] = []lens_timeouts: list[timedelta | None] = []@@ -230,7 +236,7 @@ async def _run_full_review_pr_workflow(head_sha="sha1",branch="feat",repository="o/r",- run_index=1,+ run_index=run_index,snapshotted=not already_published,already_published=already_published,new_comment_count=0,@@ -244,19 +250,27 @@ async def _run_full_review_pr_workflow(lens_chunk_count=lens_chunk_count,lens_chunks_capped=lens_chunks_capped,coordinator=coordinator,+ zero_change_skip=zero_change_skip,+ delta_band=delta_band,automatic_reviewed_head_sha=automatic_reviewed_head_sha,skip_agent_review=skip_agent_review,)gate_calls: list[str] = []+ gate_arms: list[tuple[bool, bool]] = []@activity.defn(name="gate_push_activity")async def gate_push(input: GatePushInput) -> PushGateDecision:gate_calls.append(input.previous_head_sha)+ gate_arms.append((input.zero_change_skip, input.delta_band))if fail_gate:raise ApplicationError("compare exploded", non_retryable=True)return PushGateDecision(- skip=gate_skips, would_skip=gate_skips, reason="merge_only" if gate_skips else "system_one_above_threshold"+ skip=gate_skips,+ would_skip=gate_skips,+ reason="merge_only" if gate_skips else "system_one_above_threshold",+ delta_eligible=gate_delta_review,+ delta_review=gate_delta_review,)@activity.defn(name="resolve_acting_user_activity")@@ -336,12 +350,14 @@ async def _run_full_review_pr_workflow(@activity.defn(name="single_agent_review_activity")async def single_agent_review(input: SandboxStageInput) -> None:single_agent_calls.append(input.review_design)+ main_delta_calls.append(input.delta_since_head_sha)if fail_main_session:raise ApplicationError("main session died", non_retryable=True)@activity.defn(name="lens_review_activity")async def lens_review(input: LensReviewInput) -> None:lens_calls.append((input.lens, input.chunk_id))+ lens_delta_calls.append(input.delta_since_head_sha)lens_timeouts.append(activity.info().schedule_to_close_timeout)if (input.lens, input.chunk_id) in fail_lens_units:raise ApplicationError("lens session died", non_retryable=True)@@ -450,6 +466,7 @@ async def _run_full_review_pr_workflow(track_failed_calls: list[tuple[int, str | None]] = []track_completed_calls: list[tuple[int, str | None]] = []track_flash_calls: list[tuple[FlashTurnStats | None, FlashSessionStats | None]] = []+ track_p1_calls: list[tuple[bool, bool, bool]] = []track_started_calls: list[tuple[int, str | None]] = []@activity.defn(name="track_review_failed_activity")@@ -463,6 +480,7 @@ async def _run_full_review_pr_workflow(_saw_mode("track", input.review_mode)track_completed_calls.append((input.run_index, input.turn_trigger_source))track_flash_calls.append((input.flash_turn, input.flash_sessions))+ track_p1_calls.append((input.zero_change_skip_arm, input.delta_band_arm, input.delta_review))_saw_design("track", input.review_design)marker_calls["track"] = input.markerreturn None@@ -578,6 +596,10 @@ async def _run_full_review_pr_workflow("track_failed": track_failed_calls,"track_completed": track_completed_calls,"track_flash": track_flash_calls,+ "track_p1": track_p1_calls,+ "gate_arms": gate_arms,+ "main_delta": main_delta_calls,+ "lens_delta": lens_delta_calls,"track_started": track_started_calls,"resolve_dispatches": list(StubResolvePRWorkflow.dispatches),"modes": mode_calls,@@ -901,6 +923,39 @@ async def test_review_pr_workflow_coordinator_replaces_the_separate_sessions_of_assert (sessions.lens_failures, sessions.coordinator) == (1, True)+ "gate_delta_review,fail_gate,expect_delta",+ [(True, False, True), (False, False, False), (True, True, False)],+)+async def test_review_pr_workflow_runs_a_delta_turn_only_when_the_gate_plans_one(+ gate_delta_review: bool, fail_gate: bool, expect_delta: bool+) -> None:+ # Every session of a delta turn must see the same baseline, with one session per lens over the whole change.+ # A gate that failed must leave the whole PR to review.+ recorded = await _run_full_review_pr_workflow(+ publish=True,+ review_mode="flash",+ review_design="single_agent",+ lens_chunk_count=3,+ trigger_source="automatic",+ run_index=3,+ review_authored_prs=True,+ automatic_reviewed_head_sha="sha0",+ zero_change_skip=True,+ delta_band=True,+ gate_delta_review=gate_delta_review,+ fail_gate=fail_gate,+ )++ baseline = "sha0" if expect_delta else None+ assert recorded["gate_arms"] == [(True, True)]+ assert recorded["main_delta"] == [baseline]+ assert set(recorded["lens_delta"]) == {baseline}+ assert len(recorded["lens"]) == (2 if expect_delta else 6)+ assert recorded["track_p1"] == [(True, True, expect_delta)]++@pytest.mark.asyncio@pytest.mark.parametrize("fail_lens_units,fail_main_session,expect_failed",