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(whisperbar): self-healing frame loop so one render error can't freeze the bar

Ruben Lütkea4ecef136135
diff --git a/jarvis/ui/whisperbar/overlay.py b/jarvis/ui/whisperbar/overlay.py
index e34517d38e..610d559348 100644
--- a/jarvis/ui/whisperbar/overlay.py
+++ b/jarvis/ui/whisperbar/overlay.py
@@ -373,35 +373,58 @@ class WhisperBarOverlay:
return
from PIL import ImageTk
- now = time.perf_counter()
- t = now - self._t0
- # Sound-driven look: bars while audio is present (mic OR TTS), wave while
- # silent. The coarse self._mode only decides active-vs-idle; the actual
- # wave↔bars choice comes from how recently real sound arrived. This makes
- # the silent TTS-synthesis lead-in render as the thinking wave and real
- # speech (in or out) render as the equalizer — independent of the
- # supervisor state's continue-listening flips.
- from jarvis.audio import level_tap
-
- playing = level_tap.playback_active()
- effective_mode = renderer.visual_mode(
- self._mode,
- now - self._last_audible_t,
- hold_s=AUDIBLE_HOLD_S,
- playback_active=playing,
- )
- # The level is fed live per ~60 ms TTS sub-block (player._write_samples),
- # so the equalizer reacts to Jarvis's actual loudness — thin and lively,
- # exactly like it reacts to your mic. No synthetic floor (that made the
- # bars look uniformly blocky).
- img = self._renderer.render(t, effective_mode, self._ext_level, hovered=self._hovered)
- # PhotoImage must be retained on self, else Tk GCs it before drawing.
- self._photo = ImageTk.PhotoImage(img)
- if self._image_id is None:
- self._image_id = self._canvas.create_image(0, 0, anchor="nw", image=self._photo)
- else:
- self._canvas.itemconfig(self._image_id, image=self._photo)
- self._root.after(16, self._schedule_frame) # ~60 FPS
+ # SELF-HEALING LOOP. This after-loop re-arms ONLY from its own tail, so a
+ # single transient render/Tk error (ImageTk.PhotoImage raising, a TclError
+ # during a window move, a one-off PIL glitch) used to skip the re-arm and
+ # the animation died PERMANENTLY — the Tk mainloop kept running, so the
+ # window stayed visible frozen on its last frame until an app restart
+ # ("JarvisBar stopped moving" forensic). The render body is now wrapped so
+ # one bad frame is dropped, logged, and the next tick is still armed in
+ # `finally`. The loop can no longer be killed by any one exception.
+ try:
+ now = time.perf_counter()
+ t = now - self._t0
+ # Sound-driven look: bars while audio is present (mic OR TTS), wave
+ # while silent. The coarse self._mode only decides active-vs-idle; the
+ # actual wave↔bars choice comes from how recently real sound arrived.
+ # This makes the silent TTS-synthesis lead-in render as the thinking
+ # wave and real speech (in or out) render as the equalizer —
+ # independent of the supervisor state's continue-listening flips.
+ from jarvis.audio import level_tap
+
+ playing = level_tap.playback_active()
+ effective_mode = renderer.visual_mode(
+ self._mode,
+ now - self._last_audible_t,
+ hold_s=AUDIBLE_HOLD_S,
+ playback_active=playing,
+ )
+ # The level is fed live per ~60 ms TTS sub-block (player._write_samples),
+ # so the equalizer reacts to Jarvis's actual loudness — thin and lively,
+ # exactly like it reacts to your mic. No synthetic floor (that made the
+ # bars look uniformly blocky).
+ img = self._renderer.render(
+ t, effective_mode, self._ext_level, hovered=self._hovered
+ )
+ # PhotoImage must be retained on self, else Tk GCs it before drawing.
+ self._photo = ImageTk.PhotoImage(img)
+ if self._image_id is None:
+ self._image_id = self._canvas.create_image(
+ 0, 0, anchor="nw", image=self._photo
+ )
+ else:
+ self._canvas.itemconfig(self._image_id, image=self._photo)
+ except Exception: # noqa: BLE001 — one bad frame must never freeze the bar
+ log.exception("WhisperBar frame render failed — dropping one frame")
+ finally:
+ # Re-arm unconditionally so the loop is self-healing. Guard the after()
+ # call itself: if the root was torn down mid-frame, swallow the
+ # TclError and stop re-arming (the window is gone — correct to stop).
+ if self._running and self._root is not None:
+ try:
+ self._root.after(16, self._schedule_frame) # ~60 FPS
+ except Exception: # noqa: BLE001
+ log.debug("WhisperBar frame re-arm skipped", exc_info=True)
# ------------------------------------------------------------------ #
# Drag (reposition) + click (start a voice session) #
diff --git a/tests/unit/ui/whisperbar/test_frame_loop_resilience.py b/tests/unit/ui/whisperbar/test_frame_loop_resilience.py
new file mode 100644
index 0000000000..cf064bcdcc
--- /dev/null
+++ b/tests/unit/ui/whisperbar/test_frame_loop_resilience.py
@@ -0,0 +1,79 @@
+"""The JarvisBar animation loop must be self-healing.
+
+Forensic: the bar "stopped moving / froze" until an app restart. Root cause —
+``_schedule_frame`` re-arms the next frame ONLY from its tail line, so a single
+transient render/Tk error (e.g. ``ImageTk.PhotoImage`` raising, a ``TclError``
+during a window move) skips the re-arm and the loop dies permanently while the
+Tk mainloop keeps running (window stays visible, frozen on its last frame).
+
+These tests pin the contract: one frame error drops just that frame and the
+loop keeps ticking. Headless — a fake root/canvas/renderer, no real Tk display.
+"""
+from __future__ import annotations
+
+from jarvis.ui.whisperbar.overlay import WhisperBarOverlay
+
+
+class _FakeRoot:
+ def __init__(self) -> None:
+ self.scheduled: list[tuple[int, object]] = []
+
+ def after(self, ms: int, fn: object) -> None:
+ self.scheduled.append((ms, fn))
+
+
+class _FakeCanvas:
+ def create_image(self, *a: object, **k: object) -> int:
+ return 1
+
+ def itemconfig(self, *a: object, **k: object) -> None:
+ pass
+
+
+class _BoomRenderer:
+ """A renderer whose render() always raises — simulates a transient
+ PIL/Tk hiccup mid-frame."""
+
+ def render(self, *a: object, **k: object): # noqa: ANN201
+ raise RuntimeError("transient render/Tk hiccup")
+
+
+def _bare_bar(renderer_obj: object) -> tuple[WhisperBarOverlay, _FakeRoot]:
+ bar = WhisperBarOverlay.__new__(WhisperBarOverlay)
+ bar._running = True
+ bar._mode = "think"
+ bar._ext_level = 0.0
+ bar._last_audible_t = 0.0
+ bar._t0 = 0.0
+ bar._hovered = False
+ bar._image_id = None
+ bar._photo = None
+ root = _FakeRoot()
+ bar._root = root
+ bar._canvas = _FakeCanvas()
+ bar._renderer = renderer_obj # type: ignore[assignment]
+ return bar, root
+
+
+def test_frame_loop_reschedules_after_render_error():
+ """A render exception must NOT kill the loop: the next frame is still armed
+ and the call itself does not propagate (the Tk mainloop must keep running)."""
+ bar, root = _bare_bar(_BoomRenderer())
+
+ bar._schedule_frame() # must not raise
+
+ assert root.scheduled, "frame loop died: next frame was not rescheduled"
+ ms, fn = root.scheduled[-1]
+ assert fn == bar._schedule_frame
+ assert ms == 16
+
+
+def test_frame_loop_stops_rearming_when_not_running():
+ """The self-heal must not fight a deliberate stop(): once _running is False
+ the loop returns early and does NOT re-arm."""
+ bar, root = _bare_bar(_BoomRenderer())
+ bar._running = False
+
+ bar._schedule_frame()
+
+ assert root.scheduled == []