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
fix: 团队技能链接 prune 并发竞态容忍,回退解码改用 OEM 代码页
ZhouRZea933fa6bdc1
diff --git a/jiuwenswarm/agents/harness/team/team_manager.py b/jiuwenswarm/agents/harness/team/team_manager.pyindex a93f9b2cb8..365e7b35e4 100644--- a/jiuwenswarm/agents/harness/team/team_manager.py+++ b/jiuwenswarm/agents/harness/team/team_manager.py@@ -1425,7 +1425,11 @@ class TeamManager:logger.warning("[TeamManager] global_skills_dir does not exist: %s", global_skills_dir)return Falseoffload_link_sync(global_skills_dir, target)- logger.info("[TeamManager] Refreshed team shared skill links: session_id=%s target=%s", session_id, target)+ logger.info(+ "[TeamManager] Scheduled refresh of team shared skill links: session_id=%s target=%s",+ session_id,+ target,+ )return Truedef refresh_all_team_shared_skill_links(self) -> int:diff --git a/jiuwenswarm/agents/harness/team/team_skill_links.py b/jiuwenswarm/agents/harness/team/team_skill_links.pyindex ddd36fbc44..f088ddfe42 100644--- a/jiuwenswarm/agents/harness/team/team_skill_links.py+++ b/jiuwenswarm/agents/harness/team/team_skill_links.py@@ -118,12 +118,16 @@ def link_skill_dir(source: Path, target: Path) -> None:def remove_skill_dir_link(target: Path) -> None:- """Remove a skill directory link without deleting ordinary directories."""- if target.is_symlink():- target.unlink()- return- if _is_windows_reparse_point(target):- os.rmdir(target)+ try:+ if target.is_symlink():+ target.unlink()+ return+ if _is_windows_reparse_point(target):+ os.rmdir(target)+ except (FileNotFoundError, PermissionError):+ if not os.path.lexists(target):+ return+ raisedef _create_directory_link(target_path: Path, link_path: Path) -> None:@@ -174,8 +178,9 @@ def _create_windows_junction(target_path: Path, link_path: Path) -> None:``UnicodeDecodeError`` on Chinese Windows (GBK output vs UTF-8 decoding).The ``cmd.exe /c mklink /J`` fallback only runs when the private API is- unavailable or rejected the creation. Its output is decoded with the ANSI- code page (``mbcs``) and undecodable bytes are replaced, so localized output+ unavailable or rejected the creation. Its piped output is encoded in the+ OEM code page (not the ANSI ``mbcs`` page); decoding with ``oem`` keeps+ localized output readable on Western locales, and ``errors="replace"``can neither crash the reader thread nor raise while decoding."""create_junction = getattr(_winapi, "CreateJunction", None) if _winapi is not None else None@@ -186,8 +191,11 @@ def _create_windows_junction(target_path: Path, link_path: Path) -> None:except OSError:if path_exists_or_link(link_path):raise- # Fall through to cmd.exe, which reports a descriptive error for- # cases the native call rejected (e.g. a missing target).+ # Fall through to cmd.exe. Note mklink /J does not validate the+ # target: for a missing target it exits 0 and silently creates a+ # dangling junction (the native error above was the descriptive+ # one). Dangling links are removed by later prune_skill_dir_links+ # runs because the source entry is not a valid skill directory.cmd_path = os.path.join(os.environ.get("SystemRoot", r"C:\Windows"),"System32",@@ -198,7 +206,7 @@ def _create_windows_junction(target_path: Path, link_path: Path) -> None:capture_output=True,check=False,shell=False,- encoding="mbcs" if sys.platform == "win32" else "utf-8",+ encoding="oem" if sys.platform == "win32" else "utf-8",errors="replace",)if result.returncode != 0:diff --git a/tests/unit_tests/agentserver/test_team_skill_links.py b/tests/unit_tests/agentserver/test_team_skill_links.pyindex 2ef21ecd9a..9813ec6ba2 100644--- a/tests/unit_tests/agentserver/test_team_skill_links.py+++ b/tests/unit_tests/agentserver/test_team_skill_links.py@@ -111,7 +111,7 @@ def test_cmd_fallback_passes_decoding_options(monkeypatch, tmp_path):assert captured["kwargs"]["errors"] == "replace"assert captured["kwargs"]["capture_output"] is Trueif sys.platform == "win32":- assert captured["kwargs"]["encoding"] == "mbcs"+ assert captured["kwargs"]["encoding"] == "oem"else:assert captured["kwargs"]["encoding"] == "utf-8"assert captured["cmd"][1:3] == ["/c", "mklink"]@@ -156,6 +156,58 @@ def test_native_rejection_falls_back_to_cmd_exe(tmp_path, monkeypatch):_assert_link_resolves(link, target)+@requires_windows+def test_native_rejection_missing_target_falls_back_to_dangling_junction(tmp_path, monkeypatch):+ """Native rejection with a missing target: cmd.exe exits 0 and silently+ creates a dangling junction, which later prune runs remove."""+ def _reject(_src, _dst):+ raise OSError(2, "missing target")++ monkeypatch.setattr(team_skill_links._winapi, "CreateJunction", _reject)+ source = tmp_path / "skills_src"+ source.mkdir()+ target_root = tmp_path / "skills_tgt"+ target_root.mkdir()+ link = target_root / "dangling"++ _create_windows_junction(source / "missing_target", link)++ assert os.path.lexists(link)+ assert not link.exists(), "mklink /J must not validate the target"+ team_skill_links.prune_skill_dir_links(source, target_root)+ assert not os.path.lexists(link), "prune must remove the dangling junction"+++def test_remove_skill_dir_link_tolerates_vanished_entry(tmp_path, monkeypatch):+ """A link removed by a concurrent sync between check and unlink is success."""+ link = tmp_path / "ghost"+ # Pretend the type check saw a link, but it is gone by the time the+ # removal syscall runs (concurrent prune raced us).+ monkeypatch.setattr(Path, "is_symlink", lambda self: True)++ team_skill_links.remove_skill_dir_link(link) # must not raise FileNotFoundError++ assert not os.path.lexists(link)+++@requires_windows+def test_remove_skill_dir_link_reraises_when_entry_remains(tmp_path, monkeypatch):+ """A removal error with the entry still present is not swallowed."""+ target = _make_skill(tmp_path, "skill-a")+ link = tmp_path / "link"+ _create_windows_junction(target, link)++ def _denied(_path):+ raise PermissionError(13, "Access denied")++ monkeypatch.setattr(team_skill_links.os, "rmdir", _denied)++ with pytest.raises(PermissionError):+ team_skill_links.remove_skill_dir_link(link)++ assert os.path.lexists(link)++def test_link_skill_dir_is_idempotent(tmp_path):"""Linking twice keeps a single working link and never raises."""target = _make_skill(tmp_path, "skill-a")