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(flash): 支持外部记忆与云画像
Nepham519476d34b8e
diff --git a/jiuwenswarm/resources/config.yaml b/jiuwenswarm/resources/config.yamlindex 6870358500..c30ed9972f 100644--- a/jiuwenswarm/resources/config.yaml+++ b/jiuwenswarm/resources/config.yaml@@ -265,7 +265,7 @@ memory:# ------------------------------------------------------------# 用户画像(用户级记忆概览,OfficeAceUserProfileRail 自动注入 prompt)# ------------------------------------------------------------- # 独立于 engine/external:不要求 memory.engine=external 即可生效。+ # 要求 memory.engine=external/both 且 external.provider=officeace_cloud。# endpoint/api_key 任一为空 → rail 不挂载(零残留,对话照常)。# user_id 复用上层 memory.external.user_id(${MEMORY_USER_ID:-__default__}),# 不在此段单独配置。diff --git a/jiuwenswarm/server/runtime/agent_adapter/interface_flash.py b/jiuwenswarm/server/runtime/agent_adapter/interface_flash.pyindex fe462b9adc..f4baa38282 100644--- a/jiuwenswarm/server/runtime/agent_adapter/interface_flash.py+++ b/jiuwenswarm/server/runtime/agent_adapter/interface_flash.py@@ -266,6 +266,8 @@ class JiuwenSwarmFlashAdapter(JiuWenSwarmDeepAdapter):# 动态挂载路径上,进 keep 才不会被 _drop_non_whitelist_dynamic_rails# 卸载;闸门是 modes.agent.memory.enabled(flash 记忆档归一 agent)。"_memory_rail",+ "_external_memory_rail",+ "_office_ace_user_profile_rail",# ask_user:交互请求动态挂载,留白名单避免 churn(见上方注释)。"_ask_user_rail",})@@ -868,7 +870,8 @@ class JiuwenSwarmFlashAdapter(JiuWenSwarmDeepAdapter):2. 记忆 rail:flash 的统一 memory 工具(FlashMemoryRail)走动态挂载路径,这里按归一后的 agent 档调 ``_handle_memory_rail_by_config``(闸门``modes.agent.memory.enabled``,未开启则卸载);``_memory_rail`` 在 keep- 白名单内,不会被后续卸载步骤误删。+ 白名单内,不会被后续卸载步骤误删。外接记忆与云画像沿用父类的+ engine/provider 配置入口,独立于内置 memory 的模式开关。3. 卸载白名单(``_FLASH_RAIL_KEEP`` ∪ ``_PROFILE_PROTECTED_RAILS``)外的、可能被父类/旧 reload 残留注册的动态 rail。@@ -883,6 +886,8 @@ class JiuwenSwarmFlashAdapter(JiuWenSwarmDeepAdapter):"""self._last_mode = modeawait self._handle_memory_rail_by_config("agent")+ await self._handle_external_memory_rail_by_config()+ await self._handle_office_ace_user_profile_rail_by_config()await self._drop_non_whitelist_dynamic_rails()async def try_start_dreaming(self, busy_checker=None) -> None:@@ -1261,6 +1266,7 @@ class JiuwenSwarmFlashAdapter(JiuWenSwarmDeepAdapter):("_context_processor_rail", "ContextProcessorRail"),("_memory_rail", "MemoryRail"),("_external_memory_rail", "ExternalMemoryRail"),+ ("_office_ace_user_profile_rail", "OfficeAceUserProfileRail"),("_skill_evolution_rail", "SkillEvolutionRail"),("_evolution_interrupt_rail", "EvolutionInterruptRail"),("_skill_create_rail", "SkillCreateRail"),diff --git a/tests/unit_tests/agentserver/test_flash_adapter_rails.py b/tests/unit_tests/agentserver/test_flash_adapter_rails.pyindex cad9237257..3ef50517f8 100644--- a/tests/unit_tests/agentserver/test_flash_adapter_rails.py+++ b/tests/unit_tests/agentserver/test_flash_adapter_rails.py@@ -70,7 +70,8 @@ def test_keep_whitelist_keeps_protected_and_drops_others():_info("_memory_rail"), # in KEEP(本仓决策:统一 memory 工具)_info("_ask_user_rail"), # in KEEP (interactive dynamic mount)_info("_context_assemble_rail"), # neither (dynamic reg target)- _info("_external_memory_rail"), # neither (dynamic reg target)+ _info("_external_memory_rail"), # in KEEP (config-driven)+ _info("_office_ace_user_profile_rail"), # in KEEP (config-driven)_info("_symphony_orchestration_rail"), # neither]@@ -86,6 +87,8 @@ def test_keep_whitelist_keeps_protected_and_drops_others():"_skill_active_state_rail","_skill_authorization_rail","_memory_rail",+ "_external_memory_rail",+ "_office_ace_user_profile_rail","_ask_user_rail",}@@ -136,11 +139,8 @@ def test_ask_user_rail_is_in_keep_to_avoid_churn():async def test_update_rails_for_mode_drops_non_whitelist_dynamic_rails():"""Dynamic rails outside the whitelist are unregistered each request.- The parent ``_update_rails_for_mode`` calls ``_update_agent_rails()`` which- dynamically registers ``_context_assemble_rail`` / ``_memory_rail`` /- ``_external_memory_rail`` — all outside the flash whitelist. Flash's- override must unregister any such rail that slipped through (e.g. from a- prior reload), and must NOT call ``_update_agent_rails``.+ Memory rails have their own config-driven lifecycle. Other dynamic rails+ outside the flash profile must still be removed."""adapter = JiuwenSwarmFlashAdapter()@@ -152,7 +152,6 @@ async def test_update_rails_for_mode_drops_non_whitelist_dynamic_rails():# memory's by _handle_memory_rail_by_config via the agent-profile gate.)leaked = {"_context_assemble_rail": SimpleNamespace(name="context_assemble"),- "_external_memory_rail": SimpleNamespace(name="external_memory"),"_skill_create_rail": SimpleNamespace(name="skill_create"),}for attr, rail in leaked.items():@@ -168,11 +167,15 @@ async def test_update_rails_for_mode_drops_non_whitelist_dynamic_rails():adapter._skill_rail = SimpleNamespace(name="keep-me-skill")adapter._ask_user_rail = SimpleNamespace(name="keep-me-ask-user")adapter._memory_rail = SimpleNamespace(name="keep-me-memory")+ adapter._external_memory_rail = SimpleNamespace(name="keep-me-external")+ adapter._office_ace_user_profile_rail = SimpleNamespace(name="keep-me-profile")# 统一 memory 工具的挂/卸走 agent 档闸门,这里 mock 掉以隔离 drop 逻辑本身。handle_memory = AsyncMock()with pytest.MonkeyPatch.context() as mp:mp.setattr(adapter, "_handle_memory_rail_by_config", handle_memory)+ mp.setattr(adapter, "_handle_external_memory_rail_by_config", AsyncMock())+ mp.setattr(adapter, "_handle_office_ace_user_profile_rail_by_config", AsyncMock())await adapter._update_rails_for_mode("flash")handle_memory.assert_awaited_once_with("agent")@@ -186,6 +189,8 @@ async def test_update_rails_for_mode_drops_non_whitelist_dynamic_rails():assert adapter._skill_rail is not Noneassert adapter._ask_user_rail is not None # kept, not churnedassert adapter._memory_rail is not None # kept, not churned+ assert adapter._external_memory_rail is not None+ assert adapter._office_ace_user_profile_rail is not Noneassert all(getattr(adapter, a) is None for a in leaked)assert unregister.await_count == len(leaked)diff --git a/tests/unit_tests/agentserver/test_flash_external_memory_integration.py b/tests/unit_tests/agentserver/test_flash_external_memory_integration.pynew file mode 100644index 0000000000..f555065191--- /dev/null+++ b/tests/unit_tests/agentserver/test_flash_external_memory_integration.py@@ -0,0 +1,97 @@+"""Flash wiring for the existing external-memory and user-profile lifecycle."""++from types import SimpleNamespace+from unittest.mock import AsyncMock++import pytest++from jiuwenswarm.agents.harness.common.memory import config as memory_module+from jiuwenswarm.server.runtime.agent_adapter import interface_deep+from jiuwenswarm.server.runtime.agent_adapter.interface_flash import (+ JiuwenSwarmFlashAdapter,+)+++class RecordingAgent:+ def __init__(self):+ self.rails = []+ self.unregistered = []++ async def register_rail(self, rail):+ assert rail not in self.rails+ self.rails.append(rail)++ async def unregister_rail(self, rail):+ self.rails.remove(rail)+ self.unregistered.append(rail)+++def flash_memory(monkeypatch):+ config = {+ "memory": {+ "mode": "local",+ "engine": "external",+ "external": {"provider": "officeace_cloud", "user_id": "alice"},+ "office_ace_user_profile": {+ "endpoint": "https://memory.invalid", "api_key": "test-key",+ },+ },+ }+ monkeypatch.setenv("OFFICE_ACE_DEPLOYMENT", "pc")+ monkeypatch.setattr(interface_deep, "get_config", lambda: config)+ monkeypatch.setattr(memory_module, "get_memory_section", lambda cfg=None: (cfg or config)["memory"])+ adapter = JiuwenSwarmFlashAdapter()+ adapter._instance = RecordingAgent()+ # Isolate built-in memory, not the existing external configuration gates.+ monkeypatch.setattr(adapter, "_handle_memory_rail_by_config", AsyncMock())+ external = SimpleNamespace(kind="external", _provider=SimpleNamespace(on_session_end=AsyncMock()))+ profile = SimpleNamespace(kind="profile")+ monkeypatch.setattr(adapter, "_build_external_memory_rail", lambda: external)+ monkeypatch.setattr(adapter, "_build_office_ace_user_profile_rail", lambda: profile)+ return adapter, config++[email protected]("engine,provider,expected", [+ ("external", "officeace_cloud", {"external", "profile"}),+ ("both", "officeace_cloud", {"external", "profile"}),+ ("builtin", "officeace_cloud", set()),+ ("none", "officeace_cloud", set()),+ ("external", "mem0", {"external"}),+ ("external", "", set()),+])+async def test_first_flash_request_obeys_engine_and_provider(flash_memory, engine, provider, expected):+ adapter, config = flash_memory+ config["memory"]["engine"] = engine+ config["memory"]["external"]["provider"] = provider+ await adapter._update_rails_for_mode("flash")+ assert {rail.kind for rail in adapter._instance.rails} == expected+ first = list(adapter._instance.rails)+ await adapter._update_rails_for_mode("flash")+ assert adapter._instance.rails == first+ assert not adapter._instance.unregistered+++async def test_missing_profile_credentials_keeps_external_memory(flash_memory):+ adapter, config = flash_memory+ config["memory"]["office_ace_user_profile"]["api_key"] = ""+ await adapter._update_rails_for_mode("flash")+ assert {rail.kind for rail in adapter._instance.rails} == {"external"}+++async def test_flash_uses_existing_disable_and_reenable_lifecycle(flash_memory):+ adapter, config = flash_memory+ await adapter._update_rails_for_mode("flash")+ external = adapter._external_memory_rail+ config["memory"]["engine"] = "none"+ await adapter._update_rails_for_mode("flash")+ assert adapter._instance.rails == []+ external._provider.on_session_end.assert_awaited_once_with()+ assert not adapter._external_memory_rail_registered+ assert not adapter._office_ace_user_profile_rail_registered+ config["memory"]["engine"] = "external"+ await adapter._update_rails_for_mode("flash")+ assert {rail.kind for rail in adapter._instance.rails} == {"external", "profile"}