kuri-skill-gate/tests/test_gate.py

369 lines
14 KiB
Python

"""Tests for kuri-skill-gate plugin — blocking mode.
Gated tools (patch, delegate_task) are blocked until the corresponding
safety skill is loaded via skill_view() in the current session.
"""
import importlib.util
import os
import unittest
from pathlib import Path
from unittest.mock import MagicMock
PLUGIN_DIR = Path(__file__).resolve().parent.parent
PLUGIN_INIT = PLUGIN_DIR / "__init__.py"
def _load_mod():
spec = importlib.util.spec_from_file_location("kuri_skill_gate", PLUGIN_INIT)
assert spec is not None and spec.loader is not None
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
return mod
class TestSkillGate(unittest.TestCase):
"""Plugin-level middleware tests — fresh module per test."""
def setUp(self):
self.mod = _load_mod()
# ── Registration ───────────────────────────────────────────────
def test_register(self):
ctx = MagicMock()
self.mod.register(ctx)
ctx.register_middleware.assert_called_once_with(
"tool_execution", self.mod._on_tool_execution
)
# ── Hard block when skill not loaded ────────────────────────────
def test_patch_blocked_when_not_loaded(self):
"""patch is blocked with an error when patch-tool-safety not read."""
cb = MagicMock(return_value='{"success": true}')
r = self.mod._on_tool_execution(
tool_name="patch",
args={"path": "f", "old_string": "a", "new_string": "b"},
session_id="block-1",
next_call=cb,
)
cb.assert_not_called() # tool never executed
self.assertIn("blocked by kuri-skill-gate", r)
self.assertIn("patch-tool-safety", r)
def test_delegate_task_blocked_when_not_loaded(self):
"""delegate_task is blocked when delegate-task-best-practices not read."""
cb = MagicMock(return_value='{"success": true}')
r = self.mod._on_tool_execution(
tool_name="delegate_task",
args={"goal": "x"},
session_id="block-d1",
next_call=cb,
)
cb.assert_not_called()
self.assertIn("blocked by kuri-skill-gate", r)
self.assertIn("delegate-task-best-practices", r)
# ── Pass through after skill loaded ─────────────────────────────
def test_patch_passes_after_skill_loaded(self):
"""patch executes normally once skill is loaded."""
self.mod._on_tool_execution(
tool_name="skill_view",
args={"name": "patch-tool-safety"},
session_id="ok-p",
next_call=MagicMock(return_value='{"success": true}'),
)
cb = MagicMock(return_value="patched")
r = self.mod._on_tool_execution(
tool_name="patch",
args={"path": "f", "old_string": "a", "new_string": "b"},
session_id="ok-p",
next_call=cb,
)
cb.assert_called_once()
self.assertEqual(r, "patched")
def test_delegate_task_passes_after_skill_loaded(self):
"""delegate_task executes normally once skill is loaded."""
self.mod._on_tool_execution(
tool_name="skill_view",
args={"name": "delegate-task-best-practices"},
session_id="ok-d",
next_call=MagicMock(return_value='{"success": true}'),
)
cb = MagicMock(return_value="delegated")
r = self.mod._on_tool_execution(
tool_name="delegate_task",
args={"goal": "x"},
session_id="ok-d",
next_call=cb,
)
cb.assert_called_once()
self.assertEqual(r, "delegated")
# ── Unrelated skill doesn't unlock gate ─────────────────────────
def test_unrelated_skill_does_not_unlock(self):
"""Loading an unrelated skill does not unlock patch."""
self.mod._on_tool_execution(
tool_name="skill_view",
args={"name": "unrelated-skill"},
session_id="ign",
next_call=MagicMock(return_value='{"success": true}'),
)
cb = MagicMock(return_value='{"success": true}')
r = self.mod._on_tool_execution(
tool_name="patch",
args={},
session_id="ign",
next_call=cb,
)
cb.assert_not_called()
self.assertIn("blocked by kuri-skill-gate", r)
# ── Self-gate protection ───────────────────────────────────────
def test_self_gate_exempts_own_dir(self):
"""Patch on self-gate's own files passes without skill load."""
own_init = os.path.join(self.mod._PLUGIN_DIR, "__init__.py")
cb = MagicMock(return_value="self-patched")
r = self.mod._on_tool_execution(
tool_name="patch",
args={"path": own_init, "old_string": "a", "new_string": "b"},
session_id="sg",
next_call=cb,
)
cb.assert_called_once()
self.assertEqual(r, "self-patched")
def test_self_gate_still_blocks_outside_files(self):
"""Self-gate only exempts own dir — other files still blocked."""
cb = MagicMock(return_value='{"success": true}')
r = self.mod._on_tool_execution(
tool_name="patch",
args={"path": "/outside/other-file.py", "old_string": "a", "new_string": "b"},
session_id="sg-out",
next_call=cb,
)
cb.assert_not_called()
self.assertIn("blocked by kuri-skill-gate", r)
# ── Session isolation ──────────────────────────────────────────
def test_session_isolation(self):
"""Skill loaded in one session doesn't unlock another."""
self.mod._on_tool_execution(
tool_name="skill_view",
args={"name": "patch-tool-safety"},
session_id="iso-a",
next_call=MagicMock(return_value='{"success": true}'),
)
cb = MagicMock(return_value='{"success": true}')
r = self.mod._on_tool_execution(
tool_name="patch",
args={},
session_id="iso-b", # different session — not loaded
next_call=cb,
)
cb.assert_not_called()
self.assertIn("blocked by kuri-skill-gate", r)
# iso-a still works
cb2 = MagicMock(return_value="ok")
r2 = self.mod._on_tool_execution(
tool_name="patch",
args={},
session_id="iso-a",
next_call=cb2,
)
cb2.assert_called_once()
self.assertEqual(r2, "ok")
# ── Pass-through for unmapped tools ─────────────────────────────
def test_unmapped_tool_passes_through(self):
cb = MagicMock(return_value="ok")
r = self.mod._on_tool_execution(
tool_name="read_file",
args={"path": "f"},
session_id="pt",
next_call=cb,
)
cb.assert_called_once()
self.assertEqual(r, "ok")
def test_skill_view_for_unmapped_skill_does_not_track(self):
"""skill_view for an unmapped skill doesn't affect anything."""
self.mod._on_tool_execution(
tool_name="skill_view",
args={"name": "some-other-skill"},
session_id="noop",
next_call=MagicMock(return_value='{"success": true}'),
)
cb = MagicMock(return_value='{"success": true}')
r = self.mod._on_tool_execution(
tool_name="patch",
args={},
session_id="noop",
next_call=cb,
)
cb.assert_not_called()
self.assertIn("blocked by kuri-skill-gate", r)
# ── Command pattern gates (terminal commands) ───────────────────
def test_gh_command_blocked_when_skill_not_loaded(self):
"""Terminal with gh is blocked when gh-cli not loaded."""
cb = MagicMock(return_value='{"success": true}')
r = self.mod._on_tool_execution(
tool_name="terminal",
args={"command": "gh issue comment 123 --body 'hello'"},
session_id="gh-block",
next_call=cb,
)
cb.assert_not_called()
self.assertIn("blocked by kuri-skill-gate", r)
self.assertIn("gh-cli", r)
def test_gh_command_passes_after_skill_loaded(self):
"""Terminal with gh passes once gh-cli is loaded."""
self.mod._on_tool_execution(
tool_name="skill_view",
args={"name": "gh-cli"},
session_id="gh-ok",
next_call=MagicMock(return_value='{"success": true}'),
)
cb = MagicMock(return_value="gh-output")
r = self.mod._on_tool_execution(
tool_name="terminal",
args={"command": "gh issue comment 123 --body 'hello'"},
session_id="gh-ok",
next_call=cb,
)
cb.assert_called_once()
self.assertEqual(r, "gh-output")
def test_non_gh_terminal_not_blocked(self):
"""Terminal without gh passes through."""
cb = MagicMock(return_value="ls-output")
r = self.mod._on_tool_execution(
tool_name="terminal",
args={"command": "ls -la"},
session_id="no-gh",
next_call=cb,
)
cb.assert_called_once()
self.assertEqual(r, "ls-output")
def test_gh_in_quoted_shell_subcommand_is_blocked(self):
"""A quoted shell subcommand still contains an executable gh token."""
cb = MagicMock(return_value="shell-output")
r = self.mod._on_tool_execution(
tool_name="terminal",
args={"command": "sh -c 'gh issue create --body text'"},
session_id="gh-quoted",
next_call=cb,
)
cb.assert_not_called()
self.assertIn("blocked by kuri-skill-gate", r)
def test_gh_terminal_blocked_different_session(self):
"""gh gate respects session isolation."""
# Load in session "a"
self.mod._on_tool_execution(
tool_name="skill_view",
args={"name": "gh-cli"},
session_id="gh-iso-a",
next_call=MagicMock(return_value='{"success": true}'),
)
# Block in session "b"
cb = MagicMock(return_value='{"success": true}')
r = self.mod._on_tool_execution(
tool_name="terminal",
args={"command": "gh pr create --body 'text'"},
session_id="gh-iso-b",
next_call=cb,
)
cb.assert_not_called()
self.assertIn("blocked by kuri-skill-gate", r)
def test_non_terminal_tool_with_gh_not_blocked(self):
"""Non-terminal tools with gh in args are not affected."""
cb = MagicMock(return_value="ok")
r = self.mod._on_tool_execution(
tool_name="read_file",
args={"path": "/gh/file.txt"},
session_id="gh-nonterm",
next_call=cb,
)
cb.assert_called_once()
self.assertEqual(r, "ok")
# ── skill_manage(action='patch') gate ──────────────────────────
def test_skill_manage_patch_blocked_when_not_loaded(self):
"""skill_manage(action='patch') blocked when patch-tool-safety not loaded."""
cb = MagicMock(return_value='{"success": true}')
r = self.mod._on_tool_execution(
tool_name="skill_manage",
args={"action": "patch", "name": "some-skill", "old_string": "a", "new_string": "b"},
session_id="sm-block",
next_call=cb,
)
cb.assert_not_called()
self.assertIn("blocked by kuri-skill-gate", r)
self.assertIn("patch-tool-safety", r)
def test_skill_manage_patch_passes_after_skill_loaded(self):
"""skill_manage(action='patch') passes once patch-tool-safety is loaded."""
self.mod._on_tool_execution(
tool_name="skill_view",
args={"name": "patch-tool-safety"},
session_id="sm-ok",
next_call=MagicMock(return_value='{"success": true}'),
)
cb = MagicMock(return_value="patched")
r = self.mod._on_tool_execution(
tool_name="skill_manage",
args={"action": "patch", "name": "some-skill", "old_string": "a", "new_string": "b"},
session_id="sm-ok",
next_call=cb,
)
cb.assert_called_once()
self.assertEqual(r, "patched")
def test_skill_manage_create_not_blocked(self):
"""skill_manage with non-patch action passes through."""
cb = MagicMock(return_value="created")
r = self.mod._on_tool_execution(
tool_name="skill_manage",
args={"action": "create", "name": "new-skill", "content": "# test"},
session_id="sm-create",
next_call=cb,
)
cb.assert_called_once()
self.assertEqual(r, "created")
def test_skill_manage_patch_session_isolation(self):
"""skill_manage(action='patch') respects session isolation."""
self.mod._on_tool_execution(
tool_name="skill_view",
args={"name": "patch-tool-safety"},
session_id="sm-iso-a",
next_call=MagicMock(return_value='{"success": true}'),
)
cb = MagicMock(return_value='{"success": true}')
r = self.mod._on_tool_execution(
tool_name="skill_manage",
args={"action": "patch", "name": "some-skill"},
session_id="sm-iso-b",
next_call=cb,
)
cb.assert_not_called()
self.assertIn("blocked by kuri-skill-gate", r)
if __name__ == "__main__":
unittest.main(verbosity=2)