fix: #753 Emit agent_tool_end event when function tools throw errors #754
+59
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix: Emit
agent_tool_endEvent When Function Tools Throw ErrorsSummary
Fixes inconsistent event emission for function tools by ensuring
agent_tool_endis always emitted, even when tool execution throws an error. This brings function tools in line with shell, computer, and apply patch tools, which already emit end events on errors.Problem
When a function tool throws an error, the
agent_tool_endevent was never emitted, breaking the expected event lifecycle where everyagent_tool_startshould be paired with anagent_tool_end. This inconsistency created problems for:Changes
Code Change
File:
packages/agents-core/src/runImplementation.ts:1361-1370Added
agent_tool_endemission in the error catch block before re-throwing:Test Added
File:
packages/agents-core/test/runImplementation.test.ts:1774-1819Added test case
emits agent_tool_end even when function tool throws errorthat:agent_tool_startandagent_tool_endare emittedWhy This is Correct
Testing
New Test
emits agent_tool_end even when function tool throws error- PassesRegression Testing
runImplementation.test.ts- Passagents-corepackage - PassTest Output
Comparison: Before and After
Before (Broken)
After (Fixed)
Event Data Format
When a tool throws an error, the
agent_tool_endevent is emitted with:resultparameter: String representation of the error (e.g., "Error: Tool execution failed")detailsparameter:{ toolCall: protocol.FunctionCallItem }(same as success case)Listeners can distinguish error cases by checking if the result string contains error information or by catching the error that is re-thrown after the event emission.
Backward Compatibility
✅ Fully backward compatible
Impact
Systems that rely on event lifecycle consistency (event sourcing, monitoring, debugging tools) will now receive complete event streams for function tools, matching the behavior of other tool types.
Closes #753