Skip to content

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

Description

@Linxiushen

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions