Skip to content

Nested LoopAgents will escalate all LoopAgents #2808

Description

@jhonratiu

A nested LoopAgent will escalate and break all LoopAgents

SequentialAgent(
    sub_agents=[
        LoopAgent(
            sub_agents=[
                LoopAgent(
                    sub_agents=[
                        section_evaluator,
                        EscalationChecker(name="IBreakAfter5Loops"),
                    ]
                ),
                EscalationChecker(name="IAlsoBreakAfter5Loops"),
            ],
        ),
    ],
)

Expecting to loop 25 times, but the inner loop will reach five and the outer loop will exit after running once.

  @override
  async def _run_async_impl(
      self, ctx: InvocationContext
  ) -> AsyncGenerator[Event, None]:
    times_looped = 0
    while not self.max_iterations or times_looped < self.max_iterations:
      for sub_agent in self.sub_agents:
        should_exit = False
        async for event in sub_agent.run_async(ctx):
          yield event
          if event.actions.escalate:
            should_exit = True

        if should_exit:
          return

      times_looped += 1
    return

In the LoopAgent _run_async_impl method every event appears to be passed up first, triggering an escalate on every loop agent that is part of the invocation. Something like this would stop the event from propagating further upwards but may impact the reporting.

async for event in sub_agent.run_async(ctx):
  if event.actions.escalate:
    should_exit = True
    event.actions.escalate = False
  yield event

Desktop:

  • ADK version: 1.10.0

Metadata

Metadata

Assignees

Labels

core[Component] This issue is related to the core interface and implementationneeds review[Status] The PR/issue is awaiting review from the maintainer

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions