.NET: Add AgentSkillsSourceContext to AgentSkillsSource.GetSkillsAsync#6797
Conversation
Pass agent/session context through the skills retrieval pipeline so sources, filters, and caching can make context-aware decisions. - AgentSkillsSourceContext (Agent, Session) is built by AgentSkillsProvider from the InvokingContext and flows through all sources and decorators. - FilteringAgentSkillsSource predicate now receives an AgentSkillFilterContext bundling the skill and the source context. - CachingAgentSkillsSource supports per-key isolation via CachingAgentSkillsSourceOptions.CacheIsolationKeySelector; a null selector preserves the shared-cache behavior. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR evolves the .NET “Agent Skills” pipeline to be context-aware by threading an AgentSkillsSourceContext (agent + optional session) through AgentSkillsSource.GetSkillsAsync, enabling downstream decorators (filtering/caching) and sources (including MCP) to make invocation-specific decisions.
Changes:
- Add
AgentSkillsSourceContextand update allAgentSkillsSource.GetSkillsAsyncimplementations/callers to accept it. - Update filtering to pass an
AgentSkillFilterContextinto the predicate so filters can consider both the skill and the source context. - Extend caching to support per-key cache isolation via
CachingAgentSkillsSourceOptions.CacheIsolationKeySelector, and update unit tests accordingly.
Reviewed changes
Copilot reviewed 32 out of 32 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| dotnet/src/Microsoft.Agents.AI/Skills/AgentSkillsSource.cs | Updates the core skills source abstraction to require a source context. |
| dotnet/src/Microsoft.Agents.AI/Skills/AgentSkillsSourceContext.cs | Introduces the new context object (agent + optional session). |
| dotnet/src/Microsoft.Agents.AI/Skills/AgentSkillsProvider.cs | Constructs and passes the source context from the invoking context. |
| dotnet/src/Microsoft.Agents.AI/Skills/AgentSkillsProviderBuilder.cs | Updates filter signature and adds caching options configuration. |
| dotnet/src/Microsoft.Agents.AI/Skills/AgentSkillFilterContext.cs | Adds the filter predicate context wrapper (skill + source context). |
| dotnet/src/Microsoft.Agents.AI/Skills/AggregatingAgentSkillsSource.cs | Threads the source context through aggregated sources. |
| dotnet/src/Microsoft.Agents.AI/Skills/AgentInMemorySkillsSource.cs | Updates in-memory source signature to accept the context. |
| dotnet/src/Microsoft.Agents.AI/Skills/File/AgentFileSkillsSource.cs | Updates file-based source signature to accept the context. |
| dotnet/src/Microsoft.Agents.AI/Skills/Decorators/DelegatingAgentSkillsSource.cs | Updates delegating decorator base to forward the context. |
| dotnet/src/Microsoft.Agents.AI/Skills/Decorators/FilteringAgentSkillsSource.cs | Updates filtering decorator to build/pass AgentSkillFilterContext. |
| dotnet/src/Microsoft.Agents.AI/Skills/Decorators/DeduplicatingAgentSkillsSource.cs | Updates dedup decorator to pass context through to inner source. |
| dotnet/src/Microsoft.Agents.AI/Skills/Decorators/CachingAgentSkillsSource.cs | Implements per-isolation-key caching using a concurrent dictionary. |
| dotnet/src/Microsoft.Agents.AI/Skills/Decorators/CachingAgentSkillsSourceOptions.cs | Adds options type for cache isolation key selection. |
| dotnet/src/Microsoft.Agents.AI/Microsoft.Agents.AI.csproj | Adds InternalsVisibleTo for MCP unit tests (to use internal context ctor). |
| dotnet/src/Microsoft.Agents.AI.Mcp/Skills/AgentMcpSkillsSource.cs | Updates MCP skills source signature and flows context into loaders. |
| dotnet/src/Microsoft.Agents.AI.Mcp/Skills/Loaders/IMcpSkillEntryLoader.cs | Updates loader interface to accept AgentSkillsSourceContext. |
| dotnet/src/Microsoft.Agents.AI.Mcp/Skills/Loaders/SkillMdEntryLoader.cs | Updates loader signature to accept context. |
| dotnet/src/Microsoft.Agents.AI.Mcp/Skills/Loaders/ArchiveEntryLoader.cs | Updates loader signature; passes context into extracted file source discovery. |
| dotnet/tests/Microsoft.Agents.AI.UnitTests/TestAgentSkillsSourceContextFactory.cs | Adds a test-only helper for constructing a skills source context. |
| dotnet/tests/Microsoft.Agents.AI.UnitTests/AgentSkills/TestSkillTypes.cs | Updates test source overrides to match the new signature. |
| dotnet/tests/Microsoft.Agents.AI.UnitTests/AgentSkills/FilteringAgentSkillsSourceTests.cs | Updates/extends tests for filter predicate context plumbing. |
| dotnet/tests/Microsoft.Agents.AI.UnitTests/AgentSkills/DeduplicatingAgentSkillsSourceTests.cs | Updates tests for new signature. |
| dotnet/tests/Microsoft.Agents.AI.UnitTests/AgentSkills/CachingAgentSkillsSourceTests.cs | Updates and adds tests for cache isolation keys. |
| dotnet/tests/Microsoft.Agents.AI.UnitTests/AgentSkills/AgentSkillsProviderTests.cs | Adds coverage ensuring agent/session flow into the source context. |
| dotnet/tests/Microsoft.Agents.AI.UnitTests/AgentSkills/AgentSkillsProviderBuilderTests.cs | Adds tests for caching options and updates filter signature usage. |
| dotnet/tests/Microsoft.Agents.AI.UnitTests/AgentSkills/AgentInMemorySkillsSourceTests.cs | Updates tests for new signature. |
| dotnet/tests/Microsoft.Agents.AI.UnitTests/AgentSkills/FileAgentSkillLoaderTests.cs | Updates file loader tests to pass a context. |
| dotnet/tests/Microsoft.Agents.AI.UnitTests/AgentSkills/AgentFileSkillsSourceScriptTests.cs | Updates script discovery/execution tests to pass a context. |
| dotnet/tests/Microsoft.Agents.AI.UnitTests/AgentSkills/AgentClassSkillTests.cs | Updates class-skill tests to pass a context. |
| dotnet/tests/Microsoft.Agents.AI.Mcp.UnitTests/TestAgentSkillsSourceContextFactory.cs | Adds a MCP-test-specific context factory (custom agent type). |
| dotnet/tests/Microsoft.Agents.AI.Mcp.UnitTests/Skills/AgentMcpSkillsSourceTests.cs | Updates MCP source tests for new signature. |
| dotnet/tests/Microsoft.Agents.AI.Mcp.UnitTests/Skills/AgentMcpSkillsSourceArchiveTests.cs | Updates MCP archive tests for new signature. |
There was a problem hiding this comment.
Automated Code Review
Reviewers: 5 | Confidence: 89%
✓ Correctness
No actionable issues found in this dimension.
✓ Security Reliability
The PR cleanly threads AgentSkillsSourceContext through the skills pipeline. The main reliability concern is that the new CachingAgentSkillsSource uses a ConcurrentDictionary that grows without bound when CacheIsolationKeySelector produces many distinct keys (e.g., per-session), creating a potential memory leak in long-running services. The concurrency patterns are otherwise correct, and input validation is properly applied to the new context types.
✓ Test Coverage
The PR has solid test coverage for its core new behaviors: CachingAgentSkillsSource isolation keys are tested with same-key, different-key, and null-key scenarios; FilteringAgentSkillsSource has a dedicated test verifying context is passed to predicates; and AgentSkillsProviderBuilder has an integration test for cache isolation per agent. All existing tests are appropriately updated to pass the new context parameter. The only notable gap is that all test factory helpers use
session: null, meaning no test exercises context-aware decisions based on a non-null Session value, though this is a minor concern given the Session property is simply a pass-through.
✓ Failure Modes
No actionable issues found in this dimension.
✗ Design Approach
That closes off direct use of
AgentSkillsSourceoutside friend assemblies, which undercuts the extensibility this PR is trying to add.
Flagged Issues
-
AgentSkillsSource.GetSkillsAsyncnow requiresAgentSkillsSourceContext(dotnet/src/Microsoft.Agents.AI/Skills/AgentSkillsSource.cs:24), butAgentSkillsSourceContext's only constructor isinternal(dotnet/src/Microsoft.Agents.AI/Skills/AgentSkillsSourceContext.cs:21). External consumers can still derive or receive anAgentSkillsSource, but they can no longer call its public API directly or write non-friend tests for it.
Automated review by SergeyMenshykh's agents
|
Flagged issue
Source: automated DevFlow PR review |
- Make the AgentSkillsSourceContext constructor public so external callers can invoke AgentSkillsSource.GetSkillsAsync directly; drop the Mcp.UnitTests InternalsVisibleTo entry it required. - Use a dedicated sentinel cache key for the shared bucket so an isolation selector returning an empty string gets its own bucket. - Document cache-key cardinality guidance and baseline the experimental API breaking changes in CompatibilitySuppressions.xml. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
6cf2687 to
7e89d53
Compare
Replace the AgentSkillFilterContext bundle with a Func<AgentSkill, AgentSkillsSourceContext, bool> predicate in FilteringAgentSkillsSource and AgentSkillsProviderBuilder.UseFilter, and update the tests accordingly. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Summary
Adds an
AgentSkillsSourceContext(carryingAgentand optionalSession) toAgentSkillsSource.GetSkillsAsync, so skill sources, filters, and caching can make context-aware decisions. The context is built byAgentSkillsProviderfrom theInvokingContextand flows through all sources and decorators.Changes
AgentSkillsSourceContextexposesAgentandSession, supplied from the invoking context.FilteringAgentSkillsSourcepredicate now receives anAgentSkillFilterContextbundling the skill and the source context.CachingAgentSkillsSourcesupports per-key cache isolation viaCachingAgentSkillsSourceOptions.CacheIsolationKeySelector; a null selector keeps the existing shared-cache behavior.Closes: #6710