Skip to content

feat(adapters): add native hooks for Anthropic, SK, smolagents, PydanticAI#1605

Merged
imran-siddique merged 1 commit intomainfrom
feat/remaining-native-hooks
Apr 30, 2026
Merged

feat(adapters): add native hooks for Anthropic, SK, smolagents, PydanticAI#1605
imran-siddique merged 1 commit intomainfrom
feat/remaining-native-hooks

Conversation

@imran-siddique
Copy link
Copy Markdown
Member

Rebased version of #1593 (by @miyannishar) with merge conflicts resolved against recently merged native-hooks PRs (#1582, #1585, #1588, #1591).

Adds native hook implementations for the remaining four adapters:

  • Anthropic: GovernanceMessageHook + as_message_hook() factory
  • Semantic Kernel: GovernanceFunctionFilter + as_filter() factory
  • Smolagents: GovernanceStepCallback + as_step_callback() factory
  • PydanticAI: GovernanceCapability + as_capability() factory

Includes 52 new tests across 4 test files.

Part of: #1571
Original PR: #1593 (closed due to merge conflicts)

…ticAI

Complete the native-hooks migration for all four remaining adapters:

Anthropic:
- Add GovernanceMessageHook + as_message_hook() factory
- Pre-execution: content scanning, tool allowlist, token limits
- Post-execution: tool_use validation, token tracking, audit
- Deprecate wrap() and wrap_client() with migration guidance

Semantic Kernel:
- Add GovernanceFunctionFilter + as_filter() factory
- Uses SK's native add_filter('auto_function_invocation', ...) system
- Validates function names, blocked patterns, call counts
- Deprecate wrap() and wrap_kernel() with migration guidance

Smolagents:
- Add GovernanceStepCallback + as_step_callback() factory
- Implements step_callbacks protocol: __call__(step, agent)
- Validates tool names, blocked patterns, observations
- Deprecate wrap() with migration guidance

PydanticAI:
- Add GovernanceCapability + as_capability() factory
- Lifecycle hooks: before/after_run, before/after_tool_execute
- Pre-execution policy gating, post-execution drift detection
- Deprecate wrap() with migration guidance

Package exports:
- Export AnthropicGovernanceHook, SKGovernanceFilter,
  SmolagentsGovernanceCallback, PydanticAIGovernanceCapability
  from integrations __init__.py

Tests:
- test_anthropic_hooks.py: 12 tests
- test_semantic_kernel_hooks.py: 10 tests
- test_smolagents_hooks.py: 14 tests
- test_pydantic_ai_hooks.py: 16 tests

Part of: #1571
@imran-siddique imran-siddique merged commit e1d99b9 into main Apr 30, 2026
29 checks passed
@imran-siddique imran-siddique deleted the feat/remaining-native-hooks branch April 30, 2026 03:47
@github-actions github-actions Bot added the tests label Apr 30, 2026
@github-actions
Copy link
Copy Markdown

🤖 AI Agent: security-scanner — View details

No security issues found.

@github-actions
Copy link
Copy Markdown

🤖 AI Agent: docs-sync-checker — Docs Sync

Docs Sync

  • GovernanceMessageHook in anthropic_adapter.py -- missing docstring
  • GovernanceCapability in pydantic_ai_adapter.py -- missing docstring
  • GovernanceFunctionFilter in semantic_kernel_adapter.py -- missing docstring
  • README.md -- sections for Anthropic, Semantic Kernel, Smolagents, and PydanticAI adapters need updates to reflect the new native hooks and recommended integration patterns.
  • CHANGELOG.md -- missing entry for new native hooks for Anthropic, Semantic Kernel, Smolagents, and PydanticAI. Include deprecation notices for wrap() methods.

@github-actions
Copy link
Copy Markdown

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

Scanned Files

None

@github-actions
Copy link
Copy Markdown

🤖 AI Agent: code-reviewer — Action Items:

TL;DR: 0 blockers, 2 warnings. Solid implementation with minor follow-ups.

# Sev Issue Where
1 Warn Deprecated methods (wrap, wrap_client) should be removed in future anthropic_adapter.py, pydantic_ai_adapter.py, semantic_kernel_adapter.py
2 Warn Ensure backward compatibility for deprecated methods during deprecation period anthropic_adapter.py, pydantic_ai_adapter.py, semantic_kernel_adapter.py

Action Items:

  • None (no blockers identified).

Warnings:

# Issue Fine as follow-up PRs
1 Remove deprecated methods (wrap, wrap_client) after sufficient notice period Yes
2 Add tests to ensure deprecated methods maintain backward compatibility until removal Yes

@github-actions
Copy link
Copy Markdown

🤖 AI Agent: breaking-change-detector — API Compatibility

API Compatibility

Severity Change Impact
High Deprecated wrap() method in AnthropicKernel, PydanticAIKernel, and SemanticKernelWrapper. Existing code using these methods will need to be updated to use the new recommended patterns.
High Deprecated wrap_client() function for Anthropic clients. Existing code using this function will need to migrate to as_message_hook().
High Deprecated wrap() function for PydanticAI agents. Existing code using this function will need to migrate to as_capability().
High Deprecated wrap_kernel() function for Semantic Kernel. Existing code using this function will need to migrate to as_filter() with add_filter().

@github-actions github-actions Bot added the size/XL Extra large PR (500+ lines) label Apr 30, 2026
@github-actions
Copy link
Copy Markdown

🤖 AI Agent: test-generator — `anthropic_adapter.py`

anthropic_adapter.py

  • test_governance_message_hook_create -- tests the creation of a GovernanceMessageHook instance.
  • test_governance_message_hook_create_policy_violation -- tests handling of policy violations during message creation.
  • test_governance_message_hook_tool_not_allowed -- tests behavior when a disallowed tool is used in a message.

pydantic_ai_adapter.py

  • test_governance_capability_before_run_blocked -- tests behavior when a prompt violates governance policy in before_run.
  • test_governance_capability_after_run -- tests the post-run auditing functionality of GovernanceCapability.
  • test_governance_capability_before_tool_execute_blocked -- tests handling of blocked tool execution in before_tool_execute.

semantic_kernel_adapter.py

  • test_governance_function_filter_create -- tests the creation of a GovernanceFunctionFilter instance.
  • test_governance_function_filter_tool_blocked -- tests handling of blocked tool calls in the governance filter.
  • test_governance_function_filter_audit -- tests the auditing functionality after tool execution in the governance filter.

@github-actions
Copy link
Copy Markdown

PR Review Summary

Check Status Details
🔍 Code Review ❌ Failed Issues detected
🛡️ Security Scan ✅ Completed Analysis complete
🔄 Breaking Changes ✅ Completed Analysis complete
📝 Docs Sync ✅ Completed Analysis complete
🧪 Test Coverage ❌ Failed Issues detected

Verdict: ❌ Changes needed

imran-siddique added a commit to imran-siddique/agent-governance-toolkit that referenced this pull request May 4, 2026
…ticAI (microsoft#1605)

Complete the native-hooks migration for all four remaining adapters:

Anthropic:
- Add GovernanceMessageHook + as_message_hook() factory
- Pre-execution: content scanning, tool allowlist, token limits
- Post-execution: tool_use validation, token tracking, audit
- Deprecate wrap() and wrap_client() with migration guidance

Semantic Kernel:
- Add GovernanceFunctionFilter + as_filter() factory
- Uses SK's native add_filter('auto_function_invocation', ...) system
- Validates function names, blocked patterns, call counts
- Deprecate wrap() and wrap_kernel() with migration guidance

Smolagents:
- Add GovernanceStepCallback + as_step_callback() factory
- Implements step_callbacks protocol: __call__(step, agent)
- Validates tool names, blocked patterns, observations
- Deprecate wrap() with migration guidance

PydanticAI:
- Add GovernanceCapability + as_capability() factory
- Lifecycle hooks: before/after_run, before/after_tool_execute
- Pre-execution policy gating, post-execution drift detection
- Deprecate wrap() with migration guidance

Package exports:
- Export AnthropicGovernanceHook, SKGovernanceFilter,
  SmolagentsGovernanceCallback, PydanticAIGovernanceCapability
  from integrations __init__.py

Tests:
- test_anthropic_hooks.py: 12 tests
- test_semantic_kernel_hooks.py: 10 tests
- test_smolagents_hooks.py: 14 tests
- test_pydantic_ai_hooks.py: 16 tests

Part of: microsoft#1571

Co-authored-by: Nishar <you@example.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/XL Extra large PR (500+ lines) tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant