Skip to content

Conversation

@JGoP-L
Copy link
Contributor

@JGoP-L JGoP-L commented Dec 7, 2025

Describe what this PR does / why we need it

Fixes the Reactor context loss issue in SubGraphNodeAdapter.getGraphResponseFlux() method that caused distributed tracing information (traceId, spanId) to be lost when using manual subscription with Flux.create(). This fix ensures that observation scope and trace context are properly propagated across sub-graph agent boundaries.

Does this pull request fix one issue?

Fixes #3327

Describe how you did it

Modified ReactAgent.SubGraphNodeAdapter.getGraphResponseFlux() to wrap the Flux.create() with Flux.deferContextual() pattern:

  • Captured the Reactor context using deferContextual(ctx -> ...)
  • Propagated the context to the inner subscription using .contextWrite(ctx)
  • Preserved all existing buffering and message filtering logic

The change is minimal and surgical - only 2 lines added to the 49-line method, ensuring backward compatibility while fixing the context propagation issue.

Describe how to verify it

  1. Run the included test: mvn test -Dtest=ReactAgentContextPropagationTest

    • Tests demonstrate that context is properly preserved with the fix
    • All tests pass with 0 failures
  2. Run full test suite: mvn clean test -pl spring-ai-alibaba-agent-framework

    • 195+ tests pass with 0 failures and 0 regressions
    • Confirms backward compatibility
  3. Manual verification:

    • Distributed tracing context (traceId, spanId) now propagates correctly through sub-graph agents
    • Observation scopes are properly correlated in multi-agent scenarios

Special notes for reviews

  • Root Cause: Manual subscribe() call inside Flux.create() breaks Reactor's context propagation chain
  • Solution Pattern: Using Flux.deferContextual() is the standard Reactor pattern for context-aware deferred execution
  • Impact: Zero breaking changes - fully backward compatible, only fixes context propagation
  • Testing: Comprehensive test coverage includes normal flows, error paths, and edge cases
  • Code Quality: Passes checkstyle, no compilation errors, follows Reactor best practices

Copilot AI review requested due to automatic review settings December 7, 2025 14:36
Copy link
Contributor

Copilot AI left a comment

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 a Reactor context propagation issue in the SubGraphNodeAdapter.getGraphResponseFlux() method where distributed tracing information (traceId, spanId) was being lost during manual subscription within Flux.create(). The fix wraps the existing logic with Flux.deferContextual() to properly capture and propagate the Reactor context across sub-graph agent boundaries.

Key Changes:

  • Modified ReactAgent.SubGraphNodeAdapter.getGraphResponseFlux() to use Flux.deferContextual() pattern for proper context propagation
  • Added comprehensive unit tests demonstrating both the problem (manual subscribe loses context) and the solution (deferContextual preserves context)
  • Maintained all existing buffering and message filtering logic without changes

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
spring-ai-alibaba-agent-framework/src/main/java/com/alibaba/cloud/ai/graph/agent/ReactAgent.java Wrapped Flux.create() with Flux.deferContextual() and added .contextWrite(ctx) to propagate Reactor context through sub-graph subscriptions
spring-ai-alibaba-agent-framework/src/test/java/com/alibaba/cloud/ai/graph/agent/ReactAgentContextPropagationTest.java Added unit tests demonstrating the context loss bug with manual subscription and verifying the fix with deferContextual pattern

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@chickenlj
Copy link
Collaborator

Thanks for the contribution. This has been fixed in 1.1.0.0-RC1 before we noticed this request. Please check the latest release.

@chickenlj chickenlj closed this Dec 12, 2025
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.

[BUG] ReactAgent.SubGraphNodeAdapter.getGraphResponseFlux` loses Reactor context due to manual subscription, breaking observation and trace propagation

2 participants