Repository navigation
Conversation
The Read schema declares offset >= 1 and 1 <= limit <= 2000, but nothing enforced them at runtime: a non-positive offset negative-sliced the file and silently returned its tail (e.g. offset=0 returned the last line labeled "0"), and limit <= 0 returned empty text, both with a success result. Return an error chunk instead so the model can self-correct, matching the Grep/Glob pagination validation.
|
Thanks for the pull request. One thing worth sorting out before it is reviewed:
If you think it belongs here anyway, say so and a maintainer will take a look. |
|
Closing this in favor of #3297 — @SSK988I claimed #3291 first, and their PR covers the same gap (rejecting out-of-range One datapoint from our own testing, in case it is useful there: a non-positive Apologies for the duplicate — the issue is in good hands. |
AgentScope Version
2.0.10
Description
Fixes #3291.
What Problem This Solves
This PR fixes a pagination validation gap in the builtin
Readtool.The
Readschema already declaresoffsetas a 1-based line number withge=1, andlimitwithge=1, le=2000(src/agentscope/tool/_builtin/_read.py:60-72), but — unlikeGrepandGlob— nothing validated them at runtime, so tool calls whose arguments bypass schema validation reached the slicing step unvalidated._read_text_fileslices withlines[start_idx:end_idx]wherestart_idx = offset - 1(src/agentscope/tool/_builtin/_read.py:533-535). A non-positiveoffsetmakesstart_idxnegative, so Python addresses the list from its end and the tool returns the tail of the file labeled with line numbers0or negative, with a success state.limit <= 0returns empty text, also with a success state. Reading a 10-line file onmain:Because the tool reports success, the model receives content from the wrong part of the file with impossible line numbers and no signal to self-correct.
Change
Add runtime validation in
_read_text_file, before any slicing, mirroring theGrep/Globpagination guards:offset < 1->Error: offset must be at least 1, got: {offset}.limit < 1 or limit > 2000->Error: limit must be between 1 and 2000, got: {limit}.Both return a
ToolResultState.ERRORchunk so the model can retry with valid arguments. Valid reads are unchanged, and sinceoffset/limitonly apply to plain text files, the guards live in_read_text_fileso the PDF and image paths are untouched.Testing
main(before the fix):pytest tests/builtin_read_test.py->20 passed in 0.86spytest tests/builtin_read_test.py tests/builtin_write_test.py tests/builtin_edit_test.py tests/builtin_grep_test.py tests/builtin_glob_test.py tests/builtin_bash_test.py tests/builtin_file_cache_test.py->122 passed, 61 subtests passed in 2.13spytest tests->2746 passed, 147 skipped, 600 subtests passed in 167.54spre-commit run --files src/agentscope/tool/_builtin/_read.py tests/builtin_read_test.py-> all hooks passed (mypy, black, flake8, pylint, pyroma)Checklist
Please check the following items before code is ready to be reviewed.