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.

perf(brain): bound delegated realtime turns with a wall-clock deadline

Ruben Lütke17f8aaf3365a
diff --git a/jarvis/brain/manager.py b/jarvis/brain/manager.py
index 1a343f42ee..9fa36bbbf1 100644
--- a/jarvis/brain/manager.py
+++ b/jarvis/brain/manager.py
@@ -342,6 +342,16 @@ TIER_DEFAULTS_BY_PROVIDER: dict[str, dict[str, str]] = {
}
+# Hard loop bounds for DELEGATED realtime voice turns (prefer_tool_model).
+# A voice turn must stay conversational: 6 rounds cover plan → 2-3 tools →
+# answer, and 20 s wall clock is the worst acceptable wait before the loop
+# forces one final tool-less answer round (see ToolUseLoop.deadline_s).
+# Live incident 2026-07-14: an unbounded delegate ran 14 rounds / 66 s.
+# Classic chat turns keep the dispatcher defaults (15 rounds, no deadline).
+_DELEGATE_MAX_TURNS: int = 6
+_DELEGATE_DEADLINE_S: float = 20.0
+
+
def _resolve_tier_model(
tier: str,
provider: str,
@@ -2622,6 +2632,8 @@ class BrainManager:
brain: Brain,
*,
tools_override: dict[str, Tool] | None = None,
+ max_turns: int | None = None,
+ deadline_s: float | None = None,
) -> BrainDispatcher:
"""Builds the dispatcher with an optional tool override.
@@ -2629,6 +2641,10 @@ class BrainManager:
clearly identified, ``tools_override={}`` is set — the LLM then has no
tools in its toolbox and cannot be tempted to hallucinate
``spawn_worker``. ``None`` (default) = full tool visibility.
+
+ ``max_turns`` / ``deadline_s`` (2026-07-14): per-turn loop bounds for
+ delegated realtime voice turns — see ``_DELEGATE_MAX_TURNS`` /
+ ``_DELEGATE_DEADLINE_S``. ``None`` keeps the dispatcher defaults.
"""
tools = tools_override if tools_override is not None else self._tools
system_prompt = self._build_system_prompt()
@@ -2638,12 +2654,17 @@ class BrainManager:
cards = self._plugin_usage_cards_block(tools)
if cards:
system_prompt = f"{system_prompt}\n\n{cards}"
+ kwargs: dict[str, Any] = {}
+ if max_turns is not None:
+ kwargs["max_turns"] = max_turns
return BrainDispatcher(
brain,
tools=tools,
executor=self._tool_executor,
system_prompt=system_prompt,
max_tokens=self._config.brain.max_tokens,
+ deadline_s=deadline_s,
+ **kwargs,
)
def _build_tool_ack_emitter(
@@ -7656,7 +7677,23 @@ class BrainManager:
# Set here — after dead/cooldown skips — so it always names the
# provider that genuinely runs this attempt, including a fallback win.
self._active_turn_identity = (prov_name, model)
- disp = self._build_dispatcher(brain, tools_override=_turn_tools)
+ # Delegated realtime voice turns get hard loop bounds: a voice
+ # user is gone long before round 14 (live 2026-07-14: an
+ # unbounded delegate ran 14 rounds / 66 s on "what is in my
+ # wiki"). On deadline the loop forces ONE final tool-less round,
+ # so the user still hears a grounded answer. Classic turns call
+ # with the unchanged signature (kwargs only on delegation).
+ _disp_kwargs: dict[str, Any] = (
+ {
+ "max_turns": _DELEGATE_MAX_TURNS,
+ "deadline_s": _DELEGATE_DEADLINE_S,
+ }
+ if prefer_tool_model
+ else {}
+ )
+ disp = self._build_dispatcher(
+ brain, tools_override=_turn_tools, **_disp_kwargs
+ )
# Intelligent router: the router LEAD must NOT stream its conversational
# text to TTS. On the streaming path (generate_stream) text_consumer
# speaks each chunk live DURING dispatch — so a no-tool router answer
diff --git a/tests/unit/brain/test_manager_drop_images.py b/tests/unit/brain/test_manager_drop_images.py
index 5279ccc927..6ce4f0abf1 100644
--- a/tests/unit/brain/test_manager_drop_images.py
+++ b/tests/unit/brain/test_manager_drop_images.py
@@ -49,7 +49,7 @@ def _manager_with_recorder() -> tuple[BrainManager, _RecordingDispatcher]:
recorder = _RecordingDispatcher()
manager._build_fallback_chain = lambda _level: [("fake", "fake-model")] # type: ignore[method-assign]
manager._get_brain = lambda _name, _model: _FakeBrain() # type: ignore[method-assign]
- manager._build_dispatcher = lambda _brain, *, tools_override=None: recorder # type: ignore[method-assign]
+ manager._build_dispatcher = lambda _brain, *, tools_override=None, **_kw: recorder # type: ignore[method-assign]
return manager, recorder