feat(middleware): add final answer filter for ReAct streams - #2926
Merged
jujn merged 2 commits intoSep 2, 2026
Conversation
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
jujn
approved these changes
Sep 2, 2026
Buktal
pushed a commit
that referenced
this pull request
Sep 2, 2026
…rdown to stop flaky temp-dir deletion (#2935) ### Problem Two CI runs fail intermittently with JUnit errors that are not test-logic failures but teardown failures: - `Failed to delete temp directory` (Windows, PR #2923) - `Failed to close extension context` / `Failed to delete temp directory` (Ubuntu, PR #2926) Both surface only when a test builds a transient `HarnessAgent`, drives it to completion via `.block()` / `.stream()...block()`, and uses `@TempDir` for its workspace/state home. ### Root cause This is a long-standing race, not a regression of any single commit. The harness memory flush has always been asynchronous: when an agent stream completes, `MemoryFlushMiddleware#onAgent` dispatches the flush on `Schedulers.boundedElastic()` via `subscribe(...)` — i.e. fire-and-forget. The calling test's `.block()` only waits for the business stream, **not** for that background flush. So the timeline is: 1. Test calls `.block()` and returns. 2. JUnit begins `@TempDir` teardown and deletes the temp directory. 3. The async flush on `boundedElastic` is **still writing session/transcript mirror files into that same `@TempDir`** (or still holds open file handles). The result: directory/file deletion fails with `IOException` → wrapped as `JUnitException`. It is timing-dependent (depends on IO speed, scheduler, and how many files are written), which is why it flakes rather than failing deterministically, and why Windows (stricter file locking) fails more readily than Linux. The normal production path avoids this because `HarnessAgent#close()` calls `SessionTree.awaitMirrorQuiescence(...)` + `MemoryBackgroundTasks.awaitQuiescence(...)`. The flaky tests never call `close()` on their transient agent. ### Fix Add test-side quiescence that mirrors what `HarnessAgent#close()` already does, run **after each test method but before the `TempDir` extension deletes the directory**: - `HarnessBackgroundTaskQuiescenceExtension` — an `AfterEachCallback` that calls `SessionTree.awaitMirrorQuiescence(5s)` + `MemoryBackgroundTasks.awaitQuiescence(5s)`. When nothing is in flight both calls return immediately, making it a no-op for tests that never trigger a flush. - `@HarnessQuiescence` — a composed meta-annotation (`@ExtendWith(HarnessBackgroundTaskQuiescenceExtension.class)`) so at-risk tests only need a one-line annotation. - Applied `@HarnessQuiescence` to 16 harness test classes that match the at-risk pattern (`HarnessAgent.builder()` + `.call()`/`.stream()` + `@TempDir`), including the three classes that flaked in CI: `JsonSessionDefaultLocationTest`, `HarnessAgentIntegrationExampleTest`, `HarnessAgentDynamicHookBuilderTest`. Production code is unchanged — the flush remains fire-and-forget so conversation completion is never blocked.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
AgentScope-Java Version
2.0.3-SNAPSHOT
Description
ReAct streaming currently exposes text from intermediate reasoning rounds as well as the final answer, making it difficult for applications to consume only the final user-facing text.
This PR adds an opt-in
FinalAnswerFilterMiddlewarethat:Because a round can only be classified after observing whether it produces a tool call, final-round text is emitted when the corresponding model call ends.
Tests include:
English and Chinese middleware documentation have also been updated.
Closes #2872
Checklist
mvn spotless:applymvn test)