.NET: Improve skills validation: reject consecutive hyphens and name-directory mismatches#4524
.NET: Improve skills validation: reject consecutive hyphens and name-directory mismatches#4524SergeyMenshykh wants to merge 1 commit intomicrosoft:mainfrom
Conversation
…ory mismatches - Update name regex to reject consecutive hyphens (e.g., 'my--skill') - Add validation that skill name matches its parent directory name - Add LogNameDirectoryMismatch log message for diagnostic clarity - Update error message to mention consecutive hyphens restriction Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR makes two improvements:
- Skills validation enhancements in
FileAgentSkillLoader: the name regex now also rejects consecutive hyphens (e.g.my--skill), and a new check enforces that the skill's frontmatternamematches its parent directory name. - Hosting lifetime API (unmentioned in the PR description): adds a
ServiceLifetime lifetimeparameter to everyAddAIAgent,AddWorkflow,AddAsAIAgent,WithAITool(factory), andWithSessionStore(factory)overload; adds aLifetimeproperty to the publicIHostedAgentBuilderinterface; and introduces anAddKeyedService<T>internal helper. AValidateToolLifetimeguard prevents captive-dependency registrations for tools.
Changes:
- Regex update to reject consecutive hyphens and new name-directory mismatch check in
FileAgentSkillLoader ServiceLifetimeparameter added to all hosting registration overloads in Hosting source files- Corresponding tests added and existing tests updated for both skills validation and lifetime API
Reviewed changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
dotnet/src/Microsoft.Agents.AI/Skills/FileAgentSkillLoader.cs |
Updated name regex and added name-directory mismatch validation and log message |
dotnet/src/Microsoft.Agents.AI.Hosting/IHostedAgentBuilder.cs |
Added Lifetime property to the public interface |
dotnet/src/Microsoft.Agents.AI.Hosting/HostedAgentBuilder.cs |
Implements the new Lifetime property |
dotnet/src/Microsoft.Agents.AI.Hosting/AgentHostingServiceCollectionExtensions.cs |
Adds lifetime param to all AddAIAgent overloads; adds AddKeyedService<T> helper |
dotnet/src/Microsoft.Agents.AI.Hosting/HostApplicationBuilderAgentExtensions.cs |
Adds lifetime param to host-builder AddAIAgent overloads |
dotnet/src/Microsoft.Agents.AI.Hosting/HostApplicationBuilderWorkflowExtensions.cs |
Adds lifetime param to AddWorkflow |
dotnet/src/Microsoft.Agents.AI.Hosting/HostedAgentBuilderExtensions.cs |
Adds lifetime to WithAITool(factory) and WithSessionStore(factory); adds ValidateToolLifetime |
dotnet/src/Microsoft.Agents.AI.Hosting/HostedWorkflowBuilderExtensions.cs |
Adds lifetime to AddAsAIAgent overloads |
dotnet/tests/Microsoft.Agents.AI.UnitTests/AgentSkills/FileAgentSkillLoaderTests.cs |
Adds test for consecutive hyphens, name-directory mismatch; updates duplicate-name test |
dotnet/tests/Microsoft.Agents.AI.Hosting.UnitTests/AgentHostingServiceCollectionExtensionsTests.cs |
Tests for scoped/transient lifetime registration and default lifetime |
dotnet/tests/Microsoft.Agents.AI.Hosting.UnitTests/HostApplicationBuilderAgentExtensionsTests.cs |
Tests for scoped/transient/lifetime-overload via host builder |
dotnet/tests/Microsoft.Agents.AI.Hosting.UnitTests/HostApplicationBuilderWorkflowExtensionsTests.cs |
Tests for workflow scoped/transient and AddAsAIAgent lifetime |
dotnet/tests/Microsoft.Agents.AI.Hosting.UnitTests/HostedAgentBuilderToolsExtensionsTests.cs |
Tests for tool lifetime defaults, overrides, captive dependency guards |
| /// <summary> | ||
| /// Gets the DI service lifetime used for the agent registration. | ||
| /// </summary> | ||
| ServiceLifetime Lifetime { get; } |
There was a problem hiding this comment.
The PR title and description focus exclusively on "improving skills validation" (consecutive hyphens, name-directory mismatch). However, the bulk of the changes — adding a ServiceLifetime parameter to every AddAIAgent, AddWorkflow, AddAsAIAgent, WithAITool, and WithSessionStore overload, plus adding the Lifetime property to the public IHostedAgentBuilder interface — are not mentioned in the PR description at all. These are substantial API changes that affect the public surface of the library and deserve their own description, including any breaking-change implications for consumers who implement IHostedAgentBuilder.
| /// <summary> | ||
| /// Gets the DI service lifetime used for the agent registration. | ||
| /// </summary> | ||
| ServiceLifetime Lifetime { get; } |
There was a problem hiding this comment.
Adding Lifetime to the public IHostedAgentBuilder interface is a breaking change. Any external code that implements this interface (e.g. for testing or custom agent builders) will fail to compile until it adds the new property. While the only known implementation in this repository is HostedAgentBuilder (internal), the interface is public and consumers may implement it. Consider whether this should have a default interface member implementation (e.g. ServiceLifetime Lifetime => ServiceLifetime.Singleton;) to preserve source compatibility for existing implementors, or whether a new IHostedAgentBuilderV2 interface is more appropriate. This is especially relevant given the library targets multi-frameworks (net8/net9/net10 + netstandard2.0/net472) — note that default interface members are not supported on netstandard2.0/net472.
Changes
my--skill)namein frontmatter matches its parent directory nameLogNameDirectoryMismatchfor clear diagnostics when names don't matchTesting
All 484 unit tests pass, including
FileAgentSkillLoaderTests. Full solution builds with 0 warnings/errors.