fix(string): strip newlines in shorten_middle before the length check#2449
Open
Ricardo-M-L wants to merge 1 commit into
Open
fix(string): strip newlines in shorten_middle before the length check#2449Ricardo-M-L wants to merge 1 commit into
Ricardo-M-L wants to merge 1 commit into
Conversation
shorten_middle(remove_newline=True) is used by extract_key_argument to build a single-line summary of a tool call's key argument. It returned early when len(text) <= width *before* collapsing newlines, so any argument shorter than the width that contained a newline (e.g. a multi-line Bash command or Grep pattern) was returned with its newlines intact, breaking the single-line tool status display. Collapse newlines first, then do the length check and middle-truncation. This also makes the length check operate on the post-flattening length. Behavior with remove_newline=False is unchanged. shorten_middle was previously untested; add cases for the short-text passthrough, newline stripping in short text, remove_newline=False, and middle-ellipsis on long text. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
shorten_middle(text, width, remove_newline=True)is used byextract_key_argumentto render a single-line summary of a tool call's key argument:But the function returns early on short input before collapsing newlines:
So any key argument shorter than
widththat contains a newline — a multi-line Bash command, a Grep pattern, a multi-line SearchWeb query — is returned with its newlines intact, and the tool status line wraps across multiple lines instead of staying on one.Reproduction (current behavior on
main):Fix
Collapse newlines first, then do the length check and middle-truncation:
The length check now also operates on the post-flattening length, which is what actually gets displayed. Behavior with
remove_newline=Falseis unchanged.Tests
shorten_middlehad no tests; added coverage totests/utils/test_shorten.py:remove_newline=Falsekeeps newlines,Verification