Repository navigation
[ENG-22237] fix(agents): new user messages rewrite the whole conversation into the prompt cache - #328
Merged
Merged
Conversation
…ntext message for byte-stable prompt caching The active user turn was decorated at request-build time with an inbound metadata block (Conversation info / reply-target / sender / forwarded / chat-history), but the session store persists the bare text and the LLM boundary strips that block from historical replay. So a user message's serialized bytes changed retroactively — decorated as the active turn, bare as history — on every turn, invalidating any prefix/exact-match provider prompt cache from that point onward, every turn. (openclaw#90811 fixed the timestamp half of this same asymmetry.) Rework of the earlier "strip the active turn" approach (which lost model-visible context — ClawSweeper's P1) into one that keeps the context AND recovers the cache cost: - Route current-turn inbound metadata out of the user prompt and into the hidden runtime-context custom message (the mechanism from openclaw#89428), and relocate that carrier to the ABSOLUTE TAIL of the wire request (after the active user turn and any tool-call scaffolding). The user turn stays bare and byte-identical in both the active and historical positions; the volatile carrier vanishes next turn exactly where the assistant reply begins anyway, so the request is an append-only prefix-extension through the active user turn. - A durable marker (UserMessage.runtimeContextCarrier, set by convertToLlm from the carrier's details) lets the Anthropic SDK transport, the managed Anthropic transport (anthropic-transport-stream / anthropic-payload-policy), and OpenAI-completions skip the carrier when selecting the deepest cache_control breakpoint, keeping the anchor on the last stable user turn. Storage stays BARE (invariant preserved); runtime-only turns (room events) keep their existing inline behavior, which is byte-stable because room-event/system context is not strip-eligible; legacy bare-stored sessions are unchanged. Reviewed with adversarial gpt-5.5/codex sweeps (correctness, byte-identity, compatibility, test adequacy): found + fixed the managed Anthropic transport cache-anchor path and added the missing OpenAI-completions/managed-transport breakpoint tests; a theoretical runtime-only mutation was verified unreachable and pinned with a test. Final sweep clean. Co-authored-by: openclaw#89428 (runtime-context handoff approach) (cherry picked from commit f5931f5) Co-authored-by: Peter Lindsey <[email protected]>
(cherry picked from commit c718afe) Co-authored-by: Ayaan Zaidi <[email protected]>
* fix(agents): apply image context to current turn * fix(agents): align runtime context with prompt order * fix(agents): make turn context position independent * fix(agents): bind internal context to the active user turn Keep current-turn runtime context position-independent while preserving the shared privacy notice, runtime events, prompt-cache placement, and historical preface sanitization. Consolidate header detection in its existing owner and retain the original real turn-boundary regression. Fixes openclaw#110672 Co-authored-by: chernoff-code <[email protected]> --------- Co-authored-by: chernoff-code <[email protected]> Co-authored-by: Peter Steinberger <[email protected]> (cherry picked from commit e17bc58) Co-authored-by: ℭ𝔥𝔢𝔯𝔫𝔬𝔣𝔣 <[email protected]>
The before_prompt_build hook prependContext and appendContext were added only to the active user message. The transcript stores the bare prompt, so the next turn replayed that user message without the hook text. Its bytes changed between the two requests, and every message-level prompt cache entry written during the turn missed. On Boon Web every turn carries hook context (UI page and live integration status), so each new user turn rewrote the whole conversation history into the cache. Route hook prompt context for persisted, non-runtime-only user turns into the current-turn runtime-context carrier, next to the inbound metadata. The active user message now has the same bytes as its replayed form. Runtime-only turns and turns whose user message is not persisted keep the hook context inline as before. The carrier sits at the tail of the request. Claude 5.x on non-direct anthropic-messages routes can answer a standalone trailing carrier turn instead of the user (openclaw#186434). The managed Anthropic transport therefore appends the carrier as trailing text blocks of the preceding user message, and those blocks never receive cache_control. Cache markers stay on the user text and the latest tool_result, so the cached prefix of turn N is a prefix of every later request. Refs: ENG-22237
…turn The upstream cache fix moves the current-turn runtime-context carrier to the absolute tail of every request. In a tool loop that puts the carrier after each tool result. The carrier tells the model to continue answering the active request and carries the channel delivery hint, so after a message tool result Sonnet 5.5 sent the same answer again. A local Boon Web run showed three message tool calls in one turn. Place the carrier right after the active user message instead. The managed Anthropic transport merges it into that user message as uncached trailing text, so the model reads it once per turn, next to the request it describes, and tool results carry no extra text. The user message stays byte-identical across turns, so the next turn still reads the cached prefix through it. The tool-loop span of the previous turn is written again once, because the carrier is no longer in it. Refs: ENG-22237
There was a problem hiding this comment.
All reported issues were addressed across 21 files
Reply with feedback, questions, or to request a fix.
View guided diff | Turn on auto-fix | Re-trigger cubic
When the active user message converts to nothing, for example an image-only message on a text-only model, the previous transport param can be a tool result turn. The carrier then merged into it as text after the tool results. That is the shape that made the model repeat its answer. Merge the carrier only into a param that came from a user message. Otherwise keep it as a separate uncached user turn. Also drop the stale Bedrock Converse cache point note. With the carrier placed after the active user message, tool loop requests end with tool results, so the cache point no longer lands on the carrier. Refs: ENG-22237
a-bernike
approved these changes
Nov 5, 2026
orswn
added a commit
that referenced
this pull request
Nov 9, 2026
Cut the fork release for the PRs merged to boon after the v2026.6.35-boon.4 tag: - #328 (ENG-22237): replayed user turns keep the same bytes, so a new user message in Boon Web, Slack and MS Teams reads the previous prompt from cache instead of writing the whole history again. - #280 (ENG-21157): opt-in model compat flag requiresGoogleThoughtSignatures, so Gemini 3 tool loops work behind an OpenAI-compatible proxy. No fleet model config sets it yet. - #327: the v2026.6.35-boon.4 tag was cut from the PR branch. The review fixes that merged after it change tests and one comment only. Bump the root package.json and npm-shrinkwrap.json, all 136 bundled plugin manifests (scripts/sync-plugin-versions.ts), and the version and openclaw peer floor in every extension npm-shrinkwrap.json (scripts/generate-npm-shrinkwrap.mjs --all). The regeneration also picks up the registry's updated deprecation text for @smithy/core 3.24.6 in amazon-bedrock, amazon-bedrock-mantle and tlon; no resolved version or integrity changes. Add the 2026.6.35-boon.5 CHANGELOG.md section. No source or dependency change beyond the merged PRs above.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related: ENG-22237, openclaw#86063, openclaw#186434
What Problem This Solves
Fixes an issue where every new user message in Boon Web, Slack, or MS Teams rewrote the whole conversation into the Bedrock prompt cache, even when the previous message was seconds old.
The cause: openclaw sent the active user message with its inbound metadata (Conversation info, Sender, delivery hint) and hook context (Boon Web page and live integration status). The transcript stores the bare text, so on the next turn the same message went out without them. The bytes changed, so every message-level cache entry written during the turn missed.
Why This Change Was Made
The user message now has the same bytes when it is the active turn and when it is replayed as history. The per-turn context still reaches the model, but in a separate block.
(cherry picked from ...)and credit the upstream authors.before_prompt_buildprependContextandappendContextgo into the same carrier for persisted, non-runtime-only user turns. Upstream still adds them inline, so this part diverges from upstream. Runtime-only turns, and turns whose user message is not persisted, keep hook context inline. Inbound metadata follows the upstream rule and goes into the carrier on all non-runtime-only turns.anthropic-messagesroutes) openclaw/openclaw#186434).messagetool call. In a local run it sent the same answer up to 3 times.cache_control. If the active user message converts to nothing, for example an image-only message on a text-only model, the carrier stays a separate uncached user turn. It never follows atool_result. The cache markers stay on the user text and the latesttool_result. The model reads the carrier once per turn, next to the request it describes.Not in this PR:
extensions/amazon-bedrock) and the SDK Anthropic provider are not on the fleet path. They keep upstream behavior.llm_input/before_agent_runhookpromptand trajectorypromptnow show the bare user text. The carrier with the context is in theirmessages. No bundled or Boon plugin reads that prompt field.User Impact
Evidence
Local smoke on a prod-matched paid profile through the Boon Web chat, with Claude Sonnet 5.5 on Bedrock via boon-llm-gateway. Usage numbers come from the session usage. They match the gateway
llm_request_logsrows on 135 of 135 calls.First model call of each new user message, in a 7-message thread:
Behavior checks after the change:
391.[BOON_UI_CONTEXT] path=/data_reviewand the user text block does not.[text (cache_control), carrier text]. Tool results carry no extra text.Tests:
tsgo(core, core tests, extensions),oxlint,oxfmt --check, andgit diff --checkpass.