fix: bring a cached tier back as a tier, not as a bare string
Adding applied_effort to LLMResponse put it through the cache round trip, where JSON stores a StrEnum as its plain value. Rehydrated raw, a hit would hand downstream a str while the annotation says Effort, and every `is Effort.LOW` in the library would quietly answer False on the hit path only -- the same trap thinking_observation already has a coercion for. A value outside this version's vocabulary degrades to None rather than failing the entry: projects sharing one Redis would otherwise keep invalidating each other's writes over an attribution field, and None is the honest reading of a tier this version cannot name.
This commit is contained in:
@@ -53,6 +53,29 @@ def _coerce_observation(raw: Any) -> ThinkingObservation:
|
||||
return ThinkingObservation.UNKNOWN
|
||||
|
||||
|
||||
def _coerce_applied_effort(raw: Any) -> Effort | None:
|
||||
"""缓存里的档位字符串 → 枚举;域外取值降级为 `None`,**不作废整条缓存**。
|
||||
|
||||
与 `_coerce_observation` 同源同向,理由逐条相同: 多项目共用一个 Redis 时,
|
||||
先升级的进程可能写入本版没有的档位名,未升级的进程若把这些条目判成未命中,
|
||||
两个版本就会互相打对方的缓存。归因字段不该有能力废掉内容完好的响应。
|
||||
|
||||
降级到 `None` 而不是别的档: 它的语义是"库不知道这次跑在哪档",对一个读不懂
|
||||
的取值这是唯一诚实的说法——随便挑一档等于替上游声称了一件它没说过的事。
|
||||
"""
|
||||
if raw is None:
|
||||
return None
|
||||
try:
|
||||
return Effort(raw)
|
||||
except ValueError:
|
||||
logger.warning(
|
||||
"缓存条目的 applied_effort 取值 {!r} 不在本版档位词汇内(多半由更新版本的"
|
||||
"进程写入),已降级为 None;响应内容照常复活——归因字段不作废缓存",
|
||||
raw,
|
||||
)
|
||||
return None
|
||||
|
||||
|
||||
def digest_messages(messages: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
||||
"""多模态 content part 先各自 sha256 摘要再参与序列化;文本原文参与。
|
||||
|
||||
@@ -176,6 +199,8 @@ class CacheMW:
|
||||
# 键缺失即升级前写入的旧条目,交给 dataclass 默认值
|
||||
if "thinking_observation" in fields:
|
||||
fields["thinking_observation"] = _coerce_observation(fields["thinking_observation"])
|
||||
if "applied_effort" in fields:
|
||||
fields["applied_effort"] = _coerce_applied_effort(fields["applied_effort"])
|
||||
fields.update(
|
||||
cache_hit=True,
|
||||
latency_ms=0,
|
||||
|
||||
Reference in New Issue
Block a user