feat(think): four-tier model routing with budget guard

Corrects a common misconception: the 60s heartbeat must not call any LLM.
It runs the mood decay equation -- microseconds of float math. Giving it a
model is an architecture error, not a resource problem.

Real token spend lands in two places:
  - per-turn input context (ontology + state + dyad + memory)
  - background task frequency

So route by scene, not by time:

  none      rule/math     tick, reach-out decision, fillers, avatar drive
  low       local small   life events, memory gist, fact extract
  standard  mid + short   speculative-response second segment
  high      flagship      main dialogue, proactive message, crisis

Hard constraints:
  - dialogue floor is LOW, never NONE (can get dumber, cannot lose lines)
  - crisis never degrades (product constraint, not perf tuning)
  - degradation is predictable: one rung per request, floor only at
    the hard budget threshold

Cost levers, by actual payoff:
  1. prompt caching (fixed prefix) -- by far the largest
  2. upstream compression (small model condenses 20 memories to 3)
  3. batching (one batch per 30min, not 20 single calls)
  4. swapping to a smaller model -- smallest payoff, hurts persona

Also: cost degradation is narratable. 'Net error -> I zoned out' already
exists in the projection table, so being throttled is in-character and true.

- config.py: tiers / routing / budget, replacing llm+llm_fast+llm_background
- think/: scenes, budget, router, wiring
- models.yaml restructured around tiers
- SPEC gains 9.5
- 20 new tests; 78 total pass; ruff clean
This commit is contained in:
mobai
2026-10-19 20:14:41 +08:00
parent 1012d8bb79
commit 8dea68a25b
10 changed files with 1129 additions and 33 deletions
+67
View File
@@ -1230,6 +1230,73 @@ companion.service (systemd / 计划任务)
单进程,单事件循环。**不拆微服务**——单用户场景拆了只会增加故障面。
### 9.5 模型分层与预算
**先澄清一个常见误解:60 秒心跳不调用任何 LLM。**
心跳执行的是 `s_next = b + (s − b)·exp(−dt/τ) + I`,四个 float 一次 exp,
微秒级。给它配模型是架构错误,不是资源问题。
真正的 token 消耗在两处:**每轮对话的输入上下文**,以及**后台任务的频次**。
#### 9.5.1 四档
| 档 | 用什么 | 场景 | 成本 |
|---|---|---|---|
| `none` | 不调用 | 心跳、主动性判定、填充词、表情驱动、VAD、端点 | ≈0 |
| `low` | 本地小模型 | 生活事件、记忆压缩、事实抽取、输出初筛、关系模式识别 | 电费 |
| `standard` | 中大模型 + 短上下文 | 投机响应的第二段 | 低 |
| `high` | 旗舰 | 主对话台词、主动联系措辞、危机场景 | 按 token |
#### 9.5.2 场景 → 档位
| 场景 | 档位 | 说明 |
|---|---|---|
| `dialogue_main` | high | 人设浓度最高 |
| `dialogue_followup` | standard | 投机响应第二段 |
| `proactive_message` | high | 低频高价值 |
| `crisis` | high | **永不降级** |
| `life_event` | low | 一天一次 |
| `memory_gist` | low | 量最大,批处理 |
| `fact_extract` | low | 批处理 |
| `output_guard` | low | 多数可用规则前置 |
| `pattern_detect` | low | 低频 |
| `filler` / `tick` / `reach_out` / `avatar_drive` | none | 查表或纯计算 |
#### 9.5.3 三条硬约束
1. **主对话下限是 `low`,不是 `none`。** 预算耗尽可以变笨,不能没台词。
2. **危机永不降级。** 产品约束,不是性能优化。
3. **降级可预测。** 单次请求放不下只降一档;到达硬阈值才降到下限。
#### 9.5.4 省钱杠杆(按收益排序)
1. **Prompt 缓存** —— 本体 / 世界法则 / 人设固定放前缀,不随对话变。
命中缓存后单价低一个数量级。**这是最大的单一杠杆,比换模型省得多。**
2. **上游压缩** —— 小模型先把 20 条记忆压成 3 条要点,大模型只收压缩结果。
输入从 4000 降到 800 tokens。
3. **批处理** —— 记忆整合攒 30 分钟一次处理 20 条,而不是 20 次单条调用。
4. **换小模型** —— 收益最小,且直接伤害人设。不作为首选手段。
#### 9.5.5 投机响应(同时降延迟与降成本)
```
t=0 用户说完
t=50 小模型/模板出第一句:"嗯,我在。"
t=300 大模型开始流式生成完整回应
t=800 大模型第一段接上
```
若用户在 800ms 内打断 → 大模型生成被取消 → **省掉这次调用**。
这既是延迟优化,也是成本优化。
#### 9.5.6 降级可被叙事化
大模型不可用时,它"有点走神"。
这既符合人设,又是真的(实现同构的虚构)——投影 φ 里已有 `net.error` → "刚才走神了"。
成本降级不需要对用户隐藏,只需要被正确地叙事化。
---
## 10. 完整行为规格