voltagent icon indicating copy to clipboard operation
voltagent copied to clipboard

[BUG] After an approved `needsApproval` tool runs, memory stores `approval: { id }` without `approved`, and every later generateText throws AI_TypeValidationError

Open zennicalles opened this issue 2 weeks ago • 1 comments

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: {…} } (no approved).
  • 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 stays approval-responded with no output after 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)

  • ConversationBuffer is seeded from stored memory only (buffer.ingestUIMessages(result.messages, true) in agent.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-result is merged, ConversationBuffer.tryMergeToolPart sets state = 'output-available' and keeps the stale approval (conversation-buffer.ts, if (part.approval !== undefined) existingPart.approval = part.approval only copies when the incoming part has one).
  • The flush then overwrites the input message, which had approved: true and the same message id.

Suggested fix

  • Apply approval responses from the input to the buffer before the run starts: match on approval.id / toolCallId and copy approval + state.
  • And/or, in tryMergeToolPart, when a part moves to output-available / output-error and existingPart.approval?.approved is undefined, set it to true. 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).

zennicalles avatar Nov 14 '26 01:11 zennicalles

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.

tomwangcn avatar Nov 14 '26 03:11 tomwangcn