feat(plugins): add on_agent_error_callback and on_run_error_callback lifecycle hooks#4974
Open
STHITAPRAJNAS wants to merge 1 commit intogoogle:mainfrom
Open
Conversation
…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
Collaborator
|
Hi @STHITAPRAJNAS , Thank you for your contribution! We appreciate you taking the time to submit this pull request. |
Collaborator
|
Hi @Jacksunwei , can you please review this. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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