Skip to content

.NET: Add subagents provider and sample - #5518

Merged
westey (westey-m) merged 2 commits into
microsoft:feature-harnessfrom
westey-m:harness-subagents-with-sample
Apr 28, 2026
Merged

.NET: Add subagents provider and sample#5518
westey (westey-m) merged 2 commits into
microsoft:feature-harnessfrom
westey-m:harness-subagents-with-sample

Conversation

@westey-m

Copy link
Copy Markdown
Contributor

Motivation and Context

Description

  • Add a provider to allow delegating tasks to sub agents and following up on them later.
  • Add a sample that demonstrates usage of this provider.

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 April 27, 2026 18:19
@moonbox3Evan Mattson (moonbox3) added documentation Usage: [Issues, PRs], Target: documentation in the code base and learn docs .NET Usage: [Issues, PRs], Target: .Net labels Apr 27, 2026
@github-actionsgithub-actionsBot changed the title Add subagents provider and sample.NET: Add subagents provider and sampleApr 27, 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

Adds a new .NET Harness AIContextProvider that lets an agent delegate work to named sub-agents asynchronously (start tasks, wait for completion, fetch results, continue, and clear), plus a new Harness sample demonstrating concurrent delegation to a web-search sub-agent.

Changes:

  • Introduce SubAgentsProvider + supporting state/types (SubTaskInfo, SubTaskStatus, options, session/runtime state) and register them for JSON source-gen.
  • Add comprehensive unit tests for sub-agent task lifecycle behaviors.
  • Add a new Harness sample (Harness_Step02_Research_WithSubAgents) and update shared console formatting to recognize the new tool names.

Reviewed changes

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

Show a summary per file
FileDescription
dotnet/src/Microsoft.Agents.AI/Harness/SubAgents/SubAgentsProvider.csNew provider exposing sub-agent delegation tools and session-backed state.
dotnet/src/Microsoft.Agents.AI/Harness/SubAgents/SubAgentsProviderOptions.csOptions for custom instructions and agent list formatting.
dotnet/src/Microsoft.Agents.AI/Harness/SubAgents/SubTaskInfo.csSerializable task metadata/result container.
dotnet/src/Microsoft.Agents.AI/Harness/SubAgents/SubTaskStatus.csTask status enum (Running/Completed/Failed/Lost).
dotnet/src/Microsoft.Agents.AI/Harness/SubAgents/SubAgentState.csSerializable session state (next id + task list).
dotnet/src/Microsoft.Agents.AI/Harness/SubAgents/SubAgentRuntimeState.csRuntime-only references for in-flight tasks and sub-sessions.
dotnet/src/Microsoft.Agents.AI/AgentJsonUtilities.csRegisters new sub-agent types for JSON source generation.
dotnet/tests/Microsoft.Agents.AI.UnitTests/Harness/SubAgents/SubAgentsProviderTests.csNew tests covering construction, tool behaviors, and run-context isolation.
dotnet/samples/02-agents/Harness/README.mdAdds the new sample to the Harness samples list.
dotnet/samples/02-agents/Harness/Harness_Step02_Research_WithSubAgents/README.mdDocuments the new sub-agent sample scenario and setup.
dotnet/samples/02-agents/Harness/Harness_Step02_Research_WithSubAgents/Program.csImplements the sample: parent agent delegates lookups to web-search sub-agent.
dotnet/samples/02-agents/Harness/Harness_Step02_Research_WithSubAgents/Harness_Step02_Research_WithSubAgents.csprojAdds the new sample project.
dotnet/samples/02-agents/Harness/Harness_Shared_Console/ToolCallFormatter.csUpdates tool name mappings for sub-agent tool calls.
dotnet/samples/02-agents/Harness/Harness_Shared_Console/HarnessConsole.csImproves command listing and mode display when mode provider is absent.
dotnet/agent-framework-dotnet.slnxAdds the new sample project to the solution.

Comment threaddotnet/src/Microsoft.Agents.AI/Harness/SubAgents/SubAgentsProvider.cs Outdated
Comment threaddotnet/src/Microsoft.Agents.AI/Harness/SubAgents/SubAgentsProvider.cs Outdated
Comment threaddotnet/src/Microsoft.Agents.AI/AgentJsonUtilities.cs Outdated

@github-actionsgithub-actionsBot 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.

Automated Code Review

Reviewers: 3 | Confidence: 92%

✓ Correctness

The SubAgentsProvider implementation is well-structured and follows the established patterns of existing providers (TodoProvider, FileMemoryProvider). The core logic for managing sub-tasks—starting, waiting, retrieving results, continuing, and clearing—is correct. The Task.Run wrapping for ExecutionContext isolation is justified (RunAsync at AIAgent.cs:340 synchronously sets the static AsyncLocal CurrentRunContext before returning RunCoreAsync). The runtime state mechanism works correctly via the StateBag's deserialized object cache, which preserves in-memory references across ProvideAIContextAsync calls. State serialization correctly marks non-serializable runtime data with [JsonIgnore] and handles the 'Lost' status for tasks that survive a restart. No correctness bugs found.

✓ Security Reliability

The SubAgentsProvider is well-structured and follows established patterns in the codebase (TodoProvider, AgentModeProvider). The Task.Run wrapping for ExecutionContext isolation is correct and well-documented. Tool call serialization by FunctionInvokingChatClient ensures List mutations are safe. State management via ProviderSessionState with the AgentSessionStateBagValue cache preserves in-memory runtime references correctly. No blocking security or reliability issues were found. The code handles error paths (invalid agent names, nonexistent tasks, running-task guards) consistently. Two minor suggestions are offered as incremental improvements.

✗ Design Approach

The overall sub-agent state model is workable, but the execution path is wired through a new ad-hoc child-run mechanism instead of the framework’s existing child-agent invocation path. That bypasses parent-run option propagation, so sub-agents can silently lose per-request behavior that the rest of the agent stack expects to flow into nested calls.

Flagged Issues

  • Sub-agent runs are started with raw agent.RunAsync(...) calls instead of the framework's existing delegation/helper path, which drops FunctionInvokingChatClient run properties for child runs. In the current codebase, those propagated AdditionalProperties are already used to alter child behavior such as chat-history resolution (dotnet/src/Microsoft.Agents.AI/AgentExtensions.cs:76-81, dotnet/src/Microsoft.Agents.AI/ChatClient/ChatClientAgent.cs:964-979).

Automated review by westey-m's agents

@westey-m
westey (westey-m) merged commit 899394a into microsoft:feature-harnessApr 28, 2026
12 checks passed
pullBot pushed a commit to nagyist/ms-agent-framework that referenced this pull request May 1, 2026
* .NET: Add a TODO AIContextProvider (microsoft#5233)
* Add a TODO AIContextProvider
* Add unit tests
* Address PR comments
* Address PR comments
* Fix test after removing one tool
* .NET: Add a ModeProvider for managing agent modes (microsoft#5247)
* Add a ModeProvider for managing agent modes
* Fix typo
* Fix typo
* Fix typo
* Address PR comments
* .NET: Add sample to show how to build a harness (microsoft#5268)
* Add sample to show how to build a harness
* Improve sample
* Sample max output tokens and model
* Fix encoding
* Fix model name in readme
* Address PR comments
* .NET: Add context window size compaction strategy for harness (microsoft#5304)
* Add context window size compaction strategy for harness
* Apply suggestions from code review
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* Address PR comments
---------
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
* .NET: Add a file memory provider (microsoft#5315)
* Add a file memory provider
* Address PR comments
* Fix review comments.
* Add additional unit tests
* Addressing PR comments.
* .NET: Harness: Improve prompts and add FileSystem store (microsoft#5365)
* Harness: Improve prompts and add FileSystem store
* Address PR comments
* .NET: Harness: Improve path validation (microsoft#5404)
* Harness: Improve path validation
* Address PR comments
* .NET: Add always approve helpers, improve sample and fix bug (microsoft#5451)
* Add always approve helpers, improve sample and fix bug
* Address PR comments
* .NET: Make Todo, Mode and FileMemory providers more configurable (microsoft#5477)
* Make Todo, Mode and FileMemory providers more configurable
* Address PR comments.
* .NET: Add subagents provider and sample (microsoft#5518)
* Add subagents provider and sample
* Addressing PR comments.
* .NET: Harness filememory index plus instructions consistency (microsoft#5540)
* Add FileMemoryProvider index and improve instruction consistency
* Address PR comments.
* Address PR comments
* Address PR comments.
* Apply suggestion from @rogerbarreto
Co-authored-by: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com>
---------
Co-authored-by: Roger Barreto <19890735+rogerbarreto@users.noreply.github.com>
* .NET: Refactor harness console to be more extensible and easy to understand with better UX (microsoft#5573)
* Refactor harness console to be more extensible and easy to understand with better UX.
* Fix formatting issues.
* Allow multiple clarifications in one response
* Address PR comments
* .NET: Add FileAccessProvdider and concurrency fix for FileMemoryProvider (microsoft#5583)
* Add FileAccessProvdider and concurrency fix for FileMemoryProvider
* Address PR comments
---------
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Roger Barreto <19890735+rogerbarreto@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.

4 participants

@westey-m@SergeyMenshykh@moonbox3