RoutingPolicy gains tier_modes: a tier entry may be a string
(model, mode inherits global) or {model, mode}. The explicit
tier mode overrides the global mode in both directions, so plan
can be active while goal/default stay shadow, and one tier can
be rolled back to off without touching the others. An invalid
tier mode drops that tier entirely (fail-closed: a typo must
never accidentally activate a tier, and never kills other
tiers). Tier-off tasks return worker_default and are not
audited as policy/shadow, so they never inflate shadow sample
counts. Explicit task and worker-CLI models still win over any
tier mode.
Tests: test_routing_tier.py (5 tests).
108 lines
4.2 KiB
Python
108 lines
4.2 KiB
Python
"""分档激活 v0c 单测:档模式优先全局、字符串兼容、无效档模式丢档。
|
|
|
|
运行(在仓库根目录下):
|
|
python3 -m unittest discover -s cloud/tests -p "test_routing_tier.py" -v
|
|
|
|
说明:纯函数单测,不碰库、不联网、不跑模型。关键断言:
|
|
全局 shadow 时 plan 档 active 只切 plan 档、其余档仍影子;
|
|
全局 active 时某档 off 只回滚该档;档模式写错时该档整档
|
|
丢掉、不回落全局(防 typo 误激活),其余档不受影响;
|
|
字符串档写法继续按全局模式走。
|
|
"""
|
|
|
|
import sys
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
try:
|
|
from cloud.models import decide_routing, parse_routing_policy
|
|
except ImportError:
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
|
|
from cloud.models import decide_routing, parse_routing_policy
|
|
|
|
|
|
def _policy(data):
|
|
policy = parse_routing_policy(data)
|
|
assert policy is not None, data
|
|
return policy
|
|
|
|
|
|
class TierModeTestCase(unittest.TestCase):
|
|
def test_plan_active_only_under_global_shadow(self):
|
|
policy = _policy({
|
|
"mode": "shadow",
|
|
"tiers": {
|
|
"plan": {"model": "gpt-6.1-sol", "mode": "active"},
|
|
"goal": "gpt-6.1-sol",
|
|
"default": "gpt-6.1-sol",
|
|
},
|
|
})
|
|
plan = decide_routing({"id": "t1", "plan": True}, "gpt-5.6-terra",
|
|
policy=policy)
|
|
self.assertEqual(plan.source, "policy")
|
|
self.assertTrue(plan.applied)
|
|
self.assertEqual(plan.chosen_model, "gpt-6.1-sol")
|
|
goal = decide_routing({"id": "t2", "goal_mode": True},
|
|
"gpt-5.6-terra", policy=policy)
|
|
self.assertEqual(goal.source, "shadow")
|
|
self.assertFalse(goal.applied)
|
|
self.assertEqual(goal.suggested_model, "gpt-6.1-sol")
|
|
self.assertEqual(goal.chosen_model, "gpt-5.6-terra")
|
|
|
|
def test_tier_off_rolls_back_only_that_tier(self):
|
|
policy = _policy({
|
|
"mode": "active",
|
|
"tiers": {
|
|
"plan": {"model": "gpt-6.1-sol", "mode": "off"},
|
|
"default": "gpt-6.1-sol",
|
|
},
|
|
})
|
|
plan = decide_routing({"id": "t1", "plan": True}, "gpt-5.6-terra",
|
|
policy=policy)
|
|
self.assertEqual(plan.source, "worker_default")
|
|
self.assertEqual(plan.chosen_model, "gpt-5.6-terra")
|
|
default = decide_routing({"id": "t2"}, "gpt-5.6-terra",
|
|
policy=policy)
|
|
self.assertEqual(default.source, "policy")
|
|
self.assertEqual(default.chosen_model, "gpt-6.1-sol")
|
|
|
|
def test_string_tier_inherits_global_mode(self):
|
|
policy = _policy({
|
|
"mode": "shadow",
|
|
"tiers": {"plan": "gpt-6.1-sol"},
|
|
})
|
|
decision = decide_routing({"id": "t1", "plan": True},
|
|
"gpt-5.6-terra", policy=policy)
|
|
self.assertEqual(decision.source, "shadow")
|
|
self.assertFalse(decision.applied)
|
|
|
|
def test_invalid_tier_mode_drops_tier_not_global(self):
|
|
policy = _policy({
|
|
"mode": "active",
|
|
"tiers": {
|
|
"plan": {"model": "gpt-6.1-sol", "mode": "activ"},
|
|
"default": "gpt-6.1-sol",
|
|
},
|
|
})
|
|
self.assertNotIn("plan", policy.tiers)
|
|
plan = decide_routing({"id": "t1", "plan": True}, "gpt-5.6-terra",
|
|
policy=policy)
|
|
# 丢档 = 回 worker 默认,不按全局 active 误激活。
|
|
self.assertEqual(plan.source, "worker_default")
|
|
self.assertEqual(plan.chosen_model, "gpt-5.6-terra")
|
|
default = decide_routing({"id": "t2"}, "gpt-5.6-terra",
|
|
policy=policy)
|
|
self.assertEqual(default.source, "policy")
|
|
|
|
def test_explicit_task_and_worker_still_win(self):
|
|
policy = _policy({
|
|
"mode": "active",
|
|
"tiers": {"plan": {"model": "gpt-6.1-sol",
|
|
"mode": "active"}},
|
|
})
|
|
explicit = decide_routing(
|
|
{"id": "t1", "plan": True, "model": "gpt-5.6-terra"},
|
|
"gpt-5.6-terra", policy=policy)
|
|
self.assertEqual(explicit.source, "explicit")
|
|
self.assertEqual(explicit.chosen_model, "gpt-5.6-terra")
|