Skip to content

fix(cache): 修复热缓存误判冷、断点落在 reminder,删除 cache_edits/scope - #429

Merged
njfuzrs merged 5 commits into
mainfrom
fix/prompt-cache-cold-clear-and-breakpoint
Oct 24, 2026
Merged

njfuzrs merged 5 commits into
mainfrom
fix/prompt-cache-cold-clear-and-breakpoint

Conversation

@njfuzrs

@njfuzrs njfuzrs commented Oct 24, 2026

Copy link
Copy Markdown
Owner

改了什么

修复 Context 文档 §6 列出的 5 个 prompt cache 问题,并补上对应的可观测性。

  • 问题 1/2:热缓存被当冷、清理绕过冻结
    • 新增 api/cache-warmth.ts,在 Anthropic provider 出口记录请求发出时刻,进程级保存。距上次 ≥1h(最长可能 TTL)才判冷,不知道就按热。
    • 冷清理改为 ContextManager.applyColdCacheClear(),决策写进会话状态,之后每轮逐字节重放。它在 getCleanedMessages 之前执行,所以 BeforeModel、raw.jsonl、prefix_break 看到的就是真实发出的前缀。
  • 问题 3:断点落在 reminder 上:reminder 块带本地 transient 标记(不上线),messages 区断点打在第一个 transient 块之前。
  • 问题 4:删除 cache_edits:删掉 cached-microcompact.ts 状态机及全部接线、notifyCacheDeletion。
  • 问题 5:删除 cache_control.scope=global。
  • 可观测性:新增 PromptCacheLayout 和 ColdCacheClear 事件,trace/digest.ts 做聚合。

为什么

本机轨迹回放(411 个会话,10-04 到 10-23):

  • 60 次热缓存被判冷并清理,分布在 41/132 个 Anthropic 会话,间隔 p50 114s。根因是 LoopState.lastResponseAt 每条用户消息都会重建。
  • 13% 的请求(691/5182)断点落在 reminder 上,这部分缓存写入是白写的。
  • cache_edits 不是公开 Messages API 字段,网关静默接受,效果无法验证。scope 不在公开 API 里,本仓也从未发过对应的 beta 头。

取舍:阈值从 5min 改成 1h,会少清一些本地 token,换来不在热缓存上制造 miss。详见 Agent Note。

怎么验证的

  • bun run affected-tests:run:581 文件,7566 pass / 0 fail
  • make build:自检通过,没有 will always be undefined 告警
  • lint / format:check / lint:boundary / lint:command-system / typecheck:gate(66/66,无新增)全绿
  • 变异自证:阈值改回 5min、去掉 transient 跳过、去掉冻结重放,三处各自都让对应测试变红,已恢复

上线后的验收(需要真实流量):ColdCacheClear.gap_ms < 1h 的计数为 0;PromptCacheLayout.message_breakpoint_on_transient 占比从 13% 降到 0。

Agent Note:.agents/notes/implemented/bug-fix/2026-10-24-prompt-cache冷热误判与断点落点修复.md

🤖 Generated with Claude Code

njfuzrs and others added 2 commits October 24, 2026 14:05
- 冷热判定:provider 出口记录请求发出时刻(api/cache-warmth.ts),距上次 ≥1h 才算冷,
  不知道按热。旧 LoopState.lastResponseAt 每条用户消息重建,首轮必判冷(回放 60 次/41 会话)
- 冷清理落成 ContextManager 会话状态,之后每轮逐字节重放;在 getCleanedMessages 之前执行,
  BeforeModel/raw.jsonl/prefix_break 看到的就是真实发送前缀
- reminder 块带 transient 标记,messages 区断点打在第一个 transient 之前(回放 13% 请求断点白写)
- 删除 cached-microcompact 状态机与 cache_edits(非公开字段,无法验证效果)
- 删除 cache_control.scope=global(公开 API 无此字段,且未配 beta 头)
- 新增 PromptCacheLayout / ColdCacheClear 事件与 digest 聚合

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@njfuzrs
njfuzrs enabled auto-merge October 24, 2026 06:10
njfuzrs and others added 3 commits October 24, 2026 16:38
…HE_EDITS

本 PR 删除了 cache_edits 与 global scope,这两个变量在源码里已无读取点,
help 里仍保留导致 tests/scripts/help-env-consumers.test.ts 红。同步删除并重新生成 website/ref/env.md。
@njfuzrs
njfuzrs merged commit cc96fbb into main Oct 24, 2026
4 checks passed
@njfuzrs
njfuzrs deleted the fix/prompt-cache-cold-clear-and-breakpoint branch October 24, 2026 09:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant