<#1288 make the Agent callback event stream multi-...
# flyte-github
a
#1288 make the Agent callback event stream multi-agent compatible Pull request opened by cosmicBboy This pull request introduces robust tracking and attribution of agent events to individual agent runs, especially in concurrent and nested (sub-agent) scenarios. The changes ensure that each event is stamped with the correct
agent
and
run_id
, so consumers (such as the UI) can reliably distinguish between events from different runs or agents. The UI now ignores progress events from sub-agents to prevent them from interfering with the main agent's display, while still allowing custom/unstamped events to pass through. Comprehensive tests are added to verify these behaviors. Agent run identity and event attribution: • Added a
_RunIdentity
dataclass and a context variable
_current_run
to track the current agent and run ID for each event, ensuring that concurrent and nested agent runs do not conflate their events. The
_run_loop
method sets and resets this context for each run, and
_emit
automatically stamps
agent
and
run_id
onto every
AgentEvent
. (
src/flyte/ai/agents/agent.py
) • Updated the
AgentEvent
class to include
agent
and
run_id
fields, and documented that these are now stamped automatically. (
src/flyte/ai/agents/agent.py
) UI and event filtering: • Modified the chat UI event handler so that only events from the top-level (first) run are processed; events from sub-agents (with a different
run_id
) are dropped to prevent them from interfering with the UI state, while unstamped events still pass through. (
src/flyte/ai/chat/app.py
) Testing: • Added tests to verify that all events are correctly stamped with the agent name and run ID, that sub-agent events are attributed to their own runs without interfering with the outer agent, and that the run identity context is properly restored after sub-agent execution. (
tests/flyte/ai/agents/test_agent.py
) • Added a test to ensure that the chat stream drops sub-agent progress events (with a different
run_id
) but allows unstamped events, and that sub-agent events do not clobber the UI attempt counter or phase. (
tests/flyte/ai/chat/test_app.py
) flyteorg/flyte-sdk