Skip to content

fix(Tool): use mergeSequential to preserve tool call order in paralle…#652

Merged
AlbumenJ merged 1 commit intoagentscope-ai:mainfrom
Hipple:fix/preserve-tool-execution-order
Jan 26, 2026
Merged

fix(Tool): use mergeSequential to preserve tool call order in paralle…#652
AlbumenJ merged 1 commit intoagentscope-ai:mainfrom
Hipple:fix/preserve-tool-execution-order

Conversation

@Hipple
Copy link
Contributor

@Hipple Hipple commented Jan 24, 2026

AgentScope-Java Version

1.0.7

Description

The current implementation of parallel tool execution uses Flux.merge(monos). While Flux.merge executes tasks concurrently, it emits results as soon as they complete, without preserving the source order.

This creates a critical bug in ReActAgent.java (or the relevant calling logic), where tool calls and results are paired by index:

// Logic assuming strict ordering
IntStream.range(0, toolCalls.size())
    .mapToObj(i -> Map.entry(toolCalls.get(i), results.get(i)))
    .toList()

Impact: If a later tool finishes before an earlier tool, the results become misaligned with the original toolCalls list, causing the agent to process the wrong result for a given action.

Changes Made
Replaced Flux.merge(monos) with Flux.mergeSequential(monos).

// Before: Unordered (Race condition risk)
return Flux.merge(monos).collectList();

// After: Ordered + Parallel
return Flux.mergeSequential(monos).collectList();

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

@Hipple Hipple requested a review from a team January 24, 2026 14:26
@gemini-code-assist
Copy link

Summary of Changes

Hello @Hipple, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a significant issue in the parallel execution of tools within the AgentScope-Java framework. The previous implementation, using Flux.merge, did not guarantee the order of results, which led to a critical misalignment when agents attempted to pair tool calls with their corresponding outcomes. By switching to Flux.mergeSequential, the system now ensures that results are processed in the correct sequence, thereby maintaining data integrity and preventing agents from acting on incorrect information.

Highlights

  • Bug Fix: Tool Call Order: Addressed a critical bug in parallel tool execution where results could be misaligned with their original tool calls due to Flux.merge not preserving order.
  • Reactive Stream Operator Change: Replaced Flux.merge(monos) with Flux.mergeSequential(monos) in the ToolExecutor to ensure that results from parallel tool executions are emitted in the order their sources were subscribed.
  • Impact on Agent Logic: This change prevents ReActAgent (or similar logic) from processing incorrect results by ensuring the strict ordering required for pairing tool calls with their outcomes.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@cla-assistant
Copy link

cla-assistant bot commented Jan 24, 2026

CLA assistant check
All committers have signed the CLA.

@cla-assistant
Copy link

cla-assistant bot commented Jan 24, 2026

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request addresses a critical bug in parallel tool execution where the order of results was not preserved, leading to a potential mismatch between tool calls and their outcomes. The fix, replacing Flux.merge with Flux.mergeSequential, is correct and effectively resolves the issue by ensuring results are emitted in the same order as the initial tool calls, even when executed concurrently. The change is well-described, targeted, and I have no further comments.

@codecov
Copy link

codecov bot commented Jan 24, 2026

Codecov Report

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

Files with missing lines Patch % Lines
...ain/java/io/agentscope/core/tool/ToolExecutor.java 0.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@AlbumenJ AlbumenJ merged commit 71f3465 into agentscope-ai:main Jan 26, 2026
4 of 5 checks passed
@Hipple Hipple deleted the fix/preserve-tool-execution-order branch January 26, 2026 03:25
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.

2 participants