Skip to content

fix(harness): emit subagent end before parent completion - #2544

Merged
jujn merged 1 commit into
agentscope-ai:mainfrom
zouyx:featrue/fix-subagent-end-event-order
Aug 4, 2026
Merged

fix(harness): emit subagent end before parent completion#2544
jujn merged 1 commit into
agentscope-ai:mainfrom
zouyx:featrue/fix-subagent-end-event-order

Conversation

@zouyx

@zouyx zouyx commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

AgentScope-Java Version

2.0.1-SNAPSHOT

Description

Background

The Harness stream-event tests can intermittently miss the sourced child AGENT_END event. The child end event was emitted from doFinally, which runs after the terminal signal has propagated downstream. For a quickly completing child agent, the parent can therefore complete and close its event sink before the child end event is forwarded.

This race was exposed by the CI runs of #2532, but it is independent of that middleware-ordering change.

Changes

  • Emit the child AGENT_END before success or error is propagated to the parent.
  • Retain best-effort end signaling for cancellation.
  • Guard emission with AtomicBoolean so completion and cancellation races cannot produce duplicate child end events.
  • Strengthen the ordering test to require the sourced child AGENT_END before the parent AGENT_END.

The resulting order is:

parent AGENT_START
child  AGENT_START / events / AGENT_END
parent AGENT_END

Test

mvn -pl agentscope-harness -am \
  -Dtest=HarnessAgentSubagentStreamEventsTest \
  -Dsurefire.failIfNoSpecifiedTests=false \
  -Djacoco.skip=true \
  -DargLine=-javaagent:$HOME/.m2/repository/net/bytebuddy/byte-buddy-agent/1.17.7/byte-buddy-agent-1.17.7.jar \
  test

Result: 4 tests passed, 0 failures.

Checklist

  • 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 (not applicable: no public API or documentation change)
  • Code is ready for review

@gemini-code-assist

Copy link
Copy Markdown

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

@zouyx
zouyx marked this pull request as ready for review August 3, 2026 16:00
Copilot AI review requested due to automatic review settings August 3, 2026 16:00
@gemini-code-assist

Copy link
Copy Markdown

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

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

This PR fixes an intermittent race in HarnessAgent.streamEvents() subagent forwarding where a quickly completing child agent could have its sourced AGENT_END emitted too late (after the parent stream’s terminal signal), allowing the parent to close its event sink before the child end event is forwarded.

Changes:

  • Emit the sourced child AGENT_END on success/error before the terminal signal propagates downstream, while keeping best-effort cancel handling.
  • Guard child-end emission with an AtomicBoolean to prevent duplicate end events across completion/cancellation races.
  • Strengthen the ordering test to require the sourced child AGENT_END to occur before the parent AGENT_END.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
agentscope-harness/src/main/java/io/agentscope/harness/agent/tool/AgentSpawnTool.java Moves sourced child AGENT_END emission earlier (success/error) and guards against duplicates; retains cancel best-effort signaling.
agentscope-harness/src/test/java/io/agentscope/harness/agent/HarnessAgentSubagentStreamEventsTest.java Extends ordering assertions to require sourced child AGENT_END before parent AGENT_END.

@codecov

codecov Bot commented Aug 3, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.90909% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
.../agentscope/harness/agent/tool/AgentSpawnTool.java 90.90% 0 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@jujn
jujn merged commit 2d1a5e6 into agentscope-ai:main Aug 4, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants