Skip to content

fix(harness): count a trailing newline as a line terminator in file tools - #5260

Open
BlueX888 wants to merge 1 commit into
strands-agents:mainfrom
BlueX888:fix/file-tool-line-counts
Open

BlueX888 wants to merge 1 commit into
strands-agents:mainfrom
BlueX888:fix/file-tool-line-counts

Conversation

@BlueX888

Copy link
Copy Markdown
Contributor

Human Overview

Description

The file tools treat a trailing newline as the start of a new empty line. For the canonical POSIX file "alpha\nbeta\n" (2 lines, wc -l = 2), read returns a phantom third line that offset=3 can select, the footer says of 3, and write reports Wrote 3 lines. An empty file reads as one numbered line but writes as 0 lines, and read(limit=0) returns a misleading "offset None is past the end" message. Coding agents rely on these line numbers, so the phantom line sends them to a location that does not exist.

Related Issues

None — small bug fix, no prior issue (duplicate search clean).

Documentation PR

Not needed — tool output is corrected, no API change.

Type of Change

Bug fix

Testing

Added five Python tests and five mirrored TypeScript tests: trailing-newline file has exactly two numbered lines with no phantom/footer; offset=3 reports [File has 2 lines; offset 3 is past the end.]; write("alpha\nbeta\n") reports Wrote 2 lines; empty file is 0 lines consistently ([File is empty.] / Wrote 0 lines); limit=0 raises ValueError("limit must be at least 1.").

Red on 3bd98e6 with the final tests: 5 failed in harness-py/tests/tools/test_file_tools.py, no collection errors. Green with the fix: Python 21 passed and TypeScript 21 passed (both file-tool suites).

  • I ran hatch run prepare

Additional Details

Extended context

str.split("\n") yields a final empty element for content ending in \n; that element is a terminator, not a line. Both SDKs now use one shared helper per language for both read and write, so the counts cannot drift. \r handling is unchanged (splitting remains on \n only). The Python harness package was installed from source in a dedicated venv using the CI command (pip install -e '.[dev]'); the committed tests ran against the real package. TypeScript tests ran via npx vitest run test/tools/file-tools.test.ts.

Checklist

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly (no doc changes needed)
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that I can use, modify, copy, and redistribute this contribution, under the terms of your choice.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-tool Tool behavior/api bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant