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.
Python agent-orchestrationai-agentai-coding-assistantclaude-codecoding-agentcomputer-usedesktop-applocal-firstmcppersonal-jarvisvoice-assistantvoice-coding
fix(ui): JarvisBar subprocess host respawns after a crash instead of staying hidden
Ruben Lütkedb71e54d74f4
diff --git a/jarvis/ui/jarvisbar/subprocess_overlay.py b/jarvis/ui/jarvisbar/subprocess_overlay.pyindex 9fc1e466ce..03dd31d2cf 100644--- a/jarvis/ui/jarvisbar/subprocess_overlay.py+++ b/jarvis/ui/jarvisbar/subprocess_overlay.py@@ -8,12 +8,19 @@ stdin. Child events (mute toggle, feedback, show-window) stream back onstdout and are dispatched to the callbacks the bridge registered, so thebridge wiring is reused unchanged.-Failure contract: if the host cannot spawn or dies, every method degrades to-a one-log no-op (``NullOverlay`` behavior) — the bar is cosmetic and must-never take the app down with it. No auto-restart in this first cut; a dead-host stays down until the next overlay swap / app restart. Deliberately-defines no ``_root`` attribute so the bridge's reset path early-returns-(same contract as ``NullOverlay``).+Failure contract: while the host is down, every method degrades to a one-log+no-op (``NullOverlay`` behavior) — the bar is cosmetic and must never take+the app down with it. Revised 2026-07-18: a live Mac test hit a host death+mid-session and the bar stayed hidden for the rest of it, so a dead host now+gets a BOUNDED auto-respawn — up to ``_RESPAWN_MAX_ATTEMPTS`` attempts per+``SubprocessBarOverlay`` instance lifetime, each preceded by a+``_RESPAWN_BACKOFF_SECONDS`` non-blocking backoff (scheduled on its own+daemon thread, never the caller's) — after which the last known+visibility/mode/mute/level state is re-applied so the bar comes back looking+right instead of blank. Once every attempt is spent the bar reverts to the+original contract: it stays hidden until the next overlay swap or app+restart. Deliberately defines no ``_root`` attribute so the bridge's reset+path early-returns (same contract as ``NullOverlay``)."""from __future__ import annotations@@ -22,6 +29,7 @@ import loggingimport subprocessimport sysimport threading+import timefrom collections.abc import Callablefrom typing import IO, Any@@ -40,6 +48,14 @@ class SubprocessBarOverlay:# Overridable per surface so a host's pump threads are identifiable._EVENTS_THREAD_NAME = "jarvisbar-host-events"_STDERR_THREAD_NAME = "jarvisbar-host-stderr"+ _RESPAWN_THREAD_NAME = "jarvisbar-host-respawn"++ # Bounded auto-respawn (revised 2026-07-18, see module docstring): total+ # attempts per instance lifetime and the non-blocking backoff between+ # them. A death that follows a respawn within seconds still consumes an+ # attempt — the bound itself is what keeps a crash loop from spinning.+ _RESPAWN_MAX_ATTEMPTS = 3+ _RESPAWN_BACKOFF_SECONDS = 5.0def __init__(self,@@ -54,11 +70,17 @@ class SubprocessBarOverlay:self._startup_gated = bool(startup_gated)self._mode = "idle"self._muted = False+ self._visible = False+ self._last_level: float | None = Noneself._proc: subprocess.Popen[str] | None = Noneself._send_lock = threading.Lock()self._ready = threading.Event()self._stopping = Falseself._dead_logged = False+ self._respawn_lock = threading.Lock()+ self._respawn_attempts = 0+ self._respawn_succeeded = threading.Event()+ self._respawn_exhausted = threading.Event()self._on_mute_toggle: Callable[[], None] | None = Noneself._feedback_publisher: Callable[[str, dict], None] | None = Noneself._on_show_window: Callable[[], None] | None = None@@ -70,10 +92,28 @@ class SubprocessBarOverlay:"""Spawn the bar host process (name kept for the surface contract)."""if self._proc is not None and self._proc.poll() is None:return+ self._spawn_process(timeout)++ def _spawn_process(self, timeout: float) -> bool:+ """Popen the host, wire its pump threads, and wait for its ready line.++ Shared by the initial ``start_in_thread`` call and every bounded+ respawn attempt. Returns whether the Popen call itself succeeded —+ a ready-wait timeout is logged but still counts as a live process+ (matches the pre-respawn behavior of ``start_in_thread``).+ """+ self._ready.clear()+ # Reset the death debounce BEFORE the new process (and its pump+ # thread) exist, not after: resetting it only once this method+ # returns would race the new pump thread's own EOF detection —+ # a host that dies again in the instant after spawning could find+ # ``_dead_logged`` still True from the PREVIOUS death and silently+ # swallow its own.+ self._dead_logged = Falsetry:from jarvis.core.process_utils import NO_WINDOW_CREATIONFLAGS- self._proc = subprocess.Popen( # noqa: S603 — fixed argv, own venv+ proc = subprocess.Popen( # noqa: S603 — fixed argv, own venv[sys.executable, "-m", _HOST_MODULE],stdin=subprocess.PIPE,stdout=subprocess.PIPE,@@ -86,31 +126,43 @@ class SubprocessBarOverlay:except Exception: # noqa: BLE001 — cosmetic surface; degrade, never raiselog.exception("JarvisBar host spawn failed — bar runs as a no-op")self._proc = None- return+ return False+ self._proc = procself._write_line(self._init_payload())+ # Use the LOCAL ``proc`` reference for both threads, not ``self._proc``+ # again — a fast enough death can already have a respawn attempt+ # reassigning ``self._proc`` (or ``stop()`` clearing it) by the time+ # the second thread starts, and these two threads belong to THIS+ # specific process regardless of what ``self._proc`` points to next.threading.Thread(target=self._pump_events,- args=(self._proc.stdout,),+ args=(proc.stdout,),name=self._EVENTS_THREAD_NAME,daemon=True,).start()threading.Thread(target=self._pump_stderr,- args=(self._proc.stderr,),+ args=(proc.stderr,),name=self._STDERR_THREAD_NAME,daemon=True,).start()if not self._ready.wait(timeout=timeout):log.error("JarvisBar host not ready within %.1fs", timeout)+ return Truedef stop(self) -> None:+ # Flip this BEFORE the no-proc early return: a spawn failure or a+ # host death may already have a bounded respawn attempt sleeping on+ # its own thread, and that thread's only guard against firing later+ # is this flag — it must be set even when there is no live process+ # to tear down right now.+ self._stopping = Trueproc = self._procif proc is None:return- self._stopping = Truetry:self._send({"op": "stop"})if proc.stdin is not None:@@ -145,9 +197,11 @@ class SubprocessBarOverlay:if mode not in _MODES:returnself._mode = mode+ self._visible = Trueself._send({"op": "show", "mode": mode})def hide(self) -> None:+ self._visible = Falseself._send({"op": "hide"})def reassert_z_order(self) -> None:@@ -161,7 +215,8 @@ class SubprocessBarOverlay:return releaseddef set_level(self, level: float) -> None:- self._send({"op": "set_level", "level": float(level)})+ self._last_level = float(level)+ self._send({"op": "set_level", "level": self._last_level})def set_muted(self, muted: bool) -> None:self._muted = bool(muted)@@ -226,13 +281,109 @@ class SubprocessBarOverlay:self._log_dead_once()def _log_dead_once(self) -> None:- if self._stopping or self._dead_logged:+ """Single choke point for every "the host is gone" detection site.++ ``_dead_logged`` debounces the three call sites (``_send``,+ ``_write_line``, the ``_pump_events`` EOF) for one death; it is reset+ once a respawn succeeds so the NEXT death is detected fresh. Also the+ entry point for the bounded auto-respawn: schedules the next attempt+ while attempts remain, otherwise logs the same honest give-up message+ the surface always used to log unconditionally. The check-then-act+ sequence (debounce, attempt bound, counter increment, thread start)+ runs under ``_respawn_lock`` — the events pump thread and a caller+ thread doing ``show()``/``set_level()``/etc. can both observe the+ same death at once, and without the lock a lost update could double+ -schedule the same attempt number or race ``_spawn_process`` itself.+ """+ with self._respawn_lock:+ if self._stopping or self._dead_logged:+ return+ self._dead_logged = True+ proc = self._proc+ exit_code = proc.poll() if proc is not None else None++ if self._respawn_attempts >= self._RESPAWN_MAX_ATTEMPTS:+ # Log BEFORE setting the event: a waiter unblocked by the+ # event must never observe this method as "not done yet".+ log.warning(+ "JarvisBar host process is gone (exit code %s) and all "+ "%d/%d respawn attempts are spent — the bar stays hidden "+ "until the next overlay swap or app restart.",+ exit_code,+ self._respawn_attempts,+ self._RESPAWN_MAX_ATTEMPTS,+ )+ self._respawn_exhausted.set()+ return++ self._respawn_attempts += 1+ attempt = self._respawn_attempts+ log.warning(+ "JarvisBar host process is gone (exit code %s) — scheduling "+ "respawn attempt %d/%d in %.0fs.",+ exit_code,+ attempt,+ self._RESPAWN_MAX_ATTEMPTS,+ self._RESPAWN_BACKOFF_SECONDS,+ )+ threading.Thread(+ target=self._respawn_after_backoff,+ args=(attempt,),+ name=f"{self._RESPAWN_THREAD_NAME}-{attempt}",+ daemon=True,+ ).start()++ def _respawn_after_backoff(self, attempt: int) -> None:+ """Wait out the backoff, then respawn — runs on its own daemon thread.++ Never touches the caller's thread or the app's event loop: the sleep+ and the Popen call both happen here. A death within the backoff+ window (``stop()`` called while waiting) aborts the attempt instead+ of spawning a host nobody wants anymore.+ """+ time.sleep(self._RESPAWN_BACKOFF_SECONDS)+ if self._stopping:return- self._dead_logged = True++ if not self._spawn_process(timeout=3.0):+ # The Popen call itself failed; treat it as another death so the+ # same bounded logic decides whether to try again or give up.+ # ``_spawn_process`` already reset ``_dead_logged`` up front.+ self._log_dead_once()+ return++ if self._stopping:+ # stop() raced with this respawn — tear the fresh host back down+ # instead of leaving an orphaned process behind.+ self.stop()+ return+log.warning(- "JarvisBar host process is gone — the bar stays hidden until the "- "next overlay swap or app restart."+ "JarvisBar host respawned successfully (attempt %d/%d) — "+ "re-applying the last known bar state.",+ attempt,+ self._RESPAWN_MAX_ATTEMPTS,)+ self._respawn_succeeded.set()+ self._reapply_desired_state()++ def _reapply_desired_state(self) -> None:+ """Restore visibility/mode/mute/level onto a freshly respawned host.++ ``_init_payload()`` (re-sent inside ``_spawn_process``) already+ carries persistent/accent/opacity/startup_gated straight from the+ current instance attributes, so only the state this class mirrors+ OUTSIDE the init line — shown/hidden, mode, mute, last level — needs+ a dedicated re-send here.+ """+ if self._visible:+ self._send({"op": "show", "mode": self._mode})+ else:+ self._send({"op": "hide"})+ if self._muted:+ self._send({"op": "set_muted", "muted": True})+ if self._last_level is not None:+ self._send({"op": "set_level", "level": self._last_level})def _pump_events(self, stream: IO[str] | None) -> None:if stream is None:@@ -298,6 +449,7 @@ class SubprocessMascotOverlay(SubprocessBarOverlay):_EVENTS_THREAD_NAME = "orb-host-events"_STDERR_THREAD_NAME = "orb-host-stderr"+ _RESPAWN_THREAD_NAME = "orb-host-respawn"def __init__(self, mascot_path: str | None = None) -> None:super().__init__()diff --git a/tests/unit/ui/jarvisbar/test_subprocess_overlay.py b/tests/unit/ui/jarvisbar/test_subprocess_overlay.pyindex 8d041bf586..50605ef181 100644--- a/tests/unit/ui/jarvisbar/test_subprocess_overlay.py+++ b/tests/unit/ui/jarvisbar/test_subprocess_overlay.py@@ -7,10 +7,13 @@ process. The real cross-process path is covered in test_host_protocol.py."""from __future__ import annotations+import gcimport ioimport jsonimport threading+import pytest+from jarvis.ui.jarvisbar import subprocess_overlay as modfrom jarvis.ui.jarvisbar.subprocess_overlay import (SubprocessBarOverlay,@@ -18,6 +21,25 @@ from jarvis.ui.jarvisbar.subprocess_overlay import ()[email protected](autouse=True)+def _neutralize_pending_bar_respawns():+ """Guard against a fake-host artifact leaking a real subprocess spawn.++ Every fake host's scripted stdout is a finite ``io.StringIO`` that EOFs+ right after its scripted lines — read by the real event pump as "the+ host is gone" and, since BUG respawn support, bounded-scheduled onto its+ own background thread. That is harmless while THIS test's ``Popen`` fake+ is still monkeypatched in, but the thread's backoff can outlive the test:+ once monkeypatch reverts, a still-sleeping thread would call the REAL+ ``subprocess.Popen``. Mark every proxy created during the test as+ stopping once it ends so no pending respawn thread ever fires for real.+ """+ yield+ for obj in gc.get_objects():+ if isinstance(obj, SubprocessBarOverlay):+ obj._stopping = True++class _FakeStdin:def __init__(self) -> None:self.lines: list[str] = []@@ -186,6 +208,172 @@ def test_stop_sends_stop_closes_stdin_and_waits(monkeypatch) -> None:assert surface._proc is None+# --------------------------------------------------------------------- #+# Bounded auto-respawn (2026-07-18 revision — see module docstring) #+# --------------------------------------------------------------------- #+class _SteadyStdout:+ """Stdout that blocks after "ready" instead of EOF-ing right away.++ The base ``_FakePopen.stdout`` is a finite ``io.StringIO`` that ends the+ instant its scripted lines are consumed — a fine stand-in for "the wire+ protocol works" tests, but it also means EVERY fake host "dies" (EOF)+ within a moment of spawning, which is exactly what the crash-loop tests+ below want. The respawn tests that need exactly ONE controlled death use+ this steady stream instead, so they are not racing the base fake's own+ built-in quick death.+ """++ def __init__(self, ready_line: str = '{"event": "ready"}\n') -> None:+ self._sent_ready = False+ self._ready_line = ready_line+ self._closed = threading.Event()++ def __iter__(self) -> _SteadyStdout:+ return self++ def __next__(self) -> str:+ if not self._sent_ready:+ self._sent_ready = True+ return self._ready_line+ self._closed.wait() # block like a live process's open stdout pipe+ raise StopIteration++ def close(self) -> None:+ self._closed.set()+++class _SteadyPopen(_FakePopen):+ """``_FakePopen`` whose stdout stays open until explicitly torn down."""++ def __init__(self, *args, **kwargs) -> None:+ super().__init__(*args, **kwargs)+ self.stdout = _SteadyStdout()++ def kill(self) -> None:+ super().kill()+ self.stdout.close()++ def wait(self, timeout: float | None = None) -> int:+ self.stdout.close()+ return super().wait(timeout=timeout)+++def test_host_death_triggers_respawn_and_reapplies_desired_state(+ monkeypatch,+) -> None:+ monkeypatch.setattr(mod.time, "sleep", lambda _seconds: None)+ monkeypatch.setattr(mod.subprocess, "Popen", _SteadyPopen)+ surface = SubprocessBarOverlay()+ surface.start_in_thread(timeout=2.0)+ proc = _FakePopen.last+ assert proc is not None++ surface.show("speak")+ surface.set_muted(True)+ surface.set_level(0.6)+ proc._returncode = 1 # the host died+ surface.set_level(0.9) # a call against the dead proc triggers detection++ assert surface._respawn_succeeded.wait(timeout=2.0)+ new_proc = _FakePopen.last+ assert new_proc is not proc # a fresh host process was spawned++ sent = new_proc.sent()+ assert sent[0] == {+ "op": "init",+ "persistent": True,+ "accent": "#e7c46e",+ "startup_gated": False,+ }+ # Last known state (shown in "speak", muted, last level 0.9) re-applied.+ assert [m["op"] for m in sent[1:]] == ["show", "set_muted", "set_level"]+ assert sent[1] == {"op": "show", "mode": "speak"}+ assert sent[2] == {"op": "set_muted", "muted": True}+ assert sent[3] == {"op": "set_level", "level": 0.9}+ assert surface._respawn_attempts == 1+ surface.stop()+++class _QuicklyDyingStdout:+ """Emits "ready", then EOFs after a brief REAL delay.++ ``mod.time.sleep`` gets monkeypatched to instant in these tests (so the+ 5s production backoff doesn't slow them down) — but ``mod.time`` IS the+ same module object this test file imports, so a plain ``time.sleep(...)``+ in this fake would ALSO be neutered by that same monkeypatch.+ ``threading.Event().wait(timeout=...)`` uses its own timing and is+ unaffected, giving the spawning thread genuine wall-clock room to finish+ its own post-spawn bookkeeping (ready-wait, success log, reapply) before+ this stdout's EOF fires the next chained attempt — without it, multiple+ attempts race each other and can observe a torn ``self._proc``.+ """++ def __init__(self, delay: float = 0.05) -> None:+ self._lines = iter(('{"event": "ready"}\n',))+ self._delay = delay+ self._settled = False++ def __iter__(self) -> _QuicklyDyingStdout:+ return self++ def __next__(self) -> str:+ try:+ return next(self._lines)+ except StopIteration:+ if not self._settled:+ self._settled = True+ threading.Event().wait(timeout=self._delay)+ raise+++class _CrashLoopPopen(_FakePopen):+ """``_FakePopen`` that dies again a beat after "ready", not instantly."""++ def __init__(self, *args, **kwargs) -> None:+ super().__init__(*args, **kwargs)+ self.stdout = _QuicklyDyingStdout()+++def test_respawn_is_bounded_to_three_attempts_then_gives_up(+ monkeypatch, caplog+) -> None:+ """Every attempt uses the crash-prone fake, so each respawned host dies+ again (EOF) a beat later — chaining through all 3 attempts and proving+ the surface gives up instead of looping forever."""+ monkeypatch.setattr(mod.time, "sleep", lambda _seconds: None)+ monkeypatch.setattr(mod.subprocess, "Popen", _CrashLoopPopen)+ surface = SubprocessBarOverlay()+ with caplog.at_level("WARNING", logger="jarvis.ui.jarvisbar"):+ surface.start_in_thread(timeout=2.0)+ assert surface._respawn_exhausted.wait(timeout=2.0)++ assert surface._respawn_attempts == 3+ assert "respawn attempts are spent" in caplog.text+ surface.stop()+++def test_quick_respawn_death_still_consumes_an_attempt(monkeypatch) -> None:+ """A respawned host that dies again almost immediately must still count+ against the bound — otherwise a crash loop would spawn forever."""+ monkeypatch.setattr(mod.time, "sleep", lambda _seconds: None)+ spawned: list[_FakePopen] = []++ class _CountingPopen(_CrashLoopPopen):+ def __init__(self, *args, **kwargs) -> None:+ super().__init__(*args, **kwargs)+ spawned.append(self)++ monkeypatch.setattr(mod.subprocess, "Popen", _CountingPopen)+ surface = SubprocessBarOverlay()+ surface.start_in_thread(timeout=2.0)+ assert surface._respawn_exhausted.wait(timeout=2.0)++ # Initial spawn + exactly 3 bounded respawns, each dying within its own+ # scripted EOF before the bound gave up — no unbounded crash loop.+ assert len(spawned) == 4+ surface.stop()++def test_surface_contract_exposes_every_bridge_method() -> None:from tests.unit.ui.jarvisbar.test_surface_contract import REQUIRED@@ -259,8 +447,13 @@ def test_mascot_pump_threads_use_orb_host_names(monkeypatch) -> None:super().__init__(*args, **kwargs)monkeypatch.setattr(mod.threading, "Thread", _NamedThread)- _started_mascot(monkeypatch)+ # _SteadyPopen (not the base fake) so the host doesn't EOF and schedule a+ # respawn thread — this test only cares about the two spawn-time threads.+ monkeypatch.setattr(mod.subprocess, "Popen", _SteadyPopen)+ surface = SubprocessMascotOverlay()+ surface.start_in_thread(timeout=2.0)assert names == ["orb-host-events", "orb-host-stderr"]+ surface.stop()def test_mascot_dead_host_degrades_to_noop_without_raising(monkeypatch) -> None: