Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(review-hog): log a diff-only shadow measure on flash follow-ups (#118753)

Julian Bez520bc8d70e92
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 753d6223443b..91c4d53b1695 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
@@ -660,3 +660,43 @@ def test_turn_stats_count_every_candidate_once_per_session() -> None:
assert stats.must_fix == {"main": 0, "performance_reliability": 0, "contracts_security": 1}
assert (stats.after_dedup, stats.kept, stats.dropped) == (2, 1, {"dedup_anchor": 1, "cap": 1})
assert sum(stats.candidates.values()) == stats.kept + sum(stats.dropped.values())
+
+
+def test_turn_stats_split_posted_must_fix_by_whether_the_push_changed_its_code() -> None:
+ # The diff-only shadow reads these counts: a must-fix off the change is one a review of the change alone
+ # would put at risk. Only posted must-fix count. The change size uses the units of the PR size, additions
+ # plus deletions: a deleted block counts in full, context lines and lock files do not count, and a file
+ # without a patch counts the change in its line totals.
+ unpatched = PRFile(filename="big.py", status="modified", additions=10, deletions=0, changes=[])
+ earlier = [_patched("a.py", ("addition", 10, ["x = 1"])), _patched("uv.lock", ("addition", 1, ["a"])), unpatched]
+ current = [
+ _patched("a.py", ("addition", 10, ["x = 1", "y = 2"])),
+ _patched("b.py", ("context", 4, ["def f():"]), ("deletion", 5, ["a", "b", "c", "d", "e"])),
+ _patched("uv.lock", ("addition", 1, ["a", "b", "c"])),
+ unpatched.model_copy(update={"additions": 30}),
+ ]
+ on_change = _issue("2000-1-1", IssuePriority.MUST_FIX).model_copy(update={"lines": [LineRange(start=11)]})
+ off_change = _issue("2000-1-2", IssuePriority.MUST_FIX).model_copy(update={"lines": [LineRange(start=40)]})
+ should_fix = _issue("2000-1-3", IssuePriority.SHOULD_FIX).model_copy(update={"lines": [LineRange(start=40)]})
+ cut = _issue("2000-1-4", IssuePriority.MUST_FIX).model_copy(update={"lines": [LineRange(start=60)]})
+ selection = FlashSelection(
+ kept=[on_change, off_change, should_fix],
+ dropped=[DroppedIssue(issue=cut, disposition="cap", rank=4)],
+ cap=4,
+ lens_part_count=1,
+ )
+
+ stats = flash_turn_stats(
+ [on_change, off_change, should_fix, cut],
+ selection,
+ reviewable_lines=120,
+ changed_since=ChangedSinceReview.between(earlier, current),
+ )
+ first_review = flash_turn_stats([on_change], selection, reviewable_lines=120)
+
+ assert (stats.changed_lines, stats.must_fix_on_change, stats.must_fix_off_change) == (26, 1, 1)
+ assert (first_review.changed_lines, first_review.must_fix_on_change, first_review.must_fix_off_change) == (
+ None,
+ None,
+ None,
+ )
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 c3910ba82d44..f892c5c0dc10 100644
--- a/products/review_hog/backend/reviewer/tools/single_agent_review.py
+++ b/products/review_hog/backend/reviewer/tools/single_agent_review.py
@@ -345,13 +345,26 @@ class FlashTurnStats:
# A dedup call failed and fell back to the positional pre-filter alone.
dedup_fell_back: bool
budget_left: int | None = None
+ # A shadow measure for diff-only follow-ups, None without an earlier reviewed head: the lines that
+ # changed since that head, and the kept must-fix findings on and off them. A review that sees only the
+ # change would put the findings off it at risk.
+ changed_lines: int | None = None
+ must_fix_on_change: int | None = None
+ must_fix_off_change: int | None = None
-def flash_turn_stats(candidates: list[Issue], selection: FlashSelection, *, reviewable_lines: int) -> FlashTurnStats:
+def flash_turn_stats(
+ candidates: list[Issue],
+ selection: FlashSelection,
+ *,
+ reviewable_lines: int,
+ changed_since: "ChangedSinceReview | None" = None,
+) -> FlashTurnStats:
"""Count a turn's findings per session and per drop reason.
Must-fix counts read `reported_priority`, because a dedup survivor can be raised above what its
- session reported.
+ session reported. The on and off change split reads the stored priority instead, because it counts
+ what the turn posts.
"""
per_session = dict.fromkeys(_SESSION_NAMES.values(), 0)
must_fix = dict(per_session)
@@ -361,6 +374,14 @@ def flash_turn_stats(candidates: list[Issue], selection: FlashSelection, *, revi
if issue.reported_priority in ("P0", "P1"):
must_fix[session] = must_fix.get(session, 0) + 1
cut = sum(1 for drop in selection.dropped if drop.disposition in ("cap", "budget"))
+ changed_lines: int | None = None
+ must_fix_on_change: int | None = None
+ must_fix_off_change: int | None = None
+ if changed_since is not None:
+ kept_must_fix = [issue for issue in selection.kept if issue.priority == IssuePriority.MUST_FIX]
+ changed_lines = changed_since.reviewable_line_count()
+ must_fix_on_change = sum(1 for issue in kept_must_fix if changed_since.touches(issue))
+ must_fix_off_change = len(kept_must_fix) - must_fix_on_change
return FlashTurnStats(
cap=selection.cap,
lens_part_count=selection.lens_part_count,
@@ -372,6 +393,9 @@ def flash_turn_stats(candidates: list[Issue], selection: FlashSelection, *, revi
kept=len(selection.kept),
dedup_fell_back=selection.dedup_fell_back,
budget_left=selection.budget_left,
+ changed_lines=changed_lines,
+ must_fix_on_change=must_fix_on_change,
+ must_fix_off_change=must_fix_off_change,
)
@@ -546,11 +570,13 @@ def _diff_sequence(pr_file: PRFile) -> list[_DiffLine]:
return sequence
-def _lines_new_since(pr_file: PRFile, earlier: PRFile | None) -> set[int]:
- """The head lines of the file's changes that the earlier diff did not have.
+def _diff_since(pr_file: PRFile, earlier: PRFile | None) -> tuple[set[int], int]:
+ """The head lines of the file's changes that the earlier diff did not have, and the changed diff line count.
The two diffs are matched as sequences of lines with their context, so code that a base merge or a rebase
- only moved stays old, and a repeated line matches the occurrence in the same place.
+ only moved stays old, and a repeated line matches the occurrence in the same place. The count takes the
+ added and removed diff lines on both sides of each mismatch, in the units of `PRFile.additions` plus
+ `PRFile.deletions`.
"""
current = _diff_sequence(pr_file)
before = _diff_sequence(earlier) if earlier is not None else []
@@ -560,15 +586,27 @@ def _lines_new_since(pr_file: PRFile, earlier: PRFile | None) -> set[int]:
None, [(line.kind, line.code) for line in before], [(line.kind, line.code) for line in current]
)
new_lines: set[int] = set()
+ changed_count = 0
for tag, before_start, before_end, current_start, current_end in matcher.get_opcodes():
if tag == "equal":
continue
+ changed_count += sum(1 for line in before[before_start:before_end] if line.kind != "context")
+ changed_count += sum(1 for line in current[current_start:current_end] if line.kind != "context")
# Context lines count too, so a statement moved into new surroundings is changed code.
new_lines.update(line.head_line for line in current[current_start:current_end])
# A change the earlier diff had and this one lost, such as a removed guard, changes the code around it.
if any(line.kind != "context" for line in before[before_start:before_end]):
new_lines.update(line.head_line for line in current[max(current_start - 1, 0) : current_start + 1])
- return new_lines
+ return new_lines, changed_count
+
+
+def _count_delta(pr_file: PRFile | None, earlier: PRFile | None) -> int:
+ """The changed line count of a file whose patch is missing, from its addition and deletion counts."""
+ additions = pr_file.additions if pr_file is not None else 0
+ deletions = pr_file.deletions if pr_file is not None else 0
+ earlier_additions = earlier.additions if earlier is not None else 0
+ earlier_deletions = earlier.deletions if earlier is not None else 0
+ return abs(additions - earlier_additions) + abs(deletions - earlier_deletions)
@frozen
@@ -577,19 +615,31 @@ class ChangedSinceReview:
lines: dict[str, set[int]]
files_without_patch: set[str]
+ # Per file, the diff lines added or removed since the earlier head, including files that left the PR.
+ changed_line_counts: dict[str, int]
@classmethod
def between(cls, earlier_files: list[PRFile], current_files: list[PRFile]) -> "ChangedSinceReview":
earlier_by_name = {pr_file.filename: pr_file for pr_file in earlier_files}
lines: dict[str, set[int]] = {}
files_without_patch: set[str] = set()
+ changed_line_counts: dict[str, int] = {}
for pr_file in current_files:
earlier = earlier_by_name.get(pr_file.filename)
if _patch_missing(pr_file) or (earlier is not None and _patch_missing(earlier)):
files_without_patch.add(pr_file.filename)
+ changed_line_counts[pr_file.filename] = _count_delta(pr_file, earlier)
else:
- lines[pr_file.filename] = _lines_new_since(pr_file, earlier)
- return cls(lines=lines, files_without_patch=files_without_patch)
+ lines[pr_file.filename], changed_line_counts[pr_file.filename] = _diff_since(pr_file, earlier)
+ current_names = {pr_file.filename for pr_file in current_files}
+ for earlier in earlier_files:
+ if earlier.filename not in current_names:
+ changed_line_counts[earlier.filename] = _count_delta(None, earlier)
+ return cls(lines=lines, files_without_patch=files_without_patch, changed_line_counts=changed_line_counts)
+
+ def reviewable_line_count(self) -> int:
+ """How many diff lines of reviewable files changed, in the units of `flash_reviewable_lines`."""
+ return sum(count for filename, count in self.changed_line_counts.items() if is_reviewable_path(filename))
def touches(self, issue: Issue) -> bool:
"""Whether the finding sits on or near a changed line, or in a file whose lines cannot be compared."""
diff --git a/products/review_hog/backend/temporal/activities.py b/products/review_hog/backend/temporal/activities.py
index 98c7e0fdf21e..a8170c54f4c0 100644
--- a/products/review_hog/backend/temporal/activities.py
+++ b/products/review_hog/backend/temporal/activities.py
@@ -1816,7 +1816,9 @@ async def dedup_activity(input: SandboxStageInput) -> DedupResult:
first_review=input.run_index == 1,
)
survivors = flash_selection.kept
- flash_stats = flash_turn_stats(issues, flash_selection, reviewable_lines=lens_plan.reviewable_lines)
+ flash_stats = flash_turn_stats(
+ issues, flash_selection, reviewable_lines=lens_plan.reviewable_lines, changed_since=changed_since
+ )
else:
# Flash posts what it finds whatever other comments say; only Full checks the PR's comments.
pr_comments = (
@@ -2225,6 +2227,9 @@ def _flash_event_properties(turn: FlashTurnStats | None, sessions: FlashSessionS
"flash_kept": turn.kept,
"flash_dedup_fallback": turn.dedup_fell_back,
"flash_budget_left": turn.budget_left,
+ "flash_changed_lines": turn.changed_lines,
+ "flash_must_fix_on_change": turn.must_fix_on_change,
+ "flash_must_fix_off_change": turn.must_fix_off_change,
}
)
if sessions is not None:
diff --git a/products/review_hog/backend/tests/test_review_completed_tracking.py b/products/review_hog/backend/tests/test_review_completed_tracking.py
index 8cf8c1c6fdcd..d48cb013dbea 100644
--- a/products/review_hog/backend/tests/test_review_completed_tracking.py
+++ b/products/review_hog/backend/tests/test_review_completed_tracking.py
@@ -244,6 +244,9 @@ class TestTrackReviewCompleted(BaseTest):
kept=4,
dedup_fell_back=True,
budget_left=3,
+ changed_lines=40,
+ must_fix_on_change=1,
+ must_fix_off_change=2,
)
sessions = FlashSessionStats(lens_failures=1, lens_timeouts=1, slowest_session_seconds=412.5)
@@ -264,6 +267,9 @@ class TestTrackReviewCompleted(BaseTest):
"flash_kept": 4,
"flash_dedup_fallback": True,
"flash_budget_left": 3,
+ "flash_changed_lines": 40,
+ "flash_must_fix_on_change": 1,
+ "flash_must_fix_off_change": 2,
"flash_lens_failures": 1,
"flash_lens_timeouts": 1,
"flash_slowest_session_seconds": 412.5,