Repository navigation
fix(agents): keep escalation from action-only after-agent callbacks - #8391
Open
sumedh-aerram wants to merge 5 commits into
Open
sumedh-aerram wants to merge 5 commits into
sumedh-aerram wants to merge 5 commits into
Conversation
Emit callback events for non-default actions after flushing nested state writes. This preserves escalation from agent and plugin callbacks without requiring unrelated state changes or returned content. Fixes google#8390
Check that action-only before/after agent callbacks emit the same events a state-writing callback already emitted (escalate in a LoopAgent, transfer_to_agent, skip_summarization), and that no-op callbacks emit nothing for LlmAgent, Sequential, Loop and Parallel agents, resumable or not, alongside framework-set tool confirmation and agent state actions.
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.
Please ensure you have read the contribution guide before creating a pull request.
Link to Issue or Description of Change
Fixes #8390
Problem:
An after-agent callback that only sets
actions.escalate = Trueloses itsactions, so
LoopAgentkeeps running. Before-agent callbacks and pluginagent hooks use the same handler and have the same defect.
Solution:
Flush nested state writes through
CallbackContext.actions, then emit anevent whenever the actions differ from default
EventActions. Preserve theno-op path, callback ordering, and before-callback content short-circuiting.
The content-returning path continues carrying the callback actions.
Before-agent semantics: an action-only
escalatefrombefore_agent_callbackstops a
LoopAgentafter the current iteration (the agent body still runs once);returning
Contentskips the body. This is documented in theLoopAgentandbefore_agent_callbackdocstrings.Testing Plan
Unit Tests:
tests/unittests/agents/test_agent_callback_events.pyruns realRunnerinvocations:
LoopAgent(resumable or not): onecontent-less
escalateevent authored by the agent, the agent body stillruns once, then the loop stops. This matches a callback that also writes state.
escalate,transfer_to_agentandskip_summarizationset by an action-onlybefore or after callback produce the same event sequence as a state-writing
callback, minus
state_delta. There is one event per callback run and noduplicates.
LlmAgent,Sequential,LoopandParalleltrees, resumable or not. The callbacks also read nestedstate. The runs include tool calls, tool confirmation requests and agent-state
checkpoints, and each event sequence matches a run without callbacks.
Results with upstream
base_agent.py: 8 of 26 fail, all action-only cases;the no-op matrix passes. With this PR: 26 passed. Across
tests/unittests/agents,flowsandrunners: 2,379 passed, 1 skipped and 4 xfailed. Changed-filepre-commit hooks pass, including compliance checks. mypy reports no issues
for the new test file;
base_agent.pykeeps its one existing upstream error.No-op safety:
_handle_agent_callbacksbuilds a newCallbackContextwithfresh
EventActionsfor each call. Framework fields such as agent state, toolconfirmations, auth requests, compaction, rewind and route go on separate events.
They are never written to this context. Of the built-in plugins, only
SaveFilesAsArtifactsPluginwrites here, and it also writes state, so itsevent was already emitted.
Manual End-to-End (E2E) Tests:
Ran the offline reproduction from the linked issue. Both action-only and
state-writing callbacks now print
(1, 2)and pass the assertions. That scriptand the
Runnerscenario tests above use stub agents/models, so they are not alive end-to-end run. No real-model E2E or multi-version tox matrix was run,
which is why the manual E2E checkbox below is left unchecked.
Checklist
Additional context
No public signatures or dependencies change. This restores callback action
propagation; no new configuration is needed.