-
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 4 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,85 @@ | ||
| """Bound oversized tool results before they enter agent history. | ||
|
|
||
| A single verbose tool result (a recursive ``find``, a noisy scanner, a full | ||
| page dump) can otherwise pin the whole conversation near the model's context | ||
| limit for the rest of the scan. This keeps a head + tail slice of the output | ||
| and drops the middle, mirroring how the shell capability truncates its own | ||
| output — the agent still sees the start and end plus 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. | ||
|
|
||
| Truncation happens on whichever limit is hit first (line count or UTF-8 | ||
| byte size). The removed middle is replaced with a notice recording how | ||
| many lines and bytes were dropped so the agent knows output was elided. | ||
| ``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 room for the notice and its two blank-line separators so the | ||
| # head+tail slices can't consume the whole budget and push the persisted | ||
| # value over max_bytes. Upper-bound the notice with the largest possible | ||
| # counts; the real notice is never longer. ``+ 4`` covers the separators. | ||
| 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 "" | ||
|
|
||
| # Enforce the byte budget even when the line count alone was fine. | ||
| 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 kept lines from the final slices: the byte pass above may have | ||
| # dropped whole lines from head/tail, so deriving this from the original | ||
| # head_lines/tail_lines would undercount what was actually removed. | ||
| 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.