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:
2026-10-14 23:39:50 -04:00
parent 39a319e34e
commit 48ba36dd94
4 changed files with 203 additions and 36 deletions
+24
View File
@@ -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}"
+114
View File
@@ -1002,3 +1002,117 @@ async def test_openai_tool_only_cache_hits_without_origin():
hit = await mw(request, terminal)
assert hit.cache_hit and terminal.calls == 0
assert hit.completion == completion
# —— 1.3.10(#30/#31): 可缓存 ⊂ 可交付,读写共用同一判据(设计 §6)——
_OK_COMPLETION = OpenAIChatCompletion({"role": "assistant", "content": "ok"}, "stop", "up", None)
_THINK_LENGTH = OpenAIChatCompletion(
{"role": "assistant", "reasoning_content": "t"}, "length", "up", None
)
_THINK_STOP = OpenAIChatCompletion(
{"role": "assistant", "reasoning_content": "t"}, "stop", "up", None
)
@pytest.mark.parametrize(
("mode", "response"),
[
("text", _resp(stream_end="salvaged")),
("text", _resp(finish_reason="length")),
("text", _resp(finish_reason="content_filter")),
("text", _resp(finish_reason="end_turn")),
("openai", _resp(content="", finish_reason="length", completion=_THINK_LENGTH)),
("openai", _resp(content="", finish_reason="stop", completion=_THINK_STOP)),
],
)
async def test_uncacheable_results_are_delivered_but_not_written(mode, response):
"""截断/空正文/非自然结束: 照常交回,但不写缓存,下次回源。"""
backend = InMemoryCache()
mw = _mw(backend)
terminal = _Terminal(response)
request = ChatRequest(messages=_MSGS, response_mode=mode)
assert (await mw(request, terminal)) is response
assert (await mw(request, terminal)).cache_hit is False
assert terminal.calls == 2 and not backend._store
def _stored(mode, **fields):
"""直接注入一条缓存原文(模拟旧版本或别的进程写入)。"""
backend = InMemoryCache()
data = dataclasses.asdict(_resp(**fields))
data.pop("structured_data", None)
data.pop("call_stats", None)
key = build_cache_key("m", _MSGS, "proj", None, response_mode=mode)
backend._store[key] = (float("inf"), json.dumps(data))
return backend, key
async def _hit(backend, mode):
terminal = _Terminal(_resp(content="fresh", completion=_OK_COMPLETION, finish_reason="stop"))
response = await _mw(backend)(ChatRequest(messages=_MSGS, response_mode=mode), terminal)
return response, terminal.calls
@pytest.mark.parametrize(
("mode", "fields"),
[
("text", {"stream_end": "salvaged"}),
("text", {"stream_end": "bogus"}),
("text", {"finish_reason": "length"}),
("text", {"finish_reason": 5}),
("openai", {"content": "", "completion": _THINK_LENGTH, "finish_reason": "length"}),
],
)
async def test_injected_uncacheable_entries_read_as_miss(mode, fields):
"""读路径对存储原值判定(覆盖之前): 不可缓存即 miss 回源。"""
backend, _ = _stored(mode, **fields)
response, calls = await _hit(backend, mode)
assert response.cache_hit is False and calls == 1
async def test_legacy_fidelity_length_entry_falls_back_to_completion_finish():
"""1.3.9 保真条目无顶层 finish: 回退 completion.finish_reason 识别为不可缓存。"""
completion = OpenAIChatCompletion({"role": "assistant", "content": "a"}, "length", None, None)
backend, key = _stored("openai", completion=completion)
data = json.loads(backend._store[key][1])
for name in ("finish_reason", "stream_end"):
data.pop(name)
backend._store[key] = (float("inf"), json.dumps(data))
response, calls = await _hit(backend, "openai")
assert response.cache_hit is False and calls == 1
async def test_legacy_text_entry_without_new_fields_still_hits():
"""1.3.9 文本条目无新键: 仍命中;命中 stream_end=None。"""
backend, key = _stored("text")
data = json.loads(backend._store[key][1])
for name in ("finish_reason", "stream_end"):
data.pop(name)
backend._store[key] = (float("inf"), json.dumps(data))
response, calls = await _hit(backend, "text")
assert response.cache_hit is True and calls == 0
assert response.stream_end is None and response.finish_reason is None
async def test_hit_resets_stream_end_and_keeps_effective_finish():
"""命中: stream_end 置 None;finish_reason 取判定所用值(顶层缺省时回退)。"""
backend, key = _stored("openai", completion=_OK_COMPLETION, stream_end="eof_after_finish")
data = json.loads(backend._store[key][1])
data.pop("finish_reason")
backend._store[key] = (float("inf"), json.dumps(data))
response, calls = await _hit(backend, "openai")
assert response.cache_hit is True and calls == 0
assert response.stream_end is None and response.finish_reason == "stop"
async def test_new_fields_roundtrip_through_cache():
"""两新字段随序列化往返;命中只重置 stream_end。"""
backend = InMemoryCache()
mw = _mw(backend)
terminal = _Terminal(_resp(finish_reason="stop", stream_end="eof_after_finish"))
await mw(ChatRequest(messages=_MSGS), terminal)
stored = json.loads(next(iter(backend._store.values()))[1])
assert stored["finish_reason"] == "stop" and stored["stream_end"] == "eof_after_finish"
hit = await mw(ChatRequest(messages=_MSGS), terminal)
assert hit.cache_hit is True and hit.finish_reason == "stop" and hit.stream_end is None