Repository navigation
Description
Summary
Resuming a run that parked on a client-tool interrupt, when that run made a server tool call first, saves the tool result twice. withPersistence appends a second copy of every earlier tool result after the interrupted assistant turn. The next provider request is then invalid. Anthropic answers 400 invalid_request_error: messages.N.content.0: unexpected tool_use_id found in tool_result blocks … Each tool_result block must have a corresponding tool_use block in the previous message. The thread stays broken for every later run.
Versions: @tanstack/ai 0.61.0, @tanstack/ai-persistence 0.6.7, @tanstack/ai-client 0.35.1.
Mechanism
- The run calls a server tool (result stored as
{ role: "tool", toolCallId: "call-3" }, noid). It then calls a client tool and stops on the interrupt. - On the interrupt,
buildMessagesSnapshotChunk(activities/chat/index.js) gives id-less model messages a fresh id:snapshot_${runId}_${index}. The browser'sStreamProcessortakes theMESSAGES_SNAPSHOTas its state. - The user answers.
ChatClientpostsuiMessagesToWire(messages), where the stored tool result now carriesid: "snapshot_<run>_2". mergeStoredMessages(stored, incoming)(ai-persistence/src/merge-stored.ts):- The cutoff is the last incoming id found in the store: the interrupted assistant turn.
- The incoming tool message has id
snapshot_…_2, which the store has never seen (its copy has no id). So it is pushed after the prefix: a secondcall-3result, right after the assistant turn that called only the client tool. - Id-less incoming tool messages fall through to the
existing.content === message.contentcheck. It never matches whencontentis a parts array (an MCP result, for example), so those get re-appended too.
Repro (shape)
Server: chat({ messages, middleware: [withPersistence(store)] }) with a scripted adapter:
- step 1: call server tool
load_skill. - step 2: call client tool
ask_user(the run interrupts). - step 3: text.
Browser side, as ChatClient does it:
const processor = new StreamProcessor();
for (const chunk of chunksOfFirstRun) processor.processChunk(chunk); // includes MESSAGES_SNAPSHOT
processor.addToolResult(askUserCallId, answer);
const messages = uiMessagesToWire(processor.getMessages());
// POST { messages, resume: [{ interruptId: `client_tool_${askUserCallId}`, status: "resolved", payload: answer }] }What the server sees:
posted: [user message-1, assistant message-2, tool(id "snapshot_agui-run-1_2", call-3), assistant message-5, tool(id "tool-call-6", call-6)]
stored: [user message-1, assistant message-2, tool(no id, call-3), assistant message-5]
merged → tool results in order: call-3, call-3, call-6 (expected: call-3, call-6)
Expected
A tool result already stored for a toolCallId is not appended again, whatever id the client gives it. Possible fixes:
- give stored model messages the same ids the snapshot uses;
- or have
mergeStoredMessagesmatch tool messages bytoolCallIdinstead ofid/contentidentity.
Workaround
Before chat(), drop posted role: "tool" messages whose toolCallId the stored thread already answers.
Activity
AlexDemzz commentedon Sep 30, 2026
Follow-up: the workaround I suggested above (dropping posted tool results whose
toolCallIdthe stored thread already holds) is wrong.When the posted history is the stored thread plus a new turn, the cutoff (the last incoming id found in stored) falls before an id-less result stored last. That result is then dropped, and the run reaches the model with the call unanswered.
What works for us is giving every tool result one stable id on both sides of the merge:
idon the stored thread as it loads.idandmetadata.tanstack.toolResult.id.convertMessagesToModelMessages→restoreToolResultOwnershiprestores the message id from that metadata field, which is where the browser'ssnapshot_<run>_<n>id comes back from even after the top-levelidhas been rewritten.That suggests the root fix: either persist the ids the snapshot mints, or have the merge match tool messages by
toolCallId.goce-cz commentedon Oct 12, 2026
This is marked as resolved by a PR that explicitly says that it doesn't fix this problem:
Can anyone shed some light on this please? Or re-open the issue?
We're facing the same one and the workaround we had to do feels quite wrong.