refactor: give reasoning decisions their own module
providers.py had been holding two jobs: the registry of what each provider looks like, and the decisions made from those declarations. Adding response-side judgement would have made it the module for everything about reasoning, so the decisions move to thinking.py and the registry keeps only profiles and their lookup. Moving a module breaks any deep-path import of what moved, so the six public symbols are promoted to the package root at the same time. The top level is this library's stated API surface; giving downstream a stable name to import is what makes the next reorganisation harmless. observe_thinking stays unexported — downstream reads the verdict off LLMResponse, and exporting it would be a permanent promise for nothing.
This commit is contained in:
@@ -37,7 +37,7 @@ from polygateway.errors import (
|
||||
SourceDeadError,
|
||||
TransientError,
|
||||
)
|
||||
from polygateway.providers import DEFAULT_CAPABILITIES, get_capability
|
||||
from polygateway.thinking import DEFAULT_CAPABILITIES, get_capability
|
||||
|
||||
_ENV = {k: v for k, v in {**dotenv_values(".env"), **os.environ}.items() if v is not None}
|
||||
_HAS_SOURCE = any(k.split("__")[0] == "LLM" and k.endswith("__API_KEY") for k in _ENV)
|
||||
|
||||
@@ -49,3 +49,28 @@ def test_telemetry_status_exported():
|
||||
assert "TelemetryStatus" in polygateway.__all__
|
||||
assert polygateway.TelemetryStatus is not None
|
||||
assert "TelemetryStatusProvider" not in polygateway.__all__
|
||||
|
||||
|
||||
def test_thinking_public_surface_exported():
|
||||
"""issue #16/#17: 推理决策搬进 `polygateway.thinking` 后,公共符号必须走顶层。
|
||||
|
||||
搬模块本身会断掉 `from polygateway.providers import ThinkingCapability` 这类
|
||||
深路径 import。给下游一个稳定引用点,是以后再重组不再破坏下游的前提——本库
|
||||
的约定是「顶层导出即公共 API 面」。
|
||||
|
||||
`observe_thinking` / `reconcile_thinking` **不**导出: 它们是 transport 内部
|
||||
的裁定与对账,下游读 `LLMResponse.thinking_observation` 即可,导出即多一份
|
||||
永久承诺。
|
||||
"""
|
||||
for name in (
|
||||
"ThinkingCapability",
|
||||
"ThinkingObservation",
|
||||
"ThinkingUnsupportedError",
|
||||
"get_capability",
|
||||
"register_capability",
|
||||
"resolve_thinking",
|
||||
):
|
||||
assert hasattr(polygateway, name), name
|
||||
assert name in polygateway.__all__, name
|
||||
assert "observe_thinking" not in polygateway.__all__
|
||||
assert "reconcile_thinking" not in polygateway.__all__
|
||||
|
||||
@@ -1,18 +1,12 @@
|
||||
"""providers.py 注册表测试(M1 设计 §7;register_provider 为纯函数,无可变全局)。"""
|
||||
|
||||
import pytest
|
||||
from loguru import logger
|
||||
|
||||
from polygateway.providers import (
|
||||
DEFAULT_CAPABILITIES,
|
||||
DEFAULT_PROFILES,
|
||||
ProviderProfile,
|
||||
ThinkingCapability,
|
||||
get_capability,
|
||||
get_provider,
|
||||
register_capability,
|
||||
register_provider,
|
||||
resolve_thinking,
|
||||
)
|
||||
|
||||
|
||||
@@ -75,83 +69,3 @@ class TestPureFunctionRegistration:
|
||||
def test_default_profiles_mapping_is_read_only(self):
|
||||
with pytest.raises(TypeError):
|
||||
DEFAULT_PROFILES["hack"] = None # type: ignore[index]
|
||||
|
||||
|
||||
def _warnings():
|
||||
"""捕获库发出的 WARNING;loguru 不经标准 logging,pytest 的 caplog 抓不到。"""
|
||||
messages: list[str] = []
|
||||
sink_id = logger.add(messages.append, level="WARNING")
|
||||
return messages, sink_id
|
||||
|
||||
|
||||
class TestThinkingCapability:
|
||||
"""issue #5: 能力按 model 登记——同一 provider 内部代际差异是决定性的。"""
|
||||
|
||||
def test_registered_models_carry_evidence(self):
|
||||
"""登记必须附实测证据: 表会过期,没有出处就无从判断该不该信。"""
|
||||
for model in ("MiniMax-M3", "MiniMax-M2.7", "MiniMax-M2.5"):
|
||||
cap = get_capability(model)
|
||||
assert cap is not None and cap.evidence.strip()
|
||||
|
||||
def test_m3_can_disable_but_m2x_cannot(self):
|
||||
assert get_capability("MiniMax-M3").can_disable is True
|
||||
assert get_capability("MiniMax-M2.7").can_disable is False
|
||||
assert get_capability("MiniMax-M2.5").can_disable is False
|
||||
|
||||
def test_unregistered_model_is_unknown(self):
|
||||
assert get_capability("some-brand-new-model") is None
|
||||
|
||||
def test_register_capability_is_pure(self):
|
||||
table = register_capability("x-1", ThinkingCapability(True, "实测"))
|
||||
assert get_capability("x-1", table=table) is not None
|
||||
assert get_capability("x-1") is None # 默认表未被污染
|
||||
|
||||
def test_default_capabilities_mapping_is_read_only(self):
|
||||
with pytest.raises(TypeError):
|
||||
DEFAULT_CAPABILITIES["hack"] = None # type: ignore[index]
|
||||
|
||||
|
||||
class TestResolveThinking:
|
||||
"""五条判定规则(顺序即语义);设计 §5 真值表。"""
|
||||
|
||||
def test_rule1_none_injects_nothing(self):
|
||||
got = resolve_thinking(get_provider("minimax"), None, None, model="MiniMax-M3")
|
||||
assert got == {}
|
||||
|
||||
@pytest.mark.parametrize("enable", [True, False])
|
||||
def test_rule2_unknown_shape_raises_and_points_the_way(self, enable):
|
||||
with pytest.raises(ValueError, match="register_provider") as exc:
|
||||
resolve_thinking(get_provider("openai"), None, enable, model="kimi-k3")
|
||||
assert "extra_body" in str(exc.value)
|
||||
|
||||
def test_rule3_unregistered_model_warns_but_passes(self):
|
||||
messages, sink_id = _warnings()
|
||||
try:
|
||||
got = resolve_thinking(get_provider("minimax"), None, False, model="MiniMax-M9")
|
||||
finally:
|
||||
logger.remove(sink_id)
|
||||
assert got == {"reasoning_effort": "none"}
|
||||
assert any("MiniMax-M9" in m for m in messages)
|
||||
|
||||
def test_rule4_cannot_disable_raises_with_the_model_name(self):
|
||||
cap = get_capability("MiniMax-M2.7")
|
||||
with pytest.raises(ValueError, match="MiniMax-M2.7"):
|
||||
resolve_thinking(get_provider("minimax"), cap, False, model="MiniMax-M2.7")
|
||||
|
||||
def test_rule4_only_blocks_the_off_direction(self):
|
||||
"""关不掉 ≠ 开不了: M2.x 默认就在推理,开的方向不该被拦。"""
|
||||
cap = get_capability("MiniMax-M2.7")
|
||||
got = resolve_thinking(get_provider("minimax"), cap, True, model="MiniMax-M2.7")
|
||||
assert got == {"reasoning_effort": "medium"}
|
||||
|
||||
def test_rule5_normal_path(self):
|
||||
cap = get_capability("MiniMax-M3")
|
||||
assert resolve_thinking(get_provider("minimax"), cap, False, model="MiniMax-M3") == {
|
||||
"reasoning_effort": "none"
|
||||
}
|
||||
|
||||
def test_unknown_shape_beats_capability_check(self):
|
||||
"""第 2 步先于第 4 步: 形态未知时无从注入,能力如何无关紧要。"""
|
||||
cap = ThinkingCapability(can_disable=False, evidence="构造")
|
||||
with pytest.raises(ValueError, match="register_provider"):
|
||||
resolve_thinking(get_provider("openai"), cap, False, model="whatever")
|
||||
|
||||
@@ -6,11 +6,27 @@
|
||||
"""
|
||||
|
||||
import pytest
|
||||
from loguru import logger
|
||||
|
||||
from polygateway.thinking import observe_thinking
|
||||
from polygateway.providers import get_provider
|
||||
from polygateway.thinking import (
|
||||
DEFAULT_CAPABILITIES,
|
||||
ThinkingCapability,
|
||||
get_capability,
|
||||
observe_thinking,
|
||||
register_capability,
|
||||
resolve_thinking,
|
||||
)
|
||||
from polygateway.types import ThinkingObservation
|
||||
|
||||
|
||||
def _warnings():
|
||||
"""捕获库发出的 WARNING;loguru 不经标准 logging,pytest 的 caplog 抓不到。"""
|
||||
messages: list[str] = []
|
||||
sink_id = logger.add(messages.append, level="WARNING")
|
||||
return messages, sink_id
|
||||
|
||||
|
||||
class TestObserveThinking:
|
||||
"""三态裁定: 证据硬度决定优先级,无信号一律 UNKNOWN。"""
|
||||
|
||||
@@ -74,3 +90,76 @@ def test_unknown_strings_are_rejected(bogus):
|
||||
"""非法值必须抛 ValueError: 缓存回放靠它把污染数据挡成"未命中"(设计 §6)。"""
|
||||
with pytest.raises(ValueError):
|
||||
ThinkingObservation(bogus)
|
||||
|
||||
|
||||
class TestThinkingCapability:
|
||||
"""issue #5: 能力按 model 登记——同一 provider 内部代际差异是决定性的。"""
|
||||
|
||||
def test_registered_models_carry_evidence(self):
|
||||
"""登记必须附实测证据: 表会过期,没有出处就无从判断该不该信。"""
|
||||
for model in ("MiniMax-M3", "MiniMax-M2.7", "MiniMax-M2.5"):
|
||||
cap = get_capability(model)
|
||||
assert cap is not None and cap.evidence.strip()
|
||||
|
||||
def test_m3_can_disable_but_m2x_cannot(self):
|
||||
assert get_capability("MiniMax-M3").can_disable is True
|
||||
assert get_capability("MiniMax-M2.7").can_disable is False
|
||||
assert get_capability("MiniMax-M2.5").can_disable is False
|
||||
|
||||
def test_unregistered_model_is_unknown(self):
|
||||
assert get_capability("some-brand-new-model") is None
|
||||
|
||||
def test_register_capability_is_pure(self):
|
||||
table = register_capability("x-1", ThinkingCapability(True, "实测"))
|
||||
assert get_capability("x-1", table=table) is not None
|
||||
assert get_capability("x-1") is None # 默认表未被污染
|
||||
|
||||
def test_default_capabilities_mapping_is_read_only(self):
|
||||
with pytest.raises(TypeError):
|
||||
DEFAULT_CAPABILITIES["hack"] = None # type: ignore[index]
|
||||
|
||||
|
||||
class TestResolveThinking:
|
||||
"""五条判定规则(顺序即语义);设计 §5 真值表。"""
|
||||
|
||||
def test_rule1_none_injects_nothing(self):
|
||||
got = resolve_thinking(get_provider("minimax"), None, None, model="MiniMax-M3")
|
||||
assert got == {}
|
||||
|
||||
@pytest.mark.parametrize("enable", [True, False])
|
||||
def test_rule2_unknown_shape_raises_and_points_the_way(self, enable):
|
||||
with pytest.raises(ValueError, match="register_provider") as exc:
|
||||
resolve_thinking(get_provider("openai"), None, enable, model="kimi-k3")
|
||||
assert "extra_body" in str(exc.value)
|
||||
|
||||
def test_rule3_unregistered_model_warns_but_passes(self):
|
||||
messages, sink_id = _warnings()
|
||||
try:
|
||||
got = resolve_thinking(get_provider("minimax"), None, False, model="MiniMax-M9")
|
||||
finally:
|
||||
logger.remove(sink_id)
|
||||
assert got == {"reasoning_effort": "none"}
|
||||
assert any("MiniMax-M9" in m for m in messages)
|
||||
|
||||
def test_rule4_cannot_disable_raises_with_the_model_name(self):
|
||||
cap = get_capability("MiniMax-M2.7")
|
||||
with pytest.raises(ValueError, match="MiniMax-M2.7"):
|
||||
resolve_thinking(get_provider("minimax"), cap, False, model="MiniMax-M2.7")
|
||||
|
||||
def test_rule4_only_blocks_the_off_direction(self):
|
||||
"""关不掉 ≠ 开不了: M2.x 默认就在推理,开的方向不该被拦。"""
|
||||
cap = get_capability("MiniMax-M2.7")
|
||||
got = resolve_thinking(get_provider("minimax"), cap, True, model="MiniMax-M2.7")
|
||||
assert got == {"reasoning_effort": "medium"}
|
||||
|
||||
def test_rule5_normal_path(self):
|
||||
cap = get_capability("MiniMax-M3")
|
||||
assert resolve_thinking(get_provider("minimax"), cap, False, model="MiniMax-M3") == {
|
||||
"reasoning_effort": "none"
|
||||
}
|
||||
|
||||
def test_unknown_shape_beats_capability_check(self):
|
||||
"""第 2 步先于第 4 步: 形态未知时无从注入,能力如何无关紧要。"""
|
||||
cap = ThinkingCapability(can_disable=False, evidence="构造")
|
||||
with pytest.raises(ValueError, match="register_provider"):
|
||||
resolve_thinking(get_provider("openai"), cap, False, model="whatever")
|
||||
|
||||
Reference in New Issue
Block a user