Repository navigation
fix(agents): a hook-blocked result_as_answer call must not end the run - #8577
电大老baby (HhH666-ux) wants to merge 1 commit into
Conversation
Strix Security ReviewWarning This pull request has 56 commits after the last Strix review ( ✅ No open security findings. PR summary: Fixes a bug where a `before_tool_call` hook that blocks a tool marked `result_as_answer` caused the block notice to be returned as the agent's final answer. The executor now reports a `hook_blocked` flag per native tool call and skips the `result_as_answer` short-circuit for blocked calls, so the agent continues and the block notice is passed to the model as ordinary tool context. Review summaryThe change is a narrow control-flow correction in the native tool-calling path: the per-call execution result now carries a Review 1 on this PR · Reviewed by Strix |
|
Strix (@strix-security) re-review request on The only push since the last review ( |
The shared native path (execute_single_native_tool_call) excludes hook-blocked calls from result_as_answer (crewAIInc#5697), and the ReAct path does too. AgentExecutor keeps its own copy of that logic and missed the check: when a before_tool_call hook blocks a tool with result_as_answer=True, the 'Tool execution blocked by hook' notice satisfied the short-circuit condition (tool_failure is None for a block), became the agent's final answer, and the model was never called again. Since crewAIInc#8192 made AgentExecutor the default executor, every function-calling agent hits this. Surface hook_blocked on the per-call execution result and require it to be false in both result_as_answer short-circuit sites, mirroring the shared path. The block notice still reaches the model as ordinary tool context and the agent continues. Fixes crewAIInc#8338
40fb09c to
1ceaff3
Compare
|
Rebased onto current No content change: the diff against the new base is identical to the previous revision — same two files, same +126/−0. Only the hunk offsets moved, because |
Related issue
Fixes #8338
Summary
When a
before_tool_callhook blocks a tool that hasresult_as_answer=True,AgentExecutorreturned "Tool execution blocked by hook. Tool: <name>" as the agent's final answer and never called the model again.The shared native path (
execute_single_native_tool_call) already excludes blocked calls fromresult_as_answer(#5697), and the ReAct path does too.AgentExecutorkeeps its own copy of that logic and was missing the check: for a blocked calltool_failureis deliberatelyNone, so the existing "a failed tool must not become the final answer" guard did not apply and the block notice slipped through the short-circuit. Since #8192 madeAgentExecutorthe default executor, any function-calling agent usingresult_as_answertools with a blocking hook hits this.The fix surfaces
hook_blockedon the per-call execution result and requires it to be false in bothresult_as_answershort-circuit sites, mirroring the shared path. The block notice still reaches the model as ordinary tool context, and the agent continues until a real answer.Verification
Added to
lib/crewai/tests/agents/test_native_tool_hook_contract.py:test_blocked_result_as_answer_tool_does_not_become_final_answer(sync + async): a blockingPRE_TOOL_CALLhook on aresult_as_answertool — the run must call the LLM a second time and return its answer, not the block notice. Fails on unmodifiedmain, passes with this change.test_result_as_answer_tool_still_short_circuits_when_not_blocked(sync + async): positive control — an unblockedresult_as_answertool still short-circuits with exactly one LLM call.test_blocked_native_call_reports_hook_blocked_flag: pins the newhook_blockedkey on the executor's per-call result.Full file: 23 passed with this change; all pre-existing tests unchanged.
Additional context
None
Note
Medium Risk
Changes agent termination logic on the default native tool path when hooks block result_as_answer tools; behavior fix with targeted tests but affects core execution flow.
Overview
Fixes a bug where a
PRE_TOOL_CALLhook that blocks a tool withresult_as_answer=TruecausedAgentExecutorto treat the hook block message as the agent’s final output and stop, instead of feeding it back to the model for another turn.AgentExecutornow exposeshook_blockedon native per-call results and requires it to be false in bothresult_as_answershort-circuit paths (sequential and parallel native tool handling), matching the sharedexecute_single_native_tool_callbehavior. Blocked calls still append the notice as a normal tool message; unblockedresult_as_answertools still short-circuit as before.Tests cover end-to-end sync/async kickoff, the positive control for unblocked short-circuit, and the new
hook_blockedflag on_execute_single_native_tool_call.Reviewed by Cursor Bugbot for commit 1ceaff3. Bugbot is set up for automated code reviews on this repo. Configure here.