diff --git a/scripts/install.sh b/scripts/install.sh index 9e3db73..0ed26e2 100644 --- a/scripts/install.sh +++ b/scripts/install.sh @@ -43,8 +43,9 @@ for root in .agents/skills .claude/skills; do dst="${TARGET}/${root}" log "skills: copying -> ${dst}" mkdir -p "${dst}" - find "${dst}" -mindepth 1 -delete + find -H "${dst}" -mindepth 1 -delete cp -a "${src}/." "${dst}/" + find -H "${dst}" \( -name __pycache__ -o -name '*.py[co]' \) -prune -exec rm -rf {} + if [[ ! "${TARGET}" -ef "${HOME}" ]]; then log "skills: linking in ${dst} what ${HOME}/${root} already holds" for skill in "${dst}"/*; do diff --git a/scripts/orchestrator.sh b/scripts/orchestrator.sh index 78ef794..1731950 100644 --- a/scripts/orchestrator.sh +++ b/scripts/orchestrator.sh @@ -156,7 +156,7 @@ install_hub() { chmod 700 "${APPROVERS}" cp -a "${REPO_ROOT}/orchestrator/." "${HUB}/" # Python loads a cached .pyc whose header matches the source mtime and size, and cp -a keeps the mtime: one planted in the sandbox-writable tree would run on the host - find "${HUB}" -name __pycache__ -type d -prune -exec rm -rf {} + + find -H "${HUB}" \( -name __pycache__ -o -name '*.py[co]' \) -prune -exec rm -rf {} + chmod +x "${HUB}/client/permission_hook.py" "${HUB}/client/runner.py" "${HUB}/client/approval.py" "${HUB}/client/cleanup.py" "${HUB}/client/offices.py" "${HUB}/client/sessions.py" "${HUB}/client/resources.py" log "skills: the PermissionRequest hook client is ${HUB}/client/permission_hook.py" log "skills: approvals need a pinned passkey: settings, 'Passkey registrieren', then 'make orchestrator-pin CREDENTIAL='; until then every executor refuses" diff --git a/tests/test_install.py b/tests/test_install.py index b96935f..a26f16e 100644 --- a/tests/test_install.py +++ b/tests/test_install.py @@ -11,18 +11,22 @@ from pathlib import Path INSTALL = Path(__file__).resolve().parents[1] / "scripts" / "install.sh" NPX = "#!/bin/sh\nmkdir -p .agents/skills/locked\necho locked > .agents/skills/locked/SKILL.md\n" +BYTECODE = ("__pycache__/radio.cpython-312.pyc", "json.pyc", "legacy.pyo") class TestInstall(unittest.TestCase): - def install(self, root: Path, target: Path) -> None: + def install(self, root: Path, target: Path, npx: str = NPX, planted: tuple[str, ...] = ()) -> None: repo, stubs = root / "repo", root / "bin" (repo / "scripts").mkdir(parents=True) shutil.copy(INSTALL, repo / "scripts" / "install.sh") (repo / "scripts" / "enable-plugins.js").write_text("", encoding="utf-8") (repo / "skills" / "own").mkdir(parents=True) (repo / "skills" / "own" / "SKILL.md").write_text("own", encoding="utf-8") + for name in planted: + (repo / name).parent.mkdir(parents=True, exist_ok=True) + (repo / name).write_bytes(b"planted") stubs.mkdir() - (stubs / "npx").write_text(NPX, encoding="utf-8") + (stubs / "npx").write_text(npx, encoding="utf-8") (stubs / "npx").chmod(0o755) env = {**os.environ, "PATH": f"{stubs}:/usr/bin:/bin", "TARGET": str(target), "HOME": str(root / "home")} done = subprocess.run(["bash", str(repo / "scripts" / "install.sh")], env=env, capture_output=True, text=True, check=False) @@ -55,6 +59,26 @@ class TestInstall(unittest.TestCase): expected = {name: root / "home" / folder / name if f"{folder}/{name}" in linked else None for name in ("locked", "own")} self.assertEqual(installed, expected) + def test_no_python_bytecode_reaches_the_installed_skills(self): + locked = "".join(f"echo planted > .agents/skills/locked/scripts/{name}\n" for name in BYTECODE) + npx = f"{NPX}mkdir -p .agents/skills/locked/scripts/__pycache__\n{locked}" + for linked in (False, True): + with self.subTest(linked=linked), tempfile.TemporaryDirectory() as tmp: + root = Path(tmp) + target = root / "target" + (target / ".agents" / "skills").mkdir(parents=True) + if linked: + (target / ".claude").mkdir() + (target / ".claude" / "skills").symlink_to(Path("..") / ".agents" / "skills") + self.install(root, target, npx, (*(f"skills/own/scripts/{name}" for name in BYTECODE), "skills/own/scripts/radio.py")) + for folder in (".agents/skills", ".claude/skills"): + installed = target / folder + with self.subTest(folder=folder): + bytecode = [str(path.relative_to(installed)) for path in sorted(installed.rglob("*")) if path.name == "__pycache__" or path.suffix in (".pyc", ".pyo")] + self.assertEqual(bytecode, []) + self.assertTrue((installed / "own" / "scripts" / "radio.py").is_file()) + self.assertTrue((installed / "locked" / "SKILL.md").is_file()) + if __name__ == "__main__": unittest.main() diff --git a/tests/test_orchestrator_migration.py b/tests/test_orchestrator_migration.py index bbb215b..636af9d 100644 --- a/tests/test_orchestrator_migration.py +++ b/tests/test_orchestrator_migration.py @@ -195,12 +195,17 @@ class TestOrchestratorMigration(unittest.TestCase): self.assertIn("Environment=PATH=/usr/bin:/bin", unit.read_text(encoding="utf-8").splitlines()) def test_install_ships_no_bytecode_from_the_tree(self): - planted = REPO_ROOT / "orchestrator" / "client" / "__pycache__" / "planted.cpython-0.pyc" - planted.parent.mkdir(exist_ok=True) - planted.write_bytes(b"planted") - self.addCleanup(planted.unlink, True) + client = REPO_ROOT / "orchestrator" / "client" + if not (client / "__pycache__").exists(): + (client / "__pycache__").mkdir() + self.addCleanup(shutil.rmtree, client / "__pycache__", True) + for planted in (client / "__pycache__" / "planted.cpython-0.pyc", client / "planted.pyc", client / "planted.pyo"): + planted.write_bytes(b"planted") + self.addCleanup(planted.unlink, True) self.install() - self.assertEqual(sorted((self.home / ".local" / "share" / NEW).rglob("__pycache__")), []) + hub = self.home / ".local" / "share" / NEW + self.assertEqual(sorted(path for path in hub.rglob("*") if path.name == "__pycache__" or path.suffix in (".pyc", ".pyo")), []) + self.assertTrue((hub / "client" / "permission_hook.py").is_file()) def test_old_names_live_only_in_the_migration(self): done = subprocess.run(["git", "grep", "-niE", "kvb[-_]hub", "--", ".", ":!docs/requirements", f":!{Path(__file__).relative_to(REPO_ROOT)}"], cwd=REPO_ROOT, capture_output=True, text=True, check=False)