fix: close the failure modes review found in the new code
Three of them were the same shape as the bug this branch exists to fix: something goes wrong, the library swallows it, and the caller is left with a number that means the opposite of what happened. The throttle key had no source in it. Five sources on one model is the normal case here, so the first one to break would warn once and silence the other four for the life of the process, and the message never said which gateway to look at. An unknown verdict in a cached entry threw away the whole response. The rehydrator tolerates unknown fields but not unknown values of a known field, so two library versions sharing a Redis would each invalidate the other's entries: halved hit rate, and the only log line says the cache rebuild failed. A purely observational field should not be able to void a response whose content is intact. Normalising for telemetry now degrades instead of raising, both for a bare string and for a value outside the domain. Either one used to reach the same except and cost the whole row, which is exactly how 1.3.0 lost nineteen calls without anyone noticing.
This commit is contained in:
@@ -55,6 +55,31 @@ def _canonical_meta_json(meta: Mapping[str, Any]) -> str:
|
||||
return json.dumps(dict(meta), sort_keys=True, ensure_ascii=False, allow_nan=False)
|
||||
|
||||
|
||||
def _normalize_observation(raw: object) -> str:
|
||||
"""三态裁定 → 落库用的裸 str;不是枚举也不在取值域时降级为 `unknown` 并告警。
|
||||
|
||||
**不写 `raw.value`**: `LLMResponse` 是无运行时校验的 frozen dataclass,下游
|
||||
(尤其迁移期的测试替身)写 `LLMResponse(..., thinking_observation="observed")`
|
||||
完全自然、`==` 比较照常成立,而 `.value` 会当场抛 `AttributeError`,被 `_record`
|
||||
的 `except Exception` 吞成一条泛化 warning —— 丢的不是这一列,是**整行**,而
|
||||
"遥测必录"是铁律。
|
||||
|
||||
域外取值同样只降级不抛: 直接 `ThinkingObservation(raw)` 会抛 `ValueError`,
|
||||
落到同一个 `except` 上、同样丢整行,那只修好了裸 str 一半(口误值对测试替身
|
||||
一样自然)。降级到 `unknown` 是诚实的——库确实判不出这个取值的含义,而单独
|
||||
一条点名取值的 warning 保证它不被掩盖(P5 不许默认值掩盖错误)。
|
||||
"""
|
||||
try:
|
||||
return ThinkingObservation(raw).value
|
||||
except ValueError:
|
||||
logger.warning(
|
||||
"thinking_observation 取值 {!r} 不在取值域内,本行降级记为 unknown"
|
||||
"(其余列照常落库);调用方应传 ThinkingObservation 成员",
|
||||
raw,
|
||||
)
|
||||
return ThinkingObservation.UNKNOWN.value
|
||||
|
||||
|
||||
def _cap_text(text: str, cap: int | None) -> str:
|
||||
"""超出 cap 时头部硬切并附省略标记 `…(略 N 字)`;cap 为 None 原样返回。"""
|
||||
if cap is None or len(text) <= cap:
|
||||
@@ -285,7 +310,9 @@ class TelemetryEmitter:
|
||||
model_reported: str | None,
|
||||
sampling: str | None,
|
||||
reasoning_tokens: int | None,
|
||||
# issue #16: 枚举形态进来,取 `.value` 后才下沉(归一化同样在本方法内收口)
|
||||
# issue #16: 枚举形态进来,归一化成裸 str 后才下沉(收口在 `_record` 内)。
|
||||
# 注解是契约,但 `LLMResponse` 无运行时校验,故 `_normalize_observation`
|
||||
# 仍按外部输入防御——违约的代价不该是丢掉整行遥测
|
||||
thinking_observation: ThinkingObservation,
|
||||
# issue #11: 未归一化的调用方维度,归一化在本方法内收口(recorder 只落库)
|
||||
tenant_id: str | None,
|
||||
@@ -339,10 +366,10 @@ class TelemetryEmitter:
|
||||
# 对所有人永久不可见,空串则可用一条 SQL 审计出未归属的行
|
||||
tenant_id=tenant_id or "",
|
||||
meta=_canonical_meta_json(meta),
|
||||
# 取 `.value` 落裸 str: `StrEnum` 虽是 `str` 子类,asyncpg 的参数
|
||||
# 编码对子类不保证接受,而遥测写失败只降级成一条 warning——不会当场
|
||||
# 炸,只会让 Postgres 那一路悄悄少一列数据
|
||||
thinking_observation=thinking_observation.value,
|
||||
# 落裸 str: `StrEnum` 虽是 `str` 子类,asyncpg 的参数编码对子类不
|
||||
# 保证接受,而遥测写失败只降级成一条 warning——不会当场炸,只会让
|
||||
# Postgres 那一路悄悄少一列数据
|
||||
thinking_observation=_normalize_observation(thinking_observation),
|
||||
)
|
||||
except asyncio.CancelledError:
|
||||
raise
|
||||
|
||||
Reference in New Issue
Block a user