voltagent icon indicating copy to clipboard operation
voltagent copied to clipboard

[BUG] A tool's own `hooks.onEnd` is called twice when the tool throws

Open zennicalles opened this issue 2 weeks ago • 1 comments

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).

zennicalles avatar Nov 14 '26 01:11 zennicalles

I'd like to work on this issue.

jeffierw avatar Nov 14 '26 02:11 jeffierw