test: close two always-green holes in the dimension tests
The cache-key test only asserted a hit, so a key degraded to a constant would still pass it. Adding a namespace control group that must miss proves the key still distinguishes inputs; verified by degrading build_cache_key to a constant and watching the case go red. The allow_nan=False branch had no test at all. A ChatRequest built with a nan meta value (bypassing the entry validation, i.e. a future entry point that forgets to validate) must drop the row and not raise; verified red by removing allow_nan=False. Also restore the read-only file permissions in a finally block, so a failing assertion does not get masked by a PermissionError from tmp_path cleanup; rename the warnings fixture to captured_warnings so it stops shadowing the stdlib module; and drop a downstream business term from a fixture value (zero-business-assumption rule).
This commit is contained in:
@@ -318,7 +318,7 @@ CREATE TABLE llm_calls (
|
||||
_PRE_TENANT_INSERT = (
|
||||
"INSERT INTO llm_calls (call_id, model, provider, source_name, messages, response, "
|
||||
"prompt_tokens, completion_tokens, usage_source, latency_ms) "
|
||||
"VALUES ('old-row', 'm', 'p', 's1', '[]', 'contract text', 1, 2, 'measured', 10)"
|
||||
"VALUES ('old-row', 'm', 'p', 's1', '[]', 'old body', 1, 2, 'measured', 10)"
|
||||
)
|
||||
|
||||
|
||||
@@ -399,12 +399,16 @@ class TestSQLiteCallerDimensionsAcceptance:
|
||||
conn.close()
|
||||
db.chmod(0o444)
|
||||
|
||||
recorder = SQLiteRecorder(db) # 不得抛
|
||||
assert recorder._conn is not None # 补列失败 ≠ recorder 失能
|
||||
await _record_minimal(recorder, call_id="doomed") # 只读库写不进,但不得抛
|
||||
recorder.close()
|
||||
# finally 还原权限位: 任一断言先失败时,不还原会让 tmp_path 清理连带报错,
|
||||
# 把"某条断言失败"的真因盖成一个无关的 PermissionError
|
||||
try:
|
||||
recorder = SQLiteRecorder(db) # 不得抛
|
||||
assert recorder._conn is not None # 补列失败 ≠ recorder 失能
|
||||
await _record_minimal(recorder, call_id="doomed") # 只读库写不进,但不得抛
|
||||
recorder.close()
|
||||
finally:
|
||||
db.chmod(0o644)
|
||||
|
||||
db.chmod(0o644) # 还原,让 tmp_path 清理不受阻
|
||||
stale = sqlite3.connect(db)
|
||||
assert [r[1] for r in stale.execute("PRAGMA table_info(llm_calls)")] == (
|
||||
_EXPECTED_COLUMNS[:-2]
|
||||
@@ -875,6 +879,25 @@ class TestEmitterCallerDimensions:
|
||||
)
|
||||
assert "研发" in rec.rows[0]["meta"]
|
||||
|
||||
async def test_non_finite_meta_value_drops_the_row_instead_of_poisoning_it(self):
|
||||
"""入口失守时 `allow_nan=False` 的真实结果: 整行降级丢弃,且不抛给调用方。
|
||||
|
||||
直接构造带 `nan` 的 `ChatRequest`(绕过 `validate_caller_dimensions` 这道
|
||||
主防线,模拟将来某个新入口忘记校验)。没有 `allow_nan=False` 时,
|
||||
`json.dumps` 会写出裸 `NaN` 字面量——PG 的 JSONB 会拒收,但 **SQLite 的
|
||||
`meta` 是 TEXT 列不做校验**,那串非法 JSON 会被静默存进去,污染此后一切
|
||||
按 JSON 解析 meta 的分析。宁可丢一行遥测,也不要一行毒数据。
|
||||
|
||||
同时断言不抛: 遥测的降级方向是"静默降级"(铁律),把调用方的一次正常
|
||||
业务调用因为一个维度值炸掉,方向反了。
|
||||
"""
|
||||
rec = _MemoryRecorder()
|
||||
req = ChatRequest(messages=[{"role": "user", "content": "hi"}], meta={"k": float("nan")})
|
||||
await TelemetryEmitter(rec).emit_terminal_failure(
|
||||
request=req, call_id="c", latency_ms=1, error="dead"
|
||||
)
|
||||
assert rec.rows == []
|
||||
|
||||
|
||||
class TestCostWithCachedTier:
|
||||
"""issue #3: 命中部分按缓存单价计费,避免 cost 系统性高估。"""
|
||||
|
||||
Reference in New Issue
Block a user