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): restore selected archive context in realtime sessions

rubenluetke10-beep83d5fe5c38df
diff --git a/jarvis/brain/manager.py b/jarvis/brain/manager.py
index 4d66356e49..2048582556 100644
--- a/jarvis/brain/manager.py
+++ b/jarvis/brain/manager.py
@@ -12911,6 +12911,7 @@ class BrainManager:
def clear_history(self) -> None:
self._history = []
+ self.__dict__.pop("_voice_history_seed", None)
def drop_last_turn(self, expected_user_text: str) -> bool:
"""Remove the most recent (user, assistant) pair when its user message
@@ -12988,6 +12989,18 @@ class BrainManager:
else BrainMessage(role=role, content=content)
)
self._history = seeded[-self._HISTORY_MAX :]
+ # Explicit archive selection also seeds the next duplex call. Ordinary
+ # generated turns never fill this slot: unrelated calls must stay fresh.
+ self._voice_history_seed = tuple(self._history)
+
+ def take_voice_history_seed(self) -> tuple[BrainMessage, ...]:
+ """Consume an explicit resume once, retaining the text brain's history.
+
+ A single dict pop transfers ownership even when desktop session setup
+ runs on a worker thread. Reconnects reuse the receiving call's copy.
+ """
+ history: tuple[BrainMessage, ...] = self.__dict__.pop("_voice_history_seed", ())
+ return history
# ------------------------------------------------------------------
# Live reload for the CLI tool registry (CLI integration, task 2)
diff --git a/jarvis/realtime/session.py b/jarvis/realtime/session.py
index 315badca12..a80b733a45 100644
--- a/jarvis/realtime/session.py
+++ b/jarvis/realtime/session.py
@@ -2663,6 +2663,7 @@ class RealtimeVoiceSession:
log.debug("realtime: ActionProposed subscription failed", exc_info=True)
self._delegate_turns: dict[str, _DelegateTurnState] = {}
self._delegate_history: list[BrainMessage] = []
+ self._resume_history_loaded = False
self._announcement_context_signatures: list[tuple[str, str, str]] = []
self._delegate_required_for_turn = False
self._delegate_reply_awaits_answer = False
@@ -3585,6 +3586,17 @@ class RealtimeVoiceSession:
if offer_sdp:
self._transport_offer_sdp = offer_sdp
if self._session is None:
+ if not self._resume_history_loaded:
+ take_seed = getattr(self._brain, "take_voice_history_seed", None)
+ if callable(take_seed):
+ # Seed BEFORE the provider handshake, not just the text
+ # brain. The same history grounds later tool delegation
+ # and transport reconnects on every voice surface.
+ self._delegate_history = [
+ message for message in take_seed()
+ if message.role in {"user", "assistant"}
+ ]
+ self._resume_history_loaded = True
# A cold subscription transport legitimately spends tens of
# seconds here (app-server spawn, account verification, WebRTC
# negotiation). Announcing the attempt BEFORE the wait is the
diff --git a/jarvis/ui/desktop_app.py b/jarvis/ui/desktop_app.py
index e147e7d77c..b61cb3c784 100644
--- a/jarvis/ui/desktop_app.py
+++ b/jarvis/ui/desktop_app.py
@@ -1974,6 +1974,17 @@ class DesktopApp:
brain = brain_holder["brain"]
return str(getattr(brain, "conversation_language", "")) if brain else ""
+ def seed_history(self, turns: Any) -> None:
+ brain = brain_holder["brain"]
+ if brain is None:
+ raise RuntimeError("Brain is still starting; cannot resume conversation")
+ brain.seed_history(turns)
+
+ def take_voice_history_seed(self) -> Any:
+ brain = brain_holder["brain"]
+ take = getattr(brain, "take_voice_history_seed", None)
+ return take() if callable(take) else ()
+
# Realtime direct mode builds its tool bridge from these two
# attributes at session construction; delegate through to the
# real brain so a session opened after boot sees the full tool
diff --git a/jarvis/ui/web/frontend/src/components/agentic/useVoiceCall.ts b/jarvis/ui/web/frontend/src/components/agentic/useVoiceCall.ts
index 11f7c46259..4cabfd878e 100644
--- a/jarvis/ui/web/frontend/src/components/agentic/useVoiceCall.ts
+++ b/jarvis/ui/web/frontend/src/components/agentic/useVoiceCall.ts
@@ -2,6 +2,7 @@ import { useCallback, useState } from "react";
import { useT } from "@/i18n";
import { requestVoiceCall, requestVoiceHangup } from "@/lib/voiceApi";
import { useEventStore, type VoiceState } from "@/store/events";
+import { useHomeStore } from "@/store/home";
/**
* Whether the next voice-control press should end the current conversation.
@@ -28,7 +29,9 @@ export function useVoiceCall() {
const t = useT();
const voiceState = (useEventStore((store) => store.voiceState) ?? "idle") as VoiceState;
const pushToast = useEventStore((store) => store.pushToast);
- const [busy, setBusy] = useState(false);
+ const [requestBusy, setBusy] = useState(false);
+ const selectionPending = useHomeStore((s) => s.voiceSelectionPending);
+ const busy = requestBusy || selectionPending;
const active = isVoiceActive(voiceState);
const toggleCall = useCallback(async () => {
diff --git a/jarvis/ui/web/frontend/src/components/society/chat/JarvisHistoryRail.test.tsx b/jarvis/ui/web/frontend/src/components/society/chat/JarvisHistoryRail.test.tsx
index 0d5a449026..c35e3d6da9 100644
--- a/jarvis/ui/web/frontend/src/components/society/chat/JarvisHistoryRail.test.tsx
+++ b/jarvis/ui/web/frontend/src/components/society/chat/JarvisHistoryRail.test.tsx
@@ -67,6 +67,7 @@ function json(body: unknown, status = 200) {
describe("JarvisHistoryRail", () => {
beforeEach(() => {
vi.clearAllMocks();
+ useHomeStore.setState({ voiceSelectionPending: false, voiceSwitchStopping: false });
useHomeStore.getState().resetTranscript();
Element.prototype.scrollIntoView = vi.fn();
vi.stubGlobal("WebSocket", FakeSocket);
@@ -105,6 +106,7 @@ describe("JarvisHistoryRail", () => {
messages: [],
activeThreadId: null,
activeKind: "text",
+ voiceState: "idle",
});
});
@@ -145,6 +147,7 @@ describe("JarvisHistoryRail", () => {
await screen.findByTestId("jarvis-history-voice-row");
vi.mocked(fetch).mockImplementationOnce(() => new Promise<Response>((resolve) => { finish = resolve; }));
fireEvent.click(screen.getByTestId("jarvis-history-voice-row"));
+ await waitFor(() => expect(finish).toBeTypeOf("function"));
await act(async () => {
useHomeStore.getState().ingest("VoiceSessionEnded", { hangup_reason: "hotkey" }, 5);
finish(json({ kind: "voice", id: "voice-1", messages: [{ role: "user", text: "Stale archive", ts_ms: 1 }] }));
@@ -154,6 +157,25 @@ describe("JarvisHistoryRail", () => {
expect(useEventStore.getState().messages).toEqual([]);
});
+ test("switching archives ends the active call before resuming and keeps the selected context", async () => {
+ useEventStore.setState({ voiceState: "listening" });
+ render(<JarvisHistoryRail />);
+ fireEvent.click(await screen.findByTestId("jarvis-history-voice-row"));
+ await waitFor(() => expect(fetch).toHaveBeenCalledWith("/api/voice/hangup", { method: "POST", cache: "no-store" }));
+ expect(vi.mocked(fetch).mock.calls.some(([url]) => String(url).endsWith("/resume"))).toBe(false);
+ expect(useHomeStore.getState().voiceSelectionPending).toBe(true);
+ await act(async () => {
+ useHomeStore.getState().ingest("VoiceSessionEnded", { hangup_reason: "hotkey" }, 3);
+ useEventStore.getState().setVoice("idle");
+ });
+ await waitFor(() => expect(useHomeStore.getState().transcript.map((line) => line.text)).toEqual([
+ "Previous spoken question", "Previous spoken answer",
+ ]));
+ expect(useEventStore.getState().activeThreadId).toBe("voice-1");
+ expect(useHomeStore.getState().freshVoicePending).toBe(false);
+ expect(useHomeStore.getState().voiceSelectionPending).toBe(false);
+ });
+
test("the new button clears whatever is open", async () => {
useAgentChatStore.setState({ activeSessionId: "chat-1" });
useEventStore.setState({ activeKind: "voice", activeThreadId: "voice-1" });
diff --git a/jarvis/ui/web/frontend/src/hooks/useConversations.ts b/jarvis/ui/web/frontend/src/hooks/useConversations.ts
index 347e83f27a..139d97a9fa 100644
--- a/jarvis/ui/web/frontend/src/hooks/useConversations.ts
+++ b/jarvis/ui/web/frontend/src/hooks/useConversations.ts
@@ -1,6 +1,8 @@
import { useCallback, useEffect } from "react";
import { useEventStore, type ChatMessage, type ConversationKind } from "@/store/events";
+import { useHomeStore } from "@/store/home";
+import { requestVoiceHangup } from "@/lib/voiceApi";
import {
deleteTextConversation,
detailToMessages,
@@ -13,6 +15,25 @@ import {
/** How often the history list is re-read while a poller is mounted. */
export const CONVERSATIONS_REFRESH_MS = 5000;
+let selectionGeneration = 0;
+
+/** Wait for the real session boundary rather than treating an accepted stop as completion. */
+function waitForVoiceIdle(): Promise<void> {
+ if (useEventStore.getState().voiceState === "idle") return Promise.resolve();
+ return new Promise((resolve, reject) => {
+ const timer = window.setTimeout(() => {
+ unsubscribe();
+ reject(new Error("The voice call has not ended yet. Please try again."));
+ }, 10_000);
+ const unsubscribe = useEventStore.subscribe((state) => {
+ if (state.voiceState !== "idle") return;
+ window.clearTimeout(timer);
+ unsubscribe();
+ resolve();
+ });
+ });
+}
+
/**
* The unified chat history (text threads + voice sessions) and the three
* things anyone does with it: open one, start a new one, delete one.
@@ -56,20 +77,37 @@ export function useConversations({ poll = false }: { poll?: boolean } = {}) {
*/
const openConversation = useCallback(
async (kind: ConversationKind, id: string): Promise<ChatMessage[]> => {
+ const generation = ++selectionGeneration;
+ useHomeStore.setState({ voiceSelectionPending: kind === "voice", freshVoicePending: false });
setActiveConversation(kind, id);
let messages: ChatMessage[] = [];
let traces = {};
try {
+ if (kind === "voice" && ["listening", "thinking", "speaking", "paused", "connecting"].includes(useEventStore.getState().voiceState)) {
+ useHomeStore.setState({ voiceSwitchStopping: true });
+ try {
+ await requestVoiceHangup();
+ await waitForVoiceIdle();
+ } finally {
+ useHomeStore.setState({ voiceSwitchStopping: false });
+ }
+ }
+ const selected = useEventStore.getState();
+ if (generation !== selectionGeneration || selected.activeKind !== kind || selected.activeThreadId !== id) return [];
const detail = await resumeConversation(kind, id);
messages = detailToMessages(detail);
traces = detailToTraces(detail);
- } catch {
- /* unreachable / gone — an empty thread is the honest view */
+ } catch (error) {
+ if (generation === selectionGeneration) {
+ useEventStore.getState().pushToast("error", error instanceof Error ? error.message : "Could not open conversation");
+ }
+ } finally {
+ if (generation === selectionGeneration) useHomeStore.setState({ voiceSelectionPending: false });
}
// The stored traces replace the previous conversation's, so a reply
// in the new thread never wears the steps of an old one.
const active = useEventStore.getState();
- if (active.activeKind !== kind || active.activeThreadId !== id) return [];
+ if (generation !== selectionGeneration || active.activeKind !== kind || active.activeThreadId !== id) return [];
seedThinkingTraces(traces);
setMessages(messages);
return messages;
diff --git a/jarvis/ui/web/frontend/src/lib/chatsApi.context.test.ts b/jarvis/ui/web/frontend/src/lib/chatsApi.context.test.ts
new file mode 100644
index 0000000000..3da932bf2d
--- /dev/null
+++ b/jarvis/ui/web/frontend/src/lib/chatsApi.context.test.ts
@@ -0,0 +1,23 @@
+import { afterEach, expect, it, vi } from "vitest";
+import { resumeConversation, startNewVoiceRun } from "./chatsApi";
+
+afterEach(() => vi.unstubAllGlobals());
+
+it("finishes an old resume before applying the next selection or a fresh context", async () => {
+ let finishFirst!: (response: Response) => void;
+ const requests: string[] = [];
+ vi.stubGlobal("fetch", vi.fn((url: string) => {
+ requests.push(url);
+ if (requests.length === 1) return new Promise<Response>((resolve) => { finishFirst = resolve; });
+ return Promise.resolve(new Response(JSON.stringify({ messages: [], cleared: true })));
+ }));
+ const first = resumeConversation("voice", "first");
+ const second = resumeConversation("voice", "second");
+ const fresh = startNewVoiceRun();
+ await vi.waitFor(() => expect(requests).toEqual(["/api/chats/voice/first/resume"]));
+ finishFirst(new Response(JSON.stringify({ messages: [] })));
+ await Promise.all([first, second, fresh]);
+ expect(requests).toEqual([
+ "/api/chats/voice/first/resume", "/api/chats/voice/second/resume", "/api/chats/voice/new",
+ ]);
+});
diff --git a/jarvis/ui/web/frontend/src/lib/chatsApi.ts b/jarvis/ui/web/frontend/src/lib/chatsApi.ts
index 12d2348523..bb8bc812a0 100644
--- a/jarvis/ui/web/frontend/src/lib/chatsApi.ts
+++ b/jarvis/ui/web/frontend/src/lib/chatsApi.ts
@@ -30,6 +30,15 @@ export class ChatsApiError extends Error {
}
}
+// All context-changing requests share one order. A slow archive load must
+// never seed its old context after a later selection or New voice chat.
+let contextQueue: Promise<unknown> = Promise.resolve();
+function changeContext<T>(request: () => Promise<T>): Promise<T> {
+ const result = contextQueue.then(request, request);
+ contextQueue = result.then(() => undefined, () => undefined);
+ return result;
+}
+
/**
* The voice/text history. `limit` is deliberately generous: the sidebar shows
* a handful, but the "All chats" archive promises everything, and a cap that
@@ -47,11 +56,13 @@ export async function resumeConversation(
kind: ConversationKind,
id: string,
): Promise<ConversationDetail> {
- const res = await fetch(`/api/chats/${kind}/${encodeURIComponent(id)}/resume`, {
- method: "POST",
+ return changeContext(async () => {
+ const res = await fetch(`/api/chats/${kind}/${encodeURIComponent(id)}/resume`, {
+ method: "POST",
+ });
+ if (!res.ok) throw new ChatsApiError("resume-failed", res.status);
+ return (await res.json()) as ConversationDetail;
});
- if (!res.ok) throw new ChatsApiError("resume-failed", res.status);
- return (await res.json()) as ConversationDetail;
}
export async function speakInConversation(
@@ -74,9 +85,11 @@ export async function startNewVoiceRun(): Promise<{
cleared: boolean;
ended: boolean;
}> {
- const res = await fetch("/api/chats/voice/new", { method: "POST" });
- if (!res.ok) throw new ChatsApiError("new-voice-run-failed", res.status);
- return (await res.json()) as { cleared: boolean; ended: boolean };
+ return changeContext(async () => {
+ const res = await fetch("/api/chats/voice/new", { method: "POST" });
+ if (!res.ok) throw new ChatsApiError("new-voice-run-failed", res.status);
+ return (await res.json()) as { cleared: boolean; ended: boolean };
+ });
}
export async function deleteTextConversation(id: string): Promise<void> {
diff --git a/jarvis/ui/web/frontend/src/store/home.ts b/jarvis/ui/web/frontend/src/store/home.ts
index 13fb942f9e..de74a96049 100644
--- a/jarvis/ui/web/frontend/src/store/home.ts
+++ b/jarvis/ui/web/frontend/src/store/home.ts
@@ -19,6 +19,8 @@ interface HomeStore {
jarvisCardMode: "voice" | "chat";
setJarvisCardMode: (mode: "voice" | "chat") => void;
freshVoicePending: boolean;
+ voiceSelectionPending: boolean;
+ voiceSwitchStopping: boolean;
surface: HomeSurface;
setSurface: (surface: HomeSurface) => void;
/** What was said and answered, oldest first (lib/homeTranscript.ts). */
@@ -79,6 +81,8 @@ export const useHomeStore = create<HomeStore>((set, get) => ({
jarvisCardMode: "voice",
setJarvisCardMode: (jarvisCardMode) => set({ jarvisCardMode }),
freshVoicePending: false,
+ voiceSelectionPending: false,
+ voiceSwitchStopping: false,
surface: readHomeSurface(),
setSurface: (surface) => {
writeHomeSurface(surface);
@@ -93,7 +97,7 @@ export const useHomeStore = create<HomeStore>((set, get) => ({
// empty lane; the completed conversation remains in the history rail.
if (reason !== "realtime_fallback" && reason !== "desktop_fallback") {
const events = useEventStore.getState();
- if (events.activeKind === "voice") {
+ if (events.activeKind === "voice" && !get().voiceSwitchStopping) {
events.setActiveConversation("voice", null);
events.setMessages([]);
events.seedThinkingTraces({});
@@ -101,7 +105,11 @@ export const useHomeStore = create<HomeStore>((set, get) => ({
events.setTranscription("", true);
// Remember this even while the card is closed. Returning to Jarvis
// must not restore the last archive selection or the typed surface.
- set({ transcript: [], liveReply: "", jarvisCardMode: "voice", freshVoicePending: true });
+ set({
+ transcript: [], liveReply: "", jarvisCardMode: "voice",
+ freshVoicePending: !get().voiceSwitchStopping,
+ voiceSelectionPending: get().voiceSwitchStopping && get().voiceSelectionPending,
+ });
return;
}
}
diff --git a/tests/unit/realtime/test_archive_resume.py b/tests/unit/realtime/test_archive_resume.py
new file mode 100644
index 0000000000..54a7ce03c8
--- /dev/null
+++ b/tests/unit/realtime/test_archive_resume.py
@@ -0,0 +1,116 @@
+"""An archived conversation reaches the real duplex handshake, not only the UI."""
+
+from __future__ import annotations
+
+import ast
+from pathlib import Path
+from typing import Any
+
+import pytest
+
+from jarvis.brain.manager import BrainManager
+from jarvis.core.bus import EventBus
+from jarvis.core.config import JarvisConfig
+from jarvis.realtime.session import RealtimeVoiceSession
+from tests.unit.state.test_chats_routes import _client, _make_app
+
+from .test_session import FakeProvider, _cfg
+
+
+def desktop_facade(brain: BrainManager) -> Any:
+ """Exercise the actual nested desktop facade without starting native UI/audio."""
+ source = Path(__file__).resolve().parents[3] / "jarvis/ui/desktop_app.py"
+ tree = ast.parse(source.read_text(encoding="utf-8-sig"))
+ facade = next(
+ node
+ for node in ast.walk(tree)
+ if isinstance(node, ast.ClassDef) and node.name == "_DeferredVoiceBrain"
+ )
+ module = ast.Module(
+ body=[
+ ast.ImportFrom(module="__future__", names=[ast.alias(name="annotations")], level=0),
+ facade,
+ ],
+ type_ignores=[],
+ )
+ namespace: dict[str, Any] = {"brain_holder": {"brain": brain}}
+ exec(compile(ast.fix_missing_locations(module), str(source), "exec"), namespace) # noqa: S102 - compile only the repository's own facade
+ return namespace["_DeferredVoiceBrain"]()
+
+
[email protected]("surface", ["desktop", "browser"])
+async def test_resume_route_seeds_provider_and_delegate_once(surface: str) -> None:
+ config = JarvisConfig()
+ config.brain.primary = "fake"
+ brain = BrainManager(config=config, bus=EventBus(), tools={})
+ app, _ = await _make_app(with_session=True)
+ app.state.brain = brain
+ async with _client(app) as client:
+ response = await client.post("/api/chats/voice/v1/resume")
+ assert response.status_code == 200
+ expected = (
+ {"role": "user", "text": "how is the weather"},
+ {"role": "assistant", "text": "Sunny."},
+ )
+ callback = desktop_facade(brain) if surface == "desktop" else brain
+ sessions: list[RealtimeVoiceSession] = []
+
+ async def send(_value: Any) -> None:
+ pass # Capture is unnecessary: the test inspects the actual provider input.
+
+ async def start(identifier: str) -> tuple[RealtimeVoiceSession, FakeProvider]:
+ provider = FakeProvider([])
+ session = RealtimeVoiceSession(
+ session_id=identifier,
+ send_binary=send,
+ send_json=send,
+ config=_cfg(),
+ provider=provider,
+ brain=callback,
+ surface=surface,
+ )
+ sessions.append(session)
+ await session.handle_control({"type": "audio_start", "sample_rate": 16000})
+ return session, provider
+
+ try:
+ first, provider = await start("resumed")
+ assert provider.opened_with.history == expected
+ assert [(message.role, message.content) for message in first._delegate_history] == [
+ (item["role"], item["text"]) for item in expected
+ ]
+ # A newer archive selection belongs to the next call. A duplicate start
+ # or provider reconnect in this call must retain its existing context.
+ brain.seed_history([("user", "The other topic is private jets.")])
+ await first.handle_control({"type": "audio_start", "sample_rate": 16000})
+ assert first._history_seed() == expected
+ _, second_provider = await start("different-archive")
+ assert second_provider.opened_with.history == (
+ {"role": "user", "text": "The other topic is private jets."},
+ )
+ _, fresh_provider = await start("fresh")
+ assert fresh_provider.opened_with.history == ()
+ finally:
+ for session in sessions:
+ await session.end(reason="client_stop")
+
+
+def test_clearing_history_discards_pending_voice_context() -> None:
+ brain = BrainManager(config=JarvisConfig(), bus=EventBus(), tools={})
+ brain.seed_history([("user", "Old archive")])
+ brain.clear_history()
+ assert brain.take_voice_history_seed() == ()
+ brain.seed_history([("user", "Another archive")])
+ brain.seed_history([])
+ assert brain.take_voice_history_seed() == ()
+
+
+def test_desktop_speak_seed_reaches_shared_brain() -> None:
+ brain = BrainManager(config=JarvisConfig(), bus=EventBus(), tools={})
+ facade = desktop_facade(brain)
+ facade.seed_history([("user", "Resume this exact conversation")])
+ assert [(message.role, message.content) for message in facade.take_voice_history_seed()] == [
+ ("user", "Resume this exact conversation")
+ ]
+ assert facade.take_voice_history_seed() == ()