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): a crashed local server no longer ends the call

rubenluetke10-beepea36733bd391
diff --git a/jarvis/core/config.py b/jarvis/core/config.py
index d22cf6f041..c031509d07 100644
--- a/jarvis/core/config.py
+++ b/jarvis/core/config.py
@@ -609,6 +609,10 @@ class BrainProviderConfig(BaseModel):
voice: str = ""
auth_mode: str | None = None # "oauth" | "api_key"
base_url: str | None = None
+ # Self-hosted cards only (today: local-realtime): the command Jarvis runs
+ # to start/revive the server behind ``base_url`` when it is unreachable.
+ # Empty = Jarvis never spawns anything; the user runs the server manually.
+ launch_command: str = ""
# Latency sprint 1 (2026-04-30): Gemini thinking budget per provider tier.
# Value is forwarded to ``types.ThinkingConfig.thinking_budget``.
# ``None`` → SDK default (auto-budget, highest latency footprint).
diff --git a/jarvis/plugins/realtime/openai_realtime.py b/jarvis/plugins/realtime/openai_realtime.py
index 59833e7e78..60792f3245 100644
--- a/jarvis/plugins/realtime/openai_realtime.py
+++ b/jarvis/plugins/realtime/openai_realtime.py
@@ -298,10 +298,21 @@ class _OpenAIRealtimeSession:
session_payload: dict[str, Any] | None = None,
connect_model: str = "",
history_seed: tuple[dict[str, str], ...] = (),
+ rebuild_on_transport_death: bool = False,
) -> None:
self._conn = connection
self._connection_cm = connection_cm
self._client = client
+ # Capability consumed by the session pump (BUG-071): may a transport
+ # that died mid-call be reopened in place? Hosted cards keep today's
+ # deliberate terminal semantics (the BUG-064 stack self-heals
+ # internally and a credential/quota death must not loop). A
+ # SELF-HOSTED server is different: its process can crash and come
+ # back seconds later (live 2026-08-06 19:57 — the local server died
+ # silently mid-turn and the whole call ended reason=error although
+ # the server was healthy again within a minute), so its sessions
+ # opt in and the existing budgeted rebuild machinery does the rest.
+ self.rebuild_on_transport_death = bool(rebuild_on_transport_death)
self._events = connection.__aiter__()
self.session_id = session_id
# Model the transport was opened with — required to rebuild the
@@ -1123,6 +1134,7 @@ async def _open_realtime_session(
*,
model: str,
transcription_model: str | None = "gpt-4o-mini-transcribe",
+ rebuild_on_transport_death: bool = False,
) -> _OpenAIRealtimeSession:
"""Open, configure and hand back a live session on ``client``.
@@ -1166,6 +1178,7 @@ async def _open_realtime_session(
session_payload=payload,
connect_model=model,
history_seed=tuple(getattr(cfg, "history", ()) or ()),
+ rebuild_on_transport_death=rebuild_on_transport_death,
)
try:
await connection.session.update(session=payload)
@@ -1222,6 +1235,23 @@ class OpenAIRealtimeProvider:
#: else the install has.
_LOCAL_PROBE_TIMEOUT_S = 3.0
+#: How long a connect to the self-hosted server keeps retrying when Jarvis
+#: itself can revive the server (a ``launch_command`` is configured). A local
+#: stack needs real time to come back — the reference server loads its STT,
+#: LLM proxy and TTS models for ~25 s after a crash — and the mid-call
+#: transport rebuild (BUG-071) lands here, so giving up early turns every
+#: server crash into a dead call.
+_LOCAL_CONNECT_PATIENT_WINDOW_S = 45.0
+#: The window when NOBODY revives the server (no launch command): nothing is
+#: going to change, so only ride out a brief hiccup instead of holding the
+#: call hostage.
+_LOCAL_CONNECT_SHORT_WINDOW_S = 8.0
+_LOCAL_CONNECT_RETRY_STEP_S = 2.0
+#: Never respawn the server more often than this — a crash-looping server
+#: must not be hammered back up in a tight loop (AP-24 doctrine: mark it bad,
+#: do not thrash).
+_LOCAL_LAUNCH_MIN_INTERVAL_S = 60.0
+
def _normalize_local_root(url: str) -> str:
"""Normalize a user-entered server address to an ``…/v1`` API root.
@@ -1292,12 +1322,23 @@ class LocalRealtimeProvider:
# nothing ever reaching the server.
credential_candidates: tuple[tuple[str, str | None], ...] = ()
+ # Last server spawn across ALL provider instances: the factory rebuilds
+ # providers freely, and a per-instance stamp would let every rebuild spawn
+ # another server.
+ _last_launch_at: float = float("-inf")
+
def __init__(
- self, *, base_url: str = "", api_key: str = "", model: str = ""
+ self,
+ *,
+ base_url: str = "",
+ api_key: str = "",
+ model: str = "",
+ launch_command: str = "",
) -> None:
self._base_url = _normalize_local_root(base_url)
self._api_key = (api_key or "").strip()
self._model = (model or "").strip()
+ self._launch_command = (launch_command or "").strip()
# -- factory wiring ----------------------------------------------------
@@ -1335,6 +1376,7 @@ class LocalRealtimeProvider:
# behind a proxy or started vLLM with --api-key exports it once.
api_key=os.environ.get("JARVIS_LOCAL_REALTIME_API_KEY", ""),
model=str(getattr(provider_cfg, "model", "") or ""),
+ launch_command=str(getattr(provider_cfg, "launch_command", "") or ""),
)
# -- session -----------------------------------------------------------
@@ -1379,12 +1421,137 @@ class LocalRealtimeProvider:
return name
return _MODEL
+ def _connect_retry_window_s(self) -> float:
+ """How long a failing connect keeps retrying before giving up.
+
+ Patient only when patience can pay off: with a ``launch_command``
+ Jarvis revives a crashed server itself and the retries bridge its
+ warm-up. Without one nothing will change on the other end, so a
+ short window rides out a hiccup and then reports honestly.
+ """
+ if self._launch_command:
+ return _LOCAL_CONNECT_PATIENT_WINDOW_S
+ return _LOCAL_CONNECT_SHORT_WINDOW_S
+
+ @property
+ def handshake_budget_s(self) -> float:
+ """Declared handshake need (capability read by the session opener).
+
+ The shared 12 s handshake ceiling would behead the patient reconnect
+ that bridges a revived server's ~25 s model warm-up (the same
+ mechanism the Codex transport uses for its cold start, AP-21: a
+ declared capability, never a provider-name check). The margin on top
+ of the retry window covers the actual protocol handshake once the
+ server answers.
+ """
+ return self._connect_retry_window_s() + 15.0
+
+ def _server_is_local_process(self) -> bool:
+ """Whether the configured endpoint runs on THIS machine.
+
+ Only a loopback server may be spawned here — launching a process
+ because a LAN box went down would start a second server on the wrong
+ host.
+ """
+ host = self._base_url.split("://", 1)[-1].split("/", 1)[0].split(":", 1)[0]
+ return host.lower() in {"localhost", "127.0.0.1", "::1"}
+
+ def _maybe_launch_server(self) -> bool:
+ """Revive the self-hosted server, rate-limited. True if spawned.
+
+ The reference stack's whole process dies silently under load (live
+ 2026-08-06 19:57: its log ends mid-request, the call died with it,
+ and nothing owned bringing it back). With a configured
+ ``launch_command`` Jarvis owns that: spawn detached, window-less
+ (AP-1), stderr into a log under the data dir so the NEXT crash
+ finally leaves forensics. Rate-limited so a crash-looping server is
+ not hammered (AP-24 doctrine).
+ """
+ if not self._launch_command or not self._server_is_local_process():
+ return False
+ now = time.monotonic()
+ if now - LocalRealtimeProvider._last_launch_at < _LOCAL_LAUNCH_MIN_INTERVAL_S:
+ return False
+ LocalRealtimeProvider._last_launch_at = now
+ import subprocess # lazy (AP-26)
+ from pathlib import Path
+
+ from jarvis.core.process_utils import NO_WINDOW_CREATIONFLAGS # lazy
+
+ log_dir = Path(os.environ.get("JARVIS_DATA_DIR") or "data")
+ try:
+ log_dir.mkdir(parents=True, exist_ok=True)
+ server_log = open( # noqa: SIM115 — handed to the child for its lifetime
+ log_dir / "local_realtime_server.log", "ab"
+ )
+ except OSError:
+ server_log = None
+ try:
+ if os.name == "nt":
+ command: str | list[str] = self._launch_command
+ else:
+ import shlex
+
+ command = shlex.split(self._launch_command)
+ subprocess.Popen( # noqa: S603 — the maintainer configured this command
+ command,
+ stdin=subprocess.DEVNULL,
+ stdout=server_log or subprocess.DEVNULL,
+ stderr=subprocess.STDOUT,
+ creationflags=NO_WINDOW_CREATIONFLAGS,
+ )
+ except Exception as exc: # noqa: BLE001 — a bad command must not kill the call
+ log.warning(
+ "local-realtime: reviving the server failed (%s: %s) — "
+ "check [brain.providers.local-realtime].launch_command",
+ type(exc).__name__,
+ exc,
+ )
+ return False
+ finally:
+ if server_log is not None:
+ server_log.close()
+ log.info(
+ "local-realtime: server unreachable — launched the configured "
+ "server command; waiting for it to come up"
+ )
+ return True
+
async def open_session(self, cfg: Any) -> _OpenAIRealtimeSession:
if not self._base_url:
raise RuntimeError(
"No server URL configured for the self-hosted realtime provider "
"— set it on the provider card first (e.g. http://localhost:8080)."
)
+ deadline = time.monotonic() + self._connect_retry_window_s()
+ attempt = 0
+ while True:
+ attempt += 1
+ try:
+ return await self._open_session_once(cfg)
+ except asyncio.CancelledError:
+ raise
+ except Exception as exc: # noqa: BLE001 — bounded retry, last error re-raised
+ remaining = deadline - time.monotonic()
+ if remaining <= 0:
+ raise
+ # First failure: the server may simply be gone — revive it if
+ # this install owns it, then keep knocking. A server that is
+ # merely warming up needs the same patience either way.
+ launched = self._maybe_launch_server()
+ log.info(
+ "local-realtime: connect attempt %d failed (%s)%s — "
+ "retrying for up to another %.0f s",
+ attempt,
+ type(exc).__name__,
+ " after relaunching the server" if launched else "",
+ remaining,
+ )
+ await asyncio.sleep(
+ min(_LOCAL_CONNECT_RETRY_STEP_S, max(remaining, 0.0))
+ )
+
+ async def _open_session_once(self, cfg: Any) -> _OpenAIRealtimeSession:
from openai import AsyncOpenAI # lazy (AP-26)
client = AsyncOpenAI(
@@ -1400,5 +1567,12 @@ class LocalRealtimeProvider:
# placeholder the server would reject.
connect_model = await self._resolve_model()
return await _open_realtime_session(
- client, cfg, model=connect_model, transcription_model=None
+ client,
+ cfg,
+ model=connect_model,
+ transcription_model=None,
+ # A self-hosted process can crash and be revived seconds later —
+ # a dead transport is worth rebuilding in place (BUG-071 path)
+ # instead of ending the call.
+ rebuild_on_transport_death=True,
)
diff --git a/tests/unit/realtime/test_local_realtime.py b/tests/unit/realtime/test_local_realtime.py
index 85f9885f50..2c1ce963f1 100644
--- a/tests/unit/realtime/test_local_realtime.py
+++ b/tests/unit/realtime/test_local_realtime.py
@@ -235,3 +235,194 @@ async def test_a_server_without_a_model_list_still_connects(fake_models) -> None
fake_models.fail = True
provider = LocalRealtimeProvider(base_url="http://localhost:8080")
assert await provider._resolve_model()
+
+
+# ── A dead local server must not mean a dead call ────────────────────────
+#
+# Live 2026-08-06 19:57: the self-hosted server's process died silently
+# mid-turn and the whole call ended reason=error, although the server was
+# healthy again within the minute. Three properties fix that class of
+# failure: local sessions opt into the in-place transport rebuild, a failing
+# connect is retried long enough for a restarting server to warm up, and —
+# when a launch command is configured — Jarvis revives the server itself.
+
+
+class _FakeConnection:
+ def __aiter__(self):
+ return self
+
+
+def _session_kwargs(**overrides: Any) -> dict[str, Any]:
+ kwargs: dict[str, Any] = {
+ "connection": _FakeConnection(),
+ "connection_cm": SimpleNamespace(),
+ "client": SimpleNamespace(),
+ "session_id": "s-1",
+ }
+ kwargs.update(overrides)
+ return kwargs
+
+
+def test_transport_rebuild_is_opt_in_and_off_by_default() -> None:
+ """Hosted OpenAI-protocol cards keep their deliberate terminal semantics
+ (BUG-064): a session that was not asked to rebuild must not."""
+ from jarvis.plugins.realtime.openai_realtime import _OpenAIRealtimeSession
+
+ session = _OpenAIRealtimeSession(**_session_kwargs())
+ assert session.rebuild_on_transport_death is False
+ opted_in = _OpenAIRealtimeSession(
+ **_session_kwargs(rebuild_on_transport_death=True)
+ )
+ assert opted_in.rebuild_on_transport_death is True
+
+
+async def test_local_sessions_opt_into_transport_rebuild(monkeypatch) -> None:
+ """The self-hosted card asks for the in-place rebuild: its server can
+ crash and come back, and the call must survive that."""
+ from jarvis.plugins.realtime import openai_realtime as module
+
+ captured: dict[str, Any] = {}
+
+ async def fake_open(client: Any, cfg: Any, **kwargs: Any) -> str:
+ captured.update(kwargs)
+ return "session"
+
+ monkeypatch.setattr(module, "_open_realtime_session", fake_open)
+ provider = LocalRealtimeProvider(
+ base_url="http://localhost:8080", model="my-model"
+ )
+ assert await provider.open_session(SimpleNamespace(model="my-model")) == "session"
+ assert captured["rebuild_on_transport_death"] is True
+
+
+async def test_connect_retries_until_the_server_is_back(monkeypatch) -> None:
+ """A restarting local server needs seconds to warm up; the first refused
+ connects are its warm-up, not its verdict."""
+ from jarvis.plugins.realtime import openai_realtime as module
+
+ monkeypatch.setattr(module, "_LOCAL_CONNECT_RETRY_STEP_S", 0.0)
+ provider = LocalRealtimeProvider(
+ base_url="http://localhost:8080", model="m", launch_command="serve"
+ )
+ launches: list[bool] = []
+ monkeypatch.setattr(
+ provider, "_maybe_launch_server", lambda: launches.append(True) or False
+ )
+ attempts = 0
+
+ async def flaky(cfg: Any) -> str:
+ nonlocal attempts
+ attempts += 1
+ if attempts < 3:
+ raise ConnectionError("refused")
+ return "session"
+
+ monkeypatch.setattr(provider, "_open_session_once", flaky)
+ assert await provider.open_session(SimpleNamespace(model="m")) == "session"
+ assert attempts == 3
+ assert launches # the revive path was consulted while the server was down
+
+
+async def test_connect_gives_up_honestly_after_the_window(monkeypatch) -> None:
+ from jarvis.plugins.realtime import openai_realtime as module
+
+ monkeypatch.setattr(module, "_LOCAL_CONNECT_RETRY_STEP_S", 0.01)
+ provider = LocalRealtimeProvider(base_url="http://localhost:8080", model="m")
+ monkeypatch.setattr(provider, "_connect_retry_window_s", lambda: 0.03)
+
+ async def always_down(cfg: Any) -> str:
+ raise ConnectionError("refused")
+
+ monkeypatch.setattr(provider, "_open_session_once", always_down)
+ with pytest.raises(ConnectionError):
+ await provider.open_session(SimpleNamespace(model="m"))
+
+
+async def test_cancellation_is_never_retried(monkeypatch) -> None:
+ """The desktop's startup budget cancels a slow connect; holding the
+ cancellation hostage to a retry loop would freeze the call teardown."""
+ import asyncio
+
+ provider = LocalRealtimeProvider(
+ base_url="http://localhost:8080", model="m", launch_command="serve"
+ )
+ attempts = 0
+
+ async def cancelled(cfg: Any) -> str:
+ nonlocal attempts
+ attempts += 1
+ raise asyncio.CancelledError()
+
+ monkeypatch.setattr(provider, "_open_session_once", cancelled)
+ with pytest.raises(asyncio.CancelledError):
+ await provider.open_session(SimpleNamespace(model="m"))
+ assert attempts == 1
+
+
+def test_patience_is_earned_by_a_launch_command() -> None:
+ """Without a launch command nobody revives the server — a long silent
+ wait would hold the call hostage for nothing."""
+ from jarvis.plugins.realtime import openai_realtime as module
+
+ patient = LocalRealtimeProvider(
+ base_url="http://localhost:8080", launch_command="serve"
+ )
+ unattended = LocalRealtimeProvider(base_url="http://localhost:8080")
+ assert patient._connect_retry_window_s() == module._LOCAL_CONNECT_PATIENT_WINDOW_S
+ assert unattended._connect_retry_window_s() == module._LOCAL_CONNECT_SHORT_WINDOW_S
+
+
+def _fresh_launch_state(monkeypatch) -> list[dict[str, Any]]:
+ """Reset the class-level spawn stamp and capture Popen calls."""
+ import subprocess
+
+ monkeypatch.setattr(LocalRealtimeProvider, "_last_launch_at", float("-inf"))
+ spawned: list[dict[str, Any]] = []
+
+ def fake_popen(command: Any, **kwargs: Any) -> SimpleNamespace:
+ spawned.append({"command": command, **kwargs})
+ return SimpleNamespace(pid=4711)
+
+ monkeypatch.setattr(subprocess, "Popen", fake_popen)
+ return spawned
+
+
+def test_revive_spawns_windowless_and_rate_limited(monkeypatch, tmp_path) -> None:
+ from jarvis.core.process_utils import NO_WINDOW_CREATIONFLAGS
+
+ monkeypatch.setenv("JARVIS_DATA_DIR", str(tmp_path))
+ spawned = _fresh_launch_state(monkeypatch)
+ provider = LocalRealtimeProvider(
+ base_url="http://localhost:8765", launch_command="serve --flag"
+ )
+ assert provider._maybe_launch_server() is True
+ # Immediately again: rate-limited, a crash-looping server is not hammered.
+ assert provider._maybe_launch_server() is False
+ assert len(spawned) == 1
+ assert spawned[0]["creationflags"] == NO_WINDOW_CREATIONFLAGS # AP-1
+
+
+def test_revive_refuses_remote_servers(monkeypatch) -> None:
+ """A LAN endpoint going down must not start a second server HERE."""
+ spawned = _fresh_launch_state(monkeypatch)
+ provider = LocalRealtimeProvider(
+ base_url="http://gpu.lan:8443", launch_command="serve"
+ )
+ assert provider._maybe_launch_server() is False
+ assert spawned == []
+
+
+def test_no_launch_command_means_no_spawn(monkeypatch) -> None:
+ spawned = _fresh_launch_state(monkeypatch)
+ provider = LocalRealtimeProvider(base_url="http://localhost:8765")
+ assert provider._maybe_launch_server() is False
+ assert spawned == []
+
+
+def test_declared_handshake_budget_covers_the_patient_window() -> None:
+ """The shared 12 s handshake ceiling would behead the patient reconnect
+ mid-warm-up; the declared budget must clear the retry window."""
+ patient = LocalRealtimeProvider(
+ base_url="http://localhost:8765", launch_command="serve"
+ )
+ assert patient.handshake_budget_s > patient._connect_retry_window_s()