Skip to content

Commit 75ed5e1

Browse files
authored
feat: Add line length limit to shrink_text function and settings (#715)
1 parent 1566cdd commit 75ed5e1

3 files changed

Lines changed: 21 additions & 5 deletions

File tree

rdagent/core/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class RDAgentSettings(ExtendedBaseSettings):
7878
# misc
7979
"""The limitation of context stdout"""
8080
stdout_context_len: int = 400
81+
stdout_line_len: int = 10000
8182

8283

8384
RD_AGENT_SETTINGS = RDAgentSettings()

rdagent/core/experiment.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ def execute_ret_code(self, env: Env, entry: str) -> tuple[str, int]:
266266
shrink_text(
267267
filter_redundant_text(stdout),
268268
context_lines=RD_AGENT_SETTINGS.stdout_context_len,
269+
line_len=RD_AGENT_SETTINGS.stdout_line_len,
269270
),
270271
return_code,
271272
)

rdagent/utils/fmt.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,33 @@
33
"""
44

55

6-
def shrink_text(text: str, context_lines: int = 200) -> str:
6+
def shrink_text(text: str, context_lines: int = 200, line_len: int = 5000) -> str:
77
"""
88
When the context is too long, hide the part in the middle.
99
10-
text before
11-
... (XXXXX lines are hidden) ...
12-
text after
10+
>>> shrink_text("line1\\nline2\\nline3", context_lines=2, line_len=5)
11+
'line1\\n... (1 lines are hidden) ...\\nline3'
12+
13+
>>> shrink_text("short line", context_lines=2, line_len=5)
14+
'sh... (5 chars are hidden) ...ine'
15+
16+
>>> shrink_text("a" * 5010, context_lines=2, line_len=10)
17+
'aaaaa... (5000 chars are hidden) ...aaaaa'
1318
"""
19+
1420
lines = text.splitlines()
1521
total_lines = len(lines)
1622

23+
new_lines = []
24+
for line in lines:
25+
if len(line) > line_len:
26+
# If any line is longer than line_len, we can't shrink it
27+
line = f"{line[:line_len // 2]}... ({len(line) - line_len} chars are hidden) ...{line[- line_len + line_len // 2:]}"
28+
new_lines.append(line)
29+
lines = new_lines
30+
1731
if total_lines <= context_lines:
18-
return text
32+
return "\n".join(lines)
1933

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

0 commit comments

Comments
 (0)