Skip to content

fix(core): invoke plugin beforeAgentCallback and afterAgentCallback - #1137

Open
reginaldalfret wants to merge 1 commit into
google:mainfrom
reginaldalfret:fix/plugin-agent-callbacks
Open

reginaldalfret wants to merge 1 commit into
google:mainfrom
reginaldalfret:fix/plugin-agent-callbacks

Conversation

@reginaldalfret

Copy link
Copy Markdown

Summary

Fixes #1131.

In BaseAgent.handleBeforeAgentCallback and BaseAgent.handleAfterAgentCallback, execution previously only checked his.canonicalBeforeAgentCallbacks / his.canonicalAfterAgentCallbacks. If an agent had no instance callbacks configured, it would return undefined immediately without invoking registered plugins. Furthermore, even when instance callbacks were present, invocationContext.pluginManager.runBeforeAgentCallback and
unAfterAgentCallback were never invoked.

This PR connects BaseAgent callback handling to PluginManager:

  1. In handleBeforeAgentCallback, invokes invocationContext.pluginManager.runBeforeAgentCallback({agent: this, callbackContext}). If a plugin returns Content, execution short-circuits (invocationContext.endInvocation = true) and emits the returned content. If no override is provided, canonical instance callbacks run.
  2. In handleAfterAgentCallback, invokes invocationContext.pluginManager.runAfterAgentCallback({agent: this, callbackContext}). If a plugin returns Content, it yields the replacement content event. If no override is provided, canonical instance callbacks run.
  3. If either plugin callback mutates state, state delta events are properly emitted.

Verification

pm run build completed successfully across all workspaces.

pm run ts:check completed with 0 errors.

pm run format:check confirmed Prettier code style passes.

pm run lint completed with 0 errors.

  • Unit tests in core/test/agents/base_agent_test.ts (39/39 passing) and all plugin tests in core/test/plugins/ (267/267 passing).

Fixes google#1131

BaseAgent.handleBeforeAgentCallback and BaseAgent.handleAfterAgentCallback
previously only checked for canonical callbacks on the agent instance.
As a result, if an agent had no instance callbacks configured, it would
return undefined immediately without invoking the registered plugins.
Furthermore, even with instance callbacks present, PluginManager's
runBeforeAgentCallback and runAfterAgentCallback were never invoked.

This change runs invocationContext.pluginManager.runBeforeAgentCallback
and runAfterAgentCallback in handleBeforeAgentCallback and
handleAfterAgentCallback, allowing plugins to inspect, short-circuit,
or alter agent execution as specified in BasePlugin.

Signed-off-by: reginaldalfret <reginaldalfret@gmail.com>
@hirematha

Copy link
Copy Markdown

Hi @reginaldalfret, Thanks for your pull request!
Removing the early return completely causes new Context({invocationContext}) to run even when no plugins or agent callbacks are configured, which breaks 7 tests in core/test/agents/routed_agent_test.ts (TypeError: Cannot read properties of undefined (reading 'state')).
Could we keep the early return when neither plugins (!invocationContext.pluginManager?.hasPlugins) nor canonical agent callbacks are present?

@hirematha hirematha added the request clarification [Status] The maintainer need clarification or more information from the author label Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

request clarification [Status] The maintainer need clarification or more information from the author

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(core): plugin beforeAgentCallback / afterAgentCallback are never invoked

2 participants