Conversation
…Research#9439) build_tool_preview used preview[:max_len - 3] + "..." unconditionally, so max_len of 1, 2, or 3 produced a negative slice and returned nearly the entire source string plus "...", violating the documented cap. Special-case max_len <= 3 to emit "." * max_len instead, and extend test_display with regressions for max_len in {1, 2, 3}, the exactly-4 boundary, and the no-truncation path.
teknium1
left a comment
There was a problem hiding this comment.
Thanks for the focused regression fix. The issue remains on current main, but the implementation needs to be retargeted after later display refactoring.
Problems
agent/display.py:476-482now sendsterminalandexecute_codepreviews through_truncate_preview, which already handles limits<= 3. Therefore the submitted terminal tests would pass while the remaining generic branch atagent/display.py:545-546still performs the unsafepreview[:max_len - 3] + "..."truncation.
Suggested changes
- Salvage the change at
agent/display.py:545-546by calling_truncate_preview(preview, max_len). - Add the tiny-limit regression against a generic path such as
web_search, which exercises that remaining branch.
Automated hermes-sweeper review.
| if max_len > 0 and len(preview) > max_len: | ||
| preview = preview[:max_len - 3] + "..." | ||
| if max_len <= 3: | ||
| preview = "." * max_len |
There was a problem hiding this comment.
Current main now routes terminal through _truncate_preview, so this branch no longer covers the PR's terminal repro. Please apply the shared helper to the current generic fallback branch (agent/display.py:545-546) and add a generic-tool regression such as web_search.
|
Salvaged onto current |
`build_tool_preview()`'s generic-key fallback and the cute-message helpers still truncated with a bare `text[:max_len - 3] + "..."`; for max_len 1-3 the slice goes negative and returns almost the whole string (27 chars for max_len=1). `_truncate_preview` already had the guard, so the two code paths disagreed. One truncation helper (`_tail_trunc`) with the guard, used everywhere; the head-truncating `_cute_path` gets the same clamp. Salvage of PR #48483 by @HeLLGURD (current-code fix); the earliest reports and patches were #9464 (@LarHope), #9477 (@kagura-agent) and #9497. Co-authored-by: LarHope <12761142+LarHope@users.noreply.github.com> Fixes #9439
Closes #9439
What does this PR do?
agent.display.build_tool_preview()unconditionally truncated withpreview[:max_len - 3] + \"...\". Whenmax_lenis1,2, or3,max_len - 3is≤ 0, so the slice returns nearly the entire source string and the result ends up far longer than the caller asked for — violating the documented cap.This PR special-cases
max_len <= 3to emit\".\" * max_len, so the return value is always bounded bymax_len.Related Issue
Fixes #9439
Type of Change
Changes Made
agent/display.py— addmax_len <= 3branch inbuild_tool_preview()(+4/-1 lines).tests/agent/test_display.py— add three regression tests (max_len∈ {1, 2, 3}, exactly-4 boundary, no-truncation path) and tighten the existingtest_long_value_truncatedassertion from<= 43to<= 40so the contract is actually enforced.Total diff: 2 files, +27/-3.
How to Test
Reproduction from the issue:
```python
from agent.display import build_tool_preview
for ml in (1, 2, 3, 4, 10):
r = build_tool_preview('terminal', {'command': 'abcdefghijklmnopqrstuvwxyz'}, max_len=ml)
print(f'max_len={ml}: {r!r} len={len(r)}')
```
Before this PR:
```
max_len=1: 'abcdefghijklmnopqrstuvwx...' len=27
max_len=2: 'abcdefghijklmnopqrstuvwxy...' len=28
max_len=3: 'abcdefghijklmnopqrstuvwxyz...' len=29
```
After:
```
max_len=1: '.' len=1
max_len=2: '..' len=2
max_len=3: '...' len=3
max_len=4: 'a...' len=4
max_len=10: 'abcdefg...' len=10
```
Run the full display test module:
```bash
pytest tests/agent/test_display.py -v
```
All 27 tests pass (24 existing + 3 new regressions).
Checklist
Code
build_tool_previewDocumentation & Housekeeping