Skip to content

feat(plugins): add on_agent_error_callback and on_run_error_callback#5483

Draft
caohy1988 wants to merge 3 commits intogoogle:mainfrom
caohy1988:feat/merge-error-callbacks
Draft

feat(plugins): add on_agent_error_callback and on_run_error_callback#5483
caohy1988 wants to merge 3 commits intogoogle:mainfrom
caohy1988:feat/merge-error-callbacks

Conversation

@caohy1988
Copy link
Copy Markdown

Summary

Merges #5045 and #5047 onto latest main with all conflicts resolved. Implements RFC #5044. Fixes #4863.

What this adds

Two new notification-only plugin callbacks that complete the error coverage gap:

Level before after error
Run new
Agent new
Model
Tool

Changes by file

File Change
base_plugin.py on_agent_error_callback and on_run_error_callback abstract methods
plugin_manager.py run_on_agent_error_callback, run_on_run_error_callback, and _run_notification_callbacks (best-effort dispatch: logs and continues on plugin errors)
base_agent.py try/except around _run_async_impl and _run_live_impl; new _handle_agent_error_callback method
runners.py try/except around execute_fn() in _exec_with_plugin; after_run_callback now success-only
bigquery_agent_analytics_plugin.py on_agent_error_callback (AGENT_ERROR + span cleanup), on_run_error_callback (INVOCATION_ERROR + TraceManager cleanup + flush); AGENT_ERROR and INVOCATION_ERROR views with error_traceback column
test_error_callbacks.py 17 tests: agent error fires, run error fires, success paths unaffected, both-layers-once, plugin failure resilience

Design decisions

  • after_* callbacks remain success-only — no semantic change to existing callbacks
  • Exception only, not BaseExceptionCancelledError, KeyboardInterrupt, SystemExit do not trigger error callbacks
  • Best-effort dispatch (_run_notification_callbacks) — if a plugin raises during error notification, it's logged and iteration continues. The original application exception is always re-raised
  • BQAA on_run_error_callback has try/finally — TraceManager cleanup, context var reset, and flush run even if _log_event raises

Test plan

  • 230 tests pass (213 BQAA + 17 error callbacks), 0 regressions
  • Agent error callback fires on _run_async_impl crash
  • Agent error callback fires on _run_live_impl crash
  • Run error callback fires on execution crash
  • after_agent_callback / after_run_callback NOT called on crash
  • Both callbacks fire exactly once when agent crash propagates to runner
  • Plugin failure during error callback does not mask application exception
  • BQAA logs AGENT_ERROR / INVOCATION_ERROR with traceback

🤖 Generated with Claude Code

Merges PRs google#5045 and google#5047 onto latest main, resolving conflicts.

Implements RFC google#5044 to close the error callback coverage gap:

- Add on_agent_error_callback and on_run_error_callback to BasePlugin
- Wire try/except in BaseAgent.run_async(), run_live(), and
  Runner._exec_with_plugin()
- Add _run_notification_callbacks to PluginManager (best-effort:
  logs and continues on plugin errors, never masks app exceptions)
- Add AGENT_ERROR and INVOCATION_ERROR event types to BQAA plugin
  with traceback capture and TraceManager cleanup
- Add v_agent_error and v_invocation_error views with
  error_traceback column
- Keep after_agent_callback and after_run_callback as success-only
- Catch Exception, not BaseException

Fixes google#4863, implements google#5044

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@google-cla
Copy link
Copy Markdown

google-cla Bot commented Apr 25, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@adk-bot
Copy link
Copy Markdown
Collaborator

adk-bot commented Apr 25, 2026

Response from ADK Triaging Agent

Hello @caohy1988, thank you for your contribution!

It looks like the Contributor License Agreement (CLA) has not been signed yet. Before we can review this PR, please sign the CLA. You can find more information and sign it at https://cla.developers.google.com/.

Thanks!

@adk-bot adk-bot added the core [Component] This issue is related to the core interface and implementation label Apr 25, 2026
- Move try/except in _exec_with_plugin to cover the full run lifecycle
  (before_run_callback, early-exit, execution loop). Previously only
  the execution loop was covered, so failures in before_run_callback
  escaped without run-error notification or BQAA cleanup.

- Fix traceback truncation: use max_len > 0 instead of bare if max_len,
  so -1 (no truncation sentinel) is not misinterpreted as enabled.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@caohy1988 caohy1988 marked this pull request as draft April 25, 2026 07:27
Closes the coverage gap for the error callback BQAA integration:

- test_on_agent_error_callback_logs_correctly: AGENT_ERROR event with
  error_message, status=ERROR, and traceback in content
- test_on_run_error_callback_logs_correctly: INVOCATION_ERROR event
  with same fields
- test_on_run_error_callback_cleanup_runs_on_log_failure: TraceManager
  cleanup and context var reset run even when _log_event raises
- test_traceback_not_truncated_with_negative_max_len: max_content_length=-1
  does not truncate (validates the max_len > 0 fix)
- test_error_views_contain_traceback_column: view SQL includes
  error_traceback for both AGENT_ERROR and INVOCATION_ERROR views

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core [Component] This issue is related to the core interface and implementation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement on_agent_error_callback and support AGENT_ERROR event in BigQuery Analytics Plugin

2 participants