Personal AI, On Personal Devices
Python
fix: wire persona prompt_builder into the serve + SDK agent paths
Gilbert Barajas6e40d87eb5d7
diff --git a/src/openjarvis/cli/serve.py b/src/openjarvis/cli/serve.pyindex ed95c35b..eabc9e9e 100644--- a/src/openjarvis/cli/serve.py+++ b/src/openjarvis/cli/serve.py@@ -346,6 +346,27 @@ def serve(if getattr(agent_cls, "accepts_tools", False):agent_kwargs["max_turns"] = config.agent.max_turns+ # Wire the SystemPromptBuilder so SOUL.md / MEMORY.md / USER.md+ # reach the model on the SERVE path too. ``ask.py`` has done+ # this since the persona system landed; ``serve.py`` never did,+ # so an agent served over HTTP silently answered as a generic+ # assistant while the same agent via the CLI kept its persona.+ # Guarded so agents whose ``__init__`` doesn't accept the kwarg+ # (e.g. OrchestratorAgent) opt out automatically.+ import inspect as _inspect++ if (+ "prompt_builder"+ in _inspect.signature(agent_cls.__init__).parameters+ ):+ from openjarvis.prompt.builder import SystemPromptBuilder++ agent_kwargs["prompt_builder"] = SystemPromptBuilder(+ agent_template=config.agent.default_system_prompt or "",+ memory_files_config=config.memory_files,+ system_prompt_config=config.system_prompt,+ )+agent = agent_cls(engine, model_name, **agent_kwargs)# Pin MCP transports to the agent's lifetime so HTTP# connections don't close mid-request (#461).diff --git a/src/openjarvis/sdk.py b/src/openjarvis/sdk.pyindex 81d58a1f..ef510211 100644--- a/src/openjarvis/sdk.py+++ b/src/openjarvis/sdk.py@@ -516,6 +516,20 @@ class Jarvis:existing = agent_kwargs.get("tools", [])agent_kwargs["tools"] = digest_tools + list(existing)+ # Wire the SystemPromptBuilder so SOUL.md / MEMORY.md / USER.md reach+ # the model — mirrors ``cli/ask.py`` and ``cli/serve.py``. Guarded so+ # agents whose ``__init__`` doesn't accept the kwarg opt out.+ import inspect as _inspect++ if "prompt_builder" in _inspect.signature(agent_cls.__init__).parameters:+ from openjarvis.prompt.builder import SystemPromptBuilder++ agent_kwargs["prompt_builder"] = SystemPromptBuilder(+ agent_template=self._config.agent.default_system_prompt or "",+ memory_files_config=self._config.memory_files,+ system_prompt_config=self._config.system_prompt,+ )+agent_obj = agent_cls(self._engine, model_name, **agent_kwargs)ctx = AgentContext()diff --git a/tests/cli/test_serve_persona.py b/tests/cli/test_serve_persona.pynew file mode 100644index 00000000..67445cba--- /dev/null+++ b/tests/cli/test_serve_persona.py@@ -0,0 +1,115 @@+"""Regression: ``jarvis serve`` must wire the ``SystemPromptBuilder`` into the+agent it constructs, so SOUL.md / MEMORY.md / USER.md reach the model over HTTP.++``cli/ask.py`` (and ``cli/chat_cmd.py`` and the managed-agent executor) have+wired the builder since the persona system landed. The serve path never did, so+an agent served over HTTP silently answered as a generic assistant — explicitly+denying the persona — while the same agent via the CLI kept it. Found deploying+a personal assistant: SOUL.md was correct on disk the whole time; no error, no+warning.++This test boots ``serve`` just far enough to capture the agent handed to+``create_app`` and asserts the builder (and thus the persona content) is+present. It fails on the unpatched serve path: ``agent._prompt_builder`` is+``None``, so the persona files never reach the model.+"""++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 a package attribute resolves to the click+# *command* (re-exported); 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"+ engine.engine_id = "mock"+ return engine+++def test_serve_wires_persona_builder_into_served_agent(tmp_path, monkeypatch):+ """The agent built on the serve path must carry a SystemPromptBuilder whose+ assembled prompt includes SOUL.md content (regression for the HTTP persona+ loss)."""+ from openjarvis.agents.simple import SimpleAgent+ from openjarvis.core.config import JarvisConfig+ from openjarvis.core.registry import AgentRegistry++ # Persona file with a unique sentinel we can grep for in the built prompt.+ soul = tmp_path / "SOUL.md"+ soul.write_text("SERVE_PERSONA_SENTINEL", encoding="utf-8")++ # conftest clears registries per-test; re-register the agent we exercise.+ # (SimpleAgent extends BaseAgent, whose __init__ accepts ``prompt_builder``.)+ if not AgentRegistry.contains("simple"):+ AgentRegistry.register_value("simple", SimpleAgent)++ config = JarvisConfig()+ config.server.host = "127.0.0.1"+ config.server.port = 8123+ config.intelligence.default_model = "test-model"+ config.memory_files.soul_path = str(soul)+ # Keep the heavy optional subsystems off so we reach create_app cleanly.+ config.telemetry.enabled = False+ config.agent_manager.enabled = False+ config.sessions.enabled = False+ config.channel.enabled = False+ config.skills.enabled = False+ config.agent.context_from_memory = False++ engine = _fake_engine()+ 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: {})++ # setup_security returns its own context; pass the engine straight through.+ sec = MagicMock()+ sec.engine = engine+ sec.capability_policy = None+ sec.audit_logger = None+ monkeypatch.setattr("openjarvis.security.setup_security", lambda *a, **k: sec)++ captured: dict = {}++ def _capture_create_app(*args, **kwargs):+ captured["agent"] = kwargs.get("agent")+ return MagicMock(name="app")++ with (+ patch("openjarvis.server.app.create_app", side_effect=_capture_create_app),+ patch("uvicorn.run", lambda *a, **k: None),+ ):+ result = CliRunner().invoke(+ cli, ["serve", "--agent", "simple"], catch_exceptions=False+ )++ assert result.exit_code == 0, result.output+ agent = captured.get("agent")+ assert agent is not None, (+ "serve did not construct an agent or never reached create_app; "+ f"output:\n{result.output}"+ )+ # The regression: without the fix ``agent._prompt_builder`` is None and the+ # persona files never reach the model over HTTP.+ assert agent._prompt_builder is not None, (+ "serve constructed the agent without a prompt_builder — SOUL.md / "+ "MEMORY.md / USER.md would be silently dropped on the HTTP path."+ )+ assert "SERVE_PERSONA_SENTINEL" in agent._prompt_builder.build(), (+ "prompt_builder is wired on serve, but its built prompt omits SOUL.md"+ )