[BUG] After an approved `needsApproval` tool runs, memory stores `approval: { id }` without `approved`, and every later generateText throws AI_TypeValidationError
Versions: @voltagent/core 2.11.1, ai 6.0.326, zod 4.6.7, Node 24. InMemoryStorageAdapter, default conversationPersistence (mode: "step").
Describe the bug
After the user approves a needsApproval tool call (the flow in docs/agents/tools.md, "How the Approval Flow Works"), the tool runs and the turn completes. Memory then stores the tool part as state: "output-available" with approval: { id }, and the approved: true the client sent is gone. The AI SDK's UIMessage schema rejects an output-available part whose approval.approved is not true. As a result, every later generateText on that conversation throws AI_TypeValidationError. That includes a plain follow-up question and a second approval in the same conversation.
To reproduce (mock model, no API key)
const model = new MockLanguageModelV3({ doGenerate: async ({ prompt }) => {
await new Promise(r => setTimeout(r, 1))
return prompt.some(m => m.role === 'tool')
? { content: [{ type: 'text', text: 'Refund done.' }], finishReason: { unified: 'stop', raw: 'stop' }, usage, warnings: [] }
: { content: [{ type: 'tool-call', toolCallId: 'call_1', toolName: 'refund', input: '{"amount":50}' }], finishReason: { unified: 'tool-calls', raw: 'tool_use' }, usage, warnings: [] }
}})
const storage = new InMemoryStorageAdapter()
const agent = new Agent({ name: 'a', instructions: 'x', model, memory: new Memory({ storage }),
tools: [createTool({ name: 'refund', description: 'refund', parameters: z.object({ amount: z.number() }),
needsApproval: true, execute: async () => ({ refunded: true }) })] })
const memory = { userId: 'u', conversationId: 'c' }
await agent.generateText('refund 50', { memory }) // -> tool-approval-request
const msg = structuredClone((await storage.getMessages('u', 'c')).at(-1))
const part = msg.parts.find(p => p.type === 'tool-refund')
part.state = 'approval-responded'; part.approval = { id: part.approval.id, approved: true } // what addToolApprovalResponse produces
await agent.generateText([msg], { memory }) // tool runs, "Refund done."
await agent.generateText('did it go through?', { memory }) // throws AI_TypeValidationError
Observed
- Stored part after turn 2:
{ state: 'output-available', approval: { id: 'aitxt-…' }, output: {…} }(noapproved). - Turn 3:
AI_TypeValidationError: Type validation failed … path [2,"parts",0] … approval.approved: Invalid input: expected true. - With
agent.streamText([msg], { memory })for turn 2 the symptom is different: the stored part staysapproval-respondedwith nooutputafter the run completes. The tool result is not persisted at all.
Expected: memory stores the part the way the AI SDK UI state has it: state: 'output-available', approval: { id, approved: true }, with the output. Later turns then work.
Cause (as far as I can tell)
ConversationBufferis seeded from stored memory only (buffer.ingestUIMessages(result.messages, true)inagent.ts). Stored memory still has the pre-approval part,approval: { id }. The approval response from the input is persisted separately and never reaches the buffer.- When the step's
tool-resultis merged,ConversationBuffer.tryMergeToolPartsetsstate = 'output-available'and keeps the staleapproval(conversation-buffer.ts,if (part.approval !== undefined) existingPart.approval = part.approvalonly copies when the incoming part has one). - The flush then overwrites the input message, which had
approved: trueand the same message id.
Suggested fix
- Apply approval responses from the input to the buffer before the run starts: match on
approval.id/toolCallIdand copyapproval+state. - And/or, in
tryMergeToolPart, when a part moves tooutput-available/output-errorandexistingPart.approval?.approvedis undefined, set it totrue. A part can only get an output if it was approved. - Optionally, normalize such parts when loading memory, so conversations already broken by this can recover.
Found while testing VoltAgent with Shakedown (a fault-injection test harness for agent tool calls).
Reproduced this against current main with the mock-model repro from the issue, and opened a fix in #1540. The stored tool part keeps state: "output-available" with an approval object missing approved, which fails UIMessage validation on the next turn. The PR applies only real client approval decisions (approved strictly boolean) onto buffered parts and adds regression tests for both the approved and denied paths.