test: guard reasoning-free telemetry through real client paths

This commit is contained in:
2026-09-09 01:34:36 -04:00
parent d0078c1be5
commit 47488ee4fd
4 changed files with 233 additions and 0 deletions
+87
View File
@@ -2839,3 +2839,90 @@ class TestPostgresFailureClassification:
assert [s for s in conn.statements if s.startswith("INSERT INTO llm_calls")]
assert recorder.telemetry_status.degraded is False
class TestConcurrentReasoningPathContracts:
"""真实治理链路并发时,按逻辑调用归组且 attempts 不串档。"""
async def test_chat_and_reasonless_clients_keep_distinct_rows(self):
from polygateway.errors import TransientError
from tests.unit.test_client import _client as chat_client
from tests.unit.test_client import _source, _sse
from tests.unit.test_embedding import _embed_client
from tests.unit.test_embedding import _src as embed_source
from tests.unit.test_ocr_client import _client as ocr_client
from tests.unit.test_ocr_client import _src as ocr_source
recorder = _MemoryRecorder()
calls = 0
def handler(request):
nonlocal calls
calls += 1
if calls == 1:
import httpx
return httpx.Response(503, json={"error": {"message": "retry"}})
return _sse()
chat = chat_client(
sources=[_source(provider="zhipu", model="glm-5.3", effort_fallback="nearest")],
handler=handler,
telemetry=recorder,
retry=RetryPolicy(3, 0.001, 0.01),
)
embed, _ = _embed_client(
[embed_source(enable_thinking=True)],
[TransientError("retry"), "ok"],
telemetry=recorder,
)
ocr, _, _ = ocr_client(
[ocr_source(enable_thinking=True)],
[TransientError("retry"), "text"],
telemetry=recorder,
)
try:
await asyncio.gather(
chat.chat([], reasoning_effort="medium", session_id="run", parent_call_id="chat"),
embed.embed(["text"], session_id="run", parent_call_id="embed"),
ocr.recognize_text(b"image", session_id="run", parent_call_id="ocr"),
)
groups = {
name: [r for r in recorder.rows if r["parent_call_id"] == name]
for name in ("chat", "embed", "ocr")
}
assert len(recorder.rows) == 6
assert len({r["call_id"] for r in recorder.rows}) == 6
assert all(r["session_id"] == "run" for r in recorder.rows)
assert [r["reasoning_effort"] for r in groups["chat"]] == ["medium", "low"]
for name in ("embed", "ocr"):
assert len(groups[name]) == 2
assert [r["reasoning_effort"] for r in groups[name]] == [None, None]
finally:
await chat._transport.aclose()
await chat.aclose()
await embed.aclose()
await ocr.aclose()
async def test_chat_sugar_failure_records_auto_through_retry(self):
import httpx
from polygateway.errors import AllSourcesExhausted
from tests.unit.test_client import _client, _source
recorder = _MemoryRecorder()
client = _client(
sources=[_source(model="qwen3.7-plus", enable_thinking=True)],
handler=lambda request: httpx.Response(503),
telemetry=recorder,
retry=RetryPolicy(1, 0.001, 0.01),
)
try:
with pytest.raises(AllSourcesExhausted):
await client.chat([])
attempts = [r for r in recorder.rows if r["source_name"]]
assert len(attempts) == 1
assert attempts[0]["reasoning_effort"] == "auto"
assert attempts[0]["error"]
finally:
await client._transport.aclose()