Skip to content

Commit 1e19f22

Browse files
fix: prompt caching breaks on Claude Opus 5 and Sonnet 5 (#121283)
* fix: prompt caching breaks on Claude Opus 5 and Sonnet 5 shouldPreserveThinkingBlocks() matched modern Claude ids with a hardcoded family list plus /claude-[5-9]/ future-proofing. That regex assumes the generation follows the prefix (claude-5-x), but shipped generation-5 ids place the family in between (claude-opus-5, claude-sonnet-5, claude-mythos-5), so none matched and dropThinkingBlocks flipped to true. Signed thinking blocks were stripped from replayed history, diverging every request and invalidating the Anthropic prompt cache on the default opus/sonnet aliases. Read the generation from the id instead, preserving blocks for generation 4 and newer. This handles both id shapes and matches the idiom already used in audit-extra.sync.ts and live-model-filter.ts. Closes #121251 * fix: correct Claude thinking replay contract Use the canonical Claude model identity and exact Anthropic preservation contract when deciding whether replay may retain prior thinking blocks. Carry deployment metadata through provider-family and fallback paths, and include canonicalModelId in the transcript policy cache key. Release note: Restore prompt-cache reuse for Claude Opus 5 and Sonnet 5 while continuing to strip unsupported Sonnet and Haiku 4.5 thinking history. * refactor: remove unused replay compatibility helper Use the canonical Claude drop predicate at the remaining test call sites and remove the unconsumed preservation export so the dependency/dead-code gate stays clean. * fix: keep Claude replay contract internal Use canonical llm-core identity resolution without exporting a new llm-core capability, avoiding unintended Plugin SDK API drift. --------- Co-authored-by: FullerStackDev <[email protected]>
1 parent 812bbd8 commit 1e19f22

6 files changed

Lines changed: 133 additions & 69 deletions

File tree

‎src/agents/transcript-policy.test.ts‎

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ vi.mock("../plugins/provider-hook-runtime.js", async () => {
6969
repairToolUseResultPairing: true,
7070
validateAnthropicTurns: true,
7171
allowSyntheticToolResults: true,
72-
...(modelId.includes("claude") &&
73-
!replayHelpers.shouldPreserveThinkingBlocks(modelId)
72+
...(replayHelpers.shouldDropClaudeThinkingBlocks(modelId)
7473
? { dropThinkingBlocks: true }
7574
: {}),
7675
};
@@ -92,8 +91,7 @@ vi.mock("../plugins/provider-hook-runtime.js", async () => {
9291
repairToolUseResultPairing: true,
9392
validateAnthropicTurns: true,
9493
allowSyntheticToolResults: true,
95-
...(modelId.includes("claude") &&
96-
!replayHelpers.shouldPreserveThinkingBlocks(modelId)
94+
...(replayHelpers.shouldDropClaudeThinkingBlocks(modelId)
9795
? { dropThinkingBlocks: true }
9896
: {}),
9997
};
@@ -438,23 +436,27 @@ describe("resolveTranscriptPolicy", () => {
438436
});
439437

440438
it("preserves thinking blocks for newer Claude models in unowned Anthropic transport fallback", () => {
441-
// Opus 4.6 via custom proxy: should NOT drop thinking blocks
442439
const opus46 = resolveTranscriptPolicy({
443440
provider: "custom-anthropic-proxy",
444441
modelId: "claude-opus-4-6",
445442
modelApi: "anthropic-messages",
446443
});
447444
expect(opus46.dropThinkingBlocks).toBe(false);
448445

449-
// Sonnet 4.5 via custom proxy: should NOT drop
446+
const opus5 = resolveTranscriptPolicy({
447+
provider: "custom-anthropic-proxy",
448+
modelId: "claude-opus-5",
449+
modelApi: "anthropic-messages",
450+
});
451+
expect(opus5.dropThinkingBlocks).toBe(false);
452+
450453
const sonnet45 = resolveTranscriptPolicy({
451454
provider: "custom-anthropic-proxy",
452455
modelId: "claude-sonnet-4-5-20250929",
453456
modelApi: "anthropic-messages",
454457
});
455-
expect(sonnet45.dropThinkingBlocks).toBe(false);
458+
expect(sonnet45.dropThinkingBlocks).toBe(true);
456459

457-
// Legacy Sonnet 3.7 via custom proxy: SHOULD drop
458460
const sonnet37 = resolveTranscriptPolicy({
459461
provider: "custom-anthropic-proxy",
460462
modelId: "claude-3-7-sonnet-20250219",
@@ -463,6 +465,57 @@ describe("resolveTranscriptPolicy", () => {
463465
expect(sonnet37.dropThinkingBlocks).toBe(true);
464466
});
465467

468+
it("uses canonical deployment metadata in unowned Anthropic transport fallback", () => {
469+
const policy = resolveTranscriptPolicy({
470+
provider: "custom-anthropic-proxy",
471+
modelId: "prod-opus",
472+
modelApi: "anthropic-messages",
473+
model: makeOpenAiCompatibleReasoningModel({
474+
id: "prod-opus",
475+
name: "Production Opus",
476+
provider: "custom-anthropic-proxy",
477+
api: "anthropic-messages",
478+
params: { canonicalModelId: "claude-opus-5" },
479+
}),
480+
});
481+
482+
expect(policy.dropThinkingBlocks).toBe(false);
483+
});
484+
485+
it("does not reuse cached Anthropic policies across canonical model identities", () => {
486+
const config = {} as OpenClawConfig;
487+
const model = makeOpenAiCompatibleReasoningModel({
488+
id: "production-claude",
489+
name: "Production Claude",
490+
provider: "custom-anthropic-proxy",
491+
api: "anthropic-messages",
492+
});
493+
494+
const sonnet45 = resolveTranscriptPolicy({
495+
config,
496+
provider: "custom-anthropic-proxy",
497+
modelId: model.id,
498+
modelApi: model.api,
499+
model: {
500+
...model,
501+
params: { canonicalModelId: "claude-sonnet-4-5-20250929" },
502+
},
503+
});
504+
const opus5 = resolveTranscriptPolicy({
505+
config,
506+
provider: "custom-anthropic-proxy",
507+
modelId: model.id,
508+
modelApi: model.api,
509+
model: {
510+
...model,
511+
params: { canonicalModelId: "claude-opus-5" },
512+
},
513+
});
514+
515+
expect(sonnet45.dropThinkingBlocks).toBe(true);
516+
expect(opus5.dropThinkingBlocks).toBe(false);
517+
});
518+
466519
it("strips thinking blocks for unowned Anthropic-compatible models that opt out of reasoning", () => {
467520
const policy = resolveTranscriptPolicy({
468521
provider: "qiniu",

‎src/agents/transcript-policy.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type { OpenClawConfig } from "../config/types.openclaw.js";
88
import { resolvePluginControlPlaneFingerprint } from "../plugins/plugin-control-plane-context.js";
99
import type { ProviderRuntimePluginHandle } from "../plugins/provider-hook-runtime.js";
1010
import { resolveProviderRuntimePlugin } from "../plugins/provider-hook-runtime.js";
11-
import { shouldPreserveThinkingBlocks } from "../plugins/provider-replay-helpers.js";
11+
import { shouldDropClaudeThinkingBlocks } from "../plugins/provider-replay-helpers.js";
1212
import type { ProviderRuntimeModel } from "../plugins/provider-runtime-model.types.js";
1313
import type { ProviderReplayPolicy } from "../plugins/types.js";
1414
import { isGoogleModelApi } from "./embedded-agent-helpers/google.js";
@@ -162,8 +162,8 @@ function buildUnownedProviderTransportReplayFallback(params: {
162162
},
163163
}
164164
: {}),
165-
...(isAnthropic && modelId.includes("claude")
166-
? { dropThinkingBlocks: !shouldPreserveThinkingBlocks(modelId) }
165+
...(isAnthropic && shouldDropClaudeThinkingBlocks(modelId, params.model)
166+
? { dropThinkingBlocks: true }
167167
: {}),
168168
...(isAnthropic && modelDisablesReasoningEffort(params.model)
169169
? { dropThinkingBlocks: true }
@@ -289,6 +289,10 @@ function resolveTranscriptPolicyCacheKey(params: {
289289
provider: params.provider,
290290
modelApi: params.modelApi ?? "",
291291
modelId: params.modelId ?? "",
292+
canonicalModelId:
293+
typeof params.model?.params?.canonicalModelId === "string"
294+
? params.model.params.canonicalModelId
295+
: "",
292296
dropsThinkingForReasoningCompat: modelDisablesReasoningEffort(params.model),
293297
preservesReasoningContentReplay: params.model?.reasoning === true,
294298
workspaceDir: params.workspaceDir ?? "",

‎src/plugin-sdk/provider-model-shared.test.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,10 @@ describe("buildProviderReplayFamilyHooks", () => {
222222
ctx: {
223223
provider: "anthropic-vertex",
224224
modelApi: "anthropic-messages",
225-
modelId: "claude-sonnet-4-6",
225+
modelId: "prod-opus",
226+
model: {
227+
params: { canonicalModelId: "claude-opus-5" },
228+
},
226229
},
227230
match: {
228231
validateAnthropicTurns: true,

‎src/plugin-sdk/provider-model-shared.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -407,13 +407,13 @@ export function buildProviderReplayFamilyHooks(
407407
}
408408
case "anthropic-by-model":
409409
return {
410-
buildReplayPolicy: ({ modelId }: ProviderReplayPolicyContext) =>
411-
buildAnthropicReplayPolicyForModel(modelId),
410+
buildReplayPolicy: ({ modelId, model }: ProviderReplayPolicyContext) =>
411+
buildAnthropicReplayPolicyForModel(modelId, model),
412412
};
413413
case "native-anthropic-by-model":
414414
return {
415-
buildReplayPolicy: ({ modelId }: ProviderReplayPolicyContext) =>
416-
buildNativeAnthropicReplayPolicyForModel(modelId),
415+
buildReplayPolicy: ({ modelId, model }: ProviderReplayPolicyContext) =>
416+
buildNativeAnthropicReplayPolicyForModel(modelId, model),
417417
};
418418
case "google-gemini":
419419
return {

‎src/plugins/provider-replay-helpers.test.ts‎

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,27 +128,50 @@ describe("provider replay helpers", () => {
128128
);
129129
});
130130

131-
it("preserves thinking blocks for Claude Opus 4.5+ and Sonnet 4.5+ models", () => {
132-
// These models should NOT drop thinking blocks
131+
it("preserves thinking blocks only for Claude models with native history support", () => {
133132
for (const modelId of [
134133
"claude-fable-5",
135134
"claude-opus-4-5-20251101",
136135
"claude-opus-4-6",
137-
"claude-sonnet-4-5-20250929",
138136
"claude-sonnet-4-6",
139-
"claude-haiku-4-5-20251001",
137+
"claude-opus-5",
138+
"claude-sonnet-5",
139+
"claude-mythos-5",
140+
"us.anthropic.claude-opus-5-20260101-v1:0",
140141
]) {
141142
const policy = buildAnthropicReplayPolicyForModel(modelId);
142143
expect(policy).not.toHaveProperty("dropThinkingBlocks");
143144
}
144145

145-
// These legacy models SHOULD drop thinking blocks
146-
for (const modelId of ["claude-3-7-sonnet-20250219", "claude-3-5-sonnet-20240620"]) {
146+
for (const modelId of [
147+
"claude-opus-4-1",
148+
"claude-sonnet-4-5-20250929",
149+
"claude-haiku-4-5-20251001",
150+
"claude-3-7-sonnet-20250219",
151+
"claude-3-5-sonnet-20240620",
152+
"claude-3-opus-20240229",
153+
"claude-opus-50",
154+
"claude-sonnet-50",
155+
"claude-sonnet-4-60",
156+
]) {
147157
const policy = buildAnthropicReplayPolicyForModel(modelId);
148158
expect(policy.dropThinkingBlocks).toBe(true);
149159
}
150160
});
151161

162+
it("uses canonical deployment metadata for Claude replay policy", () => {
163+
expect(
164+
buildAnthropicReplayPolicyForModel("prod-opus", {
165+
params: { canonicalModelId: "claude-opus-5" },
166+
}),
167+
).not.toHaveProperty("dropThinkingBlocks");
168+
expect(
169+
buildAnthropicReplayPolicyForModel("prod-sonnet", {
170+
params: { canonicalModelId: "claude-sonnet-4-5-20250929" },
171+
}),
172+
).toHaveProperty("dropThinkingBlocks", true);
173+
});
174+
152175
it("builds native Anthropic replay policy with selective tool-call id preservation", () => {
153176
// Sonnet 4.6 preserves thinking blocks
154177
const policy46 = buildNativeAnthropicReplayPolicyForModel("claude-sonnet-4-6");

‎src/plugins/provider-replay-helpers.ts‎

Lines changed: 28 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Provides shared replay-policy helpers for provider plugins.
2+
import { resolveClaudeModelIdentity, resolveClaudeOpus5ModelIdentity } from "@openclaw/llm-core";
23
import { normalizeLowercaseStringOrEmpty } from "@openclaw/normalization-core/string-coerce";
34
import type { AgentMessage } from "../agents/runtime/index.js";
45
import { sanitizeGoogleAssistantFirstOrdering } from "../shared/google-turn-ordering.js";
6+
import type { ProviderRuntimeModel } from "./provider-runtime-model.types.js";
57
import type {
68
ProviderReasoningOutputMode,
79
ProviderReplayPolicy,
@@ -92,59 +94,40 @@ export function buildStrictAnthropicReplayPolicy(
9294
};
9395
}
9496

95-
/**
96-
* Returns true for Claude models that preserve thinking blocks in context
97-
* natively (Fable 5, Opus 4.5+, Sonnet 4.5+, Haiku 4.5+). For these models,
98-
* dropping thinking blocks from prior turns breaks replay and prompt caching.
99-
*
100-
* See: https://platform.claude.com/docs/en/build-with-claude/extended-thinking#differences-in-thinking-across-model-versions
101-
*
102-
* @deprecated Anthropic-family provider replay helper; prefer provider-local replay hooks.
103-
*/
104-
export function shouldPreserveThinkingBlocks(modelId?: string): boolean {
105-
const id = normalizeLowercaseStringOrEmpty(modelId);
106-
if (!id.includes("claude")) {
107-
return false;
108-
}
109-
110-
// Models that preserve thinking blocks natively (Claude 4.5+):
111-
// - claude-fable-5
112-
// - claude-opus-4-x (opus-4-5, opus-4-6, ...)
113-
// - claude-sonnet-4-x (sonnet-4-5, sonnet-4-6, ...)
114-
// Note: "sonnet-4" is safe — legacy "claude-3-5-sonnet" does not contain "sonnet-4"
115-
// - claude-haiku-4-x (haiku-4-5, ...)
116-
// Models that require dropping thinking blocks:
117-
// - claude-3-7-sonnet, claude-3-5-sonnet, and earlier
118-
if (
119-
id.includes("fable-5") ||
120-
id.includes("opus-4") ||
121-
id.includes("sonnet-4") ||
122-
id.includes("haiku-4")
123-
) {
124-
return true;
125-
}
126-
127-
// Future-proofing: claude-5-x, claude-6-x etc. should also preserve
128-
if (/claude-[5-9]/.test(id) || /claude-\d{2,}/.test(id)) {
129-
return true;
130-
}
131-
132-
return false;
97+
/** @deprecated Anthropic-family provider replay helper; prefer provider-local replay hooks. */
98+
export function shouldDropClaudeThinkingBlocks(
99+
modelId?: string,
100+
model?: Pick<ProviderRuntimeModel, "params">,
101+
): boolean {
102+
const ref = { id: modelId, params: model?.params };
103+
const canonicalId = resolveClaudeModelIdentity(ref);
104+
const isClaude =
105+
canonicalId.startsWith("claude-") || resolveClaudeOpus5ModelIdentity(ref) !== undefined;
106+
const preservesThinking =
107+
resolveClaudeOpus5ModelIdentity(ref) !== undefined ||
108+
/(?:^|-)claude-(?:fable-5|mythos-(?:5|preview)|opus-4-(?:5|6|7|8)|sonnet-(?:5|4-6))(?=$|[^a-z0-9])/.test(
109+
canonicalId,
110+
);
111+
return isClaude && !preservesThinking;
133112
}
134113

135114
/** @deprecated Anthropic-family provider replay helper; prefer provider-local replay hooks. */
136-
export function buildAnthropicReplayPolicyForModel(modelId?: string): ProviderReplayPolicy {
137-
const isClaude = normalizeLowercaseStringOrEmpty(modelId).includes("claude");
115+
export function buildAnthropicReplayPolicyForModel(
116+
modelId?: string,
117+
model?: Pick<ProviderRuntimeModel, "params">,
118+
): ProviderReplayPolicy {
138119
return buildStrictAnthropicReplayPolicy({
139-
dropThinkingBlocks: isClaude && !shouldPreserveThinkingBlocks(modelId),
120+
dropThinkingBlocks: shouldDropClaudeThinkingBlocks(modelId, model),
140121
});
141122
}
142123

143124
/** @deprecated Anthropic-family provider replay helper; prefer provider-local replay hooks. */
144-
export function buildNativeAnthropicReplayPolicyForModel(modelId?: string): ProviderReplayPolicy {
145-
const isClaude = normalizeLowercaseStringOrEmpty(modelId).includes("claude");
125+
export function buildNativeAnthropicReplayPolicyForModel(
126+
modelId?: string,
127+
model?: Pick<ProviderRuntimeModel, "params">,
128+
): ProviderReplayPolicy {
146129
return buildStrictAnthropicReplayPolicy({
147-
dropThinkingBlocks: isClaude && !shouldPreserveThinkingBlocks(modelId),
130+
dropThinkingBlocks: shouldDropClaudeThinkingBlocks(modelId, model),
148131
sanitizeToolCallIds: true,
149132
preserveNativeAnthropicToolUseIds: true,
150133
});
@@ -156,12 +139,10 @@ export function buildHybridAnthropicOrOpenAIReplayPolicy(
156139
options: { anthropicModelDropThinkingBlocks?: boolean } = {},
157140
): ProviderReplayPolicy | undefined {
158141
if (ctx.modelApi === "anthropic-messages" || ctx.modelApi === "bedrock-converse-stream") {
159-
const isClaude = normalizeLowercaseStringOrEmpty(ctx.modelId).includes("claude");
160142
return buildStrictAnthropicReplayPolicy({
161143
dropThinkingBlocks:
162144
options.anthropicModelDropThinkingBlocks &&
163-
isClaude &&
164-
!shouldPreserveThinkingBlocks(ctx.modelId),
145+
shouldDropClaudeThinkingBlocks(ctx.modelId, ctx.model),
165146
});
166147
}
167148

0 commit comments

Comments
 (0)