feat(autogen): add native GovernanceInterventionHandler via AutoGen v0.4+ hooks#1591
Conversation
…0.4+ hooks Replace fragile monkey-patching with AutoGen's native intervention handler system (DefaultInterventionHandler with on_send, on_publish, on_response) introduced in AutoGen v0.4+. Changes: - Add GovernanceInterventionHandler class with three hooks: - on_send: tool call governance, content filtering, PII detection - on_publish: broadcast message governance - on_response: output content filtering, drift detection - Add AutoGenKernel.as_handler() factory method as the recommended integration path - Deprecate govern() and wrap() with DeprecationWarning pointing to as_handler() - Export AutoGenGovernanceHandler from integrations package - Add 51 new tests covering all three hook types, Cedar/OPA integration, deprecation warnings, PII detection, and backward compatibility - All 18 existing AutoGen regression tests pass unchanged
🤖 AI Agent: docs-sync-checker — Docs SyncDocs Sync
|
🤖 AI Agent: breaking-change-detector — API CompatibilityAPI Compatibility
|
🤖 AI Agent: security-scanner — View detailsNo security issues found. |
🤖 AI Agent: code-reviewer — Review SummaryReview SummaryThis pull request introduces a significant enhancement to the While the implementation is well-structured and includes comprehensive test coverage, there are a few areas that require attention to ensure security, correctness, and maintainability. CRITICAL
WARNING
SUGGESTIONS
Final AssessmentThe PR introduces a robust and much-needed improvement to the governance system by leveraging AutoGen's native Once the critical issues are resolved, this PR will be a strong candidate for merging. |
🤖 AI Agent: test-generator — `agent_os/integrations/autogen_adapter.py`Test Coverage Analysis
|
PR Review Summary
Verdict: ❌ Changes needed |
imran-siddique
left a comment
There was a problem hiding this comment.
Code Review: AutoGen GovernanceInterventionHandler
Good deprecation path and documentation, @miyannishar. This one has a few protocol-level issues that will cause runtime failures:
Blocking
1. Must inherit from DefaultInterventionHandler
The class is standalone with no base class. AutoGen's runtime performs isinstance checks against the InterventionHandler protocol. Without inheritance, the handler may silently fail to register. Follow the ADK adapter pattern:
python if _INTERVENTION_AVAILABLE: class GovernanceInterventionHandler(DefaultInterventionHandler): ... else: class GovernanceInterventionHandler: ...
2. on_response signature is wrong
AutoGen's protocol: on_response(message, *, sender: AgentId, recipient: AgentId | None)
PR implements: on_response(message, *, message_context=None, sender=None)
Missing recipient will cause TypeError at runtime. message_context is not a parameter of on_response in the protocol (it's only on on_send/on_publish).
3. on_send/on_publish make required protocol params optional
AutoGen always passes message_context and recipient/sender as keyword arguments. Defaulting to None masks integration errors. Match the protocol signatures exactly.
Security
4. Missing PII detection in on_response
PR description claims "PII detection everywhere" but on_response omits the _PII_PATTERNS scan that on_send and on_publish both perform. Agent responses with SSNs or API keys pass through unblocked.
Warnings
5. Single shared ExecutionContext across all agents
One _ctx is shared across all agents in the runtime. max_tool_calls=5 becomes a runtime-wide budget, not per-agent. Document this as intentional or create per-agent contexts.
6. Test helper uses deprecated asyncio.get_event_loop()
Use asyncio.run() to match other test files.
The protocol mismatches (#1, #2, #3) are the main blockers, as they'll cause runtime failures.
imran-siddique
left a comment
There was a problem hiding this comment.
Updated review (condensed):
TL;DR: 3 blockers (protocol mismatches that will cause runtime failures).
| # | Sev | Issue | Where |
|---|---|---|---|
| 1 | Block | Must inherit DefaultInterventionHandler -- runtime does isinstance checks |
class definition |
| 2 | Block | on_response signature wrong: missing recipient, has phantom message_context -- will TypeError |
on_response |
| 3 | Block | on_send/on_publish default required params to None -- masks integration errors |
on_send, on_publish |
| 4 | Sec | on_response omits PII scan despite PR claiming "PII detection everywhere" |
on_response |
| 5 | Warn | Shared ExecutionContext makes max_tool_calls runtime-global, not per-agent |
__init__ |
#1: Use conditional inheritance pattern from ADK adapter.
#2: Match protocol: on_response(message, *, sender, recipient).
#3: Match protocol signatures exactly for forward compat.
#4: Add _PII_PATTERNS scan to on_response like on_send/on_publish have.
imran-siddique
left a comment
There was a problem hiding this comment.
Approving native hooks migration.
…0.4+ hooks (microsoft#1591) Replace fragile monkey-patching with AutoGen's native intervention handler system (DefaultInterventionHandler with on_send, on_publish, on_response) introduced in AutoGen v0.4+. Changes: - Add GovernanceInterventionHandler class with three hooks: - on_send: tool call governance, content filtering, PII detection - on_publish: broadcast message governance - on_response: output content filtering, drift detection - Add AutoGenKernel.as_handler() factory method as the recommended integration path - Deprecate govern() and wrap() with DeprecationWarning pointing to as_handler() - Export AutoGenGovernanceHandler from integrations package - Add 51 new tests covering all three hook types, Cedar/OPA integration, deprecation warnings, PII detection, and backward compatibility - All 18 existing AutoGen regression tests pass unchanged Co-authored-by: Nishar <you@example.com>
Summary
Replaces fragile monkey-patching in the AutoGen adapter with AutoGen v0.4+'s native
InterventionHandlersystem (on_send,on_publish,on_response).Resolves #1590
Changes
New:
GovernanceInterventionHandlerclassIntercepts all message traffic in the AutoGen runtime:
on_sendFunctionCallallowlist, blocked-pattern scan, Cedar/OPA gate, max call count); general content filtering and PII detectionon_publishon_responsepost_executedrift detectionNew:
AutoGenKernel.as_handler()factoryDeprecated:
govern(),wrap(), and module-levelgovern()All now emit
DeprecationWarningpointing toas_handler(). Full backward compatibility maintained — all 18 existing regression tests pass unchanged.Export
AutoGenGovernanceHandlerexported fromagent_os.integrations.Testing
test_adapter_quality.py,test_deep_integrations.py)autogen_coremodule since AutoGen is not installed in CIDesign Decisions
DropMessagesemantics: Uses AutoGen's nativeDropMessagesentinel to block violations, matching the framework's expected behaviorautogen_coreis unavailable,as_handler()raisesRuntimeErrorwhilegovern()continues to workas_handler()mirrors ADK'sas_plugin(), OpenAI'sas_hooks(), and CrewAI'sas_hooks()Related
BasePluginintegration:google_adk_adapter.pyRunHooksrefactor: OpenAI Agents SDK adapter: use nativeRunHooksinstead ofwrap()workaround #1576, PR docs(contributing): add pre-push checklist with Docker integrated test step #1578AgentMiddlewarerefactor: fix(ci): make publish workflow green by fixing ESRP stubs and pip hash syntax #1577, PR feat(openai): native RunHooks lifecycle + BaseIntegration inheritance #1582wrap()workaround #1587, PR feat(crewai): add native GovernanceHooks using CrewAI execution hooks #1588