Skip to content

Build and run agents you can see, understand and trust.

fix(tool): unlink local symbolic links without following targets (#3006)

little-spoon77168e7274f00b
diff --git a/src/agentscope/tool/_builtin/_backend.py b/src/agentscope/tool/_builtin/_backend.py
index ecc2a21e7..4e139da11 100644
--- a/src/agentscope/tool/_builtin/_backend.py
+++ b/src/agentscope/tool/_builtin/_backend.py
@@ -1070,12 +1070,17 @@ class LocalBackend(BackendBase):
async def delete_path(self, path: str) -> None:
"""Delete a local file or directory tree.
- No-op if *path* does not exist.
+ Symbolic links are removed without following their targets,
+ including when the target no longer exists. No-op if *path*
+ does not exist.
Args:
path (`str`):
Path to delete.
"""
+ if os.path.islink(path):
+ os.remove(path)
+ return
if not os.path.exists(path):
return
if os.path.isdir(path):
diff --git a/tests/backend_local_test.py b/tests/backend_local_test.py
index c146d1526..3cd8692ad 100644
--- a/tests/backend_local_test.py
+++ b/tests/backend_local_test.py
@@ -369,6 +369,40 @@ class TestLocalBackendFilesystemHelpers(IsolatedAsyncioTestCase):
os.path.join(self.temp_dir.name, "missing"),
)
+ async def test_delete_path_symlinks_preserves_targets(self) -> None:
+ """Remove links, including dangling ones, without touching targets."""
+ for kind in ("file", "directory", "broken-file", "broken-directory"):
+ with self.subTest(kind=kind):
+ target = os.path.join(self.temp_dir.name, kind + "-target")
+ link = os.path.join(self.temp_dir.name, kind + "-link")
+ payload_path = target
+ if kind == "directory":
+ os.makedirs(target)
+ payload_path = os.path.join(target, "child.txt")
+ if not kind.startswith("broken"):
+ await self.backend.write_file(payload_path, b"preserve me")
+ try:
+ os.symlink(
+ target,
+ link,
+ target_is_directory="directory" in kind,
+ )
+ except OSError as exc:
+ if _IS_WINDOWS and exc.winerror == 1314:
+ self.skipTest("Creating symlinks requires permission")
+ raise
+
+ await self.backend.delete_path(link)
+ self.assertFalse(os.path.lexists(link))
+ if not kind.startswith("broken"):
+ self.assertEqual(
+ await self.backend.read_file(payload_path),
+ b"preserve me",
+ )
+ else:
+ self.assertFalse(os.path.exists(target))
+ await self.backend.delete_path(link)
+
@unittest.skipIf(
_IS_WINDOWS,