Skip to content

fix(agents): a hook-blocked result_as_answer call must not end the run - #8577

Open
电大老baby (HhH666-ux) wants to merge 1 commit into
crewAIInc:mainfrom
HhH666-ux:fix/agent-executor-blocked-result-as-answer
Open

电大老baby (HhH666-ux) wants to merge 1 commit into
crewAIInc:mainfrom
HhH666-ux:fix/agent-executor-blocked-result-as-answer

Conversation

@HhH666-ux

@HhH666-ux 电大老baby (HhH666-ux) commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Related issue

Fixes #8338

Summary

When a before_tool_call hook blocks a tool that has result_as_answer=True, AgentExecutor returned "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 from result_as_answer (#5697), and the ReAct path does too. AgentExecutor keeps its own copy of that logic and was missing the check: for a blocked call tool_failure is deliberately None, 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 made AgentExecutor the default executor, any function-calling agent using result_as_answer tools with a blocking hook hits this.

The fix surfaces hook_blocked on the per-call execution result and requires 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 until a real answer.

Verification

  • Tests added or updated for the changed behavior
  • Relevant tests and quality checks pass locally

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 blocking PRE_TOOL_CALL hook on a result_as_answer tool — the run must call the LLM a second time and return its answer, not the block notice. Fails on unmodified main, passes with this change.
  • test_result_as_answer_tool_still_short_circuits_when_not_blocked (sync + async): positive control — an unblocked result_as_answer tool still short-circuits with exactly one LLM call.
  • test_blocked_native_call_reports_hook_blocked_flag: pins the new hook_blocked key 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_CALL hook that blocks a tool with result_as_answer=True caused AgentExecutor to treat the hook block message as the agent’s final output and stop, instead of feeding it back to the model for another turn.

AgentExecutor now exposes hook_blocked on native per-call results and requires it to be false in both result_as_answer short-circuit paths (sequential and parallel native tool handling), matching the shared execute_single_native_tool_call behavior. Blocked calls still append the notice as a normal tool message; unblocked result_as_answer tools still short-circuit as before.

Tests cover end-to-end sync/async kickoff, the positive control for unblocked short-circuit, and the new hook_blocked flag 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.

@strix-security

strix-security Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Strix Security Review

Warning

This pull request has 56 commits after the last Strix review (77af89c). Strix has not reviewed these changes.
Automatic review on push is off for this repository. To review the latest changes, tag @strix-security in a comment, or turn on re-review on push.

✅ 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 summary

The change is a narrow control-flow correction in the native tool-calling path: the per-call execution result now carries a hook_blocked flag, and both result_as_answer short-circuit sites require that flag to be false. The flag originates from run_before_tool_call_hooks, which returns a strict boolean, and the added guards mirror the equivalent check already present in the shared native path, so the two execution paths are now consistent. No injection, authorization, cryptographic, path, or secret-handling logic is touched, no security control is removed, and the post-block continuation stays bounded by the executor's iteration limit. No security issues were identified in the changed code.

Review 1 on this PR · 77af89c · requested Oct 25 16:47 UTC when the PR was opened


Reviewed by Strix
Re-run review · Configure security review settings

@HhH666-ux

Copy link
Copy Markdown
Contributor Author

Strix (@strix-security) re-review request on 40fb09c.

The only push since the last review (77af89c) was Merge branch 'main' into fix/agent-executor-blocked-result-as-answer, which brought 18 upstream commits into the branch. The PR's own diff is unchanged — still the single 77af89c commit, 2 files, +126/-0.

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
@HhH666-ux
电大老baby (HhH666-ux) force-pushed the fix/agent-executor-blocked-result-as-answer branch from 40fb09c to 1ceaff3 Compare October 29, 2026 01:23
@HhH666-ux

Copy link
Copy Markdown
Contributor Author

Rebased onto current main; the branch was 38 commits behind and reporting a conflicted merge state. It now merges cleanly.

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 main added two lines earlier in agent_executor.py. The earlier Merge branch 'main' commit was dropped by the rebase, so the diff SHA the review ledger recorded for 40fb09c no longer applies.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Blocked result_as_answer tool becomes the agent's final answer on the native tool path

1 participant