Skip to content
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

feat: add line length limit to shrink_text function and settings #715

Merged
merged 1 commit into from
Mar 20, 2025
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
1 change: 1 addition & 0 deletions rdagent/core/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class RDAgentSettings(ExtendedBaseSettings):
# misc
"""The limitation of context stdout"""
stdout_context_len: int = 400
stdout_line_len: int = 10000


RD_AGENT_SETTINGS = RDAgentSettings()
1 change: 1 addition & 0 deletions rdagent/core/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ def execute_ret_code(self, env: Env, entry: str) -> tuple[str, int]:
shrink_text(
filter_redundant_text(stdout),
context_lines=RD_AGENT_SETTINGS.stdout_context_len,
line_len=RD_AGENT_SETTINGS.stdout_line_len,
),
return_code,
)
Expand Down
24 changes: 19 additions & 5 deletions rdagent/utils/fmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,33 @@
"""


def shrink_text(text: str, context_lines: int = 200) -> str:
def shrink_text(text: str, context_lines: int = 200, line_len: int = 5000) -> str:
"""
When the context is too long, hide the part in the middle.

text before
... (XXXXX lines are hidden) ...
text after
>>> shrink_text("line1\\nline2\\nline3", context_lines=2, line_len=5)
'line1\\n... (1 lines are hidden) ...\\nline3'

>>> shrink_text("short line", context_lines=2, line_len=5)
'sh... (5 chars are hidden) ...ine'

>>> shrink_text("a" * 5010, context_lines=2, line_len=10)
'aaaaa... (5000 chars are hidden) ...aaaaa'
"""

lines = text.splitlines()
total_lines = len(lines)

new_lines = []
for line in lines:
if len(line) > line_len:
# If any line is longer than line_len, we can't shrink it
line = f"{line[:line_len // 2]}... ({len(line) - line_len} chars are hidden) ...{line[- line_len + line_len // 2:]}"
new_lines.append(line)
lines = new_lines

if total_lines <= context_lines:
return text
return "\n".join(lines)

# Calculate how many lines to show from start and end
half_lines = context_lines // 2
Expand Down