[BUG] A tool's own `hooks.onEnd` is called twice when the tool throws
Versions: @voltagent/core 2.11.1 (same code on main), ai 6.0.326.
Describe the bug
When execute throws, the tool-level onEnd hook (createTool({ hooks: { onEnd } })) runs twice with the same error. The agent-level onToolError and onToolEnd run once. Anything that counts failures, writes audit records, or releases resources in onEnd does it twice.
To reproduce
const calls = { toolOnEnd: 0, onToolError: 0, onToolEnd: 0 }
const agent = new Agent({ name: 'a', instructions: 'x', model, // mock: calls `charge` once, then answers
hooks: { onToolError: () => void calls.onToolError++, onToolEnd: () => void calls.onToolEnd++ },
tools: [createTool({ name: 'charge', description: 'charge', parameters: z.object({}),
hooks: { onEnd: () => void calls.toolOnEnd++ },
execute: async () => { throw new Error('card declined') } })] })
await agent.generateText('charge it')
console.log(calls) // { toolOnEnd: 2, onToolError: 1, onToolEnd: 1 }
Expected: toolOnEnd: 1.
Cause: handleToolError in createToolExecutionFactory (packages/core/src/agent/agent.ts, around L6570–6584 on main) has the await tool.hooks?.onEnd?.({ … error: voltAgentError … }) block twice in a row.
Suggested fix: remove the second call, and add a test that asserts onEnd is called once for a failing tool. The success path (resolveToolEndOutput) already calls it once.
Found while testing VoltAgent with Shakedown (a fault-injection test harness for agent tool calls).
I'd like to work on this issue.