fix: gate cache writes and reads on a shared cacheability verdict
Cacheable is now a strict subset of deliverable: salvaged streams, content-less results and length/filter/unknown finishes are delivered but never written, and the read path applies the same verdict to the stored values before hit overrides (invalid stream_end or non-string finish read as a miss; 1.3.9 fidelity entries fall back to the completion's finish). Hits reset stream_end. The legacy _has_completion_output helper is folded into _is_cacheable. Red: 13 tests in tests/unit/test_cache.py failed before this change (uncacheable results were written and replayed; new fields not reset).
This commit is contained in:
@@ -30,6 +30,13 @@ def _resp(content="cached"):
|
||||
return LLMResponse(content, "", "m", "p", 1, 2, 30, None, None, False, "orig")
|
||||
|
||||
|
||||
def _const(response):
|
||||
async def terminal(request):
|
||||
return response
|
||||
|
||||
return terminal
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
async def redis_cache():
|
||||
import redis.asyncio as aioredis
|
||||
@@ -82,6 +89,23 @@ class TestRealRedisRoundtrip:
|
||||
assert first.cache_hit is False and second.cache_hit is True
|
||||
assert calls["n"] == 1
|
||||
|
||||
async def test_completion_verdict_fields_roundtrip(self, redis_cache, namespace):
|
||||
"""1.3.10: finish_reason/stream_end 经真实 Redis 往返;不可缓存结果不落库。"""
|
||||
mw = CacheMW(
|
||||
backend=redis_cache, model_fingerprint="m", default_namespace=namespace, ttl_s=60
|
||||
)
|
||||
from dataclasses import replace
|
||||
|
||||
ok = replace(_resp(), finish_reason="stop", stream_end="eof_after_finish")
|
||||
first = await mw(ChatRequest(messages=_MSGS), _const(ok))
|
||||
second = await mw(ChatRequest(messages=_MSGS), _const(ok))
|
||||
assert first.cache_hit is False and second.cache_hit is True
|
||||
assert second.finish_reason == "stop" and second.stream_end is None
|
||||
truncated = replace(_resp(), stream_end="salvaged")
|
||||
other = [{"role": "user", "content": "integration-salvaged"}]
|
||||
await mw(ChatRequest(messages=other), _const(truncated))
|
||||
assert await redis_cache.get(build_cache_key("m", other, namespace, None)) is None
|
||||
|
||||
async def test_namespace_isolation_on_shared_instance(self, redis_cache, namespace):
|
||||
"""多项目共用同一 Redis 时,namespace 不同绝不互相命中(防毒化铁律)。"""
|
||||
other = f"pgw:test:{uuid.uuid4().hex}"
|
||||
|
||||
Reference in New Issue
Block a user