Claude Code. Any Model. The most powerful AI coding agent now speaks every language.
fix(openai-codex): keep cache_control inside tool inputs in hashes
Jack Rudenkoc01900b765c7
diff --git a/packages/cli/src/code-mode/hashes.ts b/packages/cli/src/code-mode/hashes.tsindex 49db8b27..eb11e0b2 100644--- a/packages/cli/src/code-mode/hashes.ts+++ b/packages/cli/src/code-mode/hashes.ts@@ -24,18 +24,23 @@ export interface BatchResultEntry {}/**- * Per-request transport fields a key must not depend on. `cache_control` is a+ * A message, a content block or a content array without Claude Code's+ * `cache_control` markers, at the message and content-block levels ONLY+ * (recursing into `content`, e.g. a tool_result's blocks; never into a tool+ * `input`, where `cache_control` is the tool's own data). The marker is a* prompt-cache breakpoint Claude Code moves between requests: the message that* carried it when a record was written no longer does when history is* rendered, so a hash over it never matched again.*/-const TRANSPORT_KEYS = new Set(["cache_control"]);+export function withoutCacheMarkers(value: unknown): unknown {+ if (Array.isArray(value)) return value.map(withoutCacheMarkers);+ if (value === null || typeof value !== "object") return value;+ const { cache_control: _marker, ...rest } = value as Record<string, unknown>;+ if (Array.isArray(rest.content)) rest.content = rest.content.map(withoutCacheMarkers);+ return rest;+}-/**- * JSON with object keys sorted at every depth, transport fields left out: a- * stable serialisation for hashing (`batchHash`, `turnAnchorFor`, replay's- * input comparison), never for sending.- */+/** JSON with object keys sorted at every depth: a stable serialisation for hashing, never for sending. */export function canonicalJson(value: unknown): string {if (value === null || typeof value !== "object") {return JSON.stringify(value) ?? "null";@@ -44,7 +49,7 @@ export function canonicalJson(value: unknown): string {return `[${value.map((v) => canonicalJson(v)).join(",")}]`;}const keys = Object.keys(value as Record<string, unknown>)- .filter((k) => (value as Record<string, unknown>)[k] !== undefined && !TRANSPORT_KEYS.has(k))+ .filter((k) => (value as Record<string, unknown>)[k] !== undefined).sort();const body = keys.map((k) => `${JSON.stringify(k)}:${canonicalJson((value as Record<string, unknown>)[k])}`)@@ -73,7 +78,11 @@ export function batchHash(entries: readonly BatchResultEntry[]): string {const hash = createHash("sha256");for (const entry of ordered) {const canonical = entry.result- ? canonicalJson([entry.toolUseId, entry.result.isError === true, entry.result.content ?? ""])+ ? canonicalJson([+ entry.toolUseId,+ entry.result.isError === true,+ withoutCacheMarkers(entry.result.content ?? ""),+ ]): canonicalJson([entry.toolUseId, "missing"]);hash.update(canonical);hash.update("\n");diff --git a/packages/cli/src/code-mode/turn-records.ts b/packages/cli/src/code-mode/turn-records.tsindex 8fcbb7f8..c13939a0 100644--- a/packages/cli/src/code-mode/turn-records.ts+++ b/packages/cli/src/code-mode/turn-records.ts@@ -21,7 +21,7 @@import { createHash } from "node:crypto";import { log } from "../logger.js";-import { canonicalJson } from "./hashes.js";+import { canonicalJson, withoutCacheMarkers } from "./hashes.js";import type { ClaudeMessage, ResponsesItem } from "./types.js";export interface TurnRecord {@@ -75,7 +75,9 @@ export function turnAnchorFor(: blocks.filter((b) => b.type === "text" && typeof b.text === "string").map((b) => (b.text as string).trim());- const previous = previousMessage ? sha256Hex(canonicalJson(previousMessage)) : "";+ const previous = previousMessage+ ? sha256Hex(canonicalJson(withoutCacheMarkers(previousMessage)))+ : "";return sha256Hex(`${sessionKey}\0${previous}\0${texts.join("\n")}`);}