fix(runtime): keep the Anthropic cache breakpoint on replayed history #6041
All Vouch review confidence checks passed
Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.52 · 650 lines diff across 8 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.388
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #6041"
general
weight: 0.35
Astro-Han
<details open>
<summary>English</summary>
Summary
On native Anthropic connections, Maka enables automatic prompt caching with the request-level
cache_controlfield (#1420). Anthropic then places the breakpoint on the last cacheable block. Since #5740, every Turn step appends request-only context after the history: theruntime.environmentsnapshot and plugincontexts. The AI SDK merges that text into the last user message, so the breakpoint lands on it. Later requests never repeat that block at that position, so each request paid a cache write that no later request could read.This PR keeps the context request-only and moves the cache entry onto the history.
AiSdkTurnpasseshistoryMessageCounttoModelAdapter.startStream. When the resolved provider options carryanthropic.cacheControl, the adapter marks the last history message with the same value. Step N+1 and the first request of the next Turn then read the prefix that step N wrote.Unchanged behavior:
providerType: 'anthropic'setsanthropic.cacheControl, so MiniMax, Kimi, and custom Anthropic-protocol connections get no marker.The context stays out of the system prompt on purpose. Its timestamp changes on every Turn, so that placement would invalidate the full history each Turn.
Fixes #6040
Verification
keeps the cache breakpoint on durable history before request-only contextdrives a two-step tool Turn throughgetAIModelagainst a stubbed Anthropic stream. It fails before the change (no block-level marker; the context is the last block) and passes after it.adds no cache breakpoint on Anthropic-protocol connections without automatic cachingpasses before and after. When the marker ignoresanthropic.cacheControl, it is the only failure acrossai-sdk-backend.test.jsand the 323 related tests listed below.npm run build,npm run lint,npm run format:check, and@maka/runtimetypecheck passed.ai-sdk-backend.test.js256/256.model-adapter,memory-extraction,mid-turn-capacity-backend,provider-conformance,provider-contract-matrix, andcomputer-use-provider-protocol323/323.runtime-hostexecution-model-composition.test.js41/41.npm run typecheckandknipforapps/desktopandpackages/uipassed.@maka/runtimetest:dist: 3825 passed, 12 skipped, 1 failed. The failure isowned-child-process.test.js› "a supervisor fault is reported as a failure before its tree is stopped". It also failed in 4 of 6 isolated runs and does not import the changed modules.AI use
Select exactly one:
Tool(s) and scope: Devin (Claude Opus 5.5) diagnosed the issue, implemented the change, wrote the tests, and ran the local verification and caching model. The commits carry a
Generated-bytrailer.Checklist
Does this PR entail a change in behavior?
</details>
<details>
<summary>中文</summary>
摘要
对原生 Anthropic 连接,Maka 通过请求级的
cache_control字段开启自动提示缓存(#1420)。Anthropic 会把断点放在最后一个可缓存块上。自 #5740 起,每个 Turn 的每一步都会在历史之后追加仅用于本次请求的上下文:runtime.environment快照和插件的contexts。AI SDK 会把这段文本并入最后一条用户消息,于是断点落在它上面。后续请求在同一位置不会重复这个块,因此每个请求都支付了缓存写入费用,却没有任何后续请求能读到。这个 PR 保持上下文只用于本次请求,把缓存条目移到历史上。
AiSdkTurn向ModelAdapter.startStream传入historyMessageCount。当解析后的服务商选项包含anthropic.cacheControl时,适配层用同一个值标记最后一条历史消息。这样第 N+1 步和下一个 Turn 的第一个请求都能读取第 N 步写入的前缀。保持不变的行为:
providerType: 'anthropic'会设置anthropic.cacheControl,所以 MiniMax、Kimi 和自定义的 Anthropic 协议连接都不会被加标记。上下文有意不放进系统提示词。它的时间戳每个 Turn 都会变化,放在那里会让整段历史的缓存在每个 Turn 失效。
Fixes #6040
验证
keeps the cache breakpoint on durable history before request-only context通过getAIModel对接一个模拟的 Anthropic 流,跑一个两步的工具调用 Turn。修改前失败(没有块级标记,上下文是最后一个块),修改后通过。adds no cache breakpoint on Anthropic-protocol connections without automatic caching在修改前后都通过。当标记不再检查anthropic.cacheControl时,在ai-sdk-backend.test.js和下面所列的 323 个相关测试中,只有它失败。npm run build、npm run lint、npm run format:check和@maka/runtime的类型检查均通过。ai-sdk-backend.test.js256/256。model-adapter、memory-extraction、mid-turn-capacity-backend、provider-conformance、provider-contract-matrix和computer-use-provider-protocol共 323/323。runtime-host的execution-model-composition.test.js41/41。npm run typecheck以及apps/desktop、packages/ui的knip均通过。@maka/runtime的test:dist:3825 个通过,12 个跳过,1 个失败。失败的是owned-child-process.test.js› "a supervisor fault is reported as a failure before its tree is stopped"。它单独运行 6 次中也失败 4 次,并且不引用本次修改的模块。AI 使用
只选一项:
工具及范围:Devin(Claude Opus 5.5)完成了问题诊断、代码实现、测试编写,以及本地验证和缓存模型。提交带有
Generated-by尾注。检查清单
这个 PR 是否带来行为变化?
</details>