diff --git a/contributing/samples/agent_governance/README.md b/contributing/samples/agent_governance/README.md index 22fef09..81ad8fd 100644 --- a/contributing/samples/agent_governance/README.md +++ b/contributing/samples/agent_governance/README.md @@ -7,7 +7,7 @@ An ADK plugin that enforces policy-as-code rules before tool execution using the ## Install ```bash -pip install google-adk-community agentmesh-platform +pip install google-adk-community agent-governance-toolkit-core ``` ## Usage @@ -57,7 +57,7 @@ contract). Allowed calls return `None`, letting the tool proceed normally. ## Fail-closed by default -If `agentmesh-platform` is not installed, the plugin raises `ImportError` +If `agent-governance-toolkit-core` is not installed, the plugin raises `ImportError` at construction time. Pass `fail_open=True` to degrade gracefully instead (all calls pass through with a logged warning). diff --git a/src/google/adk_community/plugins/agent_governance_plugin.py b/src/google/adk_community/plugins/agent_governance_plugin.py index 05baae3..422d031 100644 --- a/src/google/adk_community/plugins/agent_governance_plugin.py +++ b/src/google/adk_community/plugins/agent_governance_plugin.py @@ -18,7 +18,7 @@ Governance Toolkit (https://github.com/microsoft/agent-governance-toolkit). Denied tool calls are short-circuited with a policy violation response. -Requires: ``pip install agentmesh-platform`` +Requires: ``pip install agent-governance-toolkit-core`` """ from __future__ import annotations @@ -37,7 +37,7 @@ class _GovernanceUnavailableError(ImportError): - """Raised when agentmesh-platform is not installed and fail_open=False.""" + """Raised when agent-governance-toolkit-core is not installed and fail_open=False.""" class AgentGovernancePlugin(BasePlugin): @@ -52,7 +52,7 @@ class AgentGovernancePlugin(BasePlugin): ``*.yaml`` policy files. Resolved relative to the caller's working directory. Must be provided explicitly. agent_did: Decentralized identifier for the agent. - fail_open: If ``True``, tool calls proceed when ``agentmesh-platform`` + fail_open: If ``True``, tool calls proceed when ``agent-governance-toolkit-core`` is not installed (logs a warning). If ``False`` (default), raises ``ImportError`` at construction time. strict: If ``True``, raises on policy load errors instead of @@ -61,7 +61,7 @@ class AgentGovernancePlugin(BasePlugin): Defaults to ``False``. Raises: - ImportError: If ``agentmesh-platform`` is not installed and + ImportError: If ``agent-governance-toolkit-core`` is not installed and ``fail_open`` is False. RuntimeError: If ``strict`` is True and a policy file fails to load. @@ -122,12 +122,12 @@ def _setup(self, *, fail_open: bool, strict: bool) -> None: except ImportError: if not fail_open: raise _GovernanceUnavailableError( - "agentmesh-platform is required for governance enforcement. " - "Install with: pip install agentmesh-platform" + "agent-governance-toolkit-core is required for governance enforcement. " + "Install with: pip install agent-governance-toolkit-core" ) logger.warning( - "agentmesh-platform not installed; governance checks disabled. " - "Install with: pip install agentmesh-platform" + "agent-governance-toolkit-core not installed; governance checks disabled. " + "Install with: pip install agent-governance-toolkit-core" ) async def before_tool_callback( diff --git a/tests/plugins/test_agent_governance_plugin.py b/tests/plugins/test_agent_governance_plugin.py index 2e4c27e..8f5acaf 100644 --- a/tests/plugins/test_agent_governance_plugin.py +++ b/tests/plugins/test_agent_governance_plugin.py @@ -14,7 +14,7 @@ """Tests for AgentGovernancePlugin. -These tests mock the agentmesh-platform dependency so they run without +These tests mock the agent-governance-toolkit-core dependency so they run without it installed, verifying the plugin's integration with the ADK plugin API. """ @@ -34,7 +34,7 @@ # --------------------------------------------------------------------------- -# Mock fixtures for agentmesh-platform +# Mock fixtures for agent-governance-toolkit-core # --------------------------------------------------------------------------- @@ -169,19 +169,34 @@ def policy_dir(tmp_path: Path) -> Path: return policies +@pytest.fixture() +def block_agentmesh(): + """Make every agentmesh import fail, even if the package is installed.""" + with patch.dict(sys.modules): + _uninstall_fake_agentmesh() + # A None parent makes agentmesh and all of its submodules unimportable. + sys.modules["agentmesh"] = None + yield + + class TestAgentGovernancePlugin: """Tests for AgentGovernancePlugin construction and behavior.""" + @pytest.mark.usefixtures("block_agentmesh") def test_raises_import_error_when_agentmesh_missing(self, tmp_path: Path): - """Plugin raises ImportError by default when agentmesh is missing.""" - # Simulate agentmesh not being importable by patching _setup - with patch.object( - AgentGovernancePlugin, - "_setup", - side_effect=ImportError("agentmesh-platform is required"), + """Plugin raises ImportError naming the package to install.""" + with pytest.raises( + ImportError, match="pip install agent-governance-toolkit-core" ): - with pytest.raises(ImportError, match="agentmesh-platform"): - AgentGovernancePlugin(policy_dir=tmp_path) + AgentGovernancePlugin(policy_dir=tmp_path) + + @pytest.mark.usefixtures("block_agentmesh") + def test_fail_open_warns_with_install_hint_when_agentmesh_missing( + self, tmp_path: Path, caplog: pytest.LogCaptureFixture + ): + """With fail_open=True, the warning names the package to install.""" + AgentGovernancePlugin(policy_dir=tmp_path, fail_open=True) + assert "pip install agent-governance-toolkit-core" in caplog.text def test_fail_open_mode_allows_construction(self, tmp_path: Path): """With fail_open=True, plugin degrades when engine returns no policies."""