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(gateway): 企业版按 edition 初始化 Redis,修复 gateway 库无法创建
caozhenhua0000ebaf91615b33
diff --git a/jiuwenswarm/extensions/redis/redis_runtime.py b/jiuwenswarm/extensions/redis/redis_runtime.pyindex 0844747ee6..1ba1412963 100644--- a/jiuwenswarm/extensions/redis/redis_runtime.py+++ b/jiuwenswarm/extensions/redis/redis_runtime.py@@ -1,13 +1,11 @@# Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.-"""Gateway 进程内 Redis 生命周期:配置解析、连接、健康检查(§3.3.4)。+"""Gateway 进程内 Redis 生命周期:配置解析、连接、健康检查。-``deployment_mode=standalone`` 或未成功连上 Redis 时,不创建客户端;业务模块可通过-``get_gateway_redis_client()`` 判空后回退本地实现。+企业版(``gateway.edition=enterprise``)启动时连接 Redis,供 Ephemeral 存储使用;+个人版跳过。连接失败时降级为无 Redis,业务模块通过 ``get_gateway_redis_client()`` 判空。"""from __future__ import annotations-from jiuwenswarm.common.local_env_config import is_enterprise-import asyncioimport importlibimport logging@@ -73,7 +71,7 @@ def get_declared_deployment_mode() -> str:def get_effective_distributed_redis_active() -> bool:- """声明为 active-standby 且 Redis 已连接、未因健康检查标记为 degraded。"""+ """企业版 Redis 已连接且未因健康检查标记为 degraded。"""return _effective_distributed_redis and not _redis_degraded and _redis_client is not None@@ -154,15 +152,10 @@ async def init_gateway_redis_from_config(full_cfg: dict[str, Any] | None) -> Nonelse:_gateway_instance_id = iid if iid else None- if _declared_deployment_mode != "active-standby":- logger.debug("[GatewayRedis] deployment_mode=standalone; skip Redis init (§3.3.4)")- return+ from jiuwenswarm.gateway.edition import EDITION_ENTERPRISE, resolve_gateway_edition- # 企业版特性:仅企业版开启时启用主备 Redis- if not is_enterprise():- logger.debug(- "[GatewayRedis] deployment_mode=active-standby but not enterprise edition; skip Redis init"- )+ if resolve_gateway_edition(cfg_in) != EDITION_ENTERPRISE:+ logger.debug("[GatewayRedis] personal edition; skip Redis init")returnr_cfg = RedisConfig.from_mapping(cfg_in.get("redis") if isinstance(cfg_in.get("redis"), dict) else {})diff --git a/tests/unit_tests/redis/test_redis_runtime.py b/tests/unit_tests/redis/test_redis_runtime.pyindex bad07df6f2..e2a94782fd 100644--- a/tests/unit_tests/redis/test_redis_runtime.py+++ b/tests/unit_tests/redis/test_redis_runtime.py@@ -30,7 +30,7 @@ def restore_import():@pytest.mark.asyncio-async def test_standalone_skips_redis(restore_import):+async def test_personal_edition_skips_redis(restore_import):from jiuwenswarm.extensions.redis import (get_declared_deployment_mode,get_effective_distributed_redis_active,@@ -39,7 +39,9 @@ async def test_standalone_skips_redis(restore_import):builtins.__import__ = _block_redis_import- await init_gateway_redis_from_config({"gateway": {"deployment_mode": "standalone"}})+ await init_gateway_redis_from_config({+ "gateway": {"deployment_mode": "standalone", "edition": "personal"},+ })assert get_declared_deployment_mode() == "standalone"assert not get_effective_distributed_redis_active()@@ -54,11 +56,10 @@ async def test_active_standby_without_redis_package_degrades(init_gateway_redis_from_config,)- monkeypatch.setenv("JIUWENSWARM_EDITION", "enterprise")builtins.__import__ = _block_redis_importawait init_gateway_redis_from_config({- "gateway": {"deployment_mode": "active-standby"},+ "gateway": {"deployment_mode": "active-standby", "edition": "enterprise"},"redis": {"host": "127.0.0.1", "port": 6379},})assert get_declared_deployment_mode() == "active-standby"@@ -88,13 +89,12 @@ async def test_active_standby_unreachable_server_degrades(async def close(self) -> None:pass- monkeypatch.setenv("JIUWENSWARM_EDITION", "enterprise")monkeypatch.setattr("jiuwenswarm.extensions.redis.redis_runtime._load_gateway_redis_client_class",lambda: _UnreachableRedisClient,)await init_gateway_redis_from_config({- "gateway": {"deployment_mode": "active-standby"},+ "gateway": {"deployment_mode": "active-standby", "edition": "enterprise"},"redis": {"host": "unreachable.test", "port": 1},})assert get_declared_deployment_mode() == "active-standby"@@ -102,7 +102,7 @@ async def test_active_standby_unreachable_server_degrades(@pytest.mark.asyncio-async def test_active_standby_without_agent_runtime_skips_connect(+async def test_personal_edition_active_standby_skips_connect(restore_import, monkeypatch: pytest.MonkeyPatch):from jiuwenswarm.extensions.redis import (@@ -114,7 +114,7 @@ async def test_active_standby_without_agent_runtime_skips_connect(monkeypatch.delenv("JIUWENSWARM_EDITION", raising=False)builtins.__import__ = _block_redis_importawait init_gateway_redis_from_config({- "gateway": {"deployment_mode": "active-standby"},+ "gateway": {"deployment_mode": "active-standby", "edition": "personal"},"redis": {"host": "127.0.0.1", "port": 6379},})assert get_declared_deployment_mode() == "active-standby"