[BUG] Filtered @before_*/@after_* hook decorators cannot be unregistered via unregister_*_hook

オープン
#9,682 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@LowmistLMT がすでに取り組んでいます。

2026年11月23日 から。

  • #9683 @LowmistLMT による — オープン

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
30/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python
領域
ai

調査の方向性

Start in lib/crewai/src/crewai/hooks/decorators.py at _create_hook_decorator, where the filtered_hook wrapper is registered but never linked back to the returned function. Compare with on and unregister in lib/crewai/src/crewai/hooks/dispatch.py, then check the four unregister_*_hook helpers in tool_hooks.py and llm_hooks.py. Done means the unregister calls return True and the registry is empty after the reproduction snippet. A linked pull request (#9683) is already open, so check it before starting.

索引モデルが issue の本文から書いたものです。

説明

bug
Description

When registering a global hook using any of the filtered hook decorators (@before_tool_call(tools=...), @after_tool_call(tools=...), @before_llm_call(agents=...), or @after_llm_call(agents=...)), _create_hook_decorator (lib/crewai/src/crewai/hooks/decorators.py:54-75) registers the internal filtered_hook wrapper while returning the original function f.

Unlike @on(...) in lib/crewai/src/crewai/hooks/dispatch.py:406 (which stores func._registered_hook = hook and resolves it in dispatch.unregister), _create_hook_decorator does not attach _registered_hook, and all four unregister_*_hook helpers in tool_hooks.py and llm_hooks.py call .remove(hook) directly. As a result, calling unregister_before_tool_call_hook(fn) (or the other three unregister functions) always returns False and leaks the hook in the global registry.

Steps to Reproduce
  1. Decorate a top-level function with @before_tool_call(tools=["delete_file"]) (or @after_tool_call, @before_llm_call(agents=...), @after_llm_call(agents=...)).
  2. Call unregister_before_tool_call_hook(my_hook) (or the matching unregister_*_hook helper).
  3. Observe that it returns False and get_before_tool_call_hooks() still contains the wrapper hook.
Expected behavior

Calling unregister_before_tool_call_hook, unregister_after_tool_call_hook, unregister_before_llm_call_hook, or unregister_after_llm_call_hook with a function decorated by the corresponding filtered @before_*/@after_* decorator should remove the registered wrapper and return True, matching dispatch.unregister behavior for @on(...).

Screenshots/Code snippets

from crewai.hooks.decorators import before_tool_call
from crewai.hooks.tool_hooks import (
clear_all_tool_call_hooks,
get_before_tool_call_hooks,
unregister_before_tool_call_hook,
)

clear_all_tool_call_hooks()

@before_tool_call(tools=["delete_file"])
def guard_delete(context):
return False

removed = unregister_before_tool_call_hook(guard_delete)
print("removed:", removed, "remaining:", len(get_before_tool_call_hooks()))

Actual: removed: False remaining: 1

Expected: removed: True remaining: 0

Operating System

macOS Sonoma

Python Version

3.12

crewAI Version

1.15.44 (main 6ecb87390)

crewAI Tools Version

N/A

Virtual Environment

Venv

Evidence
  • lib/crewai/src/crewai/hooks/decorators.py:54-75: registers filtered_hook and returns f without storing f._registered_hook = filtered_hook.
  • lib/crewai/src/crewai/hooks/tool_hooks.py:298-347 and lib/crewai/src/crewai/hooks/llm_hooks.py:313-362: unregister_*_hook calls .remove(hook) directly instead of delegating to crewai.hooks.dispatch.unregister.
  • Running the snippet above prints removed: False remaining: 1.
Possible Solution

Store f._registered_hook = filtered_hook in _create_hook_decorator when registering a filtered global hook (mirroring dispatch.on), and delegate unregister_before_tool_call_hook, unregister_after_tool_call_hook, unregister_before_llm_call_hook, and unregister_after_llm_call_hook to crewai.hooks.dispatch.unregister.

Additional context

I have a minimal fix with regression tests ready and will open a linked PR. AI assistance was used in preparing this report (llm-generated).

主要言語
Python
スター
61.2k
フォーク
9.2k
平均マージ
1日 14時間
マージ済み PR(30日)
180

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

crewAIInc/crewAI のほかの issue

crewAIInc/crewAI の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。