Skip to content

fix(agents): keep prompt history append-only mid-session for prompt-cache stability - #102610

Merged
steipete merged 1 commit into
mainfrom
claude/compassionate-curie-a5c2a3
Jul 9, 2026
Merged

steipete merged 1 commit into
mainfrom
claude/compassionate-curie-a5c2a3

Conversation

@steipete

@steipete steipete commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Closes #99495

What Problem This Solves

On the embedded agent runner, two mechanisms mutated already-sent history messages between provider requests, invalidating the Anthropic prompt-cache tail mid-session (reported with diagnostics.cacheTrace fingerprint evidence in #99495; measured at ~26% of Anthropic spend on a media-heavy deployment):

  1. Historical toolResult messages re-shaped per request. Aggregate tool-result truncation is pressure-dependent, and the freeze that should pin a result's serialized bytes (ToolResultPromptProjectionState) was created per attempt, so it never spanned turns. The same historical web-search result oscillated between fuller and shorter renderings request to request. Results with ambiguous/missing tool ids were never frozen at all.
  2. Inbound media rewrote an already-sent user-turn slot. Media claim-checks resolve asynchronously; when resolution landed after the turn's bytes had crossed the LLM boundary, the transcript slot was rewritten in place (updateMode: "inline"), flipping a text turn into a media turn at the same index and invalidating the whole tail.

Why This Change Was Made

Prompt history must be append-only within a session once bytes have been sent:

  • Session-scoped projection state (src/agents/embedded-agent-runner/session-prompt-state.ts): frozen tool-result projections now live in a bounded (64-entry LRU) process-local map keyed by session id, disposed on session reset (clearSessionResetRuntimeState). Prechecks operate on clones; only the provider-dispatch transform freezes emitted bytes. Frozen bytes are shrink-only: they move only after fresh output is exhausted and the aggregate guard still overflows, starting from the frozen projection — never re-expanding when pressure drops. Ambiguous/missing tool ids get a stable hashed fallback identity so they freeze too.
  • Sent watermark + late-media append (src/sessions/user-turn-transcript.ts): the provider-dispatch transform marks the current user turn sent. Media that resolves after that point is persisted as a new appended turn (:late-media idempotency key) instead of an inline rewrite, so the wire prefix and the transcript stay byte-identical. Media resolving before first serialization keeps the existing inline merge.

Both fixes respect the existing byte-stability invariants (#3658 canonicalization layer, #100272 tail runtime-context carrier). No config surface added; CLI-runner and channel code untouched.

User Impact

Anthropic (and any prefix-cached provider) deployments stop paying repeated 25–75K-token tail re-writes at the 2× TTL write rate on busy/media-heavy sessions. Worst case after a process restart is one deliberate re-shape, then stable. No behavior change for sessions that never hit aggregate pressure or mid-run media.

Evidence

  • New regression tests (issue-referenced): cross-attempt byte stability of historical tool results; monotonic shrink-only under a tighter cap with no re-expansion when the cap relaxes; ambiguous-key freezing; late media appends one turn while every previously-sent per-message fingerprint stays identical (summarizeMessages from cache-trace.ts); early media keeps inline merge; projection disposal on session reset.
  • Focused suites: tool-result-truncation.test.ts 61 passed, attempt.llm-boundary.cache-stability.test.ts 13 passed, user-turn-transcript.test.ts 34 passed, session-reset-cleanup.test.ts 6 passed; full src/agents/embedded-agent-runner project run 5595 passed (6 unrelated flakes passed in isolation).
  • Testbox pnpm check:changed: exit 0 on tbx_01kx31g1safq928g9fxs7qh0y2 (typecheck core + core tests, oxlint shards, import cycles, security/package guards)
  • Codex autoreview: clean, no accepted/actionable findings (gpt-5.5; run after implementation and re-run after a one-line lint fix)

Session-scoped tool-result projection freeze (shrink-only, stable
fallback identities for ambiguous tool ids) and a sent-watermark so
late-resolving inbound media appends a new user turn instead of
rewriting an already-sent slot. Stops Anthropic prompt-cache tail
invalidation on busy/media-heavy embedded sessions.
@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: L maintainer Maintainer-authored PR labels Jul 9, 2026
@steipete
steipete merged commit fd34019 into main Jul 9, 2026
120 of 124 checks passed
@steipete
steipete deleted the claude/compassionate-curie-a5c2a3 branch July 9, 2026 09:28
@steipete

steipete commented Jul 9, 2026

Copy link
Copy Markdown
Contributor Author

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Jul 10, 2026
) (openclaw#102610)

Session-scoped tool-result projection freeze (shrink-only, stable
fallback identities for ambiguous tool ids) and a sent-watermark so
late-resolving inbound media appends a new user turn instead of
rewriting an already-sent slot. Stops Anthropic prompt-cache tail
invalidation on busy/media-heavy embedded sessions.
steipete added a commit that referenced this pull request Jul 11, 2026
…02610)

Session-scoped tool-result projection freeze (shrink-only, stable
fallback identities for ambiguous tool ids) and a sent-watermark so
late-resolving inbound media appends a new user turn instead of
rewriting an already-sent slot. Stops Anthropic prompt-cache tail
invalidation on busy/media-heavy embedded sessions.

(cherry picked from commit fd34019)
zooqueen pushed a commit to zooqueen/openclaw that referenced this pull request Sep 11, 2026
) (openclaw#102610)

Session-scoped tool-result projection freeze (shrink-only, stable
fallback identities for ambiguous tool ids) and a sent-watermark so
late-resolving inbound media appends a new user turn instead of
rewriting an already-sent slot. Stops Anthropic prompt-cache tail
invalidation on busy/media-heavy embedded sessions.

(cherry picked from commit fd34019)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling maintainer Maintainer-authored PR size: L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Prompt history is not append-only mid-session → Anthropic prompt-cache thrash (two reproducible invalidators)

1 participant