Skip to content

Commit fcb19ae

Browse files
committed
fix(core): strip cache hints and metadata only at message and part level in the cache probe
1 parent 34452c7 commit fcb19ae

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

‎packages/core/src/session/cache-probe.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,8 @@ export interface Miss extends Change {
5656

5757
const INLINE_DATA_MIN = 1024
5858

59-
// Cache hints only steer breakpoints and metadata never reaches the provider, so neither affects the cached prefix.
6059
// Inline media is summarized by type and size so hashing stays fast; replacing or cutting it still changes the hash.
6160
const replacer = (key: string, value: unknown) => {
62-
if (key === "cache" || key === "metadata") return undefined
6361
if (key === "source" && value && typeof value === "object" && "data" in value) {
6462
const source = value as { type: string; mediaType?: string; data: string | Uint8Array }
6563
const size = typeof source.data === "string" ? source.data.length : source.data.byteLength
@@ -85,10 +83,19 @@ const isCheckpoint = (message: Message) =>
8583
(part) => part.type === "compaction" || (part.type === "text" && part.text.startsWith("<conversation-checkpoint>")),
8684
)
8785

86+
// Message and part cache hints only steer breakpoints and their metadata never reaches the provider, so neither affects
87+
// the cached prefix. Same-named keys inside schemas, tool inputs and results do, so only these levels are stripped.
88+
const prefixOf = (message: Message) => ({
89+
...message,
90+
cache: undefined,
91+
metadata: undefined,
92+
content: message.content.map((part) => ({ ...part, cache: undefined, metadata: undefined })),
93+
})
94+
8895
const printMessage = (message: Message): MessagePrint => ({
8996
key: messageKey(message),
9097
role: message.role,
91-
hash: hash(message),
98+
hash: hash(prefixOf(message)),
9299
media: message.content.reduce((total, part) => total + mediaCount(part), 0),
93100
checkpoint: isCheckpoint(message),
94101
})

‎packages/core/test/session-cache-probe.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,20 @@ describe("CacheProbe.diagnose", () => {
7272
})
7373
})
7474

75+
test("tools changed when a nested schema metadata property changes", () => {
76+
const mcp = (description: string) =>
77+
new ToolDefinition({
78+
name: "edit",
79+
description: "edit tool",
80+
inputSchema: { type: "object", properties: { metadata: { type: "string", description } } },
81+
})
82+
const previous = step({ ...base, tools: [tool("read"), mcp("old")] })
83+
expect(CacheProbe.diagnose(previous, step({ ...base, tools: [tool("read"), mcp("new")] }, 1_000))).toMatchObject({
84+
reason: "tools-changed",
85+
detail: "~edit",
86+
})
87+
})
88+
7589
test("instructions changed", () => {
7690
expect(diagnose({ ...base, system: [base.system[0]!, SystemPart.make("AGENTS.md: be thorough")] })).toMatchObject({
7791
reason: "instructions-changed",

0 commit comments

Comments
 (0)