Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/lib/protocol/crag-print-prompt.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand All @@ -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)
Expand Down
35 changes: 35 additions & 0 deletions test/unit/crag-print-prompt.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
callerCwdDeclaration,
callerSystemTail,
cragV2KernelActive,
dropUnsignedHistoryThinking,
foldHistorySystemMessages,
prepareCragPrintBody,
projectCallerSystem,
Expand Down Expand Up @@ -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)
})
Loading