Problem
CorrelationIdManager already scopes paired event correlations by AgentContext.context_id, but the V2 stream-to-regular fallback correlation is still stored in a single process-wide _stream_fallback_cid slot.
This becomes unsafe when a parent agent and one or more background sub-agents make LLM calls concurrently:
- The parent stream stores its request ID for a possible regular-call fallback.
- A sub-agent stream overwrites that process-wide value with its own request ID.
- If the parent falls back first, it consumes the sub-agent's correlation ID.
- The sub-agent then either loses its fallback correlation or consumes a value from another run.
The V2 client uses this correlation ID to merge streamed chunks with the final non-streamed response. A cross-context mix-up can therefore associate a fallback response with the wrong Agent run and breaks trace integrity for concurrent delegation.
Proposed change
- Store fallback correlation IDs by the existing AgentContext scope instead of introducing a new identifier.
- Pass the same
context_id scope when setting, clearing, and consuming fallback correlations.
- Preserve the current global-scope behavior for callers without an AgentContext.
- Add regression tests for interleaved parent/sub-agent set, clear, and pop operations.
Acceptance criteria
- Concurrent AgentContexts cannot overwrite or consume each other's stream fallback correlation IDs.
- Clearing a successful stream only clears the marker for that AgentContext.
- Existing callers that omit a scope retain the current single global-slot semantics.
- Focused Agentlang tests pass.
Problem
CorrelationIdManageralready scopes paired event correlations byAgentContext.context_id, but the V2 stream-to-regular fallback correlation is still stored in a single process-wide_stream_fallback_cidslot.This becomes unsafe when a parent agent and one or more background sub-agents make LLM calls concurrently:
The V2 client uses this correlation ID to merge streamed chunks with the final non-streamed response. A cross-context mix-up can therefore associate a fallback response with the wrong Agent run and breaks trace integrity for concurrent delegation.
Proposed change
context_idscope when setting, clearing, and consuming fallback correlations.Acceptance criteria