-
Notifications
You must be signed in to change notification settings - Fork 4.7k
feat(context): bound per-tool output before it enters agent history #880
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3b8980c
feat(context): bound per-tool output before it enters agent history
0xallam dab93bc
fix(context): clamp shell output cap and count byte-trimmed dropped l…
0xallam ce358aa
fix(context): bound native filesystem tool output in Responses mode
0xallam aac59de
fix(context): reserve notice budget so bounded output honors max_bytes
0xallam 95046a6
fix(context): reject tool-output byte ceilings below the notice size
0xallam 10376b4
refactor(context): trim verbose comments
0xallam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| """Bound oversized tool results before they enter agent history. | ||
|
|
||
| Keeps a head + tail slice and drops the middle, replacing it with a notice of | ||
| how much was removed. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
|
|
||
| _TRUNCATION_NOTICE = "[... {lines} lines ({bytes} bytes) truncated ...]" | ||
|
|
||
|
|
||
| def _byte_len(text: str) -> int: | ||
| return len(text.encode("utf-8")) | ||
|
|
||
|
|
||
| def _take_prefix(text: str, max_bytes: int) -> str: | ||
| budget = 0 | ||
| out: list[str] = [] | ||
| for char in text: | ||
| size = len(char.encode("utf-8")) | ||
| if budget + size > max_bytes: | ||
| break | ||
| out.append(char) | ||
| budget += size | ||
| return "".join(out) | ||
|
|
||
|
|
||
| def _take_suffix(text: str, max_bytes: int) -> str: | ||
| budget = 0 | ||
| out: list[str] = [] | ||
| for char in reversed(text): | ||
| size = len(char.encode("utf-8")) | ||
| if budget + size > max_bytes: | ||
| break | ||
| out.append(char) | ||
| budget += size | ||
| out.reverse() | ||
| return "".join(out) | ||
|
|
||
|
|
||
| def bound_text(text: str, *, max_lines: int, max_bytes: int) -> str: | ||
| """Return ``text`` unchanged when small, else a head+tail preview. | ||
|
|
||
| Truncates on whichever limit is hit first (line count or UTF-8 byte size). | ||
| ``max_bytes`` bounds the entire joined result, notice and separators | ||
| included. | ||
| """ | ||
| lines = text.split("\n") | ||
| total_bytes = _byte_len(text) | ||
| if len(lines) <= max_lines and total_bytes <= max_bytes: | ||
| return text | ||
|
|
||
| # Reserve notice + separator bytes up front; ``+ 4`` covers the two "\n\n". | ||
| notice_overhead = _byte_len(_TRUNCATION_NOTICE.format(lines=len(lines), bytes=total_bytes)) + 4 | ||
| byte_budget = max(2, max_bytes - notice_overhead) | ||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
|
|
||
| head_lines = max(1, max_lines // 2) | ||
| tail_lines = max_lines - head_lines | ||
| head = "\n".join(lines[:head_lines]) | ||
| tail = "\n".join(lines[len(lines) - tail_lines :]) if tail_lines > 0 else "" | ||
|
|
||
| half_bytes = max(1, byte_budget // 2) | ||
| if _byte_len(head) > half_bytes: | ||
| head = _take_prefix(head, half_bytes) | ||
| if tail and _byte_len(tail) > half_bytes: | ||
| tail = _take_suffix(tail, half_bytes) | ||
|
|
||
| # Count from the final slices; the byte pass may have dropped whole lines. | ||
| kept_lines = len(head.split("\n")) + (len(tail.split("\n")) if tail else 0) | ||
| dropped_lines = max(0, len(lines) - kept_lines) | ||
| dropped_bytes = max(0, total_bytes - _byte_len(head) - _byte_len(tail)) | ||
| notice = _TRUNCATION_NOTICE.format(lines=dropped_lines, bytes=dropped_bytes) | ||
| return f"{head}\n\n{notice}\n\n{tail}" if tail else f"{head}\n\n{notice}" | ||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.