Skip to content

Conversation

@westey-m
Copy link
Contributor

Motivation and Context

#3411

Description

  • Adding AgentRunContext
  • Setting AgentRunContext automatically on each run in AIAgent.

Contribution Checklist

  • The code builds clean without any errors or warnings
  • The PR follows the Contribution Guidelines
  • All unit tests pass, and I have added new tests where possible
  • Is this a breaking change? If yes, add "[BREAKING]" prefix to the title of the PR.

Copilot AI review requested due to automatic review settings January 28, 2026 11:41
@markwallace-microsoft markwallace-microsoft added documentation Improvements or additions to documentation .NET labels Jan 28, 2026
@github-actions github-actions bot changed the title Adding AgentRunContext to allow accessing agent run info in external downstream components .NET: Adding AgentRunContext to allow accessing agent run info in external downstream components Jan 28, 2026
@westey-m westey-m requested a review from stephentoub January 28, 2026 11:45
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 introduces AgentRunContext to provide ambient access to agent run information for downstream components like tools and middleware. This addresses issue #3411 regarding ConversationId propagation in Agent-as-a-Tool scenarios by making agent run context (agent, session, messages, options) accessible via AIAgent.CurrentRunContext using AsyncLocal<T>.

Changes:

  • Added AgentRunContext class to encapsulate agent run state (agent, session, request messages, run options)
  • Modified AIAgent to set CurrentRunContext using AsyncLocal<T> at the start of each run (both streaming and non-streaming)
  • Added comprehensive unit tests for AgentRunContext and updated AIAgent tests to verify context propagation
  • Added architectural decision record documenting the design rationale

Reviewed changes

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

Show a summary per file
File Description
dotnet/src/Microsoft.Agents.AI.Abstractions/AgentRunContext.cs New sealed class providing read-only access to agent run information
dotnet/src/Microsoft.Agents.AI.Abstractions/AIAgent.cs Added AsyncLocal field and CurrentRunContext property; modified RunAsync/RunStreamingAsync to set context
dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/AgentRunContextTests.cs Comprehensive unit tests for AgentRunContext constructor validation and property accessors
dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/AIAgentTests.cs Added tests verifying CurrentRunContext is set for all RunAsync/RunStreamingAsync overloads; updated TestAgentSession visibility
docs/decisions/0015-agent-run-context.md Architectural decision record explaining design choices and trade-offs

Copy link
Member

Choose a reason for hiding this comment

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

Suggest adding additional middleware tests to ensure this context can be captured as expected from the normal agent middlewares and possible the function invoking middleware.

builder.Use(shared)
builder.Use(runAsync, streamingRunAsync)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation .NET

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants