Skip to content

.NET: Add ILoggerFactory and IServiceProvider to HarnessAgent constructor#6273

Open
westey-m wants to merge 2 commits into
microsoft:mainfrom
westey-m:harnessagent-logger-sp
Open

.NET: Add ILoggerFactory and IServiceProvider to HarnessAgent constructor#6273
westey-m wants to merge 2 commits into
microsoft:mainfrom
westey-m:harnessagent-logger-sp

Conversation

@westey-m
Copy link
Copy Markdown
Contributor

@westey-m westey-m commented Jun 2, 2026

Summary

Adds optional ILoggerFactory and IServiceProvider parameters to the HarnessAgent constructor and AsHarnessAgent extension method, passing them through to all downstream components that accept them.

Components receiving ILoggerFactory

  • FunctionInvokingChatClient (via UseFunctionInvocation)
  • CompactionProvider
  • AgentSkillsProvider
  • ChatClientAgent (via BuildAIAgent)

Components receiving IServiceProvider

  • ChatClientAgent (via BuildAIAgent)
  • AIAgentBuilder.Build()

Changes

  • HarnessAgent.cs — added parameters to constructor and internal build methods
  • ChatClientHarnessExtensions.cs — added parameters to AsHarnessAgent
  • HarnessAgentTests.cs — added unit tests for new parameters

Both parameters are optional with null defaults, making this a non-breaking change.

Closes #6103

Add optional ILoggerFactory and IServiceProvider parameters to the
HarnessAgent constructor and AsHarnessAgent extension method, passing
them to all downstream components that accept them:

- FunctionInvokingChatClient (via UseFunctionInvocation)
- CompactionProvider
- AgentSkillsProvider
- ChatClientAgent (via BuildAIAgent)
- AIAgentBuilder.Build()

Closes microsoft#6103

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 2, 2026 17:10
@moonbox3 moonbox3 added the .NET label Jun 2, 2026
@github-actions github-actions Bot changed the title Add ILoggerFactory and IServiceProvider to HarnessAgent constructor .NET: Add ILoggerFactory and IServiceProvider to HarnessAgent constructor Jun 2, 2026
Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

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

Automated Code Review

Reviewers: 4 | Confidence: 82%

✓ Correctness

The PR correctly plumbs optional ILoggerFactory and IServiceProvider parameters through the HarnessAgent constructor and AsHarnessAgent extension method to all downstream components. All verified API signatures (CompactionProvider, AgentSkillsProvider, BuildAIAgent, AIAgentBuilder.Build) match the parameter passing in the diff. The changes are non-breaking since all new parameters are optional with null defaults.

✓ Security Reliability

This PR adds optional ILoggerFactory and IServiceProvider parameters to the HarnessAgent constructor and AsHarnessAgent extension method, passing them to downstream components. All downstream APIs (CompactionProvider, AgentSkillsProvider, UseFunctionInvocation, BuildAIAgent, AIAgentBuilder.Build) already accept these as nullable optional parameters with safe null-fallback behavior (e.g., NullLoggerFactory.Instance, EmptyServiceProvider.Instance). The change is non-breaking and introduces no security or reliability concerns.

✓ Test Coverage

The new tests verify that the constructor and extension method don't throw when ILoggerFactory and IServiceProvider are provided, which is consistent with the 'Constructor_SucceedsWhenOptionsIsNull' pattern. However, the test file extensively uses GetService<>() to verify internal wiring (e.g., verifying FunctionInvokingChatClient configuration, context providers present). The new tests only assert Assert.NotNull(agent) without verifying that the logerFactory/services are actually threaded through to downstream components. Additionally, all tests use CreateAllDisabledOptions() which disables AgentSkillsProvider—one of the documented consumers of loggerFactory—so that code path is never exercised with a non-null logerFactory.

✓ Design Approach

I did not find a design-approach issue in the changed harness plumbing. The new parameters are threaded into the relevant builder/agent seams that already accept them (AIAgentBuilder.Build(IServiceProvider?), ChatClientBuilderExtensions.BuildAIAgent(..., ILoggerFactory?, IServiceProvider?), and ChatClientAgent(..., ILoggerFactory?, IServiceProvider?)), and the updated flow is consistent with how the existing agent stack resolves logging and DI.


Automated review by westey-m's agents

Copy link
Copy Markdown
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 extends the .NET HarnessAgent creation APIs to accept optional ILoggerFactory and IServiceProvider, and threads those values through the internal build pipeline so downstream components can use logging and DI when available.

Changes:

  • Added optional ILoggerFactory and IServiceProvider parameters to HarnessAgent construction and internal build helpers.
  • Updated AsHarnessAgent extension method to accept and forward the same parameters.
  • Added unit tests covering construction paths with the new parameters.

Reviewed changes

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

File Description
dotnet/src/Microsoft.Agents.AI.Harness/HarnessAgent.cs Adds ILoggerFactory/IServiceProvider parameters and forwards them to builder + context providers.
dotnet/src/Microsoft.Agents.AI.Harness/ChatClientHarnessExtensions.cs Extends AsHarnessAgent to accept and forward the new optional parameters.
dotnet/tests/Microsoft.Agents.AI.Harness.UnitTests/HarnessAgentTests.cs Adds tests for new construction/extension-method parameter combinations.

Comment thread dotnet/src/Microsoft.Agents.AI.Harness/HarnessAgent.cs
- Add test verifying ILoggerFactory.CreateLogger() is called by
  downstream components (CompactionProvider, AgentSkillsProvider)
- Add test verifying IServiceProvider is queried during pipeline build

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.NET: [Feature]: Add parameters ILoggerFactory and IServiceProvider to the HarnessAgent constructor

5 participants