[BUG] The result of an approved (`needsApproval`) tool call is not persisted when the tool completes; an interrupted turn lets a resent approval execute the tool again
Versions: @voltagent/core 2.11.1, ai 6.0.326. Default conversationPersistence (mode: "step", flushOnToolResult: true).
Describe the bug
For ordinary tools, VoltAgent flushes the tool result to memory as soon as the step's tools complete. The memory docs say: "Tool completion events (tool-result, tool-error) trigger immediate flushes by default … reducing history loss risk on crashes or process interruption."
For a tool approved via needsApproval this does not happen. AI SDK 6 executes approved calls before step 1, and the result only shows up in step 1's response.messages, after the next model call. VoltAgent only records results in onStepFinish. If the turn is interrupted after the approved tool ran but before step 1 finishes (client disconnect, request timeout, crash, provider error), memory still says approval-responded with no output. Resending the same approval (client retry, queue redelivery) executes the tool again, with no new approval prompt.
To reproduce
// mock model: calls `refund` first; once a tool-result is in the prompt it waits 200 ms (honouring abortSignal), then answers
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 () => ({ refundId: `re_${++refunds}` }) })] })
await agent.generateText('refund 50', { memory }) // approval requested
const msg = /* stored assistant message, part -> approval-responded, approved: true */
const ac = new AbortController(); setTimeout(() => ac.abort(new Error('client disconnected')), 50)
await agent.generateText([structuredClone(msg)], { memory, abortSignal: ac.signal }).catch(() => {}) // refund #1 runs, then abort
// memory: { state: 'approval-responded', approved: true, output: undefined }
await agent.generateText([structuredClone(msg)], { memory }) // client retries -> refund #2, no approval prompt
Control: the same tool without needsApproval, interrupted at the same point, is stored as output-available with its output.
Observed: after the interrupted run, the stored part is approval-responded with no output. The retried request runs refund a second time (refunds === 2). On streamText the output is missing from memory even when the run completes normally (stored part stays approval-responded, output undefined).
Expected: the approved call's result is persisted as soon as it completes, the same as other tool results. A resent approval then finds a tool-result for that call, and the AI SDK skips it (collectToolApprovals ignores approvals whose call already has a result).
Cause: results are only added to the ConversationBuffer in createStepHandler (onStepFinish). The pre-step execution of approved calls in AI SDK 6 generateText / streamText has no step event of its own, so nothing records or flushes it until step 1 finishes.
Suggested fix
- Record approved-call results when they complete. Options: in
createToolExecutionFactory's success/error path (VoltAgent already wraps everyexecute), or via the AI SDKonToolCallFinishcallback. Merge the result into the buffer and flush whenflushOnToolResultis on. - Make the
streamTextpath persist the result as well. - It may also be worth documenting that approved tools have at-least-once semantics across retries.
Found while testing VoltAgent with Shakedown (a fault-injection test harness for agent tool calls).
Verified against current main with a mock-model repro: after the approved tool runs, aborting the turn before step 1 finishes leaves the stored part at approval-responded with no output, and resending the same approval re-executes the tool with no new approval prompt. I'll take this.
Scoping note for the fix: it will narrow the window between approved-tool execution and persistence by flushing the executed result immediately, not provide exactly-once semantics - a crash between the tool's own commit and storage can still require tool-side idempotency.
Fix opened in #1541 (stacked on #1540 since the two interact through the same buffered parts). It flushes the approved tool's result to memory right after execution, keeps stored terminal tool parts when a resent input would regress them, and reconciles resent input messages against stored execution evidence when building model messages - so a resent approval no longer re-executes the tool. As flagged: this narrows the window, it is not exactly-once; a crash between the tool's own commit and the flush still needs tool-side idempotency.
Thanks for the scoping note in #1541, "narrows the window, not exactly-once" is the right way to say it. One addition for the docs part of this issue, in case it helps. I maintain Call4Agents, a Python library for this kind of problem, so I have no stake in VoltAgent.
The tool-side idempotency you mention can use a key that is already there: options.toolContext.callId in a tool's execute is the AI SDK toolCallId (set in createToolExecutionFactory, agent.ts around line 6430, and overwritten from the model's tool call at ~7235). The resent approval message carries the same part, so the same toolCallId comes back on the retry. That is my reading of the code, I have not run a retry against it.
So a tool that moves money or sends something can write callId plus its arguments to its own store before the call and check for it first. If the record exists with no result, the outcome is unknown: look it up at the provider (or ask a person) instead of calling again. This covers the one window #1541 leaves, a crash between the tool's own commit and the flush.
If it is useful, a short "approved tools are at-least-once, key on toolContext.callId" note next to needsApproval in the docs would say this in two lines. Happy to send that as a PR if you want it.