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
skill安装后实时生效
yt2026e599219fa59d
diff --git a/jiuwenswarm/server/runtime/agent_adapter/interface.py b/jiuwenswarm/server/runtime/agent_adapter/interface.pyindex aadf7fc8b9..e7094fc2a2 100644--- a/jiuwenswarm/server/runtime/agent_adapter/interface.py+++ b/jiuwenswarm/server/runtime/agent_adapter/interface.py@@ -1651,6 +1651,20 @@ class JiuWenSwarm:return bump()return None+ async def _push_skill_refresh_to_bound_session(+ self,+ request: AgentRequest | None,+ ) -> None:+ """装/卸请求绑定真实会话时,同轮立即刷新该会话的 SkillUseRail。++ 委托 DeepAdapter 同名方法;适配器不支持时静默跳过,维持既有+ 「下一轮边界惰性追平」语义。+ """+ adapter = self._ensure_adapter()+ push = getattr(adapter, "_push_skill_refresh_to_bound_session", None)+ if callable(push):+ await push(request)+async def refresh_enabled_skills_from_db(self) -> None:"""企业账本变更后轻量刷新启用集(直读 DB + 重建 SkillUseRail)。@@ -2439,6 +2453,12 @@ class JiuWenSwarm:# Root create_instance / refresh often no-ops on the chat path;# bump so existing session adapters lazily rebuild SkillUseRail.self.bump_skill_enabled_version()+ # Same-turn visibility: an install/uninstall request bound to a+ # live chat session (session_id resolves to an existing session+ # adapter) refreshes that session's SkillUseRail now instead of+ # at its next turn boundary, so install -> skill_tool works+ # within the same turn.+ await self._push_skill_refresh_to_bound_session(request)except Exception as exc:logger.error("[JiuWenSwarm] skills 请求处理失败: %s", exc)return AgentResponse(diff --git a/jiuwenswarm/server/runtime/agent_adapter/interface_deep.py b/jiuwenswarm/server/runtime/agent_adapter/interface_deep.pyindex 8089089304..2b73edbe5e 100644--- a/jiuwenswarm/server/runtime/agent_adapter/interface_deep.py+++ b/jiuwenswarm/server/runtime/agent_adapter/interface_deep.py@@ -3332,6 +3332,43 @@ class JiuWenSwarmDeepAdapter:self._session_skill_versions[session_id] = currentself._session_skill_refresh_failures.pop(session_id, None)+ async def _push_skill_refresh_to_bound_session(+ self,+ request: AgentRequest | None,+ ) -> None:+ """Refresh the session adapter bound to an install/uninstall request now.++ Web/facade skill installs land on the root adapter; the bound session's+ SkillUseRail allow-list otherwise only catches up at its next turn+ boundary (``_refresh_session_skills_if_stale``), so a same-turn+ ``skill_tool`` call fails with ``Skill not found``. When the request+ carries a session id that maps to a live session adapter here, run the+ same lazy refresh immediately under the session lock; failures fall+ back to the lazy next-turn semantics and other sessions are untouched.+ """+ session_id = str(getattr(request, "session_id", None) or "").strip()+ if not session_id:+ return+ sid = self._session_adapter_key(session_id)+ if sid not in self._session_adapters:+ # Synthetic facade sessions (bff-*) never create a session adapter+ # on this root; keep the lazy next-turn behavior for them.+ return+ lock = self._session_adapter_locks.setdefault(sid, asyncio.Lock())+ try:+ async with lock:+ adapter = self._session_adapters.get(sid)+ if adapter is None:+ return+ await self._refresh_session_skills_if_stale(sid, adapter)+ except Exception as exc: # noqa: BLE001 — push 失败回退下一轮 lazy 追平+ logger.warning(+ "[JiuWenSwarmDeepAdapter] same-turn skill refresh push failed "+ "(fallback to lazy next-turn refresh): session_id=%s error=%s",+ sid,+ exc,+ )+async def _evict_idle_session_adapters(self) -> None:if self._is_session_scoped_adapter:returndiff --git a/tests/unit_tests/agentserver/test_skill_session_lazy_refresh.py b/tests/unit_tests/agentserver/test_skill_session_lazy_refresh.pyindex 38be48b8bd..eb28728217 100644--- a/tests/unit_tests/agentserver/test_skill_session_lazy_refresh.py+++ b/tests/unit_tests/agentserver/test_skill_session_lazy_refresh.py@@ -5,6 +5,7 @@from __future__ import annotationsimport asyncio+from types import SimpleNamespaceimport pytest@@ -155,3 +156,139 @@ async def test_session_child_marks_bump_version_not_root_after_concurrent_bump(assert root._skill_enabled_version == 7 # this notify + concurrent bump# Stamp the version claimed before refresh (6), not the post-await root (7).assert root._session_skill_versions.get("sess_a") == 6+++def _make_push_root(**overrides: object) -> JiuWenSwarmDeepAdapter:+ root = _make_root(_session_adapter_locks={}, **overrides)+ return root+++async def test_push_refreshes_bound_session_same_turn(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """Web install bound to a live session refreshes it now, not next turn."""+ root = _make_push_root(_skill_enabled_version=2)+ child = _make_session_child(root, session_id="sess_a")+ root._session_adapters["sess_a"] = child+ monkeypatch.setattr(interface_deep_module, "is_skill_prebuilt_tenant", lambda *_a, **_k: True)+ monkeypatch.setattr(interface_deep_module, "_skill_session_lazy_refresh_enabled", lambda: True)++ child_refreshed = False++ async def _child_refresh() -> bool:+ nonlocal child_refreshed+ child_refreshed = True+ return True++ child.refresh_enabled_skills_from_db = _child_refresh # type: ignore[method-assign]++ await root._push_skill_refresh_to_bound_session(SimpleNamespace(session_id="sess_a"))++ assert child_refreshed is True+ # Same lazy bookkeeping path: stamped current at the root stamp (2).+ assert root._session_skill_versions.get("sess_a") == 2+++async def test_push_ignores_session_without_adapter(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """Synthetic facade sessions (bff-*) never map to an adapter here: no push."""+ root = _make_push_root(_skill_enabled_version=2)+ monkeypatch.setattr(interface_deep_module, "is_skill_prebuilt_tenant", lambda *_a, **_k: True)+ monkeypatch.setattr(interface_deep_module, "_skill_session_lazy_refresh_enabled", lambda: True)++ refreshed = False++ async def _refresh() -> bool:+ nonlocal refreshed+ refreshed = True+ return True++ root.refresh_enabled_skills_from_db = _refresh # type: ignore[method-assign]++ await root._push_skill_refresh_to_bound_session(SimpleNamespace(session_id="bff-656b24ae"))++ assert refreshed is False+ assert root._session_adapters == {}+++async def test_push_without_session_id_is_noop() -> None:+ """No session binding on the request: keep the lazy semantics."""+ root = _make_push_root(_skill_enabled_version=2)++ await root._push_skill_refresh_to_bound_session(SimpleNamespace(session_id=None))+ await root._push_skill_refresh_to_bound_session(None)++ assert root._session_skill_versions == {}+++async def test_push_refresh_failure_falls_back_to_lazy(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """Failed push must neither raise nor mark the session current."""+ root = _make_push_root(_skill_enabled_version=2)+ child = _make_session_child(root, session_id="sess_a")+ root._session_adapters["sess_a"] = child+ monkeypatch.setattr(interface_deep_module, "is_skill_prebuilt_tenant", lambda *_a, **_k: True)+ monkeypatch.setattr(interface_deep_module, "_skill_session_lazy_refresh_enabled", lambda: True)++ async def _child_refresh() -> bool:+ return False++ child.refresh_enabled_skills_from_db = _child_refresh # type: ignore[method-assign]++ await root._push_skill_refresh_to_bound_session(SimpleNamespace(session_id="sess_a"))++ # Stays stale: next turn boundary retries (lazy semantics preserved).+ assert "sess_a" not in root._session_skill_versions+++async def test_push_survives_adapter_evicted_while_waiting_on_lock(+ monkeypatch: pytest.MonkeyPatch,+) -> None:+ """Adapter evicted by a lock holder while the push queues: no error.++ Mirrors the turn-boundary path holding the session lock (``setdefault``+ shares the same lock) and evicting the adapter between the push's guard+ check and its lock acquisition.+ """+ root = _make_push_root(_skill_enabled_version=2)+ child = _make_session_child(root, session_id="sess_a")+ root._session_adapters["sess_a"] = child+ monkeypatch.setattr(interface_deep_module, "is_skill_prebuilt_tenant", lambda *_a, **_k: True)+ monkeypatch.setattr(interface_deep_module, "_skill_session_lazy_refresh_enabled", lambda: True)++ lock = asyncio.Lock()+ root._session_adapter_locks["sess_a"] = lock+ lock_held = asyncio.Event()+ release_lock = asyncio.Event()++ async def _evict_holder() -> None:+ async with lock:+ lock_held.set()+ await release_lock.wait()+ root._session_adapters.pop("sess_a", None)++ holder = asyncio.create_task(_evict_holder())+ await lock_held.wait()++ async def _child_refresh() -> bool: # must not be reached+ raise AssertionError("refresh must not run after eviction")++ child.refresh_enabled_skills_from_db = _child_refresh # type: ignore[method-assign]++ push_task = asyncio.create_task(+ root._push_skill_refresh_to_bound_session(SimpleNamespace(session_id="sess_a"))+ )+ await asyncio.sleep(0) # push passes its guard and queues on the lock+ release_lock.set()++ await push_task+ await holder++ assert root._session_skill_versions == {}