Skip to content

fix(agents): Anthropic session forks miss the parent's prompt cache when thinking comes from the agent default - #197203

Merged
obviyus merged 2 commits into
mainfrom
openclaw/fork-thinking-config
Nov 11, 2026
Merged

obviyus merged 2 commits into
mainfrom
openclaw/fork-thinking-config

Conversation

@obviyus

@obviyus obviyus commented Nov 11, 2026

Copy link
Copy Markdown
Collaborator

What Problem This Solves

Fixes: on Anthropic models with thinking on, Advisor reviews and other plugin session forks miss the parent's prompt cache, so each review pays to write almost the whole conversation again. This happens when the agent's thinking comes from its configured default rather than a per-session override.

User Impact

User impact: with thinking on, Anthropic session forks now read the parent's cached prompt instead of re-writing it. A fork's cost and latency drop to roughly the uncached tail after the parent's last cached point. OpenAI and other APIs keep their current behavior.

Why This Change Was Made

A plugin fork took its thinking level only from the session's thinkingLevel override. When the agent ran on thinkingDefault (or a per-model default), the fork sent a different thinking config from the parent's. On opus that was effort: low instead of high; on haiku thinking was disabled instead of adaptive. Anthropic invalidates the cached message prefix when thinking parameters change, so the fork could only reuse the tools and system portion.

The fork's request shaping in btw-stream.ts now resolves the level the same way the parent's turn does: the session override first, then the agent and model defaults (resolveThinkingSelection), clamped for the model. This applies only on anthropic-messages. Other APIs keep sending the session override until their own cache behavior has been measured live.

The thinking-block projection does not change. In an earlier offline replay, stripping prior thinking blocks looked necessary. That replay sent no beta headers, though. On the real route, a level-only build hit the parent's latest prefix with thinking blocks kept. Stripping the blocks instead made later forks reuse the previous fork's cache rather than the parent's.

Evidence

Live measurements through the real route: an isolated test Gateway, cliproxy with anthropic-messages, thinkingDefault: high, Advisor everyToolCalls: 1, and one parent turn with three sequential session_status calls. Provider usage was read from the captured response streams.

Provider Model Thinking Build Fork Fork cacheRead / prompt (cacheWrite) Parent's preceding cached prefix Fork output_config / thinking
cliproxy (Anthropic Messages) claude-opus-5-5 high (agent default) main 2598631a 1st 950 / 5184 (4230) 4334 effort low / adaptive
main 2nd 4806 / 6186 (1376), the 1st fork's own entry 5297 effort low
this PR b4616c01* 1st 4328 / 5178 (846) 4328 effort high / adaptive
this PR 2nd 4800 / 5672 (868) 4800 effort high
cliproxy (Anthropic Messages) claude-haiku-5-5 high (agent default) main 1st 523 / 5135 (4608) 4311 none / disabled
main 2nd, 3rd 4757, 5443, from earlier forks' entries 4813, 5556 disabled
this PR 1st 4309 / 5237 (924) 4309 effort high / adaptive
this PR 2nd 5543 / 6465 (918) 5543 effort high
  • * The measured commit b4616c01 has the same production code as the PR head. The head only changes the test's mock-server callback to satisfy lint.
  • In the table, prompt = input + cacheRead + cacheWrite. The parent's prefix is the cacheRead + cacheWrite of the latest parent request that finished before the fork was sent.
  • One-variable live check on opus with thinking kept and only the level fixed: the forks read 4341 and 5368, equal to the parent prefixes of 4341 and 5368.
  • The variant that also stripped thinking blocks read 4335 on the first fork, matching the parent. Its second fork read 4766 against a parent prefix of 4785, which was the previous fork's entry. That variant is not in this PR.
  • src/gateway/server-plugin-subagent-runtime.fork-transport.test.ts now runs the gateway forkSession integration over a loopback Anthropic Messages provider with thinkingDefault: "high". It fails on main (thinking: {type: "disabled"}) and passes here. Converting the existing test keeps the test-lines ratchet at net −1.
  • btw*, session-fork and extensions/advisor tests pass (82 tests). pnpm tsgo:core and OPENCLAW_CHECK_CHANGED_SKIP_DEADCODE=1 pnpm check:changed are clean.

Gaps and accepted edge cases:

  • OpenAI forks are intentionally unchanged and unmeasured. They still ignore the agent's thinking default, which is a follow-up.
  • The fork resolves its level from the configured and bundled thinking catalog. The parent's turn can also load the provider-scoped runtime catalog. A model whose reasoning facts exist only in that runtime catalog could clamp differently.
  • /btw uses its own system prompt and keeps thinking off on purpose, so it shares no cached prefix with the parent. It is not affected.

obviyus and others added 2 commits November 11, 2026 17:11
…hen thinking comes from the agent default

OpenClaw-Publication: 261c0cd5-0c5f-4a34-93fe-b7f52875d4da
@clawsweeper

clawsweeper Bot commented Nov 11, 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.

@openclaw-barnacle openclaw-barnacle Bot added gateway Gateway runtime agents Agent runtime and tooling size: S labels Nov 11, 2026
@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. P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. 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. labels Nov 11, 2026
@clawsweeper

clawsweeper Bot commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

Anthropic session forks now resolve configured thinking defaults through the existing thinking-selection owner.

Example: Advisor forks an Anthropic session configured with thinkingDefault: high and no session override.

  • Before: The reported first fork reads 950 cached tokens and writes 4230 because its effort differs from the parent.
  • After: The reported first fork reads the parent's 4328-token prefix and writes 846 tokens.

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) Focused, useful repair with relevant live cache measurements and an owning-boundary regression; no actionable introduced bug found.
Proof confidence 🐚 platinum hermit (4/6) Sufficient (live_output): The body supplies live provider-usage measurements through an isolated Gateway and Advisor, showing corrected thinking settings and parent-prefix cache reads on two Anthropic models; the transport regression separately covers persisted context and unauthorized-reader rejection.
Patch quality 🦞 diamond lobster (5/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: Advisor reviews and other Anthropic session forks can repeatedly pay to cache nearly the entire conversation when thinking is inherited from configuration.
Reason: The collaborator-authored direction restores intended parent-setting reuse; the documented runtime-only catalog edge case and unchanged OpenAI behavior are explicitly accepted limits.

Regression provenance

Possible regression — suspected (reviewed change). No predecessor PR is attributed.

Merge readiness

✅ Ready for maintainer review

Keep open: this collaborator-authored PR fixes a current Anthropic fork cache mismatch with a focused change, relevant live measurements, and no identified introduced defect.

Priority: P2
Reviewed head: 64a4d7e5cb5d8fe6169692d6f8d59bad41b9e3ff

Before merge

None.

Findings

None.

Agent review details

How this fits together

Plugins request read-only session forks through the Gateway; the embedded runtime reconstructs parent context, shapes a provider request, and returns text and usage.

flowchart TD
  A[Plugin fork request] --> B[Gateway session access]
  B --> C[Parent context reconstruction]
  C --> D[Existing thinking selection]
  D --> E[Anthropic request builder]
  E --> F[Provider response]
  F --> G[Text and usage]
Loading

Technical review

Best possible solution:

Use the existing thinking-selection owner for Anthropic forks while retaining current context reconstruction and tool denial.

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

Current-main source establishes the missing-default path, and the author reports before/after live Gateway measurements plus a regression failing on main; no target code was executed during this review.

Is this the best way to solve the issue?

This is a small root-cause repair using the existing resolver; it avoids the earlier reverted tool-choice changes and adds no competing owner or persistence.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 3275ef4d21c3.

Provenance checked

  • Fork request thinking selection keeps the original intent (cb00966: The on-demand fork refactor replaced retained request snapshots with reconstruction through normal provider builders while preserving current model authority and tool denial.)
  • Gateway fork transport regression keeps the original intent (4b6d261: The test consolidation retained owning-boundary proof for session visibility, persisted history, and provider dispatch; the converted test preserves those contracts.)

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

PR surface

Source +15, Tests 0. Total +15 across 2 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 1 16 1 +15
Tests 1 32 32 0
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 48 33 +15

Preflight source

Source unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified.

Labels

Label changes:

  • add P2: Affected background forks repeatedly write cached conversation prefixes, increasing provider cost with a limited feature-specific blast radius.
  • add proof: sufficient: Contributor real behavior proof is sufficient.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🦞 diamond lobster.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.

Label justifications:

  • P2: Affected background forks repeatedly write cached conversation prefixes, increasing provider cost with a limited feature-specific blast radius.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🐚 platinum hermit and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.
  • proof: sufficient: Contributor real behavior proof is sufficient.

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 November 11, 2026, 7:05 AM ET / 12:05 UTC.

@obviyus
obviyus merged commit 7743284 into main Nov 11, 2026
130 checks passed
@obviyus
obviyus deleted the openclaw/fork-thinking-config branch November 11, 2026 12:15
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Nov 12, 2026
…hen thinking comes from the agent default (openclaw#197203)

* fix(agents): run Anthropic session forks at the parent's thinking level

Co-authored-by: obviyus <[email protected]>

* fix(agents): Anthropic session forks miss the parent's prompt cache when thinking comes from the agent default

OpenClaw-Publication: 261c0cd5-0c5f-4a34-93fe-b7f52875d4da

---------

Co-authored-by: obviyus <[email protected]>
RomneyDa added a commit that referenced this pull request Nov 16, 2026
…hen thinking comes from the agent default (#197203)

* fix(agents): run Anthropic session forks at the parent's thinking level

Co-authored-by: obviyus <[email protected]>

* fix(agents): Anthropic session forks miss the parent's prompt cache when thinking comes from the agent default

OpenClaw-Publication: 261c0cd5-0c5f-4a34-93fe-b7f52875d4da

---------

Co-authored-by: obviyus <[email protected]>
(cherry picked from commit 7743284)
Co-authored-by: RomneyDa <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling gateway Gateway runtime P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. 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