Repository navigation
fix(anthropic): preserve tool cache when system prompts change - #140804
Conversation
Share cache checkpoint allocation across managed and SDK Messages requests. Preserve OAuth billing text, reserve history anchors, and respect tool and TTL compatibility.
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
|
Codex review: blocked before merge. Reviewed September 7, 2026, 1:27 AM ET / 05:27 UTC. ClawSweeper reviewWhat this changesShares Anthropic request-cache allocation across both Messages builders so unchanged tools remain independently cacheable when system prompts change. Merge readiness⛔ Blocked before merge - 1 item remains The PR remains useful: current main still lacks the managed tool checkpoint. The introduced changes have no identified blocking correctness defects, and the prior review contains no unresolved findings. Priority: P2 Review scores
Verification
How this fits togetherOpenClaw’s Anthropic request builders convert tools, system instructions, and conversation history into provider requests. Cache checkpoints identify reusable prefixes while respecting route capabilities and Anthropic’s four-checkpoint limit. flowchart LR
A[Tools and system instructions] --> C[Messages request builders]
B[Conversation history] --> C
D[Retention and route capabilities] --> E[Shared checkpoint allocation]
C --> E
E --> F[Anthropic API request]
Before merge
Agent review detailsSecurityNone. PR surfaceSource -75, Tests +205, Docs +1. Total +131 across 8 files. View PR surface stats
Review metrics
Merge-risk optionsMaintainer options:
Technical reviewBest possible solution: Keep one shared checkpoint allocator while preserving explicit retention settings, route compatibility overrides, and the existing plugin payload-policy contract. Do we have a high-confidence way to reproduce the issue? Yes, at the request boundary: current main’s managed builder omits the independent tool checkpoint and consumes extra OAuth system slots. Live cache-hit effects were not reproduced during this review. Is this the best way to solve the issue? Yes. Consolidating the two builders around the existing caching rules repairs their divergence without introducing another configuration path or changing the exported plugin policy. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against 61d957a9cb64. LabelsLabel justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
History |
|
Merged via squash.
|
…law#140804) * fix(anthropic): preserve tool cache when system prompts change Share cache checkpoint allocation across managed and SDK Messages requests. Preserve OAuth billing text, reserve history anchors, and respect tool and TTL compatibility. * test(anthropic): refresh managed tool cache payload snapshot
Reworked after review. The earlier revision adjusted the legacy exported system helper in @openclaw/ai, duplicating the OAuth-preamble repair that openclaw#140804 already shipped in the production builders and altering the helper's checkpoint contract for SDK consumers. This revision reverts the helper to its main behavior and fixes the remaining gap from openclaw#147168 where it actually lives: the message allocator. applyAnthropicCacheControlToMessages now keeps walking backwards past the newest user turn and anchors the previous eligible user turn while marker budget remains, so a reshaped newest message only invalidates its own tail marker instead of the whole cached history. Evidence: new unit tests assert the three-anchor wire layout (previous user turn, latest tool result, newest user turn) through applyAnthropicPayloadPolicyToParams, plus the preserved single-marker tool-result fallback contract.
Additional instructions
Maintainers can update this repository branch.
What Problem This Solves
Fixes an issue where users running managed Anthropic Messages requests would lose reuse of unchanged tool definitions when their system prompt changed, such as after a workspace edit or skills refresh. OAuth requests also spent a cache checkpoint on the billing preamble, leaving less room to cache conversation history.
Why This Change Was Made
Both Messages builders now share system shaping, cache capability and TTL resolution, and checkpoint allocation. Supported routes checkpoint the last projected tool separately from the stable system prompt, then use the remaining slots for the latest user and tool-result anchors, within Anthropic's four-marker limit. OAuth billing and identity text is preserved; the identity checkpoint is retained only when there is no stable application-system checkpoint.
The existing Fireworks defaults and explicit compatibility overrides gate tool markers and long TTLs. Implicit long retention follows the documented endpoint-host restriction in both paths. The generic plugin-SDK payload-policy helper retains its existing behavior. Production code is reduced by 75 lines.
User Impact
Stable tool definitions remain independently cacheable when the system prompt changes. OAuth tool loops retain both conversation anchors without marking the billing block. No configuration, storage, dependency, or changelog changes are introduced.
Evidence
node scripts/run-vitest.mjswith the Anthropic provider, transport, policy, replay, usage, inline-image, and parity test paths.docs/reference/prompt-caching.mdnow documents the independent tool-prefix checkpoint.node scripts/run-vitest.mjs src/agents/anthropic-payload-policy.test.ts— 1 file passed, 17 tests passed. Together with the shared snapshot suite below, total focused proof is 15 files and 407 passing tests.scoped-clean; zero accepted/actionable P0/P1 findings; overall verdict: patch is correct. No findings required disposition.Focused output summaries:
node scripts/check-changed.mjscompleted successfully (exit 0), including production/test typechecking, lint, dead-export scans, and the final architecture guards. All three dead-export scans reported zero entries; runtime import-cycle checking reported zero cycles.Validation follow-through: strict test-array typing and three test
sort()calls were corrected, and the newly unused message-helper export was made module-private. One later gate run hit the existing managed-process cleanup errorEPROCESSGROUP_CLEANUP_FAILEDwithout a TypeScript diagnostic; the process group was verified terminated, the diagnostic typecheck passed, and the complete gate then passed without weakening guards or changing timeouts.Final autoreview output:
CI follow-up: the first exact-head run failed only
checks-node-compact-small-7(and its aggregate gate) because the shared transport snapshot lacked the intended tool checkpoint. Vitest regenerated exactly three lines inanthropic-success.snap.txt, adding the last tool's ephemeralcache_control; no assertion was weakened.node scripts/run-vitest.mjs packages/ai/src/provider-transport-parity.test.ts --updateregenerated the snapshot, then the same command without--updatepassed:The follow-up
node scripts/check-changed.mjspassed (exit 0), including full core lint. Fresh Codex autoreview remains scoped-clean with zero accepted/actionable P0/P1 findings.Final CI: run 34085820808 completed successfully on
f1479f54cdff533478b82028e9c7ba949949c98e: 77 successful jobs, 16 intentionally skipped jobs, zero failed or pending jobs. The exact-head watcher returnedGREEN(exit 0), including the corrected snapshot shard andopenclaw/ci-gate.