Skip to content

Action-only after-agent callbacks silently lose escalation #8390

In review

Description

@sumedh-aerram

An after_agent_callback that sets callback_context.actions.escalate = True
and returns None does not emit an escalation event. A LoopAgent runs its
child to max_iterations instead of stopping. Adding an unrelated state write
to the callback makes escalation work. Plugin after_agent_callback hooks
have the same failure through the shared callback handler.

Environment: ADK main (2.15.x), Python 3.11.

Run this offline reproduction without model credentials or network access:

"""Offline: action-only after-agent callbacks should stop their LoopAgent."""
import asyncio
import warnings

from google.adk.agents.base_agent import BaseAgent
from google.adk.agents.invocation_context import InvocationContext
from google.adk.agents.loop_agent import LoopAgent
from google.adk.events.event import Event
from google.adk.sessions.in_memory_session_service import InMemorySessionService


class Worker(BaseAgent):
    async def _run_async_impl(self, ctx):
        yield Event(author=self.name, invocation_id=ctx.invocation_id)


async def run(write_state):
    def stop(callback_context):
        callback_context.actions.escalate = True
        if write_state:
            callback_context.state["unrelated"] = True

    worker = Worker(name="worker", after_agent_callback=stop)
    with warnings.catch_warnings():
        warnings.simplefilter("ignore", DeprecationWarning)
        loop = LoopAgent(name="loop", max_iterations=3, sub_agents=[worker])
    service = InMemorySessionService()
    session = await service.create_session(app_name="app", user_id="user")
    ctx = InvocationContext(session_service=service, session=session,
        invocation_id="invocation", agent=loop)
    events = [event async for event in loop.run_async(ctx)]
    return sum(event.actions.escalate is True for event in events), len(events)


async def main():
    print("action only (escalation events, total events):", await run(False))
    print("same callback plus unrelated state write:", await run(True))
    assert await run(False) == (1, 2)
    assert await run(True) == (1, 2)


asyncio.run(main())

Expected: both calls print (1, 2) (one escalation, two total events).
Actual: the action-only call prints (0, 3); the state-writing control prints
(1, 2). The action-only assertion fails.

Activity

  1. added
    core
    on Nov 30, 2026
  2. surajksharma07 commented on Dec 2, 2026

    @surajksharma07
    Collaborator

    Thanks @sumedh-aerram, your script reproduces this on main (b1566b8 / 2.15.1). I get (0, 3) for the action-only callback. I also checked 1.34.1 and 1.19.0 from PyPI and got the same result, so this isn't a regression. The check in _handle_agent_callbacks has only ever looked at state (and artifacts since 2.13).

    I tried a workaround locally. It emits the event whenever callback_context.actions != EventActions() and calls has_delta() first so nested state writes still get flushed. Escalation then stops the loop and no-op callbacks still emit nothing. That seems to match #8391.

    Could you check that it holds on your end? That includes the before_agent_callback path, which now emits events for action-only callbacks too. It would also help to confirm that no framework-set action fields make no-op callbacks start emitting events.

    Once that's confirmed I'll ask the core owners to take a look at #8391. Until then, writing any state key in the callback is a safe workaround on released versions.

  3. sumedh-aerram commented on Dec 2, 2026

    @sumedh-aerram
    Author

    Thanks @surajksharma07. I confirmed this on #8391 merged with current main (now at f374364, which adds the docstring notes):

    • The workaround matches fix(agents): keep escalation from action-only after-agent callbacks #8391. The handler reads callback_context.actions first, which flushes nested state writes, then emits an event when the actions differ from EventActions().
    • Before-agent path: an action-only escalate inside a LoopAgent emits one content-less event authored by the agent. The agent body still runs once, then the loop stops. That is the same as a callback that escalates and writes state on main today. To skip the body, return Content from before_agent_callback. The PR now documents this in the LoopAgent and before_agent_callback docstrings. transfer_to_agent and skip_summarization produce the same event sequence as a callback that also writes state, minus state_delta, with no duplicates.
    • No-op safety: each handler call builds a new CallbackContext with fresh EventActions. Framework fields (agent state, tool confirmations, auth requests, compaction, rewind, route) go on separate events. Runner tests show no-op before and after callbacks emit nothing for LlmAgent, Sequential, Loop and Parallel agents, resumable or not, including runs with tool confirmations and agent-state checkpoints.
    • The new tests are in tests/unittests/agents/test_agent_callback_events.py. 8 of 26 fail on upstream base_agent.py, all of them action-only cases, and all 26 pass with the PR. The agents, flows and runners suites pass (2,379 passed).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

core

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions