Skip to content

fix(core): fix ReActAgent stream event block end ordering#1829

Merged
chickenlj merged 2 commits into
agentscope-ai:mainfrom
jujn:fix/react-agent-event-order
Jun 25, 2026
Merged

fix(core): fix ReActAgent stream event block end ordering#1829
chickenlj merged 2 commits into
agentscope-ai:mainfrom
jujn:fix/react-agent-event-order

Conversation

@jujn

@jujn jujn commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Description:

Close #1634
Fixes ReActAgent streamEvents block lifecycle ordering so text, thinking, and tool-call end events are emitted at the correct transition points instead of being deferred until MODEL_CALL_END.

Code Changes & Architecture

  1. Fixed model-call block lifecycle ordering:

    • Added ModelCallBlockLifecycle to manage active text, thinking, and tool-call blocks within a single model-call stream.
    • Flushes THINKING_BLOCK_END before TEXT_BLOCK_START.
    • Flushes active TEXT_BLOCK_END, THINKING_BLOCK_END, and prior TOOL_CALL_END events before a new TOOL_CALL_START.
    • Flushes any remaining active block ends before MODEL_CALL_END.
  2. Preserved concurrency-safe state tracking:

    • Kept AtomicBoolean for text/thinking lifecycle state.
    • Kept ConcurrentHashMap for active tool-call tracking.
    • The fix only changes end-event flush timing, not event payloads or block identity.
  3. Extended stream event ordering tests:

    • Added coverage for thinking-to-text transitions.
    • Added coverage for text/thinking-to-tool-call transitions.
    • Added coverage for multiple tool calls.
    • Added coverage for summary model-call ordering.
    • Added coverage to ensure consecutive text chunks do not duplicate start/end events.
  4. Event output log before and after repair
    使用如下输入用例测试(Text+Thinking+mult Tool Call):“请先讲一个简单的冷笑话,再并行查北京、杭州天气。”
    然后这里对输出做了简化,所有 delta 只保留第一个,其它原样输出。

修复前
78b07f10652c4aeeb3788170ef1ce6d2
MODEL_CALL_START

8aae44b7fda045cba528caf0979102b1
THINKING_BLOCK_START

9909b0399ead4094bf4516919dffc188
THINKING_BLOCK_DELTA

e0afd2ffeef04cde9cb00f754571fd35
TEXT_BLOCK_START

aae5dc4660a04147acf26d45a2f905eb
TEXT_BLOCK_DELTA

230ba43214354d46942fdf9c426b9d18
TOOL_CALL_START

e5443522eda14fe18d8f5de56f8234a5
TOOL_CALL_DELTA

ce2a278b1b484f4aa479370ec5156891
TOOL_CALL_START

d18763d17e744527b190ec4d1ca4b68c
TOOL_CALL_DELTA

dc758a3d6d3a43509627985b870e354a
TEXT_BLOCK_END

88a43946998a4b9fb21c61f41a6daae2
THINKING_BLOCK_END

f45a5a54c5ce4dc0aa8c47ade4a9ebbb
TOOL_CALL_END

5948d7bc9fa64fb49af86bd8240477bb
TOOL_CALL_END

55bd85d4adb040e8b8b29a6e1ddfad16
MODEL_CALL_END

ccb2012a41ab46af93139e68dc5216de
TOOL_RESULT_START

f62c0bbae6604aad932092eb6b50fdeb
TOOL_RESULT_START

f62b53d4a6db48408088ddaef5496f31
TOOL_RESULT_TEXT_DELTA

7c4d14cdbf8a439da7770a9af120e6e9
TOOL_RESULT_END

b81c2848b3064ed494fbdd3507a5b256
TOOL_RESULT_TEXT_DELTA

bfa2a29443de44ccb076c3f2ba1c9f10
TOOL_RESULT_END

9b919042124e40ecb35050a597b63679
MODEL_CALL_START

034e17b98ee4487e986ec0112d0ae2f6
THINKING_BLOCK_START

c3f298bba93e4bb5ab01d1491fa8285e
THINKING_BLOCK_DELTA

e88f954350334337b4812a1b1dc48e00
TEXT_BLOCK_START

5caad9d12b60443c8d9cf494c067d5da
TEXT_BLOCK_DELTA

0c667f4ccf0242c0b9a7c013761e7d86
TEXT_BLOCK_END

23992edd28b54b4996535241dfa925e9
THINKING_BLOCK_END

ed78e87125d749d29ba593c785141a62
MODEL_CALL_END
修复后
944985f3fde04b2e8b9e4fd9f746cc1e
MODEL_CALL_START

fcc2f929327942fa967ddbb2713e87bf
THINKING_BLOCK_START

42d0f4f85c6c47a490ff45603de2b728
THINKING_BLOCK_DELTA

a390f43f3a484ee7b86c125c16dcf063
THINKING_BLOCK_END

1baf71010149409fafcd36cbf256bb97
TEXT_BLOCK_START

67d15091acc64106ad9360909ec07b43
TEXT_BLOCK_DELTA

70c0e5b57aa9430886f3bf3bb1e31ecd
TEXT_BLOCK_END

cb126d6d4b2d4517a22fa9027ec5113a
TOOL_CALL_START

780f61ceba9f44dca1075c0a3a91339c
TOOL_CALL_DELTA

83c6cb62be8d4e4cb9b42140eb5600f5
TOOL_CALL_END

118c541fdbdd4e33b3e7c96afbd201b5
TOOL_CALL_START

7099bd91939d4e9280879bcd87a6bcd7
TOOL_CALL_DELTA

d541ba52bbfa45c4b250922b6c4d1a1d
TOOL_CALL_END

4f2d16bdc4f349228d2b8fbfbdcf9ee0
MODEL_CALL_END

95135746f837497f858c559af718552f
TOOL_RESULT_START

2ad377fd56c24d7a97cbccce1eb7471f
TOOL_RESULT_START

46cc54b143cc458398149379fd8cf1fc
TOOL_RESULT_TEXT_DELTA

c290c5dcd4e7404384d9ae8dc327300f
TOOL_RESULT_END

03377ffdaec74fcda66afc17949c887a
TOOL_RESULT_TEXT_DELTA

f80076da6d634d9eae7c8024c54dfac1
TOOL_RESULT_END

c161f9bd7fe74fcabc6c3ae08bf53ed4
MODEL_CALL_START

808b9d9faf8147d395ba817850631f01
THINKING_BLOCK_START

6ae924698d14461f9db3b082499de3c9
THINKING_BLOCK_DELTA

d66917b7c58e4c7e8c21d0d32a01374b
THINKING_BLOCK_END

d9a2956c122c4096a9417cd96a00b772
TEXT_BLOCK_START

99a8337989ae4a9c9b39eabefdf5ef1a
TEXT_BLOCK_DELTA

c3bb8ec529c34f4e9e7d0d2dafafdfdc
TEXT_BLOCK_END

8a4f58ae9a7543ed8a44692e868be41d
MODEL_CALL_END

Checklist

Please check the following items before code is ready to be reviewed.

  • Code has been formatted with mvn spotless:apply
  • All tests are passing (mvn test)
  • Javadoc comments are complete and follow project conventions
  • Related documentation has been updated (e.g. links, examples, etc.)
  • Code is ready for review

@jujn jujn requested review from a team and Copilot June 18, 2026 06:31

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes ReActAgent streaming event lifecycle ordering so block end events (thinking/text/tool-call) are emitted at the correct transition points instead of being deferred until MODEL_CALL_END, addressing #1634.

Changes:

  • Introduced a per-model-call lifecycle helper to coordinate start/end flushing for text, thinking, and tool-call blocks during streamEvents.
  • Adjusted end-event emission so thinking ends before text begins, and active blocks are flushed before tool-call/model-call boundaries.
  • Extended end-to-end tests to assert correct ordering across thinking→text, text/thinking→tool-call, multiple tool calls, and summary model-call flows.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
agentscope-core/src/main/java/io/agentscope/core/ReActAgent.java Adds a model-call-scoped block lifecycle tracker and updates streaming emission to flush block end events at transition points.
agentscope-core/src/test/java/io/agentscope/core/agent/ReActAgentNewLoopReplyTest.java Adds ordering-focused tests to validate corrected stream event sequencing and prevent regressions.

Comment thread agentscope-core/src/main/java/io/agentscope/core/ReActAgent.java
@codecov

codecov Bot commented Jun 18, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.38462% with 5 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...e/src/main/java/io/agentscope/core/ReActAgent.java 90.38% 1 Missing and 4 partials ⚠️

📢 Thoughts on this report? Let us know!

@AgentScopeJavaBot AgentScopeJavaBot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 AI Review

PR #1829 fixes issue #1634: ReActAgent streamEvents streaming event ordering was incorrect. The original implementation deferred THINKING_BLOCK_END, TEXT_BLOCK_END, and TOOL_CALL_END events until MODEL_CALL_END, causing frontend consumers to be unable to correctly distinguish block boundaries. The fix introduces a ModelCallBlockLifecycle inner class that flushes previous block END events at block transition points (thinking→text, any→tool-call, any→model-call-end), ensuring strictly ordered event streams. The change involves +94/-59 lines in ReActAgent.java and a new ReActAgentNewLoopReplyTest.java with comprehensive test coverage. The concurrency strategy (AtomicBoolean + ConcurrentHashMap) remains sound. The code is well-structured and the tests are thorough. No blocking issues found.

Comment thread agentscope-core/src/main/java/io/agentscope/core/ReActAgent.java
Comment thread agentscope-core/src/main/java/io/agentscope/core/ReActAgent.java

@AgentScopeJavaBot AgentScopeJavaBot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 AI Review

PR #1829 fixes issue #1634: ReActAgent streamEvents streaming event ordering was incorrect. The original implementation deferred THINKING_BLOCK_END, TEXT_BLOCK_END, and TOOL_CALL_END events until MODEL_CALL_END, causing frontend consumers to be unable to correctly distinguish block boundaries. The fix introduces a ModelCallBlockLifecycle inner class that flushes previous block END events at block transition points (thinking→text, any→tool-call, any→model-call-end), ensuring strictly ordered event streams. The change involves +94/-59 lines in ReActAgent.java and a new ReActAgentNewLoopReplyTest.java with comprehensive test coverage. The concurrency strategy (AtomicBoolean + ConcurrentHashMap) remains sound. The code is well-structured and the tests are thorough. No blocking issues found.

Comment thread agentscope-core/src/main/java/io/agentscope/core/ReActAgent.java
Comment thread agentscope-core/src/main/java/io/agentscope/core/ReActAgent.java
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@AgentScopeJavaBot AgentScopeJavaBot added bug Something isn't working area/core/agent Agent runtime, pipeline, hooks, plan labels Jun 19, 2026
@chickenlj chickenlj merged commit d3cafaa into agentscope-ai:main Jun 25, 2026
6 checks passed
@jujn jujn deleted the fix/react-agent-event-order branch June 25, 2026 06:59
amyaxy pushed a commit to amyaxy/agentscope-java that referenced this pull request Jun 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core/agent Agent runtime, pipeline, hooks, plan bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]:v2 中流式出来的数据顺序有问题

4 participants