Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
test(memory): cover the symlink-to-root spelling of the delete guard
A symlink inside the store that points at the store root resolves to the
root, so it reaches the guard as a real path rather than as a spelling.
It is the one case that would notice if _validate_path ever stopped
resolving. Also records why the async guard drops to Path: AsyncPath.resolve()
is a coroutine, so awaiting it would compare an AsyncPath to a Path.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01JukbShxX2zFkfAXbNcbQ5F
  • Loading branch information
Kayvan-Zahiri and claude committed Sep 5, 2026
commit 4e66da21d2544db31ab8f50fbf0589d93f5f1bd7
3 changes: 3 additions & 0 deletions src/anthropic/lib/tools/_beta_builtin_memory_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,9 @@ async def delete(self, command: BetaMemoryTool20250818DeleteCommand) -> str:
await self._ensure_memory_root()
full_path = await self._validate_path(command.path)

# AsyncPath.resolve() is a coroutine, so the comparison drops to Path here
# the way _validate_path already does. Awaiting it instead would compare an
# AsyncPath against a Path and never match.
if Path(str(full_path)) == Path(str(self.memory_root)).resolve():
raise ToolError("Cannot delete the /memories directory itself")

Expand Down
36 changes: 36 additions & 0 deletions tests/lib/tools/memory_tools/test_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,24 @@ def test_delete_not_allow_deleting_memories_directory_via_alias(

assert get_directory_snapshot(temp_directory) == {"memories/subdir/a.txt": "keep me"}

def test_delete_not_allow_deleting_memories_directory_via_symlink(
self, sync_local_filesystem_tool: BetaLocalFilesystemMemoryTool
) -> None:
"""A symlink inside the store that points at the store root resolves to the
root, so it reaches the guard as a real path rather than as a spelling."""
sync_local_filesystem_tool.create(
BetaMemoryTool20250818CreateCommand(command="create", file_text="keep me", path="/memories/subdir/a.txt")
)
memories_path = sync_local_filesystem_tool.memory_root
os.symlink(memories_path, memories_path / "self", target_is_directory=True)

with pytest.raises(ToolError, match="Cannot delete the /memories directory itself"):
sync_local_filesystem_tool.delete(
BetaMemoryTool20250818DeleteCommand(command="delete", path="/memories/self")
)

assert (memories_path / "subdir" / "a.txt").read_text(encoding="utf-8") == "keep me"

def test_rename(self, sync_local_filesystem_tool: BetaLocalFilesystemMemoryTool) -> None:
sync_local_filesystem_tool.create(
BetaMemoryTool20250818CreateCommand(
Expand Down Expand Up @@ -1014,6 +1032,24 @@ async def test_delete_not_allow_deleting_memories_directory_via_alias(

assert get_directory_snapshot(temp_directory) == {"memories/subdir/a.txt": "keep me"}

async def test_delete_not_allow_deleting_memories_directory_via_symlink(
self, async_local_filesystem_tool: BetaAsyncLocalFilesystemMemoryTool
) -> None:
"""A symlink inside the store that points at the store root resolves to the
root, so it reaches the guard as a real path rather than as a spelling."""
await async_local_filesystem_tool.create(
BetaMemoryTool20250818CreateCommand(command="create", file_text="keep me", path="/memories/subdir/a.txt")
)
memories_path = Path(str(async_local_filesystem_tool.memory_root))
os.symlink(memories_path, memories_path / "self", target_is_directory=True)

with pytest.raises(ToolError, match="Cannot delete the /memories directory itself"):
await async_local_filesystem_tool.delete(
BetaMemoryTool20250818DeleteCommand(command="delete", path="/memories/self")
)

assert (memories_path / "subdir" / "a.txt").read_text(encoding="utf-8") == "keep me"

async def test_rename(self, async_local_filesystem_tool: BetaAsyncLocalFilesystemMemoryTool) -> None:
await async_local_filesystem_tool.create(
BetaMemoryTool20250818CreateCommand(
Expand Down