Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

fix(realtime): honor the user's agent-instructions file in the voice session

Ruben Lütke4d2a4ed30431
diff --git a/jarvis/realtime/session.py b/jarvis/realtime/session.py
index 032ad21c15..6efbe8a449 100644
--- a/jarvis/realtime/session.py
+++ b/jarvis/realtime/session.py
@@ -453,6 +453,33 @@ _TOOL_ROLE_DIRECTIVE = (
)
+# Cap for the user agent-instructions content inside the realtime session
+# instructions. The block is re-sent with every per-turn session update, so a
+# pathologically large file must never bloat that hot path; typical files are
+# a few hundred characters and pass through untouched.
+_PREFERENCES_MAX_CHARS = 4000
+
+
+def _preferences_block(config: Any) -> str:
+ """The user's standing-instructions block (``Ruben.md`` equivalent).
+
+ The realtime engine speaks directly to the user, so it must honor the same
+ user-editable agent-instructions file as the classic deep brain — otherwise
+ tone/language/address preferences apply only on delegated turns and the
+ voice flips style mid-conversation. Read fresh per call so an edit applies
+ on the next turn (the UI promises "no restart needed"); degrade to ``""``
+ so a read fault never blocks the session handshake.
+ """
+ try:
+ from jarvis.brain import agent_instructions
+
+ return agent_instructions.render_for_prompt(
+ config, max_chars=_PREFERENCES_MAX_CHARS
+ )
+ except Exception: # noqa: BLE001 — never break the voice session on a prefs fault
+ return ""
+
+
def _session_instructions(
language: str,
*,
@@ -461,6 +488,7 @@ def _session_instructions(
model: str = "",
language_is_pinned: bool = True,
tool_directive: str = "",
+ preferences: str = "",
) -> str:
from jarvis.brain.persona_loader import load_effective_persona_prompt
@@ -489,6 +517,11 @@ def _session_instructions(
)
parts = [
persona,
+ # The user's own standing instructions come right after the persona and
+ # before every operational directive: they refine who the assistant is
+ # for THIS user (tone, dialect, address, defaults) and must frame the
+ # whole spoken output, while safety and tool rules below stay above them.
+ preferences,
tool_directive,
_REALTIME_SAFETY_APPENDIX,
input_directive,
@@ -819,6 +852,7 @@ class RealtimeVoiceSession:
model=model,
language_is_pinned=self._language_is_pinned,
tool_directive=self._tool_directive(),
+ preferences=_preferences_block(self._config),
),
language=self._language,
input_language=self._input_language,
@@ -1165,6 +1199,7 @@ class RealtimeVoiceSession:
not turn_plan.requires_orchestrator
),
),
+ preferences=_preferences_block(self._config),
),
"language": new_language,
}
diff --git a/tests/unit/realtime/test_session.py b/tests/unit/realtime/test_session.py
index a167b298b9..41e72a95c0 100644
--- a/tests/unit/realtime/test_session.py
+++ b/tests/unit/realtime/test_session.py
@@ -4240,3 +4240,106 @@ async def test_scrub_cancel_records_spoken_fallback_on_the_spoken_track():
assert spoken[0].spoken_kind == "withheld"
assert spoken[0].detail == reason
assert spoken[0].text == sess._gate.fallback_phrase()
+
+
+# ---------------------------------------------------------------------------
+# User agent-instructions (the Ruben.md-equivalent file) in the realtime path
+# ---------------------------------------------------------------------------
+
+
+def test_session_instructions_place_preferences_between_persona_and_directives(
+ monkeypatch,
+):
+ """The user's standing-instructions block must sit right after the persona
+ and before the operational directives, so it frames the whole spoken output."""
+ from jarvis.brain import persona_loader
+ from jarvis.realtime import session as session_mod
+
+ monkeypatch.setattr(
+ persona_loader, "load_effective_persona_prompt", lambda: "PERSONA_MARKER"
+ )
+ text = session_mod._session_instructions(
+ "de",
+ preferences="PREFS_MARKER",
+ tool_directive="TOOL_MARKER",
+ )
+ assert "PREFS_MARKER" in text
+ assert (
+ text.index("PERSONA_MARKER")
+ < text.index("PREFS_MARKER")
+ < text.index("TOOL_MARKER")
+ )
+
+
+def test_preferences_block_renders_the_user_file_with_the_realtime_cap(monkeypatch):
+ from jarvis.brain import agent_instructions
+ from jarvis.realtime import session as session_mod
+
+ seen = {}
+
+ def fake_render(config, *, max_chars=None):
+ seen["config"] = config
+ seen["max_chars"] = max_chars
+ return "RENDERED_PREFS"
+
+ monkeypatch.setattr(agent_instructions, "render_for_prompt", fake_render)
+ cfg = _cfg()
+ assert session_mod._preferences_block(cfg) == "RENDERED_PREFS"
+ assert seen["config"] is cfg
+ assert seen["max_chars"] == session_mod._PREFERENCES_MAX_CHARS
+
+
+def test_preferences_block_degrades_to_empty_on_a_read_fault(monkeypatch):
+ from jarvis.brain import agent_instructions
+ from jarvis.realtime import session as session_mod
+
+ def broken_render(config, *, max_chars=None):
+ raise RuntimeError("disk on fire")
+
+ monkeypatch.setattr(agent_instructions, "render_for_prompt", broken_render)
+ assert session_mod._preferences_block(_cfg()) == ""
+
+
+async def test_open_carries_user_agent_instructions_into_session_instructions(
+ monkeypatch, tmp_path
+):
+ """Regression: the realtime engine speaks directly to the user, so the
+ user's agent-instructions file must reach the provider's session
+ instructions. It previously reached only the classic deep brain, so voice
+ preferences (tone, dialect, address) applied on delegated turns but were
+ silently ignored on every direct realtime reply."""
+ from jarvis.brain import agent_instructions
+ from jarvis.core import config as core_config
+
+ monkeypatch.setattr(core_config, "DATA_DIR", tmp_path)
+ cfg = _cfg()
+ agent_instructions.save_agent_instructions(
+ cfg, "Always speak with a Bavarian accent."
+ )
+
+ provider = FakeProvider(
+ [
+ RealtimeEvent(type="input_transcript", text="hello", is_final=True),
+ RealtimeEvent(type="turn_complete"),
+ ]
+ )
+ sess = RealtimeVoiceSession(
+ session_id="s-prefs",
+ send_binary=lambda _data: asyncio.sleep(0),
+ send_json=lambda _message: asyncio.sleep(0),
+ provider=provider,
+ config=cfg,
+ bus=None,
+ )
+ await sess.handle_control({"type": "audio_start", "sample_rate": 16_000})
+ await sess.wait_finished()
+ await sess.end(reason="test")
+
+ opened = provider.opened_with.instructions
+ assert "Always speak with a Bavarian accent." in opened
+ assert "USER PREFERENCES & STANDING INSTRUCTIONS" in opened
+ # The per-turn session update must re-read the file too — an edit applies
+ # on the next message, exactly as the Settings view promises.
+ updated = provider.session.session_updates[-1]["instructions"]
+ assert "Always speak with a Bavarian accent." in updated