Merged
fix(deployment): 显式 id=None 时自动生成,避免部署状态互相覆盖与 fallback 失效 #343
fix(deployment): 显式 id=None 时自动生成,避免部署状态互相覆盖与 fallback 失效 #343
Merged
庄昭雄Add on Oct 18
Total: 3 files changed+70-0
@@ -58,6 +58,20 @@ class Deployment:
58 58 
59 def __post_init__(self):59 def __post_init__(self):
60 """字段类型与数值校验"""60 """字段类型与数值校验"""
61+ # id 允许缺省:dataclass 的 default_factory 只在参数省略时生效。
62+ # 显式传入 None(配置里没有 id,调用方把缺省值原样透传时会这样)仍要保持
63+ # “自动生成”的语义;否则多个部署共用 id=None:状态/统计按 key 合并,
64+ # 且失败后 `[d for d in available if d.id != selected.id]` 会一次清空
65+ # 全部候选,fallback 直接失效(AllDeploymentsFailed)。
66+ if self.id is None:
67+ self.id = str(uuid.uuid4())[:8]
68+ elif not isinstance(self.id, str):
69+ raise TypeError(
70+ f"Deployment.id must be a str or None, got {type(self.id).__name__}"
71+ )
72+ elif not self.id.strip():
73+ raise ValueError("Deployment.id must be a non-empty string or None")
74+ 
61 for name in ("model_name", "api_key", "api_base", "provider"):75 for name in ("model_name", "api_key", "api_base", "provider"):
62 value = getattr(self, name)76 value = getattr(self, name)
63 if not isinstance(value, str) or not value:77 if not isinstance(value, str) or not value:
@@ -195,3 +195,28 @@ def test_timeout_validation():
195 Deployment(**_base_kwargs(), timeout=-1.0)195 Deployment(**_base_kwargs(), timeout=-1.0)
196 with pytest.raises(TypeError, match="timeout"):196 with pytest.raises(TypeError, match="timeout"):
197 Deployment(**_base_kwargs(), timeout="30")197 Deployment(**_base_kwargs(), timeout="30")
198+ 
199+ 
200+def test_explicit_none_id_is_auto_generated():
201+ """id 缺省(调用方把缺省值原样透传为 None)时仍应自动生成。"""
202+ dep = Deployment(**_base_kwargs(), id=None)
203+ assert isinstance(dep.id, str)
204+ assert len(dep.id) == 8
205+ 
206+ 
207+def test_auto_generated_ids_are_unique_when_explicit_none():
208+ dep1 = Deployment(**_base_kwargs(), id=None)
209+ dep2 = Deployment(**_base_kwargs(), id=None)
210+ assert dep1.id != dep2.id
211+ 
212+ 
213+@pytest.mark.parametrize("bad_id", [123, 1.5, True, ["x"]])
214+def test_non_string_id_is_rejected(bad_id):
215+ with pytest.raises(TypeError, match="id"):
216+ Deployment(**_base_kwargs(), id=bad_id)
217+ 
218+ 
219+@pytest.mark.parametrize("bad_id", ["", " "])
220+def test_blank_id_is_rejected(bad_id):
221+ with pytest.raises(ValueError, match="id"):
222+ Deployment(**_base_kwargs(), id=bad_id)
@@ -10,6 +10,7 @@ from intelli_router.core.deployment import Deployment, DeploymentStatus
10from intelli_router.utils.exceptions import (10from intelli_router.utils.exceptions import (
11 AllDeploymentsFailed,11 AllDeploymentsFailed,
12 DeploymentNetworkError,12 DeploymentNetworkError,
13+ DeploymentServerError,
13 NoDeploymentAvailable,14 NoDeploymentAvailable,
14 RouterError,15 RouterError,
15)16)
@@ -694,3 +695,33 @@ def test_update_deployments_shared_state_no_clobber():
694 # 旧部署 a1 的状态应被清理695 # 旧部署 a1 的状态应被清理
695 assert "a1" not in shared_state.deployment_status696 assert "a1" not in shared_state.deployment_status
696 assert router_b.get_model_list() == ["model-b"]697 assert router_b.get_model_list() == ["model-b"]
698+ 
699+ 
700+@pytest.mark.asyncio
701+async def test_failover_with_explicit_none_ids_keeps_the_healthy_sibling():
702+ """多个显式 id=None 的部署:一个失败后应 fallback 到健康备用。
703+ 
704+ 修复前 ``ReliableRouter`` 以 ``d.id`` 为状态 key,所有 None-id 部署折叠成
705+ 同一个 ``"null"``:一次失败会把全部候选从 available 里清掉,直接抛
706+ ``AllDeploymentsFailed``,健康备用一次都不会被请求(@JustinBieber1223 的
707+ 检视意见要求用路由级用例固化这个症状)。
708+ """
709+ bad = Deployment(model_name="m", api_key="k", api_base="http://bad", id=None)
710+ good = Deployment(model_name="m", api_key="k", api_base="http://good", id=None)
711+ router = ReliableRouter(
712+ deployments=[bad, good], strategy="ordered-failover", num_retries=2,
713+ )
714+ calls = []
715+ 
716+ async def mock_make_request(deployment, request_body):
717+ calls.append(deployment.api_base)
718+ if deployment.api_base == "http://bad":
719+ raise DeploymentServerError(deployment.id, status_code=500)
720+ return {"choices": [{"message": {"content": "ok"}}], "usage": {}}
721+ 
722+ with patch.object(router, "_make_request", new=mock_make_request):
723+ result = await router.completion("m", [{"role": "user", "content": "hi"}])
724+ 
725+ assert result["choices"][0]["message"]["content"] == "ok"
726+ assert calls == ["http://bad", "http://good"]
727+ assert bad.id != good.id