Skip to content

fix(core): propagate acting middleware result rewrites - #2676

Open
dangzitou wants to merge 2 commits into
agentscope-ai:mainfrom
dangzitou:agent/fix-middleware-tool-rewrite
Open

fix(core): propagate acting middleware result rewrites#2676
dangzitou wants to merge 2 commits into
agentscope-ai:mainfrom
dangzitou:agent/fix-middleware-tool-rewrite

Conversation

@dangzitou

@dangzitou dangzitou commented Aug 12, 2026

Copy link
Copy Markdown

AgentScope-Java Version

2.0.3-SNAPSHOT

Description

Fixes #2672.

ReActAgent.actingStream published raw tool-result events before the onActing middleware chain. The acting phase then discarded the post-middleware events and continued with the raw resultHolder, so a middleware rewrite was visible neither to event consumers nor to the next model call.

This change:

  • publishes acting events after the middleware chain;
  • detects tool-result delta/end events that middleware replaced or filtered;
  • rebuilds only the affected ToolResultBlock values from the transformed stream;
  • preserves the original result object when middleware passes all result events through unchanged.

The new end-to-end regression test rewrites search:alpha to rewritten. On current main it failed because the public stream returned search:alpha; with this change both the public stream and the second model request contain only rewritten.

Tests

  • mvn -pl agentscope-core -Dtest=ReActAgentNewLoopE2ETest#actingMiddlewareCanRewriteToolResultForEventsAndNextModelCall test — 1 passed
  • mvn -pl agentscope-core -Dtest=ReActAgentNewLoopE2ETest,ReActAgentMiddlewareIntegrationTest test — 12 passed
  • mvn -pl agentscope-core test — 2275 passed, 9 skipped
  • Spotless check passed as part of the Maven builds

Compatibility and risk

No public API signatures change. Existing tool results retain their original ToolResultBlock instance unless middleware actually changes the tool-result event sequence. Text and data deltas, metadata, and end state are retained when rebuilding a transformed result.

Checklist

  • Code has been formatted with mvn spotless:apply
  • All relevant tests are passing
  • Javadoc comments are complete and follow project conventions
  • Related middleware documentation has been updated
  • Code is ready for review

@codecov

codecov Bot commented Aug 12, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 73.61111% with 19 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...e/src/main/java/io/agentscope/core/ReActAgent.java 73.61% 8 Missing and 11 partials ⚠️

📢 Thoughts on this report? Let us know!

@dangzitou
dangzitou marked this pull request as ready for review August 12, 2026 18:25
Copilot AI lite review requested due to automatic review settings August 12, 2026 18:25

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 ReActAgent acting-phase middleware rewrites so that transformed tool-result events are what downstream consumers observe and what the next model iteration receives, addressing issue #2672.

Changes:

  • Publish acting-phase tool-result events after the onActing middleware chain, not before it.
  • Detect when middleware replaces/filters tool-result delta/end events and rebuild only the affected ToolResultBlock values (preserving original instances when unchanged).
  • Add an end-to-end regression test ensuring a middleware rewrite is reflected both in the public event stream and the subsequent model request.

Reviewed changes

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

File Description
agentscope-core/src/main/java/io/agentscope/core/ReActAgent.java Moves event publication post-middleware and reconstructs tool results from the transformed event stream when needed.
agentscope-core/src/main/java/io/agentscope/core/middleware/MiddlewareBase.java Documents that tool-result delta-event transformations affect the next-iteration result message.
agentscope-core/src/test/java/io/agentscope/core/agent/ReActAgentNewLoopE2ETest.java Adds an E2E regression test covering acting middleware rewriting tool results for events and next model call.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +2693 to +2694
List<AgentEvent> coreToolResultEvents = new ArrayList<>();
List<AgentEvent> transformedToolResultEvents = new ArrayList<>();
@AgentScopeJavaBot AgentScopeJavaBot added bug Something isn't working area/core/agent Agent runtime, pipeline, hooks, plan labels Aug 13, 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]:middleware 工具重写失败

3 participants