muse-agent-example/tests/architecture/test_runtime_purity.py

69 lines
2.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""执行底座纯度门:runtime 不得反向依赖 Muse 业务实现。"""
from __future__ import annotations
import pathlib
import re
import unittest
ROOT = next(
parent
for parent in (pathlib.Path(__file__).resolve().parent, *pathlib.Path(__file__).resolve().parents)
if (parent / "AGENTS.md").is_file() and (parent / ".git").exists()
)
FORBIDDEN_IMPORTS = (
"agent_trace",
"muse_role",
"muse_role_contract",
"muse_db",
"psycopg",
"fixed-opus",
"role_task",
"role_policy",
"run_registry",
"persist_raw",
"read_tools",
)
DISPATCH_CLI = (
ROOT
/ "muse"
/ "lifecycle"
/ "dispatch"
/ "skills"
/ "派发智能体任务"
/ "scripts"
/ "dispatch_agent_task.py"
)
class RuntimePurityTest(unittest.TestCase):
def test_runtime_does_not_import_muse_business(self) -> None:
offenders: list[str] = []
import_pattern = re.compile(
r"(?:from|import)\s+(?:" + "|".join(map(re.escape, FORBIDDEN_IMPORTS)) + r")\b"
)
muse_pattern = re.compile(r"(?:from|import)\s+muse(?:\.|\s)")
runtime_root = ROOT / "runtime"
self.assertTrue(runtime_root.is_dir(), "runtime/ 必须存在")
for path in runtime_root.rglob("*"):
if not path.is_file() or "__pycache__" in path.parts:
continue
if path.suffix not in {".py", ".ts", ".md"}:
continue
text = path.read_text(encoding="utf-8")
if import_pattern.search(text) or muse_pattern.search(text):
offenders.append(path.relative_to(ROOT).as_posix())
self.assertEqual(offenders, [], "runtime 反向依赖 Muse 业务:\n" + "\n".join(offenders))
def test_dispatch_agent_task_is_thin_cli(self) -> None:
lines = DISPATCH_CLI.read_text(encoding="utf-8").splitlines()
self.assertLess(
len(lines),
100,
f"dispatch_agent_task.py 必须 < 100 行,实际 {len(lines)}",
)
if __name__ == "__main__":
unittest.main()