Skip to content

fix(agents): keep prompt cache identity stable across session turns - #181301

Merged
steipete merged 1 commit into
mainfrom
steipete/settle-cache-key-affinity
Oct 23, 2026
Merged

steipete merged 1 commit into
mainfrom
steipete/settle-cache-key-affinity

Conversation

@steipete

Copy link
Copy Markdown
Contributor

What Problem This Solves

A session's prompt_cache_key changed between turns, which drops the provider prompt cache entirely.

Tapped Responses bodies from one fleet parent session showed operator and recovery turns using <session-id>:0, while its requester-settle wake requests switched to openclaw-webchat-<hash>, then switched back. The Gateway logged a complete miss on the settle wake:

[prompt-cache] cache read dropped 63333 -> 0 runId=announce:requester-settle:… historyRewrite

Two caller-side selectors competed with the session-boundary owner:

  • attempt-execution.ts picked a WebChat affinity key from persisted internal delivery.
  • chat-send-agent-dispatch.ts picked one from the client type.

Both overrode the session fallback, so cache identity depended on per-turn delivery facts. That violates the prompt-prefix contract.

User Impact

Operator, WebChat, child announcement, requester-settle, and recovery turns retain the same session cache identity. Explicit overrides and reset/compaction rotation are unchanged. No migration or user action is required.

Why This Change Was Made

Both selectors and their now-unused helper are removed (net −37 lines). The existing session-boundary owner (session-boundary-prompt-cache-key.ts) supplies one key, <session-id>:<boundary-count>, for every turn of a session: operator, WebChat, child announcement, settle and restart recovery. Explicit overrides and reset/compaction rotation are unchanged. docs/reference/prompt-caching.md is updated.

Evidence

  • src/gateway/gateway.prompt-cache.test.ts, extended across operator ingress, WebChat, child announcement and restart: red on base (expected <sid>:0, received openclaw-webchat-…) and green with the fix. Also green, on a Crabbox host (Node 24.22.1): Gateway chat (74), session-boundary keys (3), session replay (48), stream resolution (10), prefix stability (6), worker inference (43).
  • Live Crabbox, Gateway kill -9 while two fleet parents were still in their first turn, every Responses body recorded: all 17 sessions (parents and children) used exactly one prompt_cache_key for every request. No openclaw-webchat-… keys appeared, and no cache read dropped to 0.

Remove delivery- and client-dependent WebChat affinity overrides so operator, WebChat, internal completion, requester-settle, and recovery turns use the existing session-boundary cache key owner. Preserve explicit caller keys and reset/compaction rotation.

Extend the capturing Gateway regression across operator ingress, WebChat, child announcement, and restart. Keep the existing WebChat prefix checks and cover the shared cache key across all six parent requests.
@clawsweeper

clawsweeper Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@steipete
steipete marked this pull request as ready for review October 23, 2026 04:07
@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation gateway Gateway runtime agents Agent runtime and tooling labels Oct 23, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-23T04:11:25.762475Z 58aecc7 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 58aecc77f6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

);
return `openclaw-webchat-${digest}`;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve full session scope in the fallback cache key

When two WebChat conversations share a persisted sessionId across different session keys or agents, or when long IDs differ only after the 62-code-point truncation, removing this digest makes both fall through to <truncated-sessionId>:<boundary>. The removed key included provider, model, agent, and the full session key, so these distinct conversations now compete for the same provider cache affinity and can lose the reuse this change is intended to preserve. Keep a bounded digest of the complete session scope while retaining the lifecycle suffix.

AGENTS.md reference: AGENTS.md:L69-L69

Useful? React with 👍 / 👎.

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. P2 Normal backlog priority with limited blast radius. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Oct 23, 2026
@clawsweeper

clawsweeper Bot commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

This PR removes delivery-dependent and client-dependent cache-key overrides so session turns share the existing session-boundary identity.

Example: Session 11af5b93-1401-4f59-961e-c44241201c93 receives an operator turn followed by a requester-settle wake.

  • Before: The operator request uses 11af5b93-1401-4f59-961e-c44241201c93:0, while the wake can use an openclaw-webchat digest.
  • After: Both requests use 11af5b93-1401-4f59-961e-c44241201c93:0 until the transcript boundary changes.

Review scores

Measure Result What it means
Overall readiness 🦐 gold shrimp (3/6) A focused, correct ownership repair with a meaningful production-path regression and limited independently inspectable runtime evidence.
Proof confidence 🦐 gold shrimp (3/6) The supplied maintainer-authored classification exempts this PR from the ordinary contributor proof gate; its body reports live Crabbox recovery across 17 sessions with one key each, but supplies no inspectable capture, so that result remains author-reported.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: A conversation can lose prompt-cache reuse when operator, WebChat and internal continuation turns choose different cache identities.
Reason: The repair restores the documented stable-session contract by deleting competing policy; the supplied maintainer-authored classification and established feature ownership settle direction.

Merge readiness

✅ Ready for maintainer review

Keep open: this PR repairs a source-confirmed cache-affinity mismatch still present on main, using the existing owner without adding production machinery; no actionable correctness or security defect was found.

Priority: P2
Reviewed head: 58aecc77f6ff7377cd4e1be56a7635b6dfc174e2

Before merge

None.

Findings

None.

Tests

  • Low-value test src/gateway/server.chat.gateway-server-chat-b.test.ts: Remove the new assertion that mocked dispatch options contain two undefined cache keys: it pins an internal call shape, while the capturing Gateway scenario already checks the provider-visible identity; retain the surrounding sequential-turn coverage.
  • Missing end-to-end proof: The reported live process-kill/recovery run has no attached request trace or logs available for inspection.
Agent review details

How this fits together

Gateway chat and agent-command ingress prepare embedded runs; transcript preparation derives cache identity from the physical session and lifecycle boundary before the provider transport serializes the request.

flowchart TD
  A[Gateway chat] --> C[Embedded run]
  B[Agent command and recovery] --> C
  C --> D[Transcript preparation]
  D --> E[Session boundary cache key]
  E --> F[Provider request]
Loading

Technical review

Best possible solution:

Use the existing transcript-boundary derivation for implicit cache identity across every ingress while preserving explicit overrides.

Do we have a high-confidence way to reproduce the issue?

Current-main source shows delivery and client facts selecting a digest instead of the existing session-boundary fallback; the PR reports a matching captured miss and a regression failing on the base, but no target code was executed during this review.

Is this the best way to solve the issue?

Removing both selectors is the smallest complete repair: the existing owner already handles explicit overrides, boundary rotation and length limits, and the obsolete helper has no remaining references.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 2c2c2f30e4e4.

Provenance checked

  • WebChat cache-affinity selection changes intended behavior with a stated reason (fix(gateway): stabilize webchat prompt cache affinity #89740: Preserve stable provider affinity across sequential WebChat messages while retaining unique run and idempotency identities.)
  • Internal completion cache-affinity selection changes intended behavior with a stated reason (d13db41: Keep private child completions on their browser conversation's cache identity.)
  • Length-bounded lifecycle suffix keeps the original intent (03a48b5: Prevent truncation from removing reset and compaction boundary distinctions on long session IDs.)

Testing

Proof path: in-process harness.

Security

None.

Evidence

What I checked:

Likely related people:

  • Vincent Koc: Raw commit 175cfe4 adds src/gateway/server.chat.gateway-server-chat-b.test.ts:1010 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 175cfe4846a1; files: src/gateway/server.chat.gateway-server-chat-b.test.ts)

PR surface

Source -53, Tests +15, Docs +1. Total -37 across 6 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 3 3 56 -53
Tests 2 31 16 +15
Docs 1 5 4 +1
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 6 39 76 -37

Labels

Label changes:

  • add P2: Mixed-ingress sessions can lose provider cache reuse and incur additional processing cost, without evidence of message loss or runtime unavailability.
  • add rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Label justifications:

  • P2: Mixed-ingress sessions can lose provider cache reuse and incur additional processing cost, without evidence of message loss or runtime unavailability.
  • rating: 🦐 gold shrimp: Overall readiness is 🦐 gold shrimp; proof is 🦐 gold shrimp and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

Reviewed October 23, 2026, 12:25 AM ET / 04:25 UTC.

@steipete

Copy link
Copy Markdown
Contributor Author

Follow-up from automated review: assess cache affinity for distinct session scopes that deliberately share a persisted session ID, including truncation collisions for unusually long IDs. This patch keeps the existing session-boundary key format and removes per-turn selectors; changing that owner's key format is outside this focused fix. The finding does not identify data-loss, security, or money risk.

@steipete
steipete merged commit ec2ae37 into main Oct 23, 2026
216 of 220 checks passed
@steipete
steipete deleted the steipete/settle-cache-key-affinity branch October 23, 2026 04:53
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Oct 24, 2026
…penclaw#181301)

Remove delivery- and client-dependent WebChat affinity overrides so operator, WebChat, internal completion, requester-settle, and recovery turns use the existing session-boundary cache key owner. Preserve explicit caller keys and reset/compaction rotation.

Extend the capturing Gateway regression across operator ingress, WebChat, child announcement, and restart. Update the prompt-caching documentation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling docs Improvements or additions to documentation gateway Gateway runtime P2 Normal backlog priority with limited blast radius. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. size: S status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant