Files
meida_backend/tests
xsl f46ecc5eb0 fix(llm): price by call time and stop paying for thinking tokens
Two defects that only surface against the real DeepSeek API, both of which
break the cost model rather than the code:

deepseek-v4-flash enables thinking mode by default. The same single-turn
question took 13.5s with it on and 1.8s with it off, and the thinking tokens
are billed at the output rate. That misses NFR-PERF-02b (15s per turn) and
inflates every conversational call. SceneConfig now carries an explicit
`thinking` flag, all four V1 scenes set it to false, and the provider always
sends the parameter rather than relying on the upstream default. Reasoning
tokens are recorded separately in Usage.tokens_reasoning so the cost of ever
turning it back on is visible.

DeepSeek moves to peak/off-peak pricing on 2026-08-17, with peak (Beijing
09:00-12:00 and 14:00-18:00) at twice the off-peak rate. Reports are generated
mostly during peak hours, so a single blended rate would leave ob_ai_call --
the only input to V2 pricing -- off by nearly a factor of two.
PeakAwareRateTable resolves the rate at call time; cost is computed and stored
then, never recomputed later at whatever the current rate happens to be.

Also here:
- Replace the live API key in the redaction test with a synthetic one. The
  test that proves we redact secrets was itself a secret leak waiting for its
  first commit.
- Correct LLM_COST_RATES_JSON, which held placeholder rates.
- Make ids.SnowflakeGenerator take an injectable clock so rollover and
  clock-rollback are testable without patching a slots class.
- Rename exceptions to the -Error suffix (N818) and clear the remaining ruff,
  mypy and import-linter findings, including two dead lines: a duplicate
  pytestmark assignment and a no-op assert on a None return.

Gates: ruff, mypy (96 files incl. tests), 4/4 import contracts,
detect-secrets, 166 unit + 3 integration tests, migration up/down round-trip.
2026-08-14 00:54:40 +08:00
..

tests/

目录 内容 依赖
unit/ 领域逻辑、LLM 网关、脱敏、错误码 无。不许连库、不许发网络请求
integration/ 带 PostgreSQL / Redis 的链路 make up,或 testcontainers
golden/ 黄金集回归(检索 / 规则 / 报告) 尚未建立,见下
make test-unit                       # 默认只跑 unit(integration 自动跳过)
MEIDA_RUN_INTEGRATION=1 uv run pytest -m integration

覆盖率门槛

pytest 对 packages/llm 与 packages/domain 统计覆盖率,低于阈值即失败。 这两个包是 mypy strict 的范围,也是业务正确性的核心,其他层靠集成测试覆盖。

绝对不要做的事

  • 不要在单测里调用真实的 DeepSeek。scripts/smoke.sh 是唯一会真的花钱的 入口。单测用 httpx.MockTransport 或 tests/unit/test_gateway.py 里的 FakeProvider。
  • 不要 time.sleep()。熔断器、预算闸门都支持注入时钟。
  • 不要断言日志文本。断言行为。

黄金集(还没建,但这是最重要的一道防线)

架构 §16.2:

AI 改代码时最容易破坏的是那些「没有断言保护的隐性行为」。检索链路和规则引擎 必须有一套固定输入 → 期望输出的样本集。

M2 落地知识库时必须同步建立(FR-KB-11,≥ 100 条用例): 检索 Top-5 命中率 ≥ 80%、硬过滤零漏、规则命中正确性、报告结构完整性、 硬性禁忌违反数 = 0。指标退化即阻断合并。

已覆盖的关键不变量

测试 保护的是什么
test_prompt_cache_prefix.py 固定前缀不含易变 token,否则输入成本翻数倍
test_daily_budget.py 日成本超限时拒绝非关键调用,不是只告警
test_gateway.py 路由、故障转移、熔断、结构化重试、每次调用都记成本
test_quota_domain.py 预扣三段式;退还幂等;服务端硬校验
test_task_state.py 任务状态机没有非法跃迁
test_log_redaction.py 手机号 / 令牌 / 密钥 / 诊断结论不进日志
test_response_envelope.py 每个错误码都有文案;配额文案不含价格字样