fix(web): keep consultation conclusions across turns and surface cache hits (BUG-555, BUG-556)

Session history was silently clipped to the first 4000 characters of the last 12 messages, so follow-ups could not see timing or audit tables. Keep an append-only tail plus a checkpoint summary, retry overflow in the same request, and expose cache hit rate in admin usage.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Jesse_Chen
2026-09-06 15:16:18 +08:00
parent 9ce7a374ed
commit bf8ad0d1ff
29 changed files with 1296 additions and 101 deletions
@@ -0,0 +1,61 @@
# PROGRESS · 普通对话上下文窗口与模型缓存(2026-09-06)
工作树:`.worktrees/consultation-context-and-cache-20260906`
分支:`codex/consultation-context-and-cache-20260906`
基线:任务书写 `origin/staging` @ `d8a0f615`;开工时 `origin/staging` 已到 `9ce7a374`(含 BUG-551/552/553),以当时远端为准。
未改 `page.tsx`、生时校正链路、Skill 正文、引擎。客户端零改动。迁移只加 `chat_sessions.context_summary`。未 bump Skill。
| 任务 | 状态 | BUG |
| --- | --- | --- |
| 5.1 摘要 + 尾巴历史 + 按模型预算 + 溢出重试 | 完成 | BUG-555 |
| 5.2 后台缓存可见 | 完成 | BUG-556 |
| 5.3 Anthropic 历史断点 | 完成 | BUG-556 |
| 5.4 共享方法段搬进系统块 | 完成 | BUG-556 |
| 5.5 BUG_HISTORY / CHANGELOG / testing | 完成 | — |
## 实现要点
- 历史 = `context_summary` 之后的全部消息。超 `historyBudgetChars` 时从最旧整条丢;单条 12,000 字截断并写省略标记。
- 摘要在最后一条用户消息、时间行之后、问题之前。检查点在 `complete_consultation_response` 成功后异步触发,不扣点,15 秒 ref 超时,乐观并发。
- 溢出识别后同一请求把历史缩到摘要 + 最后一对再 `stream` 一次。咨询公共事件是封闭联合,不能发 rectification 的 `attempt.reset`,所以重试留在服务端。
- 用量聚合按 `actual_model_id` 给出 7/30 天缓存命中;列表加 `cacheReadTokens``costMicrousd` 计价口径未改。
## 系统块 / 工具结果字节
| 块 | 之前 | 之后 | 变化 |
| --- | --- | --- | --- |
| `jyotishSkillMethodBlock` | 28,877 B | 35,323 B | +6,446 B(任务预期 +6.4 KB |
| `<jyotish-shared-method>` | 无 | 6,396 B | Full-spectrum + Event judgment skeleton |
| 单领域 career `methodology.sections` 正文 | 约 13.4 KB(含上述两段) | 7,043 B | 只留 baseline + 领域两段 |
| 三领域 career/marriage/wealth 正文 | 约 25.3 KB | 18,914 B | 同上 |
9 段运行时节选与 `providesSkillDiscovery: "on-demand"` 未回退。
## 既有断言改动
| 文件 | 原值 | 新值 | 原因 |
| --- | --- | --- | --- |
| `consultation-session-history` | 最近 12 条 × 头部 4000 字 | 按模型预算从最旧丢整条;单条 12,000 + 省略标记 | BUG-555 静默砍结论 |
| `consultation-methodology` | sections 含 Full-spectrum / Event judgment skeleton | 这两段在系统块;领域清单仍随工具结果 | 工具结果不在缓存前缀里 |
| `chat-session-authority` / consult select | `...,chart_profile_role` | 另加 `context_summary` | 检查点读写 |
| `application-billing-contract` | 三处 `usages.push(result.totalUsage)` | 一处,在 `streamWithOverflowRetry` | 溢出重试共用首次 stream |
| `admin-usage-aggregate-contract` | 只有 30 天功能用量 | 另加 cache 7/30 天窗口 | BUG-556 命中率不可见 |
## 测试
| 命令 | 结果 |
| --- | --- |
| 本单相关 `npx tsx --test`history / summary / cache contract / methodology / skill-binding / generation-settings / admin usage / onboarding-route | 58 pass / 0 fail / cancelled 0;摘要超时用例 cancelled 0 |
| `./node_modules/.bin/tsc --noEmit`(先删 `.next` | 0 错 |
| 改动文件 eslint `--quiet` | 0 error |
| `page.tsx` | 未改 |
| `npm run build -- --webpack` | 编译通过;类型检查停在既有 `dossierResponse` 路由导出(与 BUG-551/553 相同),未打印 `○ /` |
| 全量 `npx tsx --test tests/*.test.ts`(排除 `tests/database-*.test.ts` | 2726 tests / 2712 pass / 14 failDocker 地址池耗尽 + 1 条 onboarding 超时竞态(单跑通过) |
| `npm run test:db` | **blocked**:见 `BLOCKED.md`。迁移在一次性 Postgres 上经 stub 表语法检查通过 |
## 收尾限制
- 溢出重试只包住 `agent.stream()` 抛错;流开始之后供应商才报超窗时不会第二次缩窗(咨询公共事件不能发 `attempt.reset`)。
- 摘要失败只 `console.warn("session context summary failed", error)`,不打印摘要正文。
- `costMicrousd` 仍按全价输入单价。