Skip to content

[Bug]: With ToolEmitter, the event stream / AG-UI TOOL_CALL_RESULT reports emitted chunks instead of the tool's return value #3312

Closed

Description

@xuchaoguo1983

AgentScope-Java Version

2.0.3 (also present on main @ 857e3e6)

Description

ToolEmitter documents that "The emitted chunks do NOT affect what the LLM receives - only the tool method's return value is sent to the LLM." The event stream, however, treats emitted chunks as the streamed form of the result: in ReActAgent, once a tool has emitted chunks its id is added to chunkedToolIds, and emitToolResultDelta then skips emitting the tool's actual return value. ToolResultEndEvent does not carry the result either.

As a result, for a tool that emits progress chunks, the event stream (and therefore AG-UI TOOL_CALL_RESULT) reports the concatenated chunk text, while the model receives the return value. The two histories diverge:

  • AG-UI clients insert TOOL_CALL_RESULT into their message history as the tool message. A client that resends full history without server-side memory feeds the chunk text back to the model as the tool result — contradicting the ToolEmitter contract, and leaking anything that was emitted only for the UI (e.g. a one-time code shown to the user).
  • A client that relies on TOOL_CALL_RESULT to show the operation's outcome sees progress text instead of the actual result.

Reproduction (verified on 2.0.4-SNAPSHOT built from main, AG-UI server, scripted model)

@Tool(name = "enroll")
public String enroll(ToolEmitter emitter) {
    emitter.emit(ToolResultBlock.text("press once"));
    emitter.emit(ToolResultBlock.text("press again"));
    emitter.emit(ToolResultBlock.text("done"));
    return "fingerprint enrolled";
}
  • Model request: the tool result is fingerprint enrolled.
  • AG-UI TOOL_CALL_RESULT.content: press oncepress againdone (the return value never appears in the event stream).

Expected

The final tool result in the event stream (ToolResultTextDeltaEvent/ToolResultDataDeltaEvent before ToolResultEndEvent, and thus TOOL_CALL_RESULT) is the tool's return value — what the model receives — regardless of whether progress chunks were emitted. Progress chunks could remain distinguishable (e.g. by their metadata) for UIs that want to render them.

Activity

  1. oss-maintainer commented on Sep 26, 2026

    @oss-maintainer
    Collaborator

    Issue Evaluation (automated verification against main @ 857e3e6)

    Category: bug | Status: Confirmed | Severity: high (contract violation in a public API; silent data divergence between what the model sees and what protocol clients record)

    All four claims were checked against the source, independently of the report:

    # Claim Verdict Evidence
    1 ToolEmitter documents that chunks never reach the LLM TRUE agentscope-core/.../tool/ToolEmitter.java:25 — "delivered to onActingChunk() events but are NOT sent to the LLM. Only the final return value ..."; same statement at :65
    2 Once a tool emits chunks, its id is registered and the real return value is never emitted as deltas TRUE ReActAgent.java:3217 (chunkedToolIds), :3226 (id added on non-empty chunk), :3328 → emitToolResultDelta(...), :3482 — if (chunkedToolIds.contains(toolId)) { return; }, before any output of entry.getValue() is read
    3 ToolResultEndEvent carries no result payload TRUE event/ToolResultEndEvent.java:25-28 — fields are replyId, toolCallId, toolCallName, state only
    4 Event stream / AG-UI TOOL_CALL_RESULT therefore reports the concatenated chunks TRUE agui/adapter/strategy/ToolResultEventConverter.java:38-52 buffers text/data deltas; AguiStreamContext.endToolResult (:283-300) emits AguiEvent.ToolCallResult from that buffer, which is exactly the chunk text

    One correction to the report: the model context is not polluted. ReActAgent.notifyPostActingHook builds the tool message from the real ToolResultBlock (ReActAgent.java:3735-3736 → ToolResultMessageBuilder.buildToolResultMsg). The leak is into the client-side / persisted event history, which is what bites clients that resend history or that render TOOL_CALL_RESULT as the outcome.

    Blast radius is larger than AG-UI. Any consumer that rebuilds a tool result from ToolResultTextDeltaEvent/ToolResultDataDeltaEvent + ToolResultEndEvent has the same divergence:

    • service-dataplane/.../managed/SessionEventMapper.java:212-251 — persists AGENT_TOOL_RESULT from the delta buffer, so the wrong text also lands in stored session history
    • agentscope-extensions-aistio/.../AistioObserverMiddleware.java:138-155
    • agentscope-harness/.../middleware/AgentTraceMiddleware.java:188-210, AsyncToolMiddleware.java:247-250
    • agentscope-harness/.../subagent/protocol/RemoteEventCodec.java:118-127 and the AG-UI SubagentEventConverter.java:118-127 — end event carries id/name/state only

    The chunkedToolIds logic itself is localized to ReActAgent; no other agent class duplicates it.

    Existing tests do not pin this contract (ToolEmitterIntegrationTest.java:232-277 asserts hook callbacks only; AguiAgentAdapterV2Test.java:737-756 and SessionEventMapperTest.java:195-211 test manual delta aggregation, so they pass with the bug).

    Proposed fix (minimal, backward compatible)

    1. Add an optional ToolResultBlock result to ToolResultEndEvent (extra constructor overload + getter; existing constructors unchanged).
    2. In ReActAgent.runToolBatch, populate it with the actual entry.getValue() when the end event is emitted (independent of chunkedToolIds).
    3. In AguiStreamContext.endToolResult, prefer the end-event result for ToolCallResult.content, falling back to the accumulated buffer when absent — progress chunks remain available as deltas (with their existing ordering/metadata) for UIs that want to render live progress.
    4. Same "prefer real result" treatment in SessionEventMapper and AistioObserverMiddleware.

    No change to ToolEmitter, tool method signatures, or the AG-UI event records; consumers that ignore the new field keep today's behavior until they opt in.

    Needs one design decision from a maintainer before a PR: is TOOL_CALL_RESULT defined as the canonical tool result (this fix), or as a stream replay where chunks legitimately win? If the former, we will open a PR implementing steps 1-3 above plus regression tests in agentscope-core, agentscope-extensions-agui and service-dataplane.


    Automated evaluation by github-manager (oss-maintainer). Reply /approve to generate the PR as described, /revise <notes> to adjust the approach, or /reject to drop it.

  2. added
    bug
    area/core/tool
    area/ext/integration
    on Sep 26, 2026
  3. oss-maintainer commented on Sep 29, 2026

    @oss-maintainer
    Collaborator

    Proposal window closed — no competing PR will be filed from this proposal window

    The 72 h feedback window opened by the evaluation above (2026-09-26T15:06:52Z) reached its bound on 2026-09-29T15:06:52Z with no /approve, /revise or /reject directive from a maintainer or the reporter. Automated PR generation for this issue is therefore skipped: no automated PR will be generated from this window.

    The thread did answer the underlying question in another way. Community PR #3331 (fix(core): report the tool return value on ToolResultEndEvent, authored by @King52HerTz on 2026-09-28T05:45:43Z) cross-references this issue and targets the same seam the evaluation described — the emitted-chunks-vs-return-value gap on the tool-result event. As of this read #3331 is OPEN, not a draft, unmerged, head fe18b3838781ae74c5a93d637a94a04b416bf12d, mergeable_state reported MERGEABLE, last updated 2026-09-28T06:49:55Z.

    So the implementation path here is already owned by a community PR waiting on maintainer review. Filing a second PR against the same issue is exactly the collision this window policy exists to prevent, which is why the proposal is stood down rather than escalated.

    Nothing in this notice disputes the evaluation: with ToolEmitter, an AG-UI TOOL_CALL_RESULT carrying the streamed chunks instead of the tool's return value is a real correctness gap, and the reported reproduction stands. The productive thread now is review of #3331 — merge it, request changes, or close it. If #3331 is closed unmerged and this fix is still wanted, reply on this issue and the question will be re-evaluated against whatever main looks like at that point.

    Automated feedback-window maintenance by github-manager-bot

  4. added a commit that references this issue on Oct 11, 2026
    707400e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/core/toolarea/ext/integrationbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions