magic icon indicating copy to clipboard operation
magic copied to clipboard

fix(agentlang): isolate stream fallback correlations across agent contexts

Open Linxiushen opened this issue 3 months ago • 0 comments

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:

  1. The parent stream stores its request ID for a possible regular-call fallback.
  2. A sub-agent stream overwrites that process-wide value with its own request ID.
  3. If the parent falls back first, it consumes the sub-agent's correlation ID.
  4. 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.

Linxiushen avatar Aug 09 '26 05:08 Linxiushen