Split the 1341-line __init__.py into 5 focused modules: _state.py - home root helpers, mutable poll state _parser.py - shell tokenizer (independent) _predicates.py - terminal predicates + bounded output _guards.py - all 9 guard implementations + AST helpers __init__.py - thin entry point + dispatcher + re-exports Split the 2226-line test_guard.py into 14 dedicated files: 13 test_*.py files + tests/_support.py (shared helpers) Key changes: - Package-relative imports throughout (Hermes loader compatible) - No top-level module pollution (hermes_plugins.kuri_tool_guard.* only) - Typechecked via ty with symlink alias for hyphenated dir name - All 10 guard policies, messages, and block/allow behaviour preserved - 338 tests (336 original + 2 new: isolation + loader contract) - ty, ruff, bandit, vulture all pass in just verify
84 lines
2.8 KiB
Python
84 lines
2.8 KiB
Python
"""Tests for kuri-tool-guard — consecutive process poll guard."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
from tests._support import load_mod
|
|
|
|
|
|
class TestProcessPollGuard(unittest.TestCase):
|
|
"""Test bounded per-session state for consecutive process polling."""
|
|
|
|
def setUp(self):
|
|
self.mod = load_mod()
|
|
self.mod._state._POLL_STATE.clear()
|
|
|
|
def _call_at(
|
|
self,
|
|
timestamp: float,
|
|
tool_name: str = "process",
|
|
args: dict[str, object] | None = None,
|
|
session_id: str = "poll-test",
|
|
):
|
|
with patch.object(self.mod._guards.time, "time", return_value=timestamp):
|
|
return self.mod._guards._guard_process_poll(
|
|
tool_name=tool_name,
|
|
args=args or {"action": "poll"},
|
|
session_id=session_id,
|
|
)
|
|
|
|
def test_blocks_third_consecutive_poll(self):
|
|
self.assertIsNone(self._call_at(100.0))
|
|
self.assertIsNone(self._call_at(101.0))
|
|
|
|
result = self._call_at(102.0)
|
|
|
|
self.assertIsNotNone(result)
|
|
self.assertEqual(result["action"], "block")
|
|
self.assertIn("notify_on_complete=true", result["message"])
|
|
|
|
def test_any_other_tool_resets_poll_count(self):
|
|
self.assertIsNone(self._call_at(100.0))
|
|
self.assertIsNone(self._call_at(101.0))
|
|
self.assertIsNone(
|
|
self._call_at(
|
|
102.0,
|
|
tool_name="read_file",
|
|
args={"path": str(Path(tempfile.gettempdir()) / "example")},
|
|
)
|
|
)
|
|
|
|
self.assertIsNone(self._call_at(103.0))
|
|
self.assertEqual(self.mod._state._POLL_STATE["poll-test"][0], 1)
|
|
|
|
def test_lazy_cleanup_expires_state_after_24_hours(self):
|
|
self.assertIsNone(self._call_at(100.0, session_id="expired-session"))
|
|
|
|
self.assertIsNone(
|
|
self._call_at(
|
|
100.0 + self.mod._state._POLL_STATE_TTL_SECONDS + 1,
|
|
session_id="fresh-session",
|
|
)
|
|
)
|
|
|
|
self.assertNotIn("expired-session", self.mod._state._POLL_STATE)
|
|
self.assertIn("fresh-session", self.mod._state._POLL_STATE)
|
|
|
|
def test_capacity_evicts_least_recently_seen_session(self):
|
|
for index in range(self.mod._state._POLL_STATE_MAX_SESSIONS):
|
|
self.assertIsNone(self._call_at(float(index), session_id=f"session-{index}"))
|
|
|
|
self.assertIsNone(
|
|
self._call_at(
|
|
float(self.mod._state._POLL_STATE_MAX_SESSIONS),
|
|
session_id="new-session",
|
|
)
|
|
)
|
|
|
|
self.assertEqual(len(self.mod._state._POLL_STATE), self.mod._state._POLL_STATE_MAX_SESSIONS)
|
|
self.assertNotIn("session-0", self.mod._state._POLL_STATE)
|
|
self.assertIn("new-session", self.mod._state._POLL_STATE)
|