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(browser): retain capture apartment across manual handover
rubenluetke10-beep3ee35abddace
diff --git a/docs/os-parity.md b/docs/os-parity.mdindex 2273f302d2..9fd4d21e22 100644--- a/docs/os-parity.md+++ b/docs/os-parity.md@@ -1552,6 +1552,11 @@ and restores agent automation only after explicit handover and clean Chromeshutdown. The selected Chrome executable remains pinned to that profile acrossrestarts; missing binaries and profile downgrades fail closed.+The Windows browser worker retains one COM MTA usage cookie across capture+sessions and releases it when the worker exits. This prevents the capture+library's cached WinRT factories from outliving their apartment during manual+handover. Each window still stops its capture and event threads on close.+macOS and Linux keep their existing managed browser behavior. They do not exposethis sign-in capability because the current native capture/input implementationis Windows-only. The module imports without Win32 dependencies on those systems;diff --git a/jarvis/society/browser/native_window.py b/jarvis/society/browser/native_window.pyindex 51536096f6..a0bd50fc22 100644--- a/jarvis/society/browser/native_window.py+++ b/jarvis/society/browser/native_window.py@@ -16,6 +16,45 @@ import timefrom typing import Anylog = logging.getLogger(__name__)+_capture_mta_lock = threading.Lock()+_capture_mta: tuple[Any, Any] | None = None+++def _release_capture_mta() -> None:+ global _capture_mta+ with _capture_mta_lock:+ retained, _capture_mta = _capture_mta, None+ if retained is not None:+ ole32, cookie = retained+ if ole32.CoDecrementMTAUsage(cookie) < 0:+ log.warning("Could not release the browser capture COM apartment")+++def _ensure_capture_mta() -> None:+ """Keep cached WGC factories valid across this worker's capture sessions.++ windows-capture 2.0.1 drops its MTA cookie after each session. Its cached+ WinRT factories can then reference an unloaded apartment on the next+ start (upstream issue 124). Retain one cookie until this worker exits;+ individual windows still stop and join all capture and event threads.+ """+ global _capture_mta+ import atexit # noqa: PLC0415+ import ctypes # noqa: PLC0415++ with _capture_mta_lock:+ if _capture_mta is not None:+ return+ ole32 = ctypes.WinDLL("ole32")+ ole32.CoIncrementMTAUsage.argtypes = [ctypes.POINTER(ctypes.c_void_p)]+ ole32.CoIncrementMTAUsage.restype = ctypes.c_long+ ole32.CoDecrementMTAUsage.argtypes = [ctypes.c_void_p]+ ole32.CoDecrementMTAUsage.restype = ctypes.c_long+ cookie = ctypes.c_void_p()+ if ole32.CoIncrementMTAUsage(ctypes.byref(cookie)) < 0:+ raise RuntimeError("The browser capture COM apartment is unavailable")+ _capture_mta = ole32, cookie+ atexit.register(_release_capture_mta)def available() -> bool:@@ -50,6 +89,7 @@ class NativeWindow:import ctypes # noqa: PLC0415from ctypes import wintypes # noqa: PLC0415+ _ensure_capture_mta()from windows_capture import WindowsCapture # type: ignore[import-not-found]self.ctypes = ctypesdiff --git a/tests/unit/society/test_browser_on_demand.py b/tests/unit/society/test_browser_on_demand.pyindex 26a764554d..3c73af86cb 100644--- a/tests/unit/society/test_browser_on_demand.py+++ b/tests/unit/society/test_browser_on_demand.py@@ -11,6 +11,7 @@ from uuid import uuid4import pytest+from jarvis.agent_chat.store import AgentChatStorefrom jarvis.society.browser import installfrom jarvis.society.browser import live as live_modulefrom jarvis.society.browser.tool import BrowserTool@@ -28,7 +29,11 @@ async def cold_browser(tmp_path, monkeypatch):monkeypatch.setattr(install, "runner_path", lambda: Path(browser_start_runner.__file__))monkeypatch.setattr(install, "browser_executable", lambda _: Path("unused-browser"))monkeypatch.setattr("jarvis.core.config.get_jarvis_agent_secret", lambda _: None)- runtime = SocietyRuntime(tmp_path, seed_starter_team=False)+ chat_store = AgentChatStore(tmp_path / "chat.sqlite")+ runtime = SocietyRuntime(+ tmp_path, seed_starter_team=False,+ chat_service=lambda: SimpleNamespace(store=chat_store),+ )await runtime.ensure_started()await runtime.roster.create(name="Scout", provider="ollama", model="fixture")@@ -43,6 +48,7 @@ async def cold_browser(tmp_path, monkeypatch):yield runtime, installsfinally:await runtime.close()+ chat_store.close()@pytest.mark.parametrize("session_id", ["society:scout", "society:scout:routine:task:run"])@@ -50,7 +56,10 @@ async def test_cold_agent_and_routine_start_reuse_and_reopen_browser(cold_browser, tmp_path, session_id):runtime, installs = cold_browser- session = SimpleNamespace(session_id=session_id, cwd=str(tmp_path), permission_mode="bypass")+ session = runtime.chat_service().store.create_session(+ session_id=session_id, surface="society", provider="ollama", model="fixture",+ effort="", cwd=str(tmp_path), permission_mode="bypass",+ )cfg = SimpleNamespace(wiki=SimpleNamespace(vault_root=str(tmp_path / "vault")))briefing = await society_system_extra(cfg, None, session)assert "starts the browser automatically" in briefingdiff --git a/tests/unit/society/test_native_browser_window.py b/tests/unit/society/test_native_browser_window.pyindex fc7f43c2de..a1efb6b30c 100644--- a/tests/unit/society/test_native_browser_window.py+++ b/tests/unit/society/test_native_browser_window.py@@ -29,6 +29,51 @@ def scene(monkeypatch):window.close()+def test_capture_apartment_survives_window_sessions_and_releases_once(monkeypatch):+ import atexit+ import ctypes++ retained, released, cleanup = [], [], []++ def increment(pointer):+ pointer._obj.value = 123+ retained.append(123)+ return 0++ ole32 = SimpleNamespace(+ CoIncrementMTAUsage=NativeCall(increment),+ CoDecrementMTAUsage=NativeCall(lambda cookie: released.append(cookie.value) or 0),+ )+ monkeypatch.setattr(ctypes, "WinDLL", lambda _: ole32, raising=False)+ monkeypatch.setattr(atexit, "register", cleanup.append)+ monkeypatch.setattr(native_window, "_capture_mta", None)+ native_window._ensure_capture_mta()+ native_window._ensure_capture_mta()+ assert retained == [123] and not released+ assert cleanup == [native_window._release_capture_mta]+ cleanup[0]()+ cleanup[0]()+ assert released == [123]+++def test_failed_capture_apartment_can_retry_without_retaining_a_cookie(monkeypatch):+ import atexit+ import ctypes++ cleanup = []+ ole32 = SimpleNamespace(+ CoIncrementMTAUsage=NativeCall(lambda _: -1),+ CoDecrementMTAUsage=NativeCall(lambda _: pytest.fail("No cookie was acquired")),+ )+ monkeypatch.setattr(ctypes, "WinDLL", lambda _: ole32, raising=False)+ monkeypatch.setattr(atexit, "register", cleanup.append)+ monkeypatch.setattr(native_window, "_capture_mta", None)+ for _ in range(2):+ with pytest.raises(RuntimeError, match="COM apartment"):+ native_window._ensure_capture_mta()+ assert native_window._capture_mta is None and not cleanup++def popup(window, desktop, hwnd=2, **kwargs):desktop.add(hwnd, **kwargs)window._events.put_nowait(hwnd)