← Back to Pull requests

fix(runtime): keep the Anthropic cache breakpoint on replayed history #6041

Closed
unknown closed this pull request without merging on Oct 10, 2026 10:46 UTC
View on GitHub
unknown unknown commented on Oct 10, 2026 06:11 UTC
Author

<details open>
<summary>English</summary>

Summary

On native Anthropic connections, Maka enables automatic prompt caching with the request-level cache_control field (#1420). Anthropic then places the breakpoint on the last cacheable block. Since #5740, every Turn step appends request-only context after the history: the runtime.environment snapshot and plugin contexts. 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. AiSdkTurn passes historyMessageCount to ModelAdapter.startStream. When the resolved provider options carry anthropic.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:

  • Only providerType: 'anthropic' sets anthropic.cacheControl, so MiniMax, Kimi, and custom Anthropic-protocol connections get no marker.

  • A request without request-only context gets no marker, so its body is unchanged.

  • The marker goes on the dispatch copy. Durable message objects, and the memory-extraction positions keyed by them, stay unchanged.

  • Request-level automatic caching stays on. Both markers use the same TTL.
  • 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

  • New test keeps the cache breakpoint on durable history before request-only context drives a two-step tool Turn through getAIModel against a stubbed Anthropic stream. It fails before the change (no block-level marker; the context is the last block) and passes after it.

  • New test adds no cache breakpoint on Anthropic-protocol connections without automatic caching passes before and after. When the marker ignores anthropic.cacheControl, it is the only failure across ai-sdk-backend.test.js and the 323 related tests listed below.

  • With this change applied: npm run build, npm run lint, npm run format:check, and @maka/runtime typecheck passed. ai-sdk-backend.test.js 256/256. model-adapter, memory-extraction, mid-turn-capacity-backend, provider-conformance, provider-contract-matrix, and computer-use-provider-protocol 323/323. runtime-host execution-model-composition.test.js 41/41.

  • Before the latest rebase: root npm run typecheck and knip for apps/desktop and packages/ui passed. @maka/runtime test:dist: 3825 passed, 12 skipped, 1 failed. The failure is owned-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.

  • A local model of Anthropic's documented caching rules replayed the request bodies from a two-Turn, six-request session. Estimated cache reads went from 0% to 76% of input tokens.
  • AI use

    Select exactly one:

  • No generative tool made a substantive contribution

  • Generative tooling made a substantive contribution
  • 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-by trailer.

    Checklist

  • Tests cover the change and fail without it

  • Lint, format, typecheck and the affected suites pass locally
  • Does this PR entail a change in behavior?

  • Yes — described under Summary above

  • No
  • </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 协议连接都不会被加标记。

  • 没有仅用于本次请求的上下文时不加标记,请求体保持不变。

  • 标记加在发送用的副本上。持久消息对象,以及以它们为键的记忆提取位置,都保持不变。

  • 请求级自动缓存继续开启。两个标记使用相同的 TTL。
  • 上下文有意不放进系统提示词。它的时间戳每个 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.js 256/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.js 41/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 次,并且不引用本次修改的模块。

  • 用本地实现的 Anthropic 文档缓存规则模型,回放了一个两个 Turn、六个请求的会话的请求体。估算的缓存读取占输入 token 的比例从 0% 提高到 76%。
  • AI 使用

    只选一项:

  • 没有生成式工具做出实质性贡献

  • 生成式工具做出了实质性贡献
  • 工具及范围:Devin(Claude Opus 5.5)完成了问题诊断、代码实现、测试编写,以及本地验证和缓存模型。提交带有 Generated-by 尾注。

    检查清单

  • 测试覆盖了这次修改,并且没有这次修改时会失败

  • 本地的 lint、格式、类型检查和受影响的测试套件均通过
  • 这个 PR 是否带来行为变化?

  • 是 —— 已在上方摘要中说明

  • 否
  • </details>

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: Astro-Han Review duration: 274m
    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
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.5% · Safe to merge
    20.5%
    Repository