Skip to content

Python: Align GitHub Copilot provider function approval to use SDK on_pre_tool_use hook#6750

Open
giles17 wants to merge 2 commits into
microsoft:mainfrom
giles17:copilot-approval-hook
Open

Python: Align GitHub Copilot provider function approval to use SDK on_pre_tool_use hook#6750
giles17 wants to merge 2 commits into
microsoft:mainfrom
giles17:copilot-approval-hook

Conversation

@giles17

@giles17 giles17 commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Motivation & Context

The Python agent-framework-github-copilot provider enforced function approval itself: every FunctionTool was wrapped in an SDK tool-handler that, for tools declared with approval_mode="always_require", awaited an on_function_approval callback before executing (denying by default when none was configured).

The GitHub Copilot SDK already exposes a native pre-execution hook (on_pre_tool_use) that can return "ask" and route the decision to on_permission_request. The AF-level callback therefore duplicated SDK functionality and diverged from the .NET provider. This change adopts the SDK-native model so the two providers behave consistently (follow-up to #6671 / #6674).

Description & Review Guide

  • What are the major changes?

    • When a FunctionTool declared with approval_mode="always_require" is registered and the caller has not supplied their own on_pre_tool_use hook, GitHubCopilotAgent installs a default on_pre_tool_use hook that returns "ask" for those tools (routing the decision to on_permission_request) and defers (None) for all other tools.
    • If the caller supplies their own on_pre_tool_use (via default_options or per-run options), it takes precedence and the caller owns approval handling; the agent logs a warning naming any approval-required tool that will therefore not be auto-gated.
    • The hook is built in _build_session_hooks and passed via the SDK's existing hooks= parameter to create_session/resume_session (computed where the full tool set is known).
    • Removed the bespoke enforcement path: the on_function_approval option, the FunctionApprovalCallback type, the _resolve_function_approval helper, the per-run rejection guards, and the approval branch in the tool handler.
    • Added an on_pre_tool_use option to GitHubCopilotOptions; updated tests, the package README, and the function-approval sample.
  • What is the impact of these changes?

    • Approval-required tools are gated through the SDK's native mechanism and surface through on_permission_request. With the default deny-all permission handler they remain secure-by-default. Non-approval tools are unaffected.
    • Behavioral change for callers who used on_function_approval: they now configure approval via on_permission_request (and optionally on_pre_tool_use). The provider is pre-release, so this is not a breaking change to a stable API.
  • What do you want reviewers to focus on?

    • The composition rule when a caller supplies their own on_pre_tool_use (override + warning), and that the default hook only asks for always_require tools while deferring everything else.

Related Issue

Fixes #6746

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.

Replace the bespoke on_function_approval enforcement in the GitHub Copilot provider with the Copilot SDK's native on_pre_tool_use hook. When no caller hook is supplied, a default hook returns 'ask' for approval_mode='always_require' tools (routed to on_permission_request) and defers others; a caller-supplied on_pre_tool_use takes precedence and logs a warning for any unenforced approval tool.

Fixes microsoft#6746

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

github-actions Bot commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Python Test Coverage

Python Test Coverage Report •
FileStmtsMissCoverMissing
packages/github_copilot/agent_framework_github_copilot
   _agent.py3331196%53–54, 439, 454–455, 739–740, 760, 763, 895, 936
TOTAL42548508688% 

Python Unit Test Overview

Tests Skipped Failures Errors Time
8325 37 💤 0 ❌ 0 🔥 2m 15s ⏱️

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

Aligns the Python agent-framework-github-copilot provider’s function-tool approval gating with the GitHub Copilot SDK’s native on_pre_tool_use hook, removing the bespoke on_function_approval path so behavior matches the .NET provider and the SDK’s intended flow.

Changes:

  • Removed agent-level on_function_approval enforcement and shifted approval-required tool gating to an SDK on_pre_tool_use hook that returns "ask" for approval_mode="always_require" tools.
  • Added on_pre_tool_use to GitHubCopilotOptions, wired hooks= into session create/resume, and implemented precedence + warning behavior when callers supply their own hook.
  • Updated unit tests, README guidance, and the function-approval sample to reflect the new approval mechanism via on_permission_request.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
python/samples/02-agents/providers/github_copilot/github_copilot_with_function_approval.py Updates the sample to demonstrate SDK-native tool approval via on_pre_tool_useon_permission_request.
python/packages/github_copilot/tests/test_github_copilot_agent.py Reworks tests to validate default hook behavior, precedence/warnings, and forwarding of hooks to SDK sessions.
python/packages/github_copilot/README.md Documents the new approval flow and precedence rules for custom on_pre_tool_use.
python/packages/github_copilot/agent_framework_github_copilot/_agent.py Implements _build_session_hooks and forwards hooks to create_session/resume_session; removes bespoke approval callback machinery.

@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: 90% | Result: All clear

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


Automated review by giles17's agents

Use a complete PreToolUseHookInput in on_pre_tool_use hook tests so pyright/pyrefly/ty/zuban no longer report missing required TypedDict keys. Restore load_dotenv() in the function-approval sample for consistency with the other GitHub Copilot samples (PR review feedback).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@giles17 giles17 marked this pull request as ready for review June 25, 2026 21:01

@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: 86% | 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: Align GitHub Copilot provider function approval to use SDK on_pre_tool_use hook

3 participants