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(voice): a bar that says LISTENING reconciles against the backend

rubenluetke10-beep6712a970d4bc
diff --git a/jarvis/ui/web/frontend/src/App.tsx b/jarvis/ui/web/frontend/src/App.tsx
index 7dbdca6ea0..9a74c9c212 100644
--- a/jarvis/ui/web/frontend/src/App.tsx
+++ b/jarvis/ui/web/frontend/src/App.tsx
@@ -4,6 +4,7 @@ import { isSectionId, useEventStore } from "@/store/events";
import { useWebSocket } from "@/hooks/useWebSocket";
import { useBrainStatus } from "@/hooks/useBrainStatus";
import { useVoiceStatus } from "@/hooks/useVoiceStatus";
+import { useVoiceStateResync } from "@/hooks/useVoiceStateResync";
import { useAssistantNameSeed } from "@/hooks/useAssistantNameSeed";
import { useCodingMode } from "@/hooks/useCodingMode";
import { useSectionUrlMemory } from "@/hooks/useSectionUrlMemory";
@@ -152,6 +153,7 @@ export default function App() {
useWebSocket();
useBrainStatus();
useVoiceStatus();
+ useVoiceStateResync();
useAssistantNameSeed();
useCodingMode();
/*
diff --git a/jarvis/ui/web/frontend/src/hooks/useVoiceStateResync.test.tsx b/jarvis/ui/web/frontend/src/hooks/useVoiceStateResync.test.tsx
new file mode 100644
index 0000000000..c03f56df6f
--- /dev/null
+++ b/jarvis/ui/web/frontend/src/hooks/useVoiceStateResync.test.tsx
@@ -0,0 +1,130 @@
+/**
+ * A bar that says LISTENING over a microphone nobody is holding is the bug
+ * this hook exists for: `SystemStateChanged` is one-shot, so a window that
+ * missed the closing transition keeps the last state forever. These tests pin
+ * the three properties that make the correction safe — it only moves the store
+ * to idle, an unreadable answer changes nothing, and a live event beats the
+ * snapshot that was already in flight.
+ */
+import { cleanup, render, waitFor } from "@testing-library/react";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+import { useVoiceStateResync } from "@/hooks/useVoiceStateResync";
+import { useEventStore } from "@/store/events";
+
+function Harness() {
+ useVoiceStateResync();
+ return null;
+}
+
+function answer(body: unknown, ok = true) {
+ return vi.fn(async () => ({ ok, json: async () => body })) as unknown as typeof fetch;
+}
+
+describe("useVoiceStateResync", () => {
+ beforeEach(() => {
+ useEventStore.setState({
+ connected: true,
+ voiceState: "listening",
+ transcription: "left over from the dead session",
+ transcriptionFinal: false,
+ });
+ });
+
+ afterEach(() => {
+ cleanup();
+ vi.restoreAllMocks();
+ vi.unstubAllGlobals();
+ useEventStore.setState({ voiceState: "idle", transcription: "", transcriptionFinal: true });
+ });
+
+ it("drops a stale listening state when the backend reports no session", async () => {
+ vi.stubGlobal("fetch", answer({ available: true, state: "idle", voice_state: "idle" }));
+
+ render(<Harness />);
+
+ await waitFor(() => expect(useEventStore.getState().voiceState).toBe("idle"));
+ // The frozen live transcript goes with it — same session boundary.
+ expect(useEventStore.getState().transcription).toBe("");
+ expect(fetch).toHaveBeenCalledWith("/api/voice/state", { cache: "no-store" });
+ });
+
+ it("leaves a running call alone", async () => {
+ vi.stubGlobal(
+ "fetch",
+ answer({ available: true, state: "active", voice_state: "listening" }),
+ );
+
+ render(<Harness />);
+
+ await waitFor(() => expect(fetch).toHaveBeenCalled());
+ expect(useEventStore.getState().voiceState).toBe("listening");
+ });
+
+ it("asks nothing at all while the store is already idle", async () => {
+ useEventStore.setState({ voiceState: "idle" });
+ vi.stubGlobal("fetch", answer({ available: true, state: "idle", voice_state: "idle" }));
+
+ render(<Harness />);
+
+ await waitFor(() => expect(useEventStore.getState().voiceState).toBe("idle"));
+ expect(fetch).not.toHaveBeenCalled();
+ });
+
+ it("changes nothing when the backend cannot be read", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn(async () => {
+ throw new Error("offline");
+ }) as unknown as typeof fetch,
+ );
+
+ render(<Harness />);
+
+ await waitFor(() => expect(fetch).toHaveBeenCalled());
+ expect(useEventStore.getState().voiceState).toBe("listening");
+ });
+
+ it("yields to a live event that arrived while the snapshot was in flight", async () => {
+ vi.stubGlobal(
+ "fetch",
+ vi.fn(async () => {
+ // The next call starts before this answer is read — exactly the race.
+ useEventStore.getState().setVoice("speaking");
+ return { ok: true, json: async () => ({ available: true, state: "idle", voice_state: "idle" }) };
+ }) as unknown as typeof fetch,
+ );
+
+ render(<Harness />);
+
+ await waitFor(() => expect(fetch).toHaveBeenCalled());
+ expect(useEventStore.getState().voiceState).toBe("speaking");
+ });
+
+ it("still corrects against a backend that predates the fine-grained field", async () => {
+ vi.stubGlobal("fetch", answer({ available: true, state: "idle" }));
+
+ render(<Harness />);
+
+ await waitFor(() => expect(useEventStore.getState().voiceState).toBe("idle"));
+ });
+
+ it("leaves a call alone when an older backend only says the pipeline is active", async () => {
+ vi.stubGlobal("fetch", answer({ available: true, state: "active" }));
+
+ render(<Harness />);
+
+ await waitFor(() => expect(fetch).toHaveBeenCalled());
+ expect(useEventStore.getState().voiceState).toBe("listening");
+ });
+
+ it("waits for the socket before correcting anything", async () => {
+ useEventStore.setState({ connected: false });
+ vi.stubGlobal("fetch", answer({ available: true, state: "idle", voice_state: "idle" }));
+
+ render(<Harness />);
+
+ await waitFor(() => expect(useEventStore.getState().voiceState).toBe("listening"));
+ expect(fetch).not.toHaveBeenCalled();
+ });
+});
diff --git a/jarvis/ui/web/frontend/src/hooks/useVoiceStateResync.ts b/jarvis/ui/web/frontend/src/hooks/useVoiceStateResync.ts
new file mode 100644
index 0000000000..532e4fae86
--- /dev/null
+++ b/jarvis/ui/web/frontend/src/hooks/useVoiceStateResync.ts
@@ -0,0 +1,62 @@
+import { useCallback, useEffect } from "react";
+
+import { fetchVoiceRuntimeState } from "@/lib/voiceApi";
+import { useDocumentVisible } from "@/hooks/useDocumentVisible";
+import { useEventStore } from "@/store/events";
+
+/**
+ * Stop the voice surfaces from claiming a conversation that ended.
+ *
+ * The store's `voiceState` is an echo of `SystemStateChanged`, and that event
+ * is one-shot: whatever the window heard last is what it keeps showing. Every
+ * way of missing the closing transition therefore froze the bar mid-call — a
+ * dropped socket, a backend restart (the fresh supervisor starts in IDLE and
+ * publishes nothing, because for IT nothing changed), a window that mounted
+ * after the fact. The result is a bar that says LISTENING with an End button
+ * over a microphone nobody is holding (field report 2026-08-25), and it does
+ * not heal on its own — the store has no other input.
+ *
+ * So the surfaces reconcile against the backend's own word (GET
+ * /api/voice/state) at each of the three moments the gap can have opened:
+ * the socket (re)connecting, the window coming back on screen, and a move to
+ * another section — the last one being when a stale bar is actually looked at.
+ *
+ * Three rules keep this a correction and never a second source of truth:
+ *
+ * * It only ever moves the store TO idle. Starting or advancing a call stays
+ * the events' job; a REST snapshot is always a little behind them.
+ * * An unreadable answer (offline, headless, an older backend without the
+ * field) changes nothing.
+ * * A state that changed while the request was in flight wins — a live event
+ * is newer than the snapshot that was already on its way.
+ */
+export function useVoiceStateResync(): void {
+ const connected = useEventStore((s) => s.connected);
+ const activeSection = useEventStore((s) => s.activeSection);
+ const visible = useDocumentVisible();
+
+ const reconcile = useCallback(async () => {
+ const store = useEventStore.getState();
+ const before = store.voiceState;
+ // Idle is the state this hook corrects TO — nothing to check, and the
+ // common case, so the section switch costs no request at all.
+ if (before === "idle") return;
+
+ const truth = await fetchVoiceRuntimeState();
+ if (truth === null) return;
+ if (truth.available && truth.voiceState !== "idle") return;
+
+ const now = useEventStore.getState();
+ if (now.voiceState !== before) return; // a live event overtook the snapshot
+ now.setVoice("idle");
+ // The live-transcript box follows the same session boundary the websocket
+ // clears it on; left alone, the last utterance of the dead session sits
+ // there looking like someone is still speaking.
+ now.setTranscription("", true);
+ }, []);
+
+ useEffect(() => {
+ if (!connected || !visible) return;
+ void reconcile();
+ }, [activeSection, connected, reconcile, visible]);
+}
diff --git a/jarvis/ui/web/frontend/src/lib/voiceApi.ts b/jarvis/ui/web/frontend/src/lib/voiceApi.ts
index 2766494172..fb4840b4db 100644
--- a/jarvis/ui/web/frontend/src/lib/voiceApi.ts
+++ b/jarvis/ui/web/frontend/src/lib/voiceApi.ts
@@ -96,3 +96,51 @@ export async function setTtsVolume(
throw new Error(detail || `Voice volume change failed (${res.status}).`);
}
}
+
+export interface VoiceRuntimeState {
+ /** Is a speech pipeline running on this host at all? */
+ available: boolean;
+ /** The fine-grained state every voice surface renders, straight from the
+ * supervisor. `"idle"` whenever no session is running. */
+ voiceState: string;
+}
+
+/**
+ * The backend's own word on what voice is doing RIGHT NOW.
+ *
+ * `SystemStateChanged` is a one-shot event, so a window that missed one — a
+ * dropped socket, a backend restart, a fresh mount — keeps whatever it heard
+ * last with nothing to correct it. This is the REST mirror the surfaces
+ * reconcile against (hooks/useVoiceStateResync).
+ *
+ * `null` means "could not be asked" and is NOT "idle": an unknown answer must
+ * never overwrite a live state the events are keeping correct.
+ */
+export async function fetchVoiceRuntimeState(): Promise<VoiceRuntimeState | null> {
+ try {
+ const res = await fetch("/api/voice/state", { cache: "no-store" });
+ if (!res.ok) return null;
+ const body = (await res.json()) as {
+ available?: unknown;
+ state?: unknown;
+ voice_state?: unknown;
+ };
+ const available = body.available === true;
+ if (typeof body.voice_state === "string") {
+ return { available, voiceState: body.voice_state };
+ }
+ // A backend from before the field existed still answers the coarse
+ // question honestly: an idle pipeline IS "no session runs", which is the
+ // whole correction this is read for. Anything else stays "unknown" so a
+ // live call is never touched on a guess.
+ if (typeof body.state === "string") {
+ return { available, voiceState: body.state === "idle" ? "idle" : "unknown" };
+ }
+ return null;
+ } catch (error) {
+ // Offline / headless / the route missing on an older backend: the caller
+ // treats null as "no answer" and leaves the store exactly as it is.
+ console.info("Voice runtime state could not be read.", error);
+ return null;
+ }
+}
diff --git a/jarvis/ui/web/voice_call_routes.py b/jarvis/ui/web/voice_call_routes.py
index 48b2aac79c..37522bab26 100644
--- a/jarvis/ui/web/voice_call_routes.py
+++ b/jarvis/ui/web/voice_call_routes.py
@@ -17,6 +17,7 @@ the orb's actions scriptable as ``jarvis api voice <op>``.
No Brain dependency; on a headless install (no microphone, voice disabled) the
state endpoint answers honestly and the actions 503 instead of 500-ing.
"""
+
from __future__ import annotations
import logging
@@ -41,12 +42,34 @@ def _pipeline() -> Any:
@router.get("/state")
async def voice_state() -> dict[str, Any]:
- """Whether voice is available here, and what the pipeline is doing now."""
+ """Whether voice is available here, and what the pipeline is doing now.
+
+ ``voice_state`` is the fine-grained state every voice surface renders
+ (idle / connecting / listening / thinking / speaking / paused / error) —
+ the same value ``SystemStateChanged`` carries, read straight from the
+ supervisor. It exists because that event is a one-shot signal: a client
+ that mounts, reconnects or survives a backend restart after the last
+ transition never sees it and keeps whatever it heard last. A window that
+ was showing LISTENING when the socket dropped went on claiming the user
+ was being heard for the rest of its life (field report 2026-08-25). This
+ is the REST mirror those surfaces reconcile against.
+
+ Clamped to ``idle`` whenever the pipeline is not ACTIVE: a fine-grained
+ state without a running session is unearned detail, never the truth. A
+ running session whose supervisor cannot be read answers ``unknown`` rather
+ than a guess — the caller leaves a live call alone on anything but ``idle``.
+ """
pipeline = _pipeline()
if pipeline is None:
- return {"available": False, "state": "unavailable"}
- state = getattr(getattr(pipeline, "_state", None), "name", "unknown")
- return {"available": True, "state": str(state).lower()}
+ return {"available": False, "state": "unavailable", "voice_state": "idle"}
+ state = str(getattr(getattr(pipeline, "_state", None), "name", "unknown")).lower()
+ supervisor = getattr(pipeline, "_supervisor", None)
+ supervisor_state = str(getattr(supervisor, "state", "") or "").lower() or "unknown"
+ return {
+ "available": True,
+ "state": state,
+ "voice_state": supervisor_state if state == "active" else "idle",
+ }
@router.post("/call")
diff --git a/tests/unit/ui/test_voice_call_routes.py b/tests/unit/ui/test_voice_call_routes.py
index b71c4a0c42..318cb99ce3 100644
--- a/tests/unit/ui/test_voice_call_routes.py
+++ b/tests/unit/ui/test_voice_call_routes.py
@@ -19,11 +19,19 @@ from jarvis.ui.web.voice_call_routes import router
class _Pipeline:
"""The two public arming methods, and the state the GET reads."""
- def __init__(self, armed: bool = True, stopped: bool = True) -> None:
+ def __init__(
+ self,
+ armed: bool = True,
+ stopped: bool = True,
+ *,
+ state: str = "IDLE",
+ supervisor_state: str = "IDLE",
+ ) -> None:
self._armed = armed
self._stopped = stopped
self.calls: list[str] = []
- self._state = SimpleNamespace(name="IDLE")
+ self._state = SimpleNamespace(name=state)
+ self._supervisor = SimpleNamespace(state=supervisor_state)
def request_voice_session(self) -> bool:
self.calls.append("call")
@@ -73,7 +81,11 @@ def test_headless_answers_honestly_instead_of_500(monkeypatch):
assert client.post("/api/voice/hangup").status_code == 503
answer = client.get("/api/voice/state")
assert answer.status_code == 200
- assert answer.json() == {"available": False, "state": "unavailable"}
+ assert answer.json() == {
+ "available": False,
+ "state": "unavailable",
+ "voice_state": "idle",
+ }
def test_state_names_the_pipeline_state(monkeypatch):
@@ -81,4 +93,42 @@ def test_state_names_the_pipeline_state(monkeypatch):
assert client.get("/api/voice/state").json() == {
"available": True,
"state": "idle",
+ "voice_state": "idle",
+ }
+
+
+def test_state_mirrors_the_supervisor_while_a_session_runs(monkeypatch):
+ # The fine-grained state every voice surface renders: the REST mirror of
+ # the one-shot SystemStateChanged event, so a window that missed one can
+ # reconcile instead of showing the last thing it heard forever.
+ client = _client(
+ monkeypatch,
+ _Pipeline(state="ACTIVE", supervisor_state="LISTENING"),
+ )
+ assert client.get("/api/voice/state").json() == {
+ "available": True,
+ "state": "active",
+ "voice_state": "listening",
+ }
+
+
+def test_state_says_unknown_when_a_running_session_has_no_supervisor(monkeypatch):
+ # Never a guess: the client leaves a live call alone on anything but idle.
+ pipeline = _Pipeline(state="ACTIVE")
+ del pipeline._supervisor
+ client = _client(monkeypatch, pipeline)
+ assert client.get("/api/voice/state").json()["voice_state"] == "unknown"
+
+
+def test_state_clamps_a_left_over_supervisor_state_to_idle(monkeypatch):
+ # No session runs, so LISTENING is unearned detail whatever the supervisor
+ # still holds — and it is precisely the value that used to freeze the bar.
+ client = _client(
+ monkeypatch,
+ _Pipeline(state="IDLE", supervisor_state="LISTENING"),
+ )
+ assert client.get("/api/voice/state").json() == {
+ "available": True,
+ "state": "idle",
+ "voice_state": "idle",
}