Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
test(server): scan only backend source in the whole-tree invariant tests
trohitg09dbaaabeddb
diff --git a/server/tests/_source_tree.py b/server/tests/_source_tree.pynew file mode 100644index 00000000..d49fbb52--- /dev/null+++ b/server/tests/_source_tree.py@@ -0,0 +1,39 @@+"""The backend's own Python files, for tests that scan its source.++Several invariant tests read every backend module: no raw ``print()``, no+climb above ``server/``, no Node runtime lookup, no LangChain import, no raw+``send_custom_event`` payload. ``server/`` also holds trees that are not+backend source: the virtualenv and, in a source-dev install, the Mobile+workspace's phone runtime under ``.opencompany/mobile``. Each holds far more+``.py`` files than the backend itself, and scans that read them took minutes+and could trip over third-party code. Walking through this module prunes them+before anything is read, so a new scan test cannot forget one.+"""++from __future__ import annotations++import os+from pathlib import Path+from typing import List++SERVER_ROOT = Path(__file__).resolve().parents[1]++# Directory names that are never backend source, wherever they appear.+NOT_SOURCE = frozenset({".venv", ".opencompany", ".machina", "__pycache__", "node_modules"})+++def server_python_files(*skip_top: str) -> List[Path]:+ """Every ``.py`` file under ``server/`` outside :data:`NOT_SOURCE` and+ the named top-level directories (``"tests"``, ``"scripts"``, ...).++ The names in ``skip_top`` are pruned at the top level only:+ ``services/skills/`` is backend code, and pruning ``skills`` at every+ depth once hid it from two of these tests.+ """+ skipped = frozenset(skip_top)+ files: List[Path] = []+ for root, dirs, names in os.walk(SERVER_ROOT):+ at_top = Path(root) == SERVER_ROOT+ dirs[:] = [name for name in dirs if name not in NOT_SOURCE and not (at_top and name in skipped)]+ files.extend(Path(root) / name for name in names if name.endswith(".py"))+ return filesdiff --git a/server/tests/core_config/test_js_runtime.py b/server/tests/core_config/test_js_runtime.pyindex 828b08a9..ca35f391 100644--- a/server/tests/core_config/test_js_runtime.py+++ b/server/tests/core_config/test_js_runtime.py@@ -5,13 +5,13 @@ the only JavaScript runtime the backend ever spawns."""from __future__ import annotations-import inspectimport jsonfrom pathlib import Pathimport pytestfrom core import js_runtime+from tests._source_tree import SERVER_ROOT, server_python_filesdef test_override_wins_over_path(monkeypatch, tmp_path):@@ -96,12 +96,9 @@ def test_backend_never_spawns_node_or_npm():A regression that reintroduces ``shutil.which("node")`` /``which("npm")`` / an ``npx`` spawn anywhere under the backend treewould silently reintroduce a Node dependency."""- server = Path(inspect.getsourcefile(js_runtime)).resolve().parents[1]offenders: list[str] = []- for path in server.rglob("*.py"):- rel = path.relative_to(server).as_posix()- if rel.startswith(("tests/", ".venv/", "experiments/", "scripts/")) or "/node_modules/" in rel:- continue+ for path in server_python_files("tests", "experiments", "scripts"):+ rel = path.relative_to(SERVER_ROOT).as_posix()text = path.read_text(encoding="utf-8", errors="ignore")for needle in ('which("node")', "which('node')", 'which("npm")', "which('npm')", 'which("npx")', "which('npx')"):if needle in text:diff --git a/server/tests/llm/test_langchain_removed.py b/server/tests/llm/test_langchain_removed.pyindex 68a66a5d..5d4de80f 100644--- a/server/tests/llm/test_langchain_removed.py+++ b/server/tests/llm/test_langchain_removed.py@@ -23,6 +23,8 @@ from typing import Iterable, NamedTupleimport pytest+from tests._source_tree import server_python_files+pytestmark = pytest.mark.unitSERVER_ROOT = Path(__file__).resolve().parents[2]@@ -107,13 +109,8 @@ def _imports(path: Path) -> Iterable[_ImportUse]:def _production_sources() -> list[Path]:# Installed mobile engines are isolated subprocess dependencies, not server- # source. Prune runtime trees before walking their SDKs and virtualenvs.- excluded = {"tests", ".venv", ".opencompany", ".machina", "__pycache__"}- sources = []- for root, dirs, files in os.walk(SERVER_ROOT):- dirs[:] = [name for name in dirs if name not in excluded]- sources.extend(Path(root) / name for name in files if name.endswith(".py"))- return sources+ # source; the walker prunes runtime trees and the virtualenv.+ return server_python_files("tests")def test_no_module_imports_langchain():diff --git a/server/tests/test_no_raw_prints.py b/server/tests/test_no_raw_prints.pyindex bc9b3f3d..bf024895 100644--- a/server/tests/test_no_raw_prints.py+++ b/server/tests/test_no_raw_prints.py@@ -15,30 +15,27 @@ Three sanctioned exemptions, each documented in its own module:``print`` builtin we hand to user code (the user's print, not ours).Anything else must go through ``logger = get_logger(__name__)``. This-test parses every ``.py`` file under ``server/`` (excluding ``.venv``,-``tests/``, ``scripts/``, and the docs-only ``skills/`` markdown trees)-and walks each ``ast.Call`` node looking for bare ``print(...)``.+test parses every backend ``.py`` file under ``server/`` (excluding the+virtualenv, runtime data, ``tests/``, ``scripts/``, and the docs-only+``skills/`` markdown trees) and walks each ``ast.Call`` node looking for+bare ``print(...)``."""from __future__ import annotationsimport ast-import os-from pathlib import Pathfrom typing import List, Tuple-SERVER_ROOT = Path(__file__).resolve().parents[1]+from tests._source_tree import SERVER_ROOT, server_python_files-# Directories whose ``print()`` calls are out of scope.-_EXCLUDED_DIRS = {- ".opencompany", # installed runtimes belong to isolated subprocesses- ".machina", # legacy runtime data directory- ".venv",+# Top-level directories whose ``print()`` calls are out of scope. The walker+# also skips the virtualenv and runtime data: installed runtimes such as the+# Mobile phone runtime belong to isolated subprocesses.+_EXCLUDED_TOP_DIRS = ("tests", # tests themselves can print freely"scripts", # CLI smoke tests are intentional stdout tools"skills", # SKILL.md markdown — not Python source- "__pycache__",-}+)# Each entry: (relative module path, name of the enclosing function/method# whose body is allowed to contain ``print(``). The function name is@@ -61,15 +58,6 @@ _SANCTIONED: List[Tuple[str, str]] = []-def _iter_python_files() -> List[Path]:- """Yield every .py file under ``server/`` outside the exclusion set."""- files: List[Path] = []- for root, dirs, names in os.walk(SERVER_ROOT):- dirs[:] = [name for name in dirs if name not in _EXCLUDED_DIRS]- files.extend(Path(root) / name for name in names if name.endswith(".py"))- return files--def _enclosing_function_name(tree: ast.AST, target: ast.AST) -> str:"""Return the name of the deepest ``FunctionDef`` ancestor of ``target``.@@ -96,7 +84,7 @@ def test_no_raw_prints_in_server_code() -> None:sanctioned_map.setdefault(rel.replace("\\", "/"), set()).add(fn)violations: List[str] = []- for py in _iter_python_files():+ for py in server_python_files(*_EXCLUDED_TOP_DIRS):rel = py.relative_to(SERVER_ROOT).as_posix()try:source = py.read_text(encoding="utf-8")diff --git a/server/tests/test_no_root_climb.py b/server/tests/test_no_root_climb.pyindex 1299a542..f11a0d80 100644--- a/server/tests/test_no_root_climb.py+++ b/server/tests/test_no_root_climb.py@@ -27,9 +27,11 @@ import astfrom pathlib import Pathfrom typing import List-SERVER_ROOT = Path(__file__).resolve().parents[1]+from tests._source_tree import SERVER_ROOT, server_python_files-_EXCLUDED_DIRS = {".venv", "tests", "scripts", "skills", "__pycache__", "nodejs", "static"}+# Top-level trees that are not backend modules. The walker also skips the+# virtualenv and runtime data such as the Mobile phone runtime.+_EXCLUDED_TOP_DIRS = ("tests", "scripts", "skills", "nodejs", "static")_SANCTIONED = {"core/approot.py",@@ -42,16 +44,6 @@ _SANCTIONED = {_LAYOUT_LITERALS = {("client", "dist"), (".env.template",), ("package.json",)}-def _iter_python_files() -> List[Path]:- files: List[Path] = []- for path in SERVER_ROOT.rglob("*.py"):- rel_parts = path.relative_to(SERVER_ROOT).parts- if any(part in _EXCLUDED_DIRS for part in rel_parts):- continue- files.append(path)- return files--def _is_file_anchor(node: ast.AST) -> bool:"""True for ``Path(__file__)`` and ``Path(__file__).resolve()`` chains."""while isinstance(node, ast.Call):@@ -117,7 +109,7 @@ def _violations_in(path: Path, tree: ast.AST) -> List[str]:def test_only_approot_locates_the_app_root() -> None:violations: List[str] = []- for py in _iter_python_files():+ for py in server_python_files(*_EXCLUDED_TOP_DIRS):rel = py.relative_to(SERVER_ROOT).as_posix()if rel in _SANCTIONED:continuediff --git a/server/tests/test_status_broadcasts.py b/server/tests/test_status_broadcasts.pyindex d2dad2f2..8a1ea182 100644--- a/server/tests/test_status_broadcasts.py+++ b/server/tests/test_status_broadcasts.py@@ -434,16 +434,13 @@ class TestSendCustomEventPayload:@pytest.fixturedef callsite_files(self) -> list[str]:- """All files under ``server/`` containing ``send_custom_event``."""- from pathlib import Path+ """Backend files outside ``tests/`` containing ``send_custom_event``."""+ from tests._source_tree import SERVER_ROOT, server_python_files- root = Path(__file__).resolve().parent.parent # server/return [- str(p.relative_to(root)).replace("\\", "/")- for p in root.rglob("*.py")+ p.relative_to(SERVER_ROOT).as_posix()+ for p in server_python_files("tests")if "send_custom_event" in p.read_text(encoding="utf-8")- and "tests/" not in str(p).replace("\\", "/")- and p.name != "test_status_broadcasts.py"]def test_definition_lives_on_status_broadcaster(self):