feat(input): add diff_compress module + prior-review compaction helpers

Two pure stdlib helpers that shrink what lands in the model prompt:

  * compress_diff(diff, *, context=2) — re-renders a unified diff so each
    hunk keeps only  unchanged lines on either side of its +/- lines.
    File headers + hunk headers + +/- lines preserved verbatim. Pure-context
    hunks dropped (rare but legal — git emits them on whitespace-only diffs).
    Collapsed gaps of >=5 lines emit a single '@@ … N context line(s) omitted
    … @@' marker so the reviewer knows code was elided. Smaller gaps stay
    silent — the marker would be longer than the elision.

  * extract_finding_bullets(review_body) — pulls the lines of a prior review
    that look like a pragent finding (- **[SEVERITY]** path:line — …) and
    drops everything else. The model already has the diff; repeating the
    prose is just token burn.

No I/O, no network. Tolerant of malformed input — never raises. 14 unit
tests cover both helpers, including an anchor-preservation check against
parse_diff_anchors to guarantee compress-then-anchor still works.

Wiring in ai_review/opencode_review lives in the next commit.
This commit is contained in:
Marcos
2026-08-20 16:12:33 +00:00
parent f59b906395
commit 770581bf53
2 changed files with 417 additions and 0 deletions
+248
View File
@@ -0,0 +1,248 @@
"""Unit tests for pragent pilot diff_compress. No network."""
import os
import sys
HERE = os.path.dirname(os.path.abspath(__file__))
ROOT = os.path.abspath(os.path.join(HERE, "..", ".."))
sys.path.insert(0, os.path.join(ROOT, "pilot"))
import diff_compress # noqa: E402
from diff_compress import compress_diff, extract_finding_bullets # noqa: E402
# ---------------------------------------------------------------------------
# compress_diff
# ---------------------------------------------------------------------------
_DIFF = """\
diff --git a/src/a.py b/src/a.py
index 1..2 100644
--- a/src/a.py
+++ b/src/a.py
@@ -1,10 +1,11 @@
ctx1
-removed
+added
ctx2
ctx3
ctx4
ctx5
ctx6
ctx7
+extra
ctx8
@@ -20,3 +21,4 @@
tail1
tail2
+tail3
tail4
diff --git a/binary.bin b/binary.bin
new file mode 100644
index 0..1
Binary files differ
"""
def test_compress_diff_default_context_two():
text, orig, kept = compress_diff(_DIFF, context=2)
# +/- lines preserved
assert "+added" in text
assert "-removed" in text
assert "+extra" in text
assert "+tail3" in text
# 2 context lines around +/- kept, the rest collapsed
assert "ctx2" in text and "ctx3" in text
assert "ctx4" not in text # outside the +/- window
# Binary files pass through
assert "Binary files differ" in text
# File headers preserved
assert "diff --git a/src/a.py b/src/a.py" in text
assert orig > kept
def test_compress_diff_context_zero_strips_context():
text, orig, kept = compress_diff(_DIFF, context=0)
assert "+added" in text and "-removed" in text and "+extra" in text
# Context lines dropped (only +/- survive)
assert " ctx1" not in text
assert "ctx2" not in text
assert orig > kept
def test_compress_diff_negative_disables_compression():
text, orig, kept = compress_diff(_DIFF, context=-1)
assert text == _DIFF
assert orig == kept
def test_compress_diff_collapsed_gap_marker():
# Two +/- lines separated by 14 context lines, context=2 — the gap between
# them is 10 dropped lines (between the +/- windows), which exceeds the
# 5-line marker threshold. The marker tells the reviewer there's more code
# between the kept hunks.
middle = "\n".join(f" m{i}" for i in range(14)) + "\n" # trailing \n!
diff = (
"diff --git a/x.py b/x.py\n"
"--- a/x.py\n"
"+++ b/x.py\n"
"@@ -1,21 +1,23 @@\n"
+ " c1\n c2\n" # ctx near +a (kept with context=2)
+ "+a\n"
+ middle
+ "+b\n"
+ " c1\n c2\n" # ctx near +b (kept with context=2)
)
text, _, _ = compress_diff(diff, context=2)
assert "+a" in text and "+b" in text
assert "@@ …" in text and "context line(s) omitted" in text
def test_compress_diff_strips_no_newline_marker():
diff = (
"diff --git a/x.py b/x.py\n"
"--- a/x.py\n"
"+++ b/x.py\n"
"@@ -1,2 +1,2 @@\n"
" a\n"
"-b\n"
"\\ No newline at end of file\n"
"+c\n"
"\\ No newline at end of file\n"
)
text, _, _ = compress_diff(diff, context=2)
assert "\\ No newline" not in text
assert "-b" in text and "+c" in text
def test_compress_diff_empty_and_none():
text, orig, kept = compress_diff("", context=2)
assert text == ""
assert orig == 0 and kept == 0
text, orig, kept = compress_diff(None, context=2) # type: ignore[arg-context]
assert text == ""
assert orig == 0 and kept == 0
def test_compress_diff_pure_context_hunk_drops_body():
# A hunk that's *only* context lines (rare but legal — `git diff` emits
# these when the post-image differs only in whitespace outside the visible
# hunk) collapses entirely: file headers stay, the empty hunk header
# itself drops. The reviewer doesn't need to re-read unchanged code.
diff = (
"diff --git a/x.py b/x.py\n"
"--- a/x.py\n"
"+++ b/x.py\n"
"@@ -1,3 +1,3 @@\n"
" a\n"
" b\n"
" c\n"
)
text, _, _ = compress_diff(diff, context=2)
assert text == "diff --git a/x.py b/x.py\n--- a/x.py\n+++ b/x.py\n"
assert "@@ -1,3" not in text # empty hunk header dropped
def test_compress_diff_wide_window_keeps_more_context():
narrow, _, _ = compress_diff(_DIFF, context=0)
wide, _, wide_kept = compress_diff(_DIFF, context=10)
assert wide_kept > len(narrow)
# ---------------------------------------------------------------------------
# extract_finding_bullets
# ---------------------------------------------------------------------------
_BODY = """\
🤖 **AI Review** · pragent pilot · glm-5.2:cloud · `abcdef12`
Adds the salvavoid void-death item-rescue module. Risk is moderate on the
PlayerDeathEvent item/inventory path. New findings (not in prior review):
orphaned chest left in world on rescue failure, missing module-enabled check.
- **[HIGH]** `src/main/java/dev/marcospaulo/canalhandia/VoidProtection.java:162` — drop duplication race. fix: use ItemMeta to write inventory once. (ref: https://example.com)
- **[MEDIUM]** `src/main/java/dev/marcospaulo/canalhandia/VoidProtection.java:67` — O(n^2) spiral. fix: cap radius. (https://example.com/spiral)
- **[LOW]** `src/main/java/dev/marcospaulo/canalhandia/VoidProtection.java:3` — package-info javadoc missing.
_4 inline comment(s) posted below._
<!-- pragent:sha=abcdef1234567890 -->
"""
def test_extract_finding_bullets_basic():
bs = extract_finding_bullets(_BODY)
assert len(bs) == 3
assert any("HIGH" in b and "VoidProtection.java:162" in b for b in bs)
assert any("MEDIUM" in b for b in bs)
assert any("LOW" in b for b in bs)
def test_extract_finding_bullets_drops_prose():
bs = extract_finding_bullets(_BODY)
joined = "\n".join(bs)
# The summary prose is dropped.
assert "Adds the salvavoid" not in joined
assert "PlayerDeathEvent item/inventory path" not in joined
# The inline-comment footer is dropped.
assert "inline comment(s) posted below" not in joined
# The sha marker is dropped.
assert "pragent:sha=" not in joined
def test_extract_finding_bullets_accepts_lowercase_summary_bullets():
# `summary_bullets` renders `- **[HIGH]**` (bold); older reviews used
# `- [high]` (plain). Both should match.
text = (
"- [critical] `a.py:1` — bug. fix: fix it.\n"
"- **[HIGH]** `b.go:9` — race.\n"
)
bs = extract_finding_bullets(text)
assert len(bs) == 2
assert "CRITICAL" in bs[0].upper() or "critical" in bs[0]
assert "HIGH" in bs[1]
def test_extract_finding_bullets_empty_and_prose_only():
assert extract_finding_bullets("") == []
assert extract_finding_bullets(" \n \n") == []
assert extract_finding_bullets("Just some prose, no bullets here.") == []
assert extract_finding_bullets("- This is a regular bullet, not a finding.") == []
def test_extract_finding_bullets_keeps_indented_subbullets():
# A finding may carry continuation lines below it (rare in pragent output
# but legal). We only pull the matching line itself — sub-bullets stay
# with their parent as prose.
text = (
"- **[HIGH]** `a.py:1` — bug.\n"
" sub-bullet continuation that the reviewer wrote\n"
"- **[LOW]** `b.go:2` — nit.\n"
)
bs = extract_finding_bullets(text)
assert len(bs) == 2
assert all("sub-bullet continuation" not in b for b in bs)
def test_compress_diff_preserves_anchors_for_post_change_lines():
# Sanity: a finding anchored on a context line that compress_diff keeps
# must still be a valid anchor after compression. We re-run the parser the
# ai_review core uses, so a regression here surfaces as misanchored
# inline comments in production.
import ai_review
diff = (
"diff --git a/x.py b/x.py\n"
"--- a/x.py\n"
"+++ b/x.py\n"
"@@ -10,4 +10,5 @@\n"
" ctx_a\n"
" ctx_b\n"
"+new\n"
" ctx_c\n"
" ctx_d\n"
)
text, _, _ = compress_diff(diff, context=1)
anchors = ai_review.parse_diff_anchors(text)
assert 12 in anchors["x.py"] # +new
# ctx_a is within 1 line of +new at line 12, so kept.
assert 11 in anchors["x.py"]