sdk-python icon indicating copy to clipboard operation
sdk-python copied to clipboard

[BUG] Vended read/write tools count a trailing newline as an extra line (phantom last line, false "read more" notice)

Open 1AbdullahButt opened this issue 3 weeks ago • 3 comments

Human Overview

  • A 2-line file ending in a newline is reported as 3 lines. read shows an empty line 3 and says “Use offset/limit to read more” even though everything was shown.
  • Why it matters: agents get confused and make an extra, pointless read call.
  • Similar open PRs (#5309 and #5260) fix this only in a different file.

Checks

  • [x] I have updated to the lastest minor and patch version of Strands
  • [x] I have checked the documentation and this is not expected behavior
  • [x] I have searched ./issues and there are no duplicates of my issue

SDK Language

Python

Strands Version

main at 1060ccee1 (1.64.1.dev91)

Language Runtime Version

Python 3.12.10

Operating System

Windows 11

Installation Method

git clone

Steps to Reproduce

import asyncio
from types import SimpleNamespace
from strands.vended_tools.read import make_read
from strands.vended_tools.write import make_write

class MemSandbox:
    def __init__(self):
        self.files = {"/tmp/two.txt": "a\nb\n"}
    async def read_text(self, path):
        return self.files[path]
    async def write_text(self, path, content):
        self.files[path] = content

sandbox = MemSandbox()
read, write = make_read(sandbox=sandbox), make_write(sandbox=sandbox)

async def call(t, tool_input):
    tool_use = {"toolUseId": "1", "name": t.tool_name, "input": tool_input}
    async for event in t.stream(tool_use, {"agent": SimpleNamespace()}):
        pass
    return event["tool_result"]["content"][0]["text"]

for t, tool_input in [
    (read, {"path": "/tmp/two.txt"}),
    (read, {"path": "/tmp/two.txt", "limit": 2}),
    (read, {"path": "/tmp/two.txt", "offset": 3}),
    (write, {"path": "/tmp/x.txt", "content": "a\nb\n"}),
]:
    print(repr(asyncio.run(call(t, tool_input))))

Expected Behavior

A file containing "a\nb\n" has 2 lines (as cat -n and wc -l count them): there is no line 3, reading both lines shows no "read more" notice, offset 3 is past the end, and write reports 2 lines.

Actual Behavior

'     1\ta\n     2\tb\n     3\t'
'     1\ta\n     2\tb\n[Showing lines 1-2 of 3. Use offset/limit to read more.]'
'     3\t\n[Showing lines 3-3 of 3. Use offset/limit to read more.]'
'Wrote 3 lines to /tmp/x.txt.'

Any file that ends with a newline (nearly every text file) is shown with an extra empty last line, and a read that already covers the whole file tells the model there is more to read.

Possible Solution

Both tools count lines with content.split("\n"), which gives one extra empty element after a trailing newline: read and write. Dropping that final empty element when the content ends with \n would fix both.

Additional Context

Open PRs #5309 and #5260 fix the same off-by-one in the harness's own file_tools.py (harness-py and harness-ts). They don't touch these SDK vended tools in strands-py/src/strands/vended_tools/.

Happy to send a fix if that helps.

Drafted with Claude Code (AI); the repro above was run locally against main.

1AbdullahButt avatar Nov 12 '26 19:11 1AbdullahButt

I'd like to take this. With a sandbox file containing "a\nb\n", the vended read tool reports a third phantom line and a false "read more" notice, and write counts lines the same way. I plan to fix the line counting in strands.vended_tools.read and write so a single trailing newline terminates the last line instead of starting a new one, without changing how offset and limit are interpreted. I'll add tests built on the in-memory sandbox from the repro: reads with no limit, limit=2, and an offset past the end, plus a write result. Could I be assigned, as CONTRIBUTING asks for confirmation first? I'll write the Human Overview myself and disclose AI assistance in the PR.

ahmadkhancodes avatar Nov 13 '26 06:11 ahmadkhancodes

Opened a fix as #6120.

The fix adds a shared split_lines() helper in strands/vended_tools/_lines.py and uses it in both the read and write vended tools, so line counting matches cat -n and wc -l: a trailing newline terminates the last line instead of starting a new one. The read tool also raises the Use offset/limit to read more notice only when lines actually remain, rather than whenever the requested window was smaller than the file - that was the other half of the phantom-line report.

Python only; the harness-py / harness-ts copies stay with #5309 and #5260.

61 tests pass in the two affected test files, ruff clean.

AnnasMazhar avatar Nov 13 '26 11:11 AnnasMazhar

I see #6120 already fixes this, so I'll step back to avoid duplicate work. Thanks!

ahmadkhancodes avatar Nov 16 '26 06:11 ahmadkhancodes