feat(event): emit COMPACTION_START/END events from CompactionMiddleware#1962
Open
Buktal wants to merge 9 commits into
Open
feat(event): emit COMPACTION_START/END events from CompactionMiddleware#1962Buktal wants to merge 9 commits into
Buktal wants to merge 9 commits into
Conversation
When context compaction is triggered, CompactionMiddleware now emits COMPACTION_START before the summarization LLM call and COMPACTION_END after reasoning completes, allowing frontends to display progress instead of appearing frozen. Events carry token statistics so callers can show meaningful progress information. Closes agentscope-ai#1905
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
…ent and CompactionMiddleware - CompactionEventRoundTrip in AgentEventStreamTest: JSON serialization/deserialization round-trip via Jackson polymorphism for both compaction event types; field values and type discriminator are verified - CompactionMiddlewareTest: covers the four execution paths — no compaction (passthrough), compaction triggered (START/END wrap next flux), non-ReActAgent short-circuit, and compaction error fallback
jujn
suggested changes
Jul 4, 2026
Previously COMPACTION_START was emitted inside flatMapMany after the compactIfNeeded Mono completed, causing the frontend to appear stuck during the actual compaction wait period. Now the middleware performs a lightweight threshold pre-check and emits COMPACTION_START immediately before the compaction Mono subscribes. COMPACTION_END fires right after compaction completes, before reasoning output begins. All paths (success, skip, error) properly close the START/END pair so the frontend never hangs in loading state.
Contributor
The test asserted inner events appeared between START and END, but the actual middleware contract is START → END → inner (compaction finishes before reasoning begins). Also improved the error-path test to use a low threshold config that actually enters the compaction branch, ensuring the START/END pairing contract is verified on error recovery.
Tests that call agent.call() without closing the agent leave background resources (session writers, task repositories) running, causing JUnit @tempdir cleanup to fail with DirectoryNotEmptyException. Wrap agent construction in try-with-resources to ensure proper shutdown before temp directory deletion.
Contributor
Author
jujn
suggested changes
Jul 8, 2026
jujn
left a comment
Contributor
There was a problem hiding this comment.
Additionally, it is suggested to narrow down the PR description from "Fixes1905" to partial fixes (including only COMPACTION-START/END events)
Move onErrorResume before flatMapMany so it only catches errors from compactIfNeeded(), not from downstream next.apply() execution. This prevents duplicate model/tool execution and misleading compaction end events when downstream reasoning fails after a successful compaction.
Collaborator
|
This PR has conflicts with the git fetch origin
git checkout feat/compaction-events
git rebase origin/main
# resolve conflicts, then:
git push --force-with-leaseThis is a one-time reminder. Feel free to @mention me for a re-review after conflicts are resolved. Automated notification by github-manager-bot |
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.0-SNAPSHOT
Description
Fixes #1905.
Background: When
CompactionMiddlewaretriggers context compaction (a summarization LLM call), the frontend receives no events during this period and appears frozen.Changes:
COMPACTION_STARTandCOMPACTION_ENDtoAgentEventTypeCompactionStartEvent(carriesestimatedTokens,triggerThreshold) andCompactionEndEvent(carriesoriginalMessageCount,compactedMessageCount,estimatedTokensSaved)AgentEvent@JsonSubTypesCompactionMiddleware.onReasoning()wraps the reasoning stream withCOMPACTION_START→ stream →COMPACTION_ENDwhen compaction is actually triggered (events are not emitted when the token threshold is not reached)Testing: Full build passes (
mvn clean install). The events fire only when compaction triggers, consistent with the existing event model (e.g.MODEL_CALL_START/END).Checklist
mvn spotless:applymvn test)