feat(plugins): add on_agent_error_callback and on_run_error_callback lifecycle hooks#4974
feat(plugins): add on_agent_error_callback and on_run_error_callback lifecycle hooks#4974STHITAPRAJNAS wants to merge 2 commits intogoogle:mainfrom
Conversation
|
Hi @STHITAPRAJNAS , Thank you for your contribution! We appreciate you taking the time to submit this pull request. |
|
Hi @Jacksunwei , can you please review this. |
|
Formatting done |
358f9ed to
cd4ac4e
Compare
|
Hi @Jacksunwei — gentle ping! This PR has been ready for review since Mar 25 with all checks passing. Would appreciate your feedback when you get a chance. Thanks! |
…lifecycle hooks Fixes google#4774 When an unhandled exception propagates out of an agent's _run_async_impl / _run_live_impl, or out of the runner's execution loop, the existing after_agent_callback / after_run_callback were silently skipped. This made fatal failures invisible to observability plugins (e.g. BigQuery analytics), inflating success rates and losing failure events entirely. Changes: - BasePlugin: add on_agent_error_callback(agent, callback_context, error) and on_run_error_callback(invocation_context, error) with safe no-op defaults. - PluginManager: add run_on_agent_error_callback / run_on_run_error_callback dispatch methods backed by a new _run_error_callbacks helper that fans out to ALL plugins (no early-exit) and logs — but does not propagate — individual plugin failures. - base_agent.py: wrap run_async / run_live generator loops in try/except; call run_on_agent_error_callback before re-raising. - runners.py: wrap the execute_fn generator loop in try/except; call run_on_run_error_callback before re-raising. after_run_callback is intentionally skipped on the error path so plugins can distinguish clean completions from fatal failures. Tests (30 new, all passing): - tests/unittests/plugins/test_lifecycle_error_callbacks.py - tests/unittests/runners/test_runner_error_callbacks.py - tests/unittests/agents/test_agent_error_callbacks.py
cd4ac4e to
5b92cb8
Compare
|
Hi @Jacksunwei — following up on this. The |
Summary
Fixes #4774
When an unhandled exception propagates out of an agent's
_run_async_impl/_run_live_impl, or out of the runner's execution loop, the existingafter_agent_callback/after_run_callbackwere silently skipped. This made fatal failures invisible to observability plugins (e.g. BigQuery analytics), inflating success rates and losing failure events entirely.Changes
BasePlugin: addon_agent_error_callback(agent, callback_context, error)andon_run_error_callback(invocation_context, error)with safe no-op defaults.PluginManager: addrun_on_agent_error_callback/run_on_run_error_callbackdispatch methods backed by a new_run_error_callbackshelper that fans out to all plugins (no early-exit) and logs — but does not propagate — individual plugin failures.base_agent.py: wraprun_async/run_livegenerator loops intry/except; callrun_on_agent_error_callbackbefore re-raising.runners.py: wrap theexecute_fngenerator loop intry/except; callrun_on_run_error_callbackbefore re-raising.after_run_callbackis intentionally skipped on the error path so plugins can distinguish clean completions from fatal failures.Design decisions
after_run_callbackis not called on erroron_model_error_callbackvsafter_model_callbackworkTesting plan
tests/unittests/plugins/test_lifecycle_error_callbacks.py— 12 tests coveringBasePlugindefaults,PluginManagerfan-out semantics, argument forwarding, and no-early-exit behaviourtests/unittests/runners/test_runner_error_callbacks.py— 7 integration tests covering runner error/success paths with realInMemoryRunnertests/unittests/agents/test_agent_error_callbacks.py— 11 tests coveringrun_asyncandrun_liveerror paths