Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

feat(skill): 远程 import_local 下载与解压/import 经 to_thread 离环,避免堵 AgentServer 事件循环

Kinsam981302b7bc00
diff --git a/jiuwenswarm/server/runtime/skill/skill_manager.py b/jiuwenswarm/server/runtime/skill/skill_manager.py
index a041a0d7b0..09c9615b77 100644
--- a/jiuwenswarm/server/runtime/skill/skill_manager.py
+++ b/jiuwenswarm/server/runtime/skill/skill_manager.py
@@ -4105,17 +4105,21 @@ class SkillManager:
raise RuntimeError("下载文件校验失败(SHA256 不匹配)")
return body
- artifact_bytes = _download_with_requests()
+ artifact_bytes = await asyncio.to_thread(_download_with_requests)
with tempfile.TemporaryDirectory(prefix="jiuwenswarm_import_local_") as tmpdir:
tmp_path = Path(tmpdir)
- logger.info("[SkillManager] remote import extracting: url=%s tmpdir=%s", download_url, tmp_path)
- self._extract_archive_bytes_to_dir(artifact_bytes, tmp_path)
- skill_dir = self._locate_skill_dir(tmp_path)
- if skill_dir is None:
- return {"success": False, "detail": "下载内容不完整,未找到 SKILL.md"}
- logger.info("[SkillManager] remote import extracted: url=%s skill_dir=%s", download_url, skill_dir)
- return self._import_local_from_path(skill_dir, force=force, origin=download_url)
+
+ def _extract_and_import() -> dict:
+ logger.info("[SkillManager] remote import extracting: url=%s tmpdir=%s", download_url, tmp_path)
+ self._extract_archive_bytes_to_dir(artifact_bytes, tmp_path)
+ skill_dir = self._locate_skill_dir(tmp_path)
+ if skill_dir is None:
+ return {"success": False, "detail": "下载内容不完整,未找到 SKILL.md"}
+ logger.info("[SkillManager] remote import extracted: url=%s skill_dir=%s", download_url, skill_dir)
+ return self._import_local_from_path(skill_dir, force=force, origin=download_url)
+
+ return await asyncio.to_thread(_extract_and_import)
async def handle_skills_marketplace_add(self, params: dict) -> dict:
"""添加 marketplace 源.
diff --git a/tests/unit_tests/agentserver/test_skill_manager_path_safety.py b/tests/unit_tests/agentserver/test_skill_manager_path_safety.py
index e86f5a88d5..bf5cd0b5d5 100644
--- a/tests/unit_tests/agentserver/test_skill_manager_path_safety.py
+++ b/tests/unit_tests/agentserver/test_skill_manager_path_safety.py
@@ -1,3 +1,4 @@
+import asyncio
import io
import shutil
import zipfile
@@ -210,3 +211,71 @@ async def test_import_local_rejects_untrusted_remote_zip_host(tmp_path):
assert result["success"] is False
assert "example.com" in result["detail"]
+
+
+async def test_remote_import_download_goes_through_to_thread(monkeypatch, tmp_path):
+ """R-SB2: remote import_local download+extract+import must leave the event loop via to_thread."""
+ manager = SkillManager(workspace_dir=str(tmp_path / "workspace"))
+ zip_content = _zip_bytes(
+ {
+ "offload-demo/SKILL.md": (
+ "---\nname: offload-demo\ndescription: offload test\nversion: 1.0.0\n---\nbody\n"
+ ),
+ }
+ )
+ calls: list[object] = []
+ inside_to_thread = False
+ extract_inside_to_thread = False
+ import_inside_to_thread = False
+
+ real_extract = SkillManager._extract_archive_bytes_to_dir
+ real_import = SkillManager._import_local_from_path
+
+ def tracking_extract(self, body, dest_dir): # noqa: ANN001
+ nonlocal extract_inside_to_thread
+ extract_inside_to_thread = inside_to_thread
+ return real_extract(self, body, dest_dir)
+
+ def tracking_import(self, src, *, force, origin): # noqa: ANN001
+ nonlocal import_inside_to_thread
+ import_inside_to_thread = inside_to_thread
+ return real_import(self, src, force=force, origin=origin)
+
+ async def spy_to_thread(func, /, *args, **kwargs):
+ nonlocal inside_to_thread
+ calls.append(func)
+ name = getattr(func, "__name__", "")
+ if name == "_download_with_requests":
+ # Do not hit the network; still prove which callable was offloaded.
+ return zip_content
+ inside_to_thread = True
+ try:
+ return func(*args, **kwargs)
+ finally:
+ inside_to_thread = False
+
+ monkeypatch.setattr(asyncio, "to_thread", spy_to_thread)
+ monkeypatch.setattr(SkillManager, "_extract_archive_bytes_to_dir", tracking_extract)
+ monkeypatch.setattr(SkillManager, "_import_local_from_path", tracking_import)
+ monkeypatch.setattr(
+ SkillManager,
+ "_assert_import_local_download_url_allowed",
+ staticmethod(lambda download_url: None),
+ )
+
+ result = await manager._import_skill_from_remote_archive(
+ download_url="https://example.com/skills/offload-demo.zip",
+ force=False,
+ )
+
+ assert any(getattr(fn, "__name__", "") == "_download_with_requests" for fn in calls)
+ non_download = [
+ fn for fn in calls if getattr(fn, "__name__", "") != "_download_with_requests"
+ ]
+ assert non_download, "extract+import must also go through asyncio.to_thread"
+ assert extract_inside_to_thread is True
+ assert import_inside_to_thread is True
+ assert result["success"] is True
+ assert result["skill"]["name"] == "offload-demo"
+ assert (tmp_path / "workspace" / "skills" / "offload-demo" / "SKILL.md").is_file()