| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import os |
| 4 |
import stat |
| 5 |
import tempfile |
| 6 |
from typing import Iterator |
| 7 |
from pathlib import Path |
| 8 |
|
| 9 |
import pytest |
| 10 |
|
| 11 |
from anthropic.types.beta import ( |
| 12 |
BetaMemoryTool20250818ViewCommand, |
| 13 |
BetaMemoryTool20250818CreateCommand, |
| 14 |
BetaMemoryTool20250818DeleteCommand, |
| 15 |
BetaMemoryTool20250818InsertCommand, |
| 16 |
BetaMemoryTool20250818RenameCommand, |
| 17 |
BetaMemoryTool20250818StrReplaceCommand, |
| 18 |
) |
| 19 |
from anthropic.lib.tools._beta_functions import ToolError |
| 20 |
from anthropic.lib.tools._beta_builtin_memory_tool import ( |
| 21 |
BetaLocalFilesystemMemoryTool, |
| 22 |
BetaAsyncLocalFilesystemMemoryTool, |
| 23 |
) |
| 24 |
|
| 25 |
|
| 26 |
@pytest.fixture |
| 27 |
def temp_directory() -> Iterator[str]: |
| 28 |
with tempfile.TemporaryDirectory() as tmpdirname: |
| 29 |
yield tmpdirname |
| 30 |
|
| 31 |
|
| 32 |
@pytest.fixture |
| 33 |
def sync_local_filesystem_tool(temp_directory: str) -> BetaLocalFilesystemMemoryTool: |
| 34 |
return BetaLocalFilesystemMemoryTool(base_path=temp_directory) |
| 35 |
|
| 36 |
|
| 37 |
@pytest.fixture |
| 38 |
def async_local_filesystem_tool(temp_directory: str) -> BetaAsyncLocalFilesystemMemoryTool: |
| 39 |
return BetaAsyncLocalFilesystemMemoryTool(base_path=temp_directory) |
| 40 |
|
| 41 |
|
| 42 |
def get_directory_snapshot(base_path: str) -> dict[str, str]: |
| 43 |
"""Get a snapshot of directory contents with relative paths.""" |
| 44 |
snapshot: dict[str, str] = {} |
| 45 |
for root, _, files in os.walk(base_path): |
| 46 |
for file in files: |
| 47 |
full_path = os.path.join(root, file) |
| 48 |
rel_path = os.path.relpath(full_path, base_path) |
| 49 |
with open(full_path, "r") as f: |
| 50 |
snapshot[rel_path] = f.read() |
| 51 |
return snapshot |
| 52 |
|
| 53 |
|
| 54 |
class TestBetaLocalFilesystemMemoryTool: |
| 55 |
def test_mkdir_parents_not_world_writable_under_permissive_umask(self) -> None: |
| 56 |
if os.name != "posix": |
| 57 |
pytest.skip("POSIX mode bits only") |
| 58 |
# Permissive umask: newly created dirs would default to 0o777 unless an |
| 59 |
# explicit mode is enforced on every component of the tree. |
| 60 |
old_umask = os.umask(0) |
| 61 |
try: |
| 62 |
with tempfile.TemporaryDirectory() as tmp: |
| 63 |
# base_path's parent ("nested") does NOT exist yet, so the tool must |
| 64 |
# create it as an intermediate parent. The bug: pathlib applies the |
| 65 |
# 0o700 mode only to the leaf, leaving these intermediate parents at |
| 66 |
# the umask default (0o777 here) — world-writable. |
| 67 |
base_path = Path(tmp) / "nested" / "memory" |
| 68 |
tool = BetaLocalFilesystemMemoryTool(base_path=str(base_path)) |
| 69 |
for directory in (base_path.parent, base_path, tool.memory_root): |
| 70 |
mode = stat.S_IMODE(directory.stat().st_mode) |
| 71 |
assert mode == 0o700, f"{directory} has mode {oct(mode)}, expected 0o700" |
| 72 |
assert not (mode & 0o077), f"{directory} is group/other-accessible: {oct(mode)}" |
| 73 |
finally: |
| 74 |
os.umask(old_umask) |
| 75 |
|
| 76 |
def test_create(self, sync_local_filesystem_tool: BetaLocalFilesystemMemoryTool) -> None: |
| 77 |
result = sync_local_filesystem_tool.create( |
| 78 |
BetaMemoryTool20250818CreateCommand( |
| 79 |
command="create", |
| 80 |
file_text="Hello, World!", |
| 81 |
path="/memories/test_file.txt", |
| 82 |
) |
| 83 |
) |
| 84 |
assert result == "File created successfully at: /memories/test_file.txt" |
| 85 |
dir_snapshot = get_directory_snapshot(str(sync_local_filesystem_tool.base_path)) |
| 86 |
assert dir_snapshot == {"memories/test_file.txt": "Hello, World!"} |
| 87 |
|
| 88 |
def test_create_nested_directories(self, sync_local_filesystem_tool: BetaLocalFilesystemMemoryTool) -> None: |
| 89 |
result = sync_local_filesystem_tool.create( |
| 90 |
BetaMemoryTool20250818CreateCommand( |
| 91 |
command="create", |
| 92 |
file_text="Nested file", |
| 93 |
path="/memories/deep/nested/file.txt", |
| 94 |
) |
| 95 |
) |
| 96 |
assert result == "File created successfully at: /memories/deep/nested/file.txt" |
| 97 |
dir_snapshot = get_directory_snapshot(str(sync_local_filesystem_tool.base_path)) |
| 98 |
assert dir_snapshot == {"memories/deep/nested/file.txt": "Nested file"} |
| 99 |
|
| 100 |
def test_create_error_if_file_already_exists( |
| 101 |
self, sync_local_filesystem_tool: BetaLocalFilesystemMemoryTool |
| 102 |
) -> None: |
| 103 |
sync_local_filesystem_tool.create( |
| 104 |
BetaMemoryTool20250818CreateCommand( |
| 105 |
command="create", |
| 106 |
file_text="Original", |
| 107 |
path="/memories/existing.txt", |
| 108 |
) |
| 109 |
) |
| 110 |
|
| 111 |
with pytest.raises(ToolError, match="File /memories/existing.txt already exists"): |
| 112 |
sync_local_filesystem_tool.create( |
| 113 |
BetaMemoryTool20250818CreateCommand( |
| 114 |
command="create", |
| 115 |
file_text="New", |
| 116 |
path="/memories/existing.txt", |
| 117 |
) |
| 118 |
) |
| 119 |
|
| 120 |
def test_view_file(self, sync_local_filesystem_tool: BetaLocalFilesystemMemoryTool) -> None: |
| 121 |
sync_local_filesystem_tool.create( |
| 122 |
BetaMemoryTool20250818CreateCommand( |
| 123 |
command="create", |
| 124 |
file_text="Line 1\nLine 2\nLine 3", |
| 125 |
path="/memories/view_test.txt", |
| 126 |
) |
| 127 |
) |
| 128 |
|
| 129 |
result = sync_local_filesystem_tool.view( |
| 130 |
BetaMemoryTool20250818ViewCommand(command="view", path="/memories/view_test.txt") |
| 131 |
) |
| 132 |
assert ( |
| 133 |
result |
| 134 |
== "Here's the content of /memories/view_test.txt with line numbers:\n 1\tLine 1\n 2\tLine 2\n 3\tLine 3" |
| 135 |
) |
| 136 |
|
| 137 |
def test_view_directory(self, sync_local_filesystem_tool: BetaLocalFilesystemMemoryTool) -> None: |
| 138 |
sync_local_filesystem_tool.create( |
| 139 |
BetaMemoryTool20250818CreateCommand( |
| 140 |
command="create", |
| 141 |
file_text="File 1", |
| 142 |
path="/memories/dir/file1.txt", |
| 143 |
) |
| 144 |
) |
| 145 |
|
| 146 |
sync_local_filesystem_tool.create( |
| 147 |
BetaMemoryTool20250818CreateCommand( |
| 148 |
command="create", |
| 149 |
file_text="File 2", |
| 150 |
path="/memories/dir/file2.txt", |
| 151 |
) |
| 152 |
) |
| 153 |
|
| 154 |
result = sync_local_filesystem_tool.view( |
| 155 |
BetaMemoryTool20250818ViewCommand(command="view", path="/memories/dir") |
| 156 |
) |
| 157 |
|
| 158 |
assert "Here're the files and directories up to 2 levels deep in /memories/dir" in result |
| 159 |
assert "/memories/dir" in result |
| 160 |
assert "/memories/dir/file1.txt" in result |
| 161 |
assert "/memories/dir/file2.txt" in result |
| 162 |
|
| 163 |
def test_view_file_with_range(self, sync_local_filesystem_tool: BetaLocalFilesystemMemoryTool) -> None: |
| 164 |
sync_local_filesystem_tool.create( |
| 165 |
BetaMemoryTool20250818CreateCommand( |
| 166 |
command="create", |
| 167 |
file_text="Line 1\nLine 2\nLine 3\nLine 4\nLine 5", |
| 168 |
path="/memories/range_test.txt", |
| 169 |
) |
| 170 |
) |
| 171 |
|
| 172 |
result = sync_local_filesystem_tool.view( |
| 173 |
BetaMemoryTool20250818ViewCommand(command="view", path="/memories/range_test.txt", view_range=[2, 4]) |
| 174 |
) |
| 175 |
|
| 176 |
assert ( |
| 177 |
result |
| 178 |
== "Here's the content of /memories/range_test.txt with line numbers:\n 2\tLine 2\n 3\tLine 3\n 4\tLine 4" |
| 179 |
) |
| 180 |
|
| 181 |
def test_view_error_for_non_existent_file(self, sync_local_filesystem_tool: BetaLocalFilesystemMemoryTool) -> None: |
| 182 |
with pytest.raises( |
| 183 |
ToolError, match="The path /memories/nonexistent.txt does not exist. Please provide a valid path." |
| 184 |
): |
| 185 |
sync_local_filesystem_tool.view( |
| 186 |
BetaMemoryTool20250818ViewCommand(command="view", path="/memories/nonexistent.txt") |
| 187 |
) |
| 188 |
|
| 189 |
def test_view_error_for_files_with_too_many_lines( |
| 190 |
self, sync_local_filesystem_tool: BetaLocalFilesystemMemoryTool |
| 191 |
) -> None: |
| 192 |
too_many_lines = "\n".join(["line"] * 1000000) |
| 193 |
sync_local_filesystem_tool.create( |
| 194 |
BetaMemoryTool20250818CreateCommand( |
| 195 |
command="create", |
| 196 |
file_text=too_many_lines, |
| 197 |
path="/memories/huge.txt", |
| 198 |
) |
| 199 |
) |
| 200 |
|
| 201 |
with pytest.raises(ToolError, match=r"exceeds maximum line limit of 999,999 lines"): |
| 202 |
sync_local_filesystem_tool.view( |
| 203 |
BetaMemoryTool20250818ViewCommand(command="view", path="/memories/huge.txt") |
| 204 |
) |
| 205 |
|
| 206 |
def test_str_replace(self, sync_local_filesystem_tool: BetaLocalFilesystemMemoryTool) -> None: |
| 207 |
sync_local_filesystem_tool.create( |
| 208 |
BetaMemoryTool20250818CreateCommand( |
| 209 |
command="create", |
| 210 |
file_text="Line 1\nOld Text\nLine 3", |
| 211 |
path="/memories/replace_test.txt", |
| 212 |
) |
| 213 |
) |
| 214 |
|
| 215 |
result = sync_local_filesystem_tool.str_replace( |
| 216 |
BetaMemoryTool20250818StrReplaceCommand( |
| 217 |
command="str_replace", |
| 218 |
path="/memories/replace_test.txt", |
| 219 |
old_str="Old Text", |
| 220 |
new_str="New Text", |
| 221 |
) |
| 222 |
) |
| 223 |
|
| 224 |
assert ( |
| 225 |
result |
| 226 |
== "The memory file has been edited. Here is the snippet showing the change (with line numbers):\n 1\tLine 1\n 2\tNew Text\n 3\tLine 3" |
| 227 |
) |
| 228 |
|
| 229 |
dir_snapshot = get_directory_snapshot(str(sync_local_filesystem_tool.base_path)) |
| 230 |
assert dir_snapshot == {"memories/replace_test.txt": "Line 1\nNew Text\nLine 3"} |
| 231 |
|
| 232 |
def test_str_replace_error_when_string_not_found( |
| 233 |
self, sync_local_filesystem_tool: BetaLocalFilesystemMemoryTool |
| 234 |
) -> None: |
| 235 |
sync_local_filesystem_tool.create( |
| 236 |
BetaMemoryTool20250818CreateCommand( |
| 237 |
command="create", |
| 238 |
file_text="Hello, World!", |
| 239 |
path="/memories/replace_test.txt", |
| 240 |
) |
| 241 |
) |
| 242 |
|
| 243 |
with pytest.raises( |
| 244 |
ToolError, |
| 245 |
match="No replacement was performed, old_str `NotFound` did not appear verbatim in /memories/replace_test.txt.", |
| 246 |
): |
| 247 |
sync_local_filesystem_tool.str_replace( |
| 248 |
BetaMemoryTool20250818StrReplaceCommand( |
| 249 |
command="str_replace", |
| 250 |
path="/memories/replace_test.txt", |
| 251 |
old_str="NotFound", |
| 252 |
new_str="Python", |
| 253 |
) |
| 254 |
) |
| 255 |
|
| 256 |
def test_str_replace_error_when_string_appears_multiple_times( |
| 257 |
self, sync_local_filesystem_tool: BetaLocalFilesystemMemoryTool |
| 258 |
) -> None: |
| 259 |
sync_local_filesystem_tool.create( |
| 260 |
BetaMemoryTool20250818CreateCommand( |
| 261 |
command="create", |
| 262 |
file_text="Hello\nMiddle\nHello", |
| 263 |
path="/memories/replace_test.txt", |
| 264 |
) |
| 265 |
) |
| 266 |
|
| 267 |
with pytest.raises( |
| 268 |
ToolError, |
| 269 |
match=r"No replacement was performed\. Multiple occurrences of old_str `Hello` in lines: 1, 3\. Please ensure it is unique", |
| 270 |
): |
| 271 |
sync_local_filesystem_tool.str_replace( |
| 272 |
BetaMemoryTool20250818StrReplaceCommand( |
| 273 |
command="str_replace", |
| 274 |
path="/memories/replace_test.txt", |
| 275 |
old_str="Hello", |
| 276 |
new_str="Hi", |
| 277 |
) |
| 278 |
) |
| 279 |
|
| 280 |
def test_str_replace_error_for_non_existent_file( |
| 281 |
self, sync_local_filesystem_tool: BetaLocalFilesystemMemoryTool |
| 282 |
) -> None: |
| 283 |
with pytest.raises( |
| 284 |
ToolError, match="The path /memories/nonexistent.txt does not exist. Please provide a valid path." |
| 285 |
): |
| 286 |
sync_local_filesystem_tool.str_replace( |
| 287 |
BetaMemoryTool20250818StrReplaceCommand( |
| 288 |
command="str_replace", |
| 289 |
path="/memories/nonexistent.txt", |
| 290 |
old_str="old", |
| 291 |
new_str="new", |
| 292 |
) |
| 293 |
) |
| 294 |
|
| 295 |
def test_insert(self, sync_local_filesystem_tool: BetaLocalFilesystemMemoryTool) -> None: |
| 296 |
sync_local_filesystem_tool.create( |
| 297 |
BetaMemoryTool20250818CreateCommand( |
| 298 |
command="create", file_text="Line 1\nLine 2", path="/memories/insert_test.txt" |
| 299 |
) |
| 300 |
) |
| 301 |
|
| 302 |
result = sync_local_filesystem_tool.insert( |
| 303 |
BetaMemoryTool20250818InsertCommand( |
| 304 |
command="insert", path="/memories/insert_test.txt", insert_line=1, insert_text="Inserted Line" |
| 305 |
) |
| 306 |
) |
| 307 |
assert result == "The file /memories/insert_test.txt has been edited." |
| 308 |
|
| 309 |
dir_snapshot = get_directory_snapshot(str(sync_local_filesystem_tool.base_path)) |
| 310 |
assert dir_snapshot == {"memories/insert_test.txt": "Line 1\nInserted Line\nLine 2\n"} |
| 311 |
|
| 312 |
def test_insert_at_beginning_of_file(self, sync_local_filesystem_tool: BetaLocalFilesystemMemoryTool) -> None: |
| 313 |
sync_local_filesystem_tool.create( |
| 314 |
BetaMemoryTool20250818CreateCommand( |
| 315 |
command="create", file_text="Line 1\nLine 2", path="/memories/insert_test.txt" |
| 316 |
) |
| 317 |
) |
| 318 |
|
| 319 |
sync_local_filesystem_tool.insert( |
| 320 |
BetaMemoryTool20250818InsertCommand( |
| 321 |
command="insert", path="/memories/insert_test.txt", insert_line=0, insert_text="First Line" |
| 322 |
) |
| 323 |
) |
| 324 |
|
| 325 |
dir_snapshot = get_directory_snapshot(str(sync_local_filesystem_tool.base_path)) |
| 326 |
assert dir_snapshot == {"memories/insert_test.txt": "First Line\nLine 1\nLine 2\n"} |
| 327 |
|
| 328 |
def test_insert_at_end_of_file(self, sync_local_filesystem_tool: BetaLocalFilesystemMemoryTool) -> None: |
| 329 |
sync_local_filesystem_tool.create( |
| 330 |
BetaMemoryTool20250818CreateCommand( |
| 331 |
command="create", file_text="Line 1\nLine 2", path="/memories/insert_test.txt" |
| 332 |
) |
| 333 |
) |
| 334 |
|
| 335 |
sync_local_filesystem_tool.insert( |
| 336 |
BetaMemoryTool20250818InsertCommand( |
| 337 |
command="insert", path="/memories/insert_test.txt", insert_line=2, insert_text="Last Line" |
| 338 |
) |
| 339 |
) |
| 340 |
|
| 341 |
dir_snapshot = get_directory_snapshot(str(sync_local_filesystem_tool.base_path)) |
| 342 |
assert dir_snapshot == {"memories/insert_test.txt": "Line 1\nLine 2\nLast Line\n"} |
| 343 |
|
| 344 |
def test_view_preserves_crlf_line_endings(self, sync_local_filesystem_tool: BetaLocalFilesystemMemoryTool) -> None: |
| 345 |
(sync_local_filesystem_tool.memory_root / "crlf.txt").write_bytes(b"line1\r\nline2\r\nline3\r\n") |
| 346 |
|
| 347 |
result = sync_local_filesystem_tool.view( |
| 348 |
BetaMemoryTool20250818ViewCommand(command="view", path="/memories/crlf.txt") |
| 349 |
) |
| 350 |
assert ( |
| 351 |
result |
| 352 |
== "Here's the content of /memories/crlf.txt with line numbers:\n 1\tline1\r\n 2\tline2\r\n 3\tline3\r\n 4\t" |
| 353 |
) |
| 354 |
|
| 355 |
def test_str_replace_preserves_crlf_line_endings( |
| 356 |
self, sync_local_filesystem_tool: BetaLocalFilesystemMemoryTool |
| 357 |
) -> None: |
| 358 |
path = sync_local_filesystem_tool.memory_root / "crlf.txt" |
| 359 |
path.write_bytes(b"line1\r\nline2\r\nline3\r\n") |
| 360 |
|
| 361 |
sync_local_filesystem_tool.str_replace( |
| 362 |
BetaMemoryTool20250818StrReplaceCommand( |
| 363 |
command="str_replace", path="/memories/crlf.txt", old_str="line2", new_str="LINE2" |
| 364 |
) |
| 365 |
) |
| 366 |
assert path.read_bytes() == b"line1\r\nLINE2\r\nline3\r\n" |
| 367 |
|
| 368 |
def test_insert_preserves_crlf_line_endings( |
| 369 |
self, sync_local_filesystem_tool: BetaLocalFilesystemMemoryTool |
| 370 |
) -> None: |
| 371 |
path = sync_local_filesystem_tool.memory_root / "crlf.txt" |
| 372 |
path.write_bytes(b"line1\r\nline2\r\nline3\r\n") |
| 373 |
|
| 374 |
sync_local_filesystem_tool.insert( |
| 375 |
BetaMemoryTool20250818InsertCommand( |
| 376 |
command="insert", path="/memories/crlf.txt", insert_line=1, insert_text="Inserted" |
| 377 |
) |
| 378 |
) |
| 379 |
assert path.read_bytes() == b"line1\r\nInserted\nline2\r\nline3\r\n" |
| 380 |
|
| 381 |
def test_insert_error_for_non_existent_file( |
| 382 |
self, sync_local_filesystem_tool: BetaLocalFilesystemMemoryTool |
| 383 |
) -> None: |
| 384 |
with pytest.raises( |
| 385 |
ToolError, match="The path /memories/nonexistent.txt does not exist. Please provide a valid path." |
| 386 |
): |
| 387 |
sync_local_filesystem_tool.insert( |
| 388 |
BetaMemoryTool20250818InsertCommand( |
| 389 |
command="insert", path="/memories/nonexistent.txt", insert_line=0, insert_text="text" |
| 390 |
) |
| 391 |
) |
| 392 |
|
| 393 |
def test_insert_error_for_invalid_insert_line( |
| 394 |
self, sync_local_filesystem_tool: BetaLocalFilesystemMemoryTool |
| 395 |
) -> None: |
| 396 |
sync_local_filesystem_tool.create( |
| 397 |
BetaMemoryTool20250818CreateCommand( |
| 398 |
command="create", file_text="Line 1\nLine 2", path="/memories/insert_test.txt" |
| 399 |
) |
| 400 |
) |
| 401 |
|
| 402 |
with pytest.raises( |
| 403 |
ToolError, |
| 404 |
match=r"Invalid `insert_line` parameter: 10. It should be within the range \[0, 2\]", |
| 405 |
): |
| 406 |
sync_local_filesystem_tool.insert( |
| 407 |
BetaMemoryTool20250818InsertCommand( |
| 408 |
command="insert", path="/memories/insert_test.txt", insert_line=10, insert_text="text" |
| 409 |
) |
| 410 |
) |
| 411 |
|
| 412 |
def test_delete(self, sync_local_filesystem_tool: BetaLocalFilesystemMemoryTool) -> None: |
| 413 |
sync_local_filesystem_tool.create( |
| 414 |
BetaMemoryTool20250818CreateCommand( |
| 415 |
command="create", file_text="To be deleted", path="/memories/delete_me.txt" |
| 416 |
) |
| 417 |
) |
| 418 |
|
| 419 |
result = sync_local_filesystem_tool.delete( |
| 420 |
BetaMemoryTool20250818DeleteCommand(command="delete", path="/memories/delete_me.txt") |
| 421 |
) |
| 422 |
assert result == "Successfully deleted /memories/delete_me.txt" |
| 423 |
|
| 424 |
dir_snapshot = get_directory_snapshot(str(sync_local_filesystem_tool.base_path)) |
| 425 |
assert dir_snapshot == {} |
| 426 |
|
| 427 |
def test_delete_directory(self, sync_local_filesystem_tool: BetaLocalFilesystemMemoryTool) -> None: |
| 428 |
sync_local_filesystem_tool.create( |
| 429 |
BetaMemoryTool20250818CreateCommand(command="create", file_text="Content", path="/memories/subdir/file.txt") |
| 430 |
) |
| 431 |
|
| 432 |
result = sync_local_filesystem_tool.delete( |
| 433 |
BetaMemoryTool20250818DeleteCommand(command="delete", path="/memories/subdir") |
| 434 |
) |
| 435 |
assert result == "Successfully deleted /memories/subdir" |
| 436 |
|
| 437 |
dir_snapshot = get_directory_snapshot(str(sync_local_filesystem_tool.base_path)) |
| 438 |
assert dir_snapshot == {} |
| 439 |
|
| 440 |
def test_delete_error_when_file_not_found(self, sync_local_filesystem_tool: BetaLocalFilesystemMemoryTool) -> None: |
| 441 |
with pytest.raises(ToolError, match="The path /memories/nonexistent.txt does not exist"): |
| 442 |
sync_local_filesystem_tool.delete( |
| 443 |
BetaMemoryTool20250818DeleteCommand(command="delete", path="/memories/nonexistent.txt") |
| 444 |
) |
| 445 |
|
| 446 |
def test_delete_not_allow_deleting_memories_directory( |
| 447 |
self, sync_local_filesystem_tool: BetaLocalFilesystemMemoryTool |
| 448 |
) -> None: |
| 449 |
with pytest.raises(ToolError, match="Cannot delete the /memories directory itself"): |
| 450 |
sync_local_filesystem_tool.delete(BetaMemoryTool20250818DeleteCommand(command="delete", path="/memories")) |
| 451 |
|
| 452 |
def test_rename(self, sync_local_filesystem_tool: BetaLocalFilesystemMemoryTool) -> None: |
| 453 |
sync_local_filesystem_tool.create( |
| 454 |
BetaMemoryTool20250818CreateCommand( |
| 455 |
command="create", file_text="Original content", path="/memories/old_name.txt" |
| 456 |
) |
| 457 |
) |
| 458 |
|
| 459 |
result = sync_local_filesystem_tool.rename( |
| 460 |
BetaMemoryTool20250818RenameCommand( |
| 461 |
command="rename", old_path="/memories/old_name.txt", new_path="/memories/new_name.txt" |
| 462 |
) |
| 463 |
) |
| 464 |
assert result == "Successfully renamed /memories/old_name.txt to /memories/new_name.txt" |
| 465 |
|
| 466 |
dir_snapshot = get_directory_snapshot(str(sync_local_filesystem_tool.base_path)) |
| 467 |
assert dir_snapshot == {"memories/new_name.txt": "Original content"} |
| 468 |
|
| 469 |
def test_rename_to_nested_path(self, sync_local_filesystem_tool: BetaLocalFilesystemMemoryTool) -> None: |
| 470 |
sync_local_filesystem_tool.create( |
| 471 |
BetaMemoryTool20250818CreateCommand(command="create", file_text="Content", path="/memories/file.txt") |
| 472 |
) |
| 473 |
|
| 474 |
result = sync_local_filesystem_tool.rename( |
| 475 |
BetaMemoryTool20250818RenameCommand( |
| 476 |
command="rename", old_path="/memories/file.txt", new_path="/memories/nested/dir/file.txt" |
| 477 |
) |
| 478 |
) |
| 479 |
assert result == "Successfully renamed /memories/file.txt to /memories/nested/dir/file.txt" |
| 480 |
|
| 481 |
dir_snapshot = get_directory_snapshot(str(sync_local_filesystem_tool.base_path)) |
| 482 |
assert dir_snapshot == {"memories/nested/dir/file.txt": "Content"} |
| 483 |
|
| 484 |
def test_rename_error_when_source_not_found( |
| 485 |
self, sync_local_filesystem_tool: BetaLocalFilesystemMemoryTool |
| 486 |
) -> None: |
| 487 |
with pytest.raises(ToolError, match="The path /memories/nonexistent.txt does not exist"): |
| 488 |
sync_local_filesystem_tool.rename( |
| 489 |
BetaMemoryTool20250818RenameCommand( |
| 490 |
command="rename", old_path="/memories/nonexistent.txt", new_path="/memories/new.txt" |
| 491 |
) |
| 492 |
) |
| 493 |
|
| 494 |
def test_rename_error_when_destination_exists( |
| 495 |
self, sync_local_filesystem_tool: BetaLocalFilesystemMemoryTool |
| 496 |
) -> None: |
| 497 |
sync_local_filesystem_tool.create( |
| 498 |
BetaMemoryTool20250818CreateCommand(command="create", file_text="File 1", path="/memories/file1.txt") |
| 499 |
) |
| 500 |
|
| 501 |
sync_local_filesystem_tool.create( |
| 502 |
BetaMemoryTool20250818CreateCommand(command="create", file_text="File 2", path="/memories/file2.txt") |
| 503 |
) |
| 504 |
|
| 505 |
with pytest.raises(ToolError, match="The destination /memories/file2.txt already exists"): |
| 506 |
sync_local_filesystem_tool.rename( |
| 507 |
BetaMemoryTool20250818RenameCommand( |
| 508 |
command="rename", old_path="/memories/file1.txt", new_path="/memories/file2.txt" |
| 509 |
) |
| 510 |
) |
| 511 |
|
| 512 |
def test_path_validation_reject_paths_not_starting_with_memories( |
| 513 |
self, sync_local_filesystem_tool: BetaLocalFilesystemMemoryTool |
| 514 |
) -> None: |
| 515 |
with pytest.raises(ToolError, match="Path must start with /memories"): |
| 516 |
sync_local_filesystem_tool.create( |
| 517 |
BetaMemoryTool20250818CreateCommand(command="create", file_text="Invalid", path="/invalid/path.txt") |
| 518 |
) |
| 519 |
|
| 520 |
def test_path_validation_reject_paths_trying_to_escape_memories( |
| 521 |
self, sync_local_filesystem_tool: BetaLocalFilesystemMemoryTool |
| 522 |
) -> None: |
| 523 |
with pytest.raises(ToolError, match="Path /memories/../../../etc/passwd would escape /memories directory"): |
| 524 |
sync_local_filesystem_tool.create( |
| 525 |
BetaMemoryTool20250818CreateCommand( |
| 526 |
command="create", file_text="Escape attempt", path="/memories/../../../etc/passwd" |
| 527 |
) |
| 528 |
) |
| 529 |
|
| 530 |
def test_symlink_validation_reject_symlink_pointing_outside_memories( |
| 531 |
self, sync_local_filesystem_tool: BetaLocalFilesystemMemoryTool |
| 532 |
) -> None: |
| 533 |
with tempfile.TemporaryDirectory() as outside_dir: |
| 534 |
Path(outside_dir, "secret.txt").write_text("sensitive data", encoding="utf-8") |
| 535 |
|
| 536 |
memories_path = sync_local_filesystem_tool.memory_root |
| 537 |
symlink_path = memories_path / "escape_link" |
| 538 |
|
| 539 |
os.symlink(outside_dir, symlink_path, target_is_directory=True) |
| 540 |
|
| 541 |
with pytest.raises(ToolError, match="Path .* would escape /memories directory"): |
| 542 |
sync_local_filesystem_tool.view( |
| 543 |
BetaMemoryTool20250818ViewCommand(command="view", path="/memories/escape_link/secret.txt") |
| 544 |
) |
| 545 |
|
| 546 |
def test_symlink_validation_reject_creating_files_through_symlink_pointing_outside( |
| 547 |
self, sync_local_filesystem_tool: BetaLocalFilesystemMemoryTool |
| 548 |
) -> None: |
| 549 |
with tempfile.TemporaryDirectory() as outside_dir: |
| 550 |
memories_path = sync_local_filesystem_tool.memory_root |
| 551 |
symlink_path = memories_path / "bad_link" |
| 552 |
|
| 553 |
os.symlink(outside_dir, symlink_path, target_is_directory=True) |
| 554 |
|
| 555 |
with pytest.raises(ToolError, match="Path .* would escape /memories directory"): |
| 556 |
sync_local_filesystem_tool.create( |
| 557 |
BetaMemoryTool20250818CreateCommand( |
| 558 |
command="create", file_text="malicious content", path="/memories/bad_link/hacked.txt" |
| 559 |
) |
| 560 |
) |
| 561 |
|
| 562 |
def test_symlink_validation_reject_parent_directory_that_is_symlink_pointing_outside( |
| 563 |
self, sync_local_filesystem_tool: BetaLocalFilesystemMemoryTool |
| 564 |
) -> None: |
| 565 |
with tempfile.TemporaryDirectory() as outside_dir: |
| 566 |
memories_path = sync_local_filesystem_tool.memory_root |
| 567 |
symlink_dir_path = memories_path / "subdir" |
| 568 |
|
| 569 |
os.symlink(outside_dir, symlink_dir_path, target_is_directory=True) |
| 570 |
|
| 571 |
with pytest.raises(ToolError, match="Path .* would escape /memories directory"): |
| 572 |
sync_local_filesystem_tool.create( |
| 573 |
BetaMemoryTool20250818CreateCommand( |
| 574 |
command="create", file_text="content", path="/memories/subdir/nested/file.txt" |
| 575 |
) |
| 576 |
) |
| 577 |
|
| 578 |
|
| 579 |
class TestBetaAsyncLocalFilesystemMemoryTool: |
| 580 |
async def test_mkdir_parents_not_world_writable_under_permissive_umask(self) -> None: |
| 581 |
if os.name != "posix": |
| 582 |
pytest.skip("POSIX mode bits only") |
| 583 |
old_umask = os.umask(0) |
| 584 |
try: |
| 585 |
with tempfile.TemporaryDirectory() as tmp: |
| 586 |
base_path = Path(tmp) / "nested" / "memory" |
| 587 |
tool = BetaAsyncLocalFilesystemMemoryTool(base_path=str(base_path)) |
| 588 |
await tool._ensure_memory_root() # triggers lazy directory creation |
| 589 |
memory_root = Path(str(tool.memory_root)) |
| 590 |
for directory in (base_path.parent, base_path, memory_root): |
| 591 |
mode = stat.S_IMODE(directory.stat().st_mode) |
| 592 |
assert mode == 0o700, f"{directory} has mode {oct(mode)}, expected 0o700" |
| 593 |
assert not (mode & 0o077), f"{directory} is group/other-accessible: {oct(mode)}" |
| 594 |
finally: |
| 595 |
os.umask(old_umask) |
| 596 |
|
| 597 |
async def test_create(self, async_local_filesystem_tool: BetaAsyncLocalFilesystemMemoryTool) -> None: |
| 598 |
result = await async_local_filesystem_tool.create( |
| 599 |
BetaMemoryTool20250818CreateCommand( |
| 600 |
command="create", |
| 601 |
file_text="Hello, World!", |
| 602 |
path="/memories/test_file.txt", |
| 603 |
) |
| 604 |
) |
| 605 |
assert result == "File created successfully at: /memories/test_file.txt" |
| 606 |
dir_snapshot = get_directory_snapshot(str(async_local_filesystem_tool.base_path)) |
| 607 |
assert dir_snapshot == {"memories/test_file.txt": "Hello, World!"} |
| 608 |
|
| 609 |
async def test_create_nested_directories( |
| 610 |
self, async_local_filesystem_tool: BetaAsyncLocalFilesystemMemoryTool |
| 611 |
) -> None: |
| 612 |
result = await async_local_filesystem_tool.create( |
| 613 |
BetaMemoryTool20250818CreateCommand( |
| 614 |
command="create", |
| 615 |
file_text="Nested file", |
| 616 |
path="/memories/deep/nested/file.txt", |
| 617 |
) |
| 618 |
) |
| 619 |
assert result == "File created successfully at: /memories/deep/nested/file.txt" |
| 620 |
dir_snapshot = get_directory_snapshot(str(async_local_filesystem_tool.base_path)) |
| 621 |
assert dir_snapshot == {"memories/deep/nested/file.txt": "Nested file"} |
| 622 |
|
| 623 |
async def test_create_error_if_file_already_exists( |
| 624 |
self, async_local_filesystem_tool: BetaAsyncLocalFilesystemMemoryTool |
| 625 |
) -> None: |
| 626 |
await async_local_filesystem_tool.create( |
| 627 |
BetaMemoryTool20250818CreateCommand( |
| 628 |
command="create", |
| 629 |
file_text="Original", |
| 630 |
path="/memories/existing.txt", |
| 631 |
) |
| 632 |
) |
| 633 |
|
| 634 |
with pytest.raises(ToolError, match="File /memories/existing.txt already exists"): |
| 635 |
await async_local_filesystem_tool.create( |
| 636 |
BetaMemoryTool20250818CreateCommand( |
| 637 |
command="create", |
| 638 |
file_text="New", |
| 639 |
path="/memories/existing.txt", |
| 640 |
) |
| 641 |
) |
| 642 |
|
| 643 |
async def test_view_file(self, async_local_filesystem_tool: BetaAsyncLocalFilesystemMemoryTool) -> None: |
| 644 |
await async_local_filesystem_tool.create( |
| 645 |
BetaMemoryTool20250818CreateCommand( |
| 646 |
command="create", |
| 647 |
file_text="Line 1\nLine 2\nLine 3", |
| 648 |
path="/memories/view_test.txt", |
| 649 |
) |
| 650 |
) |
| 651 |
|
| 652 |
result = await async_local_filesystem_tool.view( |
| 653 |
BetaMemoryTool20250818ViewCommand(command="view", path="/memories/view_test.txt") |
| 654 |
) |
| 655 |
assert ( |
| 656 |
result |
| 657 |
== "Here's the content of /memories/view_test.txt with line numbers:\n 1\tLine 1\n 2\tLine 2\n 3\tLine 3" |
| 658 |
) |
| 659 |
|
| 660 |
async def test_view_directory(self, async_local_filesystem_tool: BetaAsyncLocalFilesystemMemoryTool) -> None: |
| 661 |
await async_local_filesystem_tool.create( |
| 662 |
BetaMemoryTool20250818CreateCommand( |
| 663 |
command="create", |
| 664 |
file_text="File 1", |
| 665 |
path="/memories/dir/file1.txt", |
| 666 |
) |
| 667 |
) |
| 668 |
|
| 669 |
await async_local_filesystem_tool.create( |
| 670 |
BetaMemoryTool20250818CreateCommand( |
| 671 |
command="create", |
| 672 |
file_text="File 2", |
| 673 |
path="/memories/dir/file2.txt", |
| 674 |
) |
| 675 |
) |
| 676 |
|
| 677 |
result = await async_local_filesystem_tool.view( |
| 678 |
BetaMemoryTool20250818ViewCommand(command="view", path="/memories/dir") |
| 679 |
) |
| 680 |
|
| 681 |
assert "Here're the files and directories up to 2 levels deep in /memories/dir" in result |
| 682 |
assert "/memories/dir" in result |
| 683 |
assert "/memories/dir/file1.txt" in result |
| 684 |
assert "/memories/dir/file2.txt" in result |
| 685 |
|
| 686 |
async def test_view_file_with_range(self, async_local_filesystem_tool: BetaAsyncLocalFilesystemMemoryTool) -> None: |
| 687 |
await async_local_filesystem_tool.create( |
| 688 |
BetaMemoryTool20250818CreateCommand( |
| 689 |
command="create", |
| 690 |
file_text="Line 1\nLine 2\nLine 3\nLine 4\nLine 5", |
| 691 |
path="/memories/range_test.txt", |
| 692 |
) |
| 693 |
) |
| 694 |
|
| 695 |
result = await async_local_filesystem_tool.view( |
| 696 |
BetaMemoryTool20250818ViewCommand(command="view", path="/memories/range_test.txt", view_range=[2, 4]) |
| 697 |
) |
| 698 |
|
| 699 |
assert ( |
| 700 |
result |
| 701 |
== "Here's the content of /memories/range_test.txt with line numbers:\n 2\tLine 2\n 3\tLine 3\n 4\tLine 4" |
| 702 |
) |
| 703 |
|
| 704 |
async def test_view_error_for_non_existent_file( |
| 705 |
self, async_local_filesystem_tool: BetaAsyncLocalFilesystemMemoryTool |
| 706 |
) -> None: |
| 707 |
with pytest.raises( |
| 708 |
ToolError, match="The path /memories/nonexistent.txt does not exist. Please provide a valid path." |
| 709 |
): |
| 710 |
await async_local_filesystem_tool.view( |
| 711 |
BetaMemoryTool20250818ViewCommand(command="view", path="/memories/nonexistent.txt") |
| 712 |
) |
| 713 |
|
| 714 |
async def test_view_error_for_files_with_too_many_lines( |
| 715 |
self, async_local_filesystem_tool: BetaAsyncLocalFilesystemMemoryTool |
| 716 |
) -> None: |
| 717 |
too_many_lines = "\n".join(["line"] * 1000000) |
| 718 |
await async_local_filesystem_tool.create( |
| 719 |
BetaMemoryTool20250818CreateCommand( |
| 720 |
command="create", |
| 721 |
file_text=too_many_lines, |
| 722 |
path="/memories/huge.txt", |
| 723 |
) |
| 724 |
) |
| 725 |
|
| 726 |
with pytest.raises(ToolError, match=r"exceeds maximum line limit of 999,999 lines"): |
| 727 |
await async_local_filesystem_tool.view( |
| 728 |
BetaMemoryTool20250818ViewCommand(command="view", path="/memories/huge.txt") |
| 729 |
) |
| 730 |
|
| 731 |
async def test_str_replace(self, async_local_filesystem_tool: BetaAsyncLocalFilesystemMemoryTool) -> None: |
| 732 |
await async_local_filesystem_tool.create( |
| 733 |
BetaMemoryTool20250818CreateCommand( |
| 734 |
command="create", |
| 735 |
file_text="Line 1\nOld Text\nLine 3", |
| 736 |
path="/memories/replace_test.txt", |
| 737 |
) |
| 738 |
) |
| 739 |
|
| 740 |
result = await async_local_filesystem_tool.str_replace( |
| 741 |
BetaMemoryTool20250818StrReplaceCommand( |
| 742 |
command="str_replace", |
| 743 |
path="/memories/replace_test.txt", |
| 744 |
old_str="Old Text", |
| 745 |
new_str="New Text", |
| 746 |
) |
| 747 |
) |
| 748 |
|
| 749 |
assert ( |
| 750 |
result |
| 751 |
== "The memory file has been edited. Here is the snippet showing the change (with line numbers):\n 1\tLine 1\n 2\tNew Text\n 3\tLine 3" |
| 752 |
) |
| 753 |
|
| 754 |
dir_snapshot = get_directory_snapshot(str(async_local_filesystem_tool.base_path)) |
| 755 |
assert dir_snapshot == {"memories/replace_test.txt": "Line 1\nNew Text\nLine 3"} |
| 756 |
|
| 757 |
async def test_str_replace_error_when_string_not_found( |
| 758 |
self, async_local_filesystem_tool: BetaAsyncLocalFilesystemMemoryTool |
| 759 |
) -> None: |
| 760 |
await async_local_filesystem_tool.create( |
| 761 |
BetaMemoryTool20250818CreateCommand( |
| 762 |
command="create", |
| 763 |
file_text="Hello, World!", |
| 764 |
path="/memories/replace_test.txt", |
| 765 |
) |
| 766 |
) |
| 767 |
|
| 768 |
with pytest.raises( |
| 769 |
ToolError, |
| 770 |
match="No replacement was performed, old_str `NotFound` did not appear verbatim in /memories/replace_test.txt.", |
| 771 |
): |
| 772 |
await async_local_filesystem_tool.str_replace( |
| 773 |
BetaMemoryTool20250818StrReplaceCommand( |
| 774 |
command="str_replace", |
| 775 |
path="/memories/replace_test.txt", |
| 776 |
old_str="NotFound", |
| 777 |
new_str="Python", |
| 778 |
) |
| 779 |
) |
| 780 |
|
| 781 |
async def test_str_replace_error_when_string_appears_multiple_times( |
| 782 |
self, async_local_filesystem_tool: BetaAsyncLocalFilesystemMemoryTool |
| 783 |
) -> None: |
| 784 |
await async_local_filesystem_tool.create( |
| 785 |
BetaMemoryTool20250818CreateCommand( |
| 786 |
command="create", |
| 787 |
file_text="Hello\nMiddle\nHello", |
| 788 |
path="/memories/replace_test.txt", |
| 789 |
) |
| 790 |
) |
| 791 |
|
| 792 |
with pytest.raises( |
| 793 |
ToolError, |
| 794 |
match=r"No replacement was performed\. Multiple occurrences of old_str `Hello` in lines: 1, 3\. Please ensure it is unique", |
| 795 |
): |
| 796 |
await async_local_filesystem_tool.str_replace( |
| 797 |
BetaMemoryTool20250818StrReplaceCommand( |
| 798 |
command="str_replace", |
| 799 |
path="/memories/replace_test.txt", |
| 800 |
old_str="Hello", |
| 801 |
new_str="Hi", |
| 802 |
) |
| 803 |
) |
| 804 |
|
| 805 |
async def test_str_replace_error_for_non_existent_file( |
| 806 |
self, async_local_filesystem_tool: BetaAsyncLocalFilesystemMemoryTool |
| 807 |
) -> None: |
| 808 |
with pytest.raises( |
| 809 |
ToolError, match="The path /memories/nonexistent.txt does not exist. Please provide a valid path." |
| 810 |
): |
| 811 |
await async_local_filesystem_tool.str_replace( |
| 812 |
BetaMemoryTool20250818StrReplaceCommand( |
| 813 |
command="str_replace", |
| 814 |
path="/memories/nonexistent.txt", |
| 815 |
old_str="old", |
| 816 |
new_str="new", |
| 817 |
) |
| 818 |
) |
| 819 |
|
| 820 |
async def test_insert(self, async_local_filesystem_tool: BetaAsyncLocalFilesystemMemoryTool) -> None: |
| 821 |
await async_local_filesystem_tool.create( |
| 822 |
BetaMemoryTool20250818CreateCommand( |
| 823 |
command="create", file_text="Line 1\nLine 2", path="/memories/insert_test.txt" |
| 824 |
) |
| 825 |
) |
| 826 |
|
| 827 |
result = await async_local_filesystem_tool.insert( |
| 828 |
BetaMemoryTool20250818InsertCommand( |
| 829 |
command="insert", path="/memories/insert_test.txt", insert_line=1, insert_text="Inserted Line" |
| 830 |
) |
| 831 |
) |
| 832 |
assert result == "The file /memories/insert_test.txt has been edited." |
| 833 |
|
| 834 |
dir_snapshot = get_directory_snapshot(str(async_local_filesystem_tool.base_path)) |
| 835 |
assert dir_snapshot == {"memories/insert_test.txt": "Line 1\nInserted Line\nLine 2\n"} |
| 836 |
|
| 837 |
async def test_insert_at_beginning_of_file( |
| 838 |
self, async_local_filesystem_tool: BetaAsyncLocalFilesystemMemoryTool |
| 839 |
) -> None: |
| 840 |
await async_local_filesystem_tool.create( |
| 841 |
BetaMemoryTool20250818CreateCommand( |
| 842 |
command="create", file_text="Line 1\nLine 2", path="/memories/insert_test.txt" |
| 843 |
) |
| 844 |
) |
| 845 |
|
| 846 |
await async_local_filesystem_tool.insert( |
| 847 |
BetaMemoryTool20250818InsertCommand( |
| 848 |
command="insert", path="/memories/insert_test.txt", insert_line=0, insert_text="First Line" |
| 849 |
) |
| 850 |
) |
| 851 |
|
| 852 |
dir_snapshot = get_directory_snapshot(str(async_local_filesystem_tool.base_path)) |
| 853 |
assert dir_snapshot == {"memories/insert_test.txt": "First Line\nLine 1\nLine 2\n"} |
| 854 |
|
| 855 |
async def test_insert_at_end_of_file(self, async_local_filesystem_tool: BetaAsyncLocalFilesystemMemoryTool) -> None: |
| 856 |
await async_local_filesystem_tool.create( |
| 857 |
BetaMemoryTool20250818CreateCommand( |
| 858 |
command="create", file_text="Line 1\nLine 2", path="/memories/insert_test.txt" |
| 859 |
) |
| 860 |
) |
| 861 |
|
| 862 |
await async_local_filesystem_tool.insert( |
| 863 |
BetaMemoryTool20250818InsertCommand( |
| 864 |
command="insert", path="/memories/insert_test.txt", insert_line=2, insert_text="Last Line" |
| 865 |
) |
| 866 |
) |
| 867 |
|
| 868 |
dir_snapshot = get_directory_snapshot(str(async_local_filesystem_tool.base_path)) |
| 869 |
assert dir_snapshot == {"memories/insert_test.txt": "Line 1\nLine 2\nLast Line\n"} |
| 870 |
|
| 871 |
async def test_view_preserves_crlf_line_endings( |
| 872 |
self, async_local_filesystem_tool: BetaAsyncLocalFilesystemMemoryTool |
| 873 |
) -> None: |
| 874 |
memory_root = Path(str(async_local_filesystem_tool.memory_root)) |
| 875 |
memory_root.mkdir(parents=True, exist_ok=True) |
| 876 |
(memory_root / "crlf.txt").write_bytes(b"line1\r\nline2\r\nline3\r\n") |
| 877 |
|
| 878 |
result = await async_local_filesystem_tool.view( |
| 879 |
BetaMemoryTool20250818ViewCommand(command="view", path="/memories/crlf.txt") |
| 880 |
) |
| 881 |
assert ( |
| 882 |
result |
| 883 |
== "Here's the content of /memories/crlf.txt with line numbers:\n 1\tline1\r\n 2\tline2\r\n 3\tline3\r\n 4\t" |
| 884 |
) |
| 885 |
|
| 886 |
async def test_str_replace_preserves_crlf_line_endings( |
| 887 |
self, async_local_filesystem_tool: BetaAsyncLocalFilesystemMemoryTool |
| 888 |
) -> None: |
| 889 |
memory_root = Path(str(async_local_filesystem_tool.memory_root)) |
| 890 |
memory_root.mkdir(parents=True, exist_ok=True) |
| 891 |
path = memory_root / "crlf.txt" |
| 892 |
path.write_bytes(b"line1\r\nline2\r\nline3\r\n") |
| 893 |
|
| 894 |
await async_local_filesystem_tool.str_replace( |
| 895 |
BetaMemoryTool20250818StrReplaceCommand( |
| 896 |
command="str_replace", path="/memories/crlf.txt", old_str="line2", new_str="LINE2" |
| 897 |
) |
| 898 |
) |
| 899 |
assert path.read_bytes() == b"line1\r\nLINE2\r\nline3\r\n" |
| 900 |
|
| 901 |
async def test_insert_preserves_crlf_line_endings( |
| 902 |
self, async_local_filesystem_tool: BetaAsyncLocalFilesystemMemoryTool |
| 903 |
) -> None: |
| 904 |
memory_root = Path(str(async_local_filesystem_tool.memory_root)) |
| 905 |
memory_root.mkdir(parents=True, exist_ok=True) |
| 906 |
path = memory_root / "crlf.txt" |
| 907 |
path.write_bytes(b"line1\r\nline2\r\nline3\r\n") |
| 908 |
|
| 909 |
await async_local_filesystem_tool.insert( |
| 910 |
BetaMemoryTool20250818InsertCommand( |
| 911 |
command="insert", path="/memories/crlf.txt", insert_line=1, insert_text="Inserted" |
| 912 |
) |
| 913 |
) |
| 914 |
assert path.read_bytes() == b"line1\r\nInserted\nline2\r\nline3\r\n" |
| 915 |
|
| 916 |
async def test_insert_error_for_non_existent_file( |
| 917 |
self, async_local_filesystem_tool: BetaAsyncLocalFilesystemMemoryTool |
| 918 |
) -> None: |
| 919 |
with pytest.raises( |
| 920 |
ToolError, match="The path /memories/nonexistent.txt does not exist. Please provide a valid path." |
| 921 |
): |
| 922 |
await async_local_filesystem_tool.insert( |
| 923 |
BetaMemoryTool20250818InsertCommand( |
| 924 |
command="insert", path="/memories/nonexistent.txt", insert_line=0, insert_text="text" |
| 925 |
) |
| 926 |
) |
| 927 |
|
| 928 |
async def test_insert_error_for_invalid_insert_line( |
| 929 |
self, async_local_filesystem_tool: BetaAsyncLocalFilesystemMemoryTool |
| 930 |
) -> None: |
| 931 |
await async_local_filesystem_tool.create( |
| 932 |
BetaMemoryTool20250818CreateCommand( |
| 933 |
command="create", file_text="Line 1\nLine 2", path="/memories/insert_test.txt" |
| 934 |
) |
| 935 |
) |
| 936 |
|
| 937 |
with pytest.raises( |
| 938 |
ToolError, |
| 939 |
match=r"Invalid `insert_line` parameter: 10\. It should be within the range \[0, 2\]", |
| 940 |
): |
| 941 |
await async_local_filesystem_tool.insert( |
| 942 |
BetaMemoryTool20250818InsertCommand( |
| 943 |
command="insert", path="/memories/insert_test.txt", insert_line=10, insert_text="text" |
| 944 |
) |
| 945 |
) |
| 946 |
|
| 947 |
async def test_delete(self, async_local_filesystem_tool: BetaAsyncLocalFilesystemMemoryTool) -> None: |
| 948 |
await async_local_filesystem_tool.create( |
| 949 |
BetaMemoryTool20250818CreateCommand( |
| 950 |
command="create", file_text="To be deleted", path="/memories/delete_me.txt" |
| 951 |
) |
| 952 |
) |
| 953 |
|
| 954 |
result = await async_local_filesystem_tool.delete( |
| 955 |
BetaMemoryTool20250818DeleteCommand(command="delete", path="/memories/delete_me.txt") |
| 956 |
) |
| 957 |
assert result == "Successfully deleted /memories/delete_me.txt" |
| 958 |
|
| 959 |
dir_snapshot = get_directory_snapshot(str(async_local_filesystem_tool.base_path)) |
| 960 |
assert dir_snapshot == {} |
| 961 |
|
| 962 |
async def test_delete_directory(self, async_local_filesystem_tool: BetaAsyncLocalFilesystemMemoryTool) -> None: |
| 963 |
await async_local_filesystem_tool.create( |
| 964 |
BetaMemoryTool20250818CreateCommand(command="create", file_text="Content", path="/memories/subdir/file.txt") |
| 965 |
) |
| 966 |
|
| 967 |
result = await async_local_filesystem_tool.delete( |
| 968 |
BetaMemoryTool20250818DeleteCommand(command="delete", path="/memories/subdir") |
| 969 |
) |
| 970 |
assert result == "Successfully deleted /memories/subdir" |
| 971 |
|
| 972 |
dir_snapshot = get_directory_snapshot(str(async_local_filesystem_tool.base_path)) |
| 973 |
assert dir_snapshot == {} |
| 974 |
|
| 975 |
async def test_delete_error_when_file_not_found( |
| 976 |
self, async_local_filesystem_tool: BetaAsyncLocalFilesystemMemoryTool |
| 977 |
) -> None: |
| 978 |
with pytest.raises(ToolError, match="The path /memories/nonexistent.txt does not exist"): |
| 979 |
await async_local_filesystem_tool.delete( |
| 980 |
BetaMemoryTool20250818DeleteCommand(command="delete", path="/memories/nonexistent.txt") |
| 981 |
) |
| 982 |
|
| 983 |
async def test_delete_not_allow_deleting_memories_directory( |
| 984 |
self, async_local_filesystem_tool: BetaAsyncLocalFilesystemMemoryTool |
| 985 |
) -> None: |
| 986 |
with pytest.raises(ToolError, match="Cannot delete the /memories directory itself"): |
| 987 |
await async_local_filesystem_tool.delete( |
| 988 |
BetaMemoryTool20250818DeleteCommand(command="delete", path="/memories") |
| 989 |
) |
| 990 |
|
| 991 |
async def test_rename(self, async_local_filesystem_tool: BetaAsyncLocalFilesystemMemoryTool) -> None: |
| 992 |
await async_local_filesystem_tool.create( |
| 993 |
BetaMemoryTool20250818CreateCommand( |
| 994 |
command="create", file_text="Original content", path="/memories/old_name.txt" |
| 995 |
) |
| 996 |
) |
| 997 |
|
| 998 |
result = await async_local_filesystem_tool.rename( |
| 999 |
BetaMemoryTool20250818RenameCommand( |
| 1000 |
command="rename", old_path="/memories/old_name.txt", new_path="/memories/new_name.txt" |
| 1001 |
) |
| 1002 |
) |
| 1003 |
assert result == "Successfully renamed /memories/old_name.txt to /memories/new_name.txt" |
| 1004 |
|
| 1005 |
dir_snapshot = get_directory_snapshot(str(async_local_filesystem_tool.base_path)) |
| 1006 |
assert dir_snapshot == {"memories/new_name.txt": "Original content"} |
| 1007 |
|
| 1008 |
async def test_rename_to_nested_path(self, async_local_filesystem_tool: BetaAsyncLocalFilesystemMemoryTool) -> None: |
| 1009 |
await async_local_filesystem_tool.create( |
| 1010 |
BetaMemoryTool20250818CreateCommand(command="create", file_text="Content", path="/memories/file.txt") |
| 1011 |
) |
| 1012 |
|
| 1013 |
result = await async_local_filesystem_tool.rename( |
| 1014 |
BetaMemoryTool20250818RenameCommand( |
| 1015 |
command="rename", old_path="/memories/file.txt", new_path="/memories/nested/dir/file.txt" |
| 1016 |
) |
| 1017 |
) |
| 1018 |
assert result == "Successfully renamed /memories/file.txt to /memories/nested/dir/file.txt" |
| 1019 |
|
| 1020 |
dir_snapshot = get_directory_snapshot(str(async_local_filesystem_tool.base_path)) |
| 1021 |
assert dir_snapshot == {"memories/nested/dir/file.txt": "Content"} |
| 1022 |
|
| 1023 |
async def test_rename_error_when_source_not_found( |
| 1024 |
self, async_local_filesystem_tool: BetaAsyncLocalFilesystemMemoryTool |
| 1025 |
) -> None: |
| 1026 |
with pytest.raises(ToolError, match="The path /memories/nonexistent.txt does not exist"): |
| 1027 |
await async_local_filesystem_tool.rename( |
| 1028 |
BetaMemoryTool20250818RenameCommand( |
| 1029 |
command="rename", old_path="/memories/nonexistent.txt", new_path="/memories/new.txt" |
| 1030 |
) |
| 1031 |
) |
| 1032 |
|
| 1033 |
async def test_rename_error_when_destination_exists( |
| 1034 |
self, async_local_filesystem_tool: BetaAsyncLocalFilesystemMemoryTool |
| 1035 |
) -> None: |
| 1036 |
await async_local_filesystem_tool.create( |
| 1037 |
BetaMemoryTool20250818CreateCommand(command="create", file_text="File 1", path="/memories/file1.txt") |
| 1038 |
) |
| 1039 |
|
| 1040 |
await async_local_filesystem_tool.create( |
| 1041 |
BetaMemoryTool20250818CreateCommand(command="create", file_text="File 2", path="/memories/file2.txt") |
| 1042 |
) |
| 1043 |
|
| 1044 |
with pytest.raises(ToolError, match="The destination /memories/file2.txt already exists"): |
| 1045 |
await async_local_filesystem_tool.rename( |
| 1046 |
BetaMemoryTool20250818RenameCommand( |
| 1047 |
command="rename", old_path="/memories/file1.txt", new_path="/memories/file2.txt" |
| 1048 |
) |
| 1049 |
) |
| 1050 |
|
| 1051 |
async def test_path_validation_reject_paths_not_starting_with_memories( |
| 1052 |
self, async_local_filesystem_tool: BetaAsyncLocalFilesystemMemoryTool |
| 1053 |
) -> None: |
| 1054 |
with pytest.raises(ToolError, match="Path must start with /memories"): |
| 1055 |
await async_local_filesystem_tool.create( |
| 1056 |
BetaMemoryTool20250818CreateCommand(command="create", file_text="Invalid", path="/invalid/path.txt") |
| 1057 |
) |
| 1058 |
|
| 1059 |
async def test_path_validation_reject_paths_trying_to_escape_memories( |
| 1060 |
self, async_local_filesystem_tool: BetaAsyncLocalFilesystemMemoryTool |
| 1061 |
) -> None: |
| 1062 |
with pytest.raises(ToolError, match="Path /memories/../../../etc/passwd would escape /memories directory"): |
| 1063 |
await async_local_filesystem_tool.create( |
| 1064 |
BetaMemoryTool20250818CreateCommand( |
| 1065 |
command="create", file_text="Escape attempt", path="/memories/../../../etc/passwd" |
| 1066 |
) |
| 1067 |
) |
| 1068 |
|
| 1069 |
async def test_validate_path_returns_resolved_path_not_symlink_target( |
| 1070 |
self, async_local_filesystem_tool: BetaAsyncLocalFilesystemMemoryTool |
| 1071 |
) -> None: |
| 1072 |
"""_validate_path must return the resolved path so that subsequent file |
| 1073 |
operations hit the real location, not the (potentially swappable) symlink. |
| 1074 |
|
| 1075 |
Without this fix, an attacker could: |
| 1076 |
1. Create /memories/link -> /memories/legit (passes validation) |
| 1077 |
2. Swap /memories/link -> /etc between validation and the file operation |
| 1078 |
3. The file operation would follow the new symlink target |
| 1079 |
""" |
| 1080 |
memories_path = Path(str(async_local_filesystem_tool.memory_root)) |
| 1081 |
memories_path.mkdir(parents=True, exist_ok=True) |
| 1082 |
|
| 1083 |
# Create a real directory inside memories and a symlink pointing to it |
| 1084 |
legit_dir = memories_path / "legit" |
| 1085 |
legit_dir.mkdir() |
| 1086 |
(legit_dir / "file.txt").write_text("content", encoding="utf-8") |
| 1087 |
|
| 1088 |
link_path = memories_path / "link" |
| 1089 |
os.symlink(legit_dir, link_path, target_is_directory=True) |
| 1090 |
|
| 1091 |
# _validate_path should return the resolved real path, not the symlink path |
| 1092 |
result = await async_local_filesystem_tool._validate_path("/memories/link/file.txt") |
| 1093 |
result_str = str(result) |
| 1094 |
|
| 1095 |
# The returned path should point to the resolved location (under legit/), |
| 1096 |
# not through the symlink |
| 1097 |
assert "link" not in result_str, f"_validate_path returned unresolved symlink path: {result_str}" |
| 1098 |
assert str(legit_dir.resolve()) in result_str |
| 1099 |
|
| 1100 |
async def test_symlink_validation_reject_symlink_pointing_outside_memories( |
| 1101 |
self, async_local_filesystem_tool: BetaAsyncLocalFilesystemMemoryTool |
| 1102 |
) -> None: |
| 1103 |
with tempfile.TemporaryDirectory() as outside_dir: |
| 1104 |
Path(outside_dir, "secret.txt").write_text("sensitive data", encoding="utf-8") |
| 1105 |
|
| 1106 |
memories_path = Path(str(async_local_filesystem_tool.memory_root)) |
| 1107 |
memories_path.mkdir(parents=True, exist_ok=True) |
| 1108 |
symlink_path = memories_path / "escape_link" |
| 1109 |
|
| 1110 |
os.symlink(outside_dir, symlink_path, target_is_directory=True) |
| 1111 |
|
| 1112 |
with pytest.raises(ToolError, match="Path .* would escape /memories directory"): |
| 1113 |
await async_local_filesystem_tool.view( |
| 1114 |
BetaMemoryTool20250818ViewCommand(command="view", path="/memories/escape_link/secret.txt") |
| 1115 |
) |
| 1116 |
|
| 1117 |
async def test_symlink_validation_reject_creating_files_through_symlink_pointing_outside( |
| 1118 |
self, async_local_filesystem_tool: BetaAsyncLocalFilesystemMemoryTool |
| 1119 |
) -> None: |
| 1120 |
with tempfile.TemporaryDirectory() as outside_dir: |
| 1121 |
memories_path = Path(str(async_local_filesystem_tool.memory_root)) |
| 1122 |
memories_path.mkdir(parents=True, exist_ok=True) |
| 1123 |
symlink_path = memories_path / "bad_link" |
| 1124 |
|
| 1125 |
os.symlink(outside_dir, symlink_path, target_is_directory=True) |
| 1126 |
|
| 1127 |
with pytest.raises(ToolError, match="Path .* would escape /memories directory"): |
| 1128 |
await async_local_filesystem_tool.create( |
| 1129 |
BetaMemoryTool20250818CreateCommand( |
| 1130 |
command="create", file_text="malicious content", path="/memories/bad_link/hacked.txt" |
| 1131 |
) |
| 1132 |
) |
| 1133 |
|
| 1134 |
async def test_symlink_validation_reject_parent_directory_that_is_symlink_pointing_outside( |
| 1135 |
self, async_local_filesystem_tool: BetaAsyncLocalFilesystemMemoryTool |
| 1136 |
) -> None: |
| 1137 |
with tempfile.TemporaryDirectory() as outside_dir: |
| 1138 |
memories_path = Path(str(async_local_filesystem_tool.memory_root)) |
| 1139 |
memories_path.mkdir(parents=True, exist_ok=True) |
| 1140 |
symlink_dir_path = memories_path / "subdir" |
| 1141 |
|
| 1142 |
os.symlink(outside_dir, symlink_dir_path, target_is_directory=True) |
| 1143 |
|
| 1144 |
with pytest.raises(ToolError, match="Path .* would escape /memories directory"): |
| 1145 |
await async_local_filesystem_tool.create( |
| 1146 |
BetaMemoryTool20250818CreateCommand( |
| 1147 |
command="create", file_text="content", path="/memories/subdir/nested/file.txt" |
| 1148 |
) |
| 1149 |
) |