[Bug]:AG-UI TEXT_MESSAGE events interleave when multiple agent_spawn run concurrently with parallel=true
环境
-
AgentScope Java 2.0.0
-
DashScope (qwen-flash)
-
Spring Boot 4.1.0, Java 21, macOS
-
Toolkit config: ToolkitConfig.builder().parallel(true).build()
-
复现步骤 在父 Agent 的 toolkit 中设置 parallel=true 父 Agent 在同一轮中并发调用多个 agent_spawn(timeout_seconds=600),每个 spawn 启动一个 sub-agent Sub-agent 内部调用工具(如 load_images),产生 TEXT_MESSAGE_START → TEXT_MESSAGE_CONTENT → TEXT_MESSAGE_END 事件流 多个 sub-agent 并发执行 实际行为
AG-UI SSE 流中报错:
Cannot send 'TEXT_MESSAGE_CONTENT' event: No active text message found with ID 'cadcfded-833f-9dfe-9521-6b4076c9bff1'. Start a text message with 'TEXT_MESSAGE_START' first.
期望行为
多个 sub-agent 的 TEXT_MESSAGE 事件应各自独立、不交错,AG-UI 适配器能正确处理。
根因推测
AgentSpawnTool.execLocalSync() 的 Path 1(streamEvents 路径)中,所有并发 sub-agent 共享同一个 AgentEventEmitter:
Optional emitterOpt = AgentEventEmitter.fromContext(ctxView); AgentEventEmitter parentEmitter = emitterOpt.get();
不同 sub-agent 的 TEXT_MESSAGE_START(id=A) / CONTENT(id=A) 与 START(id=B) / CONTENT(id=B) 在 parentEmitter 中交错。下游 AG-UI 适配器按单线程模型处理事件流,看到交错的 message ID 就报错。
事件虽然带 source tag(如 main/agent-A),但 AG-UI 适配器未按 source 分组。
临时绕过
回退 parallel=false,但所有 spawn 串行执行。
#2559 已经处理了默认配置下的情况:带有 source 的子 Agent 事件现在会转换成 subagent.* 自定义事件,不再和父 Agent 的 TEXT_MESSAGE_* 事件混在同一条 native 文本流里。
你好,我是一名 Java 开发者,想参与修复这个问题。
初步检查当前 main 后,我注意到 AgentSpawnTool.execLocalSync() 仍会将并发子 Agent 的事件转发到同一个 parent AgentEventEmitter,目前主要通过 sourcePath 标记来源。
我计划先补充一个不依赖真实模型 API 的并发回归测试,稳定复现两个子 Agent 的 TEXT_MESSAGE_START / CONTENT / END 事件交错;然后根据现有 AG-UI 状态管理方式,评估按 source 隔离消息状态或在转发边界进行有序处理。
请问该 issue 目前是否可以认领?以上测试与修复范围是否符合项目预期?
Issue Evaluation
Category: bug | Status: Mostly fixed on main — one residual gap confirmed
Verified against main@3c1c29c01 (read-only code inspection).
What is already fixed. The reported failure mode no longer reproduces through the default AG-UI path. Source-tagged sub-agent events are diverted before they can mix into the parent native text stream:
AgentEventConverterRegistry.java:100-106— ifemitSubagentEventsAsNativeisfalseandevent.getSource()is non-blank, the event goes throughSubagentEventConverterinstead of the native converters.AguiAdapterConfig.java:229—emitSubagentEventsAsNativedefaults tofalse, so the default configuration is safe.SubagentEventConverter.java:39-45,58-92— childAgentStart/End,TextBlockDelta,ToolCall*becomeCUSTOMevents (subagent.lifecycle|text|thinking|tool_call|...) that keepsourcein the payload.AguiStreamContext.java:50-51,141-146— text state is tracked per message id (startedTextMessages/endedTextMessages) and an unseen id is auto-started on delta, so interleaved ids cannot raise a "no active text message" error. The exact error string in the report is not present anywhere onmain.
This matches the note that #2559 (merged 3fcccafa2) handled the default-config case. If you still see the error, please check whether emitSubagentEventsAsNative(true) is enabled, or whether the consumer is an AG-UI client-side reducer rather than this adapter — that client-side reducer is outside this repo.
Residual gap (real, but narrower than reported). AgentSpawnTool.java:779-787 still forwards child events into the single shared parent emitter obtained via AgentEventEmitter.fromContext(ctxView), adding only a source tag. AgentSpawnTool.java:1171-1177 builds sourcePath = parentSessionId + "/" + spawned.agentId, so two concurrent agent_spawn calls for the same agentId produce an identical source. Downstream, subagent.* custom events from the two runs are therefore indistinguishable and will interleave for consumers that group by source. Impact: mis-grouped sub-agent output in concurrent fan-out UIs; severity medium; no exception thrown. Test coverage today only exercises a single child (HarnessAgentSubagentStreamTest.java:310) — there is no two-concurrent-spawn test.
Suggested scope for a fix (if anyone picks this up): make the spawn source unique per call (e.g. append the tool-call / invocation id) in AgentSpawnTool, and add a regression test with two concurrent same-agentId spawns asserting separable subagent.* streams.
@bubb1eP the issue is still open with the residual scope above — it is unclaimed, so please go ahead if you want to work on it, ideally confirming the narrowed scope with a maintainer first.
No PR is raised from this evaluation: the primary symptom is already fixed on main, and the remaining change touches event identity semantics that affect every subagent.* consumer, which needs maintainer input before implementation.
Automated evaluation by github-manager-bot