"""The mcp server's KB policy (kb.py, no MCP SDK needed): search must not reveal what read_page would refuse, and per-user config stays unreadable whatever case or trailing dots the file system ignores.""" import os import shutil import sys import tempfile import unittest from pathlib import Path ROOT = Path(__file__).resolve().parents[2] FIX = ROOT / "tests/fixtures/minimal-kb" sys.path.insert(0, str(ROOT / ".agents/modules/mcp/server")) from kb import KB, PolicyError # noqa: E402 class McpKb(unittest.TestCase): def setUp(self): self.tmp = tempfile.TemporaryDirectory() base = Path(self.tmp.name) self.root, up = base / "kb", base / "up" shutil.copytree(str(FIX), str(self.root)) shutil.copytree(str(FIX), str(up)) rr = self.root / ".agents/skills/ckb-retrieve/scripts" rr.mkdir(parents=True) shutil.copy(str(ROOT / ".agents/skills/ckb-retrieve/scripts/retrieve_candidates.py"), str(rr)) for private in ("workload/notes.md", "raw/saved-conversations/2026-11-11/chat.md"): p = up / private p.parent.mkdir(parents=True, exist_ok=True) p.write_text("zanzibarquokka is private here\n", encoding="utf-8") (self.root / "linked").mkdir() os.symlink(str(up), str(self.root / "linked/up")) self.kb = KB(self.root, git_mode="off") def tearDown(self): self.tmp.cleanup() def test_search_hides_paths_read_page_refuses(self): res = self.kb.search("zanzibarquokka") paths = [c["path"] for c in res["candidates"]] self.assertFalse([p for p in paths if "workload" in p or "saved-conversations" in p], paths) def test_query_that_looks_like_an_option_is_text(self): res = self.kb.search("--opened=wiki/x.md") self.assertIsInstance(res, dict) self.assertFalse((self.root / "tmp/access.log").exists()) def test_local_yaml_refused_in_any_case_or_with_trailing_dot(self): for name in ("source.local.yaml", "source.LOCAL.yaml", "source.local.yaml.", "source.Local.Yaml "): with self.assertRaises(PolicyError, msg=name): self.kb.resolve_read(f"wiki/{name}") if __name__ == "__main__": unittest.main()