fix(memory): use split("\n") in insert() to match view()/str_replace() - #1748
Open
otiscuilei wants to merge 1 commit into
Open
fix(memory): use split("\n") in insert() to match view()/str_replace()#1748otiscuilei wants to merge 1 commit into
otiscuilei wants to merge 1 commit into
Conversation
The filesystem memory tool's insert() split file content with
str.splitlines(), which treats ~8 extra Unicode/control characters
(\r, \v, \f, \x1c-\x1e, NEL, U+2028, U+2029) as line boundaries.
Every other command (view, str_replace) splits only on "\n", so
insert() counted and located lines differently from what view()
reports to the model, placing inserted text at the wrong offset, and
rejoined with "\n", silently discarding those boundary characters.
Switch both the sync and async insert() to content.split("\n") so line
indexing is consistent across all commands and content is preserved.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Who hits this
Anyone using the built-in filesystem memory tool (
BetaLocalFilesystemMemoryTool/BetaAsyncLocalFilesystemMemoryTool) whose memory files contain any of the charactersstr.splitlines()treats as line boundaries but"\n"does not: carriage return, vertical tab, form feed (\x0c), file/group/record separators (\x1c-\x1e), NEL (\x85), U+2028, or U+2029. When the model issues aninsertcommand against such a file, the insert lands at the wrong line and those characters are silently rewritten to\n.Root cause
Every memory command reads and splits file content the same way except
insert.view()(line 451) andstr_replace()(line 526) split on"\n", but both the sync and asyncinsert()usedcontent.splitlines().splitlines()breaks on ~8 extra Unicode/control boundaries in addition to\n. As a resultinsert():view()reports to the model, so aninsert_linechosen from the numbersview()showed landed at the wrong offset (text spliced into the middle of what the model saw as a single line), and"\n", discarding the boundary characters that were part of the file's content.Fix
Change
content.splitlines()tocontent.split("\n")in bothinsert()methods, making line indexing consistent withview()andstr_replace()and preserving the file's characters. No behavior change for ordinary\n-delimited files.Before / after
Tests
Adds one regression test (sync and async) in
tests/lib/tools/memory_tools/test_filesystem.pythat creates a file containing U+2028 and a form feed, assertsview()numbers it as a single line, runsinsert(insert_line=1, ...), and asserts the boundary characters are preserved and the text is appended after the whole line. Fails on the current code, passes with the fix. Full memory-tool suite: 65 passed.ruff check/ruff formatclean.