Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 37 additions & 7 deletions read_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
)


_MAX_PR_CHARS = 12000 # github_get_pr's total output bound
_MAX_PR_CHARS = 12000 # Default total output bound for PRs and issues
_MAX_BODY_CHARS = 60000
_MAX_REVIEWS = 10
_MAX_COMMENT_CHARS = 1000

Expand Down Expand Up @@ -203,6 +204,28 @@ def _bounded(text: str, cap: int) -> str:
return text if len(text) <= cap else text[:cap] + f"\n… (truncated at {cap} chars)"


def _bounded_body(header: str, body: str, body_max_chars: int) -> str:
"""Keep the body marker intact even when metadata alone exceeds the budget."""
requested = max(0, min(body_max_chars, _MAX_BODY_CHARS))
if requested:
header = _bounded(header, _MAX_PR_CHARS)
shown = min(len(body), requested)
else:
if len(header) + 2 + len(body) <= _MAX_PR_CHARS:
return header + "\n\n" + body
# Reserve the longest possible body marker before bounding metadata.
reserve = len(f"\n[truncated: body shows {len(body)} of {len(body)} chars]") if body else 0
metadata_marker = "\n… (metadata truncated)"
header_cap = _MAX_PR_CHARS - 2 - reserve
if len(header) > header_cap:
header = header[: header_cap - len(metadata_marker)] + metadata_marker
shown = min(len(body), max(0, _MAX_PR_CHARS - len(header) - 2 - reserve))
result = header + "\n\n" + body[:shown]
if shown < len(body):
result += f"\n[truncated: body shows {shown} of {len(body)} chars]"
return result


def _summarize_checks(rollup) -> str:
"""``N pass / N fail / N pending (/ N skipped) — failing: a, b`` from a PR's
statusCheckRollup; ``none`` when the commit has no checks."""
Expand Down Expand Up @@ -257,7 +280,7 @@ def _repos() -> list[str]:
return []

@tool
async def github_get_pr(number: int, repo: str = "") -> str:
async def github_get_pr(number: int, repo: str = "", body_max_chars: int = 0) -> str:
"""Fetch a GitHub pull request — the merge-readiness picture in one call: title,
state, draft?, author, branch, review decision, mergeability + merge state, a
checks summary (N pass / N fail / N pending + the failing check names), the
Expand All @@ -266,6 +289,9 @@ async def github_get_pr(number: int, repo: str = "") -> str:
Args:
repo: Repository as ``owner/name``. Omit to use the agent's configured default repo.
number: PR number.
body_max_chars: Body characters to fetch, capped at 60000. 0 (or negative)
uses the remaining 12000-character response budget. Do not round-trip-edit
a truncated body; fetch it in full with a larger limit first.
"""
repo = resolve_repo(repo, default_repo) or ""
if err := bad_repo(repo):
Expand Down Expand Up @@ -313,17 +339,20 @@ async def github_get_pr(number: int, repo: str = "") -> str:
f"mergeable: {d.get('mergeable') or '?'} | merge state: {d.get('mergeStateStatus') or '?'}\n"
f"checks: {checks}\n"
f"reviews ({len(reviews)}):" + ("\n" + "\n".join(review_lines) if review_lines else " none") + "\n"
f"files: {files or '(none)'}\n\n{(d.get('body') or '').strip()[:2000]}"
f"files: {files or '(none)'}"
)
return _bounded(text, _MAX_PR_CHARS)
return _bounded_body(text, str(d.get("body") or ""), body_max_chars)

@tool
async def github_get_issue(number: int, repo: str = "") -> str:
async def github_get_issue(number: int, repo: str = "", body_max_chars: int = 0) -> str:
"""Fetch a GitHub issue: title, state, author, labels, and body.

Args:
repo: Repository as ``owner/name``. Omit to use the agent's configured default repo.
number: Issue number.
body_max_chars: Body characters to fetch, capped at 60000. 0 (or negative)
uses the remaining 12000-character response budget. Do not round-trip-edit
a truncated body; fetch it in full with a larger limit first.
"""
repo = resolve_repo(repo, default_repo) or ""
if err := bad_repo(repo):
Expand All @@ -337,11 +366,12 @@ async def github_get_issue(number: int, repo: str = "") -> str:
if perr:
return perr
labels = ", ".join(str(lbl.get("name", "")) for lbl in dicts(d.get("labels")))
return (
text = (
f"Issue #{d.get('number')} [{d.get('state')}] {d.get('title')}\n"
f"by {_login(d.get('author'))} | labels: {labels or '(none)'} | "
f"{d.get('url')}\n\n{(d.get('body') or '').strip()[:2000]}"
f"{d.get('url')}"
)
return _bounded_body(text, str(d.get("body") or ""), body_max_chars)

@tool
async def github_list_issues(repo: str = "", state: str = "open", limit: int = 20) -> str:
Expand Down
68 changes: 63 additions & 5 deletions tests/test_read_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,9 +561,7 @@ async def test_get_pr_with_no_checks_or_reviews_says_so():

@pytest.mark.asyncio
async def test_get_pr_output_is_bounded():
"""A huge body + 40 long reviews + long file paths must end at the bound — and the
bound must actually be hit: the body is capped at 2000, reviews at 10 × 300, files
at 20 entries, so only the (uncapped) path lengths can carry this past 12k."""
"""Oversized metadata must not hide the body truncation marker."""
from ghplugin.read_tools import _MAX_PR_CHARS

d = {
Expand All @@ -575,8 +573,9 @@ async def test_get_pr_output_is_bounded():
}
with patch("ghplugin.read_tools.run_gh", new=AsyncMock(return_value=(0, json.dumps(d), ""))):
out = await _get_pr_tool().ainvoke({"repo": "owner/name", "number": 1})
assert out.endswith(f"… (truncated at {_MAX_PR_CHARS} chars)")
assert len(out) == _MAX_PR_CHARS + len(f"\n… (truncated at {_MAX_PR_CHARS} chars)")
assert out.endswith("[truncated: body shows 0 of 50000 chars]")
assert "metadata truncated" in out
assert len(out) <= _MAX_PR_CHARS
assert "… 30 more review(s)" in out


Expand Down Expand Up @@ -1198,3 +1197,62 @@ async def test_missing_path_hint_caps_the_listing():
out = await _read_file_tool().ainvoke({"repo": "o/r", "path": "nope.py"})
assert "'.' contains: f000.py" in out and "f059.py" in out and "f060.py" not in out
assert "(+15 more)" in out


@pytest.mark.parametrize("kind", ["pr", "issue"])
@pytest.mark.parametrize(
("size", "limit", "shown"),
[
(5000, 0, 5000),
(30000, 0, None),
(30000, 40000, 30000),
(70000, 100000, 60000),
(30000, -1, None),
(100, 1, 1),
(100, 100, 100),
(0, 0, 0),
],
)
async def test_get_body_limits(kind, size, limit, shown):
tool = next(t for t in get_read_tools() if t.name == f"github_get_{kind}")
body = (" λ\n" * ((size + 2) // 3))[:size]
data = {**json.loads(_PR_JSON), "body": body}
with patch("ghplugin.read_tools.run_gh", AsyncMock(return_value=(0, json.dumps(data), ""))):
result = await tool.ainvoke({"repo": "owner/name", "number": 38, "body_max_chars": limit})
rendered = result.split("\n\n", 1)[1]
if shown == size:
assert rendered == body
assert "[truncated:" not in result
else:
content, marker = rendered.rsplit("\n", 1)
assert content == body[: len(content)]
assert marker == f"[truncated: body shows {len(content)} of {size} chars]"
if shown is None:
assert 11000 < len(content) < 12000
else:
assert len(content) == shown
if limit <= 0:
assert len(result) <= 12000


@pytest.mark.parametrize("kind", ["pr", "issue"])
async def test_get_body_with_oversized_metadata(kind):
tool = next(t for t in get_read_tools() if t.name == f"github_get_{kind}")
for limit in (0, 40000):
data = {"title": "t" * 20000, "body": "b" * 30000}
with patch("ghplugin.read_tools.run_gh", AsyncMock(return_value=(0, json.dumps(data), ""))):
result = await tool.ainvoke({"repo": "owner/name", "number": 1, "body_max_chars": limit})
if limit:
assert result.split("\n\n", 1)[1] == data["body"]
else:
assert result.endswith("[truncated: body shows 0 of 30000 chars]")
assert len(result) <= 12000


@pytest.mark.parametrize("kind", ["pr", "issue"])
@pytest.mark.parametrize("body", [None, 42, [], {}])
async def test_get_body_unexpected_shape_never_raises(kind, body):
tool = next(t for t in get_read_tools() if t.name == f"github_get_{kind}")
with patch("ghplugin.read_tools.run_gh", AsyncMock(return_value=(0, json.dumps({"body": body}), ""))):
result = await tool.ainvoke({"repo": "owner/name", "number": 1})
assert isinstance(result, str)
Loading