Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
"""Invariant: only sanctioned ``print()`` calls are allowed in server code.
The supervisor (``cli/colors.py``) prepends ``[HH:MM:SS.fff]`` to every
aggregated line and ``configure_logging`` is the single source of truth
for structured logs. Stray ``print()`` calls bypass structlog's
processor chain — they don't get the level tag, don't get
``contextvars`` (``workflow_id`` / ``node_id``), don't go to the
RotatingFileHandler, and don't surface in the WebSocket Terminal panel.
Sanctioned exemptions include, each documented in its own module:
1. ``main._startup_log`` — pre-logger boot-progress markers.
2. ``core.container._clog`` — same role for DI-container bootstrap.
3. ``nodes.code.python_executor.captured_print`` — the sandboxed
``print`` builtin we hand to user code (the user's print, not ours).
4. ``services.employees.reconcile_jobs._run_cli`` — content-free NDJSON
command output consumed by maintenance scripts, rather than runtime logs.
Anything else must go through ``logger = get_logger(__name__)``. This
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 annotations
import ast
from typing import List, Tuple
from tests._source_tree import SERVER_ROOT, server_python_files
# 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
)
# Each entry: (relative module path, name of the enclosing function/method
# whose body is allowed to contain ``print(``). The function name is
# matched against the *deepest enclosing* ``FunctionDef`` for each call
# site, so a stray top-level ``print()`` in the same file still fails.
_SANCTIONED: List[Tuple[str, str]] = [
("main.py", "_startup_log"),
("core/container.py", "_clog"),
("nodes/code/python_executor/__init__.py", "captured_print"),
# Build-time Temporal installer — runs in its own one-shot subprocess
# BEFORE the structlog pipeline is configured (same role as
# ``_startup_log`` / ``_clog`` for the main process).
("services/temporal/_install.py", "_main"),
# The Browser node's one-shot CLIs: pre-fetching its runtime and bumping
# its pins. Same role as the Temporal installer above.
("nodes/browser/_install.py", "_main"),
("nodes/browser/_pin.py", "main"),
# JSON wire replies to the parent process, not application log messages.
("nodes/mobile/runtime/device_server.py", "main"),
# One-shot maintenance CLI: stdout is its content-free NDJSON report,
# consumed by operators/scripts, rather than application log messages.
("services/employees/reconcile_jobs.py", "_run_cli"),
]
def _enclosing_function_name(tree: ast.AST, target: ast.AST) -> str:
"""Return the name of the deepest ``FunctionDef`` ancestor of ``target``.
Returns ``""`` when the call is at module level (no enclosing
function) — those calls are always violations.
"""
parents: dict[ast.AST, ast.AST] = {}
for parent in ast.walk(tree):
for child in ast.iter_child_nodes(parent):
parents[child] = parent
node: ast.AST | None = target
while node is not None:
if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
return node.name
node = parents.get(node)
return ""
def test_no_raw_prints_in_server_code() -> None:
"""Every ``print(`` call in server runtime code is in an allow-listed body."""
# Multimap: a single module may sanction multiple entry-point bodies.
sanctioned_map: dict[str, set[str]] = {}
for rel, fn in _SANCTIONED:
sanctioned_map.setdefault(rel.replace("\\", "/"), set()).add(fn)
violations: List[str] = []
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")
except OSError:
continue
try:
tree = ast.parse(source, filename=str(py))
except SyntaxError:
continue
for node in ast.walk(tree):
if not isinstance(node, ast.Call):
continue
func = node.func
if not (isinstance(func, ast.Name) and func.id == "print"):
continue
enclosing = _enclosing_function_name(tree, node)
allowed_fns = sanctioned_map.get(rel)
if allowed_fns is not None and enclosing in allowed_fns:
continue
violations.append(
f" {rel}:{node.lineno} — print() inside {enclosing or '<module>'}() "
f"(use ``logger = get_logger(__name__); logger.info/warning/error(...)`` "
f"or one of the sanctioned helpers _startup_log / _clog)"
)
assert not violations, (
"Raw ``print()`` calls bypass the structlog pipeline (no level tag, "
"no contextvars, no file handler, no WS Terminal panel). Convert "
"to ``logger.<level>(...)`` or move into a sanctioned helper:\n\n" + "\n".join(violations)
)