Skip to content

Python: Add opt-in FunctionInvocationContext injection for skill scripts - #8749

Merged
SergeyMenshykh merged 8 commits into
microsoft:mainfrom
SergeyMenshykh:sergeymenshykh-skill-collision-handling
Sep 30, 2026
Merged

SergeyMenshykh merged 8 commits into
microsoft:mainfrom
SergeyMenshykh:sergeymenshykh-skill-collision-handling

Conversation

@SergeyMenshykh

@SergeyMenshykh SergeyMenshykh commented Sep 25, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation & Context

Tools can declare a FunctionInvocationContext parameter to access the current invocation. This PR brings the same opt-in pattern to skill scripts.

Description & Review Guide

  • What are the major changes?
    • SkillsProvider passes the invocation through SkillScript.run(...). A custom run() opts in by declaring a parameter annotated with FunctionInvocationContext; run() implementations without that annotation keep receiving host values as keyword arguments.
    • Inline script callables and file script runners can declare one FunctionInvocationContext parameter. The framework injects it, and it is hidden from the script schema. Runners must give it a default value.
    • InlineSkillScript.run and FileSkillScript.run gain an optional keyword-only context parameter; explicit application values passed as context are still forwarded unchanged.
    • Samples and docs updated.
  • What is the impact of these changes? Opt-in only. Scripts, runners, and run() overrides without a context parameter behave as before.
  • What do you want reviewers to focus on? How the context parameter is detected on run(), callbacks, and runners.

Related Issue

N/A

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.

Inline skill scripts and file-based script runners can now declare a FunctionInvocationContext parameter to receive the enclosing tool invocation context. Scripts without such a parameter keep their existing behavior. Update samples and docs accordingly.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings September 25, 2026 10:14
@agent-framework-automation agent-framework-automation Bot added documentation Usage: [Issues, PRs], Target: documentation in the code base and learn docs python Usage: [Issues, PRs], Target: Python labels Sep 25, 2026
@SergeyMenshykh SergeyMenshykh self-assigned this Sep 25, 2026
@SergeyMenshykh SergeyMenshykh moved this to In Review in Agent Framework Sep 25, 2026

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.

Copilot review overview

🟡 Changes recommended

Context detection incorrectly treats ordinary generic annotations containing FunctionInvocationContext as injectable parameters.

Get a fresh assessment by requesting another Copilot review.

Review effort: Balanced
Findings: 1 Medium severity

Open (1)
What changed in this PR

Adds opt-in FunctionInvocationContext injection for skill scripts while preserving legacy execution behavior.

Changes:

  • Adds context-aware inline and file script execution.
  • Updates samples and documentation.
  • Adds extensive sync, async, streaming, approval, and compatibility tests.
File Description
python/​packages/​core/​agent_framework/​_skills.py Implements context detection and injection.
python/​packages/​core/​tests/​core/​test_skills.py Adds comprehensive regression coverage.
python/​packages/​core/​AGENTS.md Documents context-aware scripts and runners.
python/​samples/​02-agents/​skills/​README.md Documents runtime context usage.
python/​samples/​02-agents/​skills/​code_defined_skill/​README.md Explains context in code-defined skills.
python/​samples/​02-agents/​skills/​code_defined_skill/​code_defined_skill.py Demonstrates inline context injection.
python/​samples/​02-agents/​skills/​class_based_skill/​README.md Explains context in class-based skills.
python/​samples/​02-agents/​skills/​class_based_skill/​class_based_skill.py Demonstrates method context injection.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Comment thread python/packages/core/agent_framework/_skills.py Outdated

@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.

MAF Automated Review — Iteration 1

Result: Findings reported
Scope: full PR (1 commit(s)): 482299dfa131
Model: gpt-5.6-sol-fast

Overview

This PR adds opt-in context injection for inline skill callbacks and file runners while preserving legacy run overrides, path revalidation, and approval behavior. Tests cover supported annotation forms, callable runner variants, context isolation, and reserved-argument rejection. However, all-or-nothing annotation resolution leaves an uncovered postponed-annotation case that silently treats a declared context parameter as model input, violating the intended trust boundary.

Reviewed the supplied pull-request change set across correctness, security/reliability, architecture, and failure behavior.
1 verified finding remained after source verification (1 medium) across 1 file. Details are attached to the affected lines below.

Affected areas: python/packages/core/agent_framework/_skills.py

Comment thread python/packages/core/agent_framework/_skills.py
…nto sergeymenshykh-skill-collision-handling

# Conflicts:
#	python/packages/core/agent_framework/_skills.py

@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.

MAF Automated Review — Iteration 2

Result: No findings
Scope: 19 net-new commit(s): 30b9b8e06766, 622737258c73, 7fb41d673c97, 4f4e72284c9c, 2055e666e1ac, 62337887116c, b20927b921d4, 2191c60c5e7d, 717557a3791a, 91fd47035684, f83a892f9f0e, 0843a9c0537b, e2c6df28daa9, 72272613d0c0, 5ee3e87d6768, 8ff549d3f721, 024dd9908bc1, 2c46deb91e70, 19797cba63be
Model: gpt-5.6-sol-fast

Overview

The PR adds opt-in invocation-context injection while preserving legacy script and runner dispatch. Registration-time validation, reserved-argument checks, approval ordering, path validation, and broad sync/async and concurrency coverage constrain the changed behavior. No new Critical, High, or Medium defect survived reconciliation against the implementation, tests, current incremental diff, and existing feedback.

Reviewed the supplied incremental change set across correctness, security/reliability, architecture, and failure behavior.
No publishable findings remained after source verification for this scope.

Only treat FunctionInvocationContext itself, optionally combined with None, as the context parameter. Container, callable, and Annotated annotations are no longer recognized.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Comment thread python/packages/core/agent_framework/_skills.py Outdated
Comment thread python/packages/core/agent_framework/_skills.py Outdated
SergeyMenshykh and others added 2 commits September 28, 2026 12:02
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Comment thread python/packages/core/agent_framework/_skills.py Outdated
Remove run_with_context and let SkillsProvider pass the FunctionInvocationContext to run() implementations that declare an annotated parameter. Unannotated overrides keep receiving host values as keyword arguments.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Avoid incompatible override diagnostics in zuban while retaining coverage for forwarding invocation context through run().

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@SergeyMenshykh
SergeyMenshykh added this pull request to the merge queue Sep 30, 2026
Merged via the queue into microsoft:main with commit 853c456 Sep 30, 2026
45 checks passed
@SergeyMenshykh
SergeyMenshykh deleted the sergeymenshykh-skill-collision-handling branch September 30, 2026 15:32

This branch was previously deployed

1 inactive deployment
github-app-auth — 8bfefda9 Deployed Sep 30, 2026 by SergeyMenshykh via add_label #24108
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

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants