|
| 1 | +// thinking-block-sanitize — request-path mitigation for the CC thinking-desync |
| 2 | +// wedge (anthropics/claude-code#63147). On replay paths (resume / --continue / |
| 3 | +// auto-compaction / parallel-tool-cancel), CC re-sends prior assistant turns' |
| 4 | +// thinking in the OMITTED shape `{ type:"thinking", thinking:"", signature }`. |
| 5 | +// The API rejects modified thinking in the *latest* assistant message with a |
| 6 | +// permanent 400, which wedges the session. This extension drops the omitted |
| 7 | +// thinking blocks the API treats as optional, before the request is forwarded. |
| 8 | +// |
| 9 | +// Resolved turn-selection rule (directive Open Question 1, empirical capture): |
| 10 | +// - drop omitted thinking from ALL prior assistant turns, AND |
| 11 | +// - from the LATEST assistant turn UNLESS it is an active tool-continuation |
| 12 | +// (last block is a tool_use with a following tool_result) — that case is |
| 13 | +// uncoverable by the proxy (the API needs the signed thinking for the |
| 14 | +// pending tool call; we can't restore the emptied text) → user-side |
| 15 | +// DISABLE_INTERLEAVED_THINKING=1. |
| 16 | +// Never touches non-empty thinking, and never touches redacted_thinking (v1). |
| 17 | +// |
| 18 | +// OPT-IN for v1: only runs when CACHE_FIX_THINKING_SANITIZE=on (default off) — |
| 19 | +// it mutates request bodies and its coverage is not yet live-validated. |
| 20 | +// |
| 21 | +// Order 550: after the request-body mutators (ttl-management 500) and before |
| 22 | +// session-health (590), so #160's thinking_block_count reflects the forwarded |
| 23 | +// body. The per-request drop count is exposed via ctx.meta._thinkingSanitize |
| 24 | +// for cache-telemetry (600) to merge into the per-session JSON. |
| 25 | + |
| 26 | +export function isOmittedThinking(block) { |
| 27 | + return ( |
| 28 | + !!block && |
| 29 | + block.type === "thinking" && |
| 30 | + typeof block.thinking === "string" && |
| 31 | + block.thinking.trim() === "" |
| 32 | + ); |
| 33 | +} |
| 34 | + |
| 35 | +function hasToolResult(msg) { |
| 36 | + return ( |
| 37 | + !!msg && |
| 38 | + Array.isArray(msg.content) && |
| 39 | + msg.content.some((b) => b && b.type === "tool_result") |
| 40 | + ); |
| 41 | +} |
| 42 | + |
| 43 | +// The latest assistant message is an active tool-continuation when its last |
| 44 | +// block is a tool_use that a later message answers with a tool_result. The API |
| 45 | +// requires that turn's thinking intact, so we must not strip it. |
| 46 | +export function isActiveToolContinuation(messages, idx) { |
| 47 | + const msg = messages[idx]; |
| 48 | + if (!msg || !Array.isArray(msg.content) || msg.content.length === 0) return false; |
| 49 | + const last = msg.content[msg.content.length - 1]; |
| 50 | + if (!last || last.type !== "tool_use") return false; |
| 51 | + for (let j = idx + 1; j < messages.length; j++) { |
| 52 | + if (hasToolResult(messages[j])) return true; |
| 53 | + } |
| 54 | + return false; |
| 55 | +} |
| 56 | + |
| 57 | +function latestAssistantIndex(messages) { |
| 58 | + for (let i = messages.length - 1; i >= 0; i--) { |
| 59 | + if (messages[i] && messages[i].role === "assistant") return i; |
| 60 | + } |
| 61 | + return -1; |
| 62 | +} |
| 63 | + |
| 64 | +// Pure planner: returns { messages, dropped }. Does not mutate the input. |
| 65 | +// `messages` is the new array (a message that loses all content is dropped). |
| 66 | +export function planSanitize(messages) { |
| 67 | + if (!Array.isArray(messages)) return { messages, dropped: 0 }; |
| 68 | + const latestAsst = latestAssistantIndex(messages); |
| 69 | + const protectLatest = latestAsst >= 0 && isActiveToolContinuation(messages, latestAsst); |
| 70 | + |
| 71 | + let dropped = 0; |
| 72 | + let changed = false; |
| 73 | + const out = []; |
| 74 | + for (let i = 0; i < messages.length; i++) { |
| 75 | + const msg = messages[i]; |
| 76 | + if (!msg || msg.role !== "assistant" || !Array.isArray(msg.content)) { |
| 77 | + out.push(msg); |
| 78 | + continue; |
| 79 | + } |
| 80 | + if (i === latestAsst && protectLatest) { |
| 81 | + out.push(msg); // active continuation — leave its thinking intact |
| 82 | + continue; |
| 83 | + } |
| 84 | + const kept = msg.content.filter((b) => { |
| 85 | + if (isOmittedThinking(b)) { |
| 86 | + dropped++; |
| 87 | + return false; |
| 88 | + } |
| 89 | + return true; |
| 90 | + }); |
| 91 | + if (kept.length === msg.content.length) { |
| 92 | + out.push(msg); // unchanged |
| 93 | + } else if (kept.length === 0) { |
| 94 | + changed = true; // message became empty → drop it entirely |
| 95 | + } else { |
| 96 | + out.push({ ...msg, content: kept }); |
| 97 | + changed = true; |
| 98 | + } |
| 99 | + } |
| 100 | + return { messages: changed ? out : messages, dropped }; |
| 101 | +} |
| 102 | + |
| 103 | +export default { |
| 104 | + name: "thinking-block-sanitize", |
| 105 | + description: |
| 106 | + "Drop omitted (empty-text) thinking blocks from prior assistant turns and the latest non-continuation turn, to head off the CC thinking-desync 400 (#63147). Opt-in via CACHE_FIX_THINKING_SANITIZE=on.", |
| 107 | + order: 550, |
| 108 | + |
| 109 | + async onRequest(ctx) { |
| 110 | + if (process.env.CACHE_FIX_THINKING_SANITIZE !== "on") return; |
| 111 | + const body = ctx.body; |
| 112 | + if (!body || !Array.isArray(body.messages)) return; |
| 113 | + |
| 114 | + const { messages, dropped } = planSanitize(body.messages); |
| 115 | + if (dropped > 0) body.messages = messages; |
| 116 | + |
| 117 | + // Counts only — never content. Exposed for cache-telemetry to persist and |
| 118 | + // for the #160 session-health signal. |
| 119 | + ctx.meta._thinkingSanitize = { thinking_blocks_dropped: dropped }; |
| 120 | + }, |
| 121 | +}; |
0 commit comments