Skip to content

fix(agents): keep escalation from action-only after-agent callbacks - #8391

Open
sumedh-aerram wants to merge 5 commits into
google:mainfrom
sumedh-aerram:fix/after-agent-callback-escalation
Open

sumedh-aerram wants to merge 5 commits into
google:mainfrom
sumedh-aerram:fix/after-agent-callback-escalation

Conversation

@sumedh-aerram

@sumedh-aerram sumedh-aerram commented Nov 27, 2026 •

Copy link
Copy Markdown

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 = True loses its
actions, so LoopAgent keeps running. Before-agent callbacks and plugin
agent hooks use the same handler and have the same defect.

Solution:
Flush nested state writes through CallbackContext.actions, then emit an
event whenever the actions differ from default EventActions. Preserve the
no-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 escalate from before_agent_callback
stops a LoopAgent after the current iteration (the agent body still runs once);
returning Content skips the body. This is documented in the LoopAgent and
before_agent_callback docstrings.

Testing Plan

Unit Tests:

  • I have added or updated unit tests for my change.
  • All unit tests pass locally.

tests/unittests/agents/test_agent_callback_events.py runs real Runner
invocations:

  • Before-agent escalation inside a LoopAgent (resumable or not): one
    content-less escalate event authored by the agent, the agent body still
    runs once, then the loop stops. This matches a callback that also writes state.
  • escalate, transfer_to_agent and skip_summarization set by an action-only
    before or after callback produce the same event sequence as a state-writing
    callback, minus state_delta. There is one event per callback run and no
    duplicates.
  • No-op before and after callbacks emit no events for LlmAgent, Sequential,
    Loop and Parallel trees, resumable or not. The callbacks also read nested
    state. 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,
flows and runners: 2,379 passed, 1 skipped and 4 xfailed. Changed-file
pre-commit hooks pass, including compliance checks. mypy reports no issues
for the new test file; base_agent.py keeps its one existing upstream error.

No-op safety: _handle_agent_callbacks builds a new CallbackContext with
fresh EventActions for each call. Framework fields such as agent state, tool
confirmations, auth requests, compaction, rewind and route go on separate events.
They are never written to this context. Of the built-in plugins, only
SaveFilesAsArtifactsPlugin writes here, and it also writes state, so its
event 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 script
and the Runner scenario tests above use stub agents/models, so they are not a
live 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

  • I have read the CONTRIBUTING.md document.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.
  • I have manually tested my changes end-to-end.
  • Any dependent changes have been merged and published in downstream modules.

Additional context

No public signatures or dependencies change. This restores callback action
propagation; no new configuration is needed.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Action-only after-agent callbacks silently lose escalation

1 participant