From acf63dd776b935fb467dc64ea7776e5e08557e49 Mon Sep 17 00:00:00 2001 From: panjinhe Date: Sun, 15 Nov 2026 20:47:16 -0800 Subject: [PATCH] fix(crag): drop unsigned history thinking before CRAG print replay CRAG print bypasses stripInvalidThinkingBlocks, so a history thinking block with an empty signature (e.g. carried over from a relay that never signs) went upstream verbatim and drew 400 "Invalid `signature` in `thinking` block" on every turn. Drop blocks without a usable signature; signed blocks untouched. --- src/lib/protocol/crag-print-prompt.mjs | 24 ++++++++++++++++++ test/unit/crag-print-prompt.test.mjs | 35 ++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/src/lib/protocol/crag-print-prompt.mjs b/src/lib/protocol/crag-print-prompt.mjs index 577c7d338..e2d2d0ecd 100644 --- a/src/lib/protocol/crag-print-prompt.mjs +++ b/src/lib/protocol/crag-print-prompt.mjs @@ -8,6 +8,7 @@ import fs from 'node:fs' import path from 'node:path' import { normalizeCragKernelVersion, normalizeKernelDataplane, normalizeSlotPersonaPreset } from '../vm/slot-engine.mjs' +import { hasUsableThinkingSignature } from './anthropic-policy.mjs' /** * Print is the CRAG v2 worker only. A live health report wins, then an * explicit dataplane on the VM, then the kernel.json that worker was started @@ -389,6 +390,28 @@ function foldSummarySystemTurns(messages) { return { messages: [...folded.messages, ...tail], outputConfig } } +/** + * Print bypasses stripInvalidThinkingBlocks. A history thinking block with no + * usable signature (empty, short, dummy) is always a 400 "Invalid `signature` + * in `thinking` block" upstream, e.g. history from a relay that never signs. + * Drop it, as Claude Code's own retry does. Signed blocks stay untouched: the + * kernel fingerprints them. A turn that would be left empty keeps its shape. + */ +export function dropUnsignedHistoryThinking(messages) { + if (!Array.isArray(messages)) return messages + let changed = false + const out = messages.map((message) => { + if (message?.role !== 'assistant' || !Array.isArray(message.content)) return message + const content = message.content.filter( + (block) => block?.type !== 'thinking' || hasUsableThinkingSignature(block.signature), + ) + if (content.length === message.content.length || !content.length) return message + changed = true + return { ...message, content } + }) + return changed ? out : messages +} + /** * @param {object} body caller body, before cli-node persona injection * @param {{ preset?: 'official_full' | 'zero', officialPassthrough?: boolean, capabilities?: string[], agentSummary?: boolean, foldSystemMessages?: boolean }} [options] @@ -404,6 +427,7 @@ export function prepareCragPrintBody( } = {}, ) { const out = { ...(body || {}) } + if (Array.isArray(out.messages)) out.messages = dropUnsignedHistoryThinking(out.messages) if (agentSummary) out.messages = readOnlySummaryResults(out.messages) if (foldSystemMessages) { const folded = agentSummary ? foldSummarySystemTurns(out.messages) : foldSystemTurns(out.messages) diff --git a/test/unit/crag-print-prompt.test.mjs b/test/unit/crag-print-prompt.test.mjs index 82d0b5873..45e05d3ed 100644 --- a/test/unit/crag-print-prompt.test.mjs +++ b/test/unit/crag-print-prompt.test.mjs @@ -8,6 +8,7 @@ import { callerCwdDeclaration, callerSystemTail, cragV2KernelActive, + dropUnsignedHistoryThinking, foldHistorySystemMessages, prepareCragPrintBody, projectCallerSystem, @@ -484,3 +485,37 @@ test('a fold that would put a 1h breakpoint after a 5m one keeps the system turn assert.equal(folded[2].content[0].type, 'tool_result') assert.equal(folded[2].content[1].cache_control.ttl, '1h') }) + +test('print drops history thinking blocks without a usable signature', () => { + const signed = { type: 'thinking', thinking: 'kept', signature: 'S'.repeat(64) } + const body = { + model: 'claude-fable-5-2', + messages: [ + { role: 'user', content: [{ type: 'text', text: 'q' }] }, + { + role: 'assistant', + content: [ + { type: 'thinking', thinking: 'relay history', signature: '' }, + { type: 'tool_use', id: 't1', name: 'Read', input: {} }, + ], + }, + { role: 'user', content: [{ type: 'tool_result', tool_use_id: 't1', content: 'ok' }] }, + { role: 'assistant', content: [signed, { type: 'text', text: 'done' }] }, + { role: 'user', content: [{ type: 'text', text: 'next' }] }, + ], + } + const out = prepareCragPrintBody(body, { preset: 'official_full' }) + assert.deepEqual( + out.messages[1].content.map((b) => b.type), + ['tool_use'], + ) + assert.equal(out.messages[3].content[0], signed) + assert.equal(body.messages[1].content.length, 2, 'caller body is not mutated') +}) + +test('dropUnsignedHistoryThinking keeps a turn that would be emptied and returns unchanged arrays as-is', () => { + const only = [{ role: 'assistant', content: [{ type: 'thinking', thinking: 'x', signature: '' }] }] + assert.equal(dropUnsignedHistoryThinking(only), only) + const clean = [{ role: 'assistant', content: [{ type: 'text', text: 'a' }] }] + assert.equal(dropUnsignedHistoryThinking(clean), clean) +})