diff --git a/read_tools.py b/read_tools.py index 68c9829..9b1c60f 100644 --- a/read_tools.py +++ b/read_tools.py @@ -200,6 +200,29 @@ def _one_line(text: str, cap: int) -> str: return t if len(t) <= cap else t[: cap - 1] + "…" +# A review bot (CodeRabbit) opens a thread's first comment with HTML-comment markers and +# collapsible
…
analysis chains that routinely overrun the per-body cap +# and bury the actual finding (which the bot writes AFTER them). Strip both before capping. +_HTML_COMMENT_RE = re.compile(r"", re.DOTALL) +_DETAILS_RE = re.compile(r"]*>.*?", re.DOTALL | re.IGNORECASE) +_SUMMARY_RE = re.compile(r"]*>(.*?)", re.DOTALL | re.IGNORECASE) +_HTML_TAG_RE = re.compile(r"<[^>]+>") + + +def _strip_review_boilerplate(raw: str) -> str: + """Drop HTML comments and
…
blocks from a review comment body, then + collapse whitespace, so the finding survives the cap instead of the boilerplate. If + stripping leaves nothing, fall back to the first line (the collapsed block's + headline) so the thread still shows something rather than going blank.""" + no_comments = _HTML_COMMENT_RE.sub(" ", raw or "") + body = " ".join(_DETAILS_RE.sub(" ", no_comments).split()) + if not body and (m := _SUMMARY_RE.search(no_comments)): + summary = _HTML_TAG_RE.sub(" ", m.group(1)) + first = next((ln for ln in summary.splitlines() if ln.strip()), "") + body = " ".join(first.split()) + return body + + def _bounded(text: str, cap: int) -> str: return text if len(text) <= cap else text[:cap] + f"\n… (truncated at {cap} chars)" @@ -719,8 +742,10 @@ async def github_issue_comments(number: int, repo: str = "", limit: int = 30) -> return _bounded("\n".join(lines), _MAX_PR_CHARS) @tool - async def github_review_threads(number: int, repo: str = "", include_resolved: bool = False) -> str: - """List a PR's inline review THREADS (resolvable line comments, e.g. CodeRabbit's) with the thread ids github_reply_thread / github_resolve_thread take; unresolved only unless include_resolved. Unresolved threads keep the PR's QA check from clearing, so after a fix round read, answer and resolve each.""" + async def github_review_threads( + number: int, repo: str = "", include_resolved: bool = False, body_max_chars: int = 1000 + ) -> str: + """List a PR's inline review THREADS (resolvable line comments, e.g. CodeRabbit's) with the thread ids github_reply_thread / github_resolve_thread take; unresolved only unless include_resolved. Each thread's first comment is stripped of HTML-comment /
boilerplate then capped at body_max_chars (clamped 200..8000, default 1000). Unresolved threads keep the PR's QA check from clearing, so after a fix round read, answer and resolve each.""" repo = resolve_repo(repo, default_repo) or "" if err := bad_repo(repo): return err @@ -757,12 +782,16 @@ async def github_review_threads(number: int, repo: str = "", include_resolved: b if not shown: return f"No {'' if include_resolved else 'unresolved '}review threads on {repo}#{number}." open_n = sum(1 for t in threads if not t.get("isResolved")) + try: + body_cap = max(200, min(int(body_max_chars), 8000)) + except (TypeError, ValueError): + body_cap = _MAX_COMMENT_CHARS lines = [f"{open_n} unresolved of {len(threads)} review thread(s) on {repo}#{number}:"] for t in shown: first = (dicts((t.get("comments") or {}).get("nodes")) or [{}])[0] - body = " ".join(str(first.get("body") or "").split()) - if len(body) > _MAX_COMMENT_CHARS: - body = body[: _MAX_COMMENT_CHARS - 1] + "…" + body = _strip_review_boilerplate(str(first.get("body") or "")) + if len(body) > body_cap: + body = body[: body_cap - 1] + "…" where = f"{t.get('path') or '?'}:{t.get('line') or '?'}" state = "resolved" if t.get("isResolved") else ("outdated" if t.get("isOutdated") else "open") replies = int(((t.get("comments") or {}).get("totalCount") or 1)) - 1 diff --git a/tests/test_read_tools.py b/tests/test_read_tools.py index 136d35f..b1a863a 100644 --- a/tests/test_read_tools.py +++ b/tests/test_read_tools.py @@ -1336,3 +1336,88 @@ async def test_run_failure_retains_line_length_and_upper_budget_limits(): lines = [f"error {i}: " + "x" * 250 for i in range(100)] out = await _failure("\n".join(lines), max_lines=100) assert out.splitlines()[1:] == [line[:200] for line in lines[-80:]] + + +# ── github_review_threads: strip CodeRabbit boilerplate before the per-body cap ── + + +def _threads_payload(body): + return json.dumps( + { + "data": { + "repository": { + "pullRequest": { + "reviewThreads": { + "nodes": [ + { + "id": "PRRT_x", + "isResolved": False, + "isOutdated": False, + "path": "a.py", + "line": 10, + "comments": { + "totalCount": 1, + "nodes": [{"author": {"login": "coderabbitai"}, "body": body, "url": "u"}], + }, + } + ] + } + } + } + } + } + ) + + +async def _review_threads(body, **kwargs): + gh = AsyncMock(return_value=(0, _threads_payload(body), "")) + with patch("ghplugin.read_tools.run_gh", gh): + return await _tool("github_review_threads").ainvoke({"repo": "o/r", "number": 1, **kwargs}) + + +async def test_review_threads_strips_details_and_html_comments_before_cap(): + # 1500+ chars of
analysis-chain boilerplate, then the real finding (r1, r2). + body = ( + "\n" + "
\n🧩 Analysis chain\n\n" + "boilerplate " * 150 + "\n
\n\n" + "**Potential bug:** the retry loop never resets `attempts`, so it spins forever." + ) + assert len(body) > 1500 + out = await _review_threads(body) + assert "the retry loop never resets `attempts`" in out + assert "boilerplate" not in out # the
chain is gone + assert "
" not in out + assert "\n
\n🧩 Analysis chain\n\n" + "noise " * 100 + "\n
" + out = await _review_threads(body) + assert out.splitlines()[-1] == "🧩 Analysis chain" # the collapsed block's headline + assert "noise" not in out + + +async def test_review_threads_body_max_chars_controls_the_cap(): + # 4000 returns up to 4000 chars for the body (3999 + the … ellipsis) (r3). + out = await _review_threads("x" * 5000, body_max_chars=4000) + body_line = out.splitlines()[-1] + assert len(body_line) == 4000 and body_line.endswith("…") + + +@pytest.mark.parametrize("requested, effective", [(10, 200), (0, 200), (-5, 200), (50000, 8000)]) +async def test_review_threads_body_max_chars_is_clamped(requested, effective): + out = await _review_threads("y" * 9000, body_max_chars=requested) + body_line = out.splitlines()[-1] + assert len(body_line) == effective and body_line.endswith("…") + + +async def test_review_threads_default_caps_plain_body_at_1000(): + # r4: default behavior on a plain body is unchanged — the 1000-char cap still holds. + out = await _review_threads("z" * 1500) + body_line = out.splitlines()[-1] + assert len(body_line) == 1000 and body_line.endswith("…") + + +async def test_review_threads_short_plain_body_is_returned_intact(): + out = await _review_threads("Restore blobs/ if the history move fails.") + assert out.splitlines()[-1] == "Restore blobs/ if the history move fails."