Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 13 additions & 12 deletions libs/code/deepagents_code/tui/widgets/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -1957,18 +1957,19 @@ def _format_ls_output( # noqa: PLR6301 # Grouped as method for widget cohesion

@staticmethod
def _compact_line_gutter(output: str) -> str:
r"""Tighten `read_file`'s cat -n line-number gutter for display.

The tool emits `f"{line_num:6d}\t{line}"` — a 6-wide right-justified
number plus a tab — so even single-digit line numbers carry five
leading spaces and the tab pushes content to a distant tab stop. The
model needs that raw format for edits, but the TUI renders a compact
gutter instead: numbers right-justified to the widest number actually
present, then two spaces, mirroring how grep/glob results sit flush
left. Source indentation after the gutter is preserved untouched.

Lines that don't match the cat -n shape (e.g. test fixtures or
non-numbered output) are passed through unchanged.
r"""Tighten legacy `read_file` cat-n line-number gutters for display.

Older tool output used `f"{line_num:6d}\t{line}"` — a 6-wide
right-justified number plus a tab — so even single-digit line numbers
carried five leading spaces and the tab pushed content to a distant tab
stop. The TUI renders that legacy gutter in the newer compact shape:
numbers right-justified to the widest number actually present, then two
spaces, mirroring how grep/glob results sit flush left. Source
indentation after the gutter is preserved untouched.

Lines that don't match the legacy cat-n shape (e.g. current compact
read output, test fixtures, or non-numbered output) are passed through
unchanged.

Returns:
The output with compacted gutters, or the original string if no
Expand Down
12 changes: 6 additions & 6 deletions libs/code/tests/unit_tests/tui/widgets/test_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -2760,14 +2760,14 @@ def test_format_file_output_preview_budget_hit_on_separator(self) -> None:
def test_format_output_compacts_line_number_gutter(self) -> None:
r"""Line-number gutters are tightened, all rows aligned to one column.

`read_file` emits `f"{line_num:6d}\t{line}"` — a 6-wide right-justified
number plus a tab — which renders far from the line numbers and (when
the first row's padding was stripped) misaligned. The TUI recomputes a
compact gutter: numbers right-justified to the widest number present,
two spaces, then the original source indentation.
Legacy `read_file` output used `f"{line_num:6d}\t{line}"` — a 6-wide
right-justified number plus a tab — which renders far from the line
numbers and (when the first row's padding was stripped) misaligned. The
TUI recomputes a compact gutter: numbers right-justified to the widest
number present, two spaces, then the original source indentation.
"""
msg = ToolCallMessage("read_file", {"path": "/tmp/a.py"})
# cat -n style: 6-wide right-justified number + tab + source line.
# Legacy cat-n style: 6-wide right-justified number + tab + source line.
output = ' 1\t"""doc"""\n 2\t\n 3\t indented'
result = msg._format_output(output, is_preview=False)

Expand Down
2 changes: 1 addition & 1 deletion libs/deepagents/deepagents/backends/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ def read(
limit: Maximum number of lines to read.

Returns:
String containing file content formatted with line numbers (`cat -n` format),
String containing file content formatted with line numbers,
starting at line 1.

Lines longer than 2000 characters are truncated.
Expand Down
28 changes: 13 additions & 15 deletions libs/deepagents/deepagents/backends/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
"""

MAX_LINE_LENGTH = 5000
LINE_NUMBER_WIDTH = 6
TOOL_RESULT_TOKEN_LIMIT = 20000 # Same threshold as eviction
TRUNCATION_GUIDANCE = "... [results truncated, try being more specific with your parameters]"

Expand Down Expand Up @@ -203,10 +202,11 @@ def format_content_with_line_numbers(
content: str | list[str],
start_line: int = 1,
) -> str:
"""Format file content with line numbers (`cat -n` style).
"""Format file content with line numbers.

Chunks lines longer than `MAX_LINE_LENGTH` with continuation markers
(e.g., `5.1`, `5.2`).
(e.g., `5.1`, `5.2`). Line markers are separated from source content
with two spaces so source tabs cannot be confused with a gutter separator.

Args:
content: File content as string or list of lines
Expand All @@ -222,28 +222,26 @@ def format_content_with_line_numbers(
else:
lines = content

result_lines = []
rows: list[tuple[str, str]] = []
marker_width = 0
for i, line in enumerate(lines):
line_num = i + start_line

if len(line) <= MAX_LINE_LENGTH:
result_lines.append(f"{line_num:{LINE_NUMBER_WIDTH}d}\t{line}")
marker = str(line_num)
rows.append((marker, line))
marker_width = max(marker_width, len(marker))
else:
# Split long line into chunks with continuation markers
num_chunks = (len(line) + MAX_LINE_LENGTH - 1) // MAX_LINE_LENGTH
for chunk_idx in range(num_chunks):
start = chunk_idx * MAX_LINE_LENGTH
end = min(start + MAX_LINE_LENGTH, len(line))
chunk = line[start:end]
if chunk_idx == 0:
# First chunk: use normal line number
result_lines.append(f"{line_num:{LINE_NUMBER_WIDTH}d}\t{chunk}")
else:
# Continuation chunks: use decimal notation (e.g., 5.1, 5.2)
continuation_marker = f"{line_num}.{chunk_idx}"
result_lines.append(f"{continuation_marker:>{LINE_NUMBER_WIDTH}}\t{chunk}")

return "\n".join(result_lines)
marker = str(line_num) if chunk_idx == 0 else f"{line_num}.{chunk_idx}"
rows.append((marker, chunk))
marker_width = max(marker_width, len(marker))

return "\n".join(f"{marker:>{marker_width}} {line}" for marker, line in rows)
Comment thread
robin-agent[bot] marked this conversation as resolved.
Resolved


def check_empty_content(content: str) -> str | None:
Expand Down
2 changes: 1 addition & 1 deletion libs/deepagents/deepagents/middleware/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ class ExecuteSchema(BaseModel):
- Read more sections: read_file(file_path="...", offset=100, limit=200) for next 200 lines
Comment thread
robin-agent[bot] marked this conversation as resolved.
Resolved
- Omit `limit` to use the default window; increase it only when necessary for editing
- Specify offset and limit: read_file(file_path="...", offset=0, limit=100) reads first 100 lines
- Results are returned using cat -n format, with line numbers starting at 1
- Results are returned with line numbers starting at 1, followed by two spaces and the source content
- Lines longer than 5,000 characters will be split into multiple lines with continuation markers (e.g., 5.1, 5.2, etc.). `limit` applies to source lines, so continuation rows do not consume the budget.
- You have the capability to call multiple tools in a single response. It is always better to speculatively read multiple files as a batch that are potentially useful.
- If you read a file that exists but has empty contents you will receive a system reminder warning in place of file contents.
Expand Down
10 changes: 5 additions & 5 deletions libs/deepagents/tests/unit_tests/test_end_to_end.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ def test_deep_agent_truncate_lines(self, tmp_path: Path, backend: BackendProtoco
assert "short line 0" in file_content
assert "xxx" in file_content
# All four wrapped chunks of source line 2 render in order.
for marker in ("2\t", "2.1\t", "2.2\t", "2.3\t"):
for marker in (" 2 ", "2.1 ", "2.2 ", "2.3 "):
assert marker in file_content, f"missing continuation marker {marker!r}"
# Source line 3 is the third source line and must be included.
assert "short line 2" in file_content
Expand Down Expand Up @@ -1313,11 +1313,11 @@ def test_deep_agent_read_file_pagination_does_not_skip_wrapped_lines(self, tmp_p
# All three continuation chunks of the wrapped line 2 must render in
# order, before `important instruction`, with nothing dropped at the
# page boundary.
for marker in ("2\t", "2.1\t", "2.2\t"):
for marker in (" 2 ", "2.1 ", "2.2 "):
assert marker in combined, f"missing continuation marker {marker!r}"
idx_first = combined.index("2\t")
idx_cont1 = combined.index("2.1\t")
idx_cont2 = combined.index("2.2\t")
idx_first = combined.index(" 2 ")
idx_cont1 = combined.index("2.1 ")
idx_cont2 = combined.index("2.2 ")
idx_next = combined.index("important instruction")
assert idx_first < idx_cont1 < idx_cont2 < idx_next

Expand Down
61 changes: 34 additions & 27 deletions libs/deepagents/tests/unit_tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -1254,9 +1254,16 @@ def test_format_content_with_line_numbers_short_lines(self):

lines = result.split("\n")
assert len(lines) == 3
assert " 1\tshort line 1" in lines[0]
assert " 2\tshort line 2" in lines[1]
assert " 3\tshort line 3" in lines[2]
assert "1 short line 1" in lines[0]
assert "2 short line 2" in lines[1]
assert "3 short line 3" in lines[2]

def test_format_content_with_line_numbers_preserves_source_tabs(self):
"""Test that source tabs remain source content after the gutter."""
content = ["\tif config:", "\t\tbilling_cfg = {}"]
result = format_content_with_line_numbers(content, start_line=1)

assert result.split("\n") == ["1 \tif config:", "2 \t\tbilling_cfg = {}"]

def test_format_content_with_line_numbers_long_line_with_continuation(self):
"""Test that long lines (>5000 chars) are split with continuation markers."""
Expand All @@ -1266,18 +1273,18 @@ def test_format_content_with_line_numbers_long_line_with_continuation(self):

lines = result.split("\n")
assert len(lines) == 7 # 1 short + 5 continuation (2, 2.1, 2.2, 2.3, 2.4) + 1 short
assert " 1\tshort line" in lines[0]
assert " 2\t" in lines[1]
assert " 1 short line" in lines[0]
assert " 2 " in lines[1]
assert lines[1].count("a") == 5000
assert " 2.1\t" in lines[2]
assert "2.1 " in lines[2]
assert lines[2].count("a") == 5000
assert " 2.2\t" in lines[3]
assert "2.2 " in lines[3]
assert lines[3].count("a") == 5000
assert " 2.3\t" in lines[4]
assert "2.3 " in lines[4]
assert lines[4].count("a") == 5000
assert " 2.4\t" in lines[5]
assert "2.4 " in lines[5]
assert lines[5].count("a") == 5000
assert " 3\tanother short line" in lines[6]
assert " 3 another short line" in lines[6]

def test_format_content_with_line_numbers_multiple_long_lines(self):
"""Test multiple long lines in sequence with proper line numbering."""
Expand All @@ -1287,18 +1294,18 @@ def test_format_content_with_line_numbers_multiple_long_lines(self):
result = format_content_with_line_numbers(content, start_line=5)
lines = result.split("\n")
assert len(lines) == 7 # 3 (line 5, 5.1, 5.2) + 1 middle + 3 (line 7, 7.1, 7.2)
assert " 5\t" in lines[0]
assert " 5 " in lines[0]
assert lines[0].count("x") == 5000
assert " 5.1\t" in lines[1]
assert "5.1 " in lines[1]
assert lines[1].count("x") == 5000
assert " 5.2\t" in lines[2]
assert "5.2 " in lines[2]
assert lines[2].count("x") == 5000
assert " 6\tmiddle" in lines[3]
assert " 7\t" in lines[4]
assert " 6 middle" in lines[3]
assert " 7 " in lines[4]
assert lines[4].count("y") == 5000
assert " 7.1\t" in lines[5]
assert "7.1 " in lines[5]
assert lines[5].count("y") == 5000
assert " 7.2\t" in lines[6]
assert "7.2 " in lines[6]
assert lines[6].count("y") == 5000

def test_format_content_with_line_numbers_exact_limit(self):
Expand All @@ -1309,7 +1316,7 @@ def test_format_content_with_line_numbers_exact_limit(self):

lines = result.split("\n")
assert len(lines) == 1
assert " 1\t" in lines[0]
assert "1 " in lines[0]
assert lines[0].count("b") == 5000

def test_read_file_with_long_lines_shows_continuation_markers(self):
Expand All @@ -1322,14 +1329,14 @@ def test_read_file_with_long_lines_shows_continuation_markers(self):
result = format_content_with_line_numbers(sliced, start_line=1)
lines = result.split("\n")
assert len(lines) == 5 # 1 first + 3 continuation (2, 2.1, 2.2) + 1 third
assert " 1\tfirst line" in lines[0]
assert " 2\t" in lines[1]
assert " 1 first line" in lines[0]
assert " 2 " in lines[1]
assert lines[1].count("z") == 5000
assert " 2.1\t" in lines[2]
assert "2.1 " in lines[2]
assert lines[2].count("z") == 5000
assert " 2.2\t" in lines[3]
assert "2.2 " in lines[3]
assert lines[3].count("z") == 5000
assert " 3\tthird line" in lines[4]
assert " 3 third line" in lines[4]

def test_read_file_with_offset_and_long_lines(self):
"""Test that read_file with offset handles long lines correctly."""
Expand All @@ -1341,13 +1348,13 @@ def test_read_file_with_offset_and_long_lines(self):
result = format_content_with_line_numbers(sliced, start_line=3)
lines = result.split("\n")
assert len(lines) == 4 # 3 continuation (3, 3.1, 3.2) + 1 line4
assert " 3\t" in lines[0]
assert " 3 " in lines[0]
assert lines[0].count("m") == 5000
assert " 3.1\t" in lines[1]
assert "3.1 " in lines[1]
assert lines[1].count("m") == 5000
assert " 3.2\t" in lines[2]
assert "3.2 " in lines[2]
assert lines[2].count("m") == 2000
assert " 4\tline4" in lines[3]
assert " 4 line4" in lines[3]

def test_intercept_short_toolmessage(self):
"""Test that small ToolMessages pass through unchanged."""
Expand Down
Loading