Skip to content

Python: Add SkillsSourceContext to SkillsSource.get_skills#6895

Open
giles17 wants to merge 3 commits into
microsoft:mainfrom
giles17:skills-source-context
Open

Python: Add SkillsSourceContext to SkillsSource.get_skills#6895
giles17 wants to merge 3 commits into
microsoft:mainfrom
giles17:skills-source-context

Conversation

@giles17

@giles17 giles17 commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Motivation & Context

SkillsSource.get_skills took no arguments, so skill sources and decorators had no access to the invocation context. This prevented context-aware behavior such as filtering skills per agent or isolating the skills cache per agent/tenant.

This adds a SkillsSourceContext (the invoking agent plus optional session) that the skills provider builds and threads through the entire source pipeline, so sources, filters, and caching can make context-aware decisions.

Python port of the .NET change in #6797.

Description & Review Guide

  • What are the major changes?

    • New frozen, experimental SkillsSourceContext(agent, session).
    • SkillsSource.get_skills and all sources/decorators (File, InMemory, Delegating, Deduplicating, Filtering, Caching, Aggregating, MCP) now accept and forward a SkillsSourceContext.
    • FilteringSkillsSource predicate is now context-aware: Callable[[Skill, SkillsSourceContext], bool].
    • CachingSkillsSource gains an optional cache_isolation_key_selector for per-key cache isolation; a None selector (the default) keeps the existing shared-cache behavior.
    • SkillsProvider builds the context from before_run's agent/session and passes it into the pipeline.
    • SkillsSourceContext is exported from the public API; foundry_hosting's toolbox skill source, tests, and docs are updated accordingly.
  • What is the impact of these changes?

    • Enables per-agent/session skill selection and per-key cache isolation.
    • Signature changes touch the @experimental skills API only. Default behavior is unchanged: context-ignoring leaf sources and the default (unkeyed) cache behave exactly as before.
  • What do you want reviewers to focus on?

    • The CachingSkillsSource rewrite from a single shared slot to per-key buckets with per-key locks (double-checked locking, retry-on-failure preserved), and that the default path stays equivalent to the previous single-bucket behavior.

Related Issue

Fixes #6711

Contribution Checklist

  • The code builds clean without any errors or warnings
  • All unit tests pass, and I have added new tests where possible
  • The PR follows the Contribution Guidelines
  • This PR is linked to an issue and there is no other open PR for this issue (see Related Issue above).
  • This is not a breaking change. If it is a breaking change, add the breaking change label (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.

Thread an invocation context (agent + optional session) through the skill
source pipeline so sources and decorators can make context-aware decisions.

- Add frozen, experimental SkillsSourceContext(agent, session).
- Change SkillsSource.get_skills and all sources/decorators to accept and
  forward the context.
- Make FilteringSkillsSource predicate context-aware: (skill, context) -> bool.
- Add optional cache_isolation_key_selector to CachingSkillsSource for
  per-key cache isolation (None keeps the shared-bucket behavior).
- Build the context in SkillsProvider from before_run agent/session.
- Update foundry_hosting toolbox source, exports, tests, and docs.

Python port of .NET PR microsoft#6797.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings July 2, 2026 17:46
@giles17 giles17 added documentation Usage: [Issues, PRs], Target: documentation in the code base and learn docs python Usage: [Issues, PRs], Target: Python labels Jul 2, 2026
@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/core/agent_framework
   _skills.py10693896%315, 588, 1108, 1123, 1125–1126, 1493–1494, 1738, 1767, 2311, 2806–2807, 2904, 2912, 2917, 2920, 2925, 2945, 2957, 2962, 3057, 3065, 3070, 3073, 3078, 3098, 3107, 3112, 3378–3379, 3879, 4112–4113, 4140–4141, 4148–4149
packages/foundry_hosting/agent_framework_foundry_hosting
   _toolbox.py620100% 
TOTAL43402518788% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
8549 33 💤 0 ❌ 0 🔥 2m 13s ⏱️

@github-actions github-actions Bot 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: 5 | Confidence: 91% | Result: All clear

Reviewed: Correctness, Security Reliability, Test Coverage, Failure Modes, Design Approach


Automated review by giles17's agents

Copilot AI 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 updates the Python skills “source pipeline” API to be invocation-context aware by introducing SkillsSourceContext (invoking agent + optional session) and threading it through SkillsSource.get_skills and all source/decorator implementations. This enables context-aware filtering and optional per-key cache isolation while preserving the default shared-cache behavior.

Changes:

  • Add experimental, frozen SkillsSourceContext(agent, session) and update SkillsSource.get_skills(context) across all sources/decorators.
  • Extend FilteringSkillsSource to use a context-aware predicate: Callable[[Skill, SkillsSourceContext], bool].
  • Rework CachingSkillsSource to support optional per-key cache isolation (default remains a single shared bucket), and update docs/tests/hosting integration accordingly.

Reviewed changes

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

Show a summary per file
File Description
python/packages/foundry_hosting/tests/test_toolbox.py Updates toolbox source tests to pass SkillsSourceContext into get_skills.
python/packages/foundry_hosting/agent_framework_foundry_hosting/_toolbox.py Updates hosted toolbox SkillsSource wrapper to accept/forward SkillsSourceContext.
python/packages/core/tests/core/test_skills.py Updates core skills tests for new get_skills(context) API; adds context propagation + cache isolation tests.
python/packages/core/tests/core/test_mcp_skills.py Updates MCP skills source tests to pass SkillsSourceContext.
python/packages/core/AGENTS.md Documents the new context-aware get_skills contract, predicate signature, and cache isolation behavior.
python/packages/core/agent_framework/_skills.py Implements SkillsSourceContext, updates all sources/decorators to accept/forward context, and adds per-key caching buckets/locks.
python/packages/core/agent_framework/init.py Exports SkillsSourceContext from the public API.

Comment thread python/packages/core/agent_framework/_skills.py
Comment thread python/packages/core/agent_framework/_skills.py
Comment thread python/packages/core/agent_framework/_skills.py
Comment thread python/packages/core/agent_framework/_skills.py Outdated
Comment thread python/packages/core/agent_framework/_skills.py
Comment thread python/packages/core/agent_framework/_skills.py
giles17 and others added 2 commits July 2, 2026 11:19
Address PR review: docstring examples referenced `context` without
constructing it. Add a `SkillsSourceContext` construction line (with a
placeholder agent) to each source example and a note that the provider
normally supplies it. Use `source_context` in the FilteringSkillsSource
example to avoid clashing with the predicate's `context` parameter.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Address CI failures from the SkillsSourceContext change:
- Update the skill_filtering sample to the 2-arg predicate signature
  (skill, context); the old 1-arg lambda would fail at runtime.
- Replace ad-hoc _StubAgent test stubs with the shared MockAgent /
  MockAgentSession from conftest so all type checkers (incl. ty) accept
  the SupportsAgentRun-typed agent. Add a small _NamedMockAgent subclass
  for tests needing distinct agent names, and drop now-unnecessary
  attr-defined ignores.
- Use cast(SupportsAgentRun, ...) in foundry_hosting tests, which have no
  shared mock infrastructure.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@giles17 giles17 marked this pull request as ready for review July 2, 2026 20:36

@github-actions github-actions Bot 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: 5 | Confidence: 89% | Result: All clear

Reviewed: Correctness, Security Reliability, Test Coverage, Failure Modes, Design Approach


Automated review by giles17's agents

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Usage: [Issues, PRs], Target: documentation in the code base and learn docs python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: Add AgentSkillsSourceContext to AgentSkillsSource.get_skills

2 participants