Skip to content

fix: propagate Reactor context to chunk event hooks in ReActAgent#1823

Closed
chcodex wants to merge 2 commits into
agentscope-ai:mainfrom
chcodex:bugfix/reactor-context
Closed

fix: propagate Reactor context to chunk event hooks in ReActAgent#1823
chcodex wants to merge 2 commits into
agentscope-ai:mainfrom
chcodex:bugfix/reactor-context

Conversation

@chcodex

@chcodex chcodex commented Jun 18, 2026

Copy link
Copy Markdown

fix: propagate Reactor context to chunk event hooks in ReActAgent

Three bare .subscribe() calls in ReActAgent (fireReasoningChunk,
fireActingChunk, fireSummaryChunk) created new subscription chains
without the parent Reactor context, causing ReasoningChunkEvent,
ActingChunkEvent, and SummaryChunkEvent to have an empty ContextView
in downstream hooks.

Fix each with Flux.deferContextual + contextWrite to capture and
propagate the parent context, matching the existing pattern used by
executeToolCalls at line 2389-2391.

@chcodex chcodex requested a review from a team June 18, 2026 04:10
@codecov

codecov Bot commented Jun 18, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 72.91667% with 13 lines in your changes missing coverage. Please review.

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

📢 Thoughts on this report? Let us know!

@AgentScopeJavaBot AgentScopeJavaBot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🤖 AI Review

This PR fixes a real Reactor context propagation bug where three fire-and-forget .subscribe() calls in ReActAgent (fireReasoningChunk, fireActingChunk, fireSummaryChunk) created detached subscription chains that lost the parent Reactor context. The fix correctly applies Flux.deferContextual + contextWrite to capture and propagate the parent context, matching the existing pattern already used by executeToolCalls in runToolBatch. The changes are minimal, focused, and consistent with the codebase conventions. No correctness issues found.

mci.model().getModelName())
.subscribe();
}
return Flux.deferContextual(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[praise] Clean fix. Wrapping the concatMap body in Flux.deferContextual correctly captures the parent Reactor context at subscription time, ensuring fireReasoningChunk hooks see the full ContextView (including SubagentEventBus). The pattern is consistent with the existing executeToolCalls context propagation.

hookDispatcher
.fireActingChunk(
toolUse, chunk, toolkit)
.contextWrite(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[praise] Good catch — the fireActingChunk call was the most subtle of the three since parentCtx was already in scope from the enclosing Flux.deferContextual. Adding just .contextWrite(ctx -> ctx.putAll(parentCtx)) here is the minimal correct change.

model.getModelName())
.subscribe();
}
return Flux.deferContextual(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[praise] Symmetric fix to modelCallStream. The Flux.deferContextual + contextWrite pattern is applied consistently, ensuring fireSummaryChunk hooks also receive the parent context.

@AgentScopeJavaBot AgentScopeJavaBot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🤖 AI Review

This PR fixes a real Reactor context propagation bug where three fire-and-forget .subscribe() calls in ReActAgent (fireReasoningChunk, fireActingChunk, fireSummaryChunk) created detached subscription chains that lost the parent Reactor context. The fix correctly applies Flux.deferContextual + contextWrite to capture and propagate the parent context, matching the existing pattern already used by executeToolCalls in runToolBatch. The changes are minimal, focused, and consistent with the codebase conventions. No correctness issues found.

mci.model().getModelName())
.subscribe();
}
return Flux.deferContextual(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[praise] Clean fix. Wrapping the concatMap body in Flux.deferContextual correctly captures the parent Reactor context at subscription time, ensuring fireReasoningChunk hooks see the full ContextView (including SubagentEventBus). The pattern is consistent with the existing executeToolCalls context propagation.

hookDispatcher
.fireActingChunk(
toolUse, chunk, toolkit)
.contextWrite(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[praise] Good catch — the fireActingChunk call was the most subtle of the three since parentCtx was already in scope from the enclosing Flux.deferContextual. Adding just .contextWrite(ctx -> ctx.putAll(parentCtx)) here is the minimal correct change.

model.getModelName())
.subscribe();
}
return Flux.deferContextual(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[praise] Symmetric fix to modelCallStream. The Flux.deferContextual + contextWrite pattern is applied consistently, ensuring fireSummaryChunk hooks also receive the parent context.

@AgentScopeJavaBot AgentScopeJavaBot added bug Something isn't working area/core/agent Agent runtime, pipeline, hooks, plan labels Jun 19, 2026
@chcodex

chcodex commented Jun 25, 2026

Copy link
Copy Markdown
Author

@chickenlj Hello, is there any problem with this PR? I saw that it has not been merged for a long time.

@oss-maintainer

Copy link
Copy Markdown
Collaborator

@chickenlj Hello, is there any problem with this PR? I saw that it has not been merged for a long time.

Thanks for the contribution. There're conflicts caused by other pull requests and I have fixed this issue with another request.

@chcodex chcodex deleted the bugfix/reactor-context branch June 26, 2026 04:06
zhaojinyu pushed a commit to zhaojinyu/agentscope-java that referenced this pull request Jun 26, 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.

3 participants