Skip to content

feat(core): add middleware execution ordering - #2532

Open
zouyx wants to merge 2 commits into
agentscope-ai:mainfrom
zouyx:featrue/middleware-order
Open

feat(core): add middleware execution ordering#2532
zouyx wants to merge 2 commits into
agentscope-ai:mainfrom
zouyx:featrue/middleware-order

Conversation

@zouyx

@zouyx zouyx commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

AgentScope-Java Version

2.0.1-SNAPSHOT

Description

Closes #2449

Adds MiddlewareBase.order() with a default priority of 1.

ReActAgent.Builder.build() now performs one stable descending sort after all user and automatic middleware registrations. Higher numeric values are outermost; equal priorities retain registration order.

Also adds core/Harness coverage and updates Chinese and English middleware documentation.

Validation:

mvn -pl agentscope-core,agentscope-harness -am \
  -Dtest=ReActAgentNewLoopBuilderTest,HarnessMiddlewareOrderTest \
  -Dsurefire.failIfNoSpecifiedTests=false test

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

Copilot AI review requested due to automatic review settings August 2, 2026 15:31
@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

Adds an explicit middleware ordering mechanism to AgentScope-Java so middleware execution can be prioritized deterministically (while keeping registration order stable for ties), with accompanying tests and documentation updates.

Changes:

  • Introduces MiddlewareBase.order() (default 1) as the standard middleware priority hook.
  • Sorts registered middlewares once during ReActAgent.Builder.build() using a stable descending sort by order().
  • Adds/updates core + harness tests and updates English/Chinese middleware documentation to describe the ordering behavior.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
docs/v2/zh/docs/building-blocks/middleware.md Documents the new order()-based execution ordering (Chinese).
docs/v2/en/docs/building-blocks/middleware.md Documents the new order()-based execution ordering (English).
agentscope-harness/src/test/java/io/agentscope/harness/agent/HarnessMiddlewareOrderTest.java Adds harness-level coverage to validate ordering interactions with harness middlewares.
agentscope-core/src/test/java/io/agentscope/core/agent/ReActAgentNewLoopBuilderTest.java Adds core-level coverage for descending + stable tie behavior.
agentscope-core/src/main/java/io/agentscope/core/ReActAgent.java Applies a stable descending sort of middlewares by MiddlewareBase.order() during build.
agentscope-core/src/main/java/io/agentscope/core/middleware/MiddlewareBase.java Adds the new order() API with Javadoc describing its semantics.

Comment thread agentscope-core/src/main/java/io/agentscope/core/middleware/MiddlewareBase.java Outdated
### Execution order

Onion hooks (`onAgent`, `onReasoning`, `onActing`, `onModelCall`) — **the first middleware in the list is outermost**:
Onion hooks (`onAgent`, `onReasoning`, `onActing`, `onModelCall`) are ordered by `MiddlewareBase.order()` — **higher values are outermost**. The default order is `1`; middlewares with the same order retain their builder registration order:
### 执行顺序

Onion 类 hook(`onAgent`、`onReasoning`、`onActing`、`onModelCall`)—— **列表中第一个 middleware 处于最外层**
Onion 类 hook(`onAgent`、`onReasoning`、`onActing`、`onModelCall`)按 `MiddlewareBase.order()` 排序——**数值越大越处于最外层**。默认值是 `1`;相同 order 的 middleware 保持其 Builder 注册顺序
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
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.

[Feature]: 自定义的Middleware和框架内置Middleware 执行顺序是什么样的?可以设置吗?

2 participants