Skip to content

Personal AI, On Personal Devices

Python
10.6 KB
Raw Blame
"""Regression tests for #263 — ``jarvis serve`` must build the system once.

serve.py used to construct all heavy components inline and then call
``SystemBuilder(config).build()`` a second time inside the scheduler block,
re-discovering the engine, re-instrumenting it, re-resolving tools, re-opening
the channel and re-creating the agent manager — ~30-40s of redundant work.

These tests pin the fix:

1. ``SystemBuilder.build`` is never called during ``jarvis serve`` startup
   (the duplicate build is gone).
2. The ``AgentExecutor`` still receives a system exposing the attributes it
   actually reads: ``tool_executor``, ``session_store``, ``memory_backend``,
   plus ``engine`` / ``model`` / ``config``.
"""

from __future__ import annotations

import importlib
from unittest.mock import MagicMock, patch

import pytest
from click.testing import CliRunner

from openjarvis.cli import cli

pytest.importorskip("fastapi")
pytest.importorskip("uvicorn")

# ``openjarvis.cli.serve`` as an attribute resolves to the click *command*
# (re-exported on the package); grab the real module to monkeypatch its globals.
serve_mod = importlib.import_module("openjarvis.cli.serve")


def _fake_engine() -> MagicMock:
    engine = MagicMock()
    engine.list_models.return_value = ["test-model"]
    engine.health.return_value = True
    engine.name = "mock"
    return engine


def _repopulate_registries() -> None:
    """Re-run the @register decorators wiped by the autouse conftest fixture.

    The tool/memory modules are import-cached, so a plain ``import`` inside
    serve.py is a no-op after the registries are cleared per-test. Reload the
    individual submodules so ToolRegistry/MemoryRegistry are populated exactly
    as they would be on a fresh process — otherwise serve would resolve an
    empty tool list and no memory backend, masking the very wiring under test.
    """
    import importlib
    import sys

    import openjarvis.agents  # noqa: F401
    import openjarvis.tools  # noqa: F401
    import openjarvis.tools.storage  # noqa: F401
    from openjarvis.core.registry import (
        AgentRegistry,
        MemoryRegistry,
        ToolRegistry,
    )

    if not AgentRegistry.keys():
        for mod_name in list(sys.modules):
            if mod_name.startswith("openjarvis.agents.") and not mod_name.endswith(
                "_stubs"
            ):
                try:
                    importlib.reload(sys.modules[mod_name])
                except Exception:
                    pass

    if not ToolRegistry.keys():
        for mod_name in list(sys.modules):
            if (
                mod_name.startswith("openjarvis.tools.")
                and not mod_name.endswith("_stubs")
                and not mod_name.endswith("agent_tools")
            ):
                try:
                    importlib.reload(sys.modules[mod_name])
                except Exception:
                    pass

    if not MemoryRegistry.keys():
        for mod_name in list(sys.modules):
            if mod_name.startswith(
                "openjarvis.tools.storage."
            ) and not mod_name.endswith("_stubs"):
                try:
                    importlib.reload(sys.modules[mod_name])
                except Exception:
                    pass


def _run_serve(
    tmp_path,
    monkeypatch,
    *,
    build_spy,
    set_system_spy,
    security_primitives=None,
    channel_backend=None,
):
    """Invoke ``jarvis serve`` with all heavy/blocking pieces stubbed out.

    Returns the CliRunner result. The server is never actually started
    (``run_server`` is a no-op) and no real engine is contacted.
    """
    from openjarvis.core.config import JarvisConfig
    from openjarvis.core.registry import MemoryRegistry

    _repopulate_registries()

    config = JarvisConfig()
    # Keep the scheduler block alive (it owns the executor wiring under test)
    # while pointing every store at the temp dir.
    config.agent_manager.enabled = True
    config.agent_manager.db_path = str(tmp_path / "agents.db")
    config.sessions.enabled = True
    config.sessions.db_path = str(tmp_path / "sessions.db")
    config.memory.db_path = str(tmp_path / "memory.db")
    # Disabling prompt-context injection must not disable the backend needed
    # by explicitly configured memory tools in managed-agent ticks.
    config.agent.context_from_memory = False
    config.telemetry.enabled = False
    config.traces.enabled = False
    config.channel.enabled = channel_backend is not None
    if channel_backend is not None:
        config.channel.default_channel = "test-channel"
    config.skills.enabled = False
    config.server.host = "127.0.0.1"
    config.server.port = 8123
    # Resolve a model without contacting a real engine / discovery.
    config.intelligence.default_model = "test-model"

    engine = _fake_engine()
    # Keep this wiring test independent of the optional native memory runtime.
    # The assertion is that serve resolves and passes a backend even when
    # prompt-context injection is disabled, not that SQLite itself works.
    memory_backend = MagicMock(name="memory_backend")
    monkeypatch.setattr(MemoryRegistry, "contains", MagicMock(return_value=True))
    monkeypatch.setattr(
        MemoryRegistry,
        "create",
        MagicMock(return_value=memory_backend),
    )

    monkeypatch.setattr(serve_mod, "load_config", lambda *a, **k: config)
    monkeypatch.setattr(serve_mod, "get_engine", lambda *a, **k: ("mock", engine))
    monkeypatch.setattr(serve_mod, "discover_engines", lambda *a, **k: {})
    monkeypatch.setattr(serve_mod, "discover_models", lambda *a, **k: {})
    if channel_backend is not None:
        monkeypatch.setattr(
            "openjarvis.system.builder.SystemBuilder._resolve_channel",
            lambda *args, **kwargs: channel_backend,
        )

    # setup_security returns its own context; pass the engine straight through
    # so we don't need real guardrails wired up.
    sec = MagicMock()
    sec.engine = engine
    if security_primitives is None:
        security_primitives = (None, None, None)
    sec.capability_policy, sec.rate_limiter, sec.audit_logger = security_primitives
    monkeypatch.setattr("openjarvis.security.setup_security", lambda *a, **k: sec)

    with (
        patch(
            "openjarvis.system.builder.SystemBuilder.build",
            build_spy,
        ),
        patch(
            "openjarvis.agents.executor.AgentExecutor.set_system",
            set_system_spy,
        ),
        patch("openjarvis.server.daemon.run_server", lambda *a, **k: None),
    ):
        return CliRunner().invoke(cli, ["serve"], catch_exceptions=False)


def test_serve_does_not_call_systembuilder_build(tmp_path, monkeypatch):
    """The redundant second full build is gone (#263)."""
    build_spy = MagicMock(
        side_effect=AssertionError(
            "SystemBuilder.build() must not run during `jarvis serve` startup "
            "— it is the duplicate build #263 removed."
        )
    )
    set_system_spy = MagicMock()
    inject_spy = MagicMock()
    monkeypatch.setattr(serve_mod, "inject_credentials", inject_spy)

    result = _run_serve(
        tmp_path,
        monkeypatch,
        build_spy=build_spy,
        set_system_spy=set_system_spy,
    )

    assert result.exit_code == 0, result.output
    build_spy.assert_not_called()
    inject_spy.assert_called_once_with()


def test_serve_passes_environment_cors_origins(tmp_path, monkeypatch):
    """The normal CLI path must not mask the environment override."""
    monkeypatch.setenv(
        "OPENJARVIS_CORS_ORIGINS",
        "https://frontend.example,https://admin.example",
    )
    create_app = MagicMock(return_value=MagicMock())

    with patch("openjarvis.server.app.create_app", create_app):
        result = _run_serve(
            tmp_path,
            monkeypatch,
            build_spy=MagicMock(),
            set_system_spy=MagicMock(),
        )

    assert result.exit_code == 0, result.output
    assert create_app.call_args.kwargs["cors_origins"] == [
        "https://frontend.example",
        "https://admin.example",
    ]


def test_executor_receives_required_system_attrs(tmp_path, monkeypatch):
    """The executor still gets a system exposing the attributes it reads.

    AgentExecutor reads engine/model/config/memory_backend/tool_executor/
    session_store off ``self._system``; the de-dup must not strip any of them.
    """
    build_spy = MagicMock()
    captured: dict = {}

    def _capture_set_system(self, system):  # noqa: ANN001
        captured["system"] = system
        # Preserve real behaviour so the executor is usable afterwards.
        self._system = system

    policy = object()
    limiter = object()
    audit = object()
    result = _run_serve(
        tmp_path,
        monkeypatch,
        build_spy=build_spy,
        set_system_spy=_capture_set_system,
        security_primitives=(policy, limiter, audit),
    )

    assert result.exit_code == 0, result.output
    # Built once, from the inline components — not via SystemBuilder.build().
    build_spy.assert_not_called()

    system = captured.get("system")
    assert system is not None, "executor.set_system was never called"

    # Correctness constraint from the verifier: these must survive the de-dup.
    assert system.tool_executor is not None
    assert system.session_store is not None
    assert system.memory_backend is not None

    # And the basics the executor resolves engine/model from.
    assert system.engine is not None
    assert system.model == "test-model"
    assert system.config is not None
    assert system.capability_policy is policy
    assert system.rate_limiter is limiter
    assert system.audit_logger is audit
    assert system.tool_executor._capability_policy is policy
    assert system.tool_executor._rate_limiter is limiter
    assert system.tool_executor._agent_id == system.config.server.agent


def test_channel_system_receives_remote_security_primitives(tmp_path, monkeypatch):
    from openjarvis.system import JarvisSystem

    captured = {}

    def _capture_wire(self, channel):
        captured["system"] = self

    monkeypatch.setattr(JarvisSystem, "wire_channel", _capture_wire)
    policy = object()
    limiter = object()
    audit = object()
    channel = MagicMock(channel_id="test-channel")

    result = _run_serve(
        tmp_path,
        monkeypatch,
        build_spy=MagicMock(),
        set_system_spy=MagicMock(),
        security_primitives=(policy, limiter, audit),
        channel_backend=channel,
    )

    assert result.exit_code == 0, result.output
    system = captured.get("system")
    assert system is not None
    assert system.capability_policy is policy
    assert system.rate_limiter is limiter
    assert system.audit_logger is audit