Skip to content

.NET: Adding AgentRunContext to allow accessing agent run info in external downstream components - #3476

Merged
westey (westey-m) merged 19 commits into
microsoft:mainfrom
westey-m:agentruncontext
Feb 5, 2026
Merged

.NET: Adding AgentRunContext to allow accessing agent run info in external downstream components#3476
westey (westey-m) merged 19 commits into
microsoft:mainfrom
westey-m:agentruncontext

Conversation

@westey-m

Copy link
Copy Markdown
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.

CopilotAI review requested due to automatic review settings January 28, 2026 11:41
@markwallace-microsoftMark Wallace (markwallace-microsoft) added documentation Usage: [Issues, PRs], Target: documentation in the code base and learn docs .NET Usage: [Issues, PRs], Target: .Net labels Jan 28, 2026
@github-actionsgithub-actionsBot 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 componentsJan 28, 2026

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

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
FileDescription
dotnet/src/Microsoft.Agents.AI.Abstractions/AgentRunContext.csNew sealed class providing read-only access to agent run information
dotnet/src/Microsoft.Agents.AI.Abstractions/AIAgent.csAdded AsyncLocal field and CurrentRunContext property; modified RunAsync/RunStreamingAsync to set context
dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/AgentRunContextTests.csComprehensive unit tests for AgentRunContext constructor validation and property accessors
dotnet/tests/Microsoft.Agents.AI.Abstractions.UnitTests/AIAgentTests.csAdded tests verifying CurrentRunContext is set for all RunAsync/RunStreamingAsync overloads; updated TestAgentSession visibility
docs/decisions/0015-agent-run-context.mdArchitectural decision record explaining design choices and trade-offs

Comment threaddocs/decisions/0015-agent-run-context.md Outdated
Comment threaddocs/decisions/0015-agent-run-context.md Outdated
Comment threaddocs/decisions/0015-agent-run-context.md Outdated
Comment threaddotnet/src/Microsoft.Agents.AI.Abstractions/AgentRunContext.cs Outdated
Comment threaddocs/decisions/0015-agent-run-context.md Outdated
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentationUsage: [Issues, PRs], Target: documentation in the code base and learn docs.NETUsage: [Issues, PRs], Target: .Net

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@westey-m@rogerbarreto@SergeyMenshykh@markwallace-microsoft